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>
29 /* $ModDesc: Provides handling of DNS blacklists */
31 /* Class holding data for a single entry */
35 enum EnumBanaction { I_UNKNOWN, I_KILL, I_ZLINE, I_KLINE, I_GLINE };
36 std::string name, domain, reason;
37 EnumBanaction banaction;
40 unsigned long stats_hits, stats_misses;
41 DNSBLConfEntry(): duration(86400),bitmask(0),stats_hits(0), stats_misses(0) {}
46 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
48 class DNSBLResolver : public Resolver
52 DNSBLConfEntry *ConfEntry;
56 DNSBLResolver(Module *me, InspIRCd *ServerInstance, const std::string &hostname, userrec* u, int userfd, DNSBLConfEntry *conf, bool &cached)
57 : Resolver(ServerInstance, hostname, DNS_QUERY_A, cached, me)
64 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
66 /* Check the user still exists */
67 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
69 // Now we calculate the bitmask: 256*(256*(256*a+b)+c)+d
72 unsigned int bitmask = 0;
76 /* Convert the result to an in_addr (we can gaurantee we got ipv4)
77 * Whoever did the loop that was here before, I AM CONFISCATING
78 * YOUR CRACKPIPE. you know who you are. -- Brain
80 inet_aton(result.c_str(), &resultip);
81 bitmask = resultip.s_addr >> 24; /* Last octet (network byte order */
83 bitmask &= ConfEntry->bitmask;
87 std::string reason = ConfEntry->reason;
88 std::string::size_type x = reason.find("%ip%");
89 while (x != std::string::npos)
92 reason.insert(x, them->GetIPString());
93 x = reason.find("%ip%");
96 ConfEntry->stats_hits++;
98 switch (ConfEntry->banaction)
100 case DNSBLConfEntry::I_KILL:
102 userrec::QuitUser(ServerInstance, them, std::string("Killed (") + reason + ")");
105 case DNSBLConfEntry::I_KLINE:
107 std::string ban = std::string("*@") + them->GetIPString();
109 ServerInstance->XLines->apply_lines(APPLY_KLINES);
110 show = ServerInstance->XLines->add_kline(ConfEntry->duration, ServerInstance->Config->ServerName, reason.c_str(), ban.c_str());
111 FOREACH_MOD(I_OnAddKLine,OnAddKLine(ConfEntry->duration, NULL, reason, ban));
114 case DNSBLConfEntry::I_GLINE:
116 std::string ban = std::string("*@") + them->GetIPString();
117 show = ServerInstance->XLines->add_gline(ConfEntry->duration, ServerInstance->Config->ServerName, reason.c_str(), ban.c_str());
119 ServerInstance->XLines->apply_lines(APPLY_GLINES);
120 FOREACH_MOD(I_OnAddGLine,OnAddGLine(ConfEntry->duration, NULL, reason, ban));
123 case DNSBLConfEntry::I_ZLINE:
125 show = ServerInstance->XLines->add_zline(ConfEntry->duration, ServerInstance->Config->ServerName, reason.c_str(), them->GetIPString());
127 ServerInstance->XLines->apply_lines(APPLY_ZLINES);
128 FOREACH_MOD(I_OnAddZLine,OnAddZLine(ConfEntry->duration, NULL, reason, them->GetIPString()));
131 case DNSBLConfEntry::I_UNKNOWN:
140 ServerInstance->WriteOpers("*** Connecting user %s detected as being on a DNS blacklist (%s) with result %d", them->GetFullRealHost(), ConfEntry->name.c_str(), bitmask);
144 ConfEntry->stats_misses++;
147 ConfEntry->stats_misses++;
151 virtual void OnError(ResolverError e, const std::string &errormessage)
155 virtual ~DNSBLResolver()
160 class ModuleDNSBL : public Module
163 std::vector<DNSBLConfEntry *> DNSBLConfEntries;
166 * Convert a string to EnumBanaction
168 DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
170 if(action.compare("KILL")==0)
171 return DNSBLConfEntry::I_KILL;
172 if(action.compare("KLINE")==0)
173 return DNSBLConfEntry::I_KLINE;
174 if(action.compare("ZLINE")==0)
175 return DNSBLConfEntry::I_ZLINE;
176 if(action.compare("GLINE")==0)
177 return DNSBLConfEntry::I_GLINE;
179 return DNSBLConfEntry::I_UNKNOWN;
182 ModuleDNSBL(InspIRCd *Me) : Module(Me)
187 virtual ~ModuleDNSBL()
192 virtual Version GetVersion()
194 return Version(2, 0, 0, 1, VF_VENDOR, API_VERSION);
197 void Implements(char* List)
199 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnStats] = 1;
202 /** Clear entries and free the mem it was using
206 std::vector<DNSBLConfEntry *>::iterator i;
207 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
209 DNSBLConfEntries.clear();
212 /** Fill our conf vector with data
214 virtual void ReadConf()
216 ConfigReader *MyConf = new ConfigReader(ServerInstance);
219 for (int i=0; i< MyConf->Enumerate("dnsbl"); i++)
221 DNSBLConfEntry *e = new DNSBLConfEntry();
223 e->name = MyConf->ReadValue("dnsbl", "name", i);
224 e->reason = MyConf->ReadValue("dnsbl", "reason", i);
225 e->domain = MyConf->ReadValue("dnsbl", "domain", i);
226 e->banaction = str2banaction(MyConf->ReadValue("dnsbl", "action", i));
227 e->duration = ServerInstance->Duration(MyConf->ReadValue("dnsbl", "duration", i).c_str());
228 e->bitmask = MyConf->ReadInteger("dnsbl", "bitmask", i, false);
230 /* yeah, logic here is a little messy */
233 ServerInstance->WriteOpers("*** DNSBL(#%d): invalid bitmask",i);
235 else if (e->name == "")
237 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid name",i);
239 else if (e->domain == "")
241 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid domain",i);
243 else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
245 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid banaction", i);
251 ServerInstance->WriteOpers("*** DNSBL(#%d): empty reason, using defaults",i);
252 e->reason = "Your IP has been blacklisted.";
255 /* add it, all is ok */
256 DNSBLConfEntries.push_back(e);
260 /* delete and drop it, error somewhere */
267 virtual void OnRehash(userrec* user, const std::string ¶meter)
272 virtual int OnUserRegister(userrec* user)
274 /* only do lookups on local users */
277 /* following code taken from bopm, reverses an IP address. */
279 unsigned char a, b, c, d;
280 char reversedipbuf[128];
281 std::string reversedip;
282 bool success = false;
284 if (!inet_aton(user->GetIPString(), &in))
287 /* We could have an ipv6 address here */
288 std::string x = user->GetIPString();
289 /* Is it a 4in6 address? (Compensate for this kernel kludge that people love) */
290 if (x.find("0::ffff:") == 0)
292 x.erase(x.begin(), x.begin() + 8);
293 if (inet_aton(x.c_str(), &in))
306 d = (unsigned char) (in.s_addr >> 24) & 0xFF;
307 c = (unsigned char) (in.s_addr >> 16) & 0xFF;
308 b = (unsigned char) (in.s_addr >> 8) & 0xFF;
309 a = (unsigned char) in.s_addr & 0xFF;
311 snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
312 reversedip = std::string(reversedipbuf);
314 // For each DNSBL, we will run through this lookup
315 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
317 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
318 std::string hostname = reversedip + "." + (*i)->domain;
320 /* now we'd need to fire off lookups for `hostname'. */
322 DNSBLResolver *r = new DNSBLResolver(this, ServerInstance, hostname, user, user->GetFd(), *i, cached);
323 ServerInstance->AddResolver(r, cached);
327 /* don't do anything with this hot potato */
331 virtual int OnStats(char symbol, userrec* user, string_list &results)
336 unsigned long total_hits = 0, total_misses = 0;
338 for (std::vector<DNSBLConfEntry*>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
340 total_hits += (*i)->stats_hits;
341 total_misses += (*i)->stats_misses;
343 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS DNSbl \"" + (*i)->name + "\" had " +
344 ConvToStr((*i)->stats_hits) + " hits and " + ConvToStr((*i)->stats_misses) + " misses");
347 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total hits: " + ConvToStr(total_hits));
348 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total misses: " + ConvToStr(total_misses));
354 // stuff down here is the module-factory stuff.
356 class ModuleDNSBLFactory : public ModuleFactory
363 ~ModuleDNSBLFactory()
367 virtual Module *CreateModule(InspIRCd *Me)
369 return new ModuleDNSBL(Me);
375 extern "C" DllExport void * init_module( void )
377 return new ModuleDNSBLFactory;