]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
Kill ListenSocketBase, use OnAcceptConnection for all new connections
[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, ListenSocket* 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         New->SetClass();
85
86         /*
87          * Check connect class settings and initialise settings into User.
88          * This will be done again after DNS resolution. -- w00t
89          */
90         New->CheckClass();
91
92         this->local_users.push_back(New);
93
94         if ((this->local_users.size() > ServerInstance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)ServerInstance->SE->GetMaxFds()))
95         {
96                 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
97                 this->QuitUser(New,"No more connections allowed");
98                 return;
99         }
100
101         /*
102          * even with bancache, we still have to keep User::exempt current.
103          * besides that, if we get a positive bancache hit, we still won't fuck
104          * them over if they are exempt. -- w00t
105          */
106         New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
107
108         if (BanCacheHit *b = ServerInstance->BanCache->GetHit(New->GetIPString()))
109         {
110                 if (!b->Type.empty() && !New->exempt)
111                 {
112                         /* user banned */
113                         ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
114                         if (!ServerInstance->Config->MoronBanner.empty())
115                                 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), ServerInstance->Config->MoronBanner.c_str());
116                         this->QuitUser(New, b->Reason);
117                         return;
118                 }
119                 else
120                 {
121                         ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
122                 }
123         }
124         else
125         {
126                 if (!New->exempt)
127                 {
128                         XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
129
130                         if (r)
131                         {
132                                 r->Apply(New);
133                                 return;
134                         }
135                 }
136         }
137
138         if (!ServerInstance->SE->AddFd(New, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
139         {
140                 ServerInstance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
141                 this->QuitUser(New, "Internal error handling connection");
142         }
143
144         /* NOTE: even if dns lookups are *off*, we still need to display this.
145          * BOPM and other stuff requires it.
146          */
147         New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
148
149         if (ServerInstance->Config->NoUserDns)
150         {
151                 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
152                 New->dns_done = true;
153         }
154         else
155         {
156                 New->StartDNSLookup();
157         }
158 }
159
160 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
161 {
162         if (user->quitting)
163         {
164                 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a user (%s) twice. Did your module call QuitUser twice?", user->nick.c_str());
165                 return;
166         }
167
168         if (IS_SERVER(user))
169         {
170                 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a fake user (%s)", user->nick.c_str());
171                 return;
172         }
173
174         user->quitting = true;
175
176         ServerInstance->Logs->Log("USERS", DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitreason.c_str());
177         user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), *operreason ? operreason : quitreason.c_str());
178
179         std::string reason;
180         std::string oper_reason;
181         reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
182         if (operreason && *operreason)
183                 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
184         else
185                 oper_reason = quitreason;
186
187         ServerInstance->GlobalCulls.AddItem(user);
188
189         if (user->registered == REG_ALL)
190         {
191                 FOREACH_MOD(I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
192                 user->WriteCommonQuit(reason, oper_reason);
193         }
194
195         if (user->registered != REG_ALL)
196                 if (ServerInstance->Users->unregistered_count)
197                         ServerInstance->Users->unregistered_count--;
198
199         if (IS_LOCAL(user))
200         {
201                 FOREACH_MOD(I_OnUserDisconnect,OnUserDisconnect(IS_LOCAL(user)));
202                 user->DoWrite();
203                 if (user->GetIOHook())
204                 {
205                         try
206                         {
207                                 user->GetIOHook()->OnStreamSocketClose(user);
208                         }
209                         catch (CoreException& modexcept)
210                         {
211                                 ServerInstance->Logs->Log("USERS",DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
212                         }
213                 }
214
215                 ServerInstance->SE->DelFd(user);
216                 user->Close();
217                 // user->Close() will set fd to -1; this breaks IS_LOCAL. Fix
218                 user->SetFd(INT_MAX);
219         }
220
221         /*
222          * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
223          * if they were an oper with +sn +qQ.
224          */
225         if (user->registered == REG_ALL)
226         {
227                 if (IS_LOCAL(user))
228                 {
229                         if (!user->quietquit)
230                         {
231                                 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]",
232                                         user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str());
233                         }
234                 }
235                 else
236                 {
237                         if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
238                         {
239                                 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s]",
240                                         user->server.c_str(), user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str());
241                         }
242                 }
243                 user->AddToWhoWas();
244         }
245
246         user_hash::iterator iter = this->clientlist->find(user->nick);
247
248         if (iter != this->clientlist->end())
249                 this->clientlist->erase(iter);
250         else
251                 ServerInstance->Logs->Log("USERS", DEBUG, "iter == clientlist->end, can't remove them from hash... problematic..");
252 }
253
254
255 void UserManager::AddLocalClone(User *user)
256 {
257         int range = 32;
258         clonemap::iterator x;
259         switch (user->client_sa.sa.sa_family)
260         {
261                 case AF_INET6:
262                         range = ServerInstance->Config->c_ipv6_range;
263                 break;
264                 case AF_INET:
265                         range = ServerInstance->Config->c_ipv4_range;
266                 break;
267         }
268
269         x = local_clones.find(user->GetCIDRMask(range));
270         if (x != local_clones.end())
271                 x->second++;
272         else
273                 local_clones[user->GetCIDRMask(range)] = 1;
274 }
275
276 void UserManager::AddGlobalClone(User *user)
277 {
278         int range = 32;
279         clonemap::iterator x;
280         switch (user->client_sa.sa.sa_family)
281         {
282                 case AF_INET6:
283                         range = ServerInstance->Config->c_ipv6_range;
284                 break;
285                 case AF_INET:
286                         range = ServerInstance->Config->c_ipv4_range;
287                 break;
288         }
289
290         x = global_clones.find(user->GetCIDRMask(range));
291         if (x != global_clones.end())
292                 x->second++;
293         else
294                 global_clones[user->GetCIDRMask(range)] = 1;
295 }
296
297 void UserManager::RemoveCloneCounts(User *user)
298 {
299         int range = 32;
300         switch (user->client_sa.sa.sa_family)
301         {
302                 case AF_INET6:
303                         range = ServerInstance->Config->c_ipv6_range;
304                 break;
305                 case AF_INET:
306                         range = ServerInstance->Config->c_ipv4_range;
307                 break;
308         }
309
310         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
311         if (x != local_clones.end())
312         {
313                 x->second--;
314                 if (!x->second)
315                 {
316                         local_clones.erase(x);
317                 }
318         }
319
320         clonemap::iterator y = global_clones.find(user->GetCIDRMask(range));
321         if (y != global_clones.end())
322         {
323                 y->second--;
324                 if (!y->second)
325                 {
326                         global_clones.erase(y);
327                 }
328         }
329 }
330
331 unsigned long UserManager::GlobalCloneCount(User *user)
332 {
333         int range = 32;
334         switch (user->client_sa.sa.sa_family)
335         {
336                 case AF_INET6:
337                         range = ServerInstance->Config->c_ipv6_range;
338                 break;
339                 case AF_INET:
340                         range = ServerInstance->Config->c_ipv4_range;
341                 break;
342         }
343         clonemap::iterator x = global_clones.find(user->GetCIDRMask(range));
344         if (x != global_clones.end())
345                 return x->second;
346         else
347                 return 0;
348 }
349
350 unsigned long UserManager::LocalCloneCount(User *user)
351 {
352         int range = 32;
353         switch (user->client_sa.sa.sa_family)
354         {
355                 case AF_INET6:
356                         range = ServerInstance->Config->c_ipv6_range;
357                 break;
358                 case AF_INET:
359                         range = ServerInstance->Config->c_ipv4_range;
360                 break;
361         }
362         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
363         if (x != local_clones.end())
364                 return x->second;
365         else
366                 return 0;
367 }
368
369 /* this function counts all users connected, wether they are registered or NOT. */
370 unsigned int UserManager::UserCount()
371 {
372         /*
373          * XXX: Todo:
374          *  As part of this restructuring, move clientlist/etc fields into usermanager.
375          *      -- w00t
376          */
377         return this->clientlist->size();
378 }
379
380 /* this counts only registered users, so that the percentages in /MAP don't mess up */
381 unsigned int UserManager::RegisteredUserCount()
382 {
383         return this->clientlist->size() - this->UnregisteredUserCount();
384 }
385
386 /* return how many users are opered */
387 unsigned int UserManager::OperCount()
388 {
389         return this->all_opers.size();
390 }
391
392 /* return how many users are unregistered */
393 unsigned int UserManager::UnregisteredUserCount()
394 {
395         return this->unregistered_count;
396 }
397
398 /* return how many local registered users there are */
399 unsigned int UserManager::LocalUserCount()
400 {
401         /* Doesnt count unregistered clients */
402         return (this->local_users.size() - this->UnregisteredUserCount());
403 }
404
405 void UserManager::ServerNoticeAll(const char* text, ...)
406 {
407         if (!text)
408                 return;
409
410         char textbuffer[MAXBUF];
411         char formatbuffer[MAXBUF];
412         va_list argsPtr;
413         va_start (argsPtr, text);
414         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
415         va_end(argsPtr);
416
417         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
418
419         for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
420         {
421                 User* t = *i;
422                 t->WriteServ(std::string(formatbuffer));
423         }
424 }
425
426 void UserManager::ServerPrivmsgAll(const char* text, ...)
427 {
428         if (!text)
429                 return;
430
431         char textbuffer[MAXBUF];
432         char formatbuffer[MAXBUF];
433         va_list argsPtr;
434         va_start (argsPtr, text);
435         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
436         va_end(argsPtr);
437
438         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
439
440         for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
441         {
442                 User* t = *i;
443                 t->WriteServ(std::string(formatbuffer));
444         }
445 }
446
447 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
448 {
449         char textbuffer[MAXBUF];
450         int modelen;
451         va_list argsPtr;
452
453         if (!text || !modes || !flags)
454         {
455                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
456                 return;
457         }
458
459         va_start(argsPtr, text);
460         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
461         va_end(argsPtr);
462         modelen = strlen(modes);
463
464         if (flags == WM_AND)
465         {
466                 for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
467                 {
468                         User* t = *i;
469                         bool send_to_user = true;
470
471                         for (int n = 0; n < modelen; n++)
472                         {
473                                 if (!t->IsModeSet(modes[n]))
474                                 {
475                                         send_to_user = false;
476                                         break;
477                                 }
478                         }
479                         if (send_to_user)
480                         {
481                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
482                         }
483                 }
484         }
485         else if (flags == WM_OR)
486         {
487                 for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
488                 {
489                         User* t = *i;
490                         bool send_to_user = false;
491
492                         for (int n = 0; n < modelen; n++)
493                         {
494                                 if (t->IsModeSet(modes[n]))
495                                 {
496                                         send_to_user = true;
497                                         break;
498                                 }
499                         }
500
501                         if (send_to_user)
502                         {
503                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
504                         }
505                 }
506         }
507 }
508
509 /* return how many users have a given mode e.g. 'a' */
510 int UserManager::ModeCount(const char mode)
511 {
512         ModeHandler* mh = ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
513
514         if (mh)
515                 return mh->GetCount();
516         else
517                 return 0;
518 }