]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_dnsbl.cpp
1cd08024b676a7b705f4b4471e0108abc3db70d3
[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                                 in_addr resultip;
67
68                                 /* Convert the result to an in_addr (we can gaurantee we got ipv4)
69                                  * Whoever did the loop that was here before, I AM CONFISCATING
70                                  * YOUR CRACKPIPE. you know who you are. -- Brain
71                                  */
72                                 inet_aton(result.c_str(), &resultip);
73                                 bitmask = resultip.s_addr >> 24; /* Last octet (network byte order */
74
75                                 bitmask &= ConfEntry->bitmask;
76
77                                 if (bitmask != 0)
78                                 {
79                                         std::string reason = ConfEntry->reason;
80                                         std::string::size_type x = reason.find("%ip%");
81                                         while (x != std::string::npos)
82                                         {
83                                                 reason.erase(x, 4);
84                                                 reason.insert(x, them->GetIPString());
85                                                 x = reason.find("%ip%");
86                                         }
87
88                                         ServerInstance->WriteOpers("*** Connecting user %s detected as being on a DNS blacklist (%s) with result %d", them->GetFullRealHost(), ConfEntry->name.c_str(), bitmask);
89
90                                         switch (ConfEntry->banaction)
91                                         {
92                                                 case DNSBLConfEntry::I_KILL:
93                                                 {
94                                                         userrec::QuitUser(ServerInstance, them, std::string("Killed (") + reason + ")");
95                                                         break;
96                                                 }
97                                                 case DNSBLConfEntry::I_KLINE:
98                                                 {
99                                                         ServerInstance->AddKLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, std::string("*@") + them->GetIPString());
100                                                         FOREACH_MOD(I_OnAddKLine,OnAddKLine(ConfEntry->duration, NULL, reason, std::string("*@") + them->GetIPString()));
101                                                         break;
102                                                 }
103                                                 case DNSBLConfEntry::I_GLINE:
104                                                 {
105                                                         ServerInstance->AddGLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, std::string("*@") + them->GetIPString());
106                                                         FOREACH_MOD(I_OnAddGLine,OnAddGLine(ConfEntry->duration, NULL, reason, std::string("*@") + them->GetIPString()));
107                                                         break;
108                                                 }
109                                                 case DNSBLConfEntry::I_ZLINE:
110                                                 {
111                                                         ServerInstance->AddZLine(ConfEntry->duration, ServerInstance->Config->ServerName, reason, them->GetIPString());
112                                                         FOREACH_MOD(I_OnAddZLine,OnAddZLine(ConfEntry->duration, NULL, reason, them->GetIPString()));
113                                                         break;
114                                                 }
115                                                 case DNSBLConfEntry::I_UNKNOWN:
116                                                 {
117                                                         break;
118                                                 }
119                                                 break;
120                                         }
121                                 }
122                         }
123                 }
124         }
125
126         virtual void OnError(ResolverError e, const std::string &errormessage)
127         {
128         }
129
130         virtual ~DNSBLResolver()
131         {
132         }
133 };
134
135 class ModuleDNSBL : public Module
136 {
137  private:
138         std::vector<DNSBLConfEntry *> DNSBLConfEntries;
139
140         /*
141          *      Convert a string to EnumBanaction
142          */
143         DNSBLConfEntry::EnumBanaction str2banaction(const std::string &action)
144         {
145                 if(action.compare("KILL")==0)
146                         return DNSBLConfEntry::I_KILL;
147                 if(action.compare("KLINE")==0)
148                         return DNSBLConfEntry::I_KLINE;
149                 if(action.compare("ZLINE")==0)
150                         return DNSBLConfEntry::I_ZLINE;
151                 if(action.compare("GLINE")==0)
152                         return DNSBLConfEntry::I_GLINE;
153
154                 return DNSBLConfEntry::I_UNKNOWN;
155         }
156  public:
157         ModuleDNSBL(InspIRCd *Me) : Module::Module(Me)
158         {
159                 ReadConf();
160         }
161
162         virtual ~ModuleDNSBL()
163         {
164                 ClearEntries();
165         }
166
167         virtual Version GetVersion()
168         {
169                 return Version(2, 0, 0, 1, VF_VENDOR, API_VERSION);
170         }
171
172         void Implements(char* List)
173         {
174                 List[I_OnRehash] = List[I_OnUserRegister] = 1;
175         }
176
177         /** Clear entries and free the mem it was using
178          */
179         void ClearEntries()
180         {
181                 std::vector<DNSBLConfEntry *>::iterator i;
182                 while ((i = DNSBLConfEntries.begin()) != DNSBLConfEntries.end())
183                 {
184                         DNSBLConfEntries.erase(i);
185                         delete *i;
186                 }
187         }
188
189         /** Fill our conf vector with data
190          */
191         virtual void ReadConf()
192         {
193                 ConfigReader *MyConf = new ConfigReader(ServerInstance);
194                 ClearEntries();
195
196                 for (int i=0; i< MyConf->Enumerate("dnsbl"); i++)
197                 {
198                         DNSBLConfEntry *e = new DNSBLConfEntry();
199
200                         e->name = MyConf->ReadValue("dnsbl", "name", i);
201                         e->reason = MyConf->ReadValue("dnsbl", "reason", i);
202                         e->domain = MyConf->ReadValue("dnsbl", "domain", i);
203                         e->banaction = str2banaction(MyConf->ReadValue("dnsbl", "action", i));
204                         e->duration = ServerInstance->Duration(MyConf->ReadValue("dnsbl", "duration", i).c_str());
205                         e->bitmask = MyConf->ReadInteger("dnsbl", "bitmask", i, false);
206
207                         /* yeah, logic here is a little messy */
208                         if (e->bitmask <= 0)
209                         {
210                                 ServerInstance->WriteOpers("*** DNSBL(#%d): invalid bitmask",i);
211                         }
212                         else if (e->name == "")
213                         {
214                                 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid name",i);
215                         }
216                         else if (e->domain == "")
217                         {
218                                 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid domain",i);
219                         }
220                         else if (e->banaction == DNSBLConfEntry::I_UNKNOWN)
221                         {
222                                 ServerInstance->WriteOpers("*** DNSBL(#%d): Invalid banaction", i);
223                         }
224                         else
225                         {
226                                 if (e->reason == "")
227                                 {
228                                         ServerInstance->WriteOpers("*** DNSBL(#%d): empty reason, using defaults",i);
229                                         e->reason = "Your IP has been blacklisted.";
230                                 }
231
232                                 /* add it, all is ok */
233                                 DNSBLConfEntries.push_back(e);
234                                 continue;
235                         }
236
237                         /* delete and drop it, error somewhere */
238                         delete e;
239                 }
240
241                 delete MyConf;
242         }
243
244         virtual void OnRehash(userrec* user, const std::string &parameter)
245         {
246                 ReadConf();
247         }
248
249         /*
250          * We will check each user that connects *locally* (userrec::fd>0)
251          */
252         virtual int OnUserRegister(userrec* user)
253         {
254                 if (IS_LOCAL(user))
255                 {
256                         /* following code taken from bopm, reverses an IP address. */
257                         struct in_addr in;
258                         unsigned char a, b, c, d;
259                         char reversedipbuf[128];
260                         std::string reversedip;
261                         bool success = false;
262
263                         if (!inet_aton(user->GetIPString(), &in))
264                         {
265 #ifdef IPV6
266                                 /* We could have an ipv6 address here */
267                                 std::string x = user->GetIPString();
268                                 /* Is it a 4in6 address? (Compensate for this kernel kludge that people love) */
269                                 if (x.find("0::ffff:") == 0)
270                                 {
271                                         x.erase(x.begin(), x.begin() + 8);
272                                         if (inet_aton(x.c_str(), &in))
273                                                 success = true;
274                                 }
275 #endif
276                         }
277                         else
278                         {
279                                 success = true;
280                         }
281
282                         if (!success)
283                                 return 0;
284
285                         d = (unsigned char) (in.s_addr >> 24) & 0xFF;
286                         c = (unsigned char) (in.s_addr >> 16) & 0xFF;
287                         b = (unsigned char) (in.s_addr >> 8) & 0xFF;
288                         a = (unsigned char) in.s_addr & 0xFF;
289
290                         snprintf(reversedipbuf, 128, "%d.%d.%d.%d", d, c, b, a);
291                         reversedip = std::string(reversedipbuf);
292
293                         // For each DNSBL, we will run through this lookup
294                         for (std::vector<DNSBLConfEntry *>::iterator i = DNSBLConfEntries.begin(); i != DNSBLConfEntries.end(); i++)
295                         {
296                                 // Fill hostname with a dnsbl style host (d.c.b.a.domain.tld)
297                                 std::string hostname = reversedip + "." + (*i)->domain;
298
299                                 /* now we'd need to fire off lookups for `hostname'. */
300                                 bool cached;
301                                 DNSBLResolver *r = new DNSBLResolver(this, ServerInstance, hostname, user, user->GetFd(), *i, cached);
302                                 ServerInstance->AddResolver(r, cached);
303                         }
304                 }
305
306                 /* don't do anything with this hot potato */
307                 return 0;
308         }
309 };
310
311 // stuff down here is the module-factory stuff.
312
313 class ModuleDNSBLFactory : public ModuleFactory
314 {
315  public:
316         ModuleDNSBLFactory()
317         {
318         }
319
320         ~ModuleDNSBLFactory()
321         {
322         }
323
324         virtual Module *CreateModule(InspIRCd *Me)
325         {
326                 return new ModuleDNSBL(Me);
327         }
328
329 };
330
331
332 extern "C" void * init_module( void )
333 {
334         return new ModuleDNSBLFactory;
335 }