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 RFC 1413 ident lookups */
21 // Version 1.5.0.0 - Updated to use InspSocket, faster and neater.
23 /** Handles RFC1413 ident connections to users
25 class RFC1413 : public InspSocket
28 socklen_t uslen; // length of our port number
29 socklen_t themlen; // length of their port number
30 char ident_request[128]; // buffer used to make up the request string
33 userrec* u; // user record that the lookup is associated with
36 RFC1413(InspIRCd* SI, userrec* user, int maxtime, const std::string &bindto) : InspSocket(SI, user->GetIPString(), 113, false, maxtime, bindto), u(user)
41 virtual void OnTimeout()
43 // When we timeout, the connection failed within the allowed timeframe,
44 // so we just display a notice, and tidy off the ident_data.
45 if (u && (Instance->SE->GetRef(ufd) == u))
47 u->Shrink("ident_data");
48 Instance->next_call = Instance->Time();
52 virtual bool OnDataReady()
54 char* ibuf = this->Read();
58 char* section = strtok_r(ibuf,":",&savept);
61 if (strstr(section,"USERID"))
63 section = strtok_r(NULL,":",&savept);
66 // ID type, usually UNIX or OTHER... we dont want it, so read the next token
67 section = strtok_r(NULL,":",&savept);
70 while (*section == ' ') section++; // strip leading spaces
71 for (char* j = section; *j; j++)
72 if ((*j < 33) || (*j > 126))
73 *j = '\0'; // truncate at invalid chars
76 if (u && (Instance->SE->GetRef(ufd) == u))
78 if (this->Instance->IsIdent(section))
80 u->Extend("IDENT", new std::string(std::string(section) + "," + std::string(u->ident)));
81 strlcpy(u->ident,section,IDENTMAX);
82 u->WriteServ("NOTICE "+std::string(u->nick)+" :*** Found your ident: "+std::string(u->ident));
90 section = strtok_r(NULL,":",&savept);
96 virtual void OnClose()
98 // tidy up after ourselves when the connection is done.
99 // We receive this event straight after a timeout, too.
102 // OK, now listen up. The weird looking check here is
103 // REQUIRED. Don't try and optimize it away.
105 // When a socket is closed, it is not immediately removed
106 // from the socket list, there can be a short delay
107 // before it is culled from the list. This means that
108 // without this check, there is a chance that a user
109 // may not exist when we come to ::Shrink them, which
110 // results in a segfault. The value of "u" may not
111 // always be NULL at this point, so, what we do is
112 // check against the fd_ref_table, to see if (1) the user
113 // exists, and (2) its the SAME user, on the same file
114 // descriptor that they were when the lookup began.
116 // Fixes issue reported by webs, 7 Jun 2006
117 if (u && (Instance->SE->GetRef(ufd) == u))
119 Instance->next_call = Instance->Time();
120 u->Shrink("ident_data");
124 virtual void OnError(InspSocketError e)
126 if (u && (Instance->SE->GetRef(ufd) == u))
128 if (*u->ident == '~')
129 u->WriteServ("NOTICE "+std::string(u->nick)+" :*** Could not find your ident, using "+std::string(u->ident)+" instead.");
131 Instance->next_call = Instance->Time();
132 u->Shrink("ident_data");
136 virtual bool OnConnected()
138 if (u && (Instance->SE->GetRef(ufd) == u))
140 sockaddr* sock_us = new sockaddr[2];
141 sockaddr* sock_them = new sockaddr[2];
142 bool success = false;
143 uslen = sizeof(sockaddr_in);
144 themlen = sizeof(sockaddr_in);
146 if (this->u->GetProtocolFamily() == AF_INET6)
148 themlen = sizeof(sockaddr_in6);
149 uslen = sizeof(sockaddr_in6);
152 success = ((getsockname(this->u->GetFd(),sock_us,&uslen) || getpeername(this->u->GetFd(), sock_them, &themlen)));
161 // send the request in the following format: theirsocket,oursocket
163 if (this->u->GetProtocolFamily() == AF_INET6)
164 snprintf(ident_request,127,"%d,%d\r\n",ntohs(((sockaddr_in6*)sock_them)->sin6_port),ntohs(((sockaddr_in6*)sock_us)->sin6_port));
167 snprintf(ident_request,127,"%d,%d\r\n",ntohs(((sockaddr_in*)sock_them)->sin_port),ntohs(((sockaddr_in*)sock_us)->sin_port));
168 this->Write(ident_request);
176 Instance->next_call = Instance->Time();
182 class ModuleIdent : public Module
187 std::string PortBind;
192 Conf = new ConfigReader(ServerInstance);
193 IdentTimeout = Conf->ReadInteger("ident", "timeout", 0, true);
194 PortBind = Conf->ReadValue("ident", "bind", 0);
200 ModuleIdent(InspIRCd* Me)
207 void Implements(char* List)
209 List[I_OnCleanup] = List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnUserDisconnect] = 1;
212 void OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, const std::string &extname, bool displayable)
214 if ((displayable) && (extname == "IDENT"))
217 if (GetExt("IDENT", ident))
218 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *ident);
223 virtual void OnRehash(userrec* user, const std::string ¶meter)
228 virtual int OnUserRegister(userrec* user)
231 * when the new user connects, before they authenticate with USER/NICK/PASS, we do
232 * their ident lookup. We do this by instantiating an object of type RFC1413, which
233 * is derived from InspSocket, and inserting it into the socket engine using the
234 * Server::AddSocket() call.
236 char newident[MAXBUF];
237 strcpy(newident,"~");
238 strlcat(newident,user->ident,IDENTMAX);
239 strlcpy(user->ident,newident,IDENTMAX);
242 user->WriteServ("NOTICE "+std::string(user->nick)+" :*** Looking up your ident...");
243 RFC1413* ident = new RFC1413(ServerInstance, user, IdentTimeout, PortBind);
244 if ((ident->GetState() == I_CONNECTING) || (ident->GetState() == I_CONNECTED))
246 user->Extend("ident_data", (char*)ident);
250 user->WriteServ("NOTICE "+std::string(user->nick)+" :*** Could not find your ident, using "+std::string(user->ident)+" instead.");
251 ServerInstance->next_call = ServerInstance->Time();
256 virtual bool OnCheckReady(userrec* user)
259 * The socket engine will clean up their ident request for us when it completes,
260 * either due to timeout or due to closing, so, we just hold them until they dont
261 * have an ident field any more.
264 return (!user->GetExt("ident_data", ident));
267 virtual void OnCleanup(int target_type, void* item)
269 if (target_type == TYPE_USER)
271 userrec* user = (userrec*)item;
273 std::string* identstr;
274 if (user->GetExt("ident_data", ident))
276 // FIX: If the user record is deleted, the socket wont be removed
277 // immediately so there is chance of the socket trying to write to
278 // a user which has now vanished! To prevent this, set ident::u
279 // to NULL and check it so that we dont write users who have gone away.
281 ServerInstance->SE->DelFd(ident);
284 if (user->GetExt("IDENT", identstr))
291 virtual void OnUserDisconnect(userrec* user)
294 * when the user quits tidy up any ident lookup they have pending to keep things tidy.
295 * When we call RemoveSocket, the abstractions tied into the system evnetually work their
296 * way to RFC1459::OnClose(), which shrinks off the ident_data for us, so we dont need
297 * to do it here. If we don't tidy this up, there may still be lingering idents for users
298 * who have quit, as class RFC1459 is only loosely bound to userrec* via a pair of pointers
299 * and this would leave at least one of the invalid ;)
302 std::string* identstr;
303 if (user->GetExt("ident_data", ident))
306 ServerInstance->SE->DelFd(ident);
308 if (user->GetExt("IDENT", identstr))
314 virtual ~ModuleIdent()
316 ServerInstance->next_call = ServerInstance->Time();
319 virtual Version GetVersion()
321 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
326 class ModuleIdentFactory : public ModuleFactory
333 ~ModuleIdentFactory()
337 virtual Module * CreateModule(InspIRCd* Me)
339 return new ModuleIdent(Me);
345 extern "C" DllExport void * init_module( void )
347 return new ModuleIdentFactory;