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