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());
117 case DNSBLConfEntry::I_GLINE:
119 ServerInstance->AddGLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, std::string("*@") + them->GetIPString());
122 case DNSBLConfEntry::I_ZLINE:
124 ServerInstance->AddZLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, them->GetIPString());
127 case DNSBLConfEntry::I_UNKNOWN:
138 virtual void OnError(ResolverError e, const std::string &errormessage)
142 virtual ~DNSBLResolver()
147 class ModuleDNSBL : public Module
150 std::vector<DNSBLConfEntry *> DNSBLConfEntries;
153 * Convert a string to EnumBanaction
155 DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
157 if(action.compare("KILL")==0)
158 return DNSBLConfEntry::I_KILL;
159 if(action.compare("KLINE")==0)
160 return DNSBLConfEntry::I_KLINE;
161 if(action.compare("ZLINE")==0)
162 return DNSBLConfEntry::I_ZLINE;
163 if(action.compare("GLINE")==0)
164 return DNSBLConfEntry::I_GLINE;
166 return DNSBLConfEntry::I_UNKNOWN;
169 ModuleDNSBL(InspIRCd *Me) : Module::Module(Me)
174 virtual ~ModuleDNSBL()
179 virtual Version GetVersion()
181 return Version(2, 0, 0, 1, VF_VENDOR, API_VERSION);
184 void Implements(char* List)
186 List[I_OnRehash] = List[I_OnUserRegister] = 1;
189 /** Clear entries and free the mem it was using
193 std::vector<DNSBLConfEntry *>::iterator i;
194 while ((i = DNSBLConfEntries.begin()) != DNSBLConfEntries.end())
196 DNSBLConfEntries.erase(i);
201 /** Fill our conf vector with data
203 virtual void ReadConf()
205 ConfigReader *MyConf = new ConfigReader(ServerInstance);
208 for (int i=0; i< MyConf->Enumerate("dnsbl"); i++)
210 DNSBLConfEntry *e = new DNSBLConfEntry();
212 e->name = MyConf->ReadValue("dnsbl", "name", i);
213 e->reason = MyConf->ReadValue("dnsbl", "reason", i);
214 e->domain = MyConf->ReadValue("dnsbl", "domain", i);
215 e->banaction = str2banaction(MyConf->ReadValue("dnsbl", "action", i));
216 e->duration = ServerInstance->Duration(MyConf->ReadValue("dnsbl", "duration", i).c_str());
217 e->bitmask = MyConf->ReadInteger("dnsbl", "bitmask", i, false);
219 /* yeah, logic here is a little messy */
222 ServerInstance->WriteOpers("*** DNSBL(#%d): invalid bitmask",i);
224 else if (e->name == "")
226 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid name",i);
228 else if (e->domain == "")
230 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid domain",i);
232 else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
234 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid banaction", i);
240 ServerInstance->WriteOpers("*** DNSBL(#%d): empty reason, using defaults",i);
241 e->reason = "Your IP has been blacklisted.";
244 /* add it, all is ok */
245 DNSBLConfEntries.push_back(e);
249 /* delete and drop it, error somewhere */
256 virtual void OnRehash(userrec* user, const std::string ¶meter)
262 * We will check each user that connects *locally* (userrec::fd>0)
264 virtual int OnUserRegister(userrec* user)
268 /* following code taken from bopm, reverses an IP address. */
270 unsigned char a, b, c, d;
271 char reversedipbuf[128];
272 std::string reversedip;
274 if (!inet_aton(user->GetIPString(), &in))
276 ServerInstance->WriteOpers("Invalid IP address in m_dnsbl! Bailing check");
280 d = (unsigned char) (in.s_addr >> 24) & 0xFF;
281 c = (unsigned char) (in.s_addr >> 16) & 0xFF;
282 b = (unsigned char) (in.s_addr >> 8) & 0xFF;
283 a = (unsigned char) in.s_addr & 0xFF;
285 snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
286 reversedip = std::string(reversedipbuf);
288 // For each DNSBL, we will run through this lookup
289 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
291 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
292 std::string hostname=reversedip+"."+ (*i)->domain;
294 /* now we'd need to fire off lookups for `hostname'. */
296 DNSBLResolver *r = new DNSBLResolver(this, ServerInstance, hostname, user, user->GetFd(), *i, cached);
297 ServerInstance->AddResolver(r, cached);
301 /* don't do anything with this hot potato */
306 // stuff down here is the module-factory stuff.
308 class ModuleDNSBLFactory : public ModuleFactory
315 ~ModuleDNSBLFactory()
319 virtual Module *CreateModule(InspIRCd *Me)
321 return new ModuleDNSBL(Me);
327 extern "C" void * init_module( void )
329 return new ModuleDNSBLFactory;