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