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 // Server* class used for core communications
31 insp_sockaddr sock_us; // our port number
32 insp_sockaddr sock_them; // their port number
33 socklen_t uslen; // length of our port number
34 socklen_t themlen; // length of their port number
35 char ident_request[128]; // buffer used to make up the request string
38 userrec* u; // user record that the lookup is associated with
41 RFC1413(InspIRCd* SI, userrec* user, int maxtime) : InspSocket(SI, user->GetIPString(), 113, false, maxtime), u(user)
46 virtual void OnTimeout()
48 // When we timeout, the connection failed within the allowed timeframe,
49 // so we just display a notice, and tidy off the ident_data.
50 if (u && (Instance->SE->GetRef(ufd) == u))
52 char newident[MAXBUF];
53 u->Shrink("ident_data");
54 u->WriteServ("NOTICE "+std::string(u->nick)+" :*** Could not find your ident, using ~"+std::string(u->ident)+" instead.");
56 strlcat(newident,u->ident,IDENTMAX);
57 strlcpy(u->ident,newident,IDENTMAX);
61 virtual bool OnDataReady()
63 char* ibuf = this->Read();
67 char* section = strtok_r(ibuf,":",&savept);
70 if (strstr(section,"USERID"))
72 section = strtok_r(NULL,":",&savept);
75 // ID type, usually UNIX or OTHER... we dont want it, so read the next token
76 section = strtok_r(NULL,":",&savept);
79 while (*section == ' ') section++; // strip leading spaces
80 for (char* j = section; *j; j++)
81 if ((*j < 33) || (*j > 126))
82 *j = '\0'; // truncate at invalid chars
85 if (u && (Instance->SE->GetRef(ufd) == u))
87 if (this->Instance->IsIdent(section))
89 strlcpy(u->ident,section,IDENTMAX);
90 u->WriteServ("NOTICE "+std::string(u->nick)+" :*** Found your ident: "+std::string(u->ident));
98 section = strtok_r(NULL,":",&savept);
104 virtual void OnClose()
106 // tidy up after ourselves when the connection is done.
107 // We receive this event straight after a timeout, too.
110 // OK, now listen up. The weird looking check here is
111 // REQUIRED. Don't try and optimize it away.
113 // When a socket is closed, it is not immediately removed
114 // from the socket list, there can be a short delay
115 // before it is culled from the list. This means that
116 // without this check, there is a chance that a user
117 // may not exist when we come to ::Shrink them, which
118 // results in a segfault. The value of "u" may not
119 // always be NULL at this point, so, what we do is
120 // check against the fd_ref_table, to see if (1) the user
121 // exists, and (2) its the SAME user, on the same file
122 // descriptor that they were when the lookup began.
124 // Fixes issue reported by webs, 7 Jun 2006
125 if (u && (Instance->SE->GetRef(ufd) == u))
127 u->Shrink("ident_data");
131 virtual void OnError(InspSocketError e)
133 if (u && (Instance->SE->GetRef(ufd) == u))
135 u->Shrink("ident_data");
139 virtual bool OnConnected()
141 if (u && (Instance->SE->GetRef(ufd) == u))
143 uslen = sizeof(sock_us);
144 themlen = sizeof(sock_them);
145 if ((getsockname(this->u->GetFd(),(sockaddr*)&sock_us,&uslen) || getpeername(this->u->GetFd(), (sockaddr*)&sock_them, &themlen)))
147 Instance->Log(DEBUG,"BUG: Ident: failed to get socket names");
152 // send the request in the following format: theirsocket,oursocket
154 snprintf(ident_request,127,"%d,%d\r\n",ntohs(sock_them.sin6_port),ntohs(sock_us.sin6_port));
156 snprintf(ident_request,127,"%d,%d\r\n",ntohs(sock_them.sin_port),ntohs(sock_us.sin_port));
158 this->Write(ident_request);
169 class ModuleIdent : public Module
179 Conf = new ConfigReader(ServerInstance);
180 IdentTimeout = Conf->ReadInteger("ident","timeout",0,true);
186 ModuleIdent(InspIRCd* Me)
193 void Implements(char* List)
195 List[I_OnCleanup] = List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnUserDisconnect] = 1;
198 virtual void OnRehash(userrec* user, const std::string ¶meter)
203 virtual int OnUserRegister(userrec* user)
206 * when the new user connects, before they authenticate with USER/NICK/PASS, we do
207 * their ident lookup. We do this by instantiating an object of type RFC1413, which
208 * is derived from InspSocket, and inserting it into the socket engine using the
209 * Server::AddSocket() call.
211 user->WriteServ("NOTICE "+std::string(user->nick)+" :*** Looking up your ident...");
212 RFC1413* ident = new RFC1413(ServerInstance, user, IdentTimeout);
213 if ((ident->GetState() == I_CONNECTING) || (ident->GetState() == I_CONNECTED))
215 user->Extend("ident_data", (char*)ident);
219 char newident[MAXBUF];
220 user->WriteServ("NOTICE "+std::string(user->nick)+" :*** Could not find your ident, using ~"+std::string(user->ident)+" instead.");
221 strcpy(newident,"~");
222 strlcat(newident,user->ident,IDENTMAX);
223 strlcpy(user->ident,newident,IDENTMAX);
229 virtual bool OnCheckReady(userrec* user)
232 * The socket engine will clean up their ident request for us when it completes,
233 * either due to timeout or due to closing, so, we just hold them until they dont
234 * have an ident field any more.
237 return (!user->GetExt("ident_data", ident));
240 virtual void OnCleanup(int target_type, void* item)
242 if (target_type == TYPE_USER)
244 userrec* user = (userrec*)item;
246 if (user->GetExt("ident_data", ident))
248 // FIX: If the user record is deleted, the socket wont be removed
249 // immediately so there is chance of the socket trying to write to
250 // a user which has now vanished! To prevent this, set ident::u
251 // to NULL and check it so that we dont write users who have gone away.
253 ServerInstance->SE->DelFd(ident);
259 virtual void OnUserDisconnect(userrec* user)
262 * when the user quits tidy up any ident lookup they have pending to keep things tidy.
263 * When we call RemoveSocket, the abstractions tied into the system evnetually work their
264 * way to RFC1459::OnClose(), which shrinks off the ident_data for us, so we dont need
265 * to do it here. If we don't tidy this up, there may still be lingering idents for users
266 * who have quit, as class RFC1459 is only loosely bound to userrec* via a pair of pointers
267 * and this would leave at least one of the invalid ;)
270 if (user->GetExt("ident_data", ident))
273 ServerInstance->SE->DelFd(ident);
278 virtual ~ModuleIdent()
282 virtual Version GetVersion()
284 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
289 class ModuleIdentFactory : public ModuleFactory
296 ~ModuleIdentFactory()
300 virtual Module * CreateModule(InspIRCd* Me)
302 return new ModuleIdent(Me);
308 extern "C" void * init_module( void )
310 return new ModuleIdentFactory;