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
59 CommandWebirc(InspIRCd* Instance, CGIHostlist &cHosts, bool bnotify) : Command(Instance, "WEBIRC", 0, 4, true), Hosts(cHosts), notify(bnotify)
61 this->source = "m_cgiirc.so";
62 this->syntax = "password client hostname ip";
64 CmdResult Handle(const std::vector<std::string> ¶meters, User *user)
66 if(user->registered == REG_ALL)
69 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
71 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
73 if(iter->type == WEBIRC && parameters[0] == iter->password)
75 user->Extend("cgiirc_realhost", new std::string(user->host));
76 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
78 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());
79 user->Extend("cgiirc_webirc_hostname", new std::string(parameters[2]));
80 user->Extend("cgiirc_webirc_ip", new std::string(parameters[3]));
86 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s tried to use WEBIRC, but didn't match any configured webirc blocks.", user->GetFullRealHost().c_str());
92 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
94 class CGIResolver : public Resolver
101 CGIResolver(Module* me, InspIRCd* Instance, bool NotifyOpers, const std::string &source, bool forward, User* u, int userfd, const std::string &type, bool &cached)
102 : Resolver(Instance, source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
104 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum = 0)
109 /* Check the user still exists */
110 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
113 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());
115 them->host.assign(result,0, 64);
116 them->dhost.assign(result, 0, 64);
118 them->SetSockAddr(them->GetProtocolFamily(), result.c_str(), them->GetPort());
119 them->ident.assign("~cgiirc", 0, 8);
120 them->InvalidateCache();
121 them->CheckLines(true);
125 virtual void OnError(ResolverError e, const std::string &errormessage)
127 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
130 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());
134 virtual ~CGIResolver()
139 class ModuleCgiIRC : public Module
141 CommandWebirc* mycommand;
145 ModuleCgiIRC(InspIRCd* Me) : Module(Me)
149 mycommand = new CommandWebirc(Me, Hosts, NotifyOpers);
150 ServerInstance->AddCommand(mycommand);
152 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCleanup, I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnUserDisconnect, I_OnUserConnect };
153 ServerInstance->Modules->Attach(eventlist, this, 7);
157 virtual void Prioritize()
159 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIORITY_FIRST);
162 virtual void OnRehash(User* user)
164 ConfigReader Conf(ServerInstance);
167 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
169 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
172 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
174 std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
175 std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
176 std::string password = Conf.ReadValue("cgihost", "password", i);
178 if(hostmask.length())
180 if (type == "webirc" && !password.length()) {
181 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
185 CGItype cgitype = INVALID;
188 else if (type == "ident")
190 else if (type == "passfirst")
192 else if (type == "webirc")
197 if (cgitype == INVALID)
200 Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
205 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
211 virtual void OnCleanup(int target_type, void* item)
213 if(target_type == TYPE_USER)
215 User* user = (User*)item;
216 std::string* realhost;
219 if(user->GetExt("cgiirc_realhost", realhost))
222 user->Shrink("cgiirc_realhost");
225 if(user->GetExt("cgiirc_realip", realip))
228 user->Shrink("cgiirc_realip");
233 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
235 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
239 if(user->GetExt(extname, data))
241 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
246 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
248 if(target_type == TYPE_USER)
250 User* dest = (User*)target;
252 if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
254 dest->Extend(extname, new std::string(extdata));
259 virtual void OnUserDisconnect(User* user)
261 OnCleanup(TYPE_USER, user);
265 virtual int OnUserRegister(User* user)
267 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
269 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
272 if(iter->type == PASS)
274 CheckPass(user); // We do nothing if it fails so...
275 user->CheckLines(true);
277 else if(iter->type == PASSFIRST && !CheckPass(user))
279 // If the password lookup failed, try the ident
280 CheckIdent(user); // If this fails too, do nothing
281 user->CheckLines(true);
283 else if(iter->type == IDENT)
285 CheckIdent(user); // Nothing on failure.
286 user->CheckLines(true);
288 else if(iter->type == IDENTFIRST && !CheckIdent(user))
290 // If the ident lookup fails, try the password.
292 user->CheckLines(true);
294 else if(iter->type == WEBIRC)
296 // We don't need to do anything here
304 virtual void OnUserConnect(User* user)
306 std::string *webirc_hostname, *webirc_ip;
307 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
309 user->host.assign(*webirc_hostname, 0, 64);
310 user->dhost.assign(*webirc_hostname, 0, 64);
311 delete webirc_hostname;
312 user->InvalidateCache();
313 user->Shrink("cgiirc_webirc_hostname");
315 if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
317 ServerInstance->Users->RemoveCloneCounts(user);
318 user->SetSockAddr(user->GetProtocolFamily(), webirc_ip->c_str(), user->GetPort());
320 user->InvalidateCache();
321 user->Shrink("cgiirc_webirc_ip");
322 ServerInstance->Users->AddLocalClone(user);
323 ServerInstance->Users->AddGlobalClone(user);
325 user->CheckLines(true);
329 bool CheckPass(User* user)
331 if(IsValidHost(user->password))
333 user->Extend("cgiirc_realhost", new std::string(user->host));
334 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
335 user->host.assign(user->password, 0, 64);
336 user->dhost.assign(user->password, 0, 64);
337 user->InvalidateCache();
340 ServerInstance->Users->RemoveCloneCounts(user);
342 if (user->GetProtocolFamily() == AF_INET6)
343 valid = (inet_pton(AF_INET6, user->password.c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
345 valid = (inet_aton(user->password.c_str(), &((sockaddr_in*)user->ip)->sin_addr));
347 if (inet_aton(user->password.c_str(), &((sockaddr_in*)user->ip)->sin_addr))
350 ServerInstance->Users->AddLocalClone(user);
351 ServerInstance->Users->AddGlobalClone(user);
356 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
358 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());
362 /* We got as resolved hostname in the password. */
367 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
368 ServerInstance->AddResolver(r, cached);
373 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());
377 user->password.clear();
384 bool CheckIdent(User* user)
389 int len = user->ident.length();
392 ident = user->ident.c_str();
393 else if(len == 9 && user->ident[0] == '~')
394 ident = user->ident.c_str() + 1;
398 for(int i = 0; i < 4; i++)
399 if(!HexToInt(ip[i], ident + i*2))
402 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
404 user->Extend("cgiirc_realhost", new std::string(user->host));
405 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
406 ServerInstance->Users->RemoveCloneCounts(user);
407 user->SetSockAddr(user->GetProtocolFamily(), newip, user->GetPort());
408 ServerInstance->Users->AddLocalClone(user);
409 ServerInstance->Users->AddGlobalClone(user);
413 user->host.assign(newip, 0, 16);
414 user->dhost.assign(newip, 0, 16);
415 user->ident.assign("~cgiirc", 0, 8);
418 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
419 ServerInstance->AddResolver(r, cached);
423 user->host.assign(newip, 0, 16);
424 user->dhost.assign(newip, 0, 16);
425 user->ident.assign("~cgiirc", 0, 8);
426 user->InvalidateCache();
429 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());
435 bool IsValidHost(const std::string &host)
440 for(unsigned int i = 0; i < host.size(); i++)
442 if( ((host[i] >= '0') && (host[i] <= '9')) ||
443 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
444 ((host[i] >= 'a') && (host[i] <= 'z')) ||
445 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
446 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
456 bool IsValidIP(const std::string &ip)
458 if(ip.size() < 7 || ip.size() > 15)
464 for(unsigned int i = 0; i < ip.size(); i++)
466 if((dots <= 3) && (sincedot <= 3))
468 if((ip[i] >= '0') && (ip[i] <= '9'))
472 else if(ip[i] == '.')
491 bool HexToInt(int &out, const char* in)
497 out = strtol(ip, NULL, 16);
499 if(out > 255 || out < 0)
505 virtual ~ModuleCgiIRC()
509 virtual Version GetVersion()
511 return Version("$Id$",VF_VENDOR,API_VERSION);
516 MODULE_INIT(ModuleCgiIRC)