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