]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_dnsbl.cpp
Update wiki links to use HTTPS and point to the correct pages.
[user/henk/code/inspircd.git] / src / modules / m_dnsbl.cpp
index 3a334ea371a36dedb1cd2e17aaea1150e237aad4..3dea080cee59dd99474510ae5fe5567f4eda3b44 100644 (file)
@@ -27,7 +27,7 @@
 /* $ModDesc: Provides handling of DNS blacklists */
 
 /* Class holding data for a single entry */
-class DNSBLConfEntry
+class DNSBLConfEntry : public refcountbase
 {
        public:
                enum EnumBanaction { I_UNKNOWN, I_KILL, I_ZLINE, I_KLINE, I_GLINE, I_MARK };
@@ -51,11 +51,11 @@ class DNSBLResolver : public Resolver
        std::string theiruid;
        LocalStringExt& nameExt;
        LocalIntExt& countExt;
-       DNSBLConfEntry *ConfEntry;
+       reference<DNSBLConfEntry> ConfEntry;
 
  public:
 
-       DNSBLResolver(Module *me, LocalStringExt& match, LocalIntExt& ctr, const std::string &hostname, LocalUser* u, DNSBLConfEntry *conf, bool &cached)
+       DNSBLResolver(Module *me, LocalStringExt& match, LocalIntExt& ctr, const std::string &hostname, LocalUser* u, reference<DNSBLConfEntry> conf, bool &cached)
                : Resolver(hostname, DNS_QUERY_A, cached, me), theiruid(u->uuid), nameExt(match), countExt(ctr), ConfEntry(conf)
        {
        }
@@ -70,8 +70,8 @@ class DNSBLResolver : public Resolver
                        int i = countExt.get(them);
                        if (i)
                                countExt.set(them, i - 1);
-                       // Now we calculate the bitmask: 256*(256*(256*a+b)+c)+d
-                       if(result.length())
+                       // All replies should be in 127.0.0.0/8
+                       if (result.compare(0, 4, "127.") == 0)
                        {
                                unsigned int bitmask = 0, record = 0;
                                bool match = false;
@@ -82,6 +82,7 @@ class DNSBLResolver : public Resolver
                                switch (ConfEntry->type)
                                {
                                        case DNSBLConfEntry::A_BITMASK:
+                                               // Now we calculate the bitmask: 256*(256*(256*a+b)+c)+d
                                                bitmask = resultip.s_addr >> 24; /* Last octet (network byte order) */
                                                bitmask &= ConfEntry->bitmask;
                                                match = (bitmask != 0);
@@ -141,7 +142,10 @@ class DNSBLResolver : public Resolver
                                                                ServerInstance->XLines->ApplyLines();
                                                        }
                                                        else
+                                                       {
                                                                delete kl;
+                                                               return;
+                                                       }
                                                        break;
                                                }
                                                case DNSBLConfEntry::I_GLINE:
@@ -156,7 +160,10 @@ class DNSBLResolver : public Resolver
                                                                ServerInstance->XLines->ApplyLines();
                                                        }
                                                        else
+                                                       {
                                                                delete gl;
+                                                               return;
+                                                       }
                                                        break;
                                                }
                                                case DNSBLConfEntry::I_ZLINE:
@@ -171,7 +178,10 @@ class DNSBLResolver : public Resolver
                                                                ServerInstance->XLines->ApplyLines();
                                                        }
                                                        else
+                                                       {
                                                                delete zl;
+                                                               return;
+                                                       }
                                                        break;
                                                }
                                                case DNSBLConfEntry::I_UNKNOWN:
@@ -187,7 +197,11 @@ class DNSBLResolver : public Resolver
                                        ConfEntry->stats_misses++;
                        }
                        else
+                       {
+                               if (!result.empty())
+                                       ServerInstance->SNO->WriteGlobalSno('a', "DNSBL: %s returned address outside of acceptable subnet 127.0.0.0/8: %s", ConfEntry->domain.c_str(), result.c_str());
                                ConfEntry->stats_misses++;
+                       }
                }
        }
 
@@ -209,7 +223,7 @@ class DNSBLResolver : public Resolver
 
 class ModuleDNSBL : public Module
 {
-       std::vector<DNSBLConfEntry *> DNSBLConfEntries;
+       std::vector<reference<DNSBLConfEntry> > DNSBLConfEntries;
        LocalStringExt nameExt;
        LocalIntExt countExt;
 
@@ -240,12 +254,7 @@ class ModuleDNSBL : public Module
                ServerInstance->Modules->AddService(nameExt);
                ServerInstance->Modules->AddService(countExt);
                Implementation eventlist[] = { I_OnRehash, I_OnSetUserIP, I_OnStats, I_OnSetConnectClass, I_OnCheckReady };
-               ServerInstance->Modules->Attach(eventlist, this, 5);
-       }
-
-       virtual ~ModuleDNSBL()
-       {
-               ClearEntries();
+               ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
        }
 
        Version GetVersion()
@@ -253,26 +262,17 @@ class ModuleDNSBL : public Module
                return Version("Provides handling of DNS blacklists", VF_VENDOR);
        }
 
-       /** Clear entries and free the mem it was using
-        */
-       void ClearEntries()
-       {
-               for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
-                       delete *i;
-               DNSBLConfEntries.clear();
-       }
-
        /** Fill our conf vector with data
         */
        void ReadConf()
        {
-               ClearEntries();
+               DNSBLConfEntries.clear();
 
                ConfigTagList dnsbls = ServerInstance->Config->ConfTags("dnsbl");
                for(ConfigIter i = dnsbls.first; i != dnsbls.second; ++i)
                {
                        ConfigTag* tag = i->second;
-                       DNSBLConfEntry *e = new DNSBLConfEntry();
+                       reference<DNSBLConfEntry> e = new DNSBLConfEntry();
 
                        e->name = tag->getString("name");
                        e->ident = tag->getString("ident");
@@ -337,11 +337,7 @@ class ModuleDNSBL : public Module
 
                                /* add it, all is ok */
                                DNSBLConfEntries.push_back(e);
-                               continue;
                        }
-
-                       /* delete and drop it, error somewhere */
-                       delete e;
                }
        }
 
@@ -375,6 +371,8 @@ class ModuleDNSBL : public Module
                snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
                reversedip = std::string(reversedipbuf);
 
+               countExt.set(user, DNSBLConfEntries.size());
+
                // For each DNSBL, we will run through this lookup
                unsigned int i = 0;
                while (i < DNSBLConfEntries.size())
@@ -386,9 +384,10 @@ class ModuleDNSBL : public Module
                        bool cached;
                        DNSBLResolver *r = new DNSBLResolver(this, nameExt, countExt, hostname, user, DNSBLConfEntries[i], cached);
                        ServerInstance->AddResolver(r, cached);
+                       if (user->quitting)
+                               break;
                        i++;
                }
-               countExt.set(user, i);
        }
 
        ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
@@ -417,7 +416,7 @@ class ModuleDNSBL : public Module
 
                unsigned long total_hits = 0, total_misses = 0;
 
-               for (std::vector<DNSBLConfEntry*>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
+               for (std::vector<reference<DNSBLConfEntry> >::const_iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); ++i)
                {
                        total_hits += (*i)->stats_hits;
                        total_misses += (*i)->stats_misses;