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