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