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++;
129 ConfEntry->stats_misses++;
133 virtual void OnError(ResolverError e, const std::string &errormessage)
137 virtual ~DNSBLResolver()
142 class ModuleDNSBL : public Module
145 std::vector<DNSBLConfEntry *> DNSBLConfEntries;
148 * Convert a string to EnumBanaction
150 DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
152 if(action.compare("KILL")==0)
153 return DNSBLConfEntry::I_KILL;
154 if(action.compare("KLINE")==0)
155 return DNSBLConfEntry::I_KLINE;
156 if(action.compare("ZLINE")==0)
157 return DNSBLConfEntry::I_ZLINE;
158 if(action.compare("GLINE")==0)
159 return DNSBLConfEntry::I_GLINE;
161 return DNSBLConfEntry::I_UNKNOWN;
164 ModuleDNSBL(InspIRCd *Me) : Module::Module(Me)
169 virtual ~ModuleDNSBL()
174 virtual Version GetVersion()
176 return Version(2, 0, 0, 1, VF_VENDOR, API_VERSION);
179 void Implements(char* List)
181 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnStats] = 1;
184 /** Clear entries and free the mem it was using
188 std::vector<DNSBLConfEntry *>::iterator i;
189 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
191 DNSBLConfEntries.clear();
194 /** Fill our conf vector with data
196 virtual void ReadConf()
198 ConfigReader *MyConf = new ConfigReader(ServerInstance);
201 for (int i=0; i< MyConf->Enumerate("dnsbl"); i++)
203 DNSBLConfEntry *e = new DNSBLConfEntry();
205 e->name = MyConf->ReadValue("dnsbl", "name", i);
206 e->reason = MyConf->ReadValue("dnsbl", "reason", i);
207 e->domain = MyConf->ReadValue("dnsbl", "domain", i);
208 e->banaction = str2banaction(MyConf->ReadValue("dnsbl", "action", i));
209 e->duration = ServerInstance->Duration(MyConf->ReadValue("dnsbl", "duration", i).c_str());
210 e->bitmask = MyConf->ReadInteger("dnsbl", "bitmask", i, false);
212 /* yeah, logic here is a little messy */
215 ServerInstance->WriteOpers("*** DNSBL(#%d): invalid bitmask",i);
217 else if (e->name == "")
219 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid name",i);
221 else if (e->domain == "")
223 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid domain",i);
225 else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
227 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid banaction", i);
233 ServerInstance->WriteOpers("*** DNSBL(#%d): empty reason, using defaults",i);
234 e->reason = "Your IP has been blacklisted.";
237 /* add it, all is ok */
238 DNSBLConfEntries.push_back(e);
242 /* delete and drop it, error somewhere */
249 virtual void OnRehash(userrec* user, const std::string ¶meter)
255 * We will check each user that connects *locally* (userrec::fd>0)
257 virtual int OnUserRegister(userrec* user)
261 /* following code taken from bopm, reverses an IP address. */
263 unsigned char a, b, c, d;
264 char reversedipbuf[128];
265 std::string reversedip;
266 bool success = false;
268 if (!inet_aton(user->GetIPString(), &in))
271 /* We could have an ipv6 address here */
272 std::string x = user->GetIPString();
273 /* Is it a 4in6 address? (Compensate for this kernel kludge that people love) */
274 if (x.find("0::ffff:") == 0)
276 x.erase(x.begin(), x.begin() + 8);
277 if (inet_aton(x.c_str(), &in))
290 d = (unsigned char) (in.s_addr >> 24) & 0xFF;
291 c = (unsigned char) (in.s_addr >> 16) & 0xFF;
292 b = (unsigned char) (in.s_addr >> 8) & 0xFF;
293 a = (unsigned char) in.s_addr & 0xFF;
295 snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
296 reversedip = std::string(reversedipbuf);
298 // For each DNSBL, we will run through this lookup
299 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
301 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
302 std::string hostname = reversedip + "." + (*i)->domain;
304 /* now we'd need to fire off lookups for `hostname'. */
306 DNSBLResolver *r = new DNSBLResolver(this, ServerInstance, hostname, user, user->GetFd(), *i, cached);
307 ServerInstance->AddResolver(r, cached);
311 /* don't do anything with this hot potato */
315 virtual int OnStats(char symbol, userrec* user, string_list &results)
320 unsigned long total_hits = 0, total_misses = 0;
322 for (std::vector<DNSBLConfEntry*>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
324 total_hits += (*i)->stats_hits;
325 total_misses += (*i)->stats_misses;
327 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS DNSbl \"" + (*i)->name + "\" had " +
328 ConvToStr((*i)->stats_hits) + " hits and " + ConvToStr((*i)->stats_misses) + " misses");
331 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total hits: " + ConvToStr(total_hits));
332 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total misses: " + ConvToStr(total_misses));
338 // stuff down here is the module-factory stuff.
340 class ModuleDNSBLFactory : public ModuleFactory
347 ~ModuleDNSBLFactory()
351 virtual Module *CreateModule(InspIRCd *Me)
353 return new ModuleDNSBL(Me);
359 extern "C" void * init_module( void )
361 return new ModuleDNSBLFactory;