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 */
38 #define IDENT_STATE_CONNECT 1
39 #define IDENT_STATE_WAITDATA 2
40 #define IDENT_STATE_DONE 3
42 // Ident lookups are done by attaching an RFC1413 class to the
43 // userrec record using the Extensible system.
44 // The RFC1413 class is written especially for this module but
45 // it should be relatively standalone for anyone else who wishes
46 // to have a nonblocking ident lookup in a program :)
47 // the class operates on a simple state engine, each state of the
48 // connection incrementing a state counter, leading through to
49 // a concluding state which terminates the lookup.
54 int fd; // file descriptor
55 userrec* u; // user record that the lookup is associated with
56 sockaddr_in addr; // address we're connecting to
57 in_addr addy; // binary ip address
58 int state; // state (this class operates on a state engine)
59 char ibuf[MAXBUF]; // input buffer
60 sockaddr_in sock_us; // our port number
61 sockaddr_in sock_them; // their port number
62 socklen_t uslen; // length of our port number
63 socklen_t themlen; // length of their port number
64 int nrecv; // how many bytes we've received
65 time_t timeout_end; // how long until the operation times out
66 bool timeout; // true if we've timed out and should bail
69 // establish an ident connection, maxtime is the time to spend trying
70 // returns true if successful, false if something was catastrophically wrong.
71 // note that failed connects are not reported here but detected in RFC1413::Poll()
72 // as the socket is nonblocking
74 bool Connect(userrec* user, int maxtime)
76 timeout_end = time(NULL)+maxtime;
78 if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
80 Srv->Log(DEBUG,"Ident: socket failed for: "+std::string(user->ip));
83 inet_aton(user->ip,&addy);
84 addr.sin_family = AF_INET;
86 addr.sin_port = htons(113);
89 flags = fcntl(this->fd, F_GETFL, 0);
90 fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
92 if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
94 if (errno != EINPROGRESS)
96 Srv->Log(DEBUG,"Ident: connect failed for: "+std::string(user->ip));
100 Srv->Log(DEBUG,"Ident: successful connect associated with user "+std::string(user->nick));
102 this->state = IDENT_STATE_CONNECT;
106 // Poll the socket to see if we have an ident result, and if we do apply it to the user.
107 // returns false if we cannot poll for some reason (e.g. timeout).
111 if (time(NULL) > timeout_end)
114 Srv->SendServ(u->fd,"NOTICE "+std::string(u->nick)+" :*** Could not find your ident, using "+std::string(u->ident)+" instead.");
119 if (state == IDENT_STATE_CONNECT)
121 // during state IDENT_STATE_CONNECT (leading up to the connect)
122 // we're watching for writeability
123 polls.events = POLLOUT;
127 // the rest of the time we're waiting for data
128 // back on the socket, or a socket close
129 polls.events = POLLIN;
131 int ret = poll(&polls,1,1);
137 case IDENT_STATE_CONNECT:
138 Srv->Log(DEBUG,"*** IDENT IN STATE 1");
139 uslen = sizeof(sock_us);
140 themlen = sizeof(sock_them);
141 if ((getsockname(this->u->fd,(sockaddr*)&sock_us,&uslen) || getpeername(this->u->fd, (sockaddr*)&sock_them, &themlen)))
143 Srv->Log(DEBUG,"Ident: failed to get socket names, bailing to state 3");
144 state = IDENT_STATE_DONE;
148 // send the request in the following format: theirsocket,oursocket
149 Write(this->fd,"%d,%d",ntohs(sock_them.sin_port),ntohs(sock_us.sin_port));
150 Srv->Log(DEBUG,"Sent ident request, moving to state 2");
151 state = IDENT_STATE_WAITDATA;
154 case IDENT_STATE_WAITDATA:
155 Srv->Log(DEBUG,"*** IDENT IN STATE 2");
156 nrecv = recv(this->fd,ibuf,sizeof(ibuf),0);
159 // we have the response line in the following format:
160 // 6193, 23 : USERID : UNIX : stjohns
161 // 6195, 23 : ERROR : NO-USER
163 Srv->Log(DEBUG,"Received ident response: "+std::string(ibuf));
165 shutdown(this->fd,2);
167 char* section = strtok_r(ibuf,":",&savept);
170 if (strstr(section,"USERID"))
172 section = strtok_r(NULL,":",&savept);
175 // ID type, usually UNIX or OTHER... we dont want it, so read the next token
176 section = strtok_r(NULL,":",&savept);
179 while ((*section == ' ') && (strlen(section)>0)) section++; // strip leading spaces
180 int t = strlen(section);
181 for (int j = 0; j < t; j++)
182 if ((section[j] < 33) || (section[j]>126))
183 section[j] = '\0'; // truncate at invalid chars
186 strlcpy(u->ident,section,IDENTMAX);
187 Srv->Log(DEBUG,"IDENT SET: "+std::string(u->ident));
188 Srv->SendServ(u->fd,"NOTICE "+std::string(u->nick)+" :*** Found your ident: "+std::string(u->ident));
194 section = strtok_r(NULL,":",&savept);
196 state = IDENT_STATE_DONE;
199 case IDENT_STATE_DONE:
200 Srv->Log(DEBUG,"Ident lookup is complete!");
203 Srv->Log(DEBUG,"Ident: invalid ident state!!!");
209 // returns true if the operation is completed,
210 // either due to complete request, or a timeout
214 return ((state == 3) || (timeout == true));
218 class ModuleIdent : public Module
227 Conf = new ConfigReader;
228 IdentTimeout = Conf->ReadInteger("ident","timeout",0,true);
238 virtual void OnRehash()
243 virtual void OnUserRegister(userrec* user)
245 // when the new user connects, before they authenticate with USER/NICK/PASS, we do
246 // their ident lookup.
248 RFC1413* ident = new RFC1413;
249 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :*** Looking up your ident...");
250 if (ident->Connect(user,IdentTimeout))
252 // attach the object to the user record
253 user->Extend("ident_data",(char*)ident);
254 // start it off polling (always good to have a head start)
255 // because usually connect has completed by now
260 // something went wrong, call an irc-ambulance!
261 Srv->SendServ(user->fd,"NOTICE "+std::string(user->nick)+" :*** Could not look up your ident.");
266 virtual bool OnCheckReady(userrec* user)
268 RFC1413* ident = (RFC1413*)user->GetExt("ident_data");
271 // this user has a pending ident lookup, poll it
276 // their ident is done, zap the structures
277 Srv->Log(DEBUG,"Ident: removing ident gubbins");
278 user->Shrink("ident_data");
280 // ...and send them on their way
283 // nope, we hold them in this state, they dont go anywhere
289 virtual ~ModuleIdent()
294 virtual Version GetVersion()
296 return Version(1,0,0,1,VF_VENDOR);
301 class ModuleIdentFactory : public ModuleFactory
308 ~ModuleIdentFactory()
312 virtual Module * CreateModule()
314 return new ModuleIdent;
320 extern "C" void * init_module( void )
322 return new ModuleIdentFactory;