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