1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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 */
30 enum EnumBanaction { I_UNKNOWN, I_KILL, I_ZLINE, I_KLINE, I_GLINE, I_MARK };
31 enum EnumType { A_RECORD, A_BITMASK };
32 std::string name, ident, host, 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
49 DNSBLConfEntry *ConfEntry;
53 DNSBLResolver(Module *me, const std::string &hostname, LocalUser* u, DNSBLConfEntry *conf, bool &cached)
54 : Resolver(hostname, DNS_QUERY_A, cached, me)
60 /* Note: This may be called multiple times for multiple A record results */
61 virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
63 /* Check the user still exists */
64 User* them = ServerInstance->FindUUID(theiruid);
67 // Now we calculate the bitmask: 256*(256*(256*a+b)+c)+d
70 unsigned int bitmask = 0, record = 0;
74 inet_aton(result.c_str(), &resultip);
76 switch (ConfEntry->type)
78 case DNSBLConfEntry::A_BITMASK:
79 bitmask = resultip.s_addr >> 24; /* Last octet (network byte order) */
80 bitmask &= ConfEntry->bitmask;
81 match = (bitmask != 0);
83 case DNSBLConfEntry::A_RECORD:
84 record = resultip.s_addr >> 24; /* Last octet */
85 match = (ConfEntry->records[record] == 1);
91 std::string reason = ConfEntry->reason;
92 std::string::size_type x = reason.find("%ip%");
93 while (x != std::string::npos)
96 reason.insert(x, them->GetIPString());
97 x = reason.find("%ip%");
100 ConfEntry->stats_hits++;
102 switch (ConfEntry->banaction)
104 case DNSBLConfEntry::I_KILL:
106 ServerInstance->Users->QuitUser(them, std::string("Killed (") + reason + ")");
109 case DNSBLConfEntry::I_MARK:
111 if (!ConfEntry->ident.empty())
113 them->WriteServ("304 " + them->nick + " :Your ident has been set to " + ConfEntry->ident + " because you matched " + reason);
114 them->ChangeIdent(ConfEntry->ident.c_str());
117 if (!ConfEntry->host.empty())
119 them->WriteServ("304 " + them->nick + " :Your host has been set to " + ConfEntry->host + " because you matched " + reason);
120 them->ChangeDisplayedHost(ConfEntry->host.c_str());
125 case DNSBLConfEntry::I_KLINE:
127 KLine* kl = new KLine(ServerInstance->Time(), ConfEntry->duration, ServerInstance->Config->ServerName.c_str(), reason.c_str(),
128 "*", them->GetIPString());
129 if (ServerInstance->XLines->AddLine(kl,NULL))
131 ServerInstance->SNO->WriteGlobalSno('x',"K:line added due to DNSBL match on *@%s to expire on %s: %s",
132 them->GetIPString(), ServerInstance->TimeString(kl->expiry).c_str(), reason.c_str());
133 ServerInstance->XLines->ApplyLines();
139 case DNSBLConfEntry::I_GLINE:
141 GLine* gl = new GLine(ServerInstance->Time(), ConfEntry->duration, ServerInstance->Config->ServerName.c_str(), reason.c_str(),
142 "*", them->GetIPString());
143 if (ServerInstance->XLines->AddLine(gl,NULL))
145 ServerInstance->SNO->WriteGlobalSno('x',"G:line added due to DNSBL match on *@%s to expire on %s: %s",
146 them->GetIPString(), ServerInstance->TimeString(gl->expiry).c_str(), reason.c_str());
147 ServerInstance->XLines->ApplyLines();
153 case DNSBLConfEntry::I_ZLINE:
155 ZLine* zl = new ZLine(ServerInstance->Time(), ConfEntry->duration, ServerInstance->Config->ServerName.c_str(), reason.c_str(),
156 them->GetIPString());
157 if (ServerInstance->XLines->AddLine(zl,NULL))
159 ServerInstance->SNO->WriteGlobalSno('x',"Z:line added due to DNSBL match on *@%s to expire on %s: %s",
160 them->GetIPString(), ServerInstance->TimeString(zl->expiry).c_str(), reason.c_str());
161 ServerInstance->XLines->ApplyLines();
167 case DNSBLConfEntry::I_UNKNOWN:
174 ServerInstance->SNO->WriteGlobalSno('a', "Connecting user %s detected as being on a DNS blacklist (%s) with result %d", them->GetFullRealHost().c_str(), ConfEntry->domain.c_str(), (ConfEntry->type==DNSBLConfEntry::A_BITMASK) ? bitmask : record);
177 ConfEntry->stats_misses++;
180 ConfEntry->stats_misses++;
184 virtual void OnError(ResolverError e, const std::string &errormessage)
188 virtual ~DNSBLResolver()
193 class ModuleDNSBL : public Module
196 std::vector<DNSBLConfEntry *> DNSBLConfEntries;
199 * Convert a string to EnumBanaction
201 DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
203 if(action.compare("KILL")==0)
204 return DNSBLConfEntry::I_KILL;
205 if(action.compare("KLINE")==0)
206 return DNSBLConfEntry::I_KLINE;
207 if(action.compare("ZLINE")==0)
208 return DNSBLConfEntry::I_ZLINE;
209 if(action.compare("GLINE")==0)
210 return DNSBLConfEntry::I_GLINE;
211 if(action.compare("MARK")==0)
212 return DNSBLConfEntry::I_MARK;
214 return DNSBLConfEntry::I_UNKNOWN;
219 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister, I_OnStats };
220 ServerInstance->Modules->Attach(eventlist, this, 3);
223 virtual ~ModuleDNSBL()
228 virtual Version GetVersion()
230 return Version("Provides handling of DNS blacklists", VF_VENDOR);
234 /** Clear entries and free the mem it was using
238 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
240 DNSBLConfEntries.clear();
243 /** Fill our conf vector with data
245 virtual void ReadConf()
250 for (int i=0; i< MyConf.Enumerate("dnsbl"); i++)
252 DNSBLConfEntry *e = new DNSBLConfEntry();
254 e->name = MyConf.ReadValue("dnsbl", "name", i);
255 e->ident = MyConf.ReadValue("dnsbl", "ident", i);
256 e->host = MyConf.ReadValue("dnsbl", "host", i);
257 e->reason = MyConf.ReadValue("dnsbl", "reason", i);
258 e->domain = MyConf.ReadValue("dnsbl", "domain", i);
260 if (MyConf.ReadValue("dnsbl", "type", i) == "bitmask")
262 e->type = DNSBLConfEntry::A_BITMASK;
263 e->bitmask = MyConf.ReadInteger("dnsbl", "bitmask", i, false);
267 memset(e->records, 0, sizeof(e->records));
268 e->type = DNSBLConfEntry::A_RECORD;
269 irc::portparser portrange(MyConf.ReadValue("dnsbl", "records", i), false);
271 while ((item = portrange.GetToken()))
272 e->records[item] = 1;
275 e->banaction = str2banaction(MyConf.ReadValue("dnsbl", "action", i));
276 e->duration = ServerInstance->Duration(MyConf.ReadValue("dnsbl", "duration", "60", i));
278 /* Use portparser for record replies */
280 /* yeah, logic here is a little messy */
281 if ((e->bitmask <= 0) && (DNSBLConfEntry::A_BITMASK == e->type))
283 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): invalid bitmask",i);
285 else if (e->name.empty())
287 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): Invalid name",i);
289 else if (e->domain.empty())
291 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): Invalid domain",i);
293 else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
295 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): Invalid banaction", i);
297 else if (e->duration <= 0)
299 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): Invalid duration", i);
303 if (e->reason.empty())
305 ServerInstance->SNO->WriteGlobalSno('a', "DNSBL(#%d): empty reason, using defaults",i);
306 e->reason = "Your IP has been blacklisted.";
309 /* add it, all is ok */
310 DNSBLConfEntries.push_back(e);
314 /* delete and drop it, error somewhere */
319 virtual void OnRehash(User* user)
324 virtual ModResult OnUserRegister(LocalUser* user)
326 /* following code taken from bopm, reverses an IP address. */
328 unsigned char a, b, c, d;
329 char reversedipbuf[128];
330 std::string reversedip;
333 success = inet_aton(user->GetIPString(), &in);
336 return MOD_RES_PASSTHRU;
338 d = (unsigned char) (in.s_addr >> 24) & 0xFF;
339 c = (unsigned char) (in.s_addr >> 16) & 0xFF;
340 b = (unsigned char) (in.s_addr >> 8) & 0xFF;
341 a = (unsigned char) in.s_addr & 0xFF;
343 snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
344 reversedip = std::string(reversedipbuf);
346 // For each DNSBL, we will run through this lookup
347 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
349 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
350 std::string hostname = reversedip + "." + (*i)->domain;
352 /* now we'd need to fire off lookups for `hostname'. */
354 DNSBLResolver *r = new DNSBLResolver(this, hostname, user, *i, cached);
355 ServerInstance->AddResolver(r, cached);
358 /* don't do anything with this hot potato */
359 return MOD_RES_PASSTHRU;
362 virtual ModResult OnStats(char symbol, User* user, string_list &results)
365 return MOD_RES_PASSTHRU;
367 unsigned long total_hits = 0, total_misses = 0;
369 for (std::vector<DNSBLConfEntry*>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
371 total_hits += (*i)->stats_hits;
372 total_misses += (*i)->stats_misses;
374 results.push_back(std::string(ServerInstance->Config->ServerName.c_str()) + " 304 " + user->nick + " :DNSBLSTATS DNSbl \"" + (*i)->name + "\" had " +
375 ConvToStr((*i)->stats_hits) + " hits and " + ConvToStr((*i)->stats_misses) + " misses");
378 results.push_back(std::string(ServerInstance->Config->ServerName.c_str()) + " 304 " + user->nick + " :DNSBLSTATS Total hits: " + ConvToStr(total_hits));
379 results.push_back(std::string(ServerInstance->Config->ServerName.c_str()) + " 304 " + user->nick + " :DNSBLSTATS Total misses: " + ConvToStr(total_misses));
381 return MOD_RES_PASSTHRU;
385 MODULE_INIT(ModuleDNSBL)