]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_cgiirc.cpp
Replace hardcoded mode letters, part 3
[user/henk/code/inspircd.git] / src / modules / m_cgiirc.cpp
index e4d967667e9fea039b5fd9f6d8c648b698eb31e4..740ae0438e35b4fcf662cd2dedf4d0bcc85d2f64 100644 (file)
@@ -24,8 +24,8 @@
 
 
 #include "inspircd.h"
-
-/* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
+#include "xline.h"
+#include "modules/dns.h"
 
 enum CGItype { PASS, IDENT, PASSFIRST, IDENTFIRST, WEBIRC };
 
@@ -88,9 +88,19 @@ class CommandWebirc : public Command
                                        {
                                                realhost.set(user, user->host);
                                                realip.set(user, user->GetIPString());
+
+                                               bool host_ok = (parameters[2].length() < 64);
+                                               const std::string& newhost = (host_ok ? parameters[2] : parameters[3]);
+
                                                if (notify)
-                                                       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());
-                                               webirc_hostname.set(user, parameters[2]);
+                                                       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(), newhost.c_str(), user->host.c_str());
+
+                                               // Check if we're happy with the provided hostname. If it's problematic then make sure we won't set a host later, just the IP
+                                               if (host_ok)
+                                                       webirc_hostname.set(user, parameters[2]);
+                                               else
+                                                       webirc_hostname.unset(user);
+
                                                webirc_ip.set(user, parameters[3]);
                                                return CMD_SUCCESS;
                                        }
@@ -105,49 +115,56 @@ class CommandWebirc : public Command
 
 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
  */
-class CGIResolver : public Resolver
+class CGIResolver : public DNS::Request
 {
        std::string typ;
        std::string theiruid;
        LocalIntExt& waiting;
        bool notify;
  public:
-       CGIResolver(Module* me, bool NotifyOpers, const std::string &source, LocalUser* u,
-                       const std::string &type, bool &cached, LocalIntExt& ext)
-               : Resolver(source, DNS_QUERY_PTR4, cached, me), typ(type), theiruid(u->uuid),
+       CGIResolver(DNS::Manager *mgr, Module* me, bool NotifyOpers, const std::string &source, LocalUser* u,
+                       const std::string &ttype, LocalIntExt& ext)
+               : DNS::Request(mgr, me, source, DNS::QUERY_PTR), typ(ttype), theiruid(u->uuid),
                waiting(ext), notify(NotifyOpers)
        {
        }
 
-       virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
+       void OnLookupComplete(const DNS::Query *r) CXX11_OVERRIDE
        {
                /* Check the user still exists */
                User* them = ServerInstance->FindUUID(theiruid);
-               if (them)
+               if ((them) && (!them->quitting))
                {
-                       if (notify)
-                               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());
+                       LocalUser* lu = IS_LOCAL(them);
+                       if (!lu)
+                               return;
 
-                       if (result.length() > 64)
+                       const DNS::ResourceRecord &ans_record = r->answers[0];
+                       if (ans_record.rdata.empty() || ans_record.rdata.length() > 64)
                                return;
-                       them->host = result;
-                       them->dhost = result;
+
+                       if (notify)
+                               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(), ans_record.rdata.c_str(), typ.c_str());
+
+                       them->host = them->dhost = ans_record.rdata;
                        them->InvalidateCache();
-                       them->CheckLines(true);
+                       lu->CheckLines(true);
                }
        }
 
