]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_connectban.cpp
Add the User::exempt flag to m_check.
[user/henk/code/inspircd.git] / src / modules / m_connectban.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
5  *   Copyright (C) 2014 Googolplexed <googol@googolplexed.net>
6  *   Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
7  *   Copyright (C) 2012-2014 Attila Molnar <attilamolnar@hush.com>
8  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
9  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10  *   Copyright (C) 2008, 2010 Craig Edwards <brain@inspircd.org>
11  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26
27 #include "inspircd.h"
28 #include "xline.h"
29 #include "modules/webirc.h"
30
31 class ModuleConnectBan
32         : public Module
33         , public WebIRC::EventListener
34 {
35         typedef std::map<irc::sockets::cidr_mask, unsigned int> ConnectMap;
36         ConnectMap connects;
37         unsigned int threshold;
38         unsigned int banduration;
39         unsigned int ipv4_cidr;
40         unsigned int ipv6_cidr;
41         std::string banmessage;
42
43         unsigned char GetRange(LocalUser* user)
44         {
45                 int family = user->client_sa.family();
46                 switch (family)
47                 {
48                         case AF_INET:
49                                 return ipv4_cidr;
50
51                         case AF_INET6:
52                                 return ipv6_cidr;
53
54                         case AF_UNIX:
55                                 // Ranges for UNIX sockets are ignored entirely.
56                                 return 0;
57                 }
58
59                 // If we have reached this point then we have encountered a bug.
60                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "BUG: ModuleConnectBan::GetRange(): socket type %d is unknown!", family);
61                 return 0;
62         }
63
64  public:
65         ModuleConnectBan()
66                 : WebIRC::EventListener(this)
67         {
68         }
69
70         Version GetVersion() CXX11_OVERRIDE
71         {
72                 return Version("Z-lines IP addresses which make excessive connections to the server.", VF_VENDOR);
73         }
74
75         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
76         {
77                 ConfigTag* tag = ServerInstance->Config->ConfValue("connectban");
78
79                 ipv4_cidr = tag->getUInt("ipv4cidr", 32, 1, 32);
80                 ipv6_cidr = tag->getUInt("ipv6cidr", 128, 1, 128);
81                 threshold = tag->getUInt("threshold", 10, 1);
82                 banduration = tag->getDuration("duration", 10*60, 1);
83                 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.");
84         }
85
86         void OnWebIRCAuth(LocalUser* user, const WebIRC::FlagMap* flags) CXX11_OVERRIDE
87         {
88                 if (user->exempt)
89                         return;
90
91                 // HACK: Lower the connection attempts for the gateway IP address. The user
92                 // will be rechecked for connect spamming shortly after when their IP address
93                 // is changed and OnSetUserIP is called.
94                 irc::sockets::cidr_mask mask(user->client_sa, GetRange(user));
95                 ConnectMap::iterator iter = connects.find(mask);
96                 if (iter != connects.end() && iter->second)
97                         iter->second--;
98         }
99
100         void OnSetUserIP(LocalUser* u) CXX11_OVERRIDE
101         {
102                 if (u->exempt)
103                         return;
104
105                 irc::sockets::cidr_mask mask(u->client_sa, GetRange(u));
106                 ConnectMap::iterator i = connects.find(mask);
107
108                 if (i != connects.end())
109                 {
110                         i->second++;
111
112                         if (i->second >= threshold)
113                         {
114                                 // Create Z-line for set duration.
115                                 ZLine* zl = new ZLine(ServerInstance->Time(), banduration, ServerInstance->Config->ServerName, banmessage, mask.str());
116                                 if (!ServerInstance->XLines->AddLine(zl, NULL))
117                                 {
118                                         delete zl;
119                                         return;
120                                 }
121                                 ServerInstance->XLines->ApplyLines();
122                                 std::string maskstr = mask.str();
123                                 ServerInstance->SNO->WriteGlobalSno('x', "Z-line added by module m_connectban on %s to expire in %s (on %s): Connect flooding",
124                                         maskstr.c_str(), InspIRCd::DurationString(zl->duration).c_str(), InspIRCd::TimeString(zl->expiry).c_str());
125                                 ServerInstance->SNO->WriteGlobalSno('a', "Connect flooding from IP range %s (%d)", maskstr.c_str(), threshold);
126                                 connects.erase(i);
127                         }
128                 }
129                 else
130                 {
131                         connects[mask] = 1;
132                 }
133         }
134
135         void OnGarbageCollect() CXX11_OVERRIDE
136         {
137                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Clearing map.");
138                 connects.clear();
139         }
140 };
141
142 MODULE_INIT(ModuleConnectBan)