]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ident.cpp
This needs some general QA-ing. Add support to new parser (introduced in 1.1) for...
[user/henk/code/inspircd.git] / src / modules / m_ident.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include <stdio.h>
15 #include <string>
16 #include "users.h"
17 #include "channels.h"
18 #include "modules.h"
19 #include "inspircd.h"
20
21 /* $ModDesc: Provides support for RFC 1413 ident lookups */
22
23 // Version 1.5.0.0 - Updated to use InspSocket, faster and neater.
24
25 /** Handles RFC1413 ident connections to users
26  */
27 class RFC1413 : public InspSocket
28 {
29  protected:
30         socklen_t uslen;         // length of our port number
31         socklen_t themlen;       // length of their port number
32         char ident_request[128]; // buffer used to make up the request string
33  public:
34
35         userrec* u;              // user record that the lookup is associated with
36         int ufd;
37
38         RFC1413(InspIRCd* SI, userrec* user, int maxtime) : InspSocket(SI, user->GetIPString(), 113, false, maxtime), u(user)
39         {
40                 ufd = user->GetFd();
41         }
42
43         virtual void OnTimeout()
44         {
45                 // When we timeout, the connection failed within the allowed timeframe,
46                 // so we just display a notice, and tidy off the ident_data.
47                 if (u && (Instance->SE->GetRef(ufd) == u))
48                 {
49                         u->Shrink("ident_data");
50                         Instance->next_call = Instance->Time();
51                 }
52         }
53
54         virtual bool OnDataReady()
55         {
56                 char* ibuf = this->Read();
57                 if (ibuf)
58                 {
59                         char* savept;
60                         char* section = strtok_r(ibuf,":",&savept);
61                         while (section)
62                         {
63                                 if (strstr(section,"USERID"))
64                                 {
65                                         section = strtok_r(NULL,":",&savept);
66                                         if (section)
67                                         {
68                                                 // ID type, usually UNIX or OTHER... we dont want it, so read the next token
69                                                 section = strtok_r(NULL,":",&savept);
70                                                 if (section)
71                                                 {
72                                                         while (*section == ' ') section++; // strip leading spaces
73                                                         for (char* j = section; *j; j++)
74                                                         if ((*j < 33) || (*j > 126))
75                                                                 *j = '\0'; // truncate at invalid chars
76                                                         if (*section)
77                                                         {
78                                                                 if (u && (Instance->SE->GetRef(ufd) == u))
79                                                                 {
80                                                                         if (this->Instance->IsIdent(section))
81                                                                         {
82                                                                                 strlcpy(u->ident,section,IDENTMAX);
83                                                                                 u->WriteServ("NOTICE "+std::string(u->nick)+" :*** Found your ident: "+std::string(u->ident));
84                                                                         }
85                                                                 }
86                                                         }
87                                                         return false;
88                                                 }
89                                         }
90                                 }
91                                 section = strtok_r(NULL,":",&savept);
92                         }
93                 }
94                 return false;
95         }
96
97         virtual void OnClose()
98         {
99                 // tidy up after ourselves when the connection is done.
100                 // We receive this event straight after a timeout, too.
101                 //
102                 //
103                 // OK, now listen up. The weird looking check here is
104                 // REQUIRED. Don't try and optimize it away.
105                 //
106                 // When a socket is closed, it is not immediately removed
107                 // from the socket list, there can be a short delay
108                 // before it is culled from the list. This means that
109                 // without this check, there is a chance that a user
110                 // may not exist when we come to ::Shrink them, which
111                 // results in a segfault. The value of "u" may not
112                 // always be NULL at this point, so, what we do is
113                 // check against the fd_ref_table, to see if (1) the user
114                 // exists, and (2) its the SAME user, on the same file
115                 // descriptor that they were when the lookup began.
116                 //
117                 // Fixes issue reported by webs, 7 Jun 2006
118                 if (u && (Instance->SE->GetRef(ufd) == u))
119                 {
120                         Instance->next_call = Instance->Time();
121                         u->Shrink("ident_data");
122                 }
123         }
124
125         virtual void OnError(InspSocketError e)
126         {
127                 if (u && (Instance->SE->GetRef(ufd) == u))
128                 {
129                         if (*u->ident == '~')
130                                 u->WriteServ("NOTICE "+std::string(u->nick)+" :*** Could not find your ident, using "+std::string(u->ident)+" instead.");
131
132                         Instance->next_call = Instance->Time();
133                         u->Shrink("ident_data");
134                 }
135         }
136
137         virtual bool OnConnected()
138         {
139                 if (u && (Instance->SE->GetRef(ufd) == u))
140                 {
141                         sockaddr* sock_us = new sockaddr[2];
142                         sockaddr* sock_them = new sockaddr[2];
143                         bool success = false;
144                         uslen = sizeof(sockaddr_in);
145                         themlen = sizeof(sockaddr_in);
146 #ifdef IPV6
147                         if (this->u->GetProtocolFamily() == AF_INET6)
148                         {
149                                 themlen = sizeof(sockaddr_in6);
150                                 uslen = sizeof(sockaddr_in6);
151                                 success = ((getsockname(this->u->GetFd(),sock_us,&uslen) || getpeername(this->u->GetFd(), sock_them, &themlen)));
152                         }
153                         else
154                                 success = ((getsockname(this->u->GetFd(),sock_us,&uslen) || getpeername(this->u->GetFd(), sock_them, &themlen)));
155 #else
156                         success = ((getsockname(this->u->GetFd(),sock_us,&uslen) || getpeername(this->u->GetFd(), sock_them, &themlen)));
157 #endif
158                         if (success)
159                         {
160                                 delete[] sock_us;
161                                 delete[] sock_them;
162                                 return false;
163                         }
164                         else
165                         {
166                                 // send the request in the following format: theirsocket,oursocket
167 #ifdef IPV6
168                                 if (this->u->GetProtocolFamily() == AF_INET6)
169                                         snprintf(ident_request,127,"%d,%d\r\n",ntohs(((sockaddr_in6*)sock_them)->sin6_port),ntohs(((sockaddr_in6*)sock_us)->sin6_port));
170                                 else
171                                         snprintf(ident_request,127,"%d,%d\r\n",ntohs(((sockaddr_in*)sock_them)->sin_port),ntohs(((sockaddr_in*)sock_us)->sin_port));
172 #else
173                                 snprintf(ident_request,127,"%d,%d\r\n",ntohs(((sockaddr_in*)sock_them)->sin_port),ntohs(((sockaddr_in*)sock_us)->sin_port));
174 #endif
175                                 this->Write(ident_request);
176                                 delete[] sock_us;
177                                 delete[] sock_them;
178                                 return true;
179                         }
180                 }
181                 else
182                 {
183                         Instance->next_call = Instance->Time();
184                         return true;
185                 }
186         }
187 };
188
189 class ModuleIdent : public Module
190 {
191
192         ConfigReader* Conf;
193
194         int IdentTimeout;
195
196  public:
197         void ReadSettings()
198         {
199                 Conf = new ConfigReader(ServerInstance);
200                 IdentTimeout = Conf->ReadInteger("ident","timeout",0,true);
201                 if (!IdentTimeout)
202                         IdentTimeout = 1;
203                 DELETE(Conf);
204         }
205
206         ModuleIdent(InspIRCd* Me)
207                 : Module::Module(Me)
208         {
209
210                 ReadSettings();
211         }
212
213         void Implements(char* List)
214         {
215                 List[I_OnCleanup] = List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnUserDisconnect] = 1;
216         }
217
218         virtual void OnRehash(userrec* user, const std::string &parameter)
219         {
220                 ReadSettings();
221         }
222
223         virtual int OnUserRegister(userrec* user)
224         {
225                 /*
226                  * when the new user connects, before they authenticate with USER/NICK/PASS, we do
227                  * their ident lookup. We do this by instantiating an object of type RFC1413, which
228                  * is derived from InspSocket, and inserting it into the socket engine using the
229                  * Server::AddSocket() call.
230                  */
231                 char newident[MAXBUF];
232                 strcpy(newident,"~");
233                 strlcat(newident,user->ident,IDENTMAX);
234                 strlcpy(user->ident,newident,IDENTMAX);
235                 
236
237                 user->WriteServ("NOTICE "+std::string(user->nick)+" :*** Looking up your ident...");
238                 RFC1413* ident = new RFC1413(ServerInstance, user, IdentTimeout);
239                 if ((ident->GetState() == I_CONNECTING) || (ident->GetState() == I_CONNECTED))
240                 {
241                         user->Extend("ident_data", (char*)ident);
242                 }
243                 else
244                 {
245                         user->WriteServ("NOTICE "+std::string(user->nick)+" :*** Could not find your ident, using "+std::string(user->ident)+" instead.");
246                         ServerInstance->next_call = ServerInstance->Time();
247                 }
248                 return 0;
249         }
250
251         virtual bool OnCheckReady(userrec* user)
252         {
253                 /*
254                  * The socket engine will clean up their ident request for us when it completes,
255                  * either due to timeout or due to closing, so, we just hold them until they dont
256                  * have an ident field any more.
257                  */
258                 RFC1413* ident;
259                 return (!user->GetExt("ident_data", ident));
260         }
261
262         virtual void OnCleanup(int target_type, void* item)
263         {
264                 if (target_type == TYPE_USER)
265                 {
266                         userrec* user = (userrec*)item;
267                         RFC1413* ident;
268                         if (user->GetExt("ident_data", ident))
269                         {
270                                 // FIX: If the user record is deleted, the socket wont be removed
271                                 // immediately so there is chance of the socket trying to write to
272                                 // a user which has now vanished! To prevent this, set ident::u
273                                 // to NULL and check it so that we dont write users who have gone away.
274                                 ident->u = NULL;
275                                 ServerInstance->SE->DelFd(ident);
276                                 //delete ident;
277                         }
278                 }
279         }
280
281         virtual void OnUserDisconnect(userrec* user)
282         {
283                 /*
284                  * when the user quits tidy up any ident lookup they have pending to keep things tidy.
285                  * When we call RemoveSocket, the abstractions tied into the system evnetually work their
286                  * way to RFC1459::OnClose(), which shrinks off the ident_data for us, so we dont need
287                  * to do it here. If we don't tidy this up, there may still be lingering idents for users
288                  * who have quit, as class RFC1459 is only loosely bound to userrec* via a pair of pointers
289                  * and this would leave at least one of the invalid ;)
290                  */
291                 RFC1413* ident;
292                 if (user->GetExt("ident_data", ident))
293                 {
294                         ident->u = NULL;
295                         ServerInstance->SE->DelFd(ident);
296                 }
297         }
298
299         virtual ~ModuleIdent()
300         {
301                 ServerInstance->next_call = ServerInstance->Time();
302         }
303
304         virtual Version GetVersion()
305         {
306                 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
307         }
308
309 };
310
311 class ModuleIdentFactory : public ModuleFactory
312 {
313  public:
314         ModuleIdentFactory()
315         {
316         }
317
318         ~ModuleIdentFactory()
319         {
320         }
321
322         virtual Module * CreateModule(InspIRCd* Me)
323         {
324                 return new ModuleIdent(Me);
325         }
326
327 };
328
329
330 extern "C" void * init_module( void )
331 {
332         return new ModuleIdentFactory;
333 }
334