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 * ---------------------------------------------------
23 #include <sys/types.h>
24 #include <sys/socket.h>
36 /* $ModDesc: Provides support for RFC 1413 ident lookups */
40 // State engine constants. We have three states,
41 // connecting, waiting for data, and finished.
43 #define IDENT_STATE_CONNECT 1
44 #define IDENT_STATE_WAITDATA 2
45 #define IDENT_STATE_DONE 3
47 // Ident lookups are done by attaching an RFC1413 class to the
48 // userrec record using the Extensible system.
49 // The RFC1413 class is written especially for this module but
50 // it should be relatively standalone for anyone else who wishes
51 // to have a nonblocking ident lookup in a program :)
52 // the class operates on a simple state engine, each state of the
53 // connection incrementing a state counter, leading through to
54 // a concluding state which terminates the lookup.
59 int fd; // file descriptor
60 userrec* u; // user record that the lookup is associated with
61 sockaddr_in addr; // address we're connecting to
62 in_addr addy; // binary ip address
63 int state; // state (this class operates on a state engine)
64 char ibuf[MAXBUF]; // input buffer
65 sockaddr_in sock_us; // our port number
66 sockaddr_in sock_them; // their port number
67 socklen_t uslen; // length of our port number
68 socklen_t themlen; // length of their port number
69 int nrecv; // how many bytes we've received
70 time_t timeout_end; // how long until the operation times out
71 bool timeout; // true if we've timed out and should bail
72 char ident_request[128]; // buffer used to make up the request string
75 // The destructor makes damn sure the socket is freed :)
87 // establish an ident connection, maxtime is the time to spend trying
88 // returns true if successful, false if something was catastrophically wrong.
89 // note that failed connects are not reported here but detected in RFC1413::Poll()
90 // as the socket is nonblocking
92 bool Connect(userrec* user, int maxtime)
94 timeout_end = time(NULL)+maxtime;
96 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
98 // theres been a boo-boo... no more fd's left for us, woe is me!
99 Srv->Log(DEBUG,"Ident: socket failed for: "+std::string(user->ip));
102 inet_aton(user->ip,&addy);
103 addr.sin_family = AF_INET;
104 addr.sin_addr = addy;
105 addr.sin_port = htons(113);
108 flags = fcntl(this->fd, F_GETFL, 0);
109 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
111 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
113 // theres been an error, but EINPROGRESS just means 'right, im on it, call me later'
114 if (errno != EINPROGRESS)
116 // ... so that error isnt fatal, like the rest.
117 Srv->Log(DEBUG,"Ident: connect failed for: "+std::string(user->ip));
118 shutdown(this->fd,2);
124 Srv->Log(DEBUG,"Ident: successful connect associated with user "+std::string(user->nick));
126 this->state = IDENT_STATE_CONNECT;
130 // Poll the socket to see if we have an ident result, and if we do apply it to the user.
131 // returns false if we cannot poll for some reason (e.g. timeout).
135 if (time(NULL) > timeout_end)
138 Srv->SendServ(u->fd,"NOTICE "+std::string(u->nick)+" :*** Could not find your ident, using "+std::string(u->ident)+" instead.");
139 shutdown(this->fd,2);
146 if (state == IDENT_STATE_CONNECT)
148 // during state IDENT_STATE_CONNECT (leading up to the connect)
149 // we're watching for writeability
150 polls.events = POLLOUT;
154 // the rest of the time we're waiting for data
155 // back on the socket, or a socket close
156 polls.events = POLLIN;
158 int ret = poll(&polls,1,1);
164 case IDENT_STATE_CONNECT:
165 uslen = sizeof(sock_us);
166 themlen = sizeof(sock_them);
167 if ((getsockname(this->u->fd,(sockaddr*)&sock_us,&uslen) || getpeername(this->u->fd, (sockaddr*)&sock_them, &themlen)))
169 Srv->Log(DEBUG,"Ident: failed to get socket names, bailing to state 3");
170 shutdown(this->fd,2);
173 state = IDENT_STATE_DONE;
177 // send the request in the following format: theirsocket,oursocket
178 snprintf(ident_request,127,"%d,%d\r\n",ntohs(sock_them.sin_port),ntohs(sock_us.sin_port));
179 send(this->fd,ident_request,strlen(ident_request),0);
180 Srv->Log(DEBUG,"Sent ident request, moving to state 2");
181 state = IDENT_STATE_WAITDATA;
184 case IDENT_STATE_WAITDATA:
185 nrecv = recv(this->fd,ibuf,sizeof(ibuf),0);
188 // we have the response line in the following format:
189 // 6193, 23 : USERID : UNIX : stjohns
190 // 6195, 23 : ERROR : NO-USER
192 Srv->Log(DEBUG,"Received ident response: "+std::string(ibuf));
193 shutdown(this->fd,2);
197 char* section = strtok_r(ibuf,":",&savept);
200 if (strstr(section,"USERID"))
202 section = strtok_r(NULL,":",&savept);
205 // ID type, usually UNIX or OTHER... we dont want it, so read the next token
206 section = strtok_r(NULL,":",&savept);
209 while ((*section == ' ') && (strlen(section)>0)) section++; // strip leading spaces
210 int t = strlen(section);
211 for (int j = 0; j < t; j++)
212 if ((section[j] < 33) || (section[j]>126))
213 section[j] = '\0'; // truncate at invalid chars
216 strlcpy(u->ident,section,IDENTMAX);
217 Srv->Log(DEBUG,"IDENT SET: "+std::string(u->ident));
218 Srv->SendServ(u->fd,"NOTICE "+std::string(u->nick)+" :*** Found your ident: "+std::string(u->ident));
224 section = strtok_r(NULL,":",&savept);
226 state = IDENT_STATE_DONE;
229 case IDENT_STATE_DONE:
230 shutdown(this->fd,2);
233 Srv->Log(DEBUG,"Ident lookup is complete!");
236 Srv->Log(DEBUG,"Ident: invalid ident state!!!");
243 // returns true if the operation is completed,
244 // either due to complete request, or a timeout
248 return ((state == IDENT_STATE_DONE) || (timeout == true));
252 class ModuleIdent : public Module
261 Conf = new ConfigReader;
262 IdentTimeout = Conf->ReadInteger("ident","timeout",0,true);
266 ModuleIdent(Server* Me)
273 virtual void OnRehash(std::string parameter)
278 virtual void OnUserRegister(userrec* user)
280 // when the new user connects, before they authenticate with USER/NICK/PASS, we do
281 // their ident lookup.
283 RFC1413* ident = new RFC1413;
284 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :*** Looking up your ident...");
285 if (ident->Connect(user,IdentTimeout))
287 // attach the object to the user record
288 user->Extend("ident_data",(char*)ident);
289 // start it off polling (always good to have a head start)
290 // because usually connect has completed by now
295 // something went wrong, call an irc-ambulance!
296 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :*** Could not look up your ident.");
301 virtual bool OnCheckReady(userrec* user)
303 RFC1413* ident = (RFC1413*)user->GetExt("ident_data");
306 // this user has a pending ident lookup, poll it
311 // their ident is done, zap the structures
312 Srv->Log(DEBUG,"Ident: removing ident gubbins");
313 user->Shrink("ident_data");
315 // ...and send them on their way
318 // nope, we hold them in this state, they dont go anywhere
324 virtual void OnUserDisconnect(userrec* user)
326 // when the user quits tidy up any ident lookup they have pending to keep things tidy
327 // and to prevent a memory and FD leaks
328 RFC1413* ident = (RFC1413*)user->GetExt("ident_data");
332 user->Shrink("ident_data");
336 virtual ~ModuleIdent()
340 virtual Version GetVersion()
342 return Version(1,0,0,1,VF_VENDOR);
347 class ModuleIdentFactory : public ModuleFactory
354 ~ModuleIdentFactory()
358 virtual Module * CreateModule(Server* Me)
360 return new ModuleIdent(Me);
366 extern "C" void * init_module( void )
368 return new ModuleIdentFactory;