1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
24 /* $ModDesc: Provides handling of DNS blacklists */
26 /* Class holding data for a single entry */
27 class DNSBLConfEntry : public classbase
30 enum EnumBanaction { I_UNKNOWN, I_KILL, I_ZLINE, I_KLINE, I_GLINE };
31 enum EnumType { A_RECORD, A_BITMASK };
32 std::string name, domain, reason;
33 EnumBanaction banaction;
37 unsigned char records[256];
38 unsigned long stats_hits, stats_misses;
39 DNSBLConfEntry(): type(A_BITMASK),duration(86400),bitmask(0),stats_hits(0), stats_misses(0) {}
44 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
46 class DNSBLResolver : public Resolver
50 DNSBLConfEntry *ConfEntry;
54 DNSBLResolver(Module *me, InspIRCd *Instance, const std::string &hostname, User* u, int userfd, DNSBLConfEntry *conf, bool &cached)
55 : Resolver(Instance, hostname, DNS_QUERY_A, cached, me)
62 /* Note: This may be called multiple times for multiple A record results */
63 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached, int resultnum = 0)
65 /* for bitmask reply types, we arent interested in any but the first result (number 0) */
66 if ((ConfEntry->type == DNSBLConfEntry::A_BITMASK) && (resultnum))
69 /* Check the user still exists */
70 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
72 // Now we calculate the bitmask: 256*(256*(256*a+b)+c)+d
75 unsigned int bitmask = 0, record = 0;
79 inet_aton(result.c_str(), &resultip);
81 switch (ConfEntry->type)
83 case DNSBLConfEntry::A_BITMASK:
84 bitmask = resultip.s_addr >> 24; /* Last octet (network byte order) */
85 bitmask &= ConfEntry->bitmask;
86 match = (bitmask != 0);
88 case DNSBLConfEntry::A_RECORD:
89 record = resultip.s_addr >> 24; /* Last octet */
90 match = (ConfEntry->records[record] == 1);
96 std::string reason = ConfEntry->reason;
97 std::string::size_type x = reason.find("%ip%");
98 while (x != std::string::npos)
101 reason.insert(x, them->GetIPString());
102 x = reason.find("%ip%");
105 ConfEntry->stats_hits++;
107 switch (ConfEntry->banaction)
109 case DNSBLConfEntry::I_KILL:
111 ServerInstance->Users->QuitUser(them, std::string("Killed (") + reason + ")");
114 case DNSBLConfEntry::I_KLINE:
116 KLine* kl = new KLine(ServerInstance, ServerInstance->Time(), ConfEntry->duration, ServerInstance->Config->ServerName, reason.c_str(),
117 "*", them->GetIPString());
118 if (ServerInstance->XLines->AddLine(kl,NULL))
120 ServerInstance->XLines->ApplyLines();
126 case DNSBLConfEntry::I_GLINE:
128 GLine* gl = new GLine(ServerInstance, ServerInstance->Time(), ConfEntry->duration, ServerInstance->Config->ServerName, reason.c_str(),
129 "*", them->GetIPString());
130 if (ServerInstance->XLines->AddLine(gl,NULL))
132 ServerInstance->XLines->ApplyLines();
138 case DNSBLConfEntry::I_ZLINE:
140 ZLine* zl = new ZLine(ServerInstance, ServerInstance->Time(), ConfEntry->duration, ServerInstance->Config->ServerName, reason.c_str(),
141 them->GetIPString());
142 if (ServerInstance->XLines->AddLine(zl,NULL))
144 ServerInstance->XLines->ApplyLines();
150 case DNSBLConfEntry::I_UNKNOWN:
157 ServerInstance->SNO->WriteToSnoMask('A', "Connecting user %s detected as being on a DNS blacklist (%s) with result %d", them->GetFullRealHost().c_str(), ConfEntry->name.c_str(), bitmask);
160 ConfEntry->stats_misses++;
163 ConfEntry->stats_misses++;
167 virtual void OnError(ResolverError e, const std::string &errormessage)
171 virtual ~DNSBLResolver()
176 class ModuleDNSBL : public Module
179 std::vector<DNSBLConfEntry *> DNSBLConfEntries;
182 * Convert a string to EnumBanaction
184 DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
186 if(action.compare("KILL")==0)
187 return DNSBLConfEntry::I_KILL;
188 if(action.compare("KLINE")==0)
189 return DNSBLConfEntry::I_KLINE;
190 if(action.compare("ZLINE")==0)
191 return DNSBLConfEntry::I_ZLINE;
192 if(action.compare("GLINE")==0)
193 return DNSBLConfEntry::I_GLINE;
195 return DNSBLConfEntry::I_UNKNOWN;
198 ModuleDNSBL(InspIRCd *Me) : Module(Me)
201 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnStats };
202 ServerInstance->Modules->Attach(eventlist, this, 3);
205 virtual ~ModuleDNSBL()
210 virtual Version GetVersion()
212 return Version("$Id$", VF_VENDOR, API_VERSION);
216 /** Clear entries and free the mem it was using
220 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
222 DNSBLConfEntries.clear();
225 /** Fill our conf vector with data
227 virtual void ReadConf()
229 ConfigReader *MyConf = new ConfigReader(ServerInstance);
232 for (int i=0; i< MyConf->Enumerate("dnsbl"); i++)
234 DNSBLConfEntry *e = new DNSBLConfEntry();
236 e->name = MyConf->ReadValue("dnsbl", "name", i);
237 e->reason = MyConf->ReadValue("dnsbl", "reason", i);
238 e->domain = MyConf->ReadValue("dnsbl", "domain", i);
240 if (MyConf->ReadValue("dnsbl", "type", i) == "bitmask")
242 e->type = DNSBLConfEntry::A_BITMASK;
243 e->bitmask = MyConf->ReadInteger("dnsbl", "bitmask", i, false);
247 memset(e->records, 0, sizeof(e->records));
248 e->type = DNSBLConfEntry::A_RECORD;
249 irc::portparser portrange(MyConf->ReadValue("dnsbl", "records", i), false);
251 while ((item = portrange.GetToken()))
252 e->records[item] = 1;
255 e->banaction = str2banaction(MyConf->ReadValue("dnsbl", "action", i));
256 e->duration = ServerInstance->Duration(MyConf->ReadValue("dnsbl", "duration", i));
258 /* Use portparser for record replies */
260 /* yeah, logic here is a little messy */
261 if ((e->bitmask <= 0) && (DNSBLConfEntry::A_BITMASK == e->type))
263 ServerInstance->SNO->WriteToSnoMask('A', "DNSBL(#%d): invalid bitmask",i);
265 else if (e->name.empty())
267 ServerInstance->SNO->WriteToSnoMask('A', "DNSBL(#%d): Invalid name",i);
269 else if (e->domain.empty())
271 ServerInstance->SNO->WriteToSnoMask('A', "DNSBL(#%d): Invalid domain",i);
273 else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
275 ServerInstance->SNO->WriteToSnoMask('A', "DNSBL(#%d): Invalid banaction", i);
279 if (e->reason.empty())
281 ServerInstance->SNO->WriteToSnoMask('A', "DNSBL(#%d): empty reason, using defaults",i);
282 e->reason = "Your IP has been blacklisted.";
285 /* add it, all is ok */
286 DNSBLConfEntries.push_back(e);
290 /* delete and drop it, error somewhere */
297 virtual void OnRehash(User* user, const std::string ¶meter)
302 virtual int OnUserRegister(User* user)
304 /* only do lookups on local users */
307 /* following code taken from bopm, reverses an IP address. */
309 unsigned char a, b, c, d;
310 char reversedipbuf[128];
311 std::string reversedip;
314 success = inet_aton(user->GetIPString(), &in);
319 d = (unsigned char) (in.s_addr >> 24) & 0xFF;
320 c = (unsigned char) (in.s_addr >> 16) & 0xFF;
321 b = (unsigned char) (in.s_addr >> 8) & 0xFF;
322 a = (unsigned char) in.s_addr & 0xFF;
324 snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
325 reversedip = std::string(reversedipbuf);
327 // For each DNSBL, we will run through this lookup
328 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
330 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
331 std::string hostname = reversedip + "." + (*i)->domain;
333 /* now we'd need to fire off lookups for `hostname'. */
335 DNSBLResolver *r = new DNSBLResolver(this, ServerInstance, hostname, user, user->GetFd(), *i, cached);
336 ServerInstance->AddResolver(r, cached);
340 /* don't do anything with this hot potato */
344 virtual int OnStats(char symbol, User* user, string_list &results)
349 unsigned long total_hits = 0, total_misses = 0;
351 for (std::vector<DNSBLConfEntry*>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
353 total_hits += (*i)->stats_hits;
354 total_misses += (*i)->stats_misses;
356 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS DNSbl \"" + (*i)->name + "\" had " +
357 ConvToStr((*i)->stats_hits) + " hits and " + ConvToStr((*i)->stats_misses) + " misses");
360 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total hits: " + ConvToStr(total_hits));
361 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total misses: " + ConvToStr(total_misses));
367 MODULE_INIT(ModuleDNSBL)