1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/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 &spassword ="")
37 : hostmask(mask), type(t), password(spassword)
41 typedef std::vector<CGIhost> CGIHostlist;
45 * This is used for the webirc method of CGIIRC auth, and is (really) the best way to do these things.
46 * Syntax: WEBIRC password client hostname ip
47 * Where password is a shared key, client is the name of the "client" and version (e.g. cgiirc), hostname
48 * is the resolved host of the client issuing the command and IP is the real IP of the client.
51 * To tie in with the rest of cgiirc module, and to avoid race conditions, /webirc is only processed locally
52 * and simply sets metadata on the user, which is later decoded on full connect to give something meaningful.
54 class CommandWebirc : public Command
59 CommandWebirc(InspIRCd* Instance, bool bnotify) : Command(Instance, "WEBIRC", 0, 4, true), notify(bnotify)
61 this->source = "m_cgiirc.so";
62 this->syntax = "password client hostname ip";
64 CmdResult Handle(const std::vector<std::string> ¶meters, User *user)
66 if(user->registered == REG_ALL)
69 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
71 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
73 if(iter->type == WEBIRC && parameters[0] == iter->password)
75 user->Extend("cgiirc_realhost", new std::string(user->host));
76 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
78 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", user->nick.c_str(), user->host.c_str(), parameters[2].c_str(), user->host.c_str());
79 user->Extend("cgiirc_webirc_hostname", new std::string(parameters[2]));
80 user->Extend("cgiirc_webirc_ip", new std::string(parameters[3]));
86 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s tried to use WEBIRC, but didn't match any configured webirc blocks.", user->GetFullRealHost().c_str());
90 void SetHosts(CGIHostlist &phosts)
97 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
99 class CGIResolver : public Resolver
106 CGIResolver(Module* me, InspIRCd* Instance, bool NotifyOpers, const std::string &source, bool forward, User* u, int userfd, const std::string &type, bool &cached)
107 : Resolver(Instance, source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
109 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
111 /* Check the user still exists */
112 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
115 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from %s", them->nick.c_str(), them->host.c_str(), result.c_str(), typ.c_str());
117 them->host.assign(result,0, 64);
118 them->dhost.assign(result, 0, 64);
120 them->SetClientIP(result.c_str());
121 them->ident.assign("~cgiirc", 0, 8);
122 them->InvalidateCache();
123 them->CheckLines(true);
127 virtual void OnError(ResolverError e, const std::string &errormessage)
129 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
132 ServerInstance->SNO->WriteToSnoMask('a', "Connecting user %s detected as using CGI:IRC (%s), but their host can't be resolved from their %s!", them->nick.c_str(), them->host.c_str(), typ.c_str());
136 virtual ~CGIResolver()
141 class ModuleCgiIRC : public Module
143 CommandWebirc* mycommand;
147 ModuleCgiIRC(InspIRCd* Me) : Module(Me)
149 mycommand = new CommandWebirc(Me, NotifyOpers);
151 ServerInstance->AddCommand(mycommand);
153 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCleanup, I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnUserDisconnect, I_OnUserConnect };
154 ServerInstance->Modules->Attach(eventlist, this, 7);
158 virtual void Prioritize()
160 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIORITY_FIRST);
163 virtual void OnRehash(User* user)
165 ConfigReader Conf(ServerInstance);
168 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
170 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
173 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
175 std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
176 std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
177 std::string password = Conf.ReadValue("cgihost", "password", i);
179 if(hostmask.length())
181 if (type == "webirc" && !password.length()) {
182 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
186 CGItype cgitype = INVALID;
189 else if (type == "ident")
191 else if (type == "passfirst")
193 else if (type == "webirc")
198 if (cgitype == INVALID)
201 Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
206 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
211 mycommand->SetHosts(Hosts);
214 virtual void OnCleanup(int target_type, void* item)
216 if(target_type == TYPE_USER)
218 User* user = (User*)item;
219 std::string* realhost;
222 if(user->GetExt("cgiirc_realhost", realhost))
225 user->Shrink("cgiirc_realhost");
228 if(user->GetExt("cgiirc_realip", realip))
231 user->Shrink("cgiirc_realip");
236 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
238 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
242 if(user->GetExt(extname, data))
244 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
249 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
251 if(target_type == TYPE_USER)
253 User* dest = (User*)target;
255 if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
257 dest->Extend(extname, new std::string(extdata));
262 virtual void OnUserDisconnect(User* user)
264 OnCleanup(TYPE_USER, user);
268 virtual int OnUserRegister(User* user)
270 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
272 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
275 if(iter->type == PASS)
277 CheckPass(user); // We do nothing if it fails so...
278 user->CheckLines(true);
280 else if(iter->type == PASSFIRST && !CheckPass(user))
282 // If the password lookup failed, try the ident
283 CheckIdent(user); // If this fails too, do nothing
284 user->CheckLines(true);
286 else if(iter->type == IDENT)
288 CheckIdent(user); // Nothing on failure.
289 user->CheckLines(true);
291 else if(iter->type == IDENTFIRST && !CheckIdent(user))
293 // If the ident lookup fails, try the password.
295 user->CheckLines(true);
297 else if(iter->type == WEBIRC)
299 // We don't need to do anything here
307 virtual void OnUserConnect(User* user)
309 std::string *webirc_hostname, *webirc_ip;
310 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
312 user->host.assign(*webirc_hostname, 0, 64);
313 user->dhost.assign(*webirc_hostname, 0, 64);
314 delete webirc_hostname;
315 user->InvalidateCache();
316 user->Shrink("cgiirc_webirc_hostname");
318 if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
320 ServerInstance->Users->RemoveCloneCounts(user);
321 user->SetClientIP(webirc_ip->c_str());
323 user->InvalidateCache();
324 user->Shrink("cgiirc_webirc_ip");
325 ServerInstance->Users->AddLocalClone(user);
326 ServerInstance->Users->AddGlobalClone(user);
328 user->CheckLines(true);
332 bool CheckPass(User* user)
334 if(IsValidHost(user->password))
336 user->Extend("cgiirc_realhost", new std::string(user->host));
337 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
338 user->host.assign(user->password, 0, 64);
339 user->dhost.assign(user->password, 0, 64);
340 user->InvalidateCache();
343 ServerInstance->Users->RemoveCloneCounts(user);
344 valid = user->SetClientIP(user->password.c_str());
345 ServerInstance->Users->AddLocalClone(user);
346 ServerInstance->Users->AddGlobalClone(user);
351 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
353 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick.c_str(), user->host.c_str(), user->password.c_str());
357 /* We got as resolved hostname in the password. */
362 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
363 ServerInstance->AddResolver(r, cached);
368 ServerInstance->SNO->WriteToSnoMask('a', "Connecting user %s detected as using CGI:IRC (%s), but I could not resolve their hostname!", user->nick.c_str(), user->host.c_str());
372 user->password.clear();
379 bool CheckIdent(User* user)
382 int len = user->ident.length();
386 ident = user->ident.c_str();
387 else if(len == 9 && user->ident[0] == '~')
388 ident = user->ident.c_str() + 1;
393 unsigned long ipaddr = strtoul(ident, NULL, 16);
396 newip.s_addr = htonl(ipaddr);
397 char* newipstr = inet_ntoa(newip);
399 user->Extend("cgiirc_realhost", new std::string(user->host));
400 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
401 ServerInstance->Users->RemoveCloneCounts(user);
402 user->SetClientIP(newipstr);
403 ServerInstance->Users->AddLocalClone(user);
404 ServerInstance->Users->AddGlobalClone(user);
406 user->host = newipstr;
407 user->dhost = newipstr;
408 user->ident.assign("~cgiirc", 0, 8);
413 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newipstr, false, user, user->GetFd(), "IDENT", cached);
414 ServerInstance->AddResolver(r, cached);
418 user->InvalidateCache();
421 ServerInstance->SNO->WriteToSnoMask('a', "Connecting user %s detected as using CGI:IRC (%s), but I could not resolve their hostname!", user->nick.c_str(), user->host.c_str());
427 bool IsValidHost(const std::string &host)
432 for(unsigned int i = 0; i < host.size(); i++)
434 if( ((host[i] >= '0') && (host[i] <= '9')) ||
435 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
436 ((host[i] >= 'a') && (host[i] <= 'z')) ||
437 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
438 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
448 bool IsValidIP(const std::string &ip)
450 if(ip.size() < 7 || ip.size() > 15)
456 for(unsigned int i = 0; i < ip.size(); i++)
458 if((dots <= 3) && (sincedot <= 3))
460 if((ip[i] >= '0') && (ip[i] <= '9'))
464 else if(ip[i] == '.')
483 virtual ~ModuleCgiIRC()
487 virtual Version GetVersion()
489 return Version("$Id$",VF_VENDOR,API_VERSION);
494 MODULE_INIT(ModuleCgiIRC)