]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
Prevent fake users from quitting
[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(InspIRCd* Instance, 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         User* New = NULL;
27         try
28         {
29                 New = new User(Instance);
30         }
31         catch (...)
32         {
33                 Instance->Logs->Log("USERS", DEFAULT,"*** WTF *** Duplicated UUID! -- Crack smoking monkies have been unleashed.");
34                 Instance->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(Instance, I_OnHookIO, OnHookIO(New, via));
44
45         if (New->GetIOHook())
46         {
47                 try
48                 {
49                         New->GetIOHook()->OnRawSocketAccept(socket, 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         Instance->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 = Instance->FindServerNamePtr(Instance->Config->ServerName);
67         New->ident.assign("unknown");
68
69         New->registered = REG_NONE;
70         New->signon = Instance->Time() + Instance->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         Instance->Users->AddLocalClone(New);
78         Instance->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() > Instance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)Instance->SE->GetMaxFds()))
101         {
102                 Instance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", Instance->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 = (Instance->XLines->MatchesLine("E",New) != NULL);
113
114         if (BanCacheHit *b = Instance->BanCache->GetHit(New->GetIPString()))
115         {
116                 if (!b->Type.empty() && !New->exempt)
117                 {
118                         /* user banned */
119                         Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
120                         if (*Instance->Config->MoronBanner)
121                                 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), Instance->Config->MoronBanner);
122                         this->QuitUser(New, b->Reason);
123                         return;
124                 }
125                 else
126                 {
127                         Instance->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 = Instance->XLines->MatchesLine("Z",New);
135
136                         if (r)
137                         {
138                                 r->Apply(New);
139                                 return;
140                         }
141                 }
142         }
143
144         if (!Instance->SE->AddFd(New))
145         {
146                 Instance->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 (Instance->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_FAKE(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         if (IS_FAKE(user))
181         {
182                 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a fake user (%s)", user->nick.c_str());
183                 return;
184         }
185
186         user->quitting = true;
187
188         ServerInstance->Logs->Log("USERS", DEBUG, "QuitUser: %s '%s'", 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_I(ServerInstance,I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
204                 user->PurgeEmptyChannels();
205                 user->WriteCommonQuit(reason, oper_reason);
206         }
207
208         FOREACH_MOD_I(ServerInstance,I_OnUserDisconnect,OnUserDisconnect(user));
209
210         user->UpdateNickHash(user->uuid.c_str());
211
212         user_hash::iterator iter = this->clientlist->find(user->uuid);
213
214         if (user->registered != REG_ALL)
215                 if (ServerInstance->Users->unregistered_count)
216                         ServerInstance->Users->unregistered_count--;
217
218         if (IS_LOCAL(user))
219         {
220                 if (!user->sendq.empty())
221                         user->FlushWriteBuf();
222
223                 if (user->GetIOHook())
224                 {
225                         try
226                         {
227                                 user->GetIOHook()->OnRawSocketClose(user->GetFd());
228                         }
229                         catch (CoreException& modexcept)
230                         {
231                                 ServerInstance->Logs->Log("USERS",DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
232                         }
233                 }
234
235                 ServerInstance->SE->DelFd(user);
236                 user->CloseSocket();
237         }
238
239         /*
240          * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
241          * if they were an oper with +sn +qQ.
242          */
243         if (user->registered == REG_ALL)
244         {
245                 if (IS_LOCAL(user))
246                 {
247                         if (!user->quietquit)
248                         {
249                                 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s]",
250                                         user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str());
251                         }
252                 }
253                 else
254                 {
255                         if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
256                         {
257                                 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s]",
258                                         user->server, user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str());
259                         }
260                 }
261                 user->AddToWhoWas();
262         }
263
264         if (iter != this->clientlist->end())
265                 this->clientlist->erase(iter);
266         else
267                 ServerInstance->Logs->Log("USERS", DEBUG, "iter == clientlist->end, can't remove them from hash... problematic..");
268
269         if (IS_LOCAL(user))
270         {
271                 std::vector<User*>::iterator x = find(local_users.begin(),local_users.end(),user);
272                 if (x != local_users.end())
273                         local_users.erase(x);
274                 else
275                         ServerInstance->Logs->Log("USERS", DEBUG, "Failed to remove user from vector");
276         }
277 }
278
279
280 void UserManager::AddLocalClone(User *user)
281 {
282         int range = 32;
283         clonemap::iterator x;
284         switch (user->client_sa.sa.sa_family)
285         {
286                 case AF_INET6:
287                         range = ServerInstance->Config->c_ipv6_range;
288                 break;
289                 case AF_INET:
290                         range = ServerInstance->Config->c_ipv4_range;
291                 break;
292         }
293
294         x = local_clones.find(user->GetCIDRMask(range));
295         if (x != local_clones.end())
296                 x->second++;
297         else
298                 local_clones[user->GetCIDRMask(range)] = 1;
299 }
300
301 void UserManager::AddGlobalClone(User *user)
302 {
303         int range = 32;
304         clonemap::iterator x;
305         switch (user->client_sa.sa.sa_family)
306         {
307                 case AF_INET6:
308                         range = ServerInstance->Config->c_ipv6_range;
309                 break;
310                 case AF_INET:
311                         range = ServerInstance->Config->c_ipv4_range;
312                 break;
313         }
314
315         x = global_clones.find(user->GetCIDRMask(range));
316         if (x != global_clones.end())
317                 x->second++;
318         else
319                 global_clones[user->GetCIDRMask(range)] = 1;
320 }
321
322 void UserManager::RemoveCloneCounts(User *user)
323 {
324         int range = 32;
325         switch (user->client_sa.sa.sa_family)
326         {
327                 case AF_INET6:
328                         range = ServerInstance->Config->c_ipv6_range;
329                 break;
330                 case AF_INET:
331                         range = ServerInstance->Config->c_ipv4_range;
332                 break;
333         }
334
335         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
336         if (x != local_clones.end())
337         {
338                 x->second--;
339                 if (!x->second)
340                 {
341                         local_clones.erase(x);
342                 }
343         }
344
345         clonemap::iterator y = global_clones.find(user->GetCIDRMask(range));
346         if (y != global_clones.end())
347         {
348                 y->second--;
349                 if (!y->second)
350                 {
351                         global_clones.erase(y);
352                 }
353         }
354 }
355
356 unsigned long UserManager::GlobalCloneCount(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 = global_clones.find(user->GetCIDRMask(range));
369         if (x != global_clones.end())
370                 return x->second;
371         else
372                 return 0;
373 }
374
375 unsigned long UserManager::LocalCloneCount(User *user)
376 {
377         int range = 32;
378         switch (user->client_sa.sa.sa_family)
379         {
380                 case AF_INET6:
381                         range = ServerInstance->Config->c_ipv6_range;
382                 break;
383                 case AF_INET:
384                         range = ServerInstance->Config->c_ipv4_range;
385                 break;
386         }
387         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
388         if (x != local_clones.end())
389                 return x->second;
390         else
391                 return 0;
392 }
393
394 /* this function counts all users connected, wether they are registered or NOT. */
395 unsigned int UserManager::UserCount()
396 {
397         /*
398          * XXX: Todo:
399          *  As part of this restructuring, move clientlist/etc fields into usermanager.
400          *      -- w00t
401          */
402         return this->clientlist->size();
403 }
404
405 /* this counts only registered users, so that the percentages in /MAP don't mess up */
406 unsigned int UserManager::RegisteredUserCount()
407 {
408         return this->clientlist->size() - this->UnregisteredUserCount();
409 }
410
411 /* return how many users are opered */
412 unsigned int UserManager::OperCount()
413 {
414         return this->all_opers.size();
415 }
416
417 /* return how many users are unregistered */
418 unsigned int UserManager::UnregisteredUserCount()
419 {
420         return this->unregistered_count;
421 }
422
423 /* return how many local registered users there are */
424 unsigned int UserManager::LocalUserCount()
425 {
426         /* Doesnt count unregistered clients */
427         return (this->local_users.size() - this->UnregisteredUserCount());
428 }
429
430 void UserManager::ServerNoticeAll(const char* text, ...)
431 {
432         if (!text)
433                 return;
434
435         char textbuffer[MAXBUF];
436         char formatbuffer[MAXBUF];
437         va_list argsPtr;
438         va_start (argsPtr, text);
439         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
440         va_end(argsPtr);
441
442         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
443
444         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
445         {
446                 User* t = *i;
447                 t->WriteServ(std::string(formatbuffer));
448         }
449 }
450
451 void UserManager::ServerPrivmsgAll(const char* text, ...)
452 {
453         if (!text)
454                 return;
455
456         char textbuffer[MAXBUF];
457         char formatbuffer[MAXBUF];
458         va_list argsPtr;
459         va_start (argsPtr, text);
460         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
461         va_end(argsPtr);
462
463         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
464
465         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
466         {
467                 User* t = *i;
468                 t->WriteServ(std::string(formatbuffer));
469         }
470 }
471
472 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
473 {
474         char textbuffer[MAXBUF];
475         int modelen;
476         va_list argsPtr;
477
478         if (!text || !modes || !flags)
479         {
480                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
481                 return;
482         }
483
484         va_start(argsPtr, text);
485         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
486         va_end(argsPtr);
487         modelen = strlen(modes);
488
489         if (flags == WM_AND)
490         {
491                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
492                 {
493                         User* t = *i;
494                         bool send_to_user = true;
495
496                         for (int n = 0; n < modelen; n++)
497                         {
498                                 if (!t->IsModeSet(modes[n]))
499                                 {
500                                         send_to_user = false;
501                                         break;
502                                 }
503                         }
504                         if (send_to_user)
505                         {
506                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
507                         }
508                 }
509         }
510         else if (flags == WM_OR)
511         {
512                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
513                 {
514                         User* t = *i;
515                         bool send_to_user = false;
516
517                         for (int n = 0; n < modelen; n++)
518                         {
519                                 if (t->IsModeSet(modes[n]))
520                                 {
521                                         send_to_user = true;
522                                         break;
523                                 }
524                         }
525
526                         if (send_to_user)
527                         {
528                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
529                         }
530                 }
531         }
532 }
533
534 /* return how many users have a given mode e.g. 'a' */
535 int UserManager::ModeCount(const char mode)
536 {
537         ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
538
539         if (mh)
540                 return mh->GetCount();
541         else
542                 return 0;
543 }