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)
50 : Resolver(ServerInstance, hostname, DNS_QUERY_A, me)
57 virtual void OnLookupComplete(const std::string &result)
59 /* Check the user still exists */
60 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
62 ServerInstance->Log(DEBUG, "m_dnsbl: %s got a result from dnsbl %s", them->nick, ConfEntry->name.c_str());
64 // Now we calculate the bitmask: 256*(256*(256*a+b)+c)+d
67 unsigned int bitmask=0;
68 unsigned int octetpos=0;
69 std::string tmp = result;
74 /* Fix by brain, npos is -1, so unsigned int will never match */
75 std::string::size_type lastdot = tmp.rfind(".");
77 if (lastdot == std::string::npos)
84 octet=tmp.substr(lastdot+1,tmp.length()-lastdot+1);
88 bitmask += (256 ^ octetpos) * atoi(octet.c_str());
92 bitmask &= ConfEntry->bitmask;
96 std::string reason = ConfEntry->reason;
98 while (int pos = reason.find("%ip%") != std::string::npos)
100 reason.replace(pos, 4, them->GetIPString());
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 them->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)
141 this just means they don't appear in the respective dnsbl
142 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
146 /* Check the user still exists */
147 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
149 ServerInstance->Log(DEBUG, "m_dnsbl: %s got an error while resolving for dnsbl %s", them->nick, ConfEntry->name.c_str());
153 virtual ~DNSBLResolver()
158 class ModuleDNSBL : public Module
161 std::vector<DNSBLConfEntry *> DNSBLConfEntries;
164 * Convert a string to EnumBanaction
166 DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
168 if(action.compare("KILL")==0)
169 return DNSBLConfEntry::I_KILL;
170 if(action.compare("KLINE")==0)
171 return DNSBLConfEntry::I_KLINE;
172 if(action.compare("ZLINE")==0)
173 return DNSBLConfEntry::I_ZLINE;
174 if(action.compare("GLINE")==0)
175 return DNSBLConfEntry::I_GLINE;
177 return DNSBLConfEntry::I_UNKNOWN;
180 ModuleDNSBL(InspIRCd *Me) : Module::Module(Me)
185 virtual ~ModuleDNSBL()
189 virtual Version GetVersion()
191 return Version(2, 0, 0, 0, 0, API_VERSION);
194 void Implements(char* List)
196 List[I_OnRehash] = List[I_OnUserRegister] = 1;
200 * Fill our conf vector with data
202 virtual void ReadConf()
204 ConfigReader *MyConf = new ConfigReader(ServerInstance);
205 DNSBLConfEntries.clear();
207 for (int i=0; i< MyConf->Enumerate("dnsbl"); i++)
209 DNSBLConfEntry *e = new DNSBLConfEntry();
211 e->name = MyConf->ReadValue("dnsbl", "name", i);
212 e->reason = MyConf->ReadValue("dnsbl", "reason", i);
213 e->domain = MyConf->ReadValue("dnsbl", "domain", i);
214 e->banaction = str2banaction(MyConf->ReadValue("dnsbl", "action", i));
215 e->duration = ServerInstance->Duration(MyConf->ReadValue("dnsbl", "duration", i).c_str());
216 e->bitmask = MyConf->ReadInteger("dnsbl", "bitmask", i, false);
218 /* yeah, logic here is a little messy */
221 ServerInstance->WriteOpers("*** DNSBL(#%d): invalid bitmask",i);
223 else if (e->name == "")
225 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid name",i);
227 else if (e->domain == "")
229 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid domain",i);
231 else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
233 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid banaction", i);
239 ServerInstance->WriteOpers("*** DNSBL(#%d): empty reason, using defaults",i);
240 e->reason = "Your IP has been blacklisted.";
243 /* add it, all is ok */
244 DNSBLConfEntries.push_back(e);
248 /* delete and drop it, error somewhere */
256 virtual void OnRehash(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);
289 this is satmd's old code
290 std::string reversedip;
291 std::string userip = user->GetIPString();
292 std::string tempip = userip;
294 // reversedip will created in there
295 while (tempip.length()>0)
297 unsigned int lastdot=tempip.rfind(".");
298 if (lastdot == std::string::npos)
305 reversedip += tempip.substr(lastdot+1,tempip.length()-lastdot+1);
307 tempip.resize(lastdot);
312 // For each DNSBL, we will run through this lookup
313 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
315 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
316 std::string hostname=reversedip+"."+ (*i)->domain;
318 ServerInstance->Log(DEBUG, "m_dnsbl: sending %s for resolution", hostname.c_str());
320 /* now we'd need to fire off lookups for `hostname'. */
321 DNSBLResolver *r = new DNSBLResolver(this, ServerInstance, hostname, user, user->GetFd(), *i);
322 ServerInstance->AddResolver(r);
326 /* don't do anything with this hot potato */
331 // stuff down here is the module-factory stuff.
333 class ModuleDNSBLFactory : public ModuleFactory
340 ~ModuleDNSBLFactory()
344 virtual Module *CreateModule(InspIRCd *Me)
346 return new ModuleDNSBL(Me);
352 extern "C" void * init_module( void )
354 return new ModuleDNSBLFactory;