]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_connectban.cpp
Make various self contained methods static.
[user/henk/code/inspircd.git] / src / modules / m_connectban.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "xline.h"
22
23 class ModuleConnectBan : public Module
24 {
25         clonemap connects;
26         unsigned int threshold;
27         unsigned int banduration;
28         unsigned int ipv4_cidr;
29         unsigned int ipv6_cidr;
30
31  public:
32         Version GetVersion() CXX11_OVERRIDE
33         {
34                 return Version("Throttles the connections of IP ranges who try to connect flood.", VF_VENDOR);
35         }
36
37         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
38         {
39                 ConfigTag* tag = ServerInstance->Config->ConfValue("connectban");
40
41                 ipv4_cidr = tag->getInt("ipv4cidr", 32, 1, 32);
42                 ipv6_cidr = tag->getInt("ipv6cidr", 128, 1, 128);
43                 threshold = tag->getInt("threshold", 10, 1);
44                 banduration = tag->getDuration("duration", 10*60, 1);
45         }
46
47         void OnSetUserIP(LocalUser* u) CXX11_OVERRIDE
48         {
49                 if (u->exempt)
50                         return;
51
52                 int range = 32;
53                 clonemap::iterator i;
54
55                 switch (u->client_sa.sa.sa_family)
56                 {
57                         case AF_INET6:
58                                 range = ipv6_cidr;
59                         break;
60                         case AF_INET:
61                                 range = ipv4_cidr;
62                         break;
63                 }
64
65                 irc::sockets::cidr_mask mask(u->client_sa, range);
66                 i = connects.find(mask);
67
68                 if (i != connects.end())
69                 {
70                         i->second++;
71
72                         if (i->second >= threshold)
73                         {
74                                 // Create zline for set duration.
75                                 ZLine* zl = new ZLine(ServerInstance->Time(), banduration, ServerInstance->Config->ServerName, "Your IP range has been attempting to connect too many times in too short a duration. Wait a while, and you will be able to connect.", mask.str());
76                                 if (!ServerInstance->XLines->AddLine(zl, NULL))
77                                 {
78                                         delete zl;
79                                         return;
80                                 }
81                                 ServerInstance->XLines->ApplyLines();
82                                 std::string maskstr = mask.str();
83                                 std::string timestr = InspIRCd::TimeString(zl->expiry);
84                                 ServerInstance->SNO->WriteGlobalSno('x',"Module m_connectban added Z:line on *@%s to expire on %s: Connect flooding",
85                                         maskstr.c_str(), timestr.c_str());
86                                 ServerInstance->SNO->WriteGlobalSno('a', "Connect flooding from IP range %s (%d)", maskstr.c_str(), threshold);
87                                 connects.erase(i);
88                         }
89                 }
90                 else
91                 {
92                         connects[mask] = 1;
93                 }
94         }
95
96         void OnGarbageCollect()
97         {
98                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Clearing map.");
99                 connects.clear();
100         }
101 };
102
103 MODULE_INIT(ModuleConnectBan)