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