]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ident.cpp
Started on -Wall - safe compile
[user/henk/code/inspircd.git] / src / modules / m_ident.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 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 Server *Srv;
39
40 // State engine constants. We have three states,
41 // connecting, waiting for data, and finished.
42
43 #define IDENT_STATE_CONNECT     1
44 #define IDENT_STATE_WAITDATA    2
45 #define IDENT_STATE_DONE        3
46
47 // Ident lookups are done by attaching an RFC1413 class to the
48 // userrec record using the Extensible system.
49 // The RFC1413 class is written especially for this module but
50 // it should be relatively standalone for anyone else who wishes
51 // to have a nonblocking ident lookup in a program :)
52 // the class operates on a simple state engine, each state of the
53 // connection incrementing a state counter, leading through to
54 // a concluding state which terminates the lookup.
55
56 class RFC1413
57 {
58  protected:
59         int fd;                  // file descriptor
60         userrec* u;              // user record that the lookup is associated with
61         sockaddr_in addr;        // address we're connecting to
62         in_addr addy;            // binary ip address
63         int state;               // state (this class operates on a state engine)
64         char ibuf[MAXBUF];       // input buffer
65         sockaddr_in sock_us;     // our port number
66         sockaddr_in sock_them;   // their port number
67         socklen_t uslen;         // length of our port number
68         socklen_t themlen;       // length of their port number
69         int nrecv;               // how many bytes we've received
70         time_t timeout_end;      // how long until the operation times out
71         bool timeout;            // true if we've timed out and should bail
72         char ident_request[128]; // buffer used to make up the request string
73  public:
74
75         // The destructor makes damn sure the socket is freed :)
76
77         ~RFC1413()
78         {
79                 if (this->fd != -1)
80                 {
81                         shutdown(this->fd,2);
82                         close(this->fd);
83                         this->fd = -1;
84                 }
85         }
86
87         // establish an ident connection, maxtime is the time to spend trying
88         // returns true if successful, false if something was catastrophically wrong.
89         // note that failed connects are not reported here but detected in RFC1413::Poll()
90         // as the socket is nonblocking
91
92         bool Connect(userrec* user, int maxtime)
93         {
94                 timeout_end = time(NULL)+maxtime;
95                 timeout = false;
96                 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
97                 {
98                         // theres been a boo-boo... no more fd's left for us, woe is me!
99                         Srv->Log(DEBUG,"Ident: socket failed for: "+std::string(user->ip));
100                         return false;
101                 }
102                 inet_aton(user->ip,&addy);
103                 addr.sin_family = AF_INET;
104                 addr.sin_addr = addy;
105                 addr.sin_port = htons(113);
106
107                 int flags;
108                 flags = fcntl(this->fd, F_GETFL, 0);
109                 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
110
111                 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
112                 {
113                         // theres been an error, but EINPROGRESS just means 'right, im on it, call me later'
114                         if (errno != EINPROGRESS)
115                         {
116                                 // ... so that error isnt fatal, like the rest.
117                                 Srv->Log(DEBUG,"Ident: connect failed for: "+std::string(user->ip));
118                                 shutdown(this->fd,2);
119                                 close(this->fd);
120                                 this->fd = -1;
121                                 return false;
122                         }
123                 }
124                 Srv->Log(DEBUG,"Ident: successful connect associated with user "+std::string(user->nick));
125                 this->u = user;
126                 this->state = IDENT_STATE_CONNECT;
127                 return true;
128         }
129
130         // Poll the socket to see if we have an ident result, and if we do apply it to the user.
131         // returns false if we cannot poll for some reason (e.g. timeout).
132
133         bool Poll()
134         {
135                 if (time(NULL) > timeout_end)
136                 {
137                         timeout = true;
138                         Srv->SendServ(u->fd,"NOTICE "+std::string(u->nick)+" :*** Could not find your ident, using "+std::string(u->ident)+" instead.");
139                         shutdown(this->fd,2);
140                         close(this->fd);
141                         this->fd = -1;
142                         return false;
143                 }
144                 pollfd polls;
145                 polls.fd = this->fd;
146                 if (state == IDENT_STATE_CONNECT)
147                 {
148                         // during state IDENT_STATE_CONNECT (leading up to the connect)
149                         // we're watching for writeability
150                         polls.events = POLLOUT;
151                 }
152                 else
153                 {
154                         // the rest of the time we're waiting for data
155                         // back on the socket, or a socket close
156                         polls.events = POLLIN;
157                 }
158                 int ret = poll(&polls,1,1);
159
160                 if (ret > 0)
161                 {
162                         switch (this->state)
163                         {
164                                 case IDENT_STATE_CONNECT:
165                                         uslen = sizeof(sock_us);
166                                         themlen = sizeof(sock_them);
167                                         if ((getsockname(this->u->fd,(sockaddr*)&sock_us,&uslen) || getpeername(this->u->fd, (sockaddr*)&sock_them, &themlen)))
168                                         {
169                                                 Srv->Log(DEBUG,"Ident: failed to get socket names, bailing to state 3");
170                                                 shutdown(this->fd,2);
171                                                 close(this->fd);
172                                                 this->fd = -1;
173                                                 state = IDENT_STATE_DONE;
174                                         }
175                                         else
176                                         {
177                                                 // send the request in the following format: theirsocket,oursocket
178                                                 snprintf(ident_request,127,"%d,%d\r\n",ntohs(sock_them.sin_port),ntohs(sock_us.sin_port));
179                                                 send(this->fd,ident_request,strlen(ident_request),0);
180                                                 Srv->Log(DEBUG,"Sent ident request, moving to state 2");
181                                                 state = IDENT_STATE_WAITDATA;
182                                         }
183                                 break;
184                                 case IDENT_STATE_WAITDATA:
185                                         nrecv = recv(this->fd,ibuf,sizeof(ibuf),0);
186                                         if (nrecv > 0)
187                                         {
188                                                 // we have the response line in the following format:
189                                                 // 6193, 23 : USERID : UNIX : stjohns
190                                                 // 6195, 23 : ERROR : NO-USER
191                                                 ibuf[nrecv] = '\0';
192                                                 Srv->Log(DEBUG,"Received ident response: "+std::string(ibuf));
193                                                 shutdown(this->fd,2);
194                                                 close(this->fd);
195                                                 this->fd = -1;
196                                                 char* savept;
197                                                 char* section = strtok_r(ibuf,":",&savept);
198                                                 while (section)
199                                                 {
200                                                         if (strstr(section,"USERID"))
201                                                         {
202                                                                 section = strtok_r(NULL,":",&savept);
203                                                                 if (section)
204                                                                 {
205                                                                         // ID type, usually UNIX or OTHER... we dont want it, so read the next token
206                                                                         section = strtok_r(NULL,":",&savept);
207                                                                         if (section)
208                                                                         {
209                                                                                 while ((*section == ' ') && (strlen(section)>0)) section++; // strip leading spaces
210                                                                                 int t = strlen(section);
211                                                                                 for (int j = 0; j < t; j++)
212                                                                                         if ((section[j] < 33) || (section[j]>126))
213                                                                                                 section[j] = '\0'; // truncate at invalid chars
214                                                                                 if (strlen(section))
215                                                                                 {
216                                                                                         strlcpy(u->ident,section,IDENTMAX);
217                                                                                         Srv->Log(DEBUG,"IDENT SET: "+std::string(u->ident));
218                                                                                         Srv->SendServ(u->fd,"NOTICE "+std::string(u->nick)+" :*** Found your ident: "+std::string(u->ident));
219                                                                                 }
220                                                                                 break;
221                                                                         }
222                                                                 }
223                                                         }
224                                                         section = strtok_r(NULL,":",&savept);
225                                                 }
226                                                 state = IDENT_STATE_DONE;
227                                         }
228                                 break;
229                                 case IDENT_STATE_DONE:
230                                         shutdown(this->fd,2);
231                                         close(this->fd);
232                                         this->fd = -1;
233                                         Srv->Log(DEBUG,"Ident lookup is complete!");
234                                 break;
235                                 default:
236                                         Srv->Log(DEBUG,"Ident: invalid ident state!!!");
237                                 break;
238                         }
239                 }
240                 return true;
241         }
242
243         // returns true if the operation is completed,
244         // either due to complete request, or a timeout
245
246         bool Done()
247         {
248                 return ((state == IDENT_STATE_DONE) || (timeout == true));
249         }
250 };
251
252 class ModuleIdent : public Module
253 {
254
255         ConfigReader* Conf;
256         int IdentTimeout;
257
258  public:
259         void ReadSettings()
260         {
261                 Conf = new ConfigReader;
262                 IdentTimeout = Conf->ReadInteger("ident","timeout",0,true);
263                 delete Conf;
264         }
265
266         ModuleIdent()
267         {
268                 Srv = new Server;
269                 ReadSettings();
270         }
271
272         virtual void OnRehash()
273         {
274                 ReadSettings();
275         }
276
277         virtual void OnUserRegister(userrec* user)
278         {
279                 // when the new user connects, before they authenticate with USER/NICK/PASS, we do
280                 // their ident lookup.
281
282                 RFC1413* ident = new RFC1413;
283                 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :*** Looking up your ident...");
284                 if (ident->Connect(user,IdentTimeout))
285                 {
286                         // attach the object to the user record
287                         user->Extend("ident_data",(char*)ident);
288                         // start it off polling (always good to have a head start)
289                         // because usually connect has completed by now
290                         ident->Poll();
291                 }
292                 else
293                 {
294                         // something went wrong, call an irc-ambulance!
295                         Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :*** Could not look up your ident.");
296                         delete ident;
297                 }
298         }
299
300         virtual bool OnCheckReady(userrec* user)
301         {
302                 RFC1413* ident = (RFC1413*)user->GetExt("ident_data");
303                 if (ident)
304                 {
305                         // this user has a pending ident lookup, poll it
306                         ident->Poll();
307                         // is it done?
308                         if (ident->Done())
309                         {
310                                 // their ident is done, zap the structures
311                                 Srv->Log(DEBUG,"Ident: removing ident gubbins");
312                                 user->Shrink("ident_data");
313                                 delete ident;
314                                 // ...and send them on their way
315                                 return true;
316                         }
317                         // nope, we hold them in this state, they dont go anywhere
318                         return false;
319                 }
320                 return true;
321         }
322
323         virtual void OnUserDisconnect(userrec* user)
324         {
325                 // when the user quits tidy up any ident lookup they have pending to keep things tidy
326                 // and to prevent a memory and FD leaks
327                 RFC1413* ident = (RFC1413*)user->GetExt("ident_data");
328                 if (ident)
329                 {
330                         delete ident;
331                         user->Shrink("ident_data");
332                 }
333         }
334         
335         virtual ~ModuleIdent()
336         {
337                 delete Srv;
338         }
339         
340         virtual Version GetVersion()
341         {
342                 return Version(1,0,0,1,VF_VENDOR);
343         }
344         
345 };
346
347 class ModuleIdentFactory : public ModuleFactory
348 {
349  public:
350         ModuleIdentFactory()
351         {
352         }
353         
354         ~ModuleIdentFactory()
355         {
356         }
357         
358         virtual Module * CreateModule()
359         {
360                 return new ModuleIdent;
361         }
362         
363 };
364
365
366 extern "C" void * init_module( void )
367 {
368         return new ModuleIdentFactory;
369 }
370