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/>.
27 #include "modules/ssl.h"
28 #include "modules/whois.h"
33 RPL_WHOISGATEWAY = 350
36 // We need this method up here so that it can be accessed from anywhere
37 static void ChangeIP(User* user, const std::string& newip)
39 ServerInstance->Users->RemoveCloneCounts(user);
40 user->SetClientIP(newip);
41 ServerInstance->Users->AddClone(user);
44 // Encapsulates information about a WebIRC host.
49 std::string fingerprint;
54 WebIRCHost(const std::string& mask, const std::string& fp, const std::string& pass, const std::string& hash)
62 bool Matches(LocalUser* user, const std::string& pass) const
64 // Did the user send a valid password?
65 if (!password.empty() && !ServerInstance->PassCompare(user, password, pass, passhash))
68 // Does the user have a valid fingerprint?
69 const std::string fp = SSLClientCert::GetFingerprint(&user->eh);
70 if (!fingerprint.empty() && fp != fingerprint)
73 // Does the user's hostname match our hostmask?
74 if (InspIRCd::Match(user->GetRealHost(), hostmask, ascii_case_insensitive_map))
77 // Does the user's IP address match our hostmask?
78 return InspIRCd::MatchCIDR(user->GetIPString(), hostmask, ascii_case_insensitive_map);
84 * This is used for the webirc method of CGIIRC auth, and is (really) the best way to do these things.
85 * Syntax: WEBIRC password gateway hostname ip
86 * Where password is a shared key, gateway is the name of the WebIRC gateway and version (e.g. cgiirc), hostname
87 * is the resolved host of the client issuing the command and IP is the real IP of the client.
90 * To tie in with the rest of cgiirc module, and to avoid race conditions, /webirc is only processed locally
91 * and simply sets metadata on the user, which is later decoded on full connect to give something meaningful.
93 class CommandWebIRC : public SplitCommand
96 std::vector<WebIRCHost> hosts;
98 StringExtItem gateway;
99 StringExtItem realhost;
100 StringExtItem realip;
102 CommandWebIRC(Module* Creator)
103 : SplitCommand(Creator, "WEBIRC", 4)
104 , gateway("cgiirc_gateway", ExtensionItem::EXT_USER, Creator)
105 , realhost("cgiirc_realhost", ExtensionItem::EXT_USER, Creator)
106 , realip("cgiirc_realip", ExtensionItem::EXT_USER, Creator)
108 allow_empty_last_param = false;
109 works_before_reg = true;
110 this->syntax = "password gateway hostname ip";
113 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user) CXX11_OVERRIDE
115 if (user->registered == REG_ALL)
118 irc::sockets::sockaddrs ipaddr;
119 if (!irc::sockets::aptosa(parameters[3], 0, ipaddr))
121 user->CommandFloodPenalty += 5000;
122 WriteLog("Connecting user %s (%s) tried to use WEBIRC but gave an invalid IP address.",
123 user->uuid.c_str(), user->GetIPString().c_str());
127 for (std::vector<WebIRCHost>::const_iterator iter = hosts.begin(); iter != hosts.end(); ++iter)
129 // If we don't match the host then skip to the next host.
130 if (!iter->Matches(user, parameters[0]))
133 // The user matched a WebIRC block!
134 gateway.set(user, parameters[1]);
135 realhost.set(user, user->GetRealHost());
136 realip.set(user, user->GetIPString());
138 WriteLog("Connecting user %s is using a WebIRC gateway; changing their IP from %s to %s.",
139 user->uuid.c_str(), user->GetIPString().c_str(), parameters[3].c_str());
141 // Set the IP address sent via WEBIRC. We ignore the hostname and lookup
142 // instead do our own DNS lookups because of unreliable gateways.
143 ChangeIP(user, parameters[3]);
147 user->CommandFloodPenalty += 5000;
148 WriteLog("Connecting user %s (%s) tried to use WEBIRC but didn't match any configured WebIRC hosts.",
149 user->uuid.c_str(), user->GetIPString().c_str());
153 void WriteLog(const char* message, ...) CUSTOM_PRINTF(2, 3)
156 VAFORMAT(buffer, message, message);
158 // If we are sending a snotice then the message will already be
159 // written to the logfile.
161 ServerInstance->SNO->WriteGlobalSno('w', buffer);
163 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, buffer);
167 class ModuleCgiIRC : public Module, public Whois::EventListener
170 std::vector<std::string> hosts;
172 static void RecheckClass(LocalUser* user)
174 user->MyClass = NULL;
179 void HandleIdent(LocalUser* user, const std::string& newip)
181 cmd.realhost.set(user, user->GetRealHost());
182 cmd.realip.set(user, user->GetIPString());
184 cmd.WriteLog("Connecting user %s is using an ident gateway; changing their IP from %s to %s.",
185 user->uuid.c_str(), user->GetIPString().c_str(), newip.c_str());
186 ChangeIP(user, newip);
192 : Whois::EventListener(this)
197 void init() CXX11_OVERRIDE
199 ServerInstance->SNO->EnableSnomask('w', "CGIIRC");
202 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
204 std::vector<std::string> identhosts;
205 std::vector<WebIRCHost> webirchosts;
207 ConfigTagList tags = ServerInstance->Config->ConfTags("cgihost");
208 for (ConfigIter i = tags.first; i != tags.second; ++i)
210 ConfigTag* tag = i->second;
212 // Ensure that we have the <cgihost:mask> parameter.
213 const std::string mask = tag->getString("mask");
215 throw ModuleException("<cgihost:mask> is a mandatory field, at " + tag->getTagLocation());
217 // Determine what lookup type this host uses.
218 const std::string type = tag->getString("type");
219 if (stdalgo::string::equalsci(type, "ident"))
221 // The IP address should be looked up from the hex IP address.
222 identhosts.push_back(mask);
224 else if (stdalgo::string::equalsci(type, "webirc"))
226 // The IP address will be received via the WEBIRC command.
227 const std::string fingerprint = tag->getString("fingerprint");
228 const std::string password = tag->getString("password");
230 // WebIRC blocks require a password.
231 if (fingerprint.empty() && password.empty())
232 throw ModuleException("When using <cgihost type=\"webirc\"> either the fingerprint or password field is required, at " + tag->getTagLocation());
234 webirchosts.push_back(WebIRCHost(mask, fingerprint, password, tag->getString("hash")));
238 throw ModuleException(type + " is an invalid <cgihost:mask> type, at " + tag->getTagLocation());
242 // The host configuration was valid so we can apply it.
243 hosts.swap(identhosts);
244 cmd.hosts.swap(webirchosts);
246 // Do we send an oper notice when a m_cgiirc client has their IP changed?
247 cmd.notify = ServerInstance->Config->ConfValue("cgiirc")->getBool("opernotice", true);
250 ModResult OnCheckReady(LocalUser *user) CXX11_OVERRIDE
252 if (!cmd.realip.get(user))
253 return MOD_RES_PASSTHRU;
259 user->CheckLines(true);
263 return MOD_RES_PASSTHRU;
266 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
268 // If <connect:webirc> is not set then we have nothing to do.
269 const std::string webirc = myclass->config->getString("webirc");
271 return MOD_RES_PASSTHRU;
273 // If the user is not connecting via a WebIRC gateway then they
274 // cannot match this connect class.
275 const std::string* gateway = cmd.gateway.get(user);
279 // If the gateway matches the <connect:webirc> constraint then
280 // allow the check to continue. Otherwise, reject it.
281 return InspIRCd::Match(*gateway, webirc) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
284 ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
286 for (std::vector<std::string>::const_iterator iter = hosts.begin(); iter != hosts.end(); ++iter)
288 if (!InspIRCd::Match(user->GetRealHost(), *iter, ascii_case_insensitive_map) && !InspIRCd::MatchCIDR(user->GetIPString(), *iter, ascii_case_insensitive_map))
291 CheckIdent(user); // Nothing on failure.
292 user->CheckLines(true);
295 return MOD_RES_PASSTHRU;
298 void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
300 if (!whois.IsSelfWhois() && !whois.GetSource()->HasPrivPermission("users/auspex"))
303 // If these fields are not set then the client is not using a gateway.
304 const std::string* realhost = cmd.realhost.get(whois.GetTarget());
305 const std::string* realip = cmd.realip.get(whois.GetTarget());
306 if (!realhost || !realip)
309 const std::string* gateway = cmd.gateway.get(whois.GetTarget());
311 whois.SendLine(RPL_WHOISGATEWAY, *realhost, *realip, "is connected via the " + *gateway + " WebIRC gateway");
313 whois.SendLine(RPL_WHOISGATEWAY, *realhost, *realip, "is connected via an ident gateway");
316 bool CheckIdent(LocalUser* user)
321 if (user->ident.length() == 8)
322 ident = user->ident.c_str();
323 else if (user->ident.length() == 9 && user->ident[0] == '~')
324 ident = user->ident.c_str() + 1;
329 unsigned long ipaddr = strtoul(ident, NULL, 16);
332 newip.s_addr = htonl(ipaddr);
333 std::string newipstr(inet_ntoa(newip));
335 user->ident = "~cgiirc";
336 HandleIdent(user, newipstr);
341 Version GetVersion() CXX11_OVERRIDE
343 return Version("Enables forwarding the real IP address of a user from a gateway to the IRC server", VF_VENDOR);
347 MODULE_INIT(ModuleCgiIRC)