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;
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();
107 show = ServerInstance->XLines->add_kline(ConfEntry->duration, ServerInstance->Config->ServerName, reason.c_str(), ban.c_str());
108 FOREACH_MOD(I_OnAddKLine,OnAddKLine(ConfEntry->duration, NULL, reason, ban));
111 case DNSBLConfEntry::I_GLINE:
113 std::string ban = std::string("*@") + them->GetIPString();
114 show = ServerInstance->XLines->add_gline(ConfEntry->duration, ServerInstance->Config->ServerName, reason.c_str(), ban.c_str());
115 FOREACH_MOD(I_OnAddGLine,OnAddGLine(ConfEntry->duration, NULL, reason, ban));
118 case DNSBLConfEntry::I_ZLINE:
120 show = ServerInstance->XLines->add_zline(ConfEntry->duration, ServerInstance->Config->ServerName, reason.c_str(), them->GetIPString());
121 FOREACH_MOD(I_OnAddZLine,OnAddZLine(ConfEntry->duration, NULL, reason, them->GetIPString()));
124 case DNSBLConfEntry::I_UNKNOWN:
132 ServerInstance->WriteOpers("*** Connecting user %s detected as being on a DNS blacklist (%s) with result %d", them->GetFullRealHost(), ConfEntry->name.c_str(), bitmask);
135 ConfEntry->stats_misses++;
138 ConfEntry->stats_misses++;
142 virtual void OnError(ResolverError e, const std::string &errormessage)
146 virtual ~DNSBLResolver()
151 class ModuleDNSBL : public Module
154 std::vector<DNSBLConfEntry *> DNSBLConfEntries;
157 * Convert a string to EnumBanaction
159 DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
161 if(action.compare("KILL")==0)
162 return DNSBLConfEntry::I_KILL;
163 if(action.compare("KLINE")==0)
164 return DNSBLConfEntry::I_KLINE;
165 if(action.compare("ZLINE")==0)
166 return DNSBLConfEntry::I_ZLINE;
167 if(action.compare("GLINE")==0)
168 return DNSBLConfEntry::I_GLINE;
170 return DNSBLConfEntry::I_UNKNOWN;
173 ModuleDNSBL(InspIRCd *Me) : Module(Me)
178 virtual ~ModuleDNSBL()
183 virtual Version GetVersion()
185 return Version(2, 0, 0, 1, VF_VENDOR, API_VERSION);
188 void Implements(char* List)
190 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnStats] = 1;
193 /** Clear entries and free the mem it was using
197 std::vector<DNSBLConfEntry *>::iterator i;
198 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
200 DNSBLConfEntries.clear();
203 /** Fill our conf vector with data
205 virtual void ReadConf()
207 ConfigReader *MyConf = new ConfigReader(ServerInstance);
210 for (int i=0; i< MyConf->Enumerate("dnsbl"); i++)
212 DNSBLConfEntry *e = new DNSBLConfEntry();
214 e->name = MyConf->ReadValue("dnsbl", "name", i);
215 e->reason = MyConf->ReadValue("dnsbl", "reason", i);
216 e->domain = MyConf->ReadValue("dnsbl", "domain", i);
217 e->banaction = str2banaction(MyConf->ReadValue("dnsbl", "action", i));
218 e->duration = ServerInstance->Duration(MyConf->ReadValue("dnsbl", "duration", i).c_str());
219 e->bitmask = MyConf->ReadInteger("dnsbl", "bitmask", i, false);
221 /* yeah, logic here is a little messy */
224 ServerInstance->WriteOpers("*** DNSBL(#%d): invalid bitmask",i);
226 else if (e->name == "")
228 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid name",i);
230 else if (e->domain == "")
232 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid domain",i);
234 else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
236 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid banaction", i);
242 ServerInstance->WriteOpers("*** DNSBL(#%d): empty reason, using defaults",i);
243 e->reason = "Your IP has been blacklisted.";
246 /* add it, all is ok */
247 DNSBLConfEntries.push_back(e);
251 /* delete and drop it, error somewhere */
258 virtual void OnRehash(userrec* user, const std::string ¶meter)
263 virtual int OnUserRegister(userrec* user)
265 /* only do lookups on local users */
268 /* following code taken from bopm, reverses an IP address. */
270 unsigned char a, b, c, d;
271 char reversedipbuf[128];
272 std::string reversedip;
273 bool success = false;
275 if (!inet_aton(user->GetIPString(), &in))
278 /* We could have an ipv6 address here */
279 std::string x = user->GetIPString();
280 /* Is it a 4in6 address? (Compensate for this kernel kludge that people love) */
281 if (x.find("0::ffff:") == 0)
283 x.erase(x.begin(), x.begin() + 8);
284 if (inet_aton(x.c_str(), &in))
297 d = (unsigned char) (in.s_addr >> 24) & 0xFF;
298 c = (unsigned char) (in.s_addr >> 16) & 0xFF;
299 b = (unsigned char) (in.s_addr >> 8) & 0xFF;
300 a = (unsigned char) in.s_addr & 0xFF;
302 snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
303 reversedip = std::string(reversedipbuf);
305 // For each DNSBL, we will run through this lookup
306 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
308 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
309 std::string hostname = reversedip + "." + (*i)->domain;
311 /* now we'd need to fire off lookups for `hostname'. */
313 DNSBLResolver *r = new DNSBLResolver(this, ServerInstance, hostname, user, user->GetFd(), *i, cached);
314 ServerInstance->AddResolver(r, cached);
318 /* don't do anything with this hot potato */
322 virtual int OnStats(char symbol, userrec* user, string_list &results)
327 unsigned long total_hits = 0, total_misses = 0;
329 for (std::vector<DNSBLConfEntry*>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
331 total_hits += (*i)->stats_hits;
332 total_misses += (*i)->stats_misses;
334 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS DNSbl \"" + (*i)->name + "\" had " +
335 ConvToStr((*i)->stats_hits) + " hits and " + ConvToStr((*i)->stats_misses) + " misses");
338 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total hits: " + ConvToStr(total_hits));
339 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total misses: " + ConvToStr(total_misses));
345 // stuff down here is the module-factory stuff.
347 class ModuleDNSBLFactory : public ModuleFactory
354 ~ModuleDNSBLFactory()
358 virtual Module *CreateModule(InspIRCd *Me)
360 return new ModuleDNSBL(Me);
366 extern "C" DllExport void * init_module( void )
368 return new ModuleDNSBLFactory;