+/*
+ * WEBIRC
+ * This is used for the webirc method of CGIIRC auth, and is (really) the best way to do these things.
+ * Syntax: WEBIRC password client hostname ip
+ * Where password is a shared key, client is the name of the "client" and version (e.g. cgiirc), hostname
+ * is the resolved host of the client issuing the command and IP is the real IP of the client.
+ *
+ * How it works:
+ * To tie in with the rest of cgiirc module, and to avoid race conditions, /webirc is only processed locally
+ * and simply sets metadata on the user, which is later decoded on full connect to give something meaningful.
+ */
+class CommandWebirc : public Command
+{
+ CGIHostlist Hosts;
+ bool notify;
+ public:
+ CommandWebirc(InspIRCd* Instance, CGIHostlist &cHosts, bool bnotify) : Command(Instance, "WEBIRC", 0, 4, true), Hosts(cHosts), notify(bnotify)
+ {
+ this->source = "m_cgiirc.so";
+ this->syntax = "password client hostname ip";
+ }
+ CmdResult Handle(const std::vector<std::string> ¶meters, User *user)
+ {
+ if(user->registered == REG_ALL)
+ return CMD_FAILURE;
+
+ for(CGIHostlist::iterator iter = Hosts.begin(); iter != Hosts.end(); iter++)
+ {
+ if(InspIRCd::Match(user->host, iter->hostmask, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(user->GetIPString(), iter->hostmask, ascii_case_insensitive_map))
+ {
+ if(iter->type == WEBIRC && parameters[0] == iter->password)
+ {
+ user->Extend("cgiirc_realhost", new std::string(user->host));
+ user->Extend("cgiirc_realip", new std::string(user->GetIPString()));
+ if (notify)
+ 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());
+ user->Extend("cgiirc_webirc_hostname", new std::string(parameters[2]));
+ user->Extend("cgiirc_webirc_ip", new std::string(parameters[3]));
+ return CMD_LOCALONLY;
+ }
+ }
+ }
+ return CMD_FAILURE;
+ }
+};
+
+
+/** Resolver for CGI:IRC hostnames encoded in ident/GECOS
+ */
+class CGIResolver : public Resolver
+{
+ std::string typ;
+ int theirfd;
+ User* them;
+ bool notify;
+ public:
+ CGIResolver(Module* me, InspIRCd* Instance, bool NotifyOpers, const std::string &source, bool forward, User* u, int userfd, const std::string &type, bool &cached)
+ : Resolver(Instance, source, forward ? DNS_QUERY_A : DNS_QUERY_PTR4, cached, me), typ(type), theirfd(userfd), them(u), notify(NotifyOpers) { }
+
+ virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum = 0)
+ {
+ if (resultnum)
+ return;
+
+ /* Check the user still exists */
+ if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
+ {
+ if (notify)
+ 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());
+
+ them->host.assign(result,0, 64);
+ them->dhost.assign(result, 0, 64);
+ if (querytype)
+ them->SetSockAddr(them->GetProtocolFamily(), result.c_str(), them->GetPort());
+ them->ident.assign("~cgiirc", 0, 8);
+ them->InvalidateCache();
+ them->CheckLines(true);
+ }
+ }
+
+ virtual void OnError(ResolverError e, const std::string &errormessage)
+ {
+ if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
+ {
+ if (notify)
+ 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());
+ }
+ }
+
+ virtual ~CGIResolver()
+ {
+ }
+};
+