2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007-2008 John Brooks <john.brooks@dereferenced.net>
6 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
7 * Copyright (C) 2006-2008 Craig Edwards <craigedwards@brainbox.cc>
8 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
9 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10 * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
31 enum CGItype { PASS, IDENT, PASSFIRST, IDENTFIRST, WEBIRC };
34 /** Holds a CGI site's details
43 CGIhost(const std::string &mask, CGItype t, const std::string &spassword)
44 : hostmask(mask), type(t), password(spassword)
48 typedef std::vector<CGIhost> CGIHostlist;
52 * This is used for the webirc method of CGIIRC auth, and is (really) the best way to do these things.
53 * Syntax: WEBIRC password client hostname ip
54 * Where password is a shared key, client is the name of the "client" and version (e.g. cgiirc), hostname
55 * is the resolved host of the client issuing the command and IP is the real IP of the client.
58 * To tie in with the rest of cgiirc module, and to avoid race conditions, /webirc is only processed locally
59 * and simply sets metadata on the user, which is later decoded on full connect to give something meaningful.
61 class CommandWebirc : public Command
65 StringExtItem realhost;
67 LocalStringExt webirc_hostname;
68 LocalStringExt webirc_ip;
71 CommandWebirc(Module* Creator)
72 : Command(Creator, "WEBIRC", 4),
73 realhost("cgiirc_realhost", Creator), realip("cgiirc_realip", Creator),
74 webirc_hostname("cgiirc_webirc_hostname", Creator), webirc_ip("cgiirc_webirc_ip", Creator)
76 allow_empty_last_param = false;
77 works_before_reg = true;
78 this->syntax = "password client hostname ip";
80 CmdResult Handle(const std::vector<std::string> ¶meters, User *user)
82 if(user->registered == REG_ALL)
85 irc::sockets::sockaddrs ipaddr;
86 if (!irc::sockets::aptosa(parameters[3], 0, ipaddr))
88 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s tried to use WEBIRC but gave an invalid IP address.", user->GetFullRealHost().c_str());
92 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
94 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
96 if(iter->type == WEBIRC && parameters[0] == iter->password)
98 realhost.set(user, user->host);
99 realip.set(user, user->GetIPString());
101 bool host_ok = (parameters[2].length() < 64);
102 const std::string& newhost = (host_ok ? parameters[2] : parameters[3]);
105 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(), newhost.c_str(), user->host.c_str());
107 // Check if we're happy with the provided hostname. If it's problematic then make sure we won't set a host later, just the IP
109 webirc_hostname.set(user, parameters[2]);
111 webirc_hostname.unset(user);
113 webirc_ip.set(user, parameters[3]);
119 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s tried to use WEBIRC, but didn't match any configured webirc blocks.", user->GetFullRealHost().c_str());
125 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
127 class CGIResolver : public Resolver
130 std::string theiruid;
131 LocalIntExt& waiting;
134 CGIResolver(Module* me, bool NotifyOpers, const std::string &source, LocalUser* u,
135 const std::string &type, bool &cached, LocalIntExt& ext)
136 : Resolver(source, DNS_QUERY_PTR4, cached, me), typ(type), theiruid(u->uuid),
137 waiting(ext), notify(NotifyOpers)
141 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
143 /* Check the user still exists */
144 User* them = ServerInstance->FindUUID(theiruid);
145 if ((them) && (!them->quitting))
148 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());
150 if (result.length() > 64)
153 them->dhost = result;
154 them->InvalidateCache();
155 them->CheckLines(true);
159 virtual void OnError(ResolverError e, const std::string &errormessage)
164 User* them = ServerInstance->FindUUID(theiruid);
165 if ((them) && (!them->quitting))
167 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());
171 virtual ~CGIResolver()
173 User* them = ServerInstance->FindUUID(theiruid);
176 int count = waiting.get(them);
178 waiting.set(them, count - 1);
182 class ModuleCgiIRC : public Module
187 static void RecheckClass(LocalUser* user)
189 user->MyClass = NULL;
194 static void ChangeIP(LocalUser* user, const std::string& newip)
196 ServerInstance->Users->RemoveCloneCounts(user);
197 user->SetClientIP(newip.c_str());
198 ServerInstance->Users->AddLocalClone(user);
199 ServerInstance->Users->AddGlobalClone(user);
202 void HandleIdentOrPass(LocalUser* user, const std::string& newip, bool was_pass)
204 cmd.realhost.set(user, user->host);
205 cmd.realip.set(user, user->GetIPString());
206 ChangeIP(user, newip);
207 user->host = user->dhost = user->GetIPString();
208 user->InvalidateCache();
210 // Don't create the resolver if the core couldn't put the user in a connect class or when dns is disabled
211 if (user->quitting || ServerInstance->Config->NoUserDns)
217 CGIResolver* r = new CGIResolver(this, cmd.notify, newip, user, (was_pass ? "PASS" : "IDENT"), cached, waiting);
218 waiting.set(user, waiting.get(user) + 1);
219 ServerInstance->AddResolver(r, cached);
224 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());
229 ModuleCgiIRC() : cmd(this), waiting("cgiirc-delay", this)
236 ServiceProvider* providerlist[] = { &cmd, &cmd.realhost, &cmd.realip, &cmd.webirc_hostname, &cmd.webirc_ip, &waiting };
237 ServerInstance->Modules->AddServices(providerlist, sizeof(providerlist)/sizeof(ServiceProvider*));
239 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCheckReady };
240 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
243 void OnRehash(User* user)
247 // Do we send an oper notice when a CGI:IRC has their host changed?
248 cmd.notify = ServerInstance->Config->ConfValue("cgiirc")->getBool("opernotice", true);
250 ConfigTagList tags = ServerInstance->Config->ConfTags("cgihost");
251 for (ConfigIter i = tags.first; i != tags.second; ++i)
253 ConfigTag* tag = i->second;
254 std::string hostmask = tag->getString("mask"); // An allowed CGI:IRC host
255 std::string type = tag->getString("type"); // What type of user-munging we do on this host.
256 std::string password = tag->getString("password");
258 if(hostmask.length())
260 if (type == "webirc" && password.empty())
262 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
269 else if (type == "ident")
271 else if (type == "passfirst")
273 else if (type == "webirc")
278 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:type> value in config: %s, setting it to \"pass\"", type.c_str());
281 cmd.Hosts.push_back(CGIhost(hostmask, cgitype, password));
286 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
292 ModResult OnCheckReady(LocalUser *user)
294 if (waiting.get(user))
297 std::string *webirc_ip = cmd.webirc_ip.get(user);
299 return MOD_RES_PASSTHRU;
301 ChangeIP(user, *webirc_ip);
303 std::string* webirc_hostname = cmd.webirc_hostname.get(user);
304 user->host = user->dhost = (webirc_hostname ? *webirc_hostname : user->GetIPString());
305 user->InvalidateCache();
311 user->CheckLines(true);
315 cmd.webirc_hostname.unset(user);
316 cmd.webirc_ip.unset(user);
318 return MOD_RES_PASSTHRU;
321 ModResult OnUserRegister(LocalUser* user)
323 for(CGIHostlist::iterator iter = cmd.Hosts.begin(); iter != cmd.Hosts.end(); iter++)
325 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
328 if(iter->type == PASS)
330 CheckPass(user); // We do nothing if it fails so...
331 user->CheckLines(true);
333 else if(iter->type == PASSFIRST && !CheckPass(user))
335 // If the password lookup failed, try the ident
336 CheckIdent(user); // If this fails too, do nothing
337 user->CheckLines(true);
339 else if(iter->type == IDENT)
341 CheckIdent(user); // Nothing on failure.
342 user->CheckLines(true);
344 else if(iter->type == IDENTFIRST && !CheckIdent(user))
346 // If the ident lookup fails, try the password.
348 user->CheckLines(true);
350 else if(iter->type == WEBIRC)
352 // We don't need to do anything here
354 return MOD_RES_PASSTHRU;
357 return MOD_RES_PASSTHRU;
360 bool CheckPass(LocalUser* user)
362 if(IsValidHost(user->password))
364 HandleIdentOrPass(user, user->password, true);
365 user->password.clear();
372 bool CheckIdent(LocalUser* user)
377 if (user->ident.length() == 8)
378 ident = user->ident.c_str();
379 else if (user->ident.length() == 9 && user->ident[0] == '~')
380 ident = user->ident.c_str() + 1;
385 unsigned long ipaddr = strtoul(ident, NULL, 16);
388 newip.s_addr = htonl(ipaddr);
389 std::string newipstr(inet_ntoa(newip));
391 user->ident = "~cgiirc";
392 HandleIdentOrPass(user, newipstr, false);
397 bool IsValidHost(const std::string &host)
399 if(!host.size() || host.size() > 64)
402 for(unsigned int i = 0; i < host.size(); i++)
404 if( ((host[i] >= '0') && (host[i] <= '9')) ||
405 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
406 ((host[i] >= 'a') && (host[i] <= 'z')) ||
407 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
408 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
418 virtual Version GetVersion()
420 return Version("Change user's hosts connecting from known CGI:IRC hosts",VF_VENDOR);
425 MODULE_INIT(ModuleCgiIRC)