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