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 * ---------------------------------------------------
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
25 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
27 enum CGItype { PASS, IDENT, PASSFIRST, IDENTFIRST };
29 /** Holds a CGI site's details
31 class CGIhost : public classbase
37 CGIhost(const std::string &mask = "", CGItype t = IDENTFIRST)
38 : hostmask(mask), type(t)
43 typedef std::vector<CGIhost> CGIHostlist;
45 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
47 class CGIResolver : public Resolver
54 CGIResolver(Module* me, InspIRCd* ServerInstance, bool NotifyOpers, const std::string &source, bool forward, userrec* u, int userfd, const std::string &type, bool &cached)
55 : Resolver(ServerInstance, source, forward ? DNS_QUERY_FORWARD : DNS_QUERY_REVERSE, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
57 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
59 /* Check the user still exists */
60 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
63 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", them->nick, them->host, result.c_str(), typ.c_str());
65 strlcpy(them->host, result.c_str(), 63);
66 strlcpy(them->dhost, result.c_str(), 63);
67 strlcpy(them->ident, "~cgiirc", 8);
71 virtual void OnError(ResolverError e, const std::string &errormessage)
73 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
76 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but their host can't be resolved from their %s!", them->nick, them->host,typ.c_str());
80 virtual ~CGIResolver()
85 class ModuleCgiIRC : public Module
91 ModuleCgiIRC(InspIRCd* Me) : Module::Module(Me)
97 void Implements(char* List)
99 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCleanup] = List[I_OnSyncUserMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserQuit] = 1;
102 virtual Priority Prioritize()
104 // We want to get here before m_cloaking and m_hostchange etc
105 return PRIORITY_FIRST;
108 virtual void OnRehash(userrec* user, const std::string ¶meter)
110 ConfigReader Conf(ServerInstance);
112 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
114 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
117 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
119 std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
120 std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
122 if(hostmask.length())
124 Hosts.push_back(CGIhost(hostmask));
127 Hosts.back().type = PASS;
128 else if(type == "ident")
129 Hosts.back().type = IDENT;
130 else if(type == "passfirst")
131 Hosts.back().type = PASSFIRST;
135 ServerInstance->Log(DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
141 virtual void OnCleanup(int target_type, void* item)
143 if(target_type == TYPE_USER)
145 userrec* user = (userrec*)item;
146 std::string* realhost;
149 if(user->GetExt("cgiirc_realhost", realhost))
152 user->Shrink("cgiirc_realhost");
155 if(user->GetExt("cgiirc_realip", realip))
158 user->Shrink("cgiirc_realip");
163 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname)
165 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
169 if(user->GetExt(extname, data))
171 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
176 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
178 if(target_type == TYPE_USER)
180 userrec* dest = (userrec*)target;
182 if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
184 dest->Extend(extname, new std::string(extdata));
189 virtual void OnUserQuit(userrec* user, const std::string &message)
191 OnCleanup(TYPE_USER, user);
195 virtual int OnUserRegister(userrec* user)
197 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
199 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
202 if(iter->type == PASS)
204 CheckPass(user); // We do nothing if it fails so...
206 else if(iter->type == PASSFIRST && !CheckPass(user))
208 // If the password lookup failed, try the ident
209 CheckIdent(user); // If this fails too, do nothing
211 else if(iter->type == IDENT)
213 CheckIdent(user); // Nothing on failure.
215 else if(iter->type == IDENTFIRST && !CheckIdent(user))
217 // If the ident lookup fails, try the password.
226 bool CheckPass(userrec* user)
228 if(IsValidHost(user->password))
230 user->Extend("cgiirc_realhost", new std::string(user->host));
231 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
232 strlcpy(user->host, user->password, 64);
233 strlcpy(user->dhost, user->password, 64);
236 if (insp_aton(user->password, (insp_inaddr*)&((sockaddr_in6*)&user->ip)->sin6_addr))
238 if (insp_aton(user->password, (insp_inaddr*)&((sockaddr_in*)&user->ip)->sin_addr))
241 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
243 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
247 /* We got as resolved hostname in the password. */
251 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
252 ServerInstance->AddResolver(r, cached);
254 catch (ModuleException& e)
257 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
264 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
272 bool CheckIdent(userrec* user)
277 int len = strlen(user->ident);
281 else if(len == 9 && *user->ident == '~')
282 ident = user->ident+1;
286 for(int i = 0; i < 4; i++)
287 if(!HexToInt(ip[i], ident + i*2))
290 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
292 user->Extend("cgiirc_realhost", new std::string(user->host));
293 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
295 insp_aton(newip, (insp_inaddr*)&((sockaddr_in6*)&user->ip)->sin6_addr);
297 insp_aton(newip, (insp_inaddr*)&((sockaddr_in*)&user->ip)->sin_addr);
303 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
304 ServerInstance->AddResolver(r, cached);
306 catch (ModuleException& e)
308 strlcpy(user->host, newip, 16);
309 strlcpy(user->dhost, newip, 16);
310 strlcpy(user->ident, "~cgiirc", 8);
313 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
315 /*strlcpy(user->host, newip, 16);
316 strlcpy(user->dhost, newip, 16);
317 strlcpy(user->ident, "~cgiirc", 8);*/
322 bool IsValidHost(const std::string &host)
327 for(unsigned int i = 0; i < host.size(); i++)
329 if( ((host[i] >= '0') && (host[i] <= '9')) ||
330 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
331 ((host[i] >= 'a') && (host[i] <= 'z')) ||
332 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
333 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
343 bool IsValidIP(const std::string &ip)
345 if(ip.size() < 7 || ip.size() > 15)
351 for(unsigned int i = 0; i < ip.size(); i++)
353 if((dots <= 3) && (sincedot <= 3))
355 if((ip[i] >= '0') && (ip[i] <= '9'))
359 else if(ip[i] == '.')
378 bool HexToInt(int &out, const char* in)
384 out = strtol(ip, NULL, 16);
386 if(out > 255 || out < 0)
392 virtual ~ModuleCgiIRC()
396 virtual Version GetVersion()
398 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
403 class ModuleCgiIRCFactory : public ModuleFactory
406 ModuleCgiIRCFactory()
410 ~ModuleCgiIRCFactory()
414 virtual Module * CreateModule(InspIRCd* Me)
416 return new ModuleCgiIRC(Me);
422 extern "C" void * init_module( void )
424 return new ModuleCgiIRCFactory;