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