-       virtual void OnError(ResolverError e, const std::string &errormessage)
+       void OnError(const DNS::Query *r) CXX11_OVERRIDE
        {
+               if (!notify)
+                       return;
+
                User* them = ServerInstance->FindUUID(theiruid);
-               if (them)
+               if ((them) && (!them->quitting))
                {
-                       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());
+                       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()
+       ~CGIResolver()
        {
                User* them = ServerInstance->FindUUID(theiruid);
                if (!them)
@@ -162,26 +179,72 @@ class ModuleCgiIRC : public Module
 {
        CommandWebirc cmd;
        LocalIntExt waiting;
+       dynamic_reference<DNS::Manager> DNS;
+
+       static void RecheckClass(LocalUser* user)
+       {
+               user->MyClass = NULL;
+               user->SetClass();
+               user->CheckClass();
+       }
+
+       static void ChangeIP(LocalUser* user, const std::string& newip)
+       {
+               ServerInstance->Users->RemoveCloneCounts(user);
+               user->SetClientIP(newip.c_str());
+               ServerInstance->Users->AddLocalClone(user);
+               ServerInstance->Users->AddGlobalClone(user);
+       }
+
+       void HandleIdentOrPass(LocalUser* user, const std::string& newip, bool was_pass)
+       {
+               cmd.realhost.set(user, user->host);
+               cmd.realip.set(user, user->GetIPString());
+               ChangeIP(user, newip);
+               user->host = user->dhost = user->GetIPString();
+               user->InvalidateCache();
+               RecheckClass(user);
+
+               // Don't create the resolver if the core couldn't put the user in a connect class or when dns is disabled
+               if (user->quitting || !DNS || user->MyClass->nouserdns)
+                       return;
+
+               CGIResolver* r = new CGIResolver(*this->DNS, this, cmd.notify, newip, user, (was_pass ? "PASS" : "IDENT"), waiting);
+               try
+               {
+                       waiting.set(user, waiting.get(user) + 1);
+                       this->DNS->Process(r);
+               }
+               catch (DNS::Exception &ex)
+               {
+                       int count = waiting.get(user);
+                       if (count)
+                               waiting.set(user, count - 1);
+                       delete r;
+                       if (cmd.notify)
+                                ServerInstance->SNO->WriteToSnoMask('a', "Connecting user %s detected as using CGI:IRC (%s), but I could not resolve their hostname; %s", user->nick.c_str(), user->host.c_str(), ex.GetReason());
+               }
+       }
+
 public:
-       ModuleCgiIRC() : cmd(this), waiting("cgiirc-delay", this)
+       ModuleCgiIRC()
+               : cmd(this)
+               , waiting("cgiirc-delay", this)
+               , DNS(this, "DNS")
        {
        }
 
-       void init()
+       void init() CXX11_OVERRIDE
        {
                OnRehash(NULL);
-               ServerInstance->AddCommand(&cmd);
-               ServerInstance->Extensions.Register(&cmd.realhost);
-               ServerInstance->Extensions.Register(&cmd.realip);
-               ServerInstance->Extensions.Register(&cmd.webirc_hostname);
-               ServerInstance->Extensions.Register(&cmd.webirc_ip);
-               ServerInstance->Extensions.Register(&waiting);
+               ServiceProvider* providerlist[] = { &cmd, &cmd.realhost, &cmd.realip, &cmd.webirc_hostname, &cmd.webirc_ip, &waiting };
+               ServerInstance->Modules->AddServices(providerlist, sizeof(providerlist)/sizeof(ServiceProvider*));
 
                Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnCheckReady };
-               ServerInstance->Modules->Attach(eventlist, this, 3);
+               ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
        }
 
-       void OnRehash(User* user)
+       void OnRehash(User* user) CXX11_OVERRIDE
        {
                cmd.Hosts.clear();
 
@@ -200,7 +263,7 @@ public:
                        {
                                if (type == "webirc" && password.empty())
                                {
-                                       ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
+                                       ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "m_cgiirc: Missing password in config: %s", hostmask.c_str());
                                }
                                else
                                {
@@ -216,7 +279,7 @@ public:
                                        else
                                        {
                                                cgitype = PASS;
-                                               ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:type> value in config: %s, setting it to \"pass\"", type.c_str());
+                                               ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "m_cgiirc.so: Invalid <cgihost:type> value in config: %s, setting it to \"pass\"", type.c_str());
                                        }
 
                                        cmd.Hosts.push_back(CGIhost(hostmask, cgitype, password));
@@ -224,13 +287,13 @@ public:
                        }
                        else
                        {
-                               ServerInstance->Logs->Log("CONFIG",DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
+                               ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
                                continue;
                        }
                }
        }
 
