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