]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
Change the syntax of FOREACH macros to be less dumb.
[user/henk/code/inspircd.git] / src / usermanager.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
8  *
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.
12  *
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
16  * details.
17  *
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/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "xline.h"
25 #include "bancache.h"
26 #include "iohook.h"
27
28 UserManager::UserManager()
29         : clientlist(new user_hash)
30         , uuidlist(new user_hash)
31         , unregistered_count(0), local_count(0)
32 {
33 }
34
35 UserManager::~UserManager()
36 {
37         for (user_hash::iterator i = clientlist->begin(); i != clientlist->end(); ++i)
38         {
39                 delete i->second;
40         }
41
42         delete clientlist;
43         delete uuidlist;
44 }
45
46 /* add a client connection to the sockets list */
47 void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
48 {
49         /* NOTE: Calling this one parameter constructor for User automatically
50          * allocates a new UUID and places it in the hash_map.
51          */
52         LocalUser* New = NULL;
53         try
54         {
55                 New = new LocalUser(socket, client, server);
56         }
57         catch (...)
58         {
59                 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "*** WTF *** Duplicated UUID! -- Crack smoking monkeys have been unleashed.");
60                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING *** Duplicate UUID allocated!");
61                 return;
62         }
63         UserIOHandler* eh = &New->eh;
64
65         /* Give each of the modules an attempt to hook the user for I/O */
66         FOREACH_MOD(OnHookIO, (eh, via));
67
68         if (eh->GetIOHook())
69         {
70                 try
71                 {
72                         eh->GetIOHook()->OnStreamSocketAccept(eh, client, server);
73                 }
74                 catch (CoreException& modexcept)
75                 {
76                         ServerInstance->Logs->Log("SOCKET", LOG_DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
77                 }
78         }
79
80         ServerInstance->Logs->Log("USERS", LOG_DEBUG, "New user fd: %d", socket);
81
82         this->unregistered_count++;
83
84         /* The users default nick is their UUID */
85         New->nick = New->uuid;
86         (*(this->clientlist))[New->nick] = New;
87
88         New->registered = REG_NONE;
89         New->signon = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
90         New->lastping = 1;
91
92         ServerInstance->Users->AddLocalClone(New);
93         ServerInstance->Users->AddGlobalClone(New);
94
95         New->localuseriter = this->local_users.insert(local_users.end(), New);
96         local_count++;
97
98         if ((this->local_users.size() > ServerInstance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)ServerInstance->SE->GetMaxFds()))
99         {
100                 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
101                 this->QuitUser(New,"No more connections allowed");
102                 return;
103         }
104
105         /*
106          * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
107          * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
108          */
109         New->SetClass();
110
111         /*
112          * Check connect class settings and initialise settings into User.
113          * This will be done again after DNS resolution. -- w00t
114          */
115         New->CheckClass(ServerInstance->Config->CCOnConnect);
116         if (New->quitting)
117                 return;
118
119         /*
120          * even with bancache, we still have to keep User::exempt current.
121          * besides that, if we get a positive bancache hit, we still won't fuck
122          * them over if they are exempt. -- w00t
123          */
124         New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
125
126         if (BanCacheHit *b = ServerInstance->BanCache->GetHit(New->GetIPString()))
127         {
128                 if (!b->Type.empty() && !New->exempt)
129                 {
130                         /* user banned */
131                         ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Positive hit for " + New->GetIPString());
132                         if (!ServerInstance->Config->MoronBanner.empty())
133                                 New->WriteNotice("*** " +  ServerInstance->Config->MoronBanner);
134                         this->QuitUser(New, b->Reason);
135                         return;
136                 }
137                 else
138                 {
139                         ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Negative hit for " + New->GetIPString());
140                 }
141         }
142         else
143         {
144                 if (!New->exempt)
145                 {
146                         XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
147
148                         if (r)
149                         {
150                                 r->Apply(New);
151                                 return;
152                         }
153                 }
154         }
155
156         if (!ServerInstance->SE->AddFd(eh, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
157         {
158                 ServerInstance->Logs->Log("USERS", LOG_DEBUG, "Internal error on new connection");
159                 this->QuitUser(New, "Internal error handling connection");
160         }
161
162         if (ServerInstance->Config->RawLog)
163                 New->WriteNotice("*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.");
164
165         FOREACH_MOD(OnSetUserIP, (New));
166         if (New->quitting)
167                 return;
168
169         FOREACH_MOD(OnUserInit, (New));
170 }
171
172 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
173 {
174         if (user->quitting)
175         {
176                 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit quitting user: " + user->nick);
177                 return;
178         }
179
180         if (IS_SERVER(user))
181         {
182                 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Tried to quit server user: " + user->nick);
183                 return;
184         }
185
186         user->quitting = true;
187
188         ServerInstance->Logs->Log("USERS", LOG_DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitreason.c_str());
189         user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), *operreason ? operreason : quitreason.c_str());
190
191         std::string reason;
192         std::string oper_reason;
193         reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
194         if (operreason && *operreason)
195                 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
196         else
197                 oper_reason = quitreason;
198
199         ServerInstance->GlobalCulls.AddItem(user);
200
201         if (user->registered == REG_ALL)
202         {
203                 FOREACH_MOD(OnUserQuit, (user, reason, oper_reason));
204                 user->WriteCommonQuit(reason, oper_reason);
205         }
206
207         if (user->registered != REG_ALL)
208                 if (ServerInstance->Users->unregistered_count)
209                         ServerInstance->Users->unregistered_count--;
210
211         if (IS_LOCAL(user))
212         {
213                 LocalUser* lu = IS_LOCAL(user);
214                 FOREACH_MOD(OnUserDisconnect, (lu));
215                 lu->eh.Close();
216         }
217
218         /*
219          * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
220          * if they were an oper with +s +qQ.
221          */
222         if (user->registered == REG_ALL)
223         {
224                 if (IS_LOCAL(user))
225                 {
226                         if (!user->quietquit)
227                         {
228                                 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s (%s) [%s]",
229                                         user->GetFullRealHost().c_str(), user->GetIPString().c_str(), oper_reason.c_str());
230                         }
231                 }
232                 else
233                 {
234                         if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
235                         {
236                                 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s (%s) [%s]",
237                                         user->server.c_str(), user->GetFullRealHost().c_str(), user->GetIPString().c_str(), oper_reason.c_str());
238                         }
239                 }
240         }
241
242         user_hash::iterator iter = this->clientlist->find(user->nick);
243
244         if (iter != this->clientlist->end())
245                 this->clientlist->erase(iter);
246         else
247                 ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "ERROR: Nick not found in clientlist, cannot remove: " + user->nick);
248
249         ServerInstance->Users->uuidlist->erase(user->uuid);
250 }
251
252 void UserManager::AddLocalClone(User *user)
253 {
254         local_clones[user->GetCIDRMask()]++;
255 }
256
257 void UserManager::AddGlobalClone(User *user)
258 {
259         global_clones[user->GetCIDRMask()]++;
260 }
261
262 void UserManager::RemoveCloneCounts(User *user)
263 {
264         if (IS_LOCAL(user))
265         {
266                 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
267                 if (x != local_clones.end())
268                 {
269                         x->second--;
270                         if (!x->second)
271                         {
272                                 local_clones.erase(x);
273                         }
274                 }
275         }
276
277         clonemap::iterator y = global_clones.find(user->GetCIDRMask());
278         if (y != global_clones.end())
279         {
280                 y->second--;
281                 if (!y->second)
282                 {
283                         global_clones.erase(y);
284                 }
285         }
286 }
287
288 unsigned long UserManager::GlobalCloneCount(User *user)
289 {
290         clonemap::iterator x = global_clones.find(user->GetCIDRMask());
291         if (x != global_clones.end())
292                 return x->second;
293         else
294                 return 0;
295 }
296
297 unsigned long UserManager::LocalCloneCount(User *user)
298 {
299         clonemap::iterator x = local_clones.find(user->GetCIDRMask());
300         if (x != local_clones.end())
301                 return x->second;
302         else
303                 return 0;
304 }
305
306 void UserManager::ServerNoticeAll(const char* text, ...)
307 {
308         std::string message;
309         VAFORMAT(message, text, text);
310         message = "NOTICE $" + ServerInstance->Config->ServerName + " :" + message;
311
312         for (LocalUserList::const_iterator i = local_users.begin(); i != local_users.end(); i++)
313         {
314                 User* t = *i;
315                 t->WriteServ(message);
316         }
317 }
318
319 void UserManager::GarbageCollect()
320 {
321         // Reset the already_sent IDs so we don't wrap it around and drop a message
322         LocalUser::already_sent_id = 0;
323         for (LocalUserList::const_iterator i = this->local_users.begin(); i != this->local_users.end(); i++)
324         {
325                 (**i).already_sent = 0;
326                 (**i).RemoveExpiredInvites();
327         }
328 }
329
330 /* this returns true when all modules are satisfied that the user should be allowed onto the irc server
331  * (until this returns true, a user will block in the waiting state, waiting to connect up to the
332  * registration timeout maximum seconds)
333  */
334 bool UserManager::AllModulesReportReady(LocalUser* user)
335 {
336         ModResult res;
337         FIRST_MOD_RESULT(OnCheckReady, res, (user));
338         return (res == MOD_RES_PASSTHRU);
339 }