]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_ident.cpp
723b30c870e35df2ffbd80c75fe8a960e1be0984
[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                         this->SetFd(-1);
182                 }
183         }
184
185         bool HasResult()
186         {
187                 return done;
188         }
189
190         const char* GetResult()
191         {
192                 return result.c_str();
193         }
194
195         void ReadResponse()
196         {
197                 ServerInstance->Log(DEBUG,"ReadResponse()");
198
199                 // We don't really need to buffer for incomplete replies here, since IDENT replies are
200                 // extremely short - there is *no* sane reason it'd be in more than one packet
201
202                 char ibuf[MAXBUF];
203                 int recvresult = ServerInstance->SE->Recv(this, ibuf, MAXBUF-1, 0);
204
205                 /* Cant possibly be a valid response shorter than 3 chars */
206                 if (recvresult < 3)
207                 {
208                         done = true;
209                         return;
210                 }
211
212                 irc::sepstream sep(ibuf, ':');
213                 std::string token;
214                 for (int i = 0; sep.GetToken(token); i++)
215                 {
216                         // We only really care about the 4th portion
217                         if (i < 3)
218                                 continue;
219
220                         char ident[IDENTMAX + 2];
221
222                         // Truncate the ident at any characters we don't like, skip leading spaces
223                         int k = 0;
224                         for (const char *j = token.c_str(); *j && (k < IDENTMAX + 1); j++)
225                         {
226                                 if (*j == ' ')
227                                         continue;
228
229                                 // Rules taken from InspIRCd::IsIdent
230                                 if (((*j >= 'A') && (*j <= '}')) || ((*j >= '0') && (*j <= '9')) || (*j == '-') || (*j == '.'))
231                                 {
232                                         ident[k++] = *j;
233                                         continue;
234                                 }
235
236                                 break;
237                         }
238
239                         ident[k] = '\0';
240
241                         // Redundant check with IsIdent, in case that changes and this doesn't (paranoia!)
242                         if (*ident && ServerInstance->IsIdent(ident))
243                         {
244                                 result = ident;
245                                 ServerInstance->next_call = ServerInstance->Time();
246                         }
247
248                         break;
249                 }
250
251                 Close();
252                 done = true;
253                 return;
254         }
255 };
256
257 class ModuleIdent : public Module
258 {
259  private:
260         int RequestTimeout;
261  public:
262         ModuleIdent(InspIRCd *Me)
263                 : Module(Me)
264         {
265                 OnRehash(NULL, "");
266         }
267         
268         virtual Version GetVersion()
269         {
270                 return Version(1, 1, 1, 0, VF_VENDOR, API_VERSION);
271         }
272         
273         virtual void Implements(char *List)
274         {
275                 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnCleanup] = List[I_OnUserDisconnect] = 1;
276         }
277         
278         virtual void OnRehash(User *user, const std::string &param)
279         {
280                 ConfigReader MyConf(ServerInstance);
281                 
282                 RequestTimeout = MyConf.ReadInteger("ident", "timeout", 0, true);
283                 if (!RequestTimeout)
284                         RequestTimeout = 5;
285         }
286         
287         virtual int OnUserRegister(User *user)
288         {
289                 /* User::ident is currently the username field from USER; with m_ident loaded, that
290                  * should be preceded by a ~. The field is actually IDENTMAX+2 characters wide. */
291                 memmove(user->ident + 1, user->ident, IDENTMAX);
292                 user->ident[0] = '~';
293                 // Ensure that it is null terminated
294                 user->ident[IDENTMAX + 1] = '\0';
295
296                 user->WriteServ("NOTICE Auth :*** Looking up your ident...");
297
298                 // Get the IP that the user is connected to, and bind to that for the outgoing connection
299                 #ifndef IPV6
300                 sockaddr_in laddr;
301                 #else
302                 sockaddr_in6 laddr;
303                 #endif
304                 socklen_t laddrsz = sizeof(laddr);
305
306                 if (getsockname(user->GetFd(), (sockaddr*) &laddr, &laddrsz) != 0)
307                 {
308                         user->WriteServ("NOTICE Auth :*** Could not find your ident, using %s instead.", user->ident);
309                         return 0;
310                 }
311
312                 #ifndef IPV6
313                 const char *ip = inet_ntoa(laddr.sin_addr);
314                 #else
315                 char ip[INET6_ADDRSTRLEN + 1];
316                 inet_ntop(laddr.sin6_family, &laddr.sin6_addr, ip, INET6_ADDRSTRLEN);
317                 #endif
318
319                 IdentRequestSocket *isock = NULL;
320                 try
321                 {
322                         isock = new IdentRequestSocket(ServerInstance, user, ip);
323                 }
324                 catch (ModuleException &e)
325                 {
326                         ServerInstance->Log(DEBUG,"Ident exception: %s", e.GetReason());
327                         return 0;
328                 }
329
330                 user->Extend("ident_socket", isock);
331                 return 0;
332         }
333
334         virtual bool OnCheckReady(User *user)
335         {
336                 ServerInstance->Log(DEBUG,"OnCheckReady %s", user->nick);
337
338                 /* Does user have an ident socket attached at all? */
339                 IdentRequestSocket *isock = NULL;
340                 if (!user->GetExt("ident_socket", isock))
341                 {
342                         ServerInstance->Log(DEBUG, "No ident socket :(");
343                         return true;
344                 }
345
346                 ServerInstance->Log(DEBUG, "Has ident_socket");
347
348                 if (isock->age + RequestTimeout > ServerInstance->Time() && !isock->HasResult())
349                 {
350                         /* Ident timeout */
351                         user->WriteServ("NOTICE Auth :*** Ident request timed out.");
352                         ServerInstance->Log(DEBUG, "Timeout");
353                         OnUserDisconnect(user);
354                         return true;
355                 }
356
357                 /* Got a result yet? */
358                 if (!isock->HasResult())
359                 {
360                         ServerInstance->Log(DEBUG, "No result yet");
361                         return false;
362                 }
363
364                 ServerInstance->Log(DEBUG, "Yay, result!");
365
366                 /* wooo, got a result! */
367                 if (*(isock->GetResult()) != '~')
368                         user->WriteServ("NOTICE Auth :*** Found your ident, '%s'", isock->GetResult());
369                 else
370                         user->WriteServ("NOTICE Auth :*** Could not find your ident, using %s instead.", isock->GetResult());
371
372                 strlcpy(user->ident, isock->GetResult(), IDENTMAX+1);
373                 OnUserDisconnect(user);
374                 return true;
375         }
376
377         virtual void OnCleanup(int target_type, void *item)
378         {
379                 /* Module unloading, tidy up users */
380                 if (target_type == TYPE_USER)
381                         OnUserDisconnect((User*)item);
382         }
383
384         virtual void OnUserDisconnect(User *user)
385         {
386                 /* User disconnect (generic socket detatch event) */
387                 IdentRequestSocket *isock = NULL;
388                 if (user->GetExt("ident_socket", isock))
389                 {
390                         isock->Close();
391                         delete isock;
392                         user->Shrink("ident_socket");
393                         ServerInstance->Log(DEBUG, "Removed ident socket from %s", user->nick);
394                 }
395         }
396 };
397
398 MODULE_INIT(ModuleIdent);
399