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 /** Handles RFC1413 ident connections to users
32 class RFC1413 : public InspSocket
35 // Server* class used for core communications
36 insp_sockaddr sock_us; // our port number
37 insp_sockaddr sock_them; // their port number
38 socklen_t uslen; // length of our port number
39 socklen_t themlen; // length of their port number
40 char ident_request[128]; // buffer used to make up the request string
43 userrec* u; // user record that the lookup is associated with
46 RFC1413(InspIRCd* SI, userrec* user, int maxtime) : InspSocket(SI, user->GetIPString(), 113, false, maxtime), u(user)
51 virtual void OnTimeout()
53 // When we timeout, the connection failed within the allowed timeframe,
54 // so we just display a notice, and tidy off the ident_data.
55 if (u && (Instance->SE->GetRef(ufd) == u))
57 char newident[MAXBUF];
58 u->Shrink("ident_data");
59 u->WriteServ("NOTICE "+std::string(u->nick)+" :*** Could not find your ident, using ~"+std::string(u->ident)+" instead.");
61 strlcat(newident,u->ident,IDENTMAX);
62 strlcpy(u->ident,newident,IDENTMAX);
66 virtual bool OnDataReady()
68 char* ibuf = this->Read();
72 char* section = strtok_r(ibuf,":",&savept);
75 if (strstr(section,"USERID"))
77 section = strtok_r(NULL,":",&savept);
80 // ID type, usually UNIX or OTHER... we dont want it, so read the next token
81 section = strtok_r(NULL,":",&savept);
84 while (*section == ' ') section++; // strip leading spaces
85 for (char* j = section; *j; j++)
86 if ((*j < 33) || (*j > 126))
87 *j = '\0'; // truncate at invalid chars
90 if (u && (Instance->SE->GetRef(ufd) == u))
92 if (this->Instance->IsIdent(section))
94 strlcpy(u->ident,section,IDENTMAX);
95 Instance->Log(DEBUG,"IDENT SET: "+std::string(u->ident));
96 u->WriteServ("NOTICE "+std::string(u->nick)+" :*** Found your ident: "+std::string(u->ident));
104 section = strtok_r(NULL,":",&savept);
110 virtual void OnClose()
112 // tidy up after ourselves when the connection is done.
113 // We receive this event straight after a timeout, too.
116 // OK, now listen up. The weird looking check here is
117 // REQUIRED. Don't try and optimize it away.
119 // When a socket is closed, it is not immediately removed
120 // from the socket list, there can be a short delay
121 // before it is culled from the list. This means that
122 // without this check, there is a chance that a user
123 // may not exist when we come to ::Shrink them, which
124 // results in a segfault. The value of "u" may not
125 // always be NULL at this point, so, what we do is
126 // check against the fd_ref_table, to see if (1) the user
127 // exists, and (2) its the SAME user, on the same file
128 // descriptor that they were when the lookup began.
130 // Fixes issue reported by webs, 7 Jun 2006
131 if (u && (Instance->SE->GetRef(ufd) == u))
133 u->Shrink("ident_data");
137 virtual void OnError(InspSocketError e)
139 if (u && (Instance->SE->GetRef(ufd) == u))
141 u->Shrink("ident_data");
145 virtual bool OnConnected()
147 Instance->Log(DEBUG,"Ident: connected");
148 if (u && (Instance->SE->GetRef(ufd) == u))
150 uslen = sizeof(sock_us);
151 themlen = sizeof(sock_them);
152 if ((getsockname(this->u->GetFd(),(sockaddr*)&sock_us,&uslen) || getpeername(this->u->GetFd(), (sockaddr*)&sock_them, &themlen)))
154 Instance->Log(DEBUG,"Ident: failed to get socket names, bailing");
159 // send the request in the following format: theirsocket,oursocket
161 snprintf(ident_request,127,"%d,%d\r\n",ntohs(sock_them.sin6_port),ntohs(sock_us.sin6_port));
163 snprintf(ident_request,127,"%d,%d\r\n",ntohs(sock_them.sin_port),ntohs(sock_us.sin_port));
165 this->Write(ident_request);
166 Instance->Log(DEBUG,"Sent ident request, waiting for reply");
177 class ModuleIdent : public Module
187 Conf = new ConfigReader(ServerInstance);
188 IdentTimeout = Conf->ReadInteger("ident","timeout",0,true);
194 ModuleIdent(InspIRCd* Me)
201 void Implements(char* List)
203 List[I_OnCleanup] = List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnUserDisconnect] = 1;
206 virtual void OnRehash(const std::string ¶meter)
211 virtual void OnUserRegister(userrec* user)
214 * when the new user connects, before they authenticate with USER/NICK/PASS, we do
215 * their ident lookup. We do this by instantiating an object of type RFC1413, which
216 * is derived from InspSocket, and inserting it into the socket engine using the
217 * Server::AddSocket() call.
219 user->WriteServ("NOTICE "+std::string(user->nick)+" :*** Looking up your ident...");
220 RFC1413* ident = new RFC1413(ServerInstance, user, IdentTimeout);
221 if ((ident->GetState() == I_CONNECTING) || (ident->GetState() == I_CONNECTED))
223 user->Extend("ident_data", (char*)ident);
227 char newident[MAXBUF];
228 user->WriteServ("NOTICE "+std::string(user->nick)+" :*** Could not find your ident, using ~"+std::string(user->ident)+" instead.");
229 strcpy(newident,"~");
230 strlcat(newident,user->ident,IDENTMAX);
231 strlcpy(user->ident,newident,IDENTMAX);
236 virtual bool OnCheckReady(userrec* user)
239 * The socket engine will clean up their ident request for us when it completes,
240 * either due to timeout or due to closing, so, we just hold them until they dont
241 * have an ident field any more.
244 return (!user->GetExt("ident_data", ident));
247 virtual void OnCleanup(int target_type, void* item)
249 if (target_type == TYPE_USER)
251 userrec* user = (userrec*)item;
253 if (user->GetExt("ident_data", ident))
255 // FIX: If the user record is deleted, the socket wont be removed
256 // immediately so there is chance of the socket trying to write to
257 // a user which has now vanished! To prevent this, set ident::u
258 // to NULL and check it so that we dont write users who have gone away.
264 virtual void OnUserDisconnect(userrec* user)
267 * when the user quits tidy up any ident lookup they have pending to keep things tidy.
268 * When we call RemoveSocket, the abstractions tied into the system evnetually work their
269 * way to RFC1459::OnClose(), which shrinks off the ident_data for us, so we dont need
270 * to do it here. If we don't tidy this up, there may still be lingering idents for users
271 * who have quit, as class RFC1459 is only loosely bound to userrec* via a pair of pointers
272 * and this would leave at least one of the invalid ;)
275 if (user->GetExt("ident_data", ident))
281 virtual ~ModuleIdent()
285 virtual Version GetVersion()
287 return Version(1,5,0,0,VF_VENDOR,API_VERSION);
292 class ModuleIdentFactory : public ModuleFactory
299 ~ModuleIdentFactory()
303 virtual Module * CreateModule(InspIRCd* Me)
305 return new ModuleIdent(Me);
311 extern "C" void * init_module( void )
313 return new ModuleIdentFactory;