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(InspIRCd* Instance, Module* Creator, bool bnotify)
65 : Command(Instance, Creator, "WEBIRC", 0, 4, true), 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 this->syntax = "password client hostname ip";
71 CmdResult Handle(const std::vector<std::string> ¶meters, User *user)
73 if(user->registered == REG_ALL)
76 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
78 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
80 if(iter->type == WEBIRC && parameters[0] == iter->password)
82 realhost.set(user, user->host);
83 realip.set(user, user->GetIPString());
85 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());
86 webirc_hostname.set(user, parameters[2]);
87 webirc_ip.set(user, parameters[3]);
93 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s tried to use WEBIRC, but didn't match any configured webirc blocks.", user->GetFullRealHost().c_str());
99 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
101 class CGIResolver : public Resolver
108 CGIResolver(Module* me, InspIRCd* Instance, bool NotifyOpers, const std::string &source, bool forward, User* u, int userfd, const std::string &type, bool &cached)
109 : Resolver(Instance, source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
111 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
113 /* Check the user still exists */
114 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
117 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());
119 them->host.assign(result,0, 64);
120 them->dhost.assign(result, 0, 64);
122 them->SetClientIP(result.c_str());
123 them->ident.assign("~cgiirc", 0, 8);
124 them->InvalidateCache();
125 them->CheckLines(true);
129 virtual void OnError(ResolverError e, const std::string &errormessage)
131 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
134 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());
138 virtual ~CGIResolver()
143 class ModuleCgiIRC : public Module
148 ModuleCgiIRC(InspIRCd* Me) : Module(Me), cmd(Me, this, NotifyOpers)
151 ServerInstance->AddCommand(&cmd);
152 Extensible::Register(&cmd.realhost);
153 Extensible::Register(&cmd.realip);
154 Extensible::Register(&cmd.webirc_hostname);
155 Extensible::Register(&cmd.webirc_ip);
157 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCleanup, I_OnSyncUser, I_OnDecodeMetaData, I_OnUserDisconnect, I_OnUserConnect };
158 ServerInstance->Modules->Attach(eventlist, this, 7);
162 virtual void Prioritize()
164 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIORITY_FIRST);
167 virtual void OnRehash(User* user)
169 ConfigReader Conf(ServerInstance);
172 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
174 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
177 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
179 std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
180 std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
181 std::string password = Conf.ReadValue("cgihost", "password", i);
183 if(hostmask.length())
185 if (type == "webirc" && !password.length()) {
186 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
190 CGItype cgitype = INVALID;
193 else if (type == "ident")
195 else if (type == "passfirst")
197 else if (type == "webirc")
202 if (cgitype == INVALID)
205 cmd.Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
210 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
216 virtual ModResult OnUserRegister(User* user)
218 for(CGIHostlist::iterator iter = cmd.Hosts.begin(); iter != cmd.Hosts.end(); iter++)
220 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
223 if(iter->type == PASS)
225 CheckPass(user); // We do nothing if it fails so...
226 user->CheckLines(true);
228 else if(iter->type == PASSFIRST && !CheckPass(user))
230 // If the password lookup failed, try the ident
231 CheckIdent(user); // If this fails too, do nothing
232 user->CheckLines(true);
234 else if(iter->type == IDENT)
236 CheckIdent(user); // Nothing on failure.
237 user->CheckLines(true);
239 else if(iter->type == IDENTFIRST && !CheckIdent(user))
241 // If the ident lookup fails, try the password.
243 user->CheckLines(true);
245 else if(iter->type == WEBIRC)
247 // We don't need to do anything here
249 return MOD_RES_PASSTHRU;
252 return MOD_RES_PASSTHRU;
255 virtual void OnUserConnect(User* user)
257 std::string *webirc_hostname = cmd.webirc_hostname.get(user);
258 std::string *webirc_ip = cmd.webirc_ip.get(user);
261 user->host.assign(*webirc_hostname, 0, 64);
262 user->dhost.assign(*webirc_hostname, 0, 64);
263 user->InvalidateCache();
264 cmd.webirc_hostname.unset(user);
268 ServerInstance->Users->RemoveCloneCounts(user);
269 user->SetClientIP(webirc_ip->c_str());
270 user->InvalidateCache();
271 cmd.webirc_ip.unset(user);
272 ServerInstance->Users->AddLocalClone(user);
273 ServerInstance->Users->AddGlobalClone(user);
275 user->CheckLines(true);
279 bool CheckPass(User* user)
281 if(IsValidHost(user->password))
283 cmd.realhost.set(user, user->host);
284 cmd.realip.set(user, user->GetIPString());
285 user->host.assign(user->password, 0, 64);
286 user->dhost.assign(user->password, 0, 64);
287 user->InvalidateCache();
290 ServerInstance->Users->RemoveCloneCounts(user);
291 valid = user->SetClientIP(user->password.c_str());
292 ServerInstance->Users->AddLocalClone(user);
293 ServerInstance->Users->AddGlobalClone(user);
298 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
300 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());
304 /* We got as resolved hostname in the password. */
309 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
310 ServerInstance->AddResolver(r, cached);
315 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());
319 user->password.clear();
326 bool CheckIdent(User* user)
329 int len = user->ident.length();
333 ident = user->ident.c_str();
334 else if(len == 9 && user->ident[0] == '~')
335 ident = user->ident.c_str() + 1;
340 unsigned long ipaddr = strtoul(ident, NULL, 16);
343 newip.s_addr = htonl(ipaddr);
344 char* newipstr = inet_ntoa(newip);
346 cmd.realhost.set(user, user->host);
347 cmd.realip.set(user, user->GetIPString());
348 ServerInstance->Users->RemoveCloneCounts(user);
349 user->SetClientIP(newipstr);
350 ServerInstance->Users->AddLocalClone(user);
351 ServerInstance->Users->AddGlobalClone(user);
353 user->host = newipstr;
354 user->dhost = newipstr;
355 user->ident.assign("~cgiirc", 0, 8);
360 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newipstr, false, user, user->GetFd(), "IDENT", cached);
361 ServerInstance->AddResolver(r, cached);
365 user->InvalidateCache();
368 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());
374 bool IsValidHost(const std::string &host)
379 for(unsigned int i = 0; i < host.size(); i++)
381 if( ((host[i] >= '0') && (host[i] <= '9')) ||
382 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
383 ((host[i] >= 'a') && (host[i] <= 'z')) ||
384 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
385 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
395 bool IsValidIP(const std::string &ip)
397 if(ip.size() < 7 || ip.size() > 15)
403 for(unsigned int i = 0; i < ip.size(); i++)
405 if((dots <= 3) && (sincedot <= 3))
407 if((ip[i] >= '0') && (ip[i] <= '9'))
411 else if(ip[i] == '.')
430 virtual ~ModuleCgiIRC()
434 virtual Version GetVersion()
436 return Version("$Id$",VF_VENDOR,API_VERSION);
441 MODULE_INIT(ModuleCgiIRC)