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>
22 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
24 enum CGItype { INVALID, PASS, IDENT, PASSFIRST, IDENTFIRST, WEBIRC };
27 /** Holds a CGI site's details
29 class CGIhost : public classbase
36 CGIhost(const std::string &mask = "", CGItype t = IDENTFIRST, const std::string &password ="")
37 : hostmask(mask), type(t), password(password)
41 typedef std::vector<CGIhost> CGIHostlist;
43 class CommandWebirc : public Command
49 CommandWebirc(InspIRCd* Me, CGIHostlist &Hosts, bool notify) : Command(Me, "WEBIRC", 0, 4, true), Hosts(Hosts), notify(notify)
51 this->source = "m_cgiirc.so";
52 this->syntax = "password client hostname ip";
54 CmdResult Handle(const char** parameters, int pcnt, User *user)
56 if(user->registered == REG_ALL)
59 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
61 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
63 if(iter->type == WEBIRC && parameters[0] == iter->password)
65 user->Extend("cgiirc_realhost", new std::string(user->host));
66 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
68 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);
69 user->Extend("cgiirc_webirc_hostname", new std::string(parameters[2]));
70 user->Extend("cgiirc_webirc_ip", new std::string(parameters[3]));
80 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
82 class CGIResolver : public Resolver
89 CGIResolver(Module* me, InspIRCd* ServerInstance, bool NotifyOpers, const std::string &source, bool forward, User* u, int userfd, const std::string &type, bool &cached)
90 : Resolver(ServerInstance, source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
92 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum = 0)
97 /* Check the user still exists */
98 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
101 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());
103 strlcpy(them->host, result.c_str(), 63);
104 strlcpy(them->dhost, result.c_str(), 63);
105 strlcpy(them->ident, "~cgiirc", 8);
106 them->InvalidateCache();
110 virtual void OnError(ResolverError e, const std::string &errormessage)
112 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
115 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());
119 virtual ~CGIResolver()
124 class ModuleCgiIRC : public Module
126 CommandWebirc* mycommand;
130 ModuleCgiIRC(InspIRCd* Me) : Module(Me)
134 mycommand=new CommandWebirc(Me, Hosts, NotifyOpers);
135 ServerInstance->AddCommand(mycommand);
136 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCleanup, I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnUserQuit, I_OnUserConnect };
137 ServerInstance->Modules->Attach(eventlist, this, 7);
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 void Prioritize()
147 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIO_FIRST);
150 virtual void OnRehash(User* user, const std::string ¶meter)
152 ConfigReader Conf(ServerInstance);
154 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
156 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
159 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
161 std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
162 std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
163 std::string password = Conf.ReadValue("cgihost", "password", i);
165 if(hostmask.length())
167 if (type == "webirc" && !password.length()) {
168 ServerInstance->Log(DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
172 CGItype cgitype = INVALID;
175 else if (type == "ident")
177 else if (type == "passfirst")
179 else if (type == "webirc")
184 if (cgitype == INVALID)
187 Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
192 ServerInstance->Log(DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
198 virtual void OnCleanup(int target_type, void* item)
200 if(target_type == TYPE_USER)
202 User* user = (User*)item;
203 std::string* realhost;
206 if(user->GetExt("cgiirc_realhost", realhost))
209 user->Shrink("cgiirc_realhost");
212 if(user->GetExt("cgiirc_realip", realip))
215 user->Shrink("cgiirc_realip");
220 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
222 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
226 if(user->GetExt(extname, data))
228 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
233 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
235 if(target_type == TYPE_USER)
237 User* dest = (User*)target;
239 if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
241 dest->Extend(extname, new std::string(extdata));
246 virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
248 OnCleanup(TYPE_USER, user);
252 virtual int OnUserRegister(User* user)
254 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
256 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
259 if(iter->type == PASS)
261 CheckPass(user); // We do nothing if it fails so...
263 else if(iter->type == PASSFIRST && !CheckPass(user))
265 // If the password lookup failed, try the ident
266 CheckIdent(user); // If this fails too, do nothing
268 else if(iter->type == IDENT)
270 CheckIdent(user); // Nothing on failure.
272 else if(iter->type == IDENTFIRST && !CheckIdent(user))
274 // If the ident lookup fails, try the password.
277 else if(iter->type == WEBIRC)
279 // We don't need to do anything here
287 virtual void OnUserConnect(User* user)
289 std::string *webirc_hostname, *webirc_ip;
290 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
292 strlcpy(user->host,webirc_hostname->c_str(),63);
293 strlcpy(user->dhost,webirc_hostname->c_str(),63);
294 delete webirc_hostname;
295 user->InvalidateCache();
296 user->Shrink("cgiirc_webirc_hostname");
298 if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
301 user->RemoveCloneCounts();
303 valid = (inet_pton(AF_INET6, webirc_ip->c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
306 valid = (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr));
308 if (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr))
313 user->InvalidateCache();
314 user->Shrink("cgiirc_webirc_ip");
315 ServerInstance->AddLocalClone(user);
316 ServerInstance->AddGlobalClone(user);
321 bool CheckPass(User* user)
323 if(IsValidHost(user->password))
325 user->Extend("cgiirc_realhost", new std::string(user->host));
326 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
327 strlcpy(user->host, user->password, 64);
328 strlcpy(user->dhost, user->password, 64);
329 user->InvalidateCache();
332 user->RemoveCloneCounts();
334 if (user->GetProtocolFamily() == AF_INET6)
335 valid = (inet_pton(AF_INET6, user->password, &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
337 valid = (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr));
339 if (inet_aton(user->password, &((sockaddr_in*)user->ip)->sin_addr))
342 ServerInstance->AddLocalClone(user);
343 ServerInstance->AddGlobalClone(user);
348 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
350 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);
354 /* We got as resolved hostname in the password. */
359 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
360 ServerInstance->AddResolver(r, cached);
365 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
372 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick, user->host, user->password);*/
380 bool CheckIdent(User* user)
385 int len = strlen(user->ident);
389 else if(len == 9 && *user->ident == '~')
390 ident = user->ident+1;
394 for(int i = 0; i < 4; i++)
395 if(!HexToInt(ip[i], ident + i*2))
398 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
400 user->Extend("cgiirc_realhost", new std::string(user->host));
401 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
402 user->RemoveCloneCounts();
404 if (user->GetProtocolFamily() == AF_INET6)
405 inet_pton(AF_INET6, newip, &((sockaddr_in6*)user->ip)->sin6_addr);
408 inet_aton(newip, &((sockaddr_in*)user->ip)->sin_addr);
409 ServerInstance->AddLocalClone(user);
410 ServerInstance->AddGlobalClone(user);
414 strlcpy(user->host, newip, 16);
415 strlcpy(user->dhost, newip, 16);
416 strlcpy(user->ident, "~cgiirc", 8);
419 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
420 ServerInstance->AddResolver(r, cached);
424 strlcpy(user->host, newip, 16);
425 strlcpy(user->dhost, newip, 16);
426 strlcpy(user->ident, "~cgiirc", 8);
427 user->InvalidateCache();
430 ServerInstance->WriteOpers("*** Connecting user %s detected as using CGI:IRC (%s), but i could not resolve their hostname!", user->nick, user->host);
432 /*strlcpy(user->host, newip, 16);
433 strlcpy(user->dhost, newip, 16);
434 strlcpy(user->ident, "~cgiirc", 8);*/
439 bool IsValidHost(const std::string &host)
444 for(unsigned int i = 0; i < host.size(); i++)
446 if( ((host[i] >= '0') && (host[i] <= '9')) ||
447 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
448 ((host[i] >= 'a') && (host[i] <= 'z')) ||
449 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
450 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
460 bool IsValidIP(const std::string &ip)
462 if(ip.size() < 7 || ip.size() > 15)
468 for(unsigned int i = 0; i < ip.size(); i++)
470 if((dots <= 3) && (sincedot <= 3))
472 if((ip[i] >= '0') && (ip[i] <= '9'))
476 else if(ip[i] == '.')
495 bool HexToInt(int &out, const char* in)
501 out = strtol(ip, NULL, 16);
503 if(out > 255 || out < 0)
509 virtual ~ModuleCgiIRC()
513 virtual Version GetVersion()
515 return Version(1,1,0,0,VF_VENDOR,API_VERSION);
520 MODULE_INIT(ModuleCgiIRC)