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