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