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