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