1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
26 /* $ModDesc: Provides support for RFC 1413 ident lookups */
28 // Version 1.5.0.0 - Updated to use InspSocket, faster and neater.
30 class RFC1413 : public InspSocket
33 // Server* class used for core communications
34 insp_sockaddr sock_us; // our port number
35 insp_sockaddr sock_them; // their port number
36 socklen_t uslen; // length of our port number
37 socklen_t themlen; // length of their port number
38 char ident_request[128]; // buffer used to make up the request string
41 userrec* u; // user record that the lookup is associated with
44 RFC1413(InspIRCd* SI, userrec* user, int maxtime) : InspSocket(SI, user->GetIPString(), 113, false, maxtime), u(user)
49 virtual void OnTimeout()
51 // When we timeout, the connection failed within the allowed timeframe,
52 // so we just display a notice, and tidy off the ident_data.
53 if (u && (Instance->SE->GetRef(ufd) == u))
55 u->Shrink("ident_data");
56 u->WriteServ("NOTICE "+std::string(u->nick)+" :*** Could not find your ident, using "+std::string(u->ident)+" instead.");
60 virtual bool OnDataReady()
62 char* ibuf = this->Read();
66 char* section = strtok_r(ibuf,":",&savept);
69 if (strstr(section,"USERID"))
71 section = strtok_r(NULL,":",&savept);
74 // ID type, usually UNIX or OTHER... we dont want it, so read the next token
75 section = strtok_r(NULL,":",&savept);
78 while (*section == ' ') section++; // strip leading spaces
79 for (char* j = section; *j; j++)
80 if ((*j < 33) || (*j > 126))
81 *j = '\0'; // truncate at invalid chars
84 if (u && (Instance->SE->GetRef(ufd) == u))
86 if (this->Instance->IsIdent(section))
88 strlcpy(u->ident,section,IDENTMAX);
89 Instance->Log(DEBUG,"IDENT SET: "+std::string(u->ident));
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 Instance->Log(DEBUG,"Ident: connected");
142 if (u && (Instance->SE->GetRef(ufd) == u))
144 uslen = sizeof(sock_us);
145 themlen = sizeof(sock_them);
146 if ((getsockname(this->u->GetFd(),(sockaddr*)&sock_us,&uslen) || getpeername(this->u->GetFd(), (sockaddr*)&sock_them, &themlen)))
148 Instance->Log(DEBUG,"Ident: failed to get socket names, bailing");
153 // send the request in the following format: theirsocket,oursocket
155 snprintf(ident_request,127,"%d,%d\r\n",ntohs(sock_them.sin6_port),ntohs(sock_us.sin6_port));
157 snprintf(ident_request,127,"%d,%d\r\n",ntohs(sock_them.sin_port),ntohs(sock_us.sin_port));
159 this->Write(ident_request);
160 Instance->Log(DEBUG,"Sent ident request, waiting for reply");
171 class ModuleIdent : public Module
181 Conf = new ConfigReader(ServerInstance);
182 IdentTimeout = Conf->ReadInteger("ident","timeout",0,true);
188 ModuleIdent(InspIRCd* Me)
195 void Implements(char* List)
197 List[I_OnCleanup] = List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnUserDisconnect] = 1;
200 virtual void OnRehash(const std::string ¶meter)
205 virtual void OnUserRegister(userrec* user)
208 * when the new user connects, before they authenticate with USER/NICK/PASS, we do
209 * their ident lookup. We do this by instantiating an object of type RFC1413, which
210 * is derived from InspSocket, and inserting it into the socket engine using the
211 * Server::AddSocket() call.
213 user->WriteServ("NOTICE "+std::string(user->nick)+" :*** Looking up your ident...");
214 RFC1413* ident = new RFC1413(ServerInstance, user, IdentTimeout);
215 user->Extend("ident_data", (char*)ident);
218 virtual bool OnCheckReady(userrec* user)
221 * The socket engine will clean up their ident request for us when it completes,
222 * either due to timeout or due to closing, so, we just hold them until they dont
223 * have an ident field any more.
226 return (!user->GetExt("ident_data", ident));
229 virtual void OnCleanup(int target_type, void* item)
231 if (target_type == TYPE_USER)
233 userrec* user = (userrec*)item;
235 if (user->GetExt("ident_data", ident))
237 // FIX: If the user record is deleted, the socket wont be removed
238 // immediately so there is chance of the socket trying to write to
239 // a user which has now vanished! To prevent this, set ident::u
240 // to NULL and check it so that we dont write users who have gone away.
246 virtual void OnUserDisconnect(userrec* user)
249 * when the user quits tidy up any ident lookup they have pending to keep things tidy.
250 * When we call RemoveSocket, the abstractions tied into the system evnetually work their
251 * way to RFC1459::OnClose(), which shrinks off the ident_data for us, so we dont need
252 * to do it here. If we don't tidy this up, there may still be lingering idents for users
253 * who have quit, as class RFC1459 is only loosely bound to userrec* via a pair of pointers
254 * and this would leave at least one of the invalid ;)
257 if (user->GetExt("ident_data", ident))
263 virtual ~ModuleIdent()
267 virtual Version GetVersion()
269 return Version(1,5,0,0,VF_VENDOR);
274 class ModuleIdentFactory : public ModuleFactory
281 ~ModuleIdentFactory()
285 virtual Module * CreateModule(InspIRCd* Me)
287 return new ModuleIdent(Me);
293 extern "C" void * init_module( void )
295 return new ModuleIdentFactory;