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 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
32 enum CGItype { PASS, IDENT, PASSFIRST, IDENTFIRST, WEBIRC };
35 /** Holds a CGI site's details
44 CGIhost(const std::string &mask, CGItype t, const std::string &spassword)
45 : hostmask(mask), type(t), password(spassword)
49 typedef std::vector<CGIhost> CGIHostlist;
53 * This is used for the webirc method of CGIIRC auth, and is (really) the best way to do these things.
54 * Syntax: WEBIRC password client hostname ip
55 * Where password is a shared key, client is the name of the "client" and version (e.g. cgiirc), hostname
56 * is the resolved host of the client issuing the command and IP is the real IP of the client.
59 * To tie in with the rest of cgiirc module, and to avoid race conditions, /webirc is only processed locally
60 * and simply sets metadata on the user, which is later decoded on full connect to give something meaningful.
62 class CommandWebirc : public Command
66 StringExtItem realhost;
68 LocalStringExt webirc_hostname;
69 LocalStringExt webirc_ip;
72 CommandWebirc(Module* Creator)
73 : Command(Creator, "WEBIRC", 4),
74 realhost("cgiirc_realhost", Creator), realip("cgiirc_realip", Creator),
75 webirc_hostname("cgiirc_webirc_hostname", Creator), webirc_ip("cgiirc_webirc_ip", Creator)
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 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
87 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
89 if(iter->type == WEBIRC && parameters[0] == iter->password)
91 realhost.set(user, user->host);
92 realip.set(user, user->GetIPString());
94 bool host_ok = (parameters[2].length() < 64);
95 const std::string& newhost = (host_ok ? parameters[2] : parameters[3]);
98 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());
100 // 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
102 webirc_hostname.set(user, parameters[2]);
104 webirc_hostname.unset(user);
106 webirc_ip.set(user, parameters[3]);
112 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s tried to use WEBIRC, but didn't match any configured webirc blocks.", user->GetFullRealHost().c_str());
118 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
120 class CGIResolver : public DNS::Request
123 std::string theiruid;
124 LocalIntExt& waiting;
127 CGIResolver(DNS::Manager *mgr, Module* me, bool NotifyOpers, const std::string &source, LocalUser* u,
128 const std::string &ttype, LocalIntExt& ext)
129 : DNS::Request(mgr, me, source, DNS::QUERY_PTR), typ(ttype), theiruid(u->uuid),
130 waiting(ext), notify(NotifyOpers)
134 void OnLookupComplete(const DNS::Query *r)
136 /* Check the user still exists */
137 User* them = ServerInstance->FindUUID(theiruid);
138 if ((them) && (!them->quitting))
140 LocalUser* lu = IS_LOCAL(them);
144 const DNS::ResourceRecord &ans_record = r->answers[0];
145 if (ans_record.rdata.empty() || ans_record.rdata.length() > 64)
149 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(), ans_record.rdata.c_str(), typ.c_str());
151 them->host = them->dhost = ans_record.rdata;
152 them->InvalidateCache();
153 lu->CheckLines(true);
157 void OnError(const DNS::Query *r)
162 User* them = ServerInstance->FindUUID(theiruid);
163 if ((them) && (!them->quitting))
165 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());
169 virtual ~CGIResolver()
171 User* them = ServerInstance->FindUUID(theiruid);
174 int count = waiting.get(them);
176 waiting.set(them, count - 1);
180 class ModuleCgiIRC : public Module
184 dynamic_reference<DNS::Manager> DNS;
186 static void RecheckClass(LocalUser* user)
188 user->MyClass = NULL;
193 static void ChangeIP(LocalUser* user, const std::string& newip)
195 ServerInstance->Users->RemoveCloneCounts(user);
196 user->SetClientIP(newip.c_str());
197 ServerInstance->Users->AddLocalClone(user);
198 ServerInstance->Users->AddGlobalClone(user);
201 void HandleIdentOrPass(LocalUser* user, const std::string& newip, bool was_pass)
203 cmd.realhost.set(user, user->host);
204 cmd.realip.set(user, user->GetIPString());
205 ChangeIP(user, newip);
206 user->host = user->dhost = user->GetIPString();
207 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 || !DNS || user->MyClass->nouserdns)
214 CGIResolver* r = new CGIResolver(*this->DNS, this, cmd.notify, newip, user, (was_pass ? "PASS" : "IDENT"), waiting);
217 waiting.set(user, waiting.get(user) + 1);
218 this->DNS->Process(r);
220 catch (DNS::Exception &ex)
222 int count = waiting.get(user);
224 waiting.set(user, count - 1);
227 ServerInstance->SNO->WriteToSnoMask('a', "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());
234 , waiting("cgiirc-delay", this)
242 ServiceProvider* providerlist[] = { &cmd, &cmd.realhost, &cmd.realip, &cmd.webirc_hostname, &cmd.webirc_ip, &waiting };
243 ServerInstance->Modules->AddServices(providerlist, sizeof(providerlist)/sizeof(ServiceProvider*));
245 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCheckReady };
246 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
249 void OnRehash(User* user)
253 // Do we send an oper notice when a CGI:IRC has their host changed?
254 cmd.notify = ServerInstance->Config->ConfValue("cgiirc")->getBool("opernotice", true);
256 ConfigTagList tags = ServerInstance->Config->ConfTags("cgihost");
257 for (ConfigIter i = tags.first; i != tags.second; ++i)
259 ConfigTag* tag = i->second;
260 std::string hostmask = tag->getString("mask"); // An allowed CGI:IRC host
261 std::string type = tag->getString("type"); // What type of user-munging we do on this host.
262 std::string password = tag->getString("password");
264 if(hostmask.length())
266 if (type == "webirc" && password.empty())
268 ServerInstance->Logs->Log("CONFIG",LOG_DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
275 else if (type == "ident")
277 else if (type == "passfirst")
279 else if (type == "webirc")
284 ServerInstance->Logs->Log("CONFIG",LOG_DEFAULT, "m_cgiirc.so: Invalid <cgihost:type> value in config: %s, setting it to \"pass\"", type.c_str());
287 cmd.Hosts.push_back(CGIhost(hostmask, cgitype, password));
292 ServerInstance->Logs->Log("CONFIG",LOG_DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
298 ModResult OnCheckReady(LocalUser *user)
300 if (waiting.get(user))
303 std::string *webirc_ip = cmd.webirc_ip.get(user);
305 return MOD_RES_PASSTHRU;
307 ChangeIP(user, *webirc_ip);
309 std::string* webirc_hostname = cmd.webirc_hostname.get(user);
310 user->host = user->dhost = (webirc_hostname ? *webirc_hostname : user->GetIPString());
316 user->CheckLines(true);
320 cmd.webirc_hostname.unset(user);
321 cmd.webirc_ip.unset(user);
323 return MOD_RES_PASSTHRU;
326 ModResult OnUserRegister(LocalUser* user)
328 for(CGIHostlist::iterator iter = cmd.Hosts.begin(); iter != cmd.Hosts.end(); iter++)
330 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
333 if(iter->type == PASS)
335 CheckPass(user); // We do nothing if it fails so...
336 user->CheckLines(true);
338 else if(iter->type == PASSFIRST && !CheckPass(user))
340 // If the password lookup failed, try the ident
341 CheckIdent(user); // If this fails too, do nothing
342 user->CheckLines(true);
344 else if(iter->type == IDENT)
346 CheckIdent(user); // Nothing on failure.
347 user->CheckLines(true);
349 else if(iter->type == IDENTFIRST && !CheckIdent(user))
351 // If the ident lookup fails, try the password.
353 user->CheckLines(true);
355 else if(iter->type == WEBIRC)
357 // We don't need to do anything here
359 return MOD_RES_PASSTHRU;
362 return MOD_RES_PASSTHRU;
365 bool CheckPass(LocalUser* user)
367 if(IsValidHost(user->password))
369 HandleIdentOrPass(user, user->password, true);
370 user->password.clear();
377 bool CheckIdent(LocalUser* user)
382 if (user->ident.length() == 8)
383 ident = user->ident.c_str();
384 else if (user->ident.length() == 9 && user->ident[0] == '~')
385 ident = user->ident.c_str() + 1;
390 unsigned long ipaddr = strtoul(ident, NULL, 16);
393 newip.s_addr = htonl(ipaddr);
394 std::string newipstr(inet_ntoa(newip));
396 user->ident = "~cgiirc";
397 HandleIdentOrPass(user, newipstr, false);
402 bool IsValidHost(const std::string &host)
404 if(!host.size() || host.size() > 64)
407 for(unsigned int i = 0; i < host.size(); i++)
409 if( ((host[i] >= '0') && (host[i] <= '9')) ||
410 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
411 ((host[i] >= 'a') && (host[i] <= 'z')) ||
412 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
413 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
423 virtual Version GetVersion()
425 return Version("Change user's hosts connecting from known CGI:IRC hosts",VF_VENDOR);
430 MODULE_INIT(ModuleCgiIRC)