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) {}
41 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
43 class DNSBLResolver : public Resolver
47 DNSBLConfEntry *ConfEntry;
50 DNSBLResolver(Module *me, InspIRCd *ServerInstance, const std::string &hostname, userrec* u, int userfd, DNSBLConfEntry *conf, bool &cached)
51 : Resolver(ServerInstance, hostname, DNS_QUERY_A, cached, me)
58 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
60 /* Check the user still exists */
61 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
63 // Now we calculate the bitmask: 256*(256*(256*a+b)+c)+d
66 unsigned int bitmask=0;
69 /* Convert the result to an in_addr (we can gaurantee we got ipv4)
70 * Whoever did the loop that was here before, I AM CONFISCATING
71 * YOUR CRACKPIPE. you know who you are. -- Brain
73 inet_aton(result.c_str(), &resultip);
74 bitmask = resultip.s_addr >> 24; /* Last octet (network byte order */
76 bitmask &= ConfEntry->bitmask;
80 std::string reason = ConfEntry->reason;
81 std::string::size_type x = reason.find("%ip%");
82 while (x != std::string::npos)
85 reason.insert(x, them->GetIPString());
86 x = reason.find("%ip%");
89 ServerInstance->WriteOpers("*** Connecting user %s detected as being on a DNS blacklist (%s) with result %d", them->GetFullRealHost(), ConfEntry->name.c_str(), bitmask);
91 switch (ConfEntry->banaction)
93 case DNSBLConfEntry::I_KILL:
95 userrec::QuitUser(ServerInstance, them, std::string("Killed (") + reason + ")");
98 case DNSBLConfEntry::I_KLINE:
100 ServerInstance->AddKLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, std::string("*@") + them->GetIPString());
101 FOREACH_MOD(I_OnAddKLine,OnAddKLine(ConfEntry->duration, NULL, reason, std::string("*@") + them->GetIPString()));
104 case DNSBLConfEntry::I_GLINE:
106 ServerInstance->AddGLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, std::string("*@") + them->GetIPString());
107 FOREACH_MOD(I_OnAddGLine,OnAddGLine(ConfEntry->duration, NULL, reason, std::string("*@") + them->GetIPString()));
110 case DNSBLConfEntry::I_ZLINE:
112 ServerInstance->AddZLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, them->GetIPString());
113 FOREACH_MOD(I_OnAddZLine,OnAddZLine(ConfEntry->duration, NULL, reason, them->GetIPString()));
116 case DNSBLConfEntry::I_UNKNOWN:
127 virtual void OnError(ResolverError e, const std::string &errormessage)
131 virtual ~DNSBLResolver()
136 class ModuleDNSBL : public Module
139 std::vector<DNSBLConfEntry *> DNSBLConfEntries;
142 * Convert a string to EnumBanaction
144 DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
146 if(action.compare("KILL")==0)
147 return DNSBLConfEntry::I_KILL;
148 if(action.compare("KLINE")==0)
149 return DNSBLConfEntry::I_KLINE;
150 if(action.compare("ZLINE")==0)
151 return DNSBLConfEntry::I_ZLINE;
152 if(action.compare("GLINE")==0)
153 return DNSBLConfEntry::I_GLINE;
155 return DNSBLConfEntry::I_UNKNOWN;
158 ModuleDNSBL(InspIRCd *Me) : Module::Module(Me)
163 virtual ~ModuleDNSBL()
168 virtual Version GetVersion()
170 return Version(2, 0, 0, 1, VF_VENDOR, API_VERSION);
173 void Implements(char* List)
175 List[I_OnRehash] = List[I_OnUserRegister] = 1;
178 /** Clear entries and free the mem it was using
182 std::vector<DNSBLConfEntry *>::iterator i;
183 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
185 DNSBLConfEntries.clear();
188 /** Fill our conf vector with data
190 virtual void ReadConf()
192 ConfigReader *MyConf = new ConfigReader(ServerInstance);
195 for (int i=0; i< MyConf->Enumerate("dnsbl"); i++)
197 DNSBLConfEntry *e = new DNSBLConfEntry();
199 e->name = MyConf->ReadValue("dnsbl", "name", i);
200 e->reason = MyConf->ReadValue("dnsbl", "reason", i);
201 e->domain = MyConf->ReadValue("dnsbl", "domain", i);
202 e->banaction = str2banaction(MyConf->ReadValue("dnsbl", "action", i));
203 e->duration = ServerInstance->Duration(MyConf->ReadValue("dnsbl", "duration", i).c_str());
204 e->bitmask = MyConf->ReadInteger("dnsbl", "bitmask", i, false);
206 /* yeah, logic here is a little messy */
209 ServerInstance->WriteOpers("*** DNSBL(#%d): invalid bitmask",i);
211 else if (e->name == "")
213 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid name",i);
215 else if (e->domain == "")
217 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid domain",i);
219 else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
221 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid banaction", i);
227 ServerInstance->WriteOpers("*** DNSBL(#%d): empty reason, using defaults",i);
228 e->reason = "Your IP has been blacklisted.";
231 /* add it, all is ok */
232 DNSBLConfEntries.push_back(e);
236 /* delete and drop it, error somewhere */
243 virtual void OnRehash(userrec* user, const std::string ¶meter)
249 * We will check each user that connects *locally* (userrec::fd>0)
251 virtual int OnUserRegister(userrec* user)
255 /* following code taken from bopm, reverses an IP address. */
257 unsigned char a, b, c, d;
258 char reversedipbuf[128];
259 std::string reversedip;
260 bool success = false;
262 if (!inet_aton(user->GetIPString(), &in))
265 /* We could have an ipv6 address here */
266 std::string x = user->GetIPString();
267 /* Is it a 4in6 address? (Compensate for this kernel kludge that people love) */
268 if (x.find("0::ffff:") == 0)
270 x.erase(x.begin(), x.begin() + 8);
271 if (inet_aton(x.c_str(), &in))
284 d = (unsigned char) (in.s_addr >> 24) & 0xFF;
285 c = (unsigned char) (in.s_addr >> 16) & 0xFF;
286 b = (unsigned char) (in.s_addr >> 8) & 0xFF;
287 a = (unsigned char) in.s_addr & 0xFF;
289 snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
290 reversedip = std::string(reversedipbuf);
292 // For each DNSBL, we will run through this lookup
293 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
295 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
296 std::string hostname = reversedip + "." + (*i)->domain;
298 /* now we'd need to fire off lookups for `hostname'. */
300 DNSBLResolver *r = new DNSBLResolver(this, ServerInstance, hostname, user, user->GetFd(), *i, cached);
301 ServerInstance->AddResolver(r, cached);
305 /* don't do anything with this hot potato */
310 // stuff down here is the module-factory stuff.
312 class ModuleDNSBLFactory : public ModuleFactory
319 ~ModuleDNSBLFactory()
323 virtual Module *CreateModule(InspIRCd *Me)
325 return new ModuleDNSBL(Me);
331 extern "C" void * init_module( void )
333 return new ModuleDNSBLFactory;