1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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->WriteToSnoMask('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]));
90 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
92 class CGIResolver : public Resolver
99 CGIResolver(Module* me, InspIRCd* Instance, bool NotifyOpers, const std::string &source, bool forward, User* u, int userfd, const std::string &type, bool &cached)
100 : Resolver(Instance, source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
102 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum = 0)
107 /* Check the user still exists */
108 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
111 ServerInstance->SNO->WriteToSnoMask('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());
113 them->host.assign(result,0, 64);
114 them->dhost.assign(result, 0, 64);
116 them->SetSockAddr(them->GetProtocolFamily(), result.c_str(), them->GetPort());
117 them->ident.assign("~cgiirc", 0, 8);
118 them->InvalidateCache();
119 them->CheckLines(true);
123 virtual void OnError(ResolverError e, const std::string &errormessage)
125 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
128 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());
132 virtual ~CGIResolver()
137 class ModuleCgiIRC : public Module
139 CommandWebirc* mycommand;
143 ModuleCgiIRC(InspIRCd* Me) : Module(Me)
147 mycommand = new CommandWebirc(Me, Hosts, NotifyOpers);
148 ServerInstance->AddCommand(mycommand);
150 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCleanup, I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnUserDisconnect, I_OnUserConnect };
151 ServerInstance->Modules->Attach(eventlist, this, 7);
155 virtual void Prioritize()
157 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIO_FIRST);
160 virtual void OnRehash(User* user, const std::string ¶meter)
162 ConfigReader Conf(ServerInstance);
165 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
167 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
170 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
172 std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
173 std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
174 std::string password = Conf.ReadValue("cgihost", "password", i);
176 if(hostmask.length())
178 if (type == "webirc" && !password.length()) {
179 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
183 CGItype cgitype = INVALID;
186 else if (type == "ident")
188 else if (type == "passfirst")
190 else if (type == "webirc")
195 if (cgitype == INVALID)
198 Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
203 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
209 virtual void OnCleanup(int target_type, void* item)
211 if(target_type == TYPE_USER)
213 User* user = (User*)item;
214 std::string* realhost;
217 if(user->GetExt("cgiirc_realhost", realhost))
220 user->Shrink("cgiirc_realhost");
223 if(user->GetExt("cgiirc_realip", realip))
226 user->Shrink("cgiirc_realip");
231 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
233 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
237 if(user->GetExt(extname, data))
239 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
244 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
246 if(target_type == TYPE_USER)
248 User* dest = (User*)target;
250 if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
252 dest->Extend(extname, new std::string(extdata));
257 virtual void OnUserDisconnect(User* user)
259 OnCleanup(TYPE_USER, user);
263 virtual int OnUserRegister(User* user)
265 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
267 if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
270 if(iter->type == PASS)
272 CheckPass(user); // We do nothing if it fails so...
273 user->CheckLines(true);
275 else if(iter->type == PASSFIRST && !CheckPass(user))
277 // If the password lookup failed, try the ident
278 CheckIdent(user); // If this fails too, do nothing
279 user->CheckLines(true);
281 else if(iter->type == IDENT)
283 CheckIdent(user); // Nothing on failure.
284 user->CheckLines(true);
286 else if(iter->type == IDENTFIRST && !CheckIdent(user))
288 // If the ident lookup fails, try the password.
290 user->CheckLines(true);
292 else if(iter->type == WEBIRC)
294 // We don't need to do anything here
302 virtual void OnUserConnect(User* user)
304 std::string *webirc_hostname, *webirc_ip;
305 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
307 user->host.assign(*webirc_hostname, 0, 64);
308 user->dhost.assign(*webirc_hostname, 0, 64);
309 delete webirc_hostname;
310 user->InvalidateCache();
311 user->Shrink("cgiirc_webirc_hostname");
313 if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
315 ServerInstance->Users->RemoveCloneCounts(user);
316 user->SetSockAddr(user->GetProtocolFamily(), webirc_ip->c_str(), user->GetPort());
318 user->InvalidateCache();
319 user->Shrink("cgiirc_webirc_ip");
320 ServerInstance->Users->AddLocalClone(user);
321 ServerInstance->Users->AddGlobalClone(user);
323 user->CheckLines(true);
327 bool CheckPass(User* user)
329 if(IsValidHost(user->password))
331 user->Extend("cgiirc_realhost", new std::string(user->host));
332 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
333 user->host.assign(user->password, 0, 64);
334 user->dhost.assign(user->password, 0, 64);
335 user->InvalidateCache();
338 ServerInstance->Users->RemoveCloneCounts(user);
340 if (user->GetProtocolFamily() == AF_INET6)
341 valid = (inet_pton(AF_INET6, user->password.c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
343 valid = (inet_aton(user->password.c_str(), &((sockaddr_in*)user->ip)->sin_addr));
345 if (inet_aton(user->password.c_str(), &((sockaddr_in*)user->ip)->sin_addr))
348 ServerInstance->Users->AddLocalClone(user);
349 ServerInstance->Users->AddGlobalClone(user);
354 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
356 ServerInstance->SNO->WriteToSnoMask('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());
360 /* We got as resolved hostname in the password. */
365 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
366 ServerInstance->AddResolver(r, cached);
371 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 user->password.clear();
382 bool CheckIdent(User* user)
387 int len = user->ident.length();
390 ident = user->ident.c_str();
391 else if(len == 9 && user->ident[0] == '~')
392 ident = user->ident.c_str() + 1;
396 for(int i = 0; i < 4; i++)
397 if(!HexToInt(ip[i], ident + i*2))
400 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
402 user->Extend("cgiirc_realhost", new std::string(user->host));
403 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
404 ServerInstance->Users->RemoveCloneCounts(user);
405 user->SetSockAddr(user->GetProtocolFamily(), newip, user->GetPort());
406 ServerInstance->Users->AddLocalClone(user);
407 ServerInstance->Users->AddGlobalClone(user);
411 user->host.assign(newip, 0, 16);
412 user->dhost.assign(newip, 0, 16);
413 user->ident.assign("~cgiirc", 0, 8);
416 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
417 ServerInstance->AddResolver(r, cached);
421 user->host.assign(newip, 0, 16);
422 user->dhost.assign(newip, 0, 16);
423 user->ident.assign("~cgiirc", 0, 8);
424 user->InvalidateCache();
427 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());
433 bool IsValidHost(const std::string &host)
438 for(unsigned int i = 0; i < host.size(); i++)
440 if( ((host[i] >= '0') && (host[i] <= '9')) ||
441 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
442 ((host[i] >= 'a') && (host[i] <= 'z')) ||
443 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
444 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
454 bool IsValidIP(const std::string &ip)
456 if(ip.size() < 7 || ip.size() > 15)
462 for(unsigned int i = 0; i < ip.size(); i++)
464 if((dots <= 3) && (sincedot <= 3))
466 if((ip[i] >= '0') && (ip[i] <= '9'))
470 else if(ip[i] == '.')
489 bool HexToInt(int &out, const char* in)
495 out = strtol(ip, NULL, 16);
497 if(out > 255 || out < 0)
503 virtual ~ModuleCgiIRC()
507 virtual Version GetVersion()
509 return Version("$Id$",VF_VENDOR,API_VERSION);
514 MODULE_INIT(ModuleCgiIRC)