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