2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 iwalkalone <iwalkalone69@gmail.com>
5 * Copyright (C) 2019 Matt Schatz <genius3000@g3k.solutions>
6 * Copyright (C) 2013-2016, 2018 Attila Molnar <attilamolnar@hush.com>
7 * Copyright (C) 2013, 2018-2019 Sadie Powell <sadie@witchery.services>
8 * Copyright (C) 2013, 2015 Adam <Adam@anope.org>
9 * Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
10 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
11 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
12 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
13 * Copyright (C) 2008-2010 Craig Edwards <brain@inspircd.org>
14 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
16 * This file is part of InspIRCd. InspIRCd is free software: you can
17 * redistribute it and/or modify it under the terms of the GNU General Public
18 * License as published by the Free Software Foundation, version 2.
20 * This program is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class WriteCommonQuit : public User::ForEachNeighborHandler
38 ClientProtocol::Messages::Quit quitmsg;
39 ClientProtocol::Event quitevent;
40 ClientProtocol::Messages::Quit operquitmsg;
41 ClientProtocol::Event operquitevent;
43 void Execute(LocalUser* user) CXX11_OVERRIDE
45 user->Send(user->IsOper() ? operquitevent : quitevent);
49 WriteCommonQuit(User* user, const std::string& msg, const std::string& opermsg)
51 , quitevent(ServerInstance->GetRFCEvents().quit, quitmsg)
52 , operquitmsg(user, opermsg)
53 , operquitevent(ServerInstance->GetRFCEvents().quit, operquitmsg)
55 user->ForEachNeighbor(*this, false);
59 void CheckPingTimeout(LocalUser* user)
61 // Check if it is time to ping the user yet.
62 if (ServerInstance->Time() < user->nextping)
65 // This user didn't answer the last ping, remove them.
69 FIRST_MOD_RESULT(OnConnectionFail, res, (user, I_ERR_TIMEOUT));
70 if (res == MOD_RES_ALLOW)
72 // A module is preventing this user from being timed out.
74 user->nextping = ServerInstance->Time() + user->MyClass->GetPingTime();
78 time_t secs = ServerInstance->Time() - (user->nextping - user->MyClass->GetPingTime());
79 const std::string message = "Ping timeout: " + ConvToStr(secs) + (secs != 1 ? " seconds" : " second");
80 ServerInstance->Users.QuitUser(user, message);
84 // Send a ping to the client.
85 ClientProtocol::Messages::Ping ping;
86 user->Send(ServerInstance->GetRFCEvents().ping, ping);
88 user->nextping = ServerInstance->Time() + user->MyClass->GetPingTime();
91 void CheckRegistrationTimeout(LocalUser* user)
93 if (user->GetClass() && (ServerInstance->Time() > (user->signon + user->GetClass()->GetRegTimeout())))
95 // Either the user did not send NICK/USER or a module blocked registration in
96 // OnCheckReady until the client timed out.
97 ServerInstance->Users.QuitUser(user, "Registration timeout");
101 void CheckModulesReady(LocalUser* user)
104 FIRST_MOD_RESULT(OnCheckReady, res, (user));
105 if (res == MOD_RES_PASSTHRU)
107 // User has sent NICK/USER and modules are ready.
112 // If the user has been quit in OnCheckReady then we shouldn't quit
113 // them again for having a registration timeout.
115 CheckRegistrationTimeout(user);
119 UserManager::UserManager()
121 , unregistered_count(0)
126 UserManager::~UserManager()
128 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); ++i)
134 void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
136 // User constructor allocates a new UUID for the user and inserts it into the uuidlist
137 LocalUser* const New = new LocalUser(socket, client, server);
138 UserIOHandler* eh = &New->eh;
140 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "New user fd: %d", socket);
142 this->unregistered_count++;
143 this->clientlist[New->nick] = New;
145 this->local_users.push_front(New);
146 FOREACH_MOD(OnUserInit, (New));
148 if (!SocketEngine::AddFd(eh, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
150 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "Internal error on new connection");
151 this->QuitUser(New, "Internal error handling connection");
155 // If this listener has an IO hook provider set then tell it about the connection
156 for (ListenSocket::IOHookProvList::iterator i = via->iohookprovs.begin(); i != via->iohookprovs.end(); ++i)
158 ListenSocket::IOHookProvRef& iohookprovref = *i;
161 if (!iohookprovref.GetProvider().empty())
163 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "Non-existent I/O hook '%s' in <bind:%s> tag at %s",
164 iohookprovref.GetProvider().c_str(),
165 i == via->iohookprovs.begin() ? "hook" : "ssl",
166 via->bind_tag->getTagLocation().c_str());
167 this->QuitUser(New, "Internal error handling connection");
173 iohookprovref->OnAccept(eh, client, server);
175 // IOHook could have encountered a fatal error, e.g. if the TLS ClientHello
176 // was already in the queue and there was no common TLS version.
177 if (!eh->getError().empty())
179 QuitUser(New, eh->getError());
184 if (this->local_users.size() > ServerInstance->Config->SoftLimit)
186 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
187 this->QuitUser(New,"No more connections allowed");
191 // First class check. We do this again in LocalUser::FullConnect() after DNS is done, and NICK/USER is received.
193 // If the user doesn't have an acceptable connect class CheckClass() quits them
194 New->CheckClass(ServerInstance->Config->CCOnConnect);
199 * even with bancache, we still have to keep User::exempt current.
200 * besides that, if we get a positive bancache hit, we still won't fuck
201 * them over if they are exempt. -- w00t
203 New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
205 BanCacheHit* const b = ServerInstance->BanCache.GetHit(New->GetIPString());
208 if (!b->Type.empty() && !New->exempt)
211 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Positive hit for " + New->GetIPString());
212 if (!ServerInstance->Config->XLineMessage.empty())
213 New->WriteNumeric(ERR_YOUREBANNEDCREEP, ServerInstance->Config->XLineMessage);
215 if (ServerInstance->Config->HideBans)
216 this->QuitUser(New, b->Type + "-lined", &b->Reason);
218 this->QuitUser(New, b->Reason);
223 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Negative hit for " + New->GetIPString());
230 XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
240 if (ServerInstance->Config->RawLog)
241 New->WriteNotice("*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.");
243 FOREACH_MOD(OnSetUserIP, (New));
245 FOREACH_MOD(OnUserPostInit, (New));
248 void UserManager::QuitUser(User* user, const std::string& quitmessage, const std::string* operquitmessage)
252 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit quitting user: " + user->nick);
258 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit server user: " + user->nick);
262 std::string quitmsg(quitmessage);
263 std::string operquitmsg;
265 operquitmsg.assign(*operquitmessage);
267 LocalUser* const localuser = IS_LOCAL(user);
270 ModResult MOD_RESULT;
271 FIRST_MOD_RESULT(OnUserPreQuit, MOD_RESULT, (localuser, quitmsg, operquitmsg));
272 if (MOD_RESULT == MOD_RES_DENY)
276 if (quitmsg.length() > ServerInstance->Config->Limits.MaxQuit)
277 quitmsg.erase(ServerInstance->Config->Limits.MaxQuit + 1);
279 if (operquitmsg.empty())
280 operquitmsg.assign(quitmsg);
281 else if (operquitmsg.length() > ServerInstance->Config->Limits.MaxQuit)
282 operquitmsg.erase(ServerInstance->Config->Limits.MaxQuit + 1);
284 user->quitting = true;
285 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitmessage.c_str());
288 ClientProtocol::Messages::Error errormsg(InspIRCd::Format("Closing link: (%s@%s) [%s]", user->ident.c_str(), user->GetRealHost().c_str(), operquitmsg.c_str()));
289 localuser->Send(ServerInstance->GetRFCEvents().error, errormsg);
292 ServerInstance->GlobalCulls.AddItem(user);
294 if (user->registered == REG_ALL)
296 FOREACH_MOD(OnUserQuit, (user, quitmsg, operquitmsg));
297 WriteCommonQuit(user, quitmsg, operquitmsg);
300 unregistered_count--;
304 LocalUser* lu = IS_LOCAL(user);
305 FOREACH_MOD(OnUserDisconnect, (lu));
308 if (lu->registered == REG_ALL)
309 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s (%s) [%s]", user->GetFullRealHost().c_str(), user->GetIPString().c_str(), operquitmsg.c_str());
310 local_users.erase(lu);
313 if (!clientlist.erase(user->nick))
314 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Nick not found in clientlist, cannot remove: " + user->nick);
316 uuidlist.erase(user->uuid);
317 user->PurgeEmptyChannels();
321 void UserManager::AddClone(User* user)
323 CloneCounts& counts = clonemap[user->GetCIDRMask()];
329 void UserManager::RemoveCloneCounts(User *user)
331 CloneMap::iterator it = clonemap.find(user->GetCIDRMask());
332 if (it != clonemap.end())
334 CloneCounts& counts = it->second;
336 if (counts.global == 0)
338 // No more users from this IP, remove entry from the map
348 void UserManager::RehashCloneCounts()
352 const user_hash& hash = ServerInstance->Users.GetUsers();
353 for (user_hash::const_iterator i = hash.begin(); i != hash.end(); ++i)
360 const UserManager::CloneCounts& UserManager::GetCloneCounts(User* user) const
362 CloneMap::const_iterator it = clonemap.find(user->GetCIDRMask());
363 if (it != clonemap.end())
366 return zeroclonecounts;
369 void UserManager::ServerNoticeAll(const char* text, ...)
372 VAFORMAT(message, text, text);
373 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, ServerInstance->Config->ServerName, message, MSG_NOTICE);
374 ClientProtocol::Event msgevent(ServerInstance->GetRFCEvents().privmsg, msg);
376 for (LocalList::const_iterator i = local_users.begin(); i != local_users.end(); ++i)
378 LocalUser* user = *i;
379 user->Send(msgevent);
384 * This function is called once a second from the mainloop.
385 * It is intended to do background checking on all the users, e.g. do
386 * ping checks, registration timeouts, etc.
388 void UserManager::DoBackgroundUserStuff()
390 for (LocalList::iterator i = local_users.begin(); i != local_users.end(); )
392 // It's possible that we quit the user below due to ping timeout etc. and QuitUser() removes it from the list
393 LocalUser* curr = *i;
396 if (curr->CommandFloodPenalty || curr->eh.getSendQSize())
398 unsigned int rate = curr->MyClass->GetCommandRate();
399 if (curr->CommandFloodPenalty > rate)
400 curr->CommandFloodPenalty -= rate;
402 curr->CommandFloodPenalty = 0;
403 curr->eh.OnDataReady();
406 switch (curr->registered)
409 CheckPingTimeout(curr);
413 CheckModulesReady(curr);
417 CheckRegistrationTimeout(curr);
423 already_sent_t UserManager::NextAlreadySentId()
425 if (++already_sent_id == 0)
427 // Wrapped around, reset the already_sent ids of all users
429 for (LocalList::iterator i = local_users.begin(); i != local_users.end(); ++i)
431 LocalUser* user = *i;
432 user->already_sent = 0;
435 return already_sent_id;