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/>.
28 #include "modules/dns.h"
30 // We need this method up here so that it can be accessed from anywhere
31 static void ChangeIP(User* user, const std::string& newip)
33 ServerInstance->Users->RemoveCloneCounts(user);
34 user->SetClientIP(newip.c_str());
35 ServerInstance->Users->AddClone(user);
38 // Encapsulates information about a WebIRC host.
42 const std::string hostmask;
43 const std::string password;
44 const std::string passhash;
47 WebIRCHost(const std::string& mask, const std::string& pass, const std::string& hash)
54 bool Matches(LocalUser* user, const std::string& pass) const
56 // Did the user send a valid password?
57 if (!ServerInstance->PassCompare(user, password, pass, passhash))
60 // Does the user's hostname match our hostmask?
61 if (InspIRCd::Match(user->host, hostmask, ascii_case_insensitive_map))
64 // Does the user's IP address match our hostmask?
65 return InspIRCd::MatchCIDR(user->GetIPString(), hostmask, ascii_case_insensitive_map);
71 * This is used for the webirc method of CGIIRC auth, and is (really) the best way to do these things.
72 * Syntax: WEBIRC password gateway hostname ip
73 * Where password is a shared key, gateway is the name of the WebIRC gateway and version (e.g. cgiirc), hostname
74 * is the resolved host of the client issuing the command and IP is the real IP of the client.
77 * To tie in with the rest of cgiirc module, and to avoid race conditions, /webirc is only processed locally
78 * and simply sets metadata on the user, which is later decoded on full connect to give something meaningful.
80 class CommandWebIRC : public SplitCommand
83 std::vector<WebIRCHost> hosts;
85 StringExtItem gateway;
86 StringExtItem realhost;
89 CommandWebIRC(Module* Creator)
90 : SplitCommand(Creator, "WEBIRC", 4)
91 , gateway("cgiirc_gateway", ExtensionItem::EXT_USER, Creator)
92 , realhost("cgiirc_realhost", ExtensionItem::EXT_USER, Creator)
93 , realip("cgiirc_realip", ExtensionItem::EXT_USER, Creator)
95 allow_empty_last_param = false;
96 works_before_reg = true;
97 this->syntax = "password gateway hostname ip";
100 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user) CXX11_OVERRIDE
102 if (user->registered == REG_ALL)
105 irc::sockets::sockaddrs ipaddr;
106 if (!irc::sockets::aptosa(parameters[3], 0, ipaddr))
108 user->CommandFloodPenalty += 5000;
109 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s tried to use WEBIRC but gave an invalid IP address.", user->GetFullRealHost().c_str());
113 // If the hostname is malformed then we use the IP address instead.
114 bool host_ok = (parameters[2].length() <= ServerInstance->Config->Limits.MaxHost) && (parameters[2].find_first_not_of("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-") == std::string::npos);
115 const std::string& newhost = (host_ok ? parameters[2] : parameters[3]);
117 for (std::vector<WebIRCHost>::const_iterator iter = hosts.begin(); iter != hosts.end(); ++iter)
119 // If we don't match the host then skip to the next host.
120 if (!iter->Matches(user, parameters[0]))
123 // The user matched a WebIRC block!
124 gateway.set(user, parameters[1]);
125 realhost.set(user, user->host);
126 realip.set(user, user->GetIPString());
129 ServerInstance->SNO->WriteGlobalSno('w', "Connecting user %s is using a WebIRC gateway; changing their IP/host from %s/%s to %s/%s.",
130 user->nick.c_str(), user->GetIPString().c_str(), user->host.c_str(), parameters[3].c_str(), newhost.c_str());
132 // Set the IP address and hostname sent via WEBIRC.
133 ChangeIP(user, parameters[3]);
134 user->host = user->dhost = newhost;
135 user->InvalidateCache();
139 user->CommandFloodPenalty += 5000;
140 ServerInstance->SNO->WriteGlobalSno('w', "Connecting user %s tried to use WEBIRC but didn't match any configured WebIRC hosts.", user->GetFullRealHost().c_str());
146 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
148 class CGIResolver : public DNS::Request
150 std::string theiruid;
151 LocalIntExt& waiting;
154 CGIResolver(DNS::Manager* mgr, Module* me, bool NotifyOpers, const std::string &source, LocalUser* u, LocalIntExt& ext)
155 : DNS::Request(mgr, me, source, DNS::QUERY_PTR)
158 , notify(NotifyOpers)
162 void OnLookupComplete(const DNS::Query *r) CXX11_OVERRIDE
164 /* Check the user still exists */
165 User* them = ServerInstance->FindUUID(theiruid);
166 if ((them) && (!them->quitting))
168 LocalUser* lu = IS_LOCAL(them);
172 const DNS::ResourceRecord &ans_record = r->answers[0];
173 if (ans_record.rdata.empty() || ans_record.rdata.length() > ServerInstance->Config->Limits.MaxHost)
177 ServerInstance->SNO->WriteGlobalSno('w', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s", them->nick.c_str(), them->host.c_str(), ans_record.rdata.c_str());
179 them->host = them->dhost = ans_record.rdata;
180 them->InvalidateCache();
181 lu->CheckLines(true);
185 void OnError(const DNS::Query *r) CXX11_OVERRIDE
190 User* them = ServerInstance->FindUUID(theiruid);
191 if ((them) && (!them->quitting))
193 ServerInstance->SNO->WriteToSnoMask('w', "Connecting user %s detected as using CGI:IRC (%s), but their host can't be resolved!", them->nick.c_str(), them->host.c_str());
199 User* them = ServerInstance->FindUUID(theiruid);
202 int count = waiting.get(them);
204 waiting.set(them, count - 1);
208 class ModuleCgiIRC : public Module
212 dynamic_reference<DNS::Manager> DNS;
213 std::vector<std::string> hosts;
215 static void RecheckClass(LocalUser* user)
217 user->MyClass = NULL;
222 void HandleIdent(LocalUser* user, const std::string& newip)
224 cmd.realhost.set(user, user->host);
225 cmd.realip.set(user, user->GetIPString());
226 ChangeIP(user, newip);
227 user->host = user->dhost = user->GetIPString();
228 user->InvalidateCache();
231 // Don't create the resolver if the core couldn't put the user in a connect class or when dns is disabled
232 if (user->quitting || !DNS || !user->MyClass->resolvehostnames)
235 CGIResolver* r = new CGIResolver(*this->DNS, this, cmd.notify, newip, user, waiting);
238 waiting.set(user, waiting.get(user) + 1);
239 this->DNS->Process(r);
241 catch (DNS::Exception &ex)
243 int count = waiting.get(user);
245 waiting.set(user, count - 1);
248 ServerInstance->SNO->WriteToSnoMask('w', "Connecting user %s detected as using CGI:IRC (%s), but I could not resolve their hostname; %s", user->nick.c_str(), user->host.c_str(), ex.GetReason().c_str());
255 , waiting("cgiirc-delay", ExtensionItem::EXT_USER, this)
260 void init() CXX11_OVERRIDE
262 ServerInstance->SNO->EnableSnomask('w', "CGIIRC");
265 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
267 std::vector<std::string> identhosts;
268 std::vector<WebIRCHost> webirchosts;
270 ConfigTagList tags = ServerInstance->Config->ConfTags("cgihost");
271 for (ConfigIter i = tags.first; i != tags.second; ++i)
273 ConfigTag* tag = i->second;
275 // Ensure that we have the <cgihost:mask> parameter.
276 const std::string mask = tag->getString("mask");
278 throw ModuleException("<cgihost:mask> is a mandatory field, at " + tag->getTagLocation());
280 // Determine what lookup type this host uses.
281 const std::string type = tag->getString("type");
282 if (stdalgo::string::equalsci(type, "ident"))
284 // The IP address should be looked up from the hex IP address.
285 identhosts.push_back(mask);
287 else if (stdalgo::string::equalsci(type, "webirc"))
289 // The IP address will be received via the WEBIRC command.
290 const std::string password = tag->getString("password");
292 // WebIRC blocks require a password.
293 if (password.empty())
294 throw ModuleException("When using <cgihost type=\"webirc\"> the password field is required, at " + tag->getTagLocation());
296 webirchosts.push_back(WebIRCHost(mask, password, tag->getString("hash")));
300 throw ModuleException(type + " is an invalid <cgihost:mask> type, at " + tag->getTagLocation());
304 // The host configuration was valid so we can apply it.
305 hosts.swap(identhosts);
306 cmd.hosts.swap(webirchosts);
308 // Do we send an oper notice when a m_cgiirc client has their IP/host changed?
309 cmd.notify = ServerInstance->Config->ConfValue("cgiirc")->getBool("opernotice", true);
312 ModResult OnCheckReady(LocalUser *user) CXX11_OVERRIDE
314 if (waiting.get(user))
317 if (!cmd.realip.get(user))
318 return MOD_RES_PASSTHRU;
324 user->CheckLines(true);
328 return MOD_RES_PASSTHRU;
331 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
333 // If <connect:webirc> is not set then we have nothing to do.
334 const std::string webirc = myclass->config->getString("webirc");
336 return MOD_RES_PASSTHRU;
338 // If the user is not connecting via a WebIRC gateway then they
339 // cannot match this connect class.
340 const std::string* gateway = cmd.gateway.get(user);
344 // If the gateway matches the <connect:webirc> constraint then
345 // allow the check to continue. Otherwise, reject it.
346 return InspIRCd::Match(*gateway, webirc) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
349 ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
351 for (std::vector<std::string>::const_iterator iter = hosts.begin(); iter != hosts.end(); ++iter)
353 if (!InspIRCd::Match(user->host, *iter, ascii_case_insensitive_map) && !InspIRCd::MatchCIDR(user->GetIPString(), *iter, ascii_case_insensitive_map))
356 CheckIdent(user); // Nothing on failure.
357 user->CheckLines(true);
360 return MOD_RES_PASSTHRU;
363 bool CheckIdent(LocalUser* user)
368 if (user->ident.length() == 8)
369 ident = user->ident.c_str();
370 else if (user->ident.length() == 9 && user->ident[0] == '~')
371 ident = user->ident.c_str() + 1;
376 unsigned long ipaddr = strtoul(ident, NULL, 16);
379 newip.s_addr = htonl(ipaddr);
380 std::string newipstr(inet_ntoa(newip));
382 user->ident = "~cgiirc";
383 HandleIdent(user, newipstr);
388 Version GetVersion() CXX11_OVERRIDE
390 return Version("Change user's hosts connecting from known CGI:IRC hosts",VF_VENDOR);
394 MODULE_INIT(ModuleCgiIRC)