]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_connectban.cpp
dd9ae4f5450158180cdc98aca7467b1f9dcd3133
[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 #include "modules/webirc.h"
23
24 class ModuleConnectBan
25         : public Module
26         , public WebIRC::EventListener
27 {
28         typedef std::map<irc::sockets::cidr_mask, unsigned int> ConnectMap;
29         ConnectMap connects;
30         unsigned int threshold;
31         unsigned int banduration;
32         unsigned int ipv4_cidr;
33         unsigned int ipv6_cidr;
34         std::string banmessage;
35
36         unsigned char GetRange(LocalUser* user)
37         {
38                 int family = user->client_sa.family();
39                 switch (family)
40                 {
41                         case AF_INET:
42                                 return ipv4_cidr;
43
44                         case AF_INET6:
45                                 return ipv6_cidr;
46
47                         case AF_UNIX:
48                                 // Ranges for UNIX sockets are ignored entirely.
49                                 return 0;
50                 }
51
52                 // If we have reached this point then we have encountered a bug.
53                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "BUG: ModuleConnectBan::GetRange(): socket type %d is unknown!", family);
54                 return 0;
55         }
56
57  public:
58         ModuleConnectBan()
59                 : WebIRC::EventListener(this)
60         {
61         }
62
63         Version GetVersion() CXX11_OVERRIDE
64         {
65                 return Version("Throttles the connections of IP ranges who try to connect flood", VF_VENDOR);
66         }
67
68         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
69         {
70                 ConfigTag* tag = ServerInstance->Config->ConfValue("connectban");
71
72                 ipv4_cidr = tag->getUInt("ipv4cidr", 32, 1, 32);
73                 ipv6_cidr = tag->getUInt("ipv6cidr", 128, 1, 128);
74                 threshold = tag->getUInt("threshold", 10, 1);
75                 banduration = tag->getDuration("duration", 10*60, 1);
76                 banmessage = tag->getString("banmessage", "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.");
77         }
78
79         void OnWebIRCAuth(LocalUser* user, const WebIRC::FlagMap* flags) CXX11_OVERRIDE
80         {
81                 if (user->exempt)
82                         return;
83
84                 // HACK: Lower the connection attempts for the gateway IP address. The user
85                 // will be rechecked for connect spamming shortly after when their IP address
86                 // is changed and OnSetUserIP is called.
87                 irc::sockets::cidr_mask mask(user->client_sa, GetRange(user));
88                 ConnectMap::iterator iter = connects.find(mask);
89                 if (iter != connects.end() && iter->second)
90                         iter->second--;
91         }
92
93         void OnSetUserIP(LocalUser* u) CXX11_OVERRIDE
94         {
95                 if (u->exempt)
96                         return;
97
98                 irc::sockets::cidr_mask mask(u->client_sa, GetRange(u));
99                 ConnectMap::iterator i = connects.find(mask);
100
101                 if (i != connects.end())
102                 {
103                         i->second++;
104
105                         if (i->second >= threshold)
106                         {
107                                 // Create Z-line for set duration.
108                                 ZLine* zl = new ZLine(ServerInstance->Time(), banduration, ServerInstance->Config->ServerName, banmessage, mask.str());
109                                 if (!ServerInstance->XLines->AddLine(zl, NULL))
110                                 {
111                                         delete zl;
112                                         return;
113                                 }
114                                 ServerInstance->XLines->ApplyLines();
115                                 std::string maskstr = mask.str();
116                                 ServerInstance->SNO->WriteGlobalSno('x', "Z-line added by module m_connectban on %s to expire in %s (on %s): Connect flooding",
117                                         maskstr.c_str(), InspIRCd::DurationString(zl->duration).c_str(), InspIRCd::TimeString(zl->expiry).c_str());
118                                 ServerInstance->SNO->WriteGlobalSno('a', "Connect flooding from IP range %s (%d)", maskstr.c_str(), threshold);
119                                 connects.erase(i);
120                         }
121                 }
122                 else
123                 {
124                         connects[mask] = 1;
125                 }
126         }
127
128         void OnGarbageCollect() CXX11_OVERRIDE
129         {
130                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Clearing map.");
131                 connects.clear();
132         }
133 };
134
135 MODULE_INIT(ModuleConnectBan)