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