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