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