1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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
50 DNSBLConfEntry *ConfEntry;
54 DNSBLResolver(Module *me, const std::string &hostname, LocalUser* u, int userfd, DNSBLConfEntry *conf, bool &cached)
55 : Resolver(hostname, DNS_QUERY_A, cached, me)
62 /* Note: This may be called multiple times for multiple A record results */
63 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
65 /* Check the user still exists */
66 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
68 // Now we calculate the bitmask: 256*(256*(256*a+b)+c)+d
71 unsigned int bitmask = 0, record = 0;
75 inet_aton(result.c_str(), &resultip);
77 switch (ConfEntry->type)
79 case DNSBLConfEntry::A_BITMASK:
80 bitmask = resultip.s_addr >> 24; /* Last octet (network byte order) */
81 bitmask &= ConfEntry->bitmask;
82 match = (bitmask != 0);
84 case DNSBLConfEntry::A_RECORD:
85 record = resultip.s_addr >> 24; /* Last octet */
86 match = (ConfEntry->records[record] == 1);
92 std::string reason = ConfEntry->reason;
93 std::string::size_type x = reason.find("%ip%");
94 while (x != std::string::npos)
97 reason.insert(x, them->GetIPString());
98 x = reason.find("%ip%");
101 ConfEntry->stats_hits++;
103 switch (ConfEntry->banaction)
105 case DNSBLConfEntry::I_KILL:
107 ServerInstance->Users->QuitUser(them, std::string("Killed (") + reason + ")");
110 case DNSBLConfEntry::I_MARK:
112 if (!ConfEntry->ident.empty())
114 them->WriteServ("304 " + them->nick + " :Your ident has been set to " + ConfEntry->ident + " because you matched " + reason);
115 them->ChangeIdent(ConfEntry->ident.c_str());
118 if (!ConfEntry->host.empty())
120 them->WriteServ("304 " + them->nick + " :Your host has been set to " + ConfEntry->host + " because you matched " + reason);
121 them->ChangeDisplayedHost(ConfEntry->host.c_str());
126 case DNSBLConfEntry::I_KLINE:
128 KLine* kl = new KLine(ServerInstance->Time(), ConfEntry->duration, ServerInstance->Config->ServerName.c_str(), reason.c_str(),
129 "*", them->GetIPString());
130 if (ServerInstance->XLines->AddLine(kl,NULL))
132 ServerInstance->SNO->WriteGlobalSno('x',"K:line added due to DNSBL match on *@%s to expire on %s: %s",
133 them->GetIPString(), ServerInstance->TimeString(kl->expiry).c_str(), reason.c_str());
134 ServerInstance->XLines->ApplyLines();
140 case DNSBLConfEntry::I_GLINE:
142 GLine* gl = new GLine(ServerInstance->Time(), ConfEntry->duration, ServerInstance->Config->ServerName.c_str(), reason.c_str(),
143 "*", them->GetIPString());
144 if (ServerInstance->XLines->AddLine(gl,NULL))
146 ServerInstance->SNO->WriteGlobalSno('x',"G:line added due to DNSBL match on *@%s to expire on %s: %s",
147 them->GetIPString(), ServerInstance->TimeString(gl->expiry).c_str(), reason.c_str());
148 ServerInstance->XLines->ApplyLines();
154 case DNSBLConfEntry::I_ZLINE:
156 ZLine* zl = new ZLine(ServerInstance->Time(), ConfEntry->duration, ServerInstance->Config->ServerName.c_str(), reason.c_str(),
157 them->GetIPString());
158 if (ServerInstance->XLines->AddLine(zl,NULL))
160 ServerInstance->SNO->WriteGlobalSno('x',"Z:line added due to DNSBL match on *@%s to expire on %s: %s",
161 them->GetIPString(), ServerInstance->TimeString(zl->expiry).c_str(), reason.c_str());
162 ServerInstance->XLines->ApplyLines();
168 case DNSBLConfEntry::I_UNKNOWN:
175 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);
178 ConfEntry->stats_misses++;
181 ConfEntry->stats_misses++;
185 virtual void OnError(ResolverError e, const std::string &errormessage)
189 virtual ~DNSBLResolver()
194 class ModuleDNSBL : public Module
197 std::vector<DNSBLConfEntry *> DNSBLConfEntries;
200 * Convert a string to EnumBanaction
202 DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
204 if(action.compare("KILL")==0)
205 return DNSBLConfEntry::I_KILL;
206 if(action.compare("KLINE")==0)
207 return DNSBLConfEntry::I_KLINE;
208 if(action.compare("ZLINE")==0)
209 return DNSBLConfEntry::I_ZLINE;
210 if(action.compare("GLINE")==0)
211 return DNSBLConfEntry::I_GLINE;
212 if(action.compare("MARK")==0)
213 return DNSBLConfEntry::I_MARK;
215 return DNSBLConfEntry::I_UNKNOWN;
220 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnStats };
221 ServerInstance->Modules->Attach(eventlist, this, 3);
224 virtual ~ModuleDNSBL()
229 virtual Version GetVersion()
231 return Version("Provides handling of DNS blacklists", VF_VENDOR);
235 /** Clear entries and free the mem it was using
239 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
241 DNSBLConfEntries.clear();
244 /** Fill our conf vector with data
246 virtual void ReadConf()
251 for (int i=0; i< MyConf.Enumerate("dnsbl"); i++)
253 DNSBLConfEntry *e = new DNSBLConfEntry();
255 e->name = MyConf.ReadValue("dnsbl", "name", i);
256 e->ident = MyConf.ReadValue("dnsbl", "ident", i);
257 e->host = MyConf.ReadValue("dnsbl", "host", i);
258 e->reason = MyConf.ReadValue("dnsbl", "reason", i);
259 e->domain = MyConf.ReadValue("dnsbl", "domain", i);
261 if (MyConf.ReadValue("dnsbl", "type", i) == "bitmask")
263 e->type = DNSBLConfEntry::A_BITMASK;
264 e->bitmask = MyConf.ReadInteger("dnsbl", "bitmask", i, false);
268 memset(e->records, 0, sizeof(e->records));
269 e->type = DNSBLConfEntry::A_RECORD;
270 irc::portparser portrange(MyConf.ReadValue("dnsbl", "records", i), false);
272 while ((item = portrange.GetToken()))
273 e->records[item] = 1;
276 e->banaction = str2banaction(MyConf.ReadValue("dnsbl", "action", i));
277 e->duration = ServerInstance->Duration(MyConf.ReadValue("dnsbl", "duration", "60", i));
279 /* Use portparser for record replies */
281 /* yeah, logic here is a little messy */
282 if ((e->bitmask <= 0) && (DNSBLConfEntry::A_BITMASK == e->type))
284 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): invalid bitmask",i);
286 else if (e->name.empty())
288 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): Invalid name",i);
290 else if (e->domain.empty())
292 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): Invalid domain",i);
294 else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
296 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): Invalid banaction", i);
298 else if (e->duration <= 0)
300 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): Invalid duration", i);
304 if (e->reason.empty())
306 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): empty reason, using defaults",i);
307 e->reason = "Your IP has been blacklisted.";
310 /* add it, all is ok */
311 DNSBLConfEntries.push_back(e);
315 /* delete and drop it, error somewhere */
320 virtual void OnRehash(User* user)
325 virtual ModResult OnUserRegister(LocalUser* user)
327 /* following code taken from bopm, reverses an IP address. */
329 unsigned char a, b, c, d;
330 char reversedipbuf[128];
331 std::string reversedip;
334 success = inet_aton(user->GetIPString(), &in);
337 return MOD_RES_PASSTHRU;
339 d = (unsigned char) (in.s_addr >> 24) & 0xFF;
340 c = (unsigned char) (in.s_addr >> 16) & 0xFF;
341 b = (unsigned char) (in.s_addr >> 8) & 0xFF;
342 a = (unsigned char) in.s_addr & 0xFF;
344 snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
345 reversedip = std::string(reversedipbuf);
347 // For each DNSBL, we will run through this lookup
348 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
350 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
351 std::string hostname = reversedip + "." + (*i)->domain;
353 /* now we'd need to fire off lookups for `hostname'. */
355 DNSBLResolver *r = new DNSBLResolver(this, hostname, user, user->GetFd(), *i, cached);
356 ServerInstance->AddResolver(r, cached);
359 /* don't do anything with this hot potato */
360 return MOD_RES_PASSTHRU;
363 virtual ModResult OnStats(char symbol, User* user, string_list &results)
366 return MOD_RES_PASSTHRU;
368 unsigned long total_hits = 0, total_misses = 0;
370 for (std::vector<DNSBLConfEntry*>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
372 total_hits += (*i)->stats_hits;
373 total_misses += (*i)->stats_misses;
375 results.push_back(std::string(ServerInstance->Config->ServerName.c_str()) + " 304 " + user->nick + " :DNSBLSTATS DNSbl \"" + (*i)->name + "\" had " +
376 ConvToStr((*i)->stats_hits) + " hits and " + ConvToStr((*i)->stats_misses) + " misses");
379 results.push_back(std::string(ServerInstance->Config->ServerName.c_str()) + " 304 " + user->nick + " :DNSBLSTATS Total hits: " + ConvToStr(total_hits));
380 results.push_back(std::string(ServerInstance->Config->ServerName.c_str()) + " 304 " + user->nick + " :DNSBLSTATS Total misses: " + ConvToStr(total_misses));
382 return MOD_RES_PASSTHRU;
386 MODULE_INIT(ModuleDNSBL)