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