1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
19 /* $ModDesc: Scans locally connecting clients for proxies. */
22 * How this works: basic overview.
23 * We create a socket type (derived from EventHandler -- don't feel like
24 * belting my head against the wall ala ident). For each test in the
25 * configuration file for each locally connecting client, we create a ProxySocket
26 * and run the associated test.
28 * The user is allowed to connect (delaying the connect might take ages..), and they
29 * will be destroyed *if* a bad response comes back to a test.
33 * A few things to note:
35 * O The only place that may *delete* an active or inactive
36 * ident socket is OnUserDisconnect in the module class.
37 * Because this is out of scope of the socket class there is
38 * no possibility that the socket may ever try to delete
41 * O Closure of the ident socket with the Close() method will
42 * not cause removal of the socket from memory or detatchment
43 * from its 'parent' User class. It will only flag it as an
44 * inactive socket in the socket engine.
46 * O Timeouts are handled in OnCheckReaady at the same time as
47 * checking if the ident socket has a result. This is done
48 * by checking if the age the of the class (its instantiation
49 * time) plus the timeout value is greater than the current time.
51 * O The ident socket is able to but should not modify its
52 * 'parent' user directly. Instead the ident socket class sets
53 * a completion flag and during the next call to OnCheckReady,
54 * the completion flag will be checked and any result copied to
55 * that user's class. This again ensures a single point of socket
56 * deletion for safer, neater code.
58 * O The code in the constructor of the ident socket is taken from
59 * BufferedSocket but majorly thinned down. It works for both
62 * O In the event that the ident socket throws a ModuleException,
63 * nothing is done. This is counted as total and complete
64 * failure to create a connection.
65 * --------------------------------------------------------------
68 class ProxySocket : public EventHandler
71 User *user; /* User we are attached to */
72 InspIRCd *ServerInstance; /* Server instance */
73 char challenge[10]; /* what is sent on connect, as bytes */
75 char response[20]; /* what we kill for on recieve, as bytes */
79 ProxySocket(InspIRCd *Server, User* u, const std::string &bindip, int port, char *cstr, int clen, char *rstr, int rlen)
82 ServerInstance = Server;
88 /* byte for byte copies of challenge and response. */
89 for (i = 0; i != clen; i++)
91 this->challenge[i] = cstr[i];
94 for (i = 0; i != rlen; i++)
96 this->response[i] = rstr[i];
101 /* Does this look like a v6 ip address? */
103 if ((bindip.empty()) || bindip.find(':') != std::string::npos)
107 SetFd(socket(AF_INET6, SOCK_STREAM, 0));
110 SetFd(socket(AF_INET, SOCK_STREAM, 0));
113 throw ModuleException("Could not create socket");
115 /* We allocate two of these because sizeof(sockaddr_in6) > sizeof(sockaddr_in) */
116 sockaddr* s = new sockaddr[2];
117 sockaddr* addr = new sockaddr[2];
120 /* Horrid icky nasty ugly berkely socket crap. */
125 if (inet_pton(AF_INET6, user->GetIPString(), &addy) > 0)
127 ((sockaddr_in6*)addr)->sin6_family = AF_INET6;
128 memcpy(&((sockaddr_in6*)addr)->sin6_addr, &addy, sizeof(addy));
129 ((sockaddr_in6*)addr)->sin6_port = htons(port);
130 size = sizeof(sockaddr_in6);
131 inet_pton(AF_INET6, bindip.c_str(), &n);
132 memcpy(&((sockaddr_in6*)s)->sin6_addr, &n, sizeof(sockaddr_in6));
133 ((sockaddr_in6*)s)->sin6_port = 0;
134 ((sockaddr_in6*)s)->sin6_family = AF_INET6;
142 if (inet_aton(user->GetIPString(), &addy) > 0)
144 ((sockaddr_in*)addr)->sin_family = AF_INET;
145 ((sockaddr_in*)addr)->sin_addr = addy;
146 ((sockaddr_in*)addr)->sin_port = htons(port);
147 size = sizeof(sockaddr_in);
148 inet_aton(bindip.c_str(), &n);
149 ((sockaddr_in*)s)->sin_addr = n;
150 ((sockaddr_in*)s)->sin_port = 0;
151 ((sockaddr_in*)s)->sin_family = AF_INET;
155 /* Attempt to bind (ident requests must come from the ip the query is referring to */
156 if (ServerInstance->SE->Bind(GetFd(), s, size) < 0)
161 throw ModuleException("failed to bind()");
165 ServerInstance->SE->NonBlocking(GetFd());
167 /* Attempt connection (nonblocking) */
168 if (ServerInstance->SE->Connect(this, (sockaddr*)addr, size) == -1 && errno != EINPROGRESS)
172 throw ModuleException("connect() failed");
177 /* Add fd to socket engine */
178 if (!ServerInstance->SE->AddFd(this))
181 throw ModuleException("out of fds");
184 /* Important: We set WantWrite immediately after connect()
185 * because a successful connection will trigger a writability event
187 ServerInstance->SE->WantWrite(this);
190 virtual void OnConnected()
192 ServerInstance->Log(DEBUG,"OnConnected()");
194 /* Both sockaddr_in and sockaddr_in6 can be safely casted to sockaddr, especially since the
195 * only members we use are in a part of the struct that should always be identical (at the
198 sockaddr_in laddr, raddr;
200 sockaddr_in6 laddr, raddr;
203 socklen_t laddrsz = sizeof(laddr);
204 socklen_t raddrsz = sizeof(raddr);
206 if ((getsockname(user->GetFd(), (sockaddr*) &laddr, &laddrsz) != 0) || (getpeername(user->GetFd(), (sockaddr*) &raddr, &raddrsz) != 0))
212 /* Send failed if we didnt write the whole ident request --
213 * might as well give up if this happens!
215 ServerInstance->Log(DEBUG, "Sending");
216 if (ServerInstance->SE->Send(this, this->challenge, this->clen, 0) < this->clen)
218 ServerInstance->Log(DEBUG, "Send incomplete");
223 virtual void HandleEvent(EventType et, int errornum = 0)
228 /* fd readable event, received ident response */
232 /* fd writeable event, successfully connected! */
236 /* fd error event, ohshi- */
237 ServerInstance->Log(DEBUG,"EVENT_ERROR");
238 /* We *must* Close() here immediately or we get a
239 * huge storm of EVENT_ERROR events!
249 /* Remove ident socket from engine, and close it, but dont detatch it
250 * from its parent user class, or attempt to delete its memory.
254 ServerInstance->Log(DEBUG,"Close ident socket %d", GetFd());
255 ServerInstance->SE->DelFd(this);
256 ServerInstance->SE->Close(GetFd());
257 ServerInstance->SE->Shutdown(GetFd(), SHUT_WR);
269 /* We don't really need to buffer for incomplete replies here, since IDENT replies are
270 * extremely short - there is *no* sane reason it'd be in more than one packet
273 int recvresult = ServerInstance->SE->Recv(this, ibuf, MAXBUF-1, 0);
275 ServerInstance->Log(DEBUG,"ReadResponse(): %s -- %d", ibuf, recvresult);
280 for (i = 0; i != this->rlen && i != recvresult; i++)
282 if (this->response[i] != ibuf[i])
284 ServerInstance->Log(DEBUG, "No match at pos %d: %c ne %c", i, this->response[i], ibuf[i]);
292 User::QuitUser(ServerInstance, this->user, "Open proxy detected.");
295 /* Close (but dont delete from memory) our socket
304 class ModuleProxy : public Module
309 ModuleProxy(InspIRCd *Me)
313 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCleanup, I_OnUserDisconnect };
314 ServerInstance->Modules->Attach(eventlist, this, 4);
317 virtual Version GetVersion()
319 return Version(1, 1, 1, 0, VF_VENDOR, API_VERSION);
322 virtual void Implements(char *List)
324 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCleanup] = List[I_OnUserDisconnect] = 1;
327 virtual void OnRehash(User *user, const std::string ¶m)
329 ConfigReader MyConf(ServerInstance);
331 RequestTimeout = MyConf.ReadInteger("ident", "timeout", 0, true);
336 virtual int OnUserRegister(User *user)
338 user->WriteServ("NOTICE Auth :*** Checking you for proxies...");
340 // Get the IP that the user is connected to, and bind to that for the outgoing connection
346 socklen_t laddrsz = sizeof(laddr);
348 if (getsockname(user->GetFd(), (sockaddr*) &laddr, &laddrsz) != 0)
354 const char *ip = inet_ntoa(laddr.sin_addr);
356 char ip[INET6_ADDRSTRLEN + 1];
357 inet_ntop(laddr.sin6_family, &laddr.sin6_addr, ip, INET6_ADDRSTRLEN);
360 ProxySocket *p = NULL;
363 p = new ProxySocket(ServerInstance, user, ip, 80, "GET /\n", 7, "Nothing here.", 12);
365 catch (ModuleException &e)
367 ServerInstance->Log(DEBUG,"Proxy exception: %s", e.GetReason());
371 user->Extend("proxy_socket", p);
375 virtual void OnCleanup(int target_type, void *item)
377 /* Module unloading, tidy up users */
378 if (target_type == TYPE_USER)
379 OnUserDisconnect((User*)item);
382 virtual void OnUserDisconnect(User *user)
384 /* User disconnect (generic socket detatch event) */
385 ProxySocket *p = NULL;
386 if (user->GetExt("proxy_socket", p))
390 user->Shrink("proxy_socket");
391 ServerInstance->Log(DEBUG, "Removed proxy socket from %s", user->nick);
396 MODULE_INIT(ModuleProxy)