]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
Merge the second interface for socket hooking into one, so it's all done by Add/Del...
[user/henk/code/inspircd.git] / src / usermanager.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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, int socketfamily, 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 (socketfamily == 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(socketfamily, 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          * XXX -
116          * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
117          * its a pretty big but for the moment valid assumption:
118          * file descriptors are handed out starting at 0, and are recycled as theyre freed.
119          * therefore if there is ever an fd over 65535, 65536 clients must be connected to the
120          * irc server at once (or the irc server otherwise initiating this many connections, files etc)
121          * which for the time being is a physical impossibility (even the largest networks dont have more
122          * than about 10,000 users on ONE server!)
123          */
124         if (socket >= Instance->SE->GetMaxFds())
125         {
126                 this->QuitUser(New, "Server is full");
127                 return;
128         }
129
130         /*
131          * even with bancache, we still have to keep User::exempt current.
132          * besides that, if we get a positive bancache hit, we still won't fuck
133          * them over if they are exempt. -- w00t
134          */
135         New->exempt = (Instance->XLines->MatchesLine("E",New) != NULL);
136
137         if (BanCacheHit *b = Instance->BanCache->GetHit(New->GetIPString()))
138         {
139                 if (!b->Type.empty() && !New->exempt)
140                 {
141                         /* user banned */
142                         Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
143                         if (*Instance->Config->MoronBanner)
144                                 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), Instance->Config->MoronBanner);
145                         this->QuitUser(New, b->Reason);
146                         return;
147                 }
148                 else
149                 {
150                         Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
151                 }
152         }
153         else
154         {
155                 if (!New->exempt)
156                 {
157                         XLine* r = Instance->XLines->MatchesLine("Z",New);
158
159                         if (r)
160                         {
161                                 r->Apply(New);
162                                 return;
163                         }
164                 }
165         }
166
167         if (!Instance->SE->AddFd(New))
168         {
169                 Instance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
170                 this->QuitUser(New, "Internal error handling connection");
171         }
172
173         /* NOTE: even if dns lookups are *off*, we still need to display this.
174          * BOPM and other stuff requires it.
175          */
176         New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
177
178         if (Instance->Config->NoUserDns)
179         {
180                 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
181                 New->dns_done = true;
182         }
183         else
184         {
185                 New->StartDNSLookup();
186         }
187 }
188
189 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
190 {
191         ServerInstance->Logs->Log("USERS", DEBUG,"QuitUser: %s '%s'", user->nick.c_str(), quitreason.c_str());
192         user->Write("ERROR :Closing link (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), *operreason ? operreason : quitreason.c_str());
193         user->quietquit = false;
194         user->quitmsg = quitreason;
195
196         if (!*operreason)
197                 user->operquitmsg = quitreason;
198         else
199                 user->operquitmsg = operreason;
200
201         ServerInstance->GlobalCulls.AddItem(user);
202 }
203
204
205 void UserManager::AddLocalClone(User *user)
206 {
207         int range = 32;
208         clonemap::iterator x;
209         switch (user->GetProtocolFamily())
210         {
211 #ifdef SUPPORT_IP6LINKS
212                 case AF_INET6:
213                 {
214                         range = ServerInstance->Config->c_ipv6_range;
215                 }
216                 break;
217 #endif
218                 case AF_INET:
219                 {
220                         range = ServerInstance->Config->c_ipv4_range;
221                 }
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->GetProtocolFamily())
237         {
238 #ifdef SUPPORT_IP6LINKS
239                 case AF_INET6:
240                 {
241                         range = ServerInstance->Config->c_ipv6_range;
242                 }
243                 break;
244 #endif
245                 case AF_INET:
246                 {
247                         range = ServerInstance->Config->c_ipv4_range;
248                 }
249                 break;
250         }
251
252         x = global_clones.find(user->GetCIDRMask(range));
253         if (x != global_clones.end())
254                 x->second++;
255         else
256                 global_clones[user->GetCIDRMask(range)] = 1;
257 }
258
259 void UserManager::RemoveCloneCounts(User *user)
260 {
261         int range = 32;
262         switch (user->GetProtocolFamily())
263         {
264 #ifdef SUPPORT_IP6LINKS
265                 case AF_INET6:
266                 {
267                         range = ServerInstance->Config->c_ipv6_range;
268                 }
269                 break;
270 #endif
271                 case AF_INET:
272                 {
273                         range = ServerInstance->Config->c_ipv4_range;
274                 }
275                 break;
276         }
277
278         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
279         if (x != local_clones.end())
280         {
281                 x->second--;
282                 if (!x->second)
283                 {
284                         local_clones.erase(x);
285                 }
286         }
287         
288         clonemap::iterator y = global_clones.find(user->GetCIDRMask(range));
289         if (y != global_clones.end())
290         {
291                 y->second--;
292                 if (!y->second)
293                 {
294                         global_clones.erase(y);
295                 }
296         }
297 }
298
299 unsigned long UserManager::GlobalCloneCount(User *user)
300 {
301         clonemap::iterator x = global_clones.find(user->GetIPString());
302         if (x != global_clones.end())
303                 return x->second;
304         else
305                 return 0;
306 }
307
308 unsigned long UserManager::LocalCloneCount(User *user)
309 {
310         clonemap::iterator x = local_clones.find(user->GetIPString());
311         if (x != local_clones.end())
312                 return x->second;
313         else
314                 return 0;
315 }
316
317 /* this function counts all users connected, wether they are registered or NOT. */
318 unsigned int UserManager::UserCount()
319 {
320         /*
321          * XXX: Todo:
322          *  As part of this restructuring, move clientlist/etc fields into usermanager.
323          *      -- w00t
324          */
325         return this->clientlist->size();
326 }
327
328 /* this counts only registered users, so that the percentages in /MAP don't mess up */
329 unsigned int UserManager::RegisteredUserCount()
330 {
331         return this->clientlist->size() - this->UnregisteredUserCount();
332 }
333
334 /* return how many users are opered */
335 unsigned int UserManager::OperCount()
336 {
337         return this->all_opers.size();
338 }
339
340 /* return how many users are unregistered */
341 unsigned int UserManager::UnregisteredUserCount()
342 {
343         return this->unregistered_count;
344 }
345
346 /* return how many local registered users there are */
347 unsigned int UserManager::LocalUserCount()
348 {
349         /* Doesnt count unregistered clients */
350         return (this->local_users.size() - this->UnregisteredUserCount());
351 }
352
353 void UserManager::ServerNoticeAll(const char* text, ...)
354 {
355         if (!text)
356                 return;
357
358         char textbuffer[MAXBUF];
359         char formatbuffer[MAXBUF];
360         va_list argsPtr;
361         va_start (argsPtr, text);
362         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
363         va_end(argsPtr);
364
365         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
366
367         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
368         {
369                 User* t = *i;
370                 t->WriteServ(std::string(formatbuffer));
371         }
372 }
373
374 void UserManager::ServerPrivmsgAll(const char* text, ...)
375 {
376         if (!text)
377                 return;
378
379         char textbuffer[MAXBUF];
380         char formatbuffer[MAXBUF];
381         va_list argsPtr;
382         va_start (argsPtr, text);
383         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
384         va_end(argsPtr);
385
386         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
387
388         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
389         {
390                 User* t = *i;
391                 t->WriteServ(std::string(formatbuffer));
392         }
393 }
394
395 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
396 {
397         char textbuffer[MAXBUF];
398         int modelen;
399         va_list argsPtr;
400
401         if (!text || !modes || !flags)
402         {
403                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
404                 return;
405         }
406
407         va_start(argsPtr, text);
408         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
409         va_end(argsPtr);
410         modelen = strlen(modes);
411
412         if (flags == WM_AND)
413         {
414                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
415                 {
416                         User* t = *i;
417                         bool send_to_user = true;
418
419                         for (int n = 0; n < modelen; n++)
420                         {
421                                 if (!t->IsModeSet(modes[n]))
422                                 {
423                                         send_to_user = false;
424                                         break;
425                                 }
426                         }
427                         if (send_to_user)
428                         {
429                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
430                         }
431                 }
432         }
433         else if (flags == WM_OR)
434         {
435                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
436                 {
437                         User* t = *i;
438                         bool send_to_user = false;
439
440                         for (int n = 0; n < modelen; n++)
441                         {
442                                 if (t->IsModeSet(modes[n]))
443                                 {
444                                         send_to_user = true;
445                                         break;
446                                 }
447                         }
448
449                         if (send_to_user)
450                         {
451                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
452                         }
453                 }
454         }
455 }
456
457 /* return how many users have a given mode e.g. 'a' */
458 int UserManager::ModeCount(const char mode)
459 {
460         ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
461
462         if (mh)
463                 return mh->GetCount();
464         else
465                 return 0;
466 }
467
468
469