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