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