1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 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 * ---------------------------------------------------
21 #include <sys/types.h>
22 #include <sys/socket.h>
34 /* $ModDesc: Provides support for RFC 1413 ident lookups */
48 sockaddr_in sock_them;
55 bool Connect(userrec* user, int maxtime)
57 timeout_end = time(NULL)+maxtime;
59 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
61 Srv->Log(DEBUG,"Ident: socket failed for: "+std::string(user->ip));
64 inet_aton(user->ip,&addy);
65 addr.sin_family = AF_INET;
67 addr.sin_port = htons(113);
70 flags = fcntl(this->fd, F_GETFL, 0);
71 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
73 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
75 if (errno != EINPROGRESS)
77 Srv->Log(DEBUG,"Ident: connect failed for: "+std::string(user->ip));
81 Srv->Log(DEBUG,"Ident: successful connect associated with user "+std::string(user->nick));
89 if (time(NULL) > timeout_end)
92 Srv->SendServ(u->fd,"NOTICE "+std::string(u->nick)+" :*** Could not find your ident, using "+std::string(u->ident)+" instead.");
99 polls.events = POLLOUT;
103 polls.events = POLLIN;
105 int ret = poll(&polls,1,1);
112 Srv->Log(DEBUG,"*** IDENT IN STATE 1");
113 uslen = sizeof(sock_us);
114 themlen = sizeof(sock_them);
115 if ((getsockname(this->u->fd,(sockaddr*)&sock_us,&uslen) || getpeername(this->u->fd, (sockaddr*)&sock_them, &themlen)))
117 Srv->Log(DEBUG,"Ident: failed to get socket names, bailing to state 3");
122 // send the request in the following format: theirsocket,oursocket
123 Write(this->fd,"%d,%d",ntohs(sock_them.sin_port),ntohs(sock_us.sin_port));
124 Srv->Log(DEBUG,"Sent ident request, moving to state 2");
129 Srv->Log(DEBUG,"*** IDENT IN STATE 2");
130 nrecv = recv(this->fd,ibuf,sizeof(ibuf),0);
133 // we have the response line in the following format:
134 // 6193, 23 : USERID : UNIX : stjohns
135 // 6195, 23 : ERROR : NO-USER
137 Srv->Log(DEBUG,"Received ident response: "+std::string(ibuf));
139 shutdown(this->fd,2);
141 char* section = strtok_r(ibuf,":",&savept);
144 if (strstr(section,"USERID"))
146 section = strtok_r(NULL,":",&savept);
149 // ID type, usually UNIX or OTHER... we dont want it, so read the next token
150 section = strtok_r(NULL,":",&savept);
153 while ((*section == ' ') && (strlen(section)>0)) section++; // strip leading spaces
154 if ((section[strlen(section)-1] == 13) || (section[strlen(section)-1] == 10))
155 section[strlen(section)-1] = '\0'; // strip carriage returns
156 if ((section[strlen(section)-1] == 13) || (section[strlen(section)-1] == 10))
157 section[strlen(section)-1] = '\0'; // strip linefeeds
158 while ((section[strlen(section)-1] == ' ') && (strlen(section)>0)) // strip trailing spaces
159 section[strlen(section)-1] = '\0';
162 strlcpy(u->ident,section,IDENTMAX);
163 Srv->Log(DEBUG,"IDENT SET: "+std::string(u->ident));
164 Srv->SendServ(u->fd,"NOTICE "+std::string(u->nick)+" :*** Found your ident: "+std::string(u->ident));
170 section = strtok_r(NULL,":",&savept);
176 Srv->Log(DEBUG,"Ident lookup is complete!");
179 Srv->Log(DEBUG,"Ident: invalid ident state!!!");
187 return ((state == 3) || (timeout == true));
191 class ModuleIdent : public Module
200 Conf = new ConfigReader;
201 IdentTimeout = Conf->ReadInteger("ident","timeout",0,true);
211 virtual void OnRehash()
216 virtual void OnUserRegister(userrec* user)
218 RFC1413* ident = new RFC1413;
219 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :*** Looking up your ident...");
220 if (ident->Connect(user,IdentTimeout))
222 user->Extend("ident_data",(char*)ident);
227 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :*** Could not look up your ident.");
232 virtual bool OnCheckReady(userrec* user)
234 RFC1413* ident = (RFC1413*)user->GetExt("ident_data");
240 Srv->Log(DEBUG,"Ident: removing ident gubbins");
241 user->Shrink("ident_data");
250 virtual ~ModuleIdent()
255 virtual Version GetVersion()
257 return Version(1,0,0,1,VF_VENDOR);
262 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
264 class ModuleIdentFactory : public ModuleFactory
271 ~ModuleIdentFactory()
275 virtual Module * CreateModule()
277 return new ModuleIdent;
283 extern "C" void * init_module( void )
285 return new ModuleIdentFactory;