1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
19 /* $ModDesc: Provides support for RFC1413 ident lookups */
21 class IdentRequestSocket : public EventHandler
26 InspIRCd* ServerInstance;
32 IdentRequestSocket(InspIRCd *Server, User* u, const std::string &bindip) : user(u), ServerInstance(Server), result(u->ident)
34 /* connect here on instantiation, throw on immediate failure */
40 if ((bindip.empty()) || bindip.find(':') != std::string::npos)
44 SetFd(socket(AF_INET6, SOCK_STREAM, 0));
47 SetFd(socket(AF_INET, SOCK_STREAM, 0));
50 throw ModuleException("Could not create socket");
52 sockaddr* s = new sockaddr[2];
53 sockaddr* addr = new sockaddr[2];
60 if (inet_pton(AF_INET6, user->GetIPString(), &addy) > 0)
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;
77 if (inet_aton(user->GetIPString(), &addy) > 0)
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;
90 if (ServerInstance->SE->Bind(GetFd(), s, size) < 0)
94 throw ModuleException("failed to bind()");
98 ServerInstance->SE->NonBlocking(GetFd());
100 if (ServerInstance->SE->Connect(this, (sockaddr*)addr, size) == -1 && errno != EINPROGRESS)
104 throw ModuleException("connect() failed");
109 if (!ServerInstance->SE->AddFd(this))
112 throw ModuleException("out of fds");
115 ServerInstance->SE->WantWrite(this);
118 virtual void OnConnected()
120 ServerInstance->Log(DEBUG,"OnConnected()");
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
127 sockaddr_in laddr, raddr;
129 sockaddr_in6 laddr, raddr;
132 socklen_t laddrsz = sizeof(laddr);
133 socklen_t raddrsz = sizeof(raddr);
135 if ((getsockname(user->GetFd(), (sockaddr*) &laddr, &laddrsz) != 0) || (getpeername(user->GetFd(), (sockaddr*) &raddr, &raddrsz) != 0))
144 int req_size = snprintf(req, sizeof(req), "%d,%d\r\n", ntohs(raddr.sin_port), ntohs(laddr.sin_port));
146 int req_size = snprintf(req, sizeof(req), "%d,%d\r\n", ntohs(raddr.sin6_port), ntohs(laddr.sin6_port));
149 if (ServerInstance->SE->Send(this, req, req_size, 0) < req_size)
153 virtual void HandleEvent(EventType et, int errornum = 0)
158 /* fd readable event, received ident response */
162 /* fd writeable event, successfully connected! */
166 /* fd error event, ohshi- */
167 ServerInstance->Log(DEBUG,"EVENT_ERROR");
178 ServerInstance->SE->Close(GetFd());
179 ServerInstance->SE->Shutdown(GetFd(), SHUT_WR);
180 ServerInstance->SE->DelFd(this);
190 const char* GetResult()
192 return result.c_str();
197 ServerInstance->Log(DEBUG,"ReadResponse()");
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
203 int recvresult = ServerInstance->SE->Recv(this, ibuf, MAXBUF-1, 0);
205 /* Cant possibly be a valid response shorter than 3 chars */
212 irc::sepstream sep(ibuf, ':');
214 for (int i = 0; sep.GetToken(token); i++)
216 // We only really care about the 4th portion
220 char ident[IDENTMAX + 2];
222 // Truncate the ident at any characters we don't like, skip leading spaces
224 for (const char *j = token.c_str(); *j && (k < IDENTMAX + 1); j++)
229 // Rules taken from InspIRCd::IsIdent
230 if (((*j >= 'A') && (*j <= '}')) || ((*j >= '0') && (*j <= '9')) || (*j == '-') || (*j == '.'))
241 // Redundant check with IsIdent, in case that changes and this doesn't (paranoia!)
242 if (*ident && ServerInstance->IsIdent(ident))
245 ServerInstance->next_call = ServerInstance->Time();
257 class ModuleIdent : public Module
262 ModuleIdent(InspIRCd *Me)
268 virtual Version GetVersion()
270 return Version(1, 1, 1, 0, VF_VENDOR, API_VERSION);
273 virtual void Implements(char *List)
275 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnCleanup] = List[I_OnUserDisconnect] = 1;
278 virtual void OnRehash(User *user, const std::string ¶m)
280 ConfigReader MyConf(ServerInstance);
282 RequestTimeout = MyConf.ReadInteger("ident", "timeout", 0, true);
287 virtual int OnUserRegister(User *user)
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';
296 user->WriteServ("NOTICE Auth :*** Looking up your ident...");
298 // Get the IP that the user is connected to, and bind to that for the outgoing connection
304 socklen_t laddrsz = sizeof(laddr);
306 if (getsockname(user->GetFd(), (sockaddr*) &laddr, &laddrsz) != 0)
308 user->WriteServ("NOTICE Auth :*** Could not find your ident, using %s instead.", user->ident);
313 const char *ip = inet_ntoa(laddr.sin_addr);
315 char ip[INET6_ADDRSTRLEN + 1];
316 inet_ntop(laddr.sin6_family, &laddr.sin6_addr, ip, INET6_ADDRSTRLEN);
319 IdentRequestSocket *isock = NULL;
322 isock = new IdentRequestSocket(ServerInstance, user, ip);
324 catch (ModuleException &e)
326 ServerInstance->Log(DEBUG,"Ident exception: %s", e.GetReason());
330 user->Extend("ident_socket", isock);
334 virtual bool OnCheckReady(User *user)
336 ServerInstance->Log(DEBUG,"OnCheckReady %s", user->nick);
338 /* Does user have an ident socket attached at all? */
339 IdentRequestSocket *isock = NULL;
340 if (!user->GetExt("ident_socket", isock))
342 ServerInstance->Log(DEBUG, "No ident socket :(");
346 ServerInstance->Log(DEBUG, "Has ident_socket");
348 if (isock->age + RequestTimeout > ServerInstance->Time() && !isock->HasResult())
351 user->WriteServ("NOTICE Auth :*** Ident request timed out.");
352 ServerInstance->Log(DEBUG, "Timeout");
353 OnUserDisconnect(user);
357 /* Got a result yet? */
358 if (!isock->HasResult())
360 ServerInstance->Log(DEBUG, "No result yet");
364 ServerInstance->Log(DEBUG, "Yay, result!");
366 /* wooo, got a result! */
367 if (*(isock->GetResult()) != '~')
368 user->WriteServ("NOTICE Auth :*** Found your ident, '%s'", isock->GetResult());
370 user->WriteServ("NOTICE Auth :*** Could not find your ident, using %s instead.", isock->GetResult());
372 strlcpy(user->ident, isock->GetResult(), IDENTMAX+1);
373 OnUserDisconnect(user);
377 virtual void OnCleanup(int target_type, void *item)
379 /* Module unloading, tidy up users */
380 if (target_type == TYPE_USER)
381 OnUserDisconnect((User*)item);
384 virtual void OnUserDisconnect(User *user)
386 /* User disconnect (generic socket detatch event) */
387 IdentRequestSocket *isock = NULL;
388 if (user->GetExt("ident_socket", isock))
392 user->Shrink("ident_socket");
393 ServerInstance->Log(DEBUG, "Removed ident socket from %s", user->nick);
398 MODULE_INIT(ModuleIdent);