1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
22 /* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
24 enum CGItype { INVALID, PASS, IDENT, PASSFIRST, IDENTFIRST, WEBIRC };
27 /** Holds a CGI site's details
29 class CGIhost : public classbase
36 CGIhost(const std::string &mask = "", CGItype t = IDENTFIRST, const std::string &spassword ="")
37 : hostmask(mask), type(t), password(spassword)
41 typedef std::vector<CGIhost> CGIHostlist;
45 * This is used for the webirc method of CGIIRC auth, and is (really) the best way to do these things.
46 * Syntax: WEBIRC password client hostname ip
47 * Where password is a shared key, client is the name of the "client" and version (e.g. cgiirc), hostname
48 * is the resolved host of the client issuing the command and IP is the real IP of the client.
51 * To tie in with the rest of cgiirc module, and to avoid race conditions, /webirc is only processed locally
52 * and simply sets metadata on the user, which is later decoded on full connect to give something meaningful.
54 class CommandWebirc : public Command
58 StringExtItem realhost;
60 LocalStringExt webirc_hostname;
61 LocalStringExt webirc_ip;
64 CommandWebirc(Module* Creator, bool bnotify)
65 : Command(Creator, "WEBIRC", 4), notify(bnotify),
66 realhost("cgiirc_realhost", Creator), realip("cgiirc_realip", Creator),
67 webirc_hostname("cgiirc_webirc_hostname", Creator), webirc_ip("cgiirc_webirc_ip", Creator)
69 works_before_reg = true;
70 this->syntax = "password client hostname ip";
72 CmdResult Handle(const std::vector<std::string> ¶meters, User *user)
74 if(user->registered == REG_ALL)
77 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
79 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
81 if(iter->type == WEBIRC && parameters[0] == iter->password)
83 realhost.set(user, user->host);
84 realip.set(user, user->GetIPString());
86 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(), parameters[2].c_str(), user->host.c_str());
87 webirc_hostname.set(user, parameters[2]);
88 webirc_ip.set(user, parameters[3]);
94 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s tried to use WEBIRC, but didn't match any configured webirc blocks.", user->GetFullRealHost().c_str());
100 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
102 class CGIResolver : public Resolver
109 CGIResolver(Module* me, InspIRCd* Instance, bool NotifyOpers, const std::string &source, bool forward, User* u, int userfd, const std::string &type, bool &cached)
110 : Resolver(Instance, source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
112 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
114 /* Check the user still exists */
115 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
118 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(), result.c_str(), typ.c_str());
120 them->host.assign(result,0, 64);
121 them->dhost.assign(result, 0, 64);
123 them->SetClientIP(result.c_str());
124 them->ident.assign("~cgiirc", 0, 8);
125 them->InvalidateCache();
126 them->CheckLines(true);
130 virtual void OnError(ResolverError e, const std::string &errormessage)
132 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
135 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());
139 virtual ~CGIResolver()
144 class ModuleCgiIRC : public Module
149 ModuleCgiIRC(InspIRCd* Me) : Module(Me), cmd(this, NotifyOpers)
152 ServerInstance->AddCommand(&cmd);
153 Extensible::Register(&cmd.realhost);
154 Extensible::Register(&cmd.realip);
155 Extensible::Register(&cmd.webirc_hostname);
156 Extensible::Register(&cmd.webirc_ip);
158 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCleanup, I_OnSyncUser, I_OnDecodeMetaData, I_OnUserDisconnect, I_OnUserConnect };
159 ServerInstance->Modules->Attach(eventlist, this, 7);
163 virtual void Prioritize()
165 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIORITY_FIRST);
168 virtual void OnRehash(User* user)
170 ConfigReader Conf(ServerInstance);
173 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
175 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
178 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
180 std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
181 std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
182 std::string password = Conf.ReadValue("cgihost", "password", i);
184 if(hostmask.length())
186 if (type == "webirc" && !password.length()) {
187 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
191 CGItype cgitype = INVALID;
194 else if (type == "ident")
196 else if (type == "passfirst")
198 else if (type == "webirc")
203 if (cgitype == INVALID)
206 cmd.Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
211 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
217 virtual ModResult OnUserRegister(User* user)
219 for(CGIHostlist::iterator iter = cmd.Hosts.begin(); iter != cmd.Hosts.end(); iter++)
221 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
224 if(iter->type == PASS)
226 CheckPass(user); // We do nothing if it fails so...
227 user->CheckLines(true);
229 else if(iter->type == PASSFIRST && !CheckPass(user))
231 // If the password lookup failed, try the ident
232 CheckIdent(user); // If this fails too, do nothing
233 user->CheckLines(true);
235 else if(iter->type == IDENT)
237 CheckIdent(user); // Nothing on failure.
238 user->CheckLines(true);
240 else if(iter->type == IDENTFIRST && !CheckIdent(user))
242 // If the ident lookup fails, try the password.
244 user->CheckLines(true);
246 else if(iter->type == WEBIRC)
248 // We don't need to do anything here
250 return MOD_RES_PASSTHRU;
253 return MOD_RES_PASSTHRU;
256 virtual void OnUserConnect(User* user)
258 std::string *webirc_hostname = cmd.webirc_hostname.get(user);
259 std::string *webirc_ip = cmd.webirc_ip.get(user);
262 user->host.assign(*webirc_hostname, 0, 64);
263 user->dhost.assign(*webirc_hostname, 0, 64);
264 user->InvalidateCache();
265 cmd.webirc_hostname.unset(user);
269 ServerInstance->Users->RemoveCloneCounts(user);
270 user->SetClientIP(webirc_ip->c_str());
271 user->InvalidateCache();
272 cmd.webirc_ip.unset(user);
273 ServerInstance->Users->AddLocalClone(user);
274 ServerInstance->Users->AddGlobalClone(user);
276 user->CheckLines(true);
280 bool CheckPass(User* user)
282 if(IsValidHost(user->password))
284 cmd.realhost.set(user, user->host);
285 cmd.realip.set(user, user->GetIPString());
286 user->host.assign(user->password, 0, 64);
287 user->dhost.assign(user->password, 0, 64);
288 user->InvalidateCache();
291 ServerInstance->Users->RemoveCloneCounts(user);
292 valid = user->SetClientIP(user->password.c_str());
293 ServerInstance->Users->AddLocalClone(user);
294 ServerInstance->Users->AddGlobalClone(user);
299 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
301 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s detected as using CGI:IRC (%s), changing real host to %s from PASS", user->nick.c_str(), user->host.c_str(), user->password.c_str());
305 /* We got as resolved hostname in the password. */
310 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
311 ServerInstance->AddResolver(r, cached);
316 ServerInstance->SNO->WriteToSnoMask('a', "Connecting user %s detected as using CGI:IRC (%s), but I could not resolve their hostname!", user->nick.c_str(), user->host.c_str());
320 user->password.clear();
327 bool CheckIdent(User* user)
330 int len = user->ident.length();
334 ident = user->ident.c_str();
335 else if(len == 9 && user->ident[0] == '~')
336 ident = user->ident.c_str() + 1;
341 unsigned long ipaddr = strtoul(ident, NULL, 16);
344 newip.s_addr = htonl(ipaddr);
345 char* newipstr = inet_ntoa(newip);
347 cmd.realhost.set(user, user->host);
348 cmd.realip.set(user, user->GetIPString());
349 ServerInstance->Users->RemoveCloneCounts(user);
350 user->SetClientIP(newipstr);
351 ServerInstance->Users->AddLocalClone(user);
352 ServerInstance->Users->AddGlobalClone(user);
354 user->host = newipstr;
355 user->dhost = newipstr;
356 user->ident.assign("~cgiirc", 0, 8);
361 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newipstr, false, user, user->GetFd(), "IDENT", cached);
362 ServerInstance->AddResolver(r, cached);
366 user->InvalidateCache();
369 ServerInstance->SNO->WriteToSnoMask('a', "Connecting user %s detected as using CGI:IRC (%s), but I could not resolve their hostname!", user->nick.c_str(), user->host.c_str());
375 bool IsValidHost(const std::string &host)
380 for(unsigned int i = 0; i < host.size(); i++)
382 if( ((host[i] >= '0') && (host[i] <= '9')) ||
383 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
384 ((host[i] >= 'a') && (host[i] <= 'z')) ||
385 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
386 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
396 bool IsValidIP(const std::string &ip)
398 if(ip.size() < 7 || ip.size() > 15)
404 for(unsigned int i = 0; i < ip.size(); i++)
406 if((dots <= 3) && (sincedot <= 3))
408 if((ip[i] >= '0') && (ip[i] <= '9'))
412 else if(ip[i] == '.')
431 virtual ~ModuleCgiIRC()
435 virtual Version GetVersion()
437 return Version("$Id$",VF_VENDOR,API_VERSION);
442 MODULE_INIT(ModuleCgiIRC)