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