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
105 std::string theiruid;
108 CGIResolver(Module* me, bool NotifyOpers, const std::string &source, bool forward, LocalUser* u, const std::string &type, bool &cached)
109 : Resolver(source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theiruid(u->uuid), notify(NotifyOpers) { }
111 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
113 /* Check the user still exists */
114 User* them = ServerInstance->FindUUID(theiruid);
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 if (result.length() > 64)
123 them->dhost = result;
124 them->InvalidateCache();
125 them->CheckLines(true);
129 virtual void OnError(ResolverError e, const std::string &errormessage)
131 User* them = ServerInstance->FindUUID(theiruid);
136 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());
140 virtual ~CGIResolver()
145 class ModuleCgiIRC : public Module
150 ModuleCgiIRC() : cmd(this, NotifyOpers)
153 ServerInstance->AddCommand(&cmd);
154 ServerInstance->Extensions.Register(&cmd.realhost);
155 ServerInstance->Extensions.Register(&cmd.realip);
156 ServerInstance->Extensions.Register(&cmd.webirc_hostname);
157 ServerInstance->Extensions.Register(&cmd.webirc_ip);
159 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnDecodeMetaData, I_OnUserDisconnect, I_OnUserConnect };
160 ServerInstance->Modules->Attach(eventlist, this, 5);
164 virtual void Prioritize()
166 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIORITY_FIRST);
169 virtual void OnRehash(User* user)
174 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
176 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
179 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
181 std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
182 std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
183 std::string password = Conf.ReadValue("cgihost", "password", i);
185 if(hostmask.length())
187 if (type == "webirc" && !password.length()) {
188 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
192 CGItype cgitype = INVALID;
195 else if (type == "ident")
197 else if (type == "passfirst")
199 else if (type == "webirc")
204 if (cgitype == INVALID)
207 cmd.Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
212 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
218 virtual ModResult OnUserRegister(LocalUser* user)
220 for(CGIHostlist::iterator iter = cmd.Hosts.begin(); iter != cmd.Hosts.end(); iter++)
222 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
225 if(iter->type == PASS)
227 CheckPass(user); // We do nothing if it fails so...
228 user->CheckLines(true);
230 else if(iter->type == PASSFIRST && !CheckPass(user))
232 // If the password lookup failed, try the ident
233 CheckIdent(user); // If this fails too, do nothing
234 user->CheckLines(true);
236 else if(iter->type == IDENT)
238 CheckIdent(user); // Nothing on failure.
239 user->CheckLines(true);
241 else if(iter->type == IDENTFIRST && !CheckIdent(user))
243 // If the ident lookup fails, try the password.
245 user->CheckLines(true);
247 else if(iter->type == WEBIRC)
249 // We don't need to do anything here
251 return MOD_RES_PASSTHRU;
254 return MOD_RES_PASSTHRU;
257 virtual void OnUserConnect(LocalUser* user)
259 std::string *webirc_hostname = cmd.webirc_hostname.get(user);
260 std::string *webirc_ip = cmd.webirc_ip.get(user);
261 if (webirc_hostname && webirc_hostname->length() < 64)
263 user->host = *webirc_hostname;
264 user->dhost = *webirc_hostname;
265 user->InvalidateCache();
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);
277 user->CheckLines(true);
279 cmd.webirc_hostname.unset(user);
282 bool CheckPass(LocalUser* user)
284 if(IsValidHost(user->password))
286 cmd.realhost.set(user, user->host);
287 cmd.realip.set(user, user->GetIPString());
288 user->host = user->password;
289 user->dhost = user->password;
290 user->InvalidateCache();
292 ServerInstance->Users->RemoveCloneCounts(user);
293 user->SetClientIP(user->password.c_str());
294 ServerInstance->Users->AddLocalClone(user);
295 ServerInstance->Users->AddGlobalClone(user);
303 CGIResolver* r = new CGIResolver(this, NotifyOpers, user->password, false, user, "PASS", cached);
304 ServerInstance->AddResolver(r, cached);
309 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());
312 user->password.clear();
319 bool CheckIdent(LocalUser* user)
322 int len = user->ident.length();
326 ident = user->ident.c_str();
327 else if(len == 9 && user->ident[0] == '~')
328 ident = user->ident.c_str() + 1;
333 unsigned long ipaddr = strtoul(ident, NULL, 16);
336 newip.s_addr = htonl(ipaddr);
337 char* newipstr = inet_ntoa(newip);
339 cmd.realhost.set(user, user->host);
340 cmd.realip.set(user, user->GetIPString());
341 ServerInstance->Users->RemoveCloneCounts(user);
342 user->SetClientIP(newipstr);
343 ServerInstance->Users->AddLocalClone(user);
344 ServerInstance->Users->AddGlobalClone(user);
347 user->host = newipstr;
348 user->dhost = newipstr;
349 user->ident.assign("~cgiirc", 0, 8);
354 CGIResolver* r = new CGIResolver(this, NotifyOpers, newipstr, false, user, "IDENT", cached);
355 ServerInstance->AddResolver(r, cached);
359 user->InvalidateCache();
362 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());
368 bool IsValidHost(const std::string &host)
370 if(!host.size() || host.size() > 64)
373 for(unsigned int i = 0; i < host.size(); i++)
375 if( ((host[i] >= '0') && (host[i] <= '9')) ||
376 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
377 ((host[i] >= 'a') && (host[i] <= 'z')) ||
378 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
379 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
389 bool IsValidIP(const std::string &ip)
391 if(ip.size() < 7 || ip.size() > 15)
397 for(unsigned int i = 0; i < ip.size(); i++)
399 if((dots <= 3) && (sincedot <= 3))
401 if((ip[i] >= '0') && (ip[i] <= '9'))
405 else if(ip[i] == '.')
424 virtual ~ModuleCgiIRC()
428 virtual Version GetVersion()
430 return Version("Change user's hosts connecting from known CGI:IRC hosts",VF_VENDOR);
435 MODULE_INIT(ModuleCgiIRC)