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 DNSBLConfEntry(): duration(86400),bitmask(0) {}
40 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
42 class DNSBLResolver : public Resolver
46 DNSBLConfEntry *ConfEntry;
49 DNSBLResolver(Module *me, InspIRCd *ServerInstance, const std::string &hostname, userrec* u, int userfd, DNSBLConfEntry *conf, bool &cached)
50 : Resolver(ServerInstance, hostname, DNS_QUERY_A, cached, me)
57 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
59 /* Check the user still exists */
60 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
62 // Now we calculate the bitmask: 256*(256*(256*a+b)+c)+d
65 unsigned int bitmask=0;
66 unsigned int octetpos=0;
67 std::string tmp = result;
72 /* Fix by brain, npos is -1, so unsigned int will never match */
73 std::string::size_type lastdot = tmp.rfind(".");
75 if (lastdot == std::string::npos)
82 octet=tmp.substr(lastdot+1,tmp.length()-lastdot+1);
86 bitmask += (256 ^ octetpos) * atoi(octet.c_str());
90 bitmask &= ConfEntry->bitmask;
94 std::string reason = ConfEntry->reason;
95 std::string::size_type x = reason.find("%ip%");
96 while (x != std::string::npos)
99 reason.insert(x, them->GetIPString());
100 x = reason.find("%ip%");
103 ServerInstance->WriteOpers("*** Connecting user %s detected as being on a DNS blacklist (%s) with result %d", them->GetFullRealHost(), ConfEntry->name.c_str(), bitmask);
105 switch (ConfEntry->banaction)
107 case DNSBLConfEntry::I_KILL:
109 userrec::QuitUser(ServerInstance, them, std::string("Killed (") + reason + ")");
112 case DNSBLConfEntry::I_KLINE:
114 ServerInstance->AddKLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, std::string("*@") + them->GetIPString());
115 FOREACH_MOD(I_OnAddKLine,OnAddKLine(ConfEntry->duration, NULL, reason, std::string("*@") + them->GetIPString()));
118 case DNSBLConfEntry::I_GLINE:
120 ServerInstance->AddGLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, std::string("*@") + them->GetIPString());
121 FOREACH_MOD(I_OnAddGLine,OnAddGLine(ConfEntry->duration, NULL, reason, std::string("*@") + them->GetIPString()));
124 case DNSBLConfEntry::I_ZLINE:
126 ServerInstance->AddZLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, them->GetIPString());
127 FOREACH_MOD(I_OnAddZLine,OnAddZLine(ConfEntry->duration, NULL, reason, them->GetIPString()));
130 case DNSBLConfEntry::I_UNKNOWN:
141 virtual void OnError(ResolverError e, const std::string &errormessage)
145 virtual ~DNSBLResolver()
150 class ModuleDNSBL : public Module
153 std::vector<DNSBLConfEntry *> DNSBLConfEntries;
156 * Convert a string to EnumBanaction
158 DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
160 if(action.compare("KILL")==0)
161 return DNSBLConfEntry::I_KILL;
162 if(action.compare("KLINE")==0)
163 return DNSBLConfEntry::I_KLINE;
164 if(action.compare("ZLINE")==0)
165 return DNSBLConfEntry::I_ZLINE;
166 if(action.compare("GLINE")==0)
167 return DNSBLConfEntry::I_GLINE;
169 return DNSBLConfEntry::I_UNKNOWN;
172 ModuleDNSBL(InspIRCd *Me) : Module::Module(Me)
177 virtual ~ModuleDNSBL()
182 virtual Version GetVersion()
184 return Version(2, 0, 0, 1, VF_VENDOR, API_VERSION);
187 void Implements(char* List)
189 List[I_OnRehash] = List[I_OnUserRegister] = 1;
192 /** Clear entries and free the mem it was using
196 std::vector<DNSBLConfEntry *>::iterator i;
197 while ((i = DNSBLConfEntries.begin()) != DNSBLConfEntries.end())
199 DNSBLConfEntries.erase(i);
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)
265 * We will check each user that connects *locally* (userrec::fd>0)
267 virtual int OnUserRegister(userrec* user)
271 /* following code taken from bopm, reverses an IP address. */
273 unsigned char a, b, c, d;
274 char reversedipbuf[128];
275 std::string reversedip;
276 bool success = false;
278 if (!inet_aton(user->GetIPString(), &in))
281 /* We could have an ipv6 address here */
282 std::string x = user->GetIPString();
283 /* Is it a 4in6 address? (Compensate for this kernel kludge that people love) */
284 if (x.find("0::ffff:") == 0)
286 x.erase(x.begin(), x.begin() + 8);
287 if (inet_aton(x.c_str(), &in))
300 d = (unsigned char) (in.s_addr >> 24) & 0xFF;
301 c = (unsigned char) (in.s_addr >> 16) & 0xFF;
302 b = (unsigned char) (in.s_addr >> 8) & 0xFF;
303 a = (unsigned char) in.s_addr & 0xFF;
305 snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
306 reversedip = std::string(reversedipbuf);
308 // For each DNSBL, we will run through this lookup
309 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
311 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
312 std::string hostname = reversedip + "." + (*i)->domain;
314 /* now we'd need to fire off lookups for `hostname'. */
316 DNSBLResolver *r = new DNSBLResolver(this, ServerInstance, hostname, user, user->GetFd(), *i, cached);
317 ServerInstance->AddResolver(r, cached);
321 /* don't do anything with this hot potato */
326 // stuff down here is the module-factory stuff.
328 class ModuleDNSBLFactory : public ModuleFactory
335 ~ModuleDNSBLFactory()
339 virtual Module *CreateModule(InspIRCd *Me)
341 return new ModuleDNSBL(Me);
347 extern "C" void * init_module( void )
349 return new ModuleDNSBLFactory;