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 * ---------------------------------------------------
21 /* $ModDesc: Provides support for RFC 1413 ident lookups */
23 // Version 1.5.0.0 - Updated to use InspSocket, faster and neater.
25 /** Handles RFC1413 ident connections to users
27 class RFC1413 : public InspSocket
30 socklen_t uslen; // length of our port number
31 socklen_t themlen; // length of their port number
32 char ident_request[128]; // buffer used to make up the request string
35 userrec* u; // user record that the lookup is associated with
38 RFC1413(InspIRCd* SI, userrec* user, int maxtime) : InspSocket(SI, user->GetIPString(), 113, false, maxtime), u(user)
43 virtual void OnTimeout()
45 // When we timeout, the connection failed within the allowed timeframe,
46 // so we just display a notice, and tidy off the ident_data.
47 if (u && (Instance->SE->GetRef(ufd) == u))
49 u->Shrink("ident_data");
50 Instance->next_call = Instance->Time();
54 virtual bool OnDataReady()
56 char* ibuf = this->Read();
60 char* section = strtok_r(ibuf,":",&savept);
63 if (strstr(section,"USERID"))
65 section = strtok_r(NULL,":",&savept);
68 // ID type, usually UNIX or OTHER... we dont want it, so read the next token
69 section = strtok_r(NULL,":",&savept);
72 while (*section == ' ') section++; // strip leading spaces
73 for (char* j = section; *j; j++)
74 if ((*j < 33) || (*j > 126))
75 *j = '\0'; // truncate at invalid chars
78 if (u && (Instance->SE->GetRef(ufd) == u))
80 if (this->Instance->IsIdent(section))
82 strlcpy(u->ident,section,IDENTMAX);
83 u->WriteServ("NOTICE "+std::string(u->nick)+" :*** Found your ident: "+std::string(u->ident));
91 section = strtok_r(NULL,":",&savept);
97 virtual void OnClose()
99 // tidy up after ourselves when the connection is done.
100 // We receive this event straight after a timeout, too.
103 // OK, now listen up. The weird looking check here is
104 // REQUIRED. Don't try and optimize it away.
106 // When a socket is closed, it is not immediately removed
107 // from the socket list, there can be a short delay
108 // before it is culled from the list. This means that
109 // without this check, there is a chance that a user
110 // may not exist when we come to ::Shrink them, which
111 // results in a segfault. The value of "u" may not
112 // always be NULL at this point, so, what we do is
113 // check against the fd_ref_table, to see if (1) the user
114 // exists, and (2) its the SAME user, on the same file
115 // descriptor that they were when the lookup began.
117 // Fixes issue reported by webs, 7 Jun 2006
118 if (u && (Instance->SE->GetRef(ufd) == u))
120 Instance->next_call = Instance->Time();
121 u->Shrink("ident_data");
125 virtual void OnError(InspSocketError e)
127 if (u && (Instance->SE->GetRef(ufd) == u))
129 if (*u->ident == '~')
130 u->WriteServ("NOTICE "+std::string(u->nick)+" :*** Could not find your ident, using "+std::string(u->ident)+" instead.");
132 Instance->next_call = Instance->Time();
133 u->Shrink("ident_data");
137 virtual bool OnConnected()
139 if (u && (Instance->SE->GetRef(ufd) == u))
141 sockaddr* sock_us = new sockaddr[2];
142 sockaddr* sock_them = new sockaddr[2];
143 bool success = false;
144 uslen = sizeof(sockaddr_in);
145 themlen = sizeof(sockaddr_in);
147 if (this->u->GetProtocolFamily() == AF_INET6)
149 themlen = sizeof(sockaddr_in6);
150 uslen = sizeof(sockaddr_in6);
151 success = ((getsockname(this->u->GetFd(),sock_us,&uslen) || getpeername(this->u->GetFd(), sock_them, &themlen)));
154 success = ((getsockname(this->u->GetFd(),sock_us,&uslen) || getpeername(this->u->GetFd(), sock_them, &themlen)));
156 success = ((getsockname(this->u->GetFd(),sock_us,&uslen) || getpeername(this->u->GetFd(), sock_them, &themlen)));
166 // send the request in the following format: theirsocket,oursocket
168 if (this->u->GetProtocolFamily() == AF_INET6)
169 snprintf(ident_request,127,"%d,%d\r\n",ntohs(((sockaddr_in6*)sock_them)->sin6_port),ntohs(((sockaddr_in6*)sock_us)->sin6_port));
171 snprintf(ident_request,127,"%d,%d\r\n",ntohs(((sockaddr_in*)sock_them)->sin_port),ntohs(((sockaddr_in*)sock_us)->sin_port));
173 snprintf(ident_request,127,"%d,%d\r\n",ntohs(((sockaddr_in*)sock_them)->sin_port),ntohs(((sockaddr_in*)sock_us)->sin_port));
175 this->Write(ident_request);
183 Instance->next_call = Instance->Time();
189 class ModuleIdent : public Module
199 Conf = new ConfigReader(ServerInstance);
200 IdentTimeout = Conf->ReadInteger("ident","timeout",0,true);
206 ModuleIdent(InspIRCd* Me)
213 void Implements(char* List)
215 List[I_OnCleanup] = List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnUserDisconnect] = 1;
218 virtual void OnRehash(userrec* user, const std::string ¶meter)
223 virtual int OnUserRegister(userrec* user)
226 * when the new user connects, before they authenticate with USER/NICK/PASS, we do
227 * their ident lookup. We do this by instantiating an object of type RFC1413, which
228 * is derived from InspSocket, and inserting it into the socket engine using the
229 * Server::AddSocket() call.
231 char newident[MAXBUF];
232 strcpy(newident,"~");
233 strlcat(newident,user->ident,IDENTMAX);
234 strlcpy(user->ident,newident,IDENTMAX);
237 user->WriteServ("NOTICE "+std::string(user->nick)+" :*** Looking up your ident...");
238 RFC1413* ident = new RFC1413(ServerInstance, user, IdentTimeout);
239 if ((ident->GetState() == I_CONNECTING) || (ident->GetState() == I_CONNECTED))
241 user->Extend("ident_data", (char*)ident);
245 user->WriteServ("NOTICE "+std::string(user->nick)+" :*** Could not find your ident, using "+std::string(user->ident)+" instead.");
246 ServerInstance->next_call = ServerInstance->Time();
251 virtual bool OnCheckReady(userrec* user)
254 * The socket engine will clean up their ident request for us when it completes,
255 * either due to timeout or due to closing, so, we just hold them until they dont
256 * have an ident field any more.
259 return (!user->GetExt("ident_data", ident));
262 virtual void OnCleanup(int target_type, void* item)
264 if (target_type == TYPE_USER)
266 userrec* user = (userrec*)item;
268 if (user->GetExt("ident_data", ident))
270 // FIX: If the user record is deleted, the socket wont be removed
271 // immediately so there is chance of the socket trying to write to
272 // a user which has now vanished! To prevent this, set ident::u
273 // to NULL and check it so that we dont write users who have gone away.
275 ServerInstance->SE->DelFd(ident);
281 virtual void OnUserDisconnect(userrec* user)
284 * when the user quits tidy up any ident lookup they have pending to keep things tidy.
285 * When we call RemoveSocket, the abstractions tied into the system evnetually work their
286 * way to RFC1459::OnClose(), which shrinks off the ident_data for us, so we dont need
287 * to do it here. If we don't tidy this up, there may still be lingering idents for users
288 * who have quit, as class RFC1459 is only loosely bound to userrec* via a pair of pointers
289 * and this would leave at least one of the invalid ;)
292 if (user->GetExt("ident_data", ident))
295 ServerInstance->SE->DelFd(ident);
299 virtual ~ModuleIdent()
301 ServerInstance->next_call = ServerInstance->Time();
304 virtual Version GetVersion()
306 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
311 class ModuleIdentFactory : public ModuleFactory
318 ~ModuleIdentFactory()
322 virtual Module * CreateModule(InspIRCd* Me)
324 return new ModuleIdent(Me);
330 extern "C" void * init_module( void )
332 return new ModuleIdentFactory;