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 * ---------------------------------------------------
23 #include <sys/types.h>
24 #include <sys/socket.h>
36 /* $ModDesc: Provides support for RFC 1413 ident lookups */
38 // Version 1.5.0.0 - Updated to use InspSocket, faster and neater.
40 class RFC1413 : public InspSocket
43 Server* Srv; // Server* class used for core communications
44 userrec* u; // user record that the lookup is associated with
45 sockaddr_in sock_us; // our port number
46 sockaddr_in sock_them; // their port number
47 socklen_t uslen; // length of our port number
48 socklen_t themlen; // length of their port number
49 char ident_request[128]; // buffer used to make up the request string
52 // The destructor makes damn sure the socket is freed :)
53 RFC1413(userrec* user, int maxtime, Server* S) : InspSocket(user->host, 113, false, maxtime)
55 Srv->Log(DEBUG,"Ident: associated with user "+std::string(user->nick));
60 virtual void OnTimeout()
62 // When we timeout, the connection failed within the allowed timeframe,
63 // so we just display a notice, and tidy off the ident_data.
64 u->Shrink("ident_data");
65 Srv->SendServ(u->fd,"NOTICE "+std::string(u->nick)+" :*** Could not find your ident, using "+std::string(u->ident)+" instead.");
68 virtual bool OnDataReady()
70 std::string databuf = this->Read();
74 strlcpy(ibuf,databuf.c_str(),1024);
75 Srv->Log(DEBUG,"Received ident response");
77 char* section = strtok_r(ibuf,":",&savept);
80 if (strstr(section,"USERID"))
82 section = strtok_r(NULL,":",&savept);
85 // ID type, usually UNIX or OTHER... we dont want it, so read the next token
86 section = strtok_r(NULL,":",&savept);
89 while (*section == ' ') section++; // strip leading spaces
90 int t = strlen(section);
91 for (int j = 0; j < t; j++)
92 if ((section[j] < 33) || (section[j]>126))
93 section[j] = '\0'; // truncate at invalid chars
96 strlcpy(u->ident,section,IDENTMAX);
97 Srv->Log(DEBUG,"IDENT SET: "+std::string(u->ident));
98 Srv->SendServ(u->fd,"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.
114 u->Shrink("ident_data");
117 virtual bool OnConnected()
119 uslen = sizeof(sock_us);
120 themlen = sizeof(sock_them);
121 if ((getsockname(this->u->fd,(sockaddr*)&sock_us,&uslen) || getpeername(this->u->fd, (sockaddr*)&sock_them, &themlen)))
123 Srv->Log(DEBUG,"Ident: failed to get socket names, bailing");
128 // send the request in the following format: theirsocket,oursocket
129 snprintf(ident_request,127,"%d,%d\r\n",ntohs(sock_them.sin_port),ntohs(sock_us.sin_port));
130 this->Write(ident_request);
131 Srv->Log(DEBUG,"Sent ident request, waiting for reply");
137 class ModuleIdent : public Module
147 Conf = new ConfigReader;
148 IdentTimeout = Conf->ReadInteger("ident","timeout",0,true);
152 ModuleIdent(Server* Me)
159 void Implements(char* List)
161 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCheckReady] = List[I_OnUserDisconnect] = 1;
164 virtual void OnRehash(std::string parameter)
169 virtual void OnUserRegister(userrec* user)
171 // when the new user connects, before they authenticate with USER/NICK/PASS, we do
172 // their ident lookup. We do this by instantiating an object of type RFC1413, which
173 // is derived from InspSocket, and inserting it into the socket engine using the
174 // Server::AddSocket() call.
175 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :*** Looking up your ident...");
176 RFC1413* ident = new RFC1413(user, IdentTimeout, Srv);
177 user->Extend("ident_data", (char*)ident);
178 Srv->AddSocket(ident);
181 virtual bool OnCheckReady(userrec* user)
183 // The socket engine will clean up their ident request for us when it completes,
184 // either due to timeout or due to closing, so, we just hold them until they dont
185 // have an ident field any more.
186 RFC1413* ident = (RFC1413*)user->GetExt("ident_data");
190 virtual void OnUserDisconnect(userrec* user)
192 // when the user quits tidy up any ident lookup they have pending to keep things tidy.
193 // When we call RemoveSocket, the abstractions tied into the system evnetually work their
194 // way to RFC1459::OnClose(), which shrinks off the ident_data for us, so we dont need
195 // to do it here. If we don't tidy this up, there may still be lingering idents for users
196 // who have quit, as class RFC1459 is only loosely bound to userrec* via a pair of pointers
197 // and this would leave at least one of the invalid ;)
198 RFC1413* ident = (RFC1413*)user->GetExt("ident_data");
201 Srv->RemoveSocket(ident);
205 virtual ~ModuleIdent()
209 virtual Version GetVersion()
211 return Version(1,5,0,0,VF_VENDOR);
216 class ModuleIdentFactory : public ModuleFactory
223 ~ModuleIdentFactory()
227 virtual Module * CreateModule(Server* Me)
229 return new ModuleIdent(Me);
235 extern "C" void * init_module( void )
237 return new ModuleIdentFactory;