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/dns.h"
28 #include "modules/ssl.h"
32 RPL_WHOISGATEWAY = 350
35 // We need this method up here so that it can be accessed from anywhere
36 static void ChangeIP(User* user, const std::string& newip)
38 ServerInstance->Users->RemoveCloneCounts(user);
39 user->SetClientIP(newip);
40 ServerInstance->Users->AddClone(user);
43 // Encapsulates information about a WebIRC host.
48 std::string fingerprint;
53 WebIRCHost(const std::string& mask, const std::string& fp, const std::string& pass, const std::string& hash)
61 bool Matches(LocalUser* user, const std::string& pass) const
63 // Did the user send a valid password?
64 if (!password.empty() && !ServerInstance->PassCompare(user, password, pass, passhash))
67 // Does the user have a valid fingerprint?
68 const std::string fp = SSLClientCert::GetFingerprint(&user->eh);
69 if (!fingerprint.empty() && fp != fingerprint)
72 // Does the user's hostname match our hostmask?
73 if (InspIRCd::Match(user->GetRealHost(), hostmask, ascii_case_insensitive_map))
76 // Does the user's IP address match our hostmask?
77 return InspIRCd::MatchCIDR(user->GetIPString(), hostmask, ascii_case_insensitive_map);
83 * This is used for the webirc method of CGIIRC auth, and is (really) the best way to do these things.
84 * Syntax: WEBIRC password gateway hostname ip
85 * Where password is a shared key, gateway is the name of the WebIRC gateway and version (e.g. cgiirc), hostname
86 * is the resolved host of the client issuing the command and IP is the real IP of the client.
89 * To tie in with the rest of cgiirc module, and to avoid race conditions, /webirc is only processed locally
90 * and simply sets metadata on the user, which is later decoded on full connect to give something meaningful.
92 class CommandWebIRC : public SplitCommand
95 std::vector<WebIRCHost> hosts;
97 StringExtItem gateway;
98 StringExtItem realhost;
101 CommandWebIRC(Module* Creator)
102 : SplitCommand(Creator, "WEBIRC", 4)
103 , gateway("cgiirc_gateway", ExtensionItem::EXT_USER, Creator)
104 , realhost("cgiirc_realhost", ExtensionItem::EXT_USER, Creator)
105 , realip("cgiirc_realip", ExtensionItem::EXT_USER, Creator)
107 allow_empty_last_param = false;
108 works_before_reg = true;
109 this->syntax = "password gateway hostname ip";
112 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user) CXX11_OVERRIDE
114 if (user->registered == REG_ALL)
117 irc::sockets::sockaddrs ipaddr;
118 if (!irc::sockets::aptosa(parameters[3], 0, ipaddr))
120 user->CommandFloodPenalty += 5000;
121 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s tried to use WEBIRC but gave an invalid IP address.", user->GetFullRealHost().c_str());
125 for (std::vector<WebIRCHost>::const_iterator iter = hosts.begin(); iter != hosts.end(); ++iter)
127 // If we don't match the host then skip to the next host.
128 if (!iter->Matches(user, parameters[0]))
131 // The user matched a WebIRC block!
132 gateway.set(user, parameters[1]);
133 realhost.set(user, user->GetRealHost());
134 realip.set(user, user->GetIPString());
137 ServerInstance->SNO->WriteGlobalSno('w', "Connecting user %s is using a WebIRC gateway; changing their IP from %s to %s.",
138 user->nick.c_str(), user->GetIPString().c_str(), parameters[3].c_str());
140 // Set the IP address sent via WEBIRC. We ignore the hostname and lookup
141 // instead do our own DNS lookups because of unreliable gateways.
142 ChangeIP(user, parameters[3]);
146 user->CommandFloodPenalty += 5000;
147 ServerInstance->SNO->WriteGlobalSno('w', "Connecting user %s tried to use WEBIRC but didn't match any configured WebIRC hosts.", user->GetFullRealHost().c_str());
152 class ModuleCgiIRC : public Module, public Whois::EventListener
155 std::vector<std::string> hosts;
157 static void RecheckClass(LocalUser* user)
159 user->MyClass = NULL;
164 void HandleIdent(LocalUser* user, const std::string& newip)
166 cmd.realhost.set(user, user->GetRealHost());
167 cmd.realip.set(user, user->GetIPString());
170 ServerInstance->SNO->WriteGlobalSno('w', "Connecting user %s is using an ident gateway; changing their IP from %s to %s.",
171 user->nick.c_str(), user->GetIPString().c_str(), newip.c_str());
173 ChangeIP(user, newip);
179 : Whois::EventListener(this)
184 void init() CXX11_OVERRIDE
186 ServerInstance->SNO->EnableSnomask('w', "CGIIRC");
189 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
191 std::vector<std::string> identhosts;
192 std::vector<WebIRCHost> webirchosts;
194 ConfigTagList tags = ServerInstance->Config->ConfTags("cgihost");
195 for (ConfigIter i = tags.first; i != tags.second; ++i)
197 ConfigTag* tag = i->second;
199 // Ensure that we have the <cgihost:mask> parameter.
200 const std::string mask = tag->getString("mask");
202 throw ModuleException("<cgihost:mask> is a mandatory field, at " + tag->getTagLocation());
204 // Determine what lookup type this host uses.
205 const std::string type = tag->getString("type");
206 if (stdalgo::string::equalsci(type, "ident"))
208 // The IP address should be looked up from the hex IP address.
209 identhosts.push_back(mask);
211 else if (stdalgo::string::equalsci(type, "webirc"))
213 // The IP address will be received via the WEBIRC command.
214 const std::string fingerprint = tag->getString("fingerprint");
215 const std::string password = tag->getString("password");
217 // WebIRC blocks require a password.
218 if (fingerprint.empty() && password.empty())
219 throw ModuleException("When using <cgihost type=\"webirc\"> either the fingerprint or password field is required, at " + tag->getTagLocation());
221 webirchosts.push_back(WebIRCHost(mask, fingerprint, password, tag->getString("hash")));
225 throw ModuleException(type + " is an invalid <cgihost:mask> type, at " + tag->getTagLocation());
229 // The host configuration was valid so we can apply it.
230 hosts.swap(identhosts);
231 cmd.hosts.swap(webirchosts);
233 // Do we send an oper notice when a m_cgiirc client has their IP changed?
234 cmd.notify = ServerInstance->Config->ConfValue("cgiirc")->getBool("opernotice", true);
237 ModResult OnCheckReady(LocalUser *user) CXX11_OVERRIDE
239 if (!cmd.realip.get(user))
240 return MOD_RES_PASSTHRU;
246 user->CheckLines(true);
250 return MOD_RES_PASSTHRU;
253 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
255 // If <connect:webirc> is not set then we have nothing to do.
256 const std::string webirc = myclass->config->getString("webirc");
258 return MOD_RES_PASSTHRU;
260 // If the user is not connecting via a WebIRC gateway then they
261 // cannot match this connect class.
262 const std::string* gateway = cmd.gateway.get(user);
266 // If the gateway matches the <connect:webirc> constraint then
267 // allow the check to continue. Otherwise, reject it.
268 return InspIRCd::Match(*gateway, webirc) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
271 ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
273 for (std::vector<std::string>::const_iterator iter = hosts.begin(); iter != hosts.end(); ++iter)
275 if (!InspIRCd::Match(user->GetRealHost(), *iter, ascii_case_insensitive_map) && !InspIRCd::MatchCIDR(user->GetIPString(), *iter, ascii_case_insensitive_map))
278 CheckIdent(user); // Nothing on failure.
279 user->CheckLines(true);
282 return MOD_RES_PASSTHRU;
285 void OnWhois(Whois::Context& whois) CXX11_OVERRIDE
287 if (!whois.IsSelfWhois() && !whois.GetSource()->HasPrivPermission("users/auspex"))
290 // If these fields are not set then the client is not using a gateway.
291 const std::string* realhost = cmd.realhost.get(whois.GetTarget());
292 const std::string* realip = cmd.realip.get(whois.GetTarget());
293 if (!realhost || !realip)
296 const std::string* gateway = cmd.gateway.get(whois.GetTarget());
298 whois.SendLine(RPL_WHOISGATEWAY, *realhost, *realip, "is connected via the " + *gateway + " WebIRC gateway");
300 whois.SendLine(RPL_WHOISGATEWAY, *realhost, *realip, "is connected via an ident gateway");
303 bool CheckIdent(LocalUser* user)
308 if (user->ident.length() == 8)
309 ident = user->ident.c_str();
310 else if (user->ident.length() == 9 && user->ident[0] == '~')
311 ident = user->ident.c_str() + 1;
316 unsigned long ipaddr = strtoul(ident, NULL, 16);
319 newip.s_addr = htonl(ipaddr);
320 std::string newipstr(inet_ntoa(newip));
322 user->ident = "~cgiirc";
323 HandleIdent(user, newipstr);
328 Version GetVersion() CXX11_OVERRIDE
330 return Version("Enables forwarding the real IP address of a user from a gateway to the IRC server", VF_VENDOR);
334 MODULE_INIT(ModuleCgiIRC)