diff options
author | danieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7> | 2010-02-14 00:31:44 +0000 |
---|---|---|
committer | danieldg <danieldg@e03df62e-2008-0410-955e-edbf42e46eb7> | 2010-02-14 00:31:44 +0000 |
commit | 4498f1abd163b140efcbbd9e75173665c9b1c29f (patch) | |
tree | fe2c6b8f1abe72ac96e49a5ba2c1fb72bc5de30d /src/modules/m_dnsbl.cpp | |
parent | 2552786a2fbed628e7d51a6b8e177981b1ff8d40 (diff) |
m_dnsbl updates
Prevent user registration until dnsbl lookups are complete
Allow DNSBLs to be checked using <connect:dnsbl> (MARK type)
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@12454 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'src/modules/m_dnsbl.cpp')
-rw-r--r-- | src/modules/m_dnsbl.cpp | 111 |
1 files changed, 73 insertions, 38 deletions
diff --git a/src/modules/m_dnsbl.cpp b/src/modules/m_dnsbl.cpp index 818450b79..eb3ff976d 100644 --- a/src/modules/m_dnsbl.cpp +++ b/src/modules/m_dnsbl.cpp @@ -46,24 +46,27 @@ class DNSBLConfEntry class DNSBLResolver : public Resolver { std::string theiruid; + LocalStringExt& nameExt; + LocalIntExt& countExt; DNSBLConfEntry *ConfEntry; public: - DNSBLResolver(Module *me, const std::string &hostname, LocalUser* u, DNSBLConfEntry *conf, bool &cached) - : Resolver(hostname, DNS_QUERY_A, cached, me) + DNSBLResolver(Module *me, LocalStringExt& match, LocalIntExt& ctr, const std::string &hostname, LocalUser* u, DNSBLConfEntry *conf, bool &cached) + : Resolver(hostname, DNS_QUERY_A, cached, me), theiruid(u->uuid), nameExt(match), countExt(ctr), ConfEntry(conf) { - theiruid = u->uuid; - ConfEntry = conf; } /* Note: This may be called multiple times for multiple A record results */ virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached) { /* Check the user still exists */ - User* them = ServerInstance->FindUUID(theiruid); + LocalUser* them = (LocalUser*)ServerInstance->FindUUID(theiruid); if (them) { + 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()) { @@ -120,6 +123,7 @@ class DNSBLResolver : public Resolver them->ChangeDisplayedHost(ConfEntry->host.c_str()); } + nameExt.set(them, ConfEntry->name); break; } case DNSBLConfEntry::I_KLINE: @@ -183,6 +187,13 @@ class DNSBLResolver : public Resolver virtual void OnError(ResolverError e, const std::string &errormessage) { + LocalUser* them = (LocalUser*)ServerInstance->FindUUID(theiruid); + if (them) + { + int i = countExt.get(them); + if (i) + countExt.set(them, i - 1); + } } virtual ~DNSBLResolver() @@ -192,8 +203,9 @@ class DNSBLResolver : public Resolver class ModuleDNSBL : public Module { - private: std::vector<DNSBLConfEntry *> DNSBLConfEntries; + LocalStringExt nameExt; + LocalIntExt countExt; /* * Convert a string to EnumBanaction @@ -214,10 +226,15 @@ class ModuleDNSBL : public Module return DNSBLConfEntry::I_UNKNOWN; } public: - ModuleDNSBL() { + ModuleDNSBL() : nameExt("dnsbl_match", this), countExt("dnsbl_pending", this) { } + + void init() + { ReadConf(); - Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnStats }; - ServerInstance->Modules->Attach(eventlist, this, 3); + ServerInstance->Modules->AddService(nameExt); + ServerInstance->Modules->AddService(countExt); + Implementation eventlist[] = { I_OnRehash, I_OnUserInit, I_OnStats, I_OnSetConnectClass, I_OnCheckReady }; + ServerInstance->Modules->Attach(eventlist, this, 5); } virtual ~ModuleDNSBL() @@ -225,12 +242,11 @@ class ModuleDNSBL : public Module ClearEntries(); } - virtual Version GetVersion() + Version GetVersion() { return Version("Provides handling of DNS blacklists", VF_VENDOR); } - /** Clear entries and free the mem it was using */ void ClearEntries() @@ -242,67 +258,68 @@ class ModuleDNSBL : public Module /** Fill our conf vector with data */ - virtual void ReadConf() + void ReadConf() { - ConfigReader MyConf; ClearEntries(); - for (int i=0; i< MyConf.Enumerate("dnsbl"); i++) + ConfigTagList dnsbls = ServerInstance->Config->ConfTags("dnsbl"); + for(ConfigIter i = dnsbls.first; i != dnsbls.second; ++i) { + ConfigTag* tag = i->second; DNSBLConfEntry *e = new DNSBLConfEntry(); - e->name = MyConf.ReadValue("dnsbl", "name", i); - e->ident = MyConf.ReadValue("dnsbl", "ident", i); - e->host = MyConf.ReadValue("dnsbl", "host", i); - e->reason = MyConf.ReadValue("dnsbl", "reason", i); - e->domain = MyConf.ReadValue("dnsbl", "domain", i); + e->name = tag->getString("name"); + e->ident = tag->getString("ident"); + e->host = tag->getString("host"); + e->reason = tag->getString("reason"); + e->domain = tag->getString("domain"); - if (MyConf.ReadValue("dnsbl", "type", i) == "bitmask") + if (tag->getString("type") == "bitmask") { e->type = DNSBLConfEntry::A_BITMASK; - e->bitmask = MyConf.ReadInteger("dnsbl", "bitmask", i, false); + e->bitmask = tag->getInt("bitmask"); } else { memset(e->records, 0, sizeof(e->records)); e->type = DNSBLConfEntry::A_RECORD; - irc::portparser portrange(MyConf.ReadValue("dnsbl", "records", i), false); + irc::portparser portrange(tag->getString("records"), false); long item = -1; while ((item = portrange.GetToken())) e->records[item] = 1; } - e->banaction = str2banaction(MyConf.ReadValue("dnsbl", "action", i)); - e->duration = ServerInstance->Duration(MyConf.ReadValue("dnsbl", "duration", "60", i)); + e->banaction = str2banaction(tag->getString("action")); + e->duration = ServerInstance->Duration(tag->getString("duration", "60")); /* Use portparser for record replies */ /* yeah, logic here is a little messy */ if ((e->bitmask <= 0) && (DNSBLConfEntry::A_BITMASK == e->type)) { - ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): invalid bitmask",i); + ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(%s): invalid bitmask",tag->getTagLocation().c_str()); } else if (e->name.empty()) { - ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): Invalid name",i); + ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(%s): Invalid name",tag->getTagLocation().c_str()); } else if (e->domain.empty()) { - ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): Invalid domain",i); + ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(%s): Invalid domain",tag->getTagLocation().c_str()); } else if (e->banaction == DNSBLConfEntry::I_UNKNOWN) { - ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): Invalid banaction", i); + ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(%s): Invalid banaction",tag->getTagLocation().c_str()); } else if (e->duration <= 0) { - ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): Invalid duration", i); + ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(%s): Invalid duration",tag->getTagLocation().c_str()); } else { if (e->reason.empty()) { - ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): empty reason, using defaults",i); + ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(%s): empty reason, using defaults",tag->getTagLocation().c_str()); e->reason = "Your IP has been blacklisted."; } @@ -316,12 +333,12 @@ class ModuleDNSBL : public Module } } - virtual void OnRehash(User* user) + void OnRehash(User* user) { ReadConf(); } - virtual ModResult OnUserRegister(LocalUser* user) + void OnUserInit(LocalUser* user) { /* following code taken from bopm, reverses an IP address. */ struct in_addr in; @@ -333,7 +350,7 @@ class ModuleDNSBL : public Module success = inet_aton(user->GetIPString(), &in); if (!success) - return MOD_RES_PASSTHRU; + return; d = (unsigned char) (in.s_addr >> 24) & 0xFF; c = (unsigned char) (in.s_addr >> 16) & 0xFF; @@ -344,22 +361,40 @@ class ModuleDNSBL : public Module reversedip = std::string(reversedipbuf); // For each DNSBL, we will run through this lookup - for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++) + unsigned int i = 0; + while (i < DNSBLConfEntries.size()) { // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld) - std::string hostname = reversedip + "." + (*i)->domain; + std::string hostname = reversedip + "." + DNSBLConfEntries[i]->domain; /* now we'd need to fire off lookups for `hostname'. */ bool cached; - DNSBLResolver *r = new DNSBLResolver(this, hostname, user, *i, cached); + DNSBLResolver *r = new DNSBLResolver(this, nameExt, countExt, hostname, user, DNSBLConfEntries[i], cached); ServerInstance->AddResolver(r, cached); } + countExt.set(user, i); + } - /* don't do anything with this hot potato */ + ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass) + { + std::string dnsbl; + if (!myclass->config->readString("dnsbl", dnsbl)) + return MOD_RES_PASSTHRU; + std::string* match = nameExt.get(user); + std::string myname = match ? *match : ""; + if (dnsbl == myname) + return MOD_RES_PASSTHRU; + return MOD_RES_DENY; + } + + ModResult OnCheckReady(LocalUser *user) + { + if (countExt.get(user)) + return MOD_RES_DENY; return MOD_RES_PASSTHRU; } - virtual ModResult OnStats(char symbol, User* user, string_list &results) + ModResult OnStats(char symbol, User* user, string_list &results) { if (symbol != 'd') return MOD_RES_PASSTHRU; |