]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_dnsbl.cpp
m_dnsbl stats should count no result as a miss
[user/henk/code/inspircd.git] / src / modules / m_dnsbl.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
18 #include <stdint.h>
19 #include "inspircd.h"
20 #include "dns.h"
21 #include "users.h"
22 #include "channels.h"
23 #include "modules.h"
24
25 /* $ModDesc: Provides handling of DNS blacklists */
26
27 /* Class holding data for a single entry */
28 class DNSBLConfEntry
29 {
30         public:
31                 enum EnumBanaction { I_UNKNOWN, I_KILL, I_ZLINE, I_KLINE, I_GLINE };
32                 std::string name, domain, reason;
33                 EnumBanaction banaction;
34                 long duration;
35                 int bitmask;
36                 unsigned long stats_hits, stats_misses;
37                 DNSBLConfEntry(): duration(86400),bitmask(0),stats_hits(0), stats_misses(0) {}
38                 ~DNSBLConfEntry() { }
39 };
40
41
42 /** Resolver for CGI:IRC hostnames encoded in ident/GECOS
43  */
44 class DNSBLResolver : public Resolver
45 {
46         int theirfd;
47         userrec* them;
48         DNSBLConfEntry *ConfEntry;
49
50     public:
51         DNSBLResolver(Module *me, InspIRCd *ServerInstance, const std::string &hostname, userrec* u, int userfd, DNSBLConfEntry *conf, bool &cached)
52                 : Resolver(ServerInstance, hostname, DNS_QUERY_A, cached, me)
53         {
54                 theirfd = userfd;
55                 them = u;
56                 ConfEntry = conf;
57         }
58
59         virtual void OnLookupComplete(const std::string &result, unsigned int ttl, bool cached)
60         {
61                 /* Check the user still exists */
62                 if ((them) && (them == ServerInstance->SE->GetRef(theirfd)))
63                 {
64                         // Now we calculate the bitmask: 256*(256*(256*a+b)+c)+d
65                         if(result.length())
66                         {
67                                 unsigned int bitmask=0;
68                                 in_addr resultip;
69
70                                 /* Convert the result to an in_addr (we can gaurantee we got ipv4)
71                                  * Whoever did the loop that was here before, I AM CONFISCATING
72                                  * YOUR CRACKPIPE. you know who you are. -- Brain
73                                  */
74                                 inet_aton(result.c_str(), &resultip);
75                                 bitmask = resultip.s_addr >> 24; /* Last octet (network byte order */
76
77                                 bitmask &= ConfEntry->bitmask;
78
79                                 if (bitmask != 0)
80                                 {
81                                         std::string reason = ConfEntry->reason;
82                                         std::string::size_type x = reason.find("%ip%");
83                                         while (x != std::string::npos)
84                                         {
85                                                 reason.erase(x, 4);
86                                                 reason.insert(x, them->GetIPString());
87                                                 x = reason.find("%ip%");
88                                         }
89
90                                         ServerInstance->WriteOpers("*** Connecting user %s detected as being on a DNS blacklist (%s) with result %d", them->GetFullRealHost(), ConfEntry->name.c_str(), bitmask);
91                                         ConfEntry->stats_hits++;
92
93                                         switch (ConfEntry->banaction)
94                                         {
95                                                 case DNSBLConfEntry::I_KILL:
96                                                 {
97                                                         userrec::QuitUser(ServerInstance, them, std::string("Killed (") + reason + ")");
98                                                         break;
99                                                 }
100                                                 case DNSBLConfEntry::I_KLINE:
101                                                 {
102                                                         ServerInstance->AddKLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, std::string("*@") + them->GetIPString());
103                                                         FOREACH_MOD(I_OnAddKLine,OnAddKLine(ConfEntry->duration, NULL, reason, std::string("*@") + them->GetIPString()));
104                                                         break;
105                                                 }
106                                                 case DNSBLConfEntry::I_GLINE:
107                                                 {
108                                                         ServerInstance->AddGLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, std::string("*@") + them->GetIPString());
109                                                         FOREACH_MOD(I_OnAddGLine,OnAddGLine(ConfEntry->duration, NULL, reason, std::string("*@") + them->GetIPString()));
110                                                         break;
111                                                 }
112                                                 case DNSBLConfEntry::I_ZLINE:
113                                                 {
114                                                         ServerInstance->AddZLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, them->GetIPString());
115                                                         FOREACH_MOD(I_OnAddZLine,OnAddZLine(ConfEntry->duration, NULL, reason, them->GetIPString()));
116                                                         break;
117                                                 }
118                                                 case DNSBLConfEntry::I_UNKNOWN:
119                                                 {
120                                                         break;
121                                                 }
122                                                 break;
123                                         }
124                                 }
125                                 else
126                                         ConfEntry->stats_misses++;
127                         }
128                         else
129                                 ConfEntry->stats_misses++;
130                 }
131         }
132
133         virtual void OnError(ResolverError e, const std::string &errormessage)
134         {
135         }
136
137         virtual ~DNSBLResolver()
138         {
139         }
140 };
141
142 class ModuleDNSBL : public Module
143 {
144  private:
145         std::vector<DNSBLConfEntry *> DNSBLConfEntries;
146
147         /*
148          *      Convert a string to EnumBanaction
149          */
150         DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
151         {
152                 if(action.compare("KILL")==0)
153                         return DNSBLConfEntry::I_KILL;
154                 if(action.compare("KLINE")==0)
155                         return DNSBLConfEntry::I_KLINE;
156                 if(action.compare("ZLINE")==0)
157                         return DNSBLConfEntry::I_ZLINE;
158                 if(action.compare("GLINE")==0)
159                         return DNSBLConfEntry::I_GLINE;
160
161                 return DNSBLConfEntry::I_UNKNOWN;
162         }
163  public:
164         ModuleDNSBL(InspIRCd *Me) : Module::Module(Me)
165         {
166                 ReadConf();
167         }
168
169         virtual ~ModuleDNSBL()
170         {
171                 ClearEntries();
172         }
173
174         virtual Version GetVersion()
175         {
176                 return Version(2, 0, 0, 1, VF_VENDOR, API_VERSION);
177         }
178
179         void Implements(char* List)
180         {
181                 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnStats] = 1;
182         }
183
184         /** Clear entries and free the mem it was using
185          */
186         void ClearEntries()
187         {
188                 std::vector<DNSBLConfEntry *>::iterator i;
189                 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
190                         delete *i;
191                 DNSBLConfEntries.clear();
192         }
193
194         /** Fill our conf vector with data
195          */
196         virtual void ReadConf()
197         {
198                 ConfigReader *MyConf = new ConfigReader(ServerInstance);
199                 ClearEntries();
200
201                 for (int i=0; i< MyConf->Enumerate("dnsbl"); i++)
202                 {
203                         DNSBLConfEntry *e = new DNSBLConfEntry();
204
205                         e->name = MyConf->ReadValue("dnsbl", "name", i);
206                         e->reason = MyConf->ReadValue("dnsbl", "reason", i);
207                         e->domain = MyConf->ReadValue("dnsbl", "domain", i);
208                         e->banaction = str2banaction(MyConf->ReadValue("dnsbl", "action", i));
209                         e->duration = ServerInstance->Duration(MyConf->ReadValue("dnsbl", "duration", i).c_str());
210                         e->bitmask = MyConf->ReadInteger("dnsbl", "bitmask", i, false);
211
212                         /* yeah, logic here is a little messy */
213                         if (e->bitmask <= 0)
214                         {
215                                 ServerInstance->WriteOpers("*** DNSBL(#%d): invalid bitmask",i);
216                         }
217                         else if (e->name == "")
218                         {
219                                 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid name",i);
220                         }
221                         else if (e->domain == "")
222                         {
223                                 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid domain",i);
224                         }
225                         else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
226                         {
227                                 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid banaction", i);
228                         }
229                         else
230                         {
231                                 if (e->reason == "")
232                                 {
233                                         ServerInstance->WriteOpers("*** DNSBL(#%d): empty reason, using defaults",i);
234                                         e->reason = "Your IP has been blacklisted.";
235                                 }
236
237                                 /* add it, all is ok */
238                                 DNSBLConfEntries.push_back(e);
239                                 continue;
240                         }
241
242                         /* delete and drop it, error somewhere */
243                         delete e;
244                 }
245
246                 delete MyConf;
247         }
248
249         virtual void OnRehash(userrec* user, const std::string &parameter)
250         {
251                 ReadConf();
252         }
253
254         /*
255          * We will check each user that connects *locally* (userrec::fd>0)
256          */
257         virtual int OnUserRegister(userrec* user)
258         {
259                 if (IS_LOCAL(user))
260                 {
261                         /* following code taken from bopm, reverses an IP address. */
262                         struct in_addr in;
263                         unsigned char a, b, c, d;
264                         char reversedipbuf[128];
265                         std::string reversedip;
266                         bool success = false;
267
268                         if (!inet_aton(user->GetIPString(), &in))
269                         {
270 #ifdef IPV6
271                                 /* We could have an ipv6 address here */
272                                 std::string x = user->GetIPString();
273                                 /* Is it a 4in6 address? (Compensate for this kernel kludge that people love) */
274                                 if (x.find("0::ffff:") == 0)
275                                 {
276                                         x.erase(x.begin(), x.begin() + 8);
277                                         if (inet_aton(x.c_str(), &in))
278                                                 success = true;
279                                 }
280 #endif
281                         }
282                         else
283                         {
284                                 success = true;
285                         }
286
287                         if (!success)
288                                 return 0;
289
290                         d = (unsigned char) (in.s_addr >> 24) & 0xFF;
291                         c = (unsigned char) (in.s_addr >> 16) & 0xFF;
292                         b = (unsigned char) (in.s_addr >> 8) & 0xFF;
293                         a = (unsigned char) in.s_addr & 0xFF;
294
295                         snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
296                         reversedip = std::string(reversedipbuf);
297
298                         // For each DNSBL, we will run through this lookup
299                         for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
300                         {
301                                 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
302                                 std::string hostname = reversedip + "." + (*i)->domain;
303
304                                 /* now we'd need to fire off lookups for `hostname'. */
305                                 bool cached;
306                                 DNSBLResolver *r = new DNSBLResolver(this, ServerInstance, hostname, user, user->GetFd(), *i, cached);
307                                 ServerInstance->AddResolver(r, cached);
308                         }
309                 }
310
311                 /* don't do anything with this hot potato */
312                 return 0;
313         }
314         
315         virtual int OnStats(char symbol, userrec* user, string_list &results)
316         {
317                 if (symbol != 'd')
318                         return 0;
319                 
320                 unsigned long total_hits = 0, total_misses = 0;
321
322                 for (std::vector<DNSBLConfEntry*>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
323                 {
324                         total_hits += (*i)->stats_hits;
325                         total_misses += (*i)->stats_misses;
326                         
327                         results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS DNSbl \"" + (*i)->name + "\" had " +
328                                         ConvToStr((*i)->stats_hits) + " hits and " + ConvToStr((*i)->stats_misses) + " misses");
329                 }
330                 
331                 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total hits: " + ConvToStr(total_hits));
332                 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total misses: " + ConvToStr(total_misses));
333                 
334                 return 0;
335         }
336 };
337
338 // stuff down here is the module-factory stuff.
339
340 class ModuleDNSBLFactory : public ModuleFactory
341 {
342  public:
343         ModuleDNSBLFactory()
344         {
345         }
346
347         ~ModuleDNSBLFactory()
348         {
349         }
350
351         virtual Module *CreateModule(InspIRCd *Me)
352         {
353                 return new ModuleDNSBL(Me);
354         }
355
356 };
357
358
359 extern "C" void * init_module( void )
360 {
361         return new ModuleDNSBLFactory;
362 }