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