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