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