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();
108 show = ServerInstance->XLines->add_kline(ConfEntry->duration, ServerInstance->Config->ServerName, reason.c_str(), ban.c_str());
109 FOREACH_MOD(I_OnAddKLine,OnAddKLine(ConfEntry->duration, NULL, reason, ban));
112 case DNSBLConfEntry::I_GLINE:
114 std::string ban = std::string("*@") + them->GetIPString();
115 show = ServerInstance->XLines->add_gline(ConfEntry->duration, ServerInstance->Config->ServerName, reason.c_str(), ban.c_str());
116 FOREACH_MOD(I_OnAddGLine,OnAddGLine(ConfEntry->duration, NULL, reason, ban));
119 case DNSBLConfEntry::I_ZLINE:
121 show = ServerInstance->XLines->add_zline(ConfEntry->duration, ServerInstance->Config->ServerName, reason.c_str(), them->GetIPString());
122 FOREACH_MOD(I_OnAddZLine,OnAddZLine(ConfEntry->duration, NULL, reason, them->GetIPString()));
125 case DNSBLConfEntry::I_UNKNOWN:
133 ServerInstance->WriteOpers("*** Connecting user %s detected as being on a DNS blacklist (%s) with result %d", them->GetFullRealHost(), ConfEntry->name.c_str(), bitmask);
136 ConfEntry->stats_misses++;
139 ConfEntry->stats_misses++;
143 virtual void OnError(ResolverError e, const std::string &errormessage)
147 virtual ~DNSBLResolver()
152 class ModuleDNSBL : public Module
155 std::vector<DNSBLConfEntry *> DNSBLConfEntries;
158 * Convert a string to EnumBanaction
160 DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
162 if(action.compare("KILL")==0)
163 return DNSBLConfEntry::I_KILL;
164 if(action.compare("KLINE")==0)
165 return DNSBLConfEntry::I_KLINE;
166 if(action.compare("ZLINE")==0)
167 return DNSBLConfEntry::I_ZLINE;
168 if(action.compare("GLINE")==0)
169 return DNSBLConfEntry::I_GLINE;
171 return DNSBLConfEntry::I_UNKNOWN;
174 ModuleDNSBL(InspIRCd *Me) : Module(Me)
179 virtual ~ModuleDNSBL()
184 virtual Version GetVersion()
186 return Version(2, 0, 0, 1, VF_VENDOR, API_VERSION);
189 void Implements(char* List)
191 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnStats] = 1;
194 /** Clear entries and free the mem it was using
198 std::vector<DNSBLConfEntry *>::iterator i;
199 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
201 DNSBLConfEntries.clear();
204 /** Fill our conf vector with data
206 virtual void ReadConf()
208 ConfigReader *MyConf = new ConfigReader(ServerInstance);
211 for (int i=0; i< MyConf->Enumerate("dnsbl"); i++)
213 DNSBLConfEntry *e = new DNSBLConfEntry();
215 e->name = MyConf->ReadValue("dnsbl", "name", i);
216 e->reason = MyConf->ReadValue("dnsbl", "reason", i);
217 e->domain = MyConf->ReadValue("dnsbl", "domain", i);
218 e->banaction = str2banaction(MyConf->ReadValue("dnsbl", "action", i));
219 e->duration = ServerInstance->Duration(MyConf->ReadValue("dnsbl", "duration", i).c_str());
220 e->bitmask = MyConf->ReadInteger("dnsbl", "bitmask", i, false);
222 /* yeah, logic here is a little messy */
225 ServerInstance->WriteOpers("*** DNSBL(#%d): invalid bitmask",i);
227 else if (e->name == "")
229 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid name",i);
231 else if (e->domain == "")
233 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid domain",i);
235 else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
237 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid banaction", i);
243 ServerInstance->WriteOpers("*** DNSBL(#%d): empty reason, using defaults",i);
244 e->reason = "Your IP has been blacklisted.";
247 /* add it, all is ok */
248 DNSBLConfEntries.push_back(e);
252 /* delete and drop it, error somewhere */
259 virtual void OnRehash(userrec* user, const std::string ¶meter)
264 virtual int OnUserRegister(userrec* user)
266 /* only do lookups on local users */
269 /* following code taken from bopm, reverses an IP address. */
271 unsigned char a, b, c, d;
272 char reversedipbuf[128];
273 std::string reversedip;
274 bool success = false;
276 if (!inet_aton(user->GetIPString(), &in))
279 /* We could have an ipv6 address here */
280 std::string x = user->GetIPString();
281 /* Is it a 4in6 address? (Compensate for this kernel kludge that people love) */
282 if (x.find("0::ffff:") == 0)
284 x.erase(x.begin(), x.begin() + 8);
285 if (inet_aton(x.c_str(), &in))
298 d = (unsigned char) (in.s_addr >> 24) & 0xFF;
299 c = (unsigned char) (in.s_addr >> 16) & 0xFF;
300 b = (unsigned char) (in.s_addr >> 8) & 0xFF;
301 a = (unsigned char) in.s_addr & 0xFF;
303 snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
304 reversedip = std::string(reversedipbuf);
306 // For each DNSBL, we will run through this lookup
307 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
309 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
310 std::string hostname = reversedip + "." + (*i)->domain;
312 /* now we'd need to fire off lookups for `hostname'. */
314 DNSBLResolver *r = new DNSBLResolver(this, ServerInstance, hostname, user, user->GetFd(), *i, cached);
315 ServerInstance->AddResolver(r, cached);
319 /* don't do anything with this hot potato */
323 virtual int OnStats(char symbol, userrec* user, string_list &results)
328 unsigned long total_hits = 0, total_misses = 0;
330 for (std::vector<DNSBLConfEntry*>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
332 total_hits += (*i)->stats_hits;
333 total_misses += (*i)->stats_misses;
335 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS DNSbl \"" + (*i)->name + "\" had " +
336 ConvToStr((*i)->stats_hits) + " hits and " + ConvToStr((*i)->stats_misses) + " misses");
339 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total hits: " + ConvToStr(total_hits));
340 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total misses: " + ConvToStr(total_misses));
346 // stuff down here is the module-factory stuff.
348 class ModuleDNSBLFactory : public ModuleFactory
355 ~ModuleDNSBLFactory()
359 virtual Module *CreateModule(InspIRCd *Me)
361 return new ModuleDNSBL(Me);
367 extern "C" DllExport void * init_module( void )
369 return new ModuleDNSBLFactory;