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