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 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
24 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
26 enum CGItype { INVALID, PASS, IDENT, PASSFIRST, IDENTFIRST, WEBIRC };
29 /** Holds a CGI site's details
31 class CGIhost : public classbase
38 CGIhost(const std::string &mask = "", CGItype t = IDENTFIRST, const std::string &password ="")
39 : hostmask(mask), type(t), password(password)
43 typedef std::vector<CGIhost> CGIHostlist;
45 class cmd_webirc : public command_t
51 cmd_webirc(InspIRCd* Me, CGIHostlist &Hosts, bool notify) : command_t(Me, "WEBIRC", 0, 4, true), Hosts(Hosts), notify(notify)
53 this->source = "m_cgiirc.so";
54 this->syntax = "password client hostname ip";
56 CmdResult Handle(const char** parameters, int pcnt, userrec *user)
58 if(user->registered == REG_ALL)
61 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
63 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
65 if(iter->type == WEBIRC && parameters[0] == iter->password)
67 user->Extend("cgiirc_realhost", new std::string(user->host));
68 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
70 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);
71 user->Extend("cgiirc_webirc_hostname", new std::string(parameters[2]));
72 user->Extend("cgiirc_webirc_ip", new std::string(parameters[3]));
82 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
84 class CGIResolver : public Resolver
91 CGIResolver(Module* me, InspIRCd* ServerInstance, bool NotifyOpers, const std::string &source, bool forward, userrec* u, int userfd, const std::string &type, bool &cached)
92 : Resolver(ServerInstance, source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
94 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum = 0)
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 if (type == "webirc" && !password.length()) {
169 ServerInstance->Log(DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
173 CGItype cgitype = INVALID;
176 else if (type == "ident")
178 else if (type == "passfirst")
180 else if (type == "webirc")
185 if (cgitype == INVALID)
188 Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
193 ServerInstance->Log(DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
199 virtual void OnCleanup(int target_type, void* item)
201 if(target_type == TYPE_USER)
203 userrec* user = (userrec*)item;
204 std::string* realhost;
207 if(user->GetExt("cgiirc_realhost", realhost))
210 user->Shrink("cgiirc_realhost");
213 if(user->GetExt("cgiirc_realip", realip))
216 user->Shrink("cgiirc_realip");
221 virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
223 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
227 if(user->GetExt(extname, data))
229 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
234 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
236 if(target_type == TYPE_USER)
238 userrec* dest = (userrec*)target;
240 if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
242 dest->Extend(extname, new std::string(extdata));
247 virtual void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
249 OnCleanup(TYPE_USER, user);
253 virtual int OnUserRegister(userrec* user)
255 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
257 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
260 if(iter->type == PASS)
262 CheckPass(user); // We do nothing if it fails so...
264 else if(iter->type == PASSFIRST && !CheckPass(user))
266 // If the password lookup failed, try the ident
267 CheckIdent(user); // If this fails too, do nothing
269 else if(iter->type == IDENT)
271 CheckIdent(user); // Nothing on failure.
273 else if(iter->type == IDENTFIRST && !CheckIdent(user))
275 // If the ident lookup fails, try the password.
278 else if(iter->type == WEBIRC)
280 // We don't need to do anything here
288 virtual void OnUserConnect(userrec* user)
290 std::string *webirc_hostname, *webirc_ip;
291 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
293 strlcpy(user->host,webirc_hostname->c_str(),63);
294 strlcpy(user->dhost,webirc_hostname->c_str(),63);
295 delete webirc_hostname;
296 user->InvalidateCache();
297 user->Shrink("cgiirc_webirc_hostname");
299 if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
302 user->RemoveCloneCounts();
304 valid = (inet_pton(AF_INET6, webirc_ip->c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
307 valid = (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr));
309 if (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr))
314 user->InvalidateCache();
315 user->Shrink("cgiirc_webirc_ip");
316 ServerInstance->AddLocalClone(user);
317 ServerInstance->AddGlobalClone(user);
322 bool CheckPass(userrec* user)
324 if(IsValidHost(user->password))
326 user->Extend("cgiirc_realhost", new std::string(user->host));
327 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
328 strlcpy(user->host, user->password, 64);
329 strlcpy(user->dhost, user->password, 64);
330 user->InvalidateCache();
333 user->RemoveCloneCounts();
335 if (user->GetProtocolFamily() == AF_INET6)
336 valid = (inet_pton(AF_INET6, user->password, &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
338 valid = (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr));
340 if (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr))
343 ServerInstance->AddLocalClone(user);
344 ServerInstance->AddGlobalClone(user);
349 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
351 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
355 /* We got as resolved hostname in the password. */
360 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
361 ServerInstance->AddResolver(r, cached);
366 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
373 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
381 bool CheckIdent(userrec* user)
386 int len = strlen(user->ident);
390 else if(len == 9 && *user->ident == '~')
391 ident = user->ident+1;
395 for(int i = 0; i < 4; i++)
396 if(!HexToInt(ip[i], ident + i*2))
399 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
401 user->Extend("cgiirc_realhost", new std::string(user->host));
402 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
403 user->RemoveCloneCounts();
405 if (user->GetProtocolFamily() == AF_INET6)
406 inet_pton(AF_INET6, newip, &((sockaddr_in6*)user->ip)->sin6_addr);
409 inet_aton(newip, &((sockaddr_in*)user->ip)->sin_addr);
410 ServerInstance->AddLocalClone(user);
411 ServerInstance->AddGlobalClone(user);
415 strlcpy(user->host, newip, 16);
416 strlcpy(user->dhost, newip, 16);
417 strlcpy(user->ident, "~cgiirc", 8);
420 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
421 ServerInstance->AddResolver(r, cached);
425 strlcpy(user->host, newip, 16);
426 strlcpy(user->dhost, newip, 16);
427 strlcpy(user->ident, "~cgiirc", 8);
428 user->InvalidateCache();
431 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
433 /*strlcpy(user->host, newip, 16);
434 strlcpy(user->dhost, newip, 16);
435 strlcpy(user->ident, "~cgiirc", 8);*/
440 bool IsValidHost(const std::string &host)
445 for(unsigned int i = 0; i < host.size(); i++)
447 if( ((host[i] >= '0') && (host[i] <= '9')) ||
448 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
449 ((host[i] >= 'a') && (host[i] <= 'z')) ||
450 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
451 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
461 bool IsValidIP(const std::string &ip)
463 if(ip.size() < 7 || ip.size() > 15)
469 for(unsigned int i = 0; i < ip.size(); i++)
471 if((dots <= 3) && (sincedot <= 3))
473 if((ip[i] >= '0') && (ip[i] <= '9'))
477 else if(ip[i] == '.')
496 bool HexToInt(int &out, const char* in)
502 out = strtol(ip, NULL, 16);
504 if(out > 255 || out < 0)
510 virtual ~ModuleCgiIRC()
514 virtual Version GetVersion()
516 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
521 MODULE_INIT(ModuleCgiIRC)