1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
24 /* $ModDesc: Provides handling of DNS blacklists */
26 /* Class holding data for a single entry */
30 enum EnumBanaction { I_UNKNOWN, I_KILL, I_ZLINE, I_KLINE, I_GLINE, I_MARK };
31 enum EnumType { A_RECORD, A_BITMASK };
32 std::string name, ident, host, domain, reason;
33 EnumBanaction banaction;
37 unsigned char records[256];
38 unsigned long stats_hits, stats_misses;
39 DNSBLConfEntry(): type(A_BITMASK),duration(86400),bitmask(0),stats_hits(0), stats_misses(0) {}
44 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
46 class DNSBLResolver : public Resolver
49 LocalStringExt& nameExt;
50 LocalIntExt& countExt;
51 DNSBLConfEntry *ConfEntry;
55 DNSBLResolver(Module *me, LocalStringExt& match, LocalIntExt& ctr, const std::string &hostname, LocalUser* u, DNSBLConfEntry *conf, bool &cached)
56 : Resolver(hostname, DNS_QUERY_A, cached, me), theiruid(u->uuid), nameExt(match), countExt(ctr), ConfEntry(conf)
60 /* Note: This may be called multiple times for multiple A record results */
61 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
63 /* Check the user still exists */
64 LocalUser* them = (LocalUser*)ServerInstance->FindUUID(theiruid);
67 int i = countExt.get(them);
69 countExt.set(them, i - 1);
70 // Now we calculate the bitmask: 256*(256*(256*a+b)+c)+d
73 unsigned int bitmask = 0, record = 0;
77 inet_aton(result.c_str(), &resultip);
79 switch (ConfEntry->type)
81 case DNSBLConfEntry::A_BITMASK:
82 bitmask = resultip.s_addr >> 24; /* Last octet (network byte order) */
83 bitmask &= ConfEntry->bitmask;
84 match = (bitmask != 0);
86 case DNSBLConfEntry::A_RECORD:
87 record = resultip.s_addr >> 24; /* Last octet */
88 match = (ConfEntry->records[record] == 1);
94 std::string reason = ConfEntry->reason;
95 std::string::size_type x = reason.find("%ip%");
96 while (x != std::string::npos)
99 reason.insert(x, them->GetIPString());
100 x = reason.find("%ip%");
103 ConfEntry->stats_hits++;
105 switch (ConfEntry->banaction)
107 case DNSBLConfEntry::I_KILL:
109 ServerInstance->Users->QuitUser(them, std::string("Killed (") + reason + ")");
112 case DNSBLConfEntry::I_MARK:
114 if (!ConfEntry->ident.empty())
116 them->WriteServ("304 " + them->nick + " :Your ident has been set to " + ConfEntry->ident + " because you matched " + reason);
117 them->ChangeIdent(ConfEntry->ident.c_str());
120 if (!ConfEntry->host.empty())
122 them->WriteServ("304 " + them->nick + " :Your host has been set to " + ConfEntry->host + " because you matched " + reason);
123 them->ChangeDisplayedHost(ConfEntry->host.c_str());
126 nameExt.set(them, ConfEntry->name);
129 case DNSBLConfEntry::I_KLINE:
131 KLine* kl = new KLine(ServerInstance->Time(), ConfEntry->duration, ServerInstance->Config->ServerName.c_str(), reason.c_str(),
132 "*", them->GetIPString());
133 if (ServerInstance->XLines->AddLine(kl,NULL))
135 ServerInstance->SNO->WriteGlobalSno('x',"K:line added due to DNSBL match on *@%s to expire on %s: %s",
136 them->GetIPString(), ServerInstance->TimeString(kl->expiry).c_str(), reason.c_str());
137 ServerInstance->XLines->ApplyLines();
143 case DNSBLConfEntry::I_GLINE:
145 GLine* gl = new GLine(ServerInstance->Time(), ConfEntry->duration, ServerInstance->Config->ServerName.c_str(), reason.c_str(),
146 "*", them->GetIPString());
147 if (ServerInstance->XLines->AddLine(gl,NULL))
149 ServerInstance->SNO->WriteGlobalSno('x',"G:line added due to DNSBL match on *@%s to expire on %s: %s",
150 them->GetIPString(), ServerInstance->TimeString(gl->expiry).c_str(), reason.c_str());
151 ServerInstance->XLines->ApplyLines();
157 case DNSBLConfEntry::I_ZLINE:
159 ZLine* zl = new ZLine(ServerInstance->Time(), ConfEntry->duration, ServerInstance->Config->ServerName.c_str(), reason.c_str(),
160 them->GetIPString());
161 if (ServerInstance->XLines->AddLine(zl,NULL))
163 ServerInstance->SNO->WriteGlobalSno('x',"Z:line added due to DNSBL match on *@%s to expire on %s: %s",
164 them->GetIPString(), ServerInstance->TimeString(zl->expiry).c_str(), reason.c_str());
165 ServerInstance->XLines->ApplyLines();
171 case DNSBLConfEntry::I_UNKNOWN:
178 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s detected as being on a DNS blacklist (%s) with result %d", them->GetFullRealHost().c_str(), ConfEntry->domain.c_str(), (ConfEntry->type==DNSBLConfEntry::A_BITMASK) ? bitmask : record);
181 ConfEntry->stats_misses++;
184 ConfEntry->stats_misses++;
188 virtual void OnError(ResolverError e, const std::string &errormessage)
190 LocalUser* them = (LocalUser*)ServerInstance->FindUUID(theiruid);
193 int i = countExt.get(them);
195 countExt.set(them, i - 1);
199 virtual ~DNSBLResolver()
204 class ModuleDNSBL : public Module
206 std::vector<DNSBLConfEntry *> DNSBLConfEntries;
207 LocalStringExt nameExt;
208 LocalIntExt countExt;
211 * Convert a string to EnumBanaction
213 DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
215 if(action.compare("KILL")==0)
216 return DNSBLConfEntry::I_KILL;
217 if(action.compare("KLINE")==0)
218 return DNSBLConfEntry::I_KLINE;
219 if(action.compare("ZLINE")==0)
220 return DNSBLConfEntry::I_ZLINE;
221 if(action.compare("GLINE")==0)
222 return DNSBLConfEntry::I_GLINE;
223 if(action.compare("MARK")==0)
224 return DNSBLConfEntry::I_MARK;
226 return DNSBLConfEntry::I_UNKNOWN;
229 ModuleDNSBL() : nameExt("dnsbl_match", this), countExt("dnsbl_pending", this) { }
234 ServerInstance->Modules->AddService(nameExt);
235 ServerInstance->Modules->AddService(countExt);
236 Implementation eventlist[] = { I_OnRehash, I_OnUserInit, I_OnStats, I_OnSetConnectClass, I_OnCheckReady };
237 ServerInstance->Modules->Attach(eventlist, this, 5);
240 virtual ~ModuleDNSBL()
247 return Version("Provides handling of DNS blacklists", VF_VENDOR);
250 /** Clear entries and free the mem it was using
254 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
256 DNSBLConfEntries.clear();
259 /** Fill our conf vector with data
265 ConfigTagList dnsbls = ServerInstance->Config->ConfTags("dnsbl");
266 for(ConfigIter i = dnsbls.first; i != dnsbls.second; ++i)
268 ConfigTag* tag = i->second;
269 DNSBLConfEntry *e = new DNSBLConfEntry();
271 e->name = tag->getString("name");
272 e->ident = tag->getString("ident");
273 e->host = tag->getString("host");
274 e->reason = tag->getString("reason");
275 e->domain = tag->getString("domain");
277 if (tag->getString("type") == "bitmask")
279 e->type = DNSBLConfEntry::A_BITMASK;
280 e->bitmask = tag->getInt("bitmask");
284 memset(e->records, 0, sizeof(e->records));
285 e->type = DNSBLConfEntry::A_RECORD;
286 irc::portparser portrange(tag->getString("records"), false);
288 while ((item = portrange.GetToken()))
289 e->records[item] = 1;
292 e->banaction = str2banaction(tag->getString("action"));
293 e->duration = ServerInstance->Duration(tag->getString("duration", "60"));
295 /* Use portparser for record replies */
297 /* yeah, logic here is a little messy */
298 if ((e->bitmask <= 0) && (DNSBLConfEntry::A_BITMASK == e->type))
300 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(%s): invalid bitmask",tag->getTagLocation().c_str());
302 else if (e->name.empty())
304 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(%s): Invalid name",tag->getTagLocation().c_str());
306 else if (e->domain.empty())
308 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(%s): Invalid domain",tag->getTagLocation().c_str());
310 else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
312 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(%s): Invalid banaction",tag->getTagLocation().c_str());
314 else if (e->duration <= 0)
316 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(%s): Invalid duration",tag->getTagLocation().c_str());
320 if (e->reason.empty())
322 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(%s): empty reason, using defaults",tag->getTagLocation().c_str());
323 e->reason = "Your IP has been blacklisted.";
326 /* add it, all is ok */
327 DNSBLConfEntries.push_back(e);
331 /* delete and drop it, error somewhere */
336 void OnRehash(User* user)
341 void OnUserInit(LocalUser* user)
343 /* following code taken from bopm, reverses an IP address. */
345 unsigned char a, b, c, d;
346 char reversedipbuf[128];
347 std::string reversedip;
350 success = inet_aton(user->GetIPString(), &in);
355 d = (unsigned char) (in.s_addr >> 24) & 0xFF;
356 c = (unsigned char) (in.s_addr >> 16) & 0xFF;
357 b = (unsigned char) (in.s_addr >> 8) & 0xFF;
358 a = (unsigned char) in.s_addr & 0xFF;
360 snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
361 reversedip = std::string(reversedipbuf);
363 // For each DNSBL, we will run through this lookup
365 while (i < DNSBLConfEntries.size())
367 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
368 std::string hostname = reversedip + "." + DNSBLConfEntries[i]->domain;
370 /* now we'd need to fire off lookups for `hostname'. */
372 DNSBLResolver *r = new DNSBLResolver(this, nameExt, countExt, hostname, user, DNSBLConfEntries[i], cached);
373 ServerInstance->AddResolver(r, cached);
376 countExt.set(user, i);
379 ModResult OnSetConnectClass(LocalUser* user, ConnectClass* myclass)
382 if (!myclass->config->readString("dnsbl", dnsbl))
383 return MOD_RES_PASSTHRU;
384 std::string* match = nameExt.get(user);
385 std::string myname = match ? *match : "";
387 return MOD_RES_PASSTHRU;
391 ModResult OnCheckReady(LocalUser *user)
393 if (countExt.get(user))
395 return MOD_RES_PASSTHRU;
398 ModResult OnStats(char symbol, User* user, string_list &results)
401 return MOD_RES_PASSTHRU;
403 unsigned long total_hits = 0, total_misses = 0;
405 for (std::vector<DNSBLConfEntry*>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
407 total_hits += (*i)->stats_hits;
408 total_misses += (*i)->stats_misses;
410 results.push_back(std::string(ServerInstance->Config->ServerName.c_str()) + " 304 " + user->nick + " :DNSBLSTATS DNSbl \"" + (*i)->name + "\" had " +
411 ConvToStr((*i)->stats_hits) + " hits and " + ConvToStr((*i)->stats_misses) + " misses");
414 results.push_back(std::string(ServerInstance->Config->ServerName.c_str()) + " 304 " + user->nick + " :DNSBLSTATS Total hits: " + ConvToStr(total_hits));
415 results.push_back(std::string(ServerInstance->Config->ServerName.c_str()) + " 304 " + user->nick + " :DNSBLSTATS Total misses: " + ConvToStr(total_misses));
417 return MOD_RES_PASSTHRU;
421 MODULE_INIT(ModuleDNSBL)