2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
5 * Copyright (C) 2014 Googolplexed <googol@googolplexed.net>
6 * Copyright (C) 2013, 2017-2021 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>
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.
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
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/>.
29 #include "modules/webirc.h"
31 class ModuleConnectBan
33 , public WebIRC::EventListener
35 typedef std::map<irc::sockets::cidr_mask, unsigned int> ConnectMap;
37 unsigned int threshold;
38 unsigned int banduration;
39 unsigned int ipv4_cidr;
40 unsigned int ipv6_cidr;
41 std::string banmessage;
43 unsigned char GetRange(LocalUser* user)
45 int family = user->client_sa.family();
55 // Ranges for UNIX sockets are ignored entirely.
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);
64 static bool IsExempt(LocalUser* user)
66 // E-lined and already banned users shouldn't be hit.
67 if (user->exempt || user->quitting)
70 // Users in an exempt class shouldn't be hit.
71 return user->GetClass() && !user->GetClass()->config->getBool("useconnectban", true);
76 : WebIRC::EventListener(this)
80 void Prioritize() CXX11_OVERRIDE
82 Module* corexline = ServerInstance->Modules->Find("core_xline");
83 ServerInstance->Modules->SetPriority(this, I_OnSetUserIP, PRIORITY_AFTER, corexline);
86 Version GetVersion() CXX11_OVERRIDE
88 return Version("Z-lines IP addresses which make excessive connections to the server.", VF_VENDOR);
91 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
93 ConfigTag* tag = ServerInstance->Config->ConfValue("connectban");
95 ipv4_cidr = tag->getUInt("ipv4cidr", 32, 1, 32);
96 ipv6_cidr = tag->getUInt("ipv6cidr", 128, 1, 128);
97 threshold = tag->getUInt("threshold", 10, 1);
98 banduration = tag->getDuration("duration", 10*60, 1);
99 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.");
102 void OnWebIRCAuth(LocalUser* user, const WebIRC::FlagMap* flags) CXX11_OVERRIDE
107 // HACK: Lower the connection attempts for the gateway IP address. The user
108 // will be rechecked for connect spamming shortly after when their IP address
109 // is changed and OnSetUserIP is called.
110 irc::sockets::cidr_mask mask(user->client_sa, GetRange(user));
111 ConnectMap::iterator iter = connects.find(mask);
112 if (iter != connects.end() && iter->second)
116 void OnSetUserIP(LocalUser* u) CXX11_OVERRIDE
121 irc::sockets::cidr_mask mask(u->client_sa, GetRange(u));
122 ConnectMap::iterator i = connects.find(mask);
124 if (i != connects.end())
128 if (i->second >= threshold)
130 // Create Z-line for set duration.
131 ZLine* zl = new ZLine(ServerInstance->Time(), banduration, ServerInstance->Config->ServerName, banmessage, mask.str());
132 if (!ServerInstance->XLines->AddLine(zl, NULL))
137 ServerInstance->XLines->ApplyLines();
138 std::string maskstr = mask.str();
139 ServerInstance->SNO->WriteGlobalSno('x', "Z-line added by module m_connectban on %s to expire in %s (on %s): Connect flooding",
140 maskstr.c_str(), InspIRCd::DurationString(zl->duration).c_str(), InspIRCd::TimeString(zl->expiry).c_str());
141 ServerInstance->SNO->WriteGlobalSno('a', "Connect flooding from IP range %s (%d)", maskstr.c_str(), threshold);
151 void OnGarbageCollect() CXX11_OVERRIDE
153 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Clearing map.");
158 MODULE_INIT(ModuleConnectBan)