]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ident.cpp
f08f8c868635d34be7cd4ca12fe05ba6d05bd43d
[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 RFC1413 ident lookups */
20
21 class IdentRequestSocket : public EventHandler
22 {
23  private:
24
25          User *user;
26          InspIRCd* ServerInstance;
27          bool done;
28          std::string result;
29
30  public:
31
32         IdentRequestSocket(InspIRCd *Server, User* u, const std::string &bindip) : user(u), ServerInstance(Server), result(u->ident)
33         {
34                 /* connect here on instantiation, throw on immediate failure */
35
36                 socklen_t size = 0;
37
38 #ifdef IPV6
39                 bool v6 = false;
40                 if ((bindip.empty()) || bindip.find(':') != std::string::npos)
41                 v6 = true;
42
43                 if (v6)
44                         SetFd(socket(AF_INET6, SOCK_STREAM, 0));
45                 else
46 #endif
47                         SetFd(socket(AF_INET, SOCK_STREAM, 0));
48
49                 if (GetFd() == -1)
50                         throw ModuleException("Could not create socket");
51
52                 sockaddr* s = new sockaddr[2];
53                 sockaddr* addr = new sockaddr[2];
54         
55 #ifdef IPV6
56                 if (v6)
57                 {
58                         in6_addr addy;
59                         in6_addr n;
60                         if (inet_pton(AF_INET6, user->GetIPString(), &addy) > 0)
61                         {
62                                 ((sockaddr_in6*)addr)->sin6_family = AF_INET6;
63                                 memcpy(&((sockaddr_in6*)addr)->sin6_addr, &addy, sizeof(addy));
64                                 ((sockaddr_in6*)addr)->sin6_port = htons(113);
65                                 size = sizeof(sockaddr_in6);
66                                 inet_pton(AF_INET6, IP.c_str(), &n);
67                                 memcpy(&((sockaddr_in6*)s)->sin6_addr, &n, sizeof(sockaddr_in6));
68                                 ((sockaddr_in6*)s)->sin6_port = 0;
69                                 ((sockaddr_in6*)s)->sin6_family = AF_INET6;
70                         }
71                 }
72                 else
73 #endif
74                 {
75                         in_addr addy;
76                         in_addr n;
77                         if (inet_aton(user->GetIPString(), &addy) > 0)
78                         {
79                                 ((sockaddr_in*)addr)->sin_family = AF_INET;
80                                 ((sockaddr_in*)addr)->sin_addr = addy;
81                                 ((sockaddr_in*)addr)->sin_port = htons(113);
82                                 size = sizeof(sockaddr_in);
83                                 inet_aton(bindip.c_str(), &n);
84                                 ((sockaddr_in*)s)->sin_addr = n;
85                                 ((sockaddr_in*)s)->sin_port = 0;
86                                 ((sockaddr_in*)s)->sin_family = AF_INET;
87                         }
88                 }
89
90                 if (ServerInstance->SE->Bind(GetFd(), s, size) < 0)
91                 {
92                         this->Close();
93                         delete[] s;
94                         throw ModuleException("failed to bind()");
95                 }
96
97                 delete[] s;
98                 ServerInstance->SE->NonBlocking(GetFd());
99
100                 if (ServerInstance->SE->Connect(this, (sockaddr*)addr, size) == -1 && errno != EINPROGRESS)
101                 {
102                         this->Close();
103                         delete[] addr;
104                         throw ModuleException("connect() failed");
105                 }
106
107                 delete[] addr;
108
109                 if (!ServerInstance->SE->AddFd(this))
110                 {
111                         this->Close();
112                         throw ModuleException("out of fds");
113                 }
114
115                 ServerInstance->SE->WantWrite(this);
116         }
117
118         virtual void OnConnected()
119         {
120                 ServerInstance->Log(DEBUG,"OnConnected()");
121
122                 /* Both sockaddr_in and sockaddr_in6 can be safely casted to sockaddr, especially since the
123                  * only members we use are in a part of the struct that should always be identical (at the
124                  * byte level). */
125
126                 #ifndef IPV6
127                 sockaddr_in laddr, raddr;
128                 #else
129                 sockaddr_in6 laddr, raddr;
130                 #endif
131
132                 socklen_t laddrsz = sizeof(laddr);
133                 socklen_t raddrsz = sizeof(raddr);
134
135                 if ((getsockname(user->GetFd(), (sockaddr*) &laddr, &laddrsz) != 0) || (getpeername(user->GetFd(), (sockaddr*) &raddr, &raddrsz) != 0))
136                 {
137                         done = true;
138                         return;
139                 }
140
141                 char req[32];
142
143                 #ifndef IPV6
144                 int req_size = snprintf(req, sizeof(req), "%d,%d\r\n", ntohs(raddr.sin_port), ntohs(laddr.sin_port));
145                 #else
146                 int req_size = snprintf(req, sizeof(req), "%d,%d\r\n", ntohs(raddr.sin6_port), ntohs(laddr.sin6_port));
147                 #endif
148                 
149                 if (ServerInstance->SE->Send(this, req, req_size, 0) < req_size)
150                         done = true;
151         }
152
153         virtual void HandleEvent(EventType et, int errornum = 0)
154         {
155                 switch (et)
156                 {
157                         case EVENT_READ:
158                                 /* fd readable event, received ident response */
159                                 ReadResponse();
160                         break;
161                         case EVENT_WRITE:
162                                 /* fd writeable event, successfully connected! */
163                                 OnConnected();
164                         break;
165                         case EVENT_ERROR:
166                                 /* fd error event, ohshi- */
167                                 ServerInstance->Log(DEBUG,"EVENT_ERROR");
168                                 Close();
169                                 done = true;
170                         break;
171                 }
172         }
173
174         void Close()
175         {
176                 if (GetFd() > -1)
177                 {
178                         ServerInstance->SE->Close(GetFd());
179                         ServerInstance->SE->Shutdown(GetFd(), SHUT_WR);
180                         ServerInstance->SE->DelFd(this);
181                 }
182         }
183
184         bool HasResult()
185         {
186                 return done;
187         }
188
189         const char* GetResult()
190         {
191                 return result.c_str();
192         }
193
194         void ReadResponse()
195         {
196                 ServerInstance->Log(DEBUG,"ReadResponse()");
197
198                 // We don't really need to buffer for incomplete replies here, since IDENT replies are
199                 // extremely short - there is *no* sane reason it'd be in more than one packet
200
201                 char ibuf[MAXBUF];
202                 int recvresult = ServerInstance->SE->Recv(this, ibuf, MAXBUF-1, 0);
203
204                 /* Cant possibly be a valid response shorter than 3 chars */
205                 if (recvresult < 3)
206                 {
207                         done = true;
208                         return;
209                 }
210
211                 irc::sepstream sep(ibuf, ':');
212                 std::string token;
213                 for (int i = 0; sep.GetToken(token); i++)
214                 {
215                         // We only really care about the 4th portion
216                         if (i < 3)
217                                 continue;
218
219                         char ident[IDENTMAX + 2];
220
221                         // Truncate the ident at any characters we don't like, skip leading spaces
222                         int k = 0;
223                         for (const char *j = token.c_str(); *j && (k < IDENTMAX + 1); j++)
224                         {
225                                 if (*j == ' ')
226                                         continue;
227
228                                 // Rules taken from InspIRCd::IsIdent
229                                 if (((*j >= 'A') && (*j <= '}')) || ((*j >= '0') && (*j <= '9')) || (*j == '-') || (*j == '.'))
230                                 {
231                                         ident[k++] = *j;
232                                         continue;
233                                 }
234
235                                 break;
236                         }
237
238                         ident[k] = '\0';
239
240                         // Redundant check with IsIdent, in case that changes and this doesn't (paranoia!)
241                         if (*ident && ServerInstance->IsIdent(ident))
242                         {
243                                 result = ident;
244                                 ServerInstance->next_call = ServerInstance->Time();
245                         }
246
247                         break;
248                 }
249
250                 Close();
251                 done = true;
252                 return;
253         }
254 };
255
256 class ModuleIdent : public Module
257 {
258  private:
259         int RequestTimeout;
260  public:
261         ModuleIdent(InspIRCd *Me)
262                 : Module(Me)
263         {
264                 OnRehash(NULL, "");
265         }
266         
267         virtual Version GetVersion()
268         {
269                 return Version(1, 1, 1, 0, VF_VENDOR, API_VERSION);
270         }
271         
272         virtual void Implements(char *List)
273         {
274                 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnCleanup] = List[I_OnUserDisconnect] = 1;
275         }
276         
277         virtual void OnRehash(User *user, const std::string &param)
278         {
279                 ConfigReader MyConf(ServerInstance);
280                 
281                 RequestTimeout = MyConf.ReadInteger("ident", "timeout", 0, true);
282                 if (!RequestTimeout)
283                         RequestTimeout = 5;
284         }
285         
286         virtual int OnUserRegister(User *user)
287         {
288                 /* User::ident is currently the username field from USER; with m_ident loaded, that
289                  * should be preceded by a ~. The field is actually IDENTMAX+2 characters wide. */
290                 memmove(user->ident + 1, user->ident, IDENTMAX);
291                 user->ident[0] = '~';
292                 // Ensure that it is null terminated
293                 user->ident[IDENTMAX + 1] = '\0';
294
295                 user->WriteServ("NOTICE Auth :*** Looking up your ident...");
296
297                 // Get the IP that the user is connected to, and bind to that for the outgoing connection
298                 #ifndef IPV6
299                 sockaddr_in laddr;
300                 #else
301                 sockaddr_in6 laddr;
302                 #endif
303                 socklen_t laddrsz = sizeof(laddr);
304
305                 if (getsockname(user->GetFd(), (sockaddr*) &laddr, &laddrsz) != 0)
306                 {
307                         user->WriteServ("NOTICE Auth :*** Could not find your ident, using %s instead.", user->ident);
308                         return 0;
309                 }
310
311                 #ifndef IPV6
312                 const char *ip = inet_ntoa(laddr.sin_addr);
313                 #else
314                 char ip[INET6_ADDRSTRLEN + 1];
315                 inet_ntop(laddr.sin6_family, &laddr.sin6_addr, ip, INET6_ADDRSTRLEN);
316                 #endif
317
318                 IdentRequestSocket *isock = NULL;
319                 try
320                 {
321                         isock = new IdentRequestSocket(ServerInstance, user, ip);
322                 }
323                 catch (ModuleException &e)
324                 {
325                         ServerInstance->Log(DEBUG,"Ident exception: %s", e.GetReason());
326                         return 0;
327                 }
328
329                 user->Extend("ident_socket", isock);
330                 return 0;
331         }
332
333         virtual bool OnCheckReady(User *user)
334         {
335                 ServerInstance->Log(DEBUG,"OnCheckReady %s", user->nick);
336
337                 /* Does user have an ident socket attached at all? */
338                 IdentRequestSocket *isock = NULL;
339                 if (!user->GetExt("ident_socket", isock))
340                 {
341                         ServerInstance->Log(DEBUG, "No ident socket :(");
342                         return true;
343                 }
344
345                 ServerInstance->Log(DEBUG, "Has ident_socket");
346
347                 if (isock->age + RequestTimeout > ServerInstance->Time() && !isock->HasResult())
348                 {
349                         /* Ident timeout */
350                         user->WriteServ("NOTICE Auth :*** Ident request timed out.");
351                         ServerInstance->Log(DEBUG, "Timeout");
352                         OnUserDisconnect(user);
353                         return true;
354                 }
355
356                 /* Got a result yet? */
357                 if (!isock->HasResult())
358                 {
359                         ServerInstance->Log(DEBUG, "No result yet");
360                         return false;
361                 }
362
363                 ServerInstance->Log(DEBUG, "Yay, result!");
364
365                 /* wooo, got a result! */
366                 if (*(isock->GetResult()) != '~')
367                         user->WriteServ("NOTICE Auth :*** Found your ident, '%s'", isock->GetResult());
368                 else
369                         user->WriteServ("NOTICE Auth :*** Could not find your ident, using %s instead.", isock->GetResult());
370
371                 strlcpy(user->ident, isock->GetResult(), IDENTMAX+1);
372                 OnUserDisconnect(user);
373                 return true;
374         }
375
376         virtual void OnCleanup(int target_type, void *item)
377         {
378                 /* Module unloading, tidy up users */
379                 if (target_type == TYPE_USER)
380                         OnUserDisconnect((User*)item);
381         }
382
383         virtual void OnUserDisconnect(User *user)
384         {
385                 /* User disconnect (generic socket detatch event) */
386                 IdentRequestSocket *isock = NULL;
387                 if (user->GetExt("ident_socket", isock))
388                 {
389                         isock->Close();
390                         delete isock;
391                         user->Shrink("ident_socket");
392                         ServerInstance->Log(DEBUG, "Removed ident socket from %s", user->nick);
393                 }
394         }
395 };
396
397 MODULE_INIT(ModuleIdent);
398