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