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;
187 static void RecheckClass(LocalUser* user)
189 user->MyClass = NULL;
194 static void ChangeIP(LocalUser* user, const std::string& newip)
196 ServerInstance->Users->RemoveCloneCounts(user);
197 user->SetClientIP(newip.c_str());
198 ServerInstance->Users->AddLocalClone(user);
199 ServerInstance->Users->AddGlobalClone(user);
202 void HandleIdentOrPass(LocalUser* user, const std::string& newip, bool was_pass)
204 cmd.realhost.set(user, user->host);
205 cmd.realip.set(user, user->GetIPString());
206 ChangeIP(user, newip);
207 user->host = user->dhost = user->GetIPString();
208 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 || 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)
251 nouserdns = ServerInstance->Config->ConfValue("performance")->getBool("nouserdns");
254 // Do we send an oper notice when a CGI:IRC has their host changed?
255 cmd.notify = ServerInstance->Config->ConfValue("cgiirc")->getBool("opernotice", true);
257 ConfigTagList tags = ServerInstance->Config->ConfTags("cgihost");
258 for (ConfigIter i = tags.first; i != tags.second; ++i)
260 ConfigTag* tag = i->second;
261 std::string hostmask = tag->getString("mask"); // An allowed CGI:IRC host
262 std::string type = tag->getString("type"); // What type of user-munging we do on this host.
263 std::string password = tag->getString("password");
265 if(hostmask.length())
267 if (type == "webirc" && password.empty())
269 ServerInstance->Logs->Log("CONFIG",LOG_DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
276 else if (type == "ident")
278 else if (type == "passfirst")
280 else if (type == "webirc")
285 ServerInstance->Logs->Log("CONFIG",LOG_DEFAULT, "m_cgiirc.so: Invalid <cgihost:type> value in config: %s, setting it to \"pass\"", type.c_str());
288 cmd.Hosts.push_back(CGIhost(hostmask, cgitype, password));
293 ServerInstance->Logs->Log("CONFIG",LOG_DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
299 ModResult OnCheckReady(LocalUser *user)
301 if (waiting.get(user))
304 std::string *webirc_ip = cmd.webirc_ip.get(user);
306 return MOD_RES_PASSTHRU;
308 ChangeIP(user, *webirc_ip);
310 std::string* webirc_hostname = cmd.webirc_hostname.get(user);
311 user->host = user->dhost = (webirc_hostname ? *webirc_hostname : user->GetIPString());
317 user->CheckLines(true);
321 cmd.webirc_hostname.unset(user);
322 cmd.webirc_ip.unset(user);
324 return MOD_RES_PASSTHRU;
327 ModResult OnUserRegister(LocalUser* user)
329 for(CGIHostlist::iterator iter = cmd.Hosts.begin(); iter != cmd.Hosts.end(); iter++)
331 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
334 if(iter->type == PASS)
336 CheckPass(user); // We do nothing if it fails so...
337 user->CheckLines(true);
339 else if(iter->type == PASSFIRST && !CheckPass(user))
341 // If the password lookup failed, try the ident
342 CheckIdent(user); // If this fails too, do nothing
343 user->CheckLines(true);
345 else if(iter->type == IDENT)
347 CheckIdent(user); // Nothing on failure.
348 user->CheckLines(true);
350 else if(iter->type == IDENTFIRST && !CheckIdent(user))
352 // If the ident lookup fails, try the password.
354 user->CheckLines(true);
356 else if(iter->type == WEBIRC)
358 // We don't need to do anything here
360 return MOD_RES_PASSTHRU;
363 return MOD_RES_PASSTHRU;
366 bool CheckPass(LocalUser* user)
368 if(IsValidHost(user->password))
370 HandleIdentOrPass(user, user->password, true);
371 user->password.clear();
378 bool CheckIdent(LocalUser* user)
383 if (user->ident.length() == 8)
384 ident = user->ident.c_str();
385 else if (user->ident.length() == 9 && user->ident[0] == '~')
386 ident = user->ident.c_str() + 1;
391 unsigned long ipaddr = strtoul(ident, NULL, 16);
394 newip.s_addr = htonl(ipaddr);
395 std::string newipstr(inet_ntoa(newip));
397 user->ident = "~cgiirc";
398 HandleIdentOrPass(user, newipstr, false);
403 bool IsValidHost(const std::string &host)
405 if(!host.size() || host.size() > 64)
408 for(unsigned int i = 0; i < host.size(); i++)
410 if( ((host[i] >= '0') && (host[i] <= '9')) ||
411 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
412 ((host[i] >= 'a') && (host[i] <= 'z')) ||
413 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
414 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
424 virtual Version GetVersion()
426 return Version("Change user's hosts connecting from known CGI:IRC hosts",VF_VENDOR);
431 MODULE_INIT(ModuleCgiIRC)