]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
Split LocalUser and RemoteUser
[user/henk/code/inspircd.git] / src / usermanager.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core */
15
16 #include "inspircd.h"
17 #include "xline.h"
18 #include "bancache.h"
19
20 /* add a client connection to the sockets list */
21 void UserManager::AddUser(int socket, ClientListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
22 {
23         /* NOTE: Calling this one parameter constructor for User automatically
24          * allocates a new UUID and places it in the hash_map.
25          */
26         LocalUser* New = NULL;
27         try
28         {
29                 New = new LocalUser();
30         }
31         catch (...)
32         {
33                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** WTF *** Duplicated UUID! -- Crack smoking monkies have been unleashed.");
34                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING *** Duplicate UUID allocated!");
35                 return;
36         }
37
38         New->SetFd(socket);
39         memcpy(&New->client_sa, client, sizeof(irc::sockets::sockaddrs));
40         memcpy(&New->server_sa, server, sizeof(irc::sockets::sockaddrs));
41
42         /* Give each of the modules an attempt to hook the user for I/O */
43         FOREACH_MOD(I_OnHookIO, OnHookIO(New, via));
44
45         if (New->GetIOHook())
46         {
47                 try
48                 {
49                         New->GetIOHook()->OnStreamSocketAccept(New, client, server);
50                 }
51                 catch (CoreException& modexcept)
52                 {
53                         ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
54                 }
55         }
56
57         ServerInstance->Logs->Log("USERS", DEBUG,"New user fd: %d", socket);
58
59         this->unregistered_count++;
60
61         (*(this->clientlist))[New->uuid] = New;
62
63         /* The users default nick is their UUID */
64         New->nick.assign(New->uuid, 0, ServerInstance->Config->Limits.NickMax);
65
66         New->server = ServerInstance->Config->ServerName;
67         New->ident.assign("unknown");
68
69         New->registered = REG_NONE;
70         New->signon = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
71         New->lastping = 1;
72
73         /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
74         New->dhost.assign(New->GetIPString(), 0, 64);
75         New->host.assign(New->GetIPString(), 0, 64);
76
77         ServerInstance->Users->AddLocalClone(New);
78         ServerInstance->Users->AddGlobalClone(New);
79
80         /*
81          * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
82          * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
83          */
84         ConnectClass* i = New->SetClass();
85
86         if (!i)
87         {
88                 this->QuitUser(New, "Access denied by configuration");
89                 return;
90         }
91
92         /*
93          * Check connect class settings and initialise settings into User.
94          * This will be done again after DNS resolution. -- w00t
95          */
96         New->CheckClass();
97
98         this->local_users.push_back(New);
99
100         if ((this->local_users.size() > ServerInstance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)ServerInstance->SE->GetMaxFds()))
101         {
102                 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
103                 this->QuitUser(New,"No more connections allowed");
104                 return;
105         }
106
107         /*
108          * even with bancache, we still have to keep User::exempt current.
109          * besides that, if we get a positive bancache hit, we still won't fuck
110          * them over if they are exempt. -- w00t
111          */
112         New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
113
114         if (BanCacheHit *b = ServerInstance->BanCache->GetHit(New->GetIPString()))
115         {
116                 if (!b->Type.empty() && !New->exempt)
117                 {
118                         /* user banned */
119                         ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
120                         if (!ServerInstance->Config->MoronBanner.empty())
121                                 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), ServerInstance->Config->MoronBanner.c_str());
122                         this->QuitUser(New, b->Reason);
123                         return;
124                 }
125                 else
126                 {
127                         ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
128                 }
129         }
130         else
131         {
132                 if (!New->exempt)
133                 {
134                         XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
135
136                         if (r)
137                         {
138                                 r->Apply(New);
139                                 return;
140                         }
141                 }
142         }
143
144         if (!ServerInstance->SE->AddFd(New, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
145         {
146                 ServerInstance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
147                 this->QuitUser(New, "Internal error handling connection");
148         }
149
150         /* NOTE: even if dns lookups are *off*, we still need to display this.
151          * BOPM and other stuff requires it.
152          */
153         New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
154
155         if (ServerInstance->Config->NoUserDns)
156         {
157                 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
158                 New->dns_done = true;
159         }
160         else
161         {
162                 New->StartDNSLookup();
163         }
164 }
165
166 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
167 {
168         if (user->quitting)
169         {
170                 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a user (%s) twice. Did your module call QuitUser twice?", user->nick.c_str());
171                 return;
172         }
173
174         if (IS_SERVER(user))
175         {
176                 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a fake user (%s)", user->nick.c_str());
177                 return;
178         }
179
180         user->quitting = true;
181
182         ServerInstance->Logs->Log("USERS", DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitreason.c_str());
183         user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), *operreason ? operreason : quitreason.c_str());
184
185         std::string reason;
186         std::string oper_reason;
187         reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
188         if (operreason && *operreason)
189                 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
190         else
191                 oper_reason = quitreason;
192
193         ServerInstance->GlobalCulls.AddItem(user);
194
195         if (user->registered == REG_ALL)
196         {
197                 FOREACH_MOD(I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
198                 user->WriteCommonQuit(reason, oper_reason);
199         }
200
201         FOREACH_MOD(I_OnUserDisconnect,OnUserDisconnect(user));
202
203         if (user->registered != REG_ALL)
204                 if (ServerInstance->Users->unregistered_count)
205                         ServerInstance->Users->unregistered_count--;
206
207         if (IS_LOCAL(user))
208         {
209                 user->DoWrite();
210                 if (user->GetIOHook())
211                 {
212                         try
213                         {
214                                 user->GetIOHook()->OnStreamSocketClose(user);
215                         }
216                         catch (CoreException& modexcept)
217                         {
218                                 ServerInstance->Logs->Log("USERS",DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
219                         }
220                 }
221
222                 ServerInstance->SE->DelFd(user);
223                 user->Close();
224                 // user->Close() will set fd to -1; this breaks IS_LOCAL. Fix
225                 user->SetFd(INT_MAX);
226         }
227
228         /*
229          * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
230          * if they were an oper with +sn +qQ.
231          */
232         if (user->registered == REG_ALL)
233         {
234                 if (IS_LOCAL(user))
235                 {
236                         if (!user->quietquit)
237                         {
238                                 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]",
239                                         user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str());
240                         }
241                 }
242                 else
243                 {
244                         if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
245                         {
246                                 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s]",
247                                         user->server.c_str(), user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str());
248                         }
249                 }
250                 user->AddToWhoWas();
251         }
252
253         user_hash::iterator iter = this->clientlist->find(user->nick);
254
255         if (iter != this->clientlist->end())
256                 this->clientlist->erase(iter);
257         else
258                 ServerInstance->Logs->Log("USERS", DEBUG, "iter == clientlist->end, can't remove them from hash... problematic..");
259 }
260
261
262 void UserManager::AddLocalClone(User *user)
263 {
264         int range = 32;
265         clonemap::iterator x;
266         switch (user->client_sa.sa.sa_family)
267         {
268                 case AF_INET6:
269                         range = ServerInstance->Config->c_ipv6_range;
270                 break;
271                 case AF_INET:
272                         range = ServerInstance->Config->c_ipv4_range;
273                 break;
274         }
275
276         x = local_clones.find(user->GetCIDRMask(range));
277         if (x != local_clones.end())
278                 x->second++;
279         else
280                 local_clones[user->GetCIDRMask(range)] = 1;
281 }
282
283 void UserManager::AddGlobalClone(User *user)
284 {
285         int range = 32;
286         clonemap::iterator x;
287         switch (user->client_sa.sa.sa_family)
288         {
289                 case AF_INET6:
290                         range = ServerInstance->Config->c_ipv6_range;
291                 break;
292                 case AF_INET:
293                         range = ServerInstance->Config->c_ipv4_range;
294                 break;
295         }
296
297         x = global_clones.find(user->GetCIDRMask(range));
298         if (x != global_clones.end())
299                 x->second++;
300         else
301                 global_clones[user->GetCIDRMask(range)] = 1;
302 }
303
304 void UserManager::RemoveCloneCounts(User *user)
305 {
306         int range = 32;
307         switch (user->client_sa.sa.sa_family)
308         {
309                 case AF_INET6:
310                         range = ServerInstance->Config->c_ipv6_range;
311                 break;
312                 case AF_INET:
313                         range = ServerInstance->Config->c_ipv4_range;
314                 break;
315         }
316
317         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
318         if (x != local_clones.end())
319         {
320                 x->second--;
321                 if (!x->second)
322                 {
323                         local_clones.erase(x);
324                 }
325         }
326
327         clonemap::iterator y = global_clones.find(user->GetCIDRMask(range));
328         if (y != global_clones.end())
329         {
330                 y->second--;
331                 if (!y->second)
332                 {
333                         global_clones.erase(y);
334                 }
335         }
336 }
337
338 unsigned long UserManager::GlobalCloneCount(User *user)
339 {
340         int range = 32;
341         switch (user->client_sa.sa.sa_family)
342         {
343                 case AF_INET6:
344                         range = ServerInstance->Config->c_ipv6_range;
345                 break;
346                 case AF_INET:
347                         range = ServerInstance->Config->c_ipv4_range;
348                 break;
349         }
350         clonemap::iterator x = global_clones.find(user->GetCIDRMask(range));
351         if (x != global_clones.end())
352                 return x->second;
353         else
354                 return 0;
355 }
356
357 unsigned long UserManager::LocalCloneCount(User *user)
358 {
359         int range = 32;
360         switch (user->client_sa.sa.sa_family)
361         {
362                 case AF_INET6:
363                         range = ServerInstance->Config->c_ipv6_range;
364                 break;
365                 case AF_INET:
366                         range = ServerInstance->Config->c_ipv4_range;
367                 break;
368         }
369         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
370         if (x != local_clones.end())
371                 return x->second;
372         else
373                 return 0;
374 }
375
376 /* this function counts all users connected, wether they are registered or NOT. */
377 unsigned int UserManager::UserCount()
378 {
379         /*
380          * XXX: Todo:
381          *  As part of this restructuring, move clientlist/etc fields into usermanager.
382          *      -- w00t
383          */
384         return this->clientlist->size();
385 }
386
387 /* this counts only registered users, so that the percentages in /MAP don't mess up */
388 unsigned int UserManager::RegisteredUserCount()
389 {
390         return this->clientlist->size() - this->UnregisteredUserCount();
391 }
392
393 /* return how many users are opered */
394 unsigned int UserManager::OperCount()
395 {
396         return this->all_opers.size();
397 }
398
399 /* return how many users are unregistered */
400 unsigned int UserManager::UnregisteredUserCount()
401 {
402         return this->unregistered_count;
403 }
404
405 /* return how many local registered users there are */
406 unsigned int UserManager::LocalUserCount()
407 {
408         /* Doesnt count unregistered clients */
409         return (this->local_users.size() - this->UnregisteredUserCount());
410 }
411
412 void UserManager::ServerNoticeAll(const char* text, ...)
413 {
414         if (!text)
415                 return;
416
417         char textbuffer[MAXBUF];
418         char formatbuffer[MAXBUF];
419         va_list argsPtr;
420         va_start (argsPtr, text);
421         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
422         va_end(argsPtr);
423
424         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
425
426         for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
427         {
428                 User* t = *i;
429                 t->WriteServ(std::string(formatbuffer));
430         }
431 }
432
433 void UserManager::ServerPrivmsgAll(const char* text, ...)
434 {
435         if (!text)
436                 return;
437
438         char textbuffer[MAXBUF];
439         char formatbuffer[MAXBUF];
440         va_list argsPtr;
441         va_start (argsPtr, text);
442         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
443         va_end(argsPtr);
444
445         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
446
447         for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
448         {
449                 User* t = *i;
450                 t->WriteServ(std::string(formatbuffer));
451         }
452 }
453
454 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
455 {
456         char textbuffer[MAXBUF];
457         int modelen;
458         va_list argsPtr;
459
460         if (!text || !modes || !flags)
461         {
462                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
463                 return;
464         }
465
466         va_start(argsPtr, text);
467         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
468         va_end(argsPtr);
469         modelen = strlen(modes);
470
471         if (flags == WM_AND)
472         {
473                 for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
474                 {
475                         User* t = *i;
476                         bool send_to_user = true;
477
478                         for (int n = 0; n < modelen; n++)
479                         {
480                                 if (!t->IsModeSet(modes[n]))
481                                 {
482                                         send_to_user = false;
483                                         break;
484                                 }
485                         }
486                         if (send_to_user)
487                         {
488                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
489                         }
490                 }
491         }
492         else if (flags == WM_OR)
493         {
494                 for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
495                 {
496                         User* t = *i;
497                         bool send_to_user = false;
498
499                         for (int n = 0; n < modelen; n++)
500                         {
501                                 if (t->IsModeSet(modes[n]))
502                                 {
503                                         send_to_user = true;
504                                         break;
505                                 }
506                         }
507
508                         if (send_to_user)
509                         {
510                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
511                         }
512                 }
513         }
514 }
515
516 /* return how many users have a given mode e.g. 'a' */
517 int UserManager::ModeCount(const char mode)
518 {
519         ModeHandler* mh = ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
520
521         if (mh)
522                 return mh->GetCount();
523         else
524                 return 0;
525 }