1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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
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, bool NotifyOpers, const std::string &source, bool forward, LocalUser* u, int userfd, const std::string &type, bool &cached)
110 : Resolver(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->eh == 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);
122 them->InvalidateCache();
123 them->CheckLines(true);
127 virtual void OnError(ResolverError e, const std::string &errormessage)
129 if ((them) && (&them->eh == ServerInstance->SE->GetRef(theirfd)))
132 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());
136 virtual ~CGIResolver()
141 class ModuleCgiIRC : public Module
146 ModuleCgiIRC() : cmd(this, NotifyOpers)
149 ServerInstance->AddCommand(&cmd);
150 ServerInstance->Extensions.Register(&cmd.realhost);
151 ServerInstance->Extensions.Register(&cmd.realip);
152 ServerInstance->Extensions.Register(&cmd.webirc_hostname);
153 ServerInstance->Extensions.Register(&cmd.webirc_ip);
155 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnDecodeMetaData, I_OnUserDisconnect, I_OnUserConnect };
156 ServerInstance->Modules->Attach(eventlist, this, 5);
160 virtual void Prioritize()
162 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIORITY_FIRST);
165 virtual void OnRehash(User* user)
170 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
172 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
175 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
177 std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
178 std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
179 std::string password = Conf.ReadValue("cgihost", "password", i);
181 if(hostmask.length())
183 if (type == "webirc" && !password.length()) {
184 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
188 CGItype cgitype = INVALID;
191 else if (type == "ident")
193 else if (type == "passfirst")
195 else if (type == "webirc")
200 if (cgitype == INVALID)
203 cmd.Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
208 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
214 virtual ModResult OnUserRegister(LocalUser* user)
216 for(CGIHostlist::iterator iter = cmd.Hosts.begin(); iter != cmd.Hosts.end(); iter++)
218 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
221 if(iter->type == PASS)
223 CheckPass(user); // We do nothing if it fails so...
224 user->CheckLines(true);
226 else if(iter->type == PASSFIRST && !CheckPass(user))
228 // If the password lookup failed, try the ident
229 CheckIdent(user); // If this fails too, do nothing
230 user->CheckLines(true);
232 else if(iter->type == IDENT)
234 CheckIdent(user); // Nothing on failure.
235 user->CheckLines(true);
237 else if(iter->type == IDENTFIRST && !CheckIdent(user))
239 // If the ident lookup fails, try the password.
241 user->CheckLines(true);
243 else if(iter->type == WEBIRC)
245 // We don't need to do anything here
247 return MOD_RES_PASSTHRU;
250 return MOD_RES_PASSTHRU;
253 virtual void OnUserConnect(LocalUser* user)
255 std::string *webirc_hostname = cmd.webirc_hostname.get(user);
256 std::string *webirc_ip = cmd.webirc_ip.get(user);
259 user->host.assign(*webirc_hostname, 0, 64);
260 user->dhost.assign(*webirc_hostname, 0, 64);
261 user->InvalidateCache();
262 cmd.webirc_hostname.unset(user);
266 ServerInstance->Users->RemoveCloneCounts(user);
267 user->SetClientIP(webirc_ip->c_str());
268 user->InvalidateCache();
269 cmd.webirc_ip.unset(user);
270 ServerInstance->Users->AddLocalClone(user);
271 ServerInstance->Users->AddGlobalClone(user);
274 user->CheckLines(true);
278 bool CheckPass(LocalUser* user)
280 if(IsValidHost(user->password))
282 cmd.realhost.set(user, user->host);
283 cmd.realip.set(user, user->GetIPString());
284 user->host.assign(user->password, 0, 64);
285 user->dhost.assign(user->password, 0, 64);
286 user->InvalidateCache();
288 ServerInstance->Users->RemoveCloneCounts(user);
289 user->SetClientIP(user->password.c_str());
290 ServerInstance->Users->AddLocalClone(user);
291 ServerInstance->Users->AddGlobalClone(user);
299 CGIResolver* r = new CGIResolver(this, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
300 ServerInstance->AddResolver(r, cached);
305 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());
308 user->password.clear();
315 bool CheckIdent(LocalUser* user)
318 int len = user->ident.length();
322 ident = user->ident.c_str();
323 else if(len == 9 && user->ident[0] == '~')
324 ident = user->ident.c_str() + 1;
329 unsigned long ipaddr = strtoul(ident, NULL, 16);
332 newip.s_addr = htonl(ipaddr);
333 char* newipstr = inet_ntoa(newip);
335 cmd.realhost.set(user, user->host);
336 cmd.realip.set(user, user->GetIPString());
337 ServerInstance->Users->RemoveCloneCounts(user);
338 user->SetClientIP(newipstr);
339 ServerInstance->Users->AddLocalClone(user);
340 ServerInstance->Users->AddGlobalClone(user);
343 user->host = newipstr;
344 user->dhost = newipstr;
345 user->ident.assign("~cgiirc", 0, 8);
350 CGIResolver* r = new CGIResolver(this, NotifyOpers, newipstr, false, user, user->GetFd(), "IDENT", cached);
351 ServerInstance->AddResolver(r, cached);
355 user->InvalidateCache();
358 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());
364 bool IsValidHost(const std::string &host)
369 for(unsigned int i = 0; i < host.size(); i++)
371 if( ((host[i] >= '0') && (host[i] <= '9')) ||
372 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
373 ((host[i] >= 'a') && (host[i] <= 'z')) ||
374 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
375 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
385 bool IsValidIP(const std::string &ip)
387 if(ip.size() < 7 || ip.size() > 15)
393 for(unsigned int i = 0; i < ip.size(); i++)
395 if((dots <= 3) && (sincedot <= 3))
397 if((ip[i] >= '0') && (ip[i] <= '9'))
401 else if(ip[i] == '.')
420 virtual ~ModuleCgiIRC()
424 virtual Version GetVersion()
426 return Version("Change user's hosts connecting from known CGI:IRC hosts",VF_VENDOR);
431 MODULE_INIT(ModuleCgiIRC)