]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_cgiirc.cpp
core_hostname_lookup: find answer record of the correct type instead of assuming...
[user/henk/code/inspircd.git] / src / modules / m_cgiirc.cpp
index d2f24f9109999dd4010bcb986a37dff23e1b9cf9..721d6ba081a693b0237d326b7aab742cd7416e0e 100644 (file)
 #include "xline.h"
 #include "modules/dns.h"
 
-/* $ModDesc: Change user's hosts connecting from known CGI:IRC hosts */
-
 enum CGItype { PASS, IDENT, PASSFIRST, IDENTFIRST, WEBIRC };
 
+// We need this method up here so that it can be accessed from anywhere
+static void ChangeIP(User* user, const std::string& newip)
+{
+       ServerInstance->Users->RemoveCloneCounts(user);
+       user->SetClientIP(newip.c_str());
+       ServerInstance->Users->AddClone(user);
+}
 
 /** Holds a CGI site's details
  */
@@ -65,14 +70,12 @@ class CommandWebirc : public Command
        bool notify;
        StringExtItem realhost;
        StringExtItem realip;
-       LocalStringExt webirc_hostname;
-       LocalStringExt webirc_ip;
 
        CGIHostlist Hosts;
        CommandWebirc(Module* Creator)
                : Command(Creator, "WEBIRC", 4),
-                 realhost("cgiirc_realhost", Creator), realip("cgiirc_realip", Creator),
-                 webirc_hostname("cgiirc_webirc_hostname", Creator), webirc_ip("cgiirc_webirc_ip", Creator)
+                 realhost("cgiirc_realhost", ExtensionItem::EXT_USER, Creator)
+                 , realip("cgiirc_realip", ExtensionItem::EXT_USER, Creator)
                {
                        works_before_reg = true;
                        this->syntax = "password client hostname ip";
@@ -91,25 +94,25 @@ class CommandWebirc : public Command
                                                realhost.set(user, user->host);
                                                realip.set(user, user->GetIPString());
 
-                                               bool host_ok = (parameters[2].length() < 64);
+                                               // 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
+                                               bool host_ok = (parameters[2].length() <= ServerInstance->Config->Limits.MaxHost);
                                                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(), newhost.c_str(), user->host.c_str());
+                                                       ServerInstance->SNO->WriteGlobalSno('w', "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);
+                                               // Where the magic happens - change their IP
+                                               ChangeIP(user, parameters[3]);
+                                               // And follow this up by changing their host
+                                               user->host = user->dhost = newhost;
+                                               user->InvalidateCache();
 
-                                               webirc_ip.set(user, parameters[3]);
                                                return CMD_SUCCESS;
                                        }
                                }
                        }
 
-                       ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s tried to use WEBIRC, but didn't match any configured webirc blocks.", user->GetFullRealHost().c_str());
+                       ServerInstance->SNO->WriteGlobalSno('w', "Connecting user %s tried to use WEBIRC, but didn't match any configured webirc blocks.", user->GetFullRealHost().c_str());
                        return CMD_FAILURE;
                }
 };
@@ -142,11 +145,11 @@ class CGIResolver : public DNS::Request
                                return;
 
                        const DNS::ResourceRecord &ans_record = r->answers[0];
-                       if (ans_record.rdata.empty() || ans_record.rdata.length() > 64)
+                       if (ans_record.rdata.empty() || ans_record.rdata.length() > ServerInstance->Config->Limits.MaxHost)
                                return;
 
                        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());
+                               ServerInstance->SNO->WriteGlobalSno('w', "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();
@@ -162,7 +165,7 @@ class CGIResolver : public DNS::Request
                User* them = ServerInstance->FindUUID(theiruid);
                if ((them) && (!them->quitting))
                {
-                       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('w', "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());
                }
        }
 
@@ -190,14 +193,6 @@ class ModuleCgiIRC : public Module
                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);
@@ -208,7 +203,7 @@ class ModuleCgiIRC : public Module
                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)
+               if (user->quitting || !DNS || !user->MyClass->resolvehostnames)
                        return;
 
                CGIResolver* r = new CGIResolver(*this->DNS, this, cmd.notify, newip, user, (was_pass ? "PASS" : "IDENT"), waiting);
@@ -224,29 +219,24 @@ class ModuleCgiIRC : public Module
                                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());
+                                ServerInstance->SNO->WriteToSnoMask('w', "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().c_str());
                }
        }
 
 public:
        ModuleCgiIRC()
                : cmd(this)
-               , waiting("cgiirc-delay", this)
+               , waiting("cgiirc-delay", ExtensionItem::EXT_USER, this)
                , DNS(this, "DNS")
        {
        }
 
        void init() CXX11_OVERRIDE
        {
-               OnRehash(NULL);
-               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, sizeof(eventlist)/sizeof(Implementation));
+               ServerInstance->SNO->EnableSnomask('w', "CGIIRC");
        }
 
-       void OnRehash(User* user) CXX11_OVERRIDE
+       void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
        {
                cmd.Hosts.clear();
 
@@ -281,7 +271,7 @@ public:
                                        else
                                        {
                                                cgitype = PASS;
-                                               ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "m_cgiirc.so: Invalid <cgihost:type> value in config: %s, setting it to \"pass\"", type.c_str());
+                                               ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Invalid <cgihost:type> value in config: %s, setting it to \"pass\"", type.c_str());
                                        }
 
                                        cmd.Hosts.push_back(CGIhost(hostmask, cgitype, password));
@@ -289,7 +279,7 @@ public:
                        }
                        else
                        {
-                               ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "m_cgiirc.so: Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
+                               ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Invalid <cgihost:mask> value in config: %s", hostmask.c_str());
                                continue;
                        }
                }
@@ -300,15 +290,9 @@ public:
                if (waiting.get(user))
                        return MOD_RES_DENY;
 
-               std::string *webirc_ip = cmd.webirc_ip.get(user);
-               if (!webirc_ip)
+               if (!cmd.realip.get(user))
                        return MOD_RES_PASSTHRU;
 
-               ChangeIP(user, *webirc_ip);
-
-               std::string* webirc_hostname = cmd.webirc_hostname.get(user);
-               user->host = user->dhost = (webirc_hostname ? *webirc_hostname : user->GetIPString());
-
                RecheckClass(user);
                if (user->quitting)
                        return MOD_RES_DENY;
@@ -317,9 +301,6 @@ public:
                if (user->quitting)
                        return MOD_RES_DENY;
 
-               cmd.webirc_hostname.unset(user);
-               cmd.webirc_ip.unset(user);
-
                return MOD_RES_PASSTHRU;
        }
 
@@ -401,7 +382,7 @@ public:
 
        bool IsValidHost(const std::string &host)
        {
-               if(!host.size() || host.size() > 64)
+               if(!host.size() || host.size() > ServerInstance->Config->Limits.MaxHost)
                        return false;
 
                for(unsigned int i = 0; i < host.size(); i++)
@@ -424,7 +405,6 @@ public:
        {
                return Version("Change user's hosts connecting from known CGI:IRC hosts",VF_VENDOR);
        }
-
 };
 
 MODULE_INIT(ModuleCgiIRC)