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