]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
Release v2.0.27
[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
27 UserManager::UserManager()
28         : unregistered_count(0), local_count(0)
29 {
30 }
31
32 /* add a client connection to the sockets list */
33 void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
34 {
35         /* NOTE: Calling this one parameter constructor for User automatically
36          * allocates a new UUID and places it in the hash_map.
37          */
38         LocalUser* New = NULL;
39         try
40         {
41                 New = new LocalUser(socket, client, server);
42         }
43         catch (...)
44         {
45                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** WTF *** Duplicated UUID! -- Crack smoking monkeys have been unleashed.");
46                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING *** Duplicate UUID allocated!");
47                 return;
48         }
49         UserIOHandler* eh = &New->eh;
50
51         /* Give each of the modules an attempt to hook the user for I/O */
52         FOREACH_MOD(I_OnHookIO, OnHookIO(eh, via));
53
54         if (eh->GetIOHook())
55         {
56                 try
57                 {
58                         eh->GetIOHook()->OnStreamSocketAccept(eh, client, server);
59                 }
60                 catch (CoreException& modexcept)
61                 {
62                         ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
63                 }
64         }
65
66         ServerInstance->Logs->Log("USERS", DEBUG,"New user fd: %d", socket);
67
68         this->unregistered_count++;
69
70         /* The users default nick is their UUID */
71         New->nick = New->uuid;
72         (*(this->clientlist))[New->nick] = New;
73
74         New->registered = REG_NONE;
75         New->signon = ServerInstance->Time();
76         New->lastping = 1;
77
78         ServerInstance->Users->AddLocalClone(New);
79         ServerInstance->Users->AddGlobalClone(New);
80
81         New->localuseriter = this->local_users.insert(local_users.end(), New);
82         local_count++;
83
84         if ((this->local_users.size() > ServerInstance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)ServerInstance->SE->GetMaxFds()))
85         {
86                 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
87                 this->QuitUser(New,"No more connections allowed");
88                 return;
89         }
90
91         /*
92          * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
93          * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
94          */
95         New->SetClass();
96
97         /*
98          * Check connect class settings and initialise settings into User.
99          * This will be done again after DNS resolution. -- w00t
100          */
101         New->CheckClass();
102         if (New->quitting)
103                 return;
104
105         /*
106          * even with bancache, we still have to keep User::exempt current.
107          * besides that, if we get a positive bancache hit, we still won't fuck
108          * them over if they are exempt. -- w00t
109          */
110         New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
111
112         if (BanCacheHit *b = ServerInstance->BanCache->GetHit(New->GetIPString()))
113         {
114                 if (!b->Type.empty() && !New->exempt)
115                 {
116                         /* user banned */
117                         ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
118                         if (!ServerInstance->Config->MoronBanner.empty())
119                                 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), ServerInstance->Config->MoronBanner.c_str());
120
121                         if (ServerInstance->Config->HideBans)
122                                 this->QuitUser(New, b->Type + "-Lined", b->Reason.c_str());
123                         else
124                                 this->QuitUser(New, b->Reason);
125                         return;
126                 }
127                 else
128                 {
129                         ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
130                 }
131         }
132         else
133         {
134                 if (!New->exempt)
135                 {
136                         XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
137
138                         if (r)
139                         {
140                                 r->Apply(New);
141                                 return;
142                         }
143                 }
144         }
145
146         if (!ServerInstance->SE->AddFd(eh, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
147         {
148                 ServerInstance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
149                 this->QuitUser(New, "Internal error handling connection");
150                 return;
151         }
152
153         /* NOTE: even if dns lookups are *off*, we still need to display this.
154          * BOPM and other stuff requires it.
155          */
156         New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
157         if (ServerInstance->Config->RawLog)
158                 New->WriteServ("NOTICE Auth :*** Raw I/O logging is enabled on this server. All messages, passwords, and commands are being recorded.");
159
160         FOREACH_MOD(I_OnSetUserIP,OnSetUserIP(New));
161         if (New->quitting)
162                 return;
163
164         FOREACH_MOD(I_OnUserInit,OnUserInit(New));
165
166         if (ServerInstance->Config->NoUserDns)
167         {
168                 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
169                 New->dns_done = true;
170         }
171         else
172         {
173                 New->StartDNSLookup();
174         }
175 }
176
177 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
178 {
179         if (user->quitting)
180         {
181                 ServerInstance->Logs->Log("USERS", DEFAULT, "ERROR: Tried to quit quitting user: " + user->nick);
182                 return;
183         }
184
185         if (IS_SERVER(user))
186         {
187                 ServerInstance->Logs->Log("USERS", DEFAULT, "ERROR: Tried to quit server user: " + user->nick);
188                 return;
189         }
190
191         user->quitting = true;
192
193         ServerInstance->Logs->Log("USERS", DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitreason.c_str());
194         user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), *operreason ? operreason : quitreason.c_str());
195
196         std::string reason;
197         std::string oper_reason;
198         reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
199         if (operreason && *operreason)
200                 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
201         else
202                 oper_reason = quitreason;
203
204         ServerInstance->GlobalCulls.AddItem(user);
205
206         if (user->registered == REG_ALL)
207         {
208                 FOREACH_MOD(I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
209                 user->WriteCommonQuit(reason, oper_reason);
210         }
211
212         if (user->registered != REG_ALL)
213                 if (ServerInstance->Users->unregistered_count)
214                         ServerInstance->Users->unregistered_count--;
215
216         if (IS_LOCAL(user))
217         {
218                 LocalUser* lu = IS_LOCAL(user);
219                 FOREACH_MOD(I_OnUserDisconnect,OnUserDisconnect(lu));
220                 lu->eh.Close();
221         }
222
223         /*
224          * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
225          * if they were an oper with +s +qQ.
226          */
227         if (user->registered == REG_ALL)
228         {
229                 if (IS_LOCAL(user))
230                 {
231                         if (!user->quietquit)
232                         {
233                                 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s (%s) [%s]",
234                                         user->GetFullRealHost().c_str(), user->GetIPString(), oper_reason.c_str());
235                         }
236                 }
237                 else
238                 {
239                         if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
240                         {
241                                 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s (%s) [%s]",
242                                         user->server.c_str(), user->GetFullRealHost().c_str(), user->GetIPString(), oper_reason.c_str());
243                         }
244                 }
245                 user->AddToWhoWas();
246         }
247
248         user_hash::iterator iter = this->clientlist->find(user->nick);
249
250         if (iter != this->clientlist->end())
251                 this->clientlist->erase(iter);
252         else
253                 ServerInstance->Logs->Log("USERS", DEFAULT, "ERROR: Nick not found in clientlist, cannot remove: " + user->nick);
254
255         ServerInstance->Users->uuidlist->erase(user->uuid);
256 }
257
258
259 void UserManager::AddLocalClone(User *user)
260 {
261         local_clones[user->GetCIDRMask()]++;
262 }
263
264 void UserManager::AddGlobalClone(User *user)
265 {
266         global_clones[user->GetCIDRMask()]++;
267 }
268
269 void UserManager::RemoveCloneCounts(User *user)
270 {
271         if (IS_LOCAL(user))
272         {
273                 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
274                 if (x != local_clones.end())
275                 {
276                         x->second--;
277                         if (!x->second)
278                         {
279                                 local_clones.erase(x);
280                         }
281                 }
282         }
283
284         clonemap::iterator y = global_clones.find(user->GetCIDRMask());
285         if (y != global_clones.end())
286         {
287                 y->second--;
288                 if (!y->second)
289                 {
290                         global_clones.erase(y);
291                 }
292         }
293 }
294
295 void UserManager::RehashCloneCounts()
296 {
297         local_clones.clear();
298         global_clones.clear();
299
300         const user_hash& hash = *ServerInstance->Users->clientlist;
301         for (user_hash::const_iterator i = hash.begin(); i != hash.end(); ++i)
302         {
303                 User* u = i->second;
304
305                 if (IS_LOCAL(u))
306                         AddLocalClone(u);
307                 AddGlobalClone(u);
308         }
309 }
310
311 unsigned long UserManager::GlobalCloneCount(User *user)
312 {
313         clonemap::iterator x = global_clones.find(user->GetCIDRMask());
314         if (x != global_clones.end())
315                 return x->second;
316         else
317                 return 0;
318 }
319
320 unsigned long UserManager::LocalCloneCount(User *user)
321 {
322         clonemap::iterator x = local_clones.find(user->GetCIDRMask());
323         if (x != local_clones.end())
324                 return x->second;
325         else
326                 return 0;
327 }
328
329 /* this function counts all users connected, wether they are registered or NOT. */
330 unsigned int UserManager::UserCount()
331 {
332         /*
333          * XXX: Todo:
334          *  As part of this restructuring, move clientlist/etc fields into usermanager.
335          *      -- w00t
336          */
337         return this->clientlist->size();
338 }
339
340 /* this counts only registered users, so that the percentages in /MAP don't mess up */
341 unsigned int UserManager::RegisteredUserCount()
342 {
343         return this->clientlist->size() - this->UnregisteredUserCount();
344 }
345
346 /* return how many users are opered */
347 unsigned int UserManager::OperCount()
348 {
349         return this->all_opers.size();
350 }
351
352 /* return how many users are unregistered */
353 unsigned int UserManager::UnregisteredUserCount()
354 {
355         return this->unregistered_count;
356 }
357
358 /* return how many local registered users there are */
359 unsigned int UserManager::LocalUserCount()
360 {
361         /* Doesnt count unregistered clients */
362         return (this->local_count - this->UnregisteredUserCount());
363 }
364
365 void UserManager::ServerNoticeAll(const char* text, ...)
366 {
367         if (!text)
368                 return;
369
370         char textbuffer[MAXBUF];
371         char formatbuffer[MAXBUF];
372         va_list argsPtr;
373         va_start (argsPtr, text);
374         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
375         va_end(argsPtr);
376
377         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
378
379         for (LocalUserList::const_iterator i = local_users.begin(); i != local_users.end(); i++)
380         {
381                 User* t = *i;
382                 t->WriteServ(std::string(formatbuffer));
383         }
384 }
385
386 void UserManager::ServerPrivmsgAll(const char* text, ...)
387 {
388         if (!text)
389                 return;
390
391         char textbuffer[MAXBUF];
392         char formatbuffer[MAXBUF];
393         va_list argsPtr;
394         va_start (argsPtr, text);
395         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
396         va_end(argsPtr);
397
398         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
399
400         for (LocalUserList::const_iterator i = local_users.begin(); i != local_users.end(); i++)
401         {
402                 User* t = *i;
403                 t->WriteServ(std::string(formatbuffer));
404         }
405 }
406
407
408 /* return how many users have a given mode e.g. 'a' */
409 int UserManager::ModeCount(const char mode)
410 {
411         int c = 0;
412         for(user_hash::iterator i = clientlist->begin(); i != clientlist->end(); ++i)
413         {
414                 User* u = i->second;
415                 if (u->modes[mode-65])
416                         c++;
417         }
418         return c;
419 }