]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_connectban.cpp
81b0fcfa79f3aff3d02eeff65781896ddbcf8d13
[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-2020 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         void Prioritize() CXX11_OVERRIDE
71         {
72                 Module* corexline = ServerInstance->Modules->Find("core_xline");
73                 ServerInstance->Modules->SetPriority(this, I_OnSetUserIP, PRIORITY_AFTER, corexline);
74         }
75
76         Version GetVersion() CXX11_OVERRIDE
77         {
78                 return Version("Z-lines IP addresses which make excessive connections to the server.", VF_VENDOR);
79         }
80
81         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
82         {
83                 ConfigTag* tag = ServerInstance->Config->ConfValue("connectban");
84
85                 ipv4_cidr = tag->getUInt("ipv4cidr", 32, 1, 32);
86                 ipv6_cidr = tag->getUInt("ipv6cidr", 128, 1, 128);
87                 threshold = tag->getUInt("threshold", 10, 1);
88                 banduration = tag->getDuration("duration", 10*60, 1);
89                 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.");
90         }
91
92         void OnWebIRCAuth(LocalUser* user, const WebIRC::FlagMap* flags) CXX11_OVERRIDE
93         {
94                 if (user->exempt)
95                         return;
96
97                 // HACK: Lower the connection attempts for the gateway IP address. The user
98                 // will be rechecked for connect spamming shortly after when their IP address
99                 // is changed and OnSetUserIP is called.
100                 irc::sockets::cidr_mask mask(user->client_sa, GetRange(user));
101                 ConnectMap::iterator iter = connects.find(mask);
102                 if (iter != connects.end() && iter->second)
103                         iter->second--;
104         }
105
106         void OnSetUserIP(LocalUser* u) CXX11_OVERRIDE
107         {
108                 if (u->exempt || u->quitting)
109                         return;
110
111                 irc::sockets::cidr_mask mask(u->client_sa, GetRange(u));
112                 ConnectMap::iterator i = connects.find(mask);
113
114                 if (i != connects.end())
115                 {
116                         i->second++;
117
118                         if (i->second >= threshold)
119                         {
120                                 // Create Z-line for set duration.
121                                 ZLine* zl = new ZLine(ServerInstance->Time(), banduration, ServerInstance->Config->ServerName, banmessage, mask.str());
122                                 if (!ServerInstance->XLines->AddLine(zl, NULL))
123                                 {
124                                         delete zl;
125                                         return;
126                                 }
127                                 ServerInstance->XLines->ApplyLines();
128                                 std::string maskstr = mask.str();
129                                 ServerInstance->SNO->WriteGlobalSno('x', "Z-line added by module m_connectban on %s to expire in %s (on %s): Connect flooding",
130                                         maskstr.c_str(), InspIRCd::DurationString(zl->duration).c_str(), InspIRCd::TimeString(zl->expiry).c_str());
131                                 ServerInstance->SNO->WriteGlobalSno('a', "Connect flooding from IP range %s (%d)", maskstr.c_str(), threshold);
132                                 connects.erase(i);
133                         }
134                 }
135                 else
136                 {
137                         connects[mask] = 1;
138                 }
139         }
140
141         void OnGarbageCollect() CXX11_OVERRIDE
142         {
143                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Clearing map.");
144                 connects.clear();
145         }
146 };
147
148 MODULE_INIT(ModuleConnectBan)