]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_dnsbl.cpp
7f4f6ac88d7039af2d3ed686742fad5b77787bdb
[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                 }
129         }
130
131         virtual void OnError(ResolverError e, const std::string &errormessage)
132         {
133         }
134
135         virtual ~DNSBLResolver()
136         {
137         }
138 };
139
140 class ModuleDNSBL : public Module
141 {
142  private:
143         std::vector<DNSBLConfEntry *> DNSBLConfEntries;
144
145         /*
146          *      Convert a string to EnumBanaction
147          */
148         DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
149         {
150                 if(action.compare("KILL")==0)
151                         return DNSBLConfEntry::I_KILL;
152                 if(action.compare("KLINE")==0)
153                         return DNSBLConfEntry::I_KLINE;
154                 if(action.compare("ZLINE")==0)
155                         return DNSBLConfEntry::I_ZLINE;
156                 if(action.compare("GLINE")==0)
157                         return DNSBLConfEntry::I_GLINE;
158
159                 return DNSBLConfEntry::I_UNKNOWN;
160         }
161  public:
162         ModuleDNSBL(InspIRCd *Me) : Module::Module(Me)
163         {
164                 ReadConf();
165         }
166
167         virtual ~ModuleDNSBL()
168         {
169                 ClearEntries();
170         }
171
172         virtual Version GetVersion()
173         {
174                 return Version(2, 0, 0, 1, VF_VENDOR, API_VERSION);
175         }
176
177         void Implements(char* List)
178         {
179                 List[I_OnRehash] = List[I_OnUserRegister] = List[I_OnStats] = 1;
180         }
181
182         /** Clear entries and free the mem it was using
183          */
184         void ClearEntries()
185         {
186                 std::vector<DNSBLConfEntry *>::iterator i;
187                 for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
188                         delete *i;
189                 DNSBLConfEntries.clear();
190         }
191
192         /** Fill our conf vector with data
193          */
194         virtual void ReadConf()
195         {
196                 ConfigReader *MyConf = new ConfigReader(ServerInstance);
197                 ClearEntries();
198
199                 for (int i=0; i< MyConf->Enumerate("dnsbl"); i++)
200                 {
201                         DNSBLConfEntry *e = new DNSBLConfEntry();
202
203                         e->name = MyConf->ReadValue("dnsbl", "name", i);
204                         e->reason = MyConf->ReadValue("dnsbl", "reason", i);
205                         e->domain = MyConf->ReadValue("dnsbl", "domain", i);
206                         e->banaction = str2banaction(MyConf->ReadValue("dnsbl", "action", i));
207                         e->duration = ServerInstance->Duration(MyConf->ReadValue("dnsbl", "duration", i).c_str());
208                         e->bitmask = MyConf->ReadInteger("dnsbl", "bitmask", i, false);
209
210                         /* yeah, logic here is a little messy */
211                         if (e->bitmask <= 0)
212                         {
213                                 ServerInstance->WriteOpers("*** DNSBL(#%d): invalid bitmask",i);
214                         }
215                         else if (e->name == "")
216                         {
217                                 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid name",i);
218                         }
219                         else if (e->domain == "")
220                         {
221                                 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid domain",i);
222                         }
223                         else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
224                         {
225                                 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid banaction", i);
226                         }
227                         else
228                         {
229                                 if (e->reason == "")
230                                 {
231                                         ServerInstance->WriteOpers("*** DNSBL(#%d): empty reason, using defaults",i);
232                                         e->reason = "Your IP has been blacklisted.";
233                                 }
234
235                                 /* add it, all is ok */
236                                 DNSBLConfEntries.push_back(e);
237                                 continue;
238                         }
239
240                         /* delete and drop it, error somewhere */
241                         delete e;
242                 }
243
244                 delete MyConf;
245         }
246
247         virtual void OnRehash(userrec* user, const std::string &parameter)
248         {
249                 ReadConf();
250         }
251
252         /*
253          * We will check each user that connects *locally* (userrec::fd>0)
254          */
255         virtual int OnUserRegister(userrec* user)
256         {
257                 if (IS_LOCAL(user))
258                 {
259                         /* following code taken from bopm, reverses an IP address. */
260                         struct in_addr in;
261                         unsigned char a, b, c, d;
262                         char reversedipbuf[128];
263                         std::string reversedip;
264                         bool success = false;
265
266                         if (!inet_aton(user->GetIPString(), &in))
267                         {
268 #ifdef IPV6
269                                 /* We could have an ipv6 address here */
270                                 std::string x = user->GetIPString();
271                                 /* Is it a 4in6 address? (Compensate for this kernel kludge that people love) */
272                                 if (x.find("0::ffff:") == 0)
273                                 {
274                                         x.erase(x.begin(), x.begin() + 8);
275                                         if (inet_aton(x.c_str(), &in))
276                                                 success = true;
277                                 }
278 #endif
279                         }
280                         else
281                         {
282                                 success = true;
283                         }
284
285                         if (!success)
286                                 return 0;
287
288                         d = (unsigned char) (in.s_addr >> 24) & 0xFF;
289                         c = (unsigned char) (in.s_addr >> 16) & 0xFF;
290                         b = (unsigned char) (in.s_addr >> 8) & 0xFF;
291                         a = (unsigned char) in.s_addr & 0xFF;
292
293                         snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
294                         reversedip = std::string(reversedipbuf);
295
296                         // For each DNSBL, we will run through this lookup
297                         for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
298                         {
299                                 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
300                                 std::string hostname = reversedip + "." + (*i)->domain;
301
302                                 /* now we'd need to fire off lookups for `hostname'. */
303                                 bool cached;
304                                 DNSBLResolver *r = new DNSBLResolver(this, ServerInstance, hostname, user, user->GetFd(), *i, cached);
305                                 ServerInstance->AddResolver(r, cached);
306                         }
307                 }
308
309                 /* don't do anything with this hot potato */
310                 return 0;
311         }
312         
313         virtual int OnStats(char symbol, userrec* user, string_list &results)
314         {
315                 if (symbol != 'd')
316                         return 0;
317                 
318                 unsigned long total_hits = 0, total_misses = 0;
319
320                 for (std::vector<DNSBLConfEntry*>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
321                 {
322                         total_hits += (*i)->stats_hits;
323                         total_misses += (*i)->stats_misses;
324                         
325                         results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS DNSbl \"" + (*i)->name + "\" had " +
326                                         ConvToStr((*i)->stats_hits) + " hits and " + ConvToStr((*i)->stats_misses) + " misses");
327                 }
328                 
329                 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total hits: " + ConvToStr(total_hits));
330                 results.push_back(std::string(ServerInstance->Config->ServerName) + " 304 " + user->nick + " :DNSBLSTATS Total misses: " + ConvToStr(total_misses));
331                 
332                 return 0;
333         }
334 };
335
336 // stuff down here is the module-factory stuff.
337
338 class ModuleDNSBLFactory : public ModuleFactory
339 {
340  public:
341         ModuleDNSBLFactory()
342         {
343         }
344
345         ~ModuleDNSBLFactory()
346         {
347         }
348
349         virtual Module *CreateModule(InspIRCd *Me)
350         {
351                 return new ModuleDNSBL(Me);
352         }
353
354 };
355
356
357 extern "C" void * init_module( void )
358 {
359         return new ModuleDNSBLFactory;
360 }