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;
68 /* Convert the result to an in_addr (we can gaurantee we got ipv4)
69 * Whoever did the loop that was here before, I AM CONFISCATING
70 * YOUR CRACKPIPE. you know who you are. -- Brain
72 inet_aton(result.c_str(), &resultip);
73 bitmask = resultip.s_addr & 0xFF; /* Last octet (network byte order */
75 bitmask &= ConfEntry->bitmask;
79 std::string reason = ConfEntry->reason;
80 std::string::size_type x = reason.find("%ip%");
81 while (x != std::string::npos)
84 reason.insert(x, them->GetIPString());
85 x = reason.find("%ip%");
88 ServerInstance->WriteOpers("*** Connecting user %s detected as being on a DNS blacklist (%s) with result %d", them->GetFullRealHost(), ConfEntry->name.c_str(), bitmask);
90 switch (ConfEntry->banaction)
92 case DNSBLConfEntry::I_KILL:
94 userrec::QuitUser(ServerInstance, them, std::string("Killed (") + reason + ")");
97 case DNSBLConfEntry::I_KLINE:
99 ServerInstance->AddKLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, std::string("*@") + them->GetIPString());
100 FOREACH_MOD(I_OnAddKLine,OnAddKLine(ConfEntry->duration, NULL, reason, std::string("*@") + them->GetIPString()));
103 case DNSBLConfEntry::I_GLINE:
105 ServerInstance->AddGLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, std::string("*@") + them->GetIPString());
106 FOREACH_MOD(I_OnAddGLine,OnAddGLine(ConfEntry->duration, NULL, reason, std::string("*@") + them->GetIPString()));
109 case DNSBLConfEntry::I_ZLINE:
111 ServerInstance->AddZLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, them->GetIPString());
112 FOREACH_MOD(I_OnAddZLine,OnAddZLine(ConfEntry->duration, NULL, reason, them->GetIPString()));
115 case DNSBLConfEntry::I_UNKNOWN:
126 virtual void OnError(ResolverError e, const std::string &errormessage)
130 virtual ~DNSBLResolver()
135 class ModuleDNSBL : public Module
138 std::vector<DNSBLConfEntry *> DNSBLConfEntries;
141 * Convert a string to EnumBanaction
143 DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
145 if(action.compare("KILL")==0)
146 return DNSBLConfEntry::I_KILL;
147 if(action.compare("KLINE")==0)
148 return DNSBLConfEntry::I_KLINE;
149 if(action.compare("ZLINE")==0)
150 return DNSBLConfEntry::I_ZLINE;
151 if(action.compare("GLINE")==0)
152 return DNSBLConfEntry::I_GLINE;
154 return DNSBLConfEntry::I_UNKNOWN;
157 ModuleDNSBL(InspIRCd *Me) : Module::Module(Me)
162 virtual ~ModuleDNSBL()
167 virtual Version GetVersion()
169 return Version(2, 0, 0, 1, VF_VENDOR, API_VERSION);
172 void Implements(char* List)
174 List[I_OnRehash] = List[I_OnUserRegister] = 1;
177 /** Clear entries and free the mem it was using
181 std::vector<DNSBLConfEntry *>::iterator i;
182 while ((i = DNSBLConfEntries.begin()) != DNSBLConfEntries.end())
184 DNSBLConfEntries.erase(i);
189 /** Fill our conf vector with data
191 virtual void ReadConf()
193 ConfigReader *MyConf = new ConfigReader(ServerInstance);
196 for (int i=0; i< MyConf->Enumerate("dnsbl"); i++)
198 DNSBLConfEntry *e = new DNSBLConfEntry();
200 e->name = MyConf->ReadValue("dnsbl", "name", i);
201 e->reason = MyConf->ReadValue("dnsbl", "reason", i);
202 e->domain = MyConf->ReadValue("dnsbl", "domain", i);
203 e->banaction = str2banaction(MyConf->ReadValue("dnsbl", "action", i));
204 e->duration = ServerInstance->Duration(MyConf->ReadValue("dnsbl", "duration", i).c_str());
205 e->bitmask = MyConf->ReadInteger("dnsbl", "bitmask", i, false);
207 /* yeah, logic here is a little messy */
210 ServerInstance->WriteOpers("*** DNSBL(#%d): invalid bitmask",i);
212 else if (e->name == "")
214 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid name",i);
216 else if (e->domain == "")
218 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid domain",i);
220 else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
222 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid banaction", i);
228 ServerInstance->WriteOpers("*** DNSBL(#%d): empty reason, using defaults",i);
229 e->reason = "Your IP has been blacklisted.";
232 /* add it, all is ok */
233 DNSBLConfEntries.push_back(e);
237 /* delete and drop it, error somewhere */
244 virtual void OnRehash(userrec* user, const std::string ¶meter)
250 * We will check each user that connects *locally* (userrec::fd>0)
252 virtual int OnUserRegister(userrec* user)
256 /* following code taken from bopm, reverses an IP address. */
258 unsigned char a, b, c, d;
259 char reversedipbuf[128];
260 std::string reversedip;
261 bool success = false;
263 if (!inet_aton(user->GetIPString(), &in))
266 /* We could have an ipv6 address here */
267 std::string x = user->GetIPString();
268 /* Is it a 4in6 address? (Compensate for this kernel kludge that people love) */
269 if (x.find("0::ffff:") == 0)
271 x.erase(x.begin(), x.begin() + 8);
272 if (inet_aton(x.c_str(), &in))
285 d = (unsigned char) (in.s_addr >> 24) & 0xFF;
286 c = (unsigned char) (in.s_addr >> 16) & 0xFF;
287 b = (unsigned char) (in.s_addr >> 8) & 0xFF;
288 a = (unsigned char) in.s_addr & 0xFF;
290 snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
291 reversedip = std::string(reversedipbuf);
293 // For each DNSBL, we will run through this lookup
294 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
296 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
297 std::string hostname = reversedip + "." + (*i)->domain;
299 /* now we'd need to fire off lookups for `hostname'. */
301 DNSBLResolver *r = new DNSBLResolver(this, ServerInstance, hostname, user, user->GetFd(), *i, cached);
302 ServerInstance->AddResolver(r, cached);
306 /* don't do anything with this hot potato */
311 // stuff down here is the module-factory stuff.
313 class ModuleDNSBLFactory : public ModuleFactory
320 ~ModuleDNSBLFactory()
324 virtual Module *CreateModule(InspIRCd *Me)
326 return new ModuleDNSBL(Me);
332 extern "C" void * init_module( void )
334 return new ModuleDNSBLFactory;