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