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(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
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);
115 them->ident.assign("~cgiirc", 0, 8);
116 them->InvalidateCache();
121 virtual void OnError(ResolverError e, const std::string &errormessage)
123 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
126 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());
130 virtual ~CGIResolver()
135 class ModuleCgiIRC : public Module
137 CommandWebirc* mycommand;
141 ModuleCgiIRC(InspIRCd* Me) : Module(Me)
145 mycommand = new CommandWebirc(Me, Hosts, NotifyOpers);
146 ServerInstance->AddCommand(mycommand);
148 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCleanup, I_OnSyncUserMetaData, I_OnDecodeMetaData, I_OnUserDisconnect, I_OnUserConnect };
149 ServerInstance->Modules->Attach(eventlist, this, 7);
153 virtual void Prioritize()
155 ServerInstance->Modules->SetPriority(this, I_OnUserConnect, PRIO_FIRST);
158 virtual void OnRehash(User* user, const std::string ¶meter)
160 ConfigReader Conf(ServerInstance);
163 NotifyOpers = Conf.ReadFlag("cgiirc", "opernotice", 0); // If we send an oper notice when a CGI:IRC has their host changed.
165 if(Conf.GetError() == CONF_VALUE_NOT_FOUND)
168 for(int i = 0; i < Conf.Enumerate("cgihost"); i++)
170 std::string hostmask = Conf.ReadValue("cgihost", "mask", i); // An allowed CGI:IRC host
171 std::string type = Conf.ReadValue("cgihost", "type", i); // What type of user-munging we do on this host.
172 std::string password = Conf.ReadValue("cgihost", "password", i);
174 if(hostmask.length())
176 if (type == "webirc" && !password.length()) {
177 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
181 CGItype cgitype = INVALID;
184 else if (type == "ident")
186 else if (type == "passfirst")
188 else if (type == "webirc")
193 if (cgitype == INVALID)
196 Hosts.push_back(CGIhost(hostmask,cgitype, password.length() ? password : "" ));
201 ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
207 virtual void OnCleanup(int target_type, void* item)
209 if(target_type == TYPE_USER)
211 User* user = (User*)item;
212 std::string* realhost;
215 if(user->GetExt("cgiirc_realhost", realhost))
218 user->Shrink("cgiirc_realhost");
221 if(user->GetExt("cgiirc_realip", realip))
224 user->Shrink("cgiirc_realip");
229 virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
231 if((extname == "cgiirc_realhost") || (extname == "cgiirc_realip"))
235 if(user->GetExt(extname, data))
237 proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, *data);
242 virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
244 if(target_type == TYPE_USER)
246 User* dest = (User*)target;
248 if(((extname == "cgiirc_realhost") || (extname == "cgiirc_realip")) && (!dest->GetExt(extname, bleh)))
250 dest->Extend(extname, new std::string(extdata));
255 virtual void OnUserDisconnect(User* user)
257 OnCleanup(TYPE_USER, user);
261 virtual int OnUserRegister(User* user)
263 for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
265 if(ServerInstance->MatchText(user->host, iter->hostmask) || ServerInstance->MatchText(user->GetIPString(), iter->hostmask))
268 if(iter->type == PASS)
270 CheckPass(user); // We do nothing if it fails so...
273 else if(iter->type == PASSFIRST && !CheckPass(user))
275 // If the password lookup failed, try the ident
276 CheckIdent(user); // If this fails too, do nothing
279 else if(iter->type == IDENT)
281 CheckIdent(user); // Nothing on failure.
284 else if(iter->type == IDENTFIRST && !CheckIdent(user))
286 // If the ident lookup fails, try the password.
290 else if(iter->type == WEBIRC)
292 // We don't need to do anything here
300 virtual void OnUserConnect(User* user)
302 std::string *webirc_hostname, *webirc_ip;
303 if(user->GetExt("cgiirc_webirc_hostname", webirc_hostname))
305 user->host.assign(*webirc_hostname, 0, 64);
306 user->dhost.assign(*webirc_hostname, 0, 64);
307 delete webirc_hostname;
308 user->InvalidateCache();
309 user->Shrink("cgiirc_webirc_hostname");
311 if(user->GetExt("cgiirc_webirc_ip", webirc_ip))
314 ServerInstance->Users->RemoveCloneCounts(user);
316 valid = (inet_pton(AF_INET6, webirc_ip->c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
319 valid = (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr));
321 if (inet_aton(webirc_ip->c_str(), &((sockaddr_in*)user->ip)->sin_addr))
326 user->InvalidateCache();
327 user->Shrink("cgiirc_webirc_ip");
328 ServerInstance->Users->AddLocalClone(user);
329 ServerInstance->Users->AddGlobalClone(user);
335 bool CheckPass(User* user)
337 if(IsValidHost(user->password))
339 user->Extend("cgiirc_realhost", new std::string(user->host));
340 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
341 user->host.assign(user->password, 0, 64);
342 user->dhost.assign(user->password, 0, 64);
343 user->InvalidateCache();
346 ServerInstance->Users->RemoveCloneCounts(user);
348 if (user->GetProtocolFamily() == AF_INET6)
349 valid = (inet_pton(AF_INET6, user->password.c_str(), &((sockaddr_in6*)user->ip)->sin6_addr) > 0);
351 valid = (inet_aton(user->password.c_str(), &((sockaddr_in*)user->ip)->sin_addr));
353 if (inet_aton(user->password.c_str(), &((sockaddr_in*)user->ip)->sin_addr))
356 ServerInstance->Users->AddLocalClone(user);
357 ServerInstance->Users->AddGlobalClone(user);
362 /* We were given a IP in the password, we don't do DNS so they get this is as their host as well. */
364 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());
368 /* We got as resolved hostname in the password. */
373 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, user->password, false, user, user->GetFd(), "PASS", cached);
374 ServerInstance->AddResolver(r, cached);
379 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());
383 user->password.clear();
390 bool CheckIdent(User* user)
395 int len = user->ident.length();
398 ident = user->ident.c_str();
399 else if(len == 9 && user->ident[0] == '~')
400 ident = user->ident.c_str() + 1;
404 for(int i = 0; i < 4; i++)
405 if(!HexToInt(ip[i], ident + i*2))
408 snprintf(newip, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
410 user->Extend("cgiirc_realhost", new std::string(user->host));
411 user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
412 ServerInstance->Users->RemoveCloneCounts(user);
414 if (user->GetProtocolFamily() == AF_INET6)
415 inet_pton(AF_INET6, newip, &((sockaddr_in6*)user->ip)->sin6_addr);
418 inet_aton(newip, &((sockaddr_in*)user->ip)->sin_addr);
419 ServerInstance->Users->AddLocalClone(user);
420 ServerInstance->Users->AddGlobalClone(user);
424 user->host.assign(newip, 0, 16);
425 user->dhost.assign(newip, 0, 16);
426 user->ident.assign("~cgiirc", 0, 8);
429 CGIResolver* r = new CGIResolver(this, ServerInstance, NotifyOpers, newip, false, user, user->GetFd(), "IDENT", cached);
430 ServerInstance->AddResolver(r, cached);
434 user->host.assign(newip, 0, 16);
435 user->dhost.assign(newip, 0, 16);
436 user->ident.assign("~cgiirc", 0, 8);
437 user->InvalidateCache();
440 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());
446 bool IsValidHost(const std::string &host)
451 for(unsigned int i = 0; i < host.size(); i++)
453 if( ((host[i] >= '0') && (host[i] <= '9')) ||
454 ((host[i] >= 'A') && (host[i] <= 'Z')) ||
455 ((host[i] >= 'a') && (host[i] <= 'z')) ||
456 ((host[i] == '-') && (i > 0) && (i+1 < host.size()) && (host[i-1] != '.') && (host[i+1] != '.')) ||
457 ((host[i] == '.') && (i > 0) && (i+1 < host.size())) )
467 bool IsValidIP(const std::string &ip)
469 if(ip.size() < 7 || ip.size() > 15)
475 for(unsigned int i = 0; i < ip.size(); i++)
477 if((dots <= 3) && (sincedot <= 3))
479 if((ip[i] >= '0') && (ip[i] <= '9'))
483 else if(ip[i] == '.')
502 bool HexToInt(int &out, const char* in)
508 out = strtol(ip, NULL, 16);
510 if(out > 255 || out < 0)
516 virtual ~ModuleCgiIRC()
520 virtual Version GetVersion()
522 return Version(1,2,0,0,VF_VENDOR,API_VERSION);
527 MODULE_INIT(ModuleCgiIRC)