-       ModResult OnCheckReady(LocalUser *user)
+       ModResult OnCheckReady(LocalUser *user) CXX11_OVERRIDE
        {
                if (waiting.get(user))
                        return MOD_RES_DENY;
@@ -239,31 +302,26 @@ public:
                if (!webirc_ip)
                        return MOD_RES_PASSTHRU;
 
-               ServerInstance->Users->RemoveCloneCounts(user);
-               user->SetClientIP(webirc_ip->c_str());
-               cmd.webirc_ip.unset(user);
+               ChangeIP(user, *webirc_ip);
 
                std::string* webirc_hostname = cmd.webirc_hostname.get(user);
-               if (webirc_hostname && webirc_hostname->length() < 64)
-                       user->host = user->dhost = *webirc_hostname;
-               else
-                       user->host = user->dhost = user->GetIPString();
+               user->host = user->dhost = (webirc_hostname ? *webirc_hostname : user->GetIPString());
 
-               user->InvalidateCache();
-               cmd.webirc_hostname.unset(user);
+               RecheckClass(user);
+               if (user->quitting)
+                       return MOD_RES_DENY;
 
-               ServerInstance->Users->AddLocalClone(user);
-               ServerInstance->Users->AddGlobalClone(user);
-               user->SetClass();
-               user->CheckClass();
                user->CheckLines(true);
                if (user->quitting)
                        return MOD_RES_DENY;
 
+               cmd.webirc_hostname.unset(user);
+               cmd.webirc_ip.unset(user);
+
                return MOD_RES_PASSTHRU;
        }
 
-       ModResult OnUserRegister(LocalUser* user)
+       ModResult OnUserRegister(LocalUser* user) CXX11_OVERRIDE
        {
                for(CGIHostlist::iterator iter = cmd.Hosts.begin(); iter != cmd.Hosts.end(); iter++)
                {
@@ -306,32 +364,7 @@ public:
        {
                if(IsValidHost(user->password))
                {
-                       cmd.realhost.set(user, user->host);
-                       cmd.realip.set(user, user->GetIPString());
-                       user->host = user->password;
-                       user->dhost = user->password;
-                       user->InvalidateCache();
-
-                       ServerInstance->Users->RemoveCloneCounts(user);
-                       user->SetClientIP(user->password.c_str());
-                       ServerInstance->Users->AddLocalClone(user);
-                       ServerInstance->Users->AddGlobalClone(user);
-                       user->SetClass();
-                       user->CheckClass();
-
-                       try
-                       {
-                               bool cached;
-                               CGIResolver* r = new CGIResolver(this, cmd.notify, user->password, user, "PASS", cached, waiting);
-                               ServerInstance->AddResolver(r, cached);
-                               waiting.set(user, waiting.get(user) + 1);
-                       }
-                       catch (...)
-                       {
-                               if (cmd.notify)
-                                       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());
-                       }
-
+                       HandleIdentOrPass(user, user->password, true);
                        user->password.clear();
                        return true;
                }
@@ -342,12 +375,11 @@ public:
        bool CheckIdent(LocalUser* user)
        {
                const char* ident;
-               int len = user->ident.length();
                in_addr newip;
 
-               if(len == 8)
+               if (user->ident.length() == 8)
                        ident = user->ident.c_str();
-               else if(len == 9 && user->ident[0] == '~')
+               else if (user->ident.length() == 9 && user->ident[0] == '~')
                        ident = user->ident.c_str() + 1;
                else
                        return false;
@@ -357,33 +389,10 @@ public:
                if (errno)
                        return false;
                newip.s_addr = htonl(ipaddr);
-               char* newipstr = inet_ntoa(newip);
-
-               cmd.realhost.set(user, user->host);
-               cmd.realip.set(user, user->GetIPString());
-               ServerInstance->Users->RemoveCloneCounts(user);
-               user->SetClientIP(newipstr);
-               ServerInstance->Users->AddLocalClone(user);
-               ServerInstance->Users->AddGlobalClone(user);
-               user->SetClass();
-               user->CheckClass();
-               user->host = newipstr;
-               user->dhost = newipstr;
-               user->ident.assign("~cgiirc", 0, 8);
-               try
-               {
-                       bool cached;
-                       CGIResolver* r = new CGIResolver(this, cmd.notify, newipstr, user, "IDENT", cached, waiting);
-                       ServerInstance->AddResolver(r, cached);
-                       waiting.set(user, waiting.get(user) + 1);
-               }
-               catch (...)
-               {
-                       user->InvalidateCache();
+               std::string newipstr(inet_ntoa(newip));
 
-                       if(cmd.notify)
-                                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());
-               }
+               user->ident = "~cgiirc";
+               HandleIdentOrPass(user, newipstr, false);
 
                return true;
        }
@@ -409,7 +418,7 @@ public:
                return true;
        }
 
-       virtual Version GetVersion()
+       Version GetVersion() CXX11_OVERRIDE
        {
                return Version("Change user's hosts connecting from known CGI:IRC hosts",VF_VENDOR);
        }