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 * ---------------------------------------------------
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
27 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
29 enum CGItype { PASS, IDENT, PASSFIRST, IDENTFIRST, WEBIRC };
32 /** Holds a CGI site's details
34 class CGIhost : public classbase
41 CGIhost(const std::string &mask = "", CGItype t = IDENTFIRST, const std::string &password ="")
42 : hostmask(mask), type(t), password(password)
46 typedef std::vector<CGIhost> CGIHostlist;
48 class cmd_webirc : public command_t
54 cmd_webirc(InspIRCd* Me, CGIHostlist &Hosts, bool notify) : command_t(Me, "WEBIRC", 0, 4, true), Hosts(Hosts), notify(notify)
56 this->source = "m_cgiirc.so";
57 this->syntax = "password client hostname ip";
59 CmdResult Handle(const char** parameters, int pcnt, userrec *user)
61 if(user->registered == REG_ALL)
64 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
66 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
68 if(iter->type == WEBIRC && parameters[0] == iter->password)
70 user->Extend("cgiirc_realhost", new std::string(user->host));
71 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
73 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", user->nick, user->host, parameters[2], user->host);
74 user->Extend("cgiirc_webirc_hostname", new std::string(parameters[2]));
75 user->Extend("cgiirc_webirc_ip", new std::string(parameters[3]));
85 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
87 class CGIResolver : public Resolver
94 CGIResolver(Module* me, InspIRCd* ServerInstance, bool NotifyOpers, const std::string &source, bool forward, userrec* u, int userfd, const std::string &type, bool &cached)
95 : Resolver(ServerInstance, source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
97 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
99 /* Check the user still exists */
100 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
103 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());
105 strlcpy(them->host, result.c_str(), 63);
106 strlcpy(them->dhost, result.c_str(), 63);
107 strlcpy(them->ident, "~cgiirc", 8);
108 them->InvalidateCache();
112 virtual void OnError(ResolverError e, const std::string &errormessage)
114 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
117 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());
121 virtual ~CGIResolver()
126 class ModuleCgiIRC : public Module
128 cmd_webirc* mycommand;
132 ModuleCgiIRC(InspIRCd* Me) : Module(Me)
136 mycommand=new cmd_webirc(Me, Hosts, NotifyOpers);
137 ServerInstance->AddCommand(mycommand);
140 void Implements(char* List)
142 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnCleanup] = List[I_OnSyncUserMetaData] = List[I_OnDecodeMetaData] = List[I_OnUserQuit] = List[I_OnUserConnect] = 1;
145 virtual Priority Prioritize()
147 // We want to get here before m_cloaking and m_hostchange etc
148 return PRIORITY_FIRST;
151 virtual void OnRehash(userrec* user, const std::string ¶meter)
153 ConfigReader Conf(ServerInstance);
155 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
157 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
160 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
162 std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
163 std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
164 std::string password = Conf.ReadValue("cgihost", "password", i);
166 if(hostmask.length())
168 Hosts.push_back(CGIhost(hostmask));
171 Hosts.back().type = PASS;
172 else if(type == "ident")
173 Hosts.back().type = IDENT;
174 else if(type == "passfirst")
175 Hosts.back().type = PASSFIRST;
176 else if(type == "webirc") {
177 Hosts.back().type = WEBIRC;
178 if(password.length())
179 Hosts.back().password=password;
181 ServerInstance->Log(DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
186 ServerInstance->Log(DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
192 virtual void OnCleanup(int target_type, void* item)
194 if(target_type == TYPE_USER)
196 userrec* user = (userrec*)item;
197 std::string* realhost;
200 if(user->GetExt("cgiirc_realhost", realhost))
203 user->Shrink("cgiirc_realhost");
206 if(user->GetExt("cgiirc_realip", realip))
209 user->Shrink("cgiirc_realip");
214 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
216 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
220 if(user->GetExt(extname, data))
222 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
227 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
229 if(target_type == TYPE_USER)
231 userrec* dest = (userrec*)target;
233 if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
235 dest->Extend(extname, new std::string(extdata));
240 virtual void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
242 OnCleanup(TYPE_USER, user);
246 virtual int OnUserRegister(userrec* user)
248 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
250 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
253 if(iter->type == PASS)
255 CheckPass(user); // We do nothing if it fails so...
257 else if(iter->type == PASSFIRST && !CheckPass(user))
259 // If the password lookup failed, try the ident
260 CheckIdent(user); // If this fails too, do nothing
262 else if(iter->type == IDENT)
264 CheckIdent(user); // Nothing on failure.
266 else if(iter->type == IDENTFIRST && !CheckIdent(user))
268 // If the ident lookup fails, try the password.
271 else if(iter->type == WEBIRC)
273 // We don't need to do anything here
281 virtual void OnUserConnect(userrec* user)
283 std::string *webirc_hostname, *webirc_ip;
284 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
286 strlcpy(user->host,webirc_hostname->c_str(),63);
287 strlcpy(user->dhost,webirc_hostname->c_str(),63);
288 delete webirc_hostname;
289 user->InvalidateCache();
290 user->Shrink("cgiirc_webirc_hostname");
292 if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
296 valid = (inet_pton(AF_INET6, webirc_ip->c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
299 valid = (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr));
301 if (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr))
306 user->InvalidateCache();
307 user->Shrink("cgiirc_webirc_ip");
311 bool CheckPass(userrec* user)
313 if(IsValidHost(user->password))
315 user->Extend("cgiirc_realhost", new std::string(user->host));
316 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
317 strlcpy(user->host, user->password, 64);
318 strlcpy(user->dhost, user->password, 64);
319 user->InvalidateCache();
323 if (user->GetProtocolFamily() == AF_INET6)
324 valid = (inet_pton(AF_INET6, user->password, &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
326 valid = (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr));
328 if (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr))
333 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
335 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
339 /* We got as resolved hostname in the password. */
344 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
345 ServerInstance->AddResolver(r, cached);
347 catch (ModuleException& e)
350 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
357 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
365 bool CheckIdent(userrec* user)
370 int len = strlen(user->ident);
374 else if(len == 9 && *user->ident == '~')
375 ident = user->ident+1;
379 for(int i = 0; i < 4; i++)
380 if(!HexToInt(ip[i], ident + i*2))
383 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
385 user->Extend("cgiirc_realhost", new std::string(user->host));
386 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
388 if (user->GetProtocolFamily() == AF_INET6)
389 inet_pton(AF_INET6, newip, &((sockaddr_in6*)user->ip)->sin6_addr);
391 inet_aton(newip, &((sockaddr_in*)user->ip)->sin_addr);
393 inet_aton(newip, &((sockaddr_in*)user->ip)->sin_addr);
398 strlcpy(user->host, newip, 16);
399 strlcpy(user->dhost, newip, 16);
400 strlcpy(user->ident, "~cgiirc", 8);
403 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
404 ServerInstance->AddResolver(r, cached);
406 catch (ModuleException& e)
408 strlcpy(user->host, newip, 16);
409 strlcpy(user->dhost, newip, 16);
410 strlcpy(user->ident, "~cgiirc", 8);
411 user->InvalidateCache();
414 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
416 /*strlcpy(user->host, newip, 16);
417 strlcpy(user->dhost, newip, 16);
418 strlcpy(user->ident, "~cgiirc", 8);*/
423 bool IsValidHost(const std::string &host)
428 for(unsigned int i = 0; i < host.size(); i++)
430 if( ((host[i] >= '0') && (host[i] <= '9')) ||
431 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
432 ((host[i] >= 'a') && (host[i] <= 'z')) ||
433 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
434 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
444 bool IsValidIP(const std::string &ip)
446 if(ip.size() < 7 || ip.size() > 15)
452 for(unsigned int i = 0; i < ip.size(); i++)
454 if((dots <= 3) && (sincedot <= 3))
456 if((ip[i] >= '0') && (ip[i] <= '9'))
460 else if(ip[i] == '.')
479 bool HexToInt(int &out, const char* in)
485 out = strtol(ip, NULL, 16);
487 if(out > 255 || out < 0)
493 virtual ~ModuleCgiIRC()
497 virtual Version GetVersion()
499 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
504 class ModuleCgiIRCFactory : public ModuleFactory
507 ModuleCgiIRCFactory()
511 ~ModuleCgiIRCFactory()
515 virtual Module * CreateModule(InspIRCd* Me)
517 return new ModuleCgiIRC(Me);
523 extern "C" DllExport void * init_module( void )
525 return new ModuleCgiIRCFactory;