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* Srv; // Server* class used for core communications
34 sockaddr_in sock_us; // our port number
35 sockaddr_in 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
43 RFC1413(userrec* user, int maxtime, Server* S) : InspSocket((char*)inet_ntoa(user->ip4), 113, false, maxtime), Srv(S), u(user)
45 Srv->Log(DEBUG,"Ident: associated.");
48 virtual void OnTimeout()
50 // When we timeout, the connection failed within the allowed timeframe,
51 // so we just display a notice, and tidy off the ident_data.
54 u->Shrink("ident_data");
55 Srv->SendServ(u->fd,"NOTICE "+std::string(u->nick)+" :*** Could not find your ident, using "+std::string(u->ident)+" instead.");
59 virtual bool OnDataReady()
61 char* ibuf = this->Read();
65 char* section = strtok_r(ibuf,":",&savept);
68 if (strstr(section,"USERID"))
70 section = strtok_r(NULL,":",&savept);
73 // ID type, usually UNIX or OTHER... we dont want it, so read the next token
74 section = strtok_r(NULL,":",&savept);
77 while (*section == ' ') section++; // strip leading spaces
78 for (char* j = section; *j; j++)
79 if ((*j < 33) || (*j > 126))
80 *j = '\0'; // truncate at invalid chars
85 strlcpy(u->ident,section,IDENTMAX);
86 Srv->Log(DEBUG,"IDENT SET: "+std::string(u->ident));
87 Srv->SendServ(u->fd,"NOTICE "+std::string(u->nick)+" :*** Found your ident: "+std::string(u->ident));
94 section = strtok_r(NULL,":",&savept);
100 virtual void OnClose()
102 // tidy up after ourselves when the connection is done.
103 // We receive this event straight after a timeout, too.
106 u->Shrink("ident_data");
110 virtual void OnError(InspSocketError e)
114 u->Shrink("ident_data");
118 virtual bool OnConnected()
122 uslen = sizeof(sock_us);
123 themlen = sizeof(sock_them);
124 if ((getsockname(this->u->fd,(sockaddr*)&sock_us,&uslen) || getpeername(this->u->fd, (sockaddr*)&sock_them, &themlen)))
126 Srv->Log(DEBUG,"Ident: failed to get socket names, bailing");
131 // send the request in the following format: theirsocket,oursocket
132 snprintf(ident_request,127,"%d,%d\r\n",ntohs(sock_them.sin_port),ntohs(sock_us.sin_port));
133 this->Write(ident_request);
134 Srv->Log(DEBUG,"Sent ident request, waiting for reply");
145 class ModuleIdent : public Module
155 Conf = new ConfigReader;
156 IdentTimeout = Conf->ReadInteger("ident","timeout",0,true);
162 ModuleIdent(Server* Me)
169 void Implements(char* List)
171 List[I_OnCleanup] = List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnUserDisconnect] = 1;
174 virtual void OnRehash(const std::string ¶meter)
179 virtual void OnUserRegister(userrec* user)
182 * when the new user connects, before they authenticate with USER/NICK/PASS, we do
183 * their ident lookup. We do this by instantiating an object of type RFC1413, which
184 * is derived from InspSocket, and inserting it into the socket engine using the
185 * Server::AddSocket() call.
187 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :*** Looking up your ident...");
188 RFC1413* ident = new RFC1413(user, IdentTimeout, Srv);
189 if (ident->GetState() != I_ERROR)
191 user->Extend("ident_data", (char*)ident);
192 Srv->AddSocket(ident);
196 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :*** Could not find your ident, using "+std::string(user->ident)+" instead.");
201 virtual bool OnCheckReady(userrec* user)
204 * The socket engine will clean up their ident request for us when it completes,
205 * either due to timeout or due to closing, so, we just hold them until they dont
206 * have an ident field any more.
208 RFC1413* ident = (RFC1413*)user->GetExt("ident_data");
212 virtual void OnCleanup(int target_type, void* item)
214 if (target_type == TYPE_USER)
216 userrec* user = (userrec*)item;
217 RFC1413* ident = (RFC1413*)user->GetExt("ident_data");
220 // FIX: If the user record is deleted, the socket wont be removed
221 // immediately so there is chance of the socket trying to write to
222 // a user which has now vanished! To prevent this, set ident::u
223 // to NULL and check it so that we dont write users who have gone away.
225 Srv->RemoveSocket(ident);
230 virtual void OnUserDisconnect(userrec* user)
233 * when the user quits tidy up any ident lookup they have pending to keep things tidy.
234 * When we call RemoveSocket, the abstractions tied into the system evnetually work their
235 * way to RFC1459::OnClose(), which shrinks off the ident_data for us, so we dont need
236 * to do it here. If we don't tidy this up, there may still be lingering idents for users
237 * who have quit, as class RFC1459 is only loosely bound to userrec* via a pair of pointers
238 * and this would leave at least one of the invalid ;)
240 RFC1413* ident = (RFC1413*)user->GetExt("ident_data");
244 Srv->RemoveSocket(ident);
248 virtual ~ModuleIdent()
252 virtual Version GetVersion()
254 return Version(1,5,0,0,VF_VENDOR);
259 class ModuleIdentFactory : public ModuleFactory
266 ~ModuleIdentFactory()
270 virtual Module * CreateModule(Server* Me)
272 return new ModuleIdent(Me);
278 extern "C" void * init_module( void )
280 return new ModuleIdentFactory;