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 extern InspIRCd* ServerInstance;
28 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
30 /* $ModDesc: Provides support for RFC 1413 ident lookups */
32 // Version 1.5.0.0 - Updated to use InspSocket, faster and neater.
34 class RFC1413 : public InspSocket
37 Server* Srv; // Server* class used for core communications
38 insp_sockaddr sock_us; // our port number
39 insp_sockaddr sock_them; // their port number
40 socklen_t uslen; // length of our port number
41 socklen_t themlen; // length of their port number
42 char ident_request[128]; // buffer used to make up the request string
45 userrec* u; // user record that the lookup is associated with
48 RFC1413(InspIRCd* SI, userrec* user, int maxtime, Server* S) : InspSocket(SI, user->GetIPString(), 113, false, maxtime), Srv(S), u(user), ufd(user->fd)
50 log(DEBUG,"Ident: associated.");
53 virtual void OnTimeout()
55 // When we timeout, the connection failed within the allowed timeframe,
56 // so we just display a notice, and tidy off the ident_data.
57 if (u && (fd_ref_table[ufd] == u))
59 u->Shrink("ident_data");
60 u->WriteServ("NOTICE "+std::string(u->nick)+" :*** Could not find your ident, using "+std::string(u->ident)+" instead.");
64 virtual bool OnDataReady()
66 char* ibuf = this->Read();
70 char* section = strtok_r(ibuf,":",&savept);
73 if (strstr(section,"USERID"))
75 section = strtok_r(NULL,":",&savept);
78 // ID type, usually UNIX or OTHER... we dont want it, so read the next token
79 section = strtok_r(NULL,":",&savept);
82 while (*section == ' ') section++; // strip leading spaces
83 for (char* j = section; *j; j++)
84 if ((*j < 33) || (*j > 126))
85 *j = '\0'; // truncate at invalid chars
88 if (u && (fd_ref_table[ufd] == u))
90 strlcpy(u->ident,section,IDENTMAX);
91 log(DEBUG,"IDENT SET: "+std::string(u->ident));
92 u->WriteServ("NOTICE "+std::string(u->nick)+" :*** Found your ident: "+std::string(u->ident));
99 section = strtok_r(NULL,":",&savept);
105 virtual void OnClose()
107 // tidy up after ourselves when the connection is done.
108 // We receive this event straight after a timeout, too.
111 // OK, now listen up. The weird looking check here is
112 // REQUIRED. Don't try and optimize it away.
114 // When a socket is closed, it is not immediately removed
115 // from the socket list, there can be a short delay
116 // before it is culled from the list. This means that
117 // without this check, there is a chance that a user
118 // may not exist when we come to ::Shrink them, which
119 // results in a segfault. The value of "u" may not
120 // always be NULL at this point, so, what we do is
121 // check against the fd_ref_table, to see if (1) the user
122 // exists, and (2) its the SAME user, on the same file
123 // descriptor that they were when the lookup began.
125 // Fixes issue reported by webs, 7 Jun 2006
126 if (u && (fd_ref_table[ufd] == u))
128 u->Shrink("ident_data");
132 virtual void OnError(InspSocketError e)
134 if (u && (fd_ref_table[ufd] == u))
136 u->Shrink("ident_data");
140 virtual bool OnConnected()
142 if (u && (fd_ref_table[ufd] == u))
144 uslen = sizeof(sock_us);
145 themlen = sizeof(sock_them);
146 if ((getsockname(this->u->fd,(sockaddr*)&sock_us,&uslen) || getpeername(this->u->fd, (sockaddr*)&sock_them, &themlen)))
148 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 log(DEBUG,"Sent ident request, waiting for reply");
171 class ModuleIdent : public Module
181 Conf = new ConfigReader;
182 IdentTimeout = Conf->ReadInteger("ident","timeout",0,true);
188 ModuleIdent(Server* 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, Srv);
215 if (ident->GetState() != I_ERROR)
217 user->Extend("ident_data", (char*)ident);
218 Srv->AddSocket(ident);
222 user->WriteServ("NOTICE "+std::string(user->nick)+" :*** Could not find your ident, using "+std::string(user->ident)+" instead.");
227 virtual bool OnCheckReady(userrec* user)
230 * The socket engine will clean up their ident request for us when it completes,
231 * either due to timeout or due to closing, so, we just hold them until they dont
232 * have an ident field any more.
235 return (!user->GetExt("ident_data", ident));
238 virtual void OnCleanup(int target_type, void* item)
240 if (target_type == TYPE_USER)
242 userrec* user = (userrec*)item;
244 if (user->GetExt("ident_data", ident))
246 // FIX: If the user record is deleted, the socket wont be removed
247 // immediately so there is chance of the socket trying to write to
248 // a user which has now vanished! To prevent this, set ident::u
249 // to NULL and check it so that we dont write users who have gone away.
251 Srv->RemoveSocket(ident);
256 virtual void OnUserDisconnect(userrec* user)
259 * when the user quits tidy up any ident lookup they have pending to keep things tidy.
260 * When we call RemoveSocket, the abstractions tied into the system evnetually work their
261 * way to RFC1459::OnClose(), which shrinks off the ident_data for us, so we dont need
262 * to do it here. If we don't tidy this up, there may still be lingering idents for users
263 * who have quit, as class RFC1459 is only loosely bound to userrec* via a pair of pointers
264 * and this would leave at least one of the invalid ;)
267 if (user->GetExt("ident_data", ident))
270 Srv->RemoveSocket(ident);
274 virtual ~ModuleIdent()
278 virtual Version GetVersion()
280 return Version(1,5,0,0,VF_VENDOR);
285 class ModuleIdentFactory : public ModuleFactory
292 ~ModuleIdentFactory()
296 virtual Module * CreateModule(Server* Me)
298 return new ModuleIdent(Me);
304 extern "C" void * init_module( void )
306 return new ModuleIdentFactory;