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