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