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 * ---------------------------------------------------
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
25 /* $ModDesc: Provides handling of DNS blacklists */
27 /* Class holding data for a single entry */
31 enum EnumBanaction { I_UNKNOWN, I_KILL, I_ZLINE, I_KLINE, I_GLINE };
32 std::string name, domain, reason;
33 EnumBanaction banaction;
36 unsigned long stats_hits, stats_misses;
37 DNSBLConfEntry(): duration(86400),bitmask(0),stats_hits(0), stats_misses(0) {}
42 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
44 class DNSBLResolver : public Resolver
48 DNSBLConfEntry *ConfEntry;
51 DNSBLResolver(Module *me, InspIRCd *ServerInstance, const std::string &hostname, userrec* u, int userfd, DNSBLConfEntry *conf, bool &cached)
52 : Resolver(ServerInstance, hostname, DNS_QUERY_A, cached, me)
59 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
61 /* Check the user still exists */
62 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
64 // Now we calculate the bitmask: 256*(256*(256*a+b)+c)+d
67 unsigned int bitmask=0;
70 /* Convert the result to an in_addr (we can gaurantee we got ipv4)
71 * Whoever did the loop that was here before, I AM CONFISCATING
72 * YOUR CRACKPIPE. you know who you are. -- Brain
74 inet_aton(result.c_str(), &resultip);
75 bitmask = resultip.s_addr >> 24; /* Last octet (network byte order */
77 bitmask &= ConfEntry->bitmask;
81 std::string reason = ConfEntry->reason;
82 std::string::size_type x = reason.find("%ip%");
83 while (x != std::string::npos)
86 reason.insert(x, them->GetIPString());
87 x = reason.find("%ip%");
90 ServerInstance->WriteOpers("*** Connecting user %s detected as being on a DNS blacklist (%s) with result %d", them->GetFullRealHost(), ConfEntry->name.c_str(), bitmask);
91 ConfEntry->stats_hits++;
93 switch (ConfEntry->banaction)
95 case DNSBLConfEntry::I_KILL:
97 userrec::QuitUser(ServerInstance, them, std::string("Killed (") + reason + ")");
100 case DNSBLConfEntry::I_KLINE:
102 ServerInstance->AddKLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, std::string("*@") + them->GetIPString());
103 FOREACH_MOD(I_OnAddKLine,OnAddKLine(ConfEntry->duration, NULL, reason, std::string("*@") + them->GetIPString()));
106 case DNSBLConfEntry::I_GLINE:
108 ServerInstance->AddGLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, std::string("*@") + them->GetIPString());
109 FOREACH_MOD(I_OnAddGLine,OnAddGLine(ConfEntry->duration, NULL, reason, std::string("*@") + them->GetIPString()));
112 case DNSBLConfEntry::I_ZLINE:
114 ServerInstance->AddZLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, them->GetIPString());
115 FOREACH_MOD(I_OnAddZLine,OnAddZLine(ConfEntry->duration, NULL, reason, them->GetIPString()));
118 case DNSBLConfEntry::I_UNKNOWN:
126 ConfEntry->stats_misses++;
131 virtual void OnError(ResolverError e, const std::string &errormessage)
135 virtual ~DNSBLResolver()
140 class ModuleDNSBL : public Module
143 std::vector<DNSBLConfEntry *> DNSBLConfEntries;
146 * Convert a string to EnumBanaction
148 DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
150 if(action.compare("KILL")==0)
151 return DNSBLConfEntry::I_KILL;
152 if(action.compare("KLINE")==0)
153 return DNSBLConfEntry::I_KLINE;
154 if(action.compare("ZLINE")==0)
155 return DNSBLConfEntry::I_ZLINE;
156 if(action.compare("GLINE")==0)
157 return DNSBLConfEntry::I_GLINE;
159 return DNSBLConfEntry::I_UNKNOWN;
162 ModuleDNSBL(InspIRCd *Me) : Module::Module(Me)
167 virtual ~ModuleDNSBL()
172 virtual Version GetVersion()
174 return Version(2, 0, 0, 1, VF_VENDOR, API_VERSION);
177 void Implements(char* List)
179 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnStats] = 1;
182 /** Clear entries and free the mem it was using
186 std::vector<DNSBLConfEntry *>::iterator i;
187 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
189 DNSBLConfEntries.clear();
192 /** Fill our conf vector with data
194 virtual void ReadConf()
196 ConfigReader *MyConf = new ConfigReader(ServerInstance);
199 for (int i=0; i< MyConf->Enumerate("dnsbl"); i++)
201 DNSBLConfEntry *e = new DNSBLConfEntry();
203 e->name = MyConf->ReadValue("dnsbl", "name", i);
204 e->reason = MyConf->ReadValue("dnsbl", "reason", i);
205 e->domain = MyConf->ReadValue("dnsbl", "domain", i);
206 e->banaction = str2banaction(MyConf->ReadValue("dnsbl", "action", i));
207 e->duration = ServerInstance->Duration(MyConf->ReadValue("dnsbl", "duration", i).c_str());
208 e->bitmask = MyConf->ReadInteger("dnsbl", "bitmask", i, false);
210 /* yeah, logic here is a little messy */
213 ServerInstance->WriteOpers("*** DNSBL(#%d): invalid bitmask",i);
215 else if (e->name == "")
217 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid name",i);
219 else if (e->domain == "")
221 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid domain",i);
223 else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
225 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid banaction", i);
231 ServerInstance->WriteOpers("*** DNSBL(#%d): empty reason, using defaults",i);
232 e->reason = "Your IP has been blacklisted.";
235 /* add it, all is ok */
236 DNSBLConfEntries.push_back(e);
240 /* delete and drop it, error somewhere */
247 virtual void OnRehash(userrec* user, const std::string ¶meter)
253 * We will check each user that connects *locally* (userrec::fd>0)
255 virtual int OnUserRegister(userrec* user)
259 /* following code taken from bopm, reverses an IP address. */
261 unsigned char a, b, c, d;
262 char reversedipbuf[128];
263 std::string reversedip;
264 bool success = false;
266 if (!inet_aton(user->GetIPString(), &in))
269 /* We could have an ipv6 address here */
270 std::string x = user->GetIPString();
271 /* Is it a 4in6 address? (Compensate for this kernel kludge that people love) */
272 if (x.find("0::ffff:") == 0)
274 x.erase(x.begin(), x.begin() + 8);
275 if (inet_aton(x.c_str(), &in))
288 d = (unsigned char) (in.s_addr >> 24) & 0xFF;
289 c = (unsigned char) (in.s_addr >> 16) & 0xFF;
290 b = (unsigned char) (in.s_addr >> 8) & 0xFF;
291 a = (unsigned char) in.s_addr & 0xFF;
293 snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
294 reversedip = std::string(reversedipbuf);
296 // For each DNSBL, we will run through this lookup
297 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
299 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
300 std::string hostname = reversedip + "." + (*i)->domain;
302 /* now we'd need to fire off lookups for `hostname'. */
304 DNSBLResolver *r = new DNSBLResolver(this, ServerInstance, hostname, user, user->GetFd(), *i, cached);
305 ServerInstance->AddResolver(r, cached);
309 /* don't do anything with this hot potato */
313 virtual int OnStats(char symbol, userrec* user, string_list &results)
318 unsigned long total_hits = 0, total_misses = 0;
320 for (std::vector<DNSBLConfEntry*>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
322 total_hits += (*i)->stats_hits;
323 total_misses += (*i)->stats_misses;
325 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS DNSbl \"" + (*i)->name + "\" had " +
326 ConvToStr((*i)->stats_hits) + " hits and " + ConvToStr((*i)->stats_misses) + " misses");
329 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total hits: " + ConvToStr(total_hits));
330 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total misses: " + ConvToStr(total_misses));
336 // stuff down here is the module-factory stuff.
338 class ModuleDNSBLFactory : public ModuleFactory
345 ~ModuleDNSBLFactory()
349 virtual Module *CreateModule(InspIRCd *Me)
351 return new ModuleDNSBL(Me);
357 extern "C" void * init_module( void )
359 return new ModuleDNSBLFactory;