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 enum CGItype { IDENT, WEBIRC };
32 // We need this method up here so that it can be accessed from anywhere
33 static void ChangeIP(User* user, const std::string& newip)
35 ServerInstance->Users->RemoveCloneCounts(user);
36 user->SetClientIP(newip.c_str());
37 ServerInstance->Users->AddClone(user);
40 /** Holds a CGI site's details
49 CGIhost(const std::string &mask, CGItype t, const std::string &spassword)
50 : hostmask(mask), type(t), password(spassword)
54 typedef std::vector<CGIhost> CGIHostlist;
58 * This is used for the webirc method of CGIIRC auth, and is (really) the best way to do these things.
59 * Syntax: WEBIRC password gateway hostname ip
60 * Where password is a shared key, gateway is the name of the WebIRC gateway and version (e.g. cgiirc), hostname
61 * is the resolved host of the client issuing the command and IP is the real IP of the client.
64 * To tie in with the rest of cgiirc module, and to avoid race conditions, /webirc is only processed locally
65 * and simply sets metadata on the user, which is later decoded on full connect to give something meaningful.
67 class CommandWebIRC : public SplitCommand
71 StringExtItem gateway;
72 StringExtItem realhost;
76 CommandWebIRC(Module* Creator)
77 : SplitCommand(Creator, "WEBIRC", 4)
78 , gateway("cgiirc_gateway", ExtensionItem::EXT_USER, Creator)
79 , realhost("cgiirc_realhost", ExtensionItem::EXT_USER, Creator)
80 , realip("cgiirc_realip", ExtensionItem::EXT_USER, Creator)
82 allow_empty_last_param = false;
83 works_before_reg = true;
84 this->syntax = "password gateway hostname ip";
86 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user)
88 if(user->registered == REG_ALL)
91 irc::sockets::sockaddrs ipaddr;
92 if (!irc::sockets::aptosa(parameters[3], 0, ipaddr))
94 user->CommandFloodPenalty += 5000;
95 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s tried to use WEBIRC but gave an invalid IP address.", user->GetFullRealHost().c_str());
99 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
101 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
103 if(iter->type == WEBIRC && parameters[0] == iter->password)
105 gateway.set(user, parameters[1]);
106 realhost.set(user, user->host);
107 realip.set(user, user->GetIPString());
109 // 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
110 bool host_ok = (parameters[2].length() <= ServerInstance->Config->Limits.MaxHost);
111 const std::string& newhost = (host_ok ? parameters[2] : parameters[3]);
114 ServerInstance->SNO->WriteGlobalSno('w', "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());
116 // Where the magic happens - change their IP
117 ChangeIP(user, parameters[3]);
118 // And follow this up by changing their host
119 user->host = user->dhost = newhost;
120 user->InvalidateCache();
127 user->CommandFloodPenalty += 5000;
128 ServerInstance->SNO->WriteGlobalSno('w', "Connecting user %s tried to use WEBIRC, but didn't match any configured webirc blocks.", user->GetFullRealHost().c_str());
134 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
136 class CGIResolver : public DNS::Request
138 std::string theiruid;
139 LocalIntExt& waiting;
142 CGIResolver(DNS::Manager* mgr, Module* me, bool NotifyOpers, const std::string &source, LocalUser* u, LocalIntExt& ext)
143 : DNS::Request(mgr, me, source, DNS::QUERY_PTR)
146 , notify(NotifyOpers)
150 void OnLookupComplete(const DNS::Query *r) CXX11_OVERRIDE
152 /* Check the user still exists */
153 User* them = ServerInstance->FindUUID(theiruid);
154 if ((them) && (!them->quitting))
156 LocalUser* lu = IS_LOCAL(them);
160 const DNS::ResourceRecord &ans_record = r->answers[0];
161 if (ans_record.rdata.empty() || ans_record.rdata.length() > ServerInstance->Config->Limits.MaxHost)
165 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());
167 them->host = them->dhost = ans_record.rdata;
168 them->InvalidateCache();
169 lu->CheckLines(true);
173 void OnError(const DNS::Query *r) CXX11_OVERRIDE
178 User* them = ServerInstance->FindUUID(theiruid);
179 if ((them) && (!them->quitting))
181 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());
187 User* them = ServerInstance->FindUUID(theiruid);
190 int count = waiting.get(them);
192 waiting.set(them, count - 1);
196 class ModuleCgiIRC : public Module
200 dynamic_reference<DNS::Manager> DNS;
202 static void RecheckClass(LocalUser* user)
204 user->MyClass = NULL;
209 void HandleIdent(LocalUser* user, const std::string& newip)
211 cmd.realhost.set(user, user->host);
212 cmd.realip.set(user, user->GetIPString());
213 ChangeIP(user, newip);
214 user->host = user->dhost = user->GetIPString();
215 user->InvalidateCache();
218 // Don't create the resolver if the core couldn't put the user in a connect class or when dns is disabled
219 if (user->quitting || !DNS || !user->MyClass->resolvehostnames)
222 CGIResolver* r = new CGIResolver(*this->DNS, this, cmd.notify, newip, user, waiting);
225 waiting.set(user, waiting.get(user) + 1);
226 this->DNS->Process(r);
228 catch (DNS::Exception &ex)
230 int count = waiting.get(user);
232 waiting.set(user, count - 1);
235 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());
242 , waiting("cgiirc-delay", ExtensionItem::EXT_USER, this)
247 void init() CXX11_OVERRIDE
249 ServerInstance->SNO->EnableSnomask('w', "CGIIRC");
252 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
256 // Do we send an oper notice when a CGI:IRC has their host changed?
257 cmd.notify = ServerInstance->Config->ConfValue("cgiirc")->getBool("opernotice", true);
259 ConfigTagList tags = ServerInstance->Config->ConfTags("cgihost");
260 for (ConfigIter i = tags.first; i != tags.second; ++i)
262 ConfigTag* tag = i->second;
263 std::string hostmask = tag->getString("mask"); // An allowed CGI:IRC host
264 std::string type = tag->getString("type"); // What type of user-munging we do on this host.
265 std::string password = tag->getString("password");
267 if(hostmask.length())
269 if (type == "webirc" && password.empty())
271 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Missing password in config: %s", hostmask.c_str());
278 else if (type == "webirc")
283 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Invalid <cgihost:type> value in config: %s, setting it to \"ident\"", type.c_str());
286 cmd.Hosts.push_back(CGIhost(hostmask, cgitype, password));
291 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
297 ModResult OnCheckReady(LocalUser *user) CXX11_OVERRIDE
299 if (waiting.get(user))
302 if (!cmd.realip.get(user))
303 return MOD_RES_PASSTHRU;
309 user->CheckLines(true);
313 return MOD_RES_PASSTHRU;
316 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) CXX11_OVERRIDE
318 // If <connect:webirc> is not set then we have nothing to do.
319 const std::string webirc = myclass->config->getString("webirc");
321 return MOD_RES_PASSTHRU;
323 // If the user is not connecting via a WebIRC gateway then they
324 // cannot match this connect class.
325 const std::string* gateway = cmd.gateway.get(user);
329 // If the gateway matches the <connect:webirc> constraint then
330 // allow the check to continue. Otherwise, reject it.
331 return InspIRCd::Match(*gateway, webirc) ? MOD_RES_PASSTHRU : MOD_RES_DENY;
334 ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
336 for(CGIHostlist::iterator iter = cmd.Hosts.begin(); iter != cmd.Hosts.end(); iter++)
338 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
341 if(iter->type == IDENT)
343 CheckIdent(user); // Nothing on failure.
344 user->CheckLines(true);
346 else if(iter->type == WEBIRC)
348 // We don't need to do anything here
350 return MOD_RES_PASSTHRU;
353 return MOD_RES_PASSTHRU;
356 bool CheckIdent(LocalUser* user)
361 if (user->ident.length() == 8)
362 ident = user->ident.c_str();
363 else if (user->ident.length() == 9 && user->ident[0] == '~')
364 ident = user->ident.c_str() + 1;
369 unsigned long ipaddr = strtoul(ident, NULL, 16);
372 newip.s_addr = htonl(ipaddr);
373 std::string newipstr(inet_ntoa(newip));
375 user->ident = "~cgiirc";
376 HandleIdent(user, newipstr);
381 Version GetVersion() CXX11_OVERRIDE
383 return Version("Change user's hosts connecting from known CGI:IRC hosts",VF_VENDOR);
387 MODULE_INIT(ModuleCgiIRC)