1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
28 /* $ModDesc: Provides handling of DNS blacklists */
30 /* Class holding data for a single entry */
34 enum EnumBanaction { I_UNKNOWN, I_KILL, I_ZLINE, I_KLINE, I_GLINE };
35 std::string name, domain, reason;
36 EnumBanaction banaction;
39 unsigned long stats_hits, stats_misses;
40 DNSBLConfEntry(): duration(86400),bitmask(0),stats_hits(0), stats_misses(0) {}
45 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
47 class DNSBLResolver : public Resolver
51 DNSBLConfEntry *ConfEntry;
55 DNSBLResolver(Module *me, InspIRCd *ServerInstance, const std::string &hostname, userrec* u, int userfd, DNSBLConfEntry *conf, bool &cached)
56 : Resolver(ServerInstance, hostname, DNS_QUERY_A, cached, me)
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;
75 /* Convert the result to an in_addr (we can gaurantee we got ipv4)
76 * Whoever did the loop that was here before, I AM CONFISCATING
77 * YOUR CRACKPIPE. you know who you are. -- Brain
79 inet_aton(result.c_str(), &resultip);
80 bitmask = resultip.s_addr >> 24; /* Last octet (network byte order */
82 bitmask &= ConfEntry->bitmask;
86 std::string reason = ConfEntry->reason;
87 std::string::size_type x = reason.find("%ip%");
88 while (x != std::string::npos)
91 reason.insert(x, them->GetIPString());
92 x = reason.find("%ip%");
95 ConfEntry->stats_hits++;
97 switch (ConfEntry->banaction)
99 case DNSBLConfEntry::I_KILL:
101 userrec::QuitUser(ServerInstance, them, std::string("Killed (") + reason + ")");
104 case DNSBLConfEntry::I_KLINE:
106 std::string ban = std::string("*@") + them->GetIPString();
108 ServerInstance->XLines->apply_lines(APPLY_KLINES);
109 show = ServerInstance->XLines->add_kline(ConfEntry->duration, ServerInstance->Config->ServerName, reason.c_str(), ban.c_str());
110 FOREACH_MOD(I_OnAddKLine,OnAddKLine(ConfEntry->duration, NULL, reason, ban));
113 case DNSBLConfEntry::I_GLINE:
115 std::string ban = std::string("*@") + them->GetIPString();
116 show = ServerInstance->XLines->add_gline(ConfEntry->duration, ServerInstance->Config->ServerName, reason.c_str(), ban.c_str());
118 ServerInstance->XLines->apply_lines(APPLY_GLINES);
119 FOREACH_MOD(I_OnAddGLine,OnAddGLine(ConfEntry->duration, NULL, reason, ban));
122 case DNSBLConfEntry::I_ZLINE:
124 show = ServerInstance->XLines->add_zline(ConfEntry->duration, ServerInstance->Config->ServerName, reason.c_str(), them->GetIPString());
126 ServerInstance->XLines->apply_lines(APPLY_ZLINES);
127 FOREACH_MOD(I_OnAddZLine,OnAddZLine(ConfEntry->duration, NULL, reason, them->GetIPString()));
130 case DNSBLConfEntry::I_UNKNOWN:
139 ServerInstance->WriteOpers("*** Connecting user %s detected as being on a DNS blacklist (%s) with result %d", them->GetFullRealHost(), ConfEntry->name.c_str(), bitmask);
143 ConfEntry->stats_misses++;
146 ConfEntry->stats_misses++;
150 virtual void OnError(ResolverError e, const std::string &errormessage)
154 virtual ~DNSBLResolver()
159 class ModuleDNSBL : public Module
162 std::vector<DNSBLConfEntry *> DNSBLConfEntries;
165 * Convert a string to EnumBanaction
167 DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
169 if(action.compare("KILL")==0)
170 return DNSBLConfEntry::I_KILL;
171 if(action.compare("KLINE")==0)
172 return DNSBLConfEntry::I_KLINE;
173 if(action.compare("ZLINE")==0)
174 return DNSBLConfEntry::I_ZLINE;
175 if(action.compare("GLINE")==0)
176 return DNSBLConfEntry::I_GLINE;
178 return DNSBLConfEntry::I_UNKNOWN;
181 ModuleDNSBL(InspIRCd *Me) : Module(Me)
186 virtual ~ModuleDNSBL()
191 virtual Version GetVersion()
193 return Version(2, 0, 0, 1, VF_VENDOR, API_VERSION);
196 void Implements(char* List)
198 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnStats] = 1;
201 /** Clear entries and free the mem it was using
205 std::vector<DNSBLConfEntry *>::iterator i;
206 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
208 DNSBLConfEntries.clear();
211 /** Fill our conf vector with data
213 virtual void ReadConf()
215 ConfigReader *MyConf = new ConfigReader(ServerInstance);
218 for (int i=0; i< MyConf->Enumerate("dnsbl"); i++)
220 DNSBLConfEntry *e = new DNSBLConfEntry();
222 e->name = MyConf->ReadValue("dnsbl", "name", i);
223 e->reason = MyConf->ReadValue("dnsbl", "reason", i);
224 e->domain = MyConf->ReadValue("dnsbl", "domain", i);
225 e->banaction = str2banaction(MyConf->ReadValue("dnsbl", "action", i));
226 e->duration = ServerInstance->Duration(MyConf->ReadValue("dnsbl", "duration", i));
227 e->bitmask = MyConf->ReadInteger("dnsbl", "bitmask", i, false);
229 /* yeah, logic here is a little messy */
232 ServerInstance->WriteOpers("*** DNSBL(#%d): invalid bitmask",i);
234 else if (e->name.empty())
236 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid name",i);
238 else if (e->domain.empty())
240 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid domain",i);
242 else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
244 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid banaction", i);
248 if (e->reason.empty())
250 ServerInstance->WriteOpers("*** DNSBL(#%d): empty reason, using defaults",i);
251 e->reason = "Your IP has been blacklisted.";
254 /* add it, all is ok */
255 DNSBLConfEntries.push_back(e);
259 /* delete and drop it, error somewhere */
266 virtual void OnRehash(userrec* user, const std::string ¶meter)
271 virtual int OnUserRegister(userrec* user)
273 /* only do lookups on local users */
276 /* following code taken from bopm, reverses an IP address. */
278 unsigned char a, b, c, d;
279 char reversedipbuf[128];
280 std::string reversedip;
281 bool success = false;
283 if (!inet_aton(user->GetIPString(), &in))
286 /* We could have an ipv6 address here */
287 std::string x = user->GetIPString();
288 /* Is it a 4in6 address? (Compensate for this kernel kludge that people love) */
289 if (x.find("0::ffff:") == 0)
291 x.erase(x.begin(), x.begin() + 8);
292 if (inet_aton(x.c_str(), &in))
305 d = (unsigned char) (in.s_addr >> 24) & 0xFF;
306 c = (unsigned char) (in.s_addr >> 16) & 0xFF;
307 b = (unsigned char) (in.s_addr >> 8) & 0xFF;
308 a = (unsigned char) in.s_addr & 0xFF;
310 snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
311 reversedip = std::string(reversedipbuf);
313 // For each DNSBL, we will run through this lookup
314 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
316 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
317 std::string hostname = reversedip + "." + (*i)->domain;
319 /* now we'd need to fire off lookups for `hostname'. */
321 DNSBLResolver *r = new DNSBLResolver(this, ServerInstance, hostname, user, user->GetFd(), *i, cached);
322 ServerInstance->AddResolver(r, cached);
326 /* don't do anything with this hot potato */
330 virtual int OnStats(char symbol, userrec* user, string_list &results)
335 unsigned long total_hits = 0, total_misses = 0;
337 for (std::vector<DNSBLConfEntry*>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
339 total_hits += (*i)->stats_hits;
340 total_misses += (*i)->stats_misses;
342 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS DNSbl \"" + (*i)->name + "\" had " +
343 ConvToStr((*i)->stats_hits) + " hits and " + ConvToStr((*i)->stats_misses) + " misses");
346 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total hits: " + ConvToStr(total_hits));
347 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total misses: " + ConvToStr(total_misses));
353 MODULE_INIT(ModuleDNSBL);