]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
Change module API to use LocalUser* where correct
[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         if (user->registered != REG_ALL)
202                 if (ServerInstance->Users->unregistered_count)
203                         ServerInstance->Users->unregistered_count--;
204
205         if (IS_LOCAL(user))
206         {
207                 FOREACH_MOD(I_OnUserDisconnect,OnUserDisconnect(IS_LOCAL(user)));
208                 user->DoWrite();
209                 if (user->GetIOHook())
210                 {
211                         try
212                         {
213                                 user->GetIOHook()->OnStreamSocketClose(user);
214                         }
215                         catch (CoreException& modexcept)
216                         {
217                                 ServerInstance->Logs->Log("USERS",DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
218                         }
219                 }
220
221                 ServerInstance->SE->DelFd(user);
222                 user->Close();
223                 // user->Close() will set fd to -1; this breaks IS_LOCAL. Fix
224                 user->SetFd(INT_MAX);
225         }
226
227         /*
228          * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
229          * if they were an oper with +sn +qQ.
230          */
231         if (user->registered == REG_ALL)
232         {
233                 if (IS_LOCAL(user))
234                 {
235                         if (!user->quietquit)
236                         {
237                                 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]",
238                                         user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str());
239                         }
240                 }
241                 else
242                 {
243                         if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
244                         {
245                                 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s]",
246                                         user->server.c_str(), user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str());
247                         }
248                 }
249                 user->AddToWhoWas();
250         }
251
252         user_hash::iterator iter = this->clientlist->find(user->nick);
253
254         if (iter != this->clientlist->end())
255                 this->clientlist->erase(iter);
256         else
257                 ServerInstance->Logs->Log("USERS", DEBUG, "iter == clientlist->end, can't remove them from hash... problematic..");
258 }
259
260
261 void UserManager::AddLocalClone(User *user)
262 {
263         int range = 32;
264         clonemap::iterator x;
265         switch (user->client_sa.sa.sa_family)
266         {
267                 case AF_INET6:
268                         range = ServerInstance->Config->c_ipv6_range;
269                 break;
270                 case AF_INET:
271                         range = ServerInstance->Config->c_ipv4_range;
272                 break;
273         }
274
275         x = local_clones.find(user->GetCIDRMask(range));
276         if (x != local_clones.end())
277                 x->second++;
278         else
279                 local_clones[user->GetCIDRMask(range)] = 1;
280 }
281
282 void UserManager::AddGlobalClone(User *user)
283 {
284         int range = 32;
285         clonemap::iterator x;
286         switch (user->client_sa.sa.sa_family)
287         {
288                 case AF_INET6:
289                         range = ServerInstance->Config->c_ipv6_range;
290                 break;
291                 case AF_INET:
292                         range = ServerInstance->Config->c_ipv4_range;
293                 break;
294         }
295
296         x = global_clones.find(user->GetCIDRMask(range));
297         if (x != global_clones.end())
298                 x->second++;
299         else
300                 global_clones[user->GetCIDRMask(range)] = 1;
301 }
302
303 void UserManager::RemoveCloneCounts(User *user)
304 {
305         int range = 32;
306         switch (user->client_sa.sa.sa_family)
307         {
308                 case AF_INET6:
309                         range = ServerInstance->Config->c_ipv6_range;
310                 break;
311                 case AF_INET:
312                         range = ServerInstance->Config->c_ipv4_range;
313                 break;
314         }
315
316         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
317         if (x != local_clones.end())
318         {
319                 x->second--;
320                 if (!x->second)
321                 {
322                         local_clones.erase(x);
323                 }
324         }
325
326         clonemap::iterator y = global_clones.find(user->GetCIDRMask(range));
327         if (y != global_clones.end())
328         {
329                 y->second--;
330                 if (!y->second)
331                 {
332                         global_clones.erase(y);
333                 }
334         }
335 }
336
337 unsigned long UserManager::GlobalCloneCount(User *user)
338 {
339         int range = 32;
340         switch (user->client_sa.sa.sa_family)
341         {
342                 case AF_INET6:
343                         range = ServerInstance->Config->c_ipv6_range;
344                 break;
345                 case AF_INET:
346                         range = ServerInstance->Config->c_ipv4_range;
347                 break;
348         }
349         clonemap::iterator x = global_clones.find(user->GetCIDRMask(range));
350         if (x != global_clones.end())
351                 return x->second;
352         else
353                 return 0;
354 }
355
356 unsigned long UserManager::LocalCloneCount(User *user)
357 {
358         int range = 32;
359         switch (user->client_sa.sa.sa_family)
360         {
361                 case AF_INET6:
362                         range = ServerInstance->Config->c_ipv6_range;
363                 break;
364                 case AF_INET:
365                         range = ServerInstance->Config->c_ipv4_range;
366                 break;
367         }
368         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
369         if (x != local_clones.end())
370                 return x->second;
371         else
372                 return 0;
373 }
374
375 /* this function counts all users connected, wether they are registered or NOT. */
376 unsigned int UserManager::UserCount()
377 {
378         /*
379          * XXX: Todo:
380          *  As part of this restructuring, move clientlist/etc fields into usermanager.
381          *      -- w00t
382          */
383         return this->clientlist->size();
384 }
385
386 /* this counts only registered users, so that the percentages in /MAP don't mess up */
387 unsigned int UserManager::RegisteredUserCount()
388 {
389         return this->clientlist->size() - this->UnregisteredUserCount();
390 }
391
392 /* return how many users are opered */
393 unsigned int UserManager::OperCount()
394 {
395         return this->all_opers.size();
396 }
397
398 /* return how many users are unregistered */
399 unsigned int UserManager::UnregisteredUserCount()
400 {
401         return this->unregistered_count;
402 }
403
404 /* return how many local registered users there are */
405 unsigned int UserManager::LocalUserCount()
406 {
407         /* Doesnt count unregistered clients */
408         return (this->local_users.size() - this->UnregisteredUserCount());
409 }
410
411 void UserManager::ServerNoticeAll(const char* text, ...)
412 {
413         if (!text)
414                 return;
415
416         char textbuffer[MAXBUF];
417         char formatbuffer[MAXBUF];
418         va_list argsPtr;
419         va_start (argsPtr, text);
420         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
421         va_end(argsPtr);
422
423         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
424
425         for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
426         {
427                 User* t = *i;
428                 t->WriteServ(std::string(formatbuffer));
429         }
430 }
431
432 void UserManager::ServerPrivmsgAll(const char* text, ...)
433 {
434         if (!text)
435                 return;
436
437         char textbuffer[MAXBUF];
438         char formatbuffer[MAXBUF];
439         va_list argsPtr;
440         va_start (argsPtr, text);
441         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
442         va_end(argsPtr);
443
444         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
445
446         for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
447         {
448                 User* t = *i;
449                 t->WriteServ(std::string(formatbuffer));
450         }
451 }
452
453 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
454 {
455         char textbuffer[MAXBUF];
456         int modelen;
457         va_list argsPtr;
458
459         if (!text || !modes || !flags)
460         {
461                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
462                 return;
463         }
464
465         va_start(argsPtr, text);
466         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
467         va_end(argsPtr);
468         modelen = strlen(modes);
469
470         if (flags == WM_AND)
471         {
472                 for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
473                 {
474                         User* t = *i;
475                         bool send_to_user = true;
476
477                         for (int n = 0; n < modelen; n++)
478                         {
479                                 if (!t->IsModeSet(modes[n]))
480                                 {
481                                         send_to_user = false;
482                                         break;
483                                 }
484                         }
485                         if (send_to_user)
486                         {
487                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
488                         }
489                 }
490         }
491         else if (flags == WM_OR)
492         {
493                 for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
494                 {
495                         User* t = *i;
496                         bool send_to_user = false;
497
498                         for (int n = 0; n < modelen; n++)
499                         {
500                                 if (t->IsModeSet(modes[n]))
501                                 {
502                                         send_to_user = true;
503                                         break;
504                                 }
505                         }
506
507                         if (send_to_user)
508                         {
509                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
510                         }
511                 }
512         }
513 }
514
515 /* return how many users have a given mode e.g. 'a' */
516 int UserManager::ModeCount(const char mode)
517 {
518         ModeHandler* mh = ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
519
520         if (mh)
521                 return mh->GetCount();
522         else
523                 return 0;
524 }