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