]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ident.cpp
1e9933516d4a87237ee70fe0b28999d27e08624a
[user/henk/code/inspircd.git] / src / modules / m_ident.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include <string>
21 #include <stdlib.h>
22 #include <time.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/time.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <poll.h>
31 #include "users.h"
32 #include "channels.h"
33 #include "modules.h"
34 #include "inspircd.h"
35
36 /* $ModDesc: Provides support for RFC 1413 ident lookups */
37
38 // Version 1.5.0.0 - Updated to use InspSocket, faster and neater.
39
40 class RFC1413 : public InspSocket
41 {
42  protected:
43         Server* Srv;             // Server* class used for core communications
44         userrec* u;              // user record that the lookup is associated with
45         sockaddr_in sock_us;     // our port number
46         sockaddr_in sock_them;   // their port number
47         socklen_t uslen;         // length of our port number
48         socklen_t themlen;       // length of their port number
49         char ident_request[128]; // buffer used to make up the request string
50  public:
51
52         // The destructor makes damn sure the socket is freed :)
53         RFC1413(userrec* user, int maxtime, Server* S) : InspSocket(user->host, 113, false, maxtime)
54         {
55                 Srv->Log(DEBUG,"Ident: associated with user "+std::string(user->nick));
56                 u = user;
57                 Srv = S;
58         }
59
60         virtual void OnTimeout()
61         {
62                 // When we timeout, the connection failed within the allowed timeframe,
63                 // so we just display a notice, and tidy off the ident_data.
64                 u->Shrink("ident_data");
65                 Srv->SendServ(u->fd,"NOTICE "+std::string(u->nick)+" :*** Could not find your ident, using "+std::string(u->ident)+" instead.");
66         }
67
68         virtual bool OnDataReady()
69         {
70                 std::string databuf = this->Read();
71                 if (databuf != "")
72                 {
73                         char ibuf[1024];
74                         strlcpy(ibuf,databuf.c_str(),1024);
75                         Srv->Log(DEBUG,"Received ident response");
76                         char* savept;
77                         char* section = strtok_r(ibuf,":",&savept);
78                         while (section)
79                         {
80                                 if (strstr(section,"USERID"))
81                                 {
82                                         section = strtok_r(NULL,":",&savept);
83                                         if (section)
84                                         {
85                                                 // ID type, usually UNIX or OTHER... we dont want it, so read the next token
86                                                 section = strtok_r(NULL,":",&savept);
87                                                 if (section)
88                                                 {
89                                                         while (*section == ' ') section++; // strip leading spaces
90                                                         int t = strlen(section);
91                                                         for (int j = 0; j < t; j++)
92                                                         if ((section[j] < 33) || (section[j]>126))
93                                                                 section[j] = '\0'; // truncate at invalid chars
94                                                         if (strlen(section))
95                                                         {
96                                                                 strlcpy(u->ident,section,IDENTMAX);
97                                                                 Srv->Log(DEBUG,"IDENT SET: "+std::string(u->ident));
98                                                                 Srv->SendServ(u->fd,"NOTICE "+std::string(u->nick)+" :*** Found your ident: "+std::string(u->ident));
99                                                         }
100                                                         return false;
101                                                 }
102                                         }
103                                 }
104                                 section = strtok_r(NULL,":",&savept);
105                         }
106                 }
107                 return false;
108         }
109
110         virtual void OnClose()
111         {
112                 // tidy up after ourselves when the connection is done.
113                 // We receive this event straight after a timeout, too.
114                 u->Shrink("ident_data");
115         }
116
117         virtual bool OnConnected()
118         {
119                 uslen = sizeof(sock_us);
120                 themlen = sizeof(sock_them);
121                 if ((getsockname(this->u->fd,(sockaddr*)&sock_us,&uslen) || getpeername(this->u->fd, (sockaddr*)&sock_them, &themlen)))
122                 {
123                         Srv->Log(DEBUG,"Ident: failed to get socket names, bailing");
124                         return false;
125                 }
126                 else
127                 {
128                         // send the request in the following format: theirsocket,oursocket
129                         snprintf(ident_request,127,"%d,%d\r\n",ntohs(sock_them.sin_port),ntohs(sock_us.sin_port));
130                         this->Write(ident_request);
131                         Srv->Log(DEBUG,"Sent ident request, waiting for reply");
132                         return true;
133                 }
134         }
135 };
136
137 class ModuleIdent : public Module
138 {
139
140         ConfigReader* Conf;
141         Server* Srv;
142         int IdentTimeout;
143
144  public:
145         void ReadSettings()
146         {
147                 Conf = new ConfigReader;
148                 IdentTimeout = Conf->ReadInteger("ident","timeout",0,true);
149                 delete Conf;
150         }
151
152         ModuleIdent(Server* Me)
153                 : Module::Module(Me)
154         {
155                 Srv = Me;
156                 ReadSettings();
157         }
158
159         void Implements(char* List)
160         {
161                 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnUserDisconnect] = 1;
162         }
163
164         virtual void OnRehash(std::string parameter)
165         {
166                 ReadSettings();
167         }
168
169         virtual void OnUserRegister(userrec* user)
170         {
171                 // when the new user connects, before they authenticate with USER/NICK/PASS, we do
172                 // their ident lookup. We do this by instantiating an object of type RFC1413, which
173                 // is derived from InspSocket, and inserting it into the socket engine using the
174                 // Server::AddSocket() call.
175                 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :*** Looking up your ident...");
176                 RFC1413* ident = new RFC1413(user, IdentTimeout, Srv);
177                 user->Extend("ident_data", (char*)ident);
178                 Srv->AddSocket(ident);
179         }
180
181         virtual bool OnCheckReady(userrec* user)
182         {
183                 // The socket engine will clean up their ident request for us when it completes,
184                 // either due to timeout or due to closing, so, we just hold them until they dont
185                 // have an ident field any more.
186                 RFC1413* ident = (RFC1413*)user->GetExt("ident_data");
187                 return (!ident);
188         }
189
190         virtual void OnUserDisconnect(userrec* user)
191         {
192                 // when the user quits tidy up any ident lookup they have pending to keep things tidy.
193                 // When we call RemoveSocket, the abstractions tied into the system evnetually work their
194                 // way to RFC1459::OnClose(), which shrinks off the ident_data for us, so we dont need
195                 // to do it here. If we don't tidy this up, there may still be lingering idents for users
196                 // who have quit, as class RFC1459 is only loosely bound to userrec* via a pair of pointers
197                 // and this would leave at least one of the invalid ;)
198                 RFC1413* ident = (RFC1413*)user->GetExt("ident_data");
199                 if (ident)
200                 {
201                         Srv->RemoveSocket(ident);
202                 }
203         }
204         
205         virtual ~ModuleIdent()
206         {
207         }
208         
209         virtual Version GetVersion()
210         {
211                 return Version(1,5,0,0,VF_VENDOR);
212         }
213         
214 };
215
216 class ModuleIdentFactory : public ModuleFactory
217 {
218  public:
219         ModuleIdentFactory()
220         {
221         }
222         
223         ~ModuleIdentFactory()
224         {
225         }
226         
227         virtual Module * CreateModule(Server* Me)
228         {
229                 return new ModuleIdent(Me);
230         }
231         
232 };
233
234
235 extern "C" void * init_module( void )
236 {
237         return new ModuleIdentFactory;
238 }
239