2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Dennis Friis <peavey@inspircd.org>
6 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
7 * Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 class WriteCommonQuit : public User::ForEachNeighborHandler
31 ClientProtocol::Messages::Quit quitmsg;
32 ClientProtocol::Event quitevent;
33 ClientProtocol::Messages::Quit operquitmsg;
34 ClientProtocol::Event operquitevent;
36 void Execute(LocalUser* user) CXX11_OVERRIDE
38 user->Send(user->IsOper() ? operquitevent : quitevent);
42 WriteCommonQuit(User* user, const std::string& msg, const std::string& opermsg)
44 , quitevent(ServerInstance->GetRFCEvents().quit, quitmsg)
45 , operquitmsg(user, opermsg)
46 , operquitevent(ServerInstance->GetRFCEvents().quit, operquitmsg)
48 user->ForEachNeighbor(*this, false);
53 UserManager::UserManager()
55 , unregistered_count(0)
59 UserManager::~UserManager()
61 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); ++i)
67 void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
69 // User constructor allocates a new UUID for the user and inserts it into the uuidlist
70 LocalUser* const New = new LocalUser(socket, client, server);
71 UserIOHandler* eh = &New->eh;
73 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "New user fd: %d", socket);
75 this->unregistered_count++;
76 this->clientlist[New->nick] = New;
78 this->local_users.push_front(New);
80 if (!SocketEngine::AddFd(eh, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
82 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "Internal error on new connection");
83 this->QuitUser(New, "Internal error handling connection");
87 // If this listener has an IO hook provider set then tell it about the connection
88 for (ListenSocket::IOHookProvList::iterator i = via->iohookprovs.begin(); i != via->iohookprovs.end(); ++i)
90 ListenSocket::IOHookProvRef& iohookprovref = *i;
94 iohookprovref->OnAccept(eh, client, server);
95 // IOHook could have encountered a fatal error, e.g. if the TLS ClientHello was already in the queue and there was no common TLS version
96 if (!eh->getError().empty())
98 QuitUser(New, eh->getError());
103 if (this->local_users.size() > ServerInstance->Config->SoftLimit)
105 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
106 this->QuitUser(New,"No more connections allowed");
110 // First class check. We do this again in LocalUser::FullConnect() after DNS is done, and NICK/USER is received.
112 // If the user doesn't have an acceptable connect class CheckClass() quits them
113 New->CheckClass(ServerInstance->Config->CCOnConnect);
118 * even with bancache, we still have to keep User::exempt current.
119 * besides that, if we get a positive bancache hit, we still won't fuck
120 * them over if they are exempt. -- w00t
122 New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
124 BanCacheHit* const b = ServerInstance->BanCache.GetHit(New->GetIPString());
127 if (!b->Type.empty() && !New->exempt)
130 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Positive hit for " + New->GetIPString());
131 if (!ServerInstance->Config->XLineMessage.empty())
132 New->WriteNumeric(ERR_YOUREBANNEDCREEP, ServerInstance->Config->XLineMessage);
134 if (ServerInstance->Config->HideBans)
135 this->QuitUser(New, b->Type + "-Lined", &b->Reason);
137 this->QuitUser(New, b->Reason);
142 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Negative hit for " + New->GetIPString());
149 XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
159 if (ServerInstance->Config->RawLog)
160 New->WriteNotice("*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.");
162 FOREACH_MOD(OnSetUserIP, (New));
166 FOREACH_MOD(OnUserInit, (New));
169 void UserManager::QuitUser(User* user, const std::string& quitreason, const std::string* operreason)
173 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit quitting user: " + user->nick);
179 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit server user: " + user->nick);
183 user->quitting = true;
185 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitreason.c_str());
186 LocalUser* const localuser = IS_LOCAL(user);
189 ClientProtocol::Messages::Error errormsg(InspIRCd::Format("Closing link: (%s@%s) [%s]", user->ident.c_str(), user->GetRealHost().c_str(), operreason ? operreason->c_str() : quitreason.c_str()));
190 localuser->Send(ServerInstance->GetRFCEvents().error, errormsg);
194 reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
196 operreason = &reason;
198 ServerInstance->GlobalCulls.AddItem(user);
200 if (user->registered == REG_ALL)
202 FOREACH_MOD(OnUserQuit, (user, reason, *operreason));
203 WriteCommonQuit(user, reason, *operreason);
206 unregistered_count--;
210 LocalUser* lu = IS_LOCAL(user);
211 FOREACH_MOD(OnUserDisconnect, (lu));
214 if (lu->registered == REG_ALL)
215 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s (%s) [%s]", user->GetFullRealHost().c_str(), user->GetIPString().c_str(), operreason->c_str());
216 local_users.erase(lu);
219 if (!clientlist.erase(user->nick))
220 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Nick not found in clientlist, cannot remove: " + user->nick);
222 uuidlist.erase(user->uuid);
223 user->PurgeEmptyChannels();
227 void UserManager::AddClone(User* user)
229 CloneCounts& counts = clonemap[user->GetCIDRMask()];
235 void UserManager::RemoveCloneCounts(User *user)
237 CloneMap::iterator it = clonemap.find(user->GetCIDRMask());
238 if (it != clonemap.end())
240 CloneCounts& counts = it->second;
242 if (counts.global == 0)
244 // No more users from this IP, remove entry from the map
254 void UserManager::RehashCloneCounts()
258 const user_hash& hash = ServerInstance->Users.GetUsers();
259 for (user_hash::const_iterator i = hash.begin(); i != hash.end(); ++i)
266 const UserManager::CloneCounts& UserManager::GetCloneCounts(User* user) const
268 CloneMap::const_iterator it = clonemap.find(user->GetCIDRMask());
269 if (it != clonemap.end())
272 return zeroclonecounts;
275 void UserManager::ServerNoticeAll(const char* text, ...)
278 VAFORMAT(message, text, text);
279 ClientProtocol::Messages::Privmsg msg(ClientProtocol::Messages::Privmsg::nocopy, ServerInstance->FakeClient, ServerInstance->Config->ServerName, message, MSG_NOTICE);
280 ClientProtocol::Event msgevent(ServerInstance->GetRFCEvents().privmsg, msg);
282 for (LocalList::const_iterator i = local_users.begin(); i != local_users.end(); ++i)
284 LocalUser* user = *i;
285 user->Send(msgevent);
289 /* this returns true when all modules are satisfied that the user should be allowed onto the irc server
290 * (until this returns true, a user will block in the waiting state, waiting to connect up to the
291 * registration timeout maximum seconds)
293 bool UserManager::AllModulesReportReady(LocalUser* user)
296 FIRST_MOD_RESULT(OnCheckReady, res, (user));
297 return (res == MOD_RES_PASSTHRU);
301 * This function is called once a second from the mainloop.
302 * It is intended to do background checking on all the users, e.g. do
303 * ping checks, registration timeouts, etc.
305 void UserManager::DoBackgroundUserStuff()
307 for (LocalList::iterator i = local_users.begin(); i != local_users.end(); )
309 // It's possible that we quit the user below due to ping timeout etc. and QuitUser() removes it from the list
310 LocalUser* curr = *i;
313 if (curr->CommandFloodPenalty || curr->eh.getSendQSize())
315 unsigned int rate = curr->MyClass->GetCommandRate();
316 if (curr->CommandFloodPenalty > rate)
317 curr->CommandFloodPenalty -= rate;
319 curr->CommandFloodPenalty = 0;
320 curr->eh.OnDataReady();
323 switch (curr->registered)
326 if (ServerInstance->Time() >= curr->nping)
328 // This user didn't answer the last ping, remove them
331 time_t time = ServerInstance->Time() - (curr->nping - curr->MyClass->GetPingTime());
332 const std::string message = "Ping timeout: " + ConvToStr(time) + (time != 1 ? " seconds" : " second");
333 this->QuitUser(curr, message);
336 ClientProtocol::Messages::Ping ping;
337 curr->Send(ServerInstance->GetRFCEvents().ping, ping);
339 curr->nping = ServerInstance->Time() + curr->MyClass->GetPingTime();
343 if (AllModulesReportReady(curr))
345 /* User has sent NICK/USER, modules are okay, DNS finished. */
350 // If the user has been quit in OnCheckReady then we shouldn't
351 // quit them again for having a registration timeout.
357 if (curr->registered != REG_ALL && curr->MyClass && (ServerInstance->Time() > (curr->signon + curr->MyClass->GetRegTimeout())))
360 * registration timeout -- didnt send USER/NICK/HOST
361 * in the time specified in their connection class.
363 this->QuitUser(curr, "Registration timeout");
369 already_sent_t UserManager::NextAlreadySentId()
371 if (++already_sent_id == 0)
373 // Wrapped around, reset the already_sent ids of all users
375 for (LocalList::iterator i = local_users.begin(); i != local_users.end(); ++i)
377 LocalUser* user = *i;
378 user->already_sent = 0;
381 return already_sent_id;