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