]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
Move a socket sanity check into the generic listener handler, avoiding the need to...
[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          * 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         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         user->quietquit = false;
178         user->quitmsg = quitreason;
179
180         if (!*operreason)
181                 user->operquitmsg = quitreason;
182         else
183                 user->operquitmsg = operreason;
184
185         ServerInstance->GlobalCulls.AddItem(user);
186 }
187
188
189 void UserManager::AddLocalClone(User *user)
190 {
191         int range = 32;
192         clonemap::iterator x;
193         switch (user->GetProtocolFamily())
194         {
195 #ifdef SUPPORT_IP6LINKS
196                 case AF_INET6:
197                 {
198                         range = ServerInstance->Config->c_ipv6_range;
199                 }
200                 break;
201 #endif
202                 case AF_INET:
203                 {
204                         range = ServerInstance->Config->c_ipv4_range;
205                 }
206                 break;
207         }
208
209         x = local_clones.find(user->GetCIDRMask(range));
210         if (x != local_clones.end())
211                 x->second++;
212         else
213                 local_clones[user->GetCIDRMask(range)] = 1;
214 }
215
216 void UserManager::AddGlobalClone(User *user)
217 {
218         int range = 32;
219         clonemap::iterator x;
220         switch (user->GetProtocolFamily())
221         {
222 #ifdef SUPPORT_IP6LINKS
223                 case AF_INET6:
224                 {
225                         range = ServerInstance->Config->c_ipv6_range;
226                 }
227                 break;
228 #endif
229                 case AF_INET:
230                 {
231                         range = ServerInstance->Config->c_ipv4_range;
232                 }
233                 break;
234         }
235
236         x = global_clones.find(user->GetCIDRMask(range));
237         if (x != global_clones.end())
238                 x->second++;
239         else
240                 global_clones[user->GetCIDRMask(range)] = 1;
241 }
242
243 void UserManager::RemoveCloneCounts(User *user)
244 {
245         int range = 32;
246         switch (user->GetProtocolFamily())
247         {
248 #ifdef SUPPORT_IP6LINKS
249                 case AF_INET6:
250                 {
251                         range = ServerInstance->Config->c_ipv6_range;
252                 }
253                 break;
254 #endif
255                 case AF_INET:
256                 {
257                         range = ServerInstance->Config->c_ipv4_range;
258                 }
259                 break;
260         }
261
262         clonemap::iterator x = local_clones.find(user->GetCIDRMask(range));
263         if (x != local_clones.end())
264         {
265                 x->second--;
266                 if (!x->second)
267                 {
268                         local_clones.erase(x);
269                 }
270         }
271         
272         clonemap::iterator y = global_clones.find(user->GetCIDRMask(range));
273         if (y != global_clones.end())
274         {
275                 y->second--;
276                 if (!y->second)
277                 {
278                         global_clones.erase(y);
279                 }
280         }
281 }
282
283 unsigned long UserManager::GlobalCloneCount(User *user)
284 {
285         clonemap::iterator x = global_clones.find(user->GetIPString());
286         if (x != global_clones.end())
287                 return x->second;
288         else
289                 return 0;
290 }
291
292 unsigned long UserManager::LocalCloneCount(User *user)
293 {
294         clonemap::iterator x = local_clones.find(user->GetIPString());
295         if (x != local_clones.end())
296                 return x->second;
297         else
298                 return 0;
299 }
300
301 /* this function counts all users connected, wether they are registered or NOT. */
302 unsigned int UserManager::UserCount()
303 {
304         /*
305          * XXX: Todo:
306          *  As part of this restructuring, move clientlist/etc fields into usermanager.
307          *      -- w00t
308          */
309         return this->clientlist->size();
310 }
311
312 /* this counts only registered users, so that the percentages in /MAP don't mess up */
313 unsigned int UserManager::RegisteredUserCount()
314 {
315         return this->clientlist->size() - this->UnregisteredUserCount();
316 }
317
318 /* return how many users are opered */
319 unsigned int UserManager::OperCount()
320 {
321         return this->all_opers.size();
322 }
323
324 /* return how many users are unregistered */
325 unsigned int UserManager::UnregisteredUserCount()
326 {
327         return this->unregistered_count;
328 }
329
330 /* return how many local registered users there are */
331 unsigned int UserManager::LocalUserCount()
332 {
333         /* Doesnt count unregistered clients */
334         return (this->local_users.size() - this->UnregisteredUserCount());
335 }
336
337 void UserManager::ServerNoticeAll(const char* text, ...)
338 {
339         if (!text)
340                 return;
341
342         char textbuffer[MAXBUF];
343         char formatbuffer[MAXBUF];
344         va_list argsPtr;
345         va_start (argsPtr, text);
346         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
347         va_end(argsPtr);
348
349         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
350
351         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
352         {
353                 User* t = *i;
354                 t->WriteServ(std::string(formatbuffer));
355         }
356 }
357
358 void UserManager::ServerPrivmsgAll(const char* text, ...)
359 {
360         if (!text)
361                 return;
362
363         char textbuffer[MAXBUF];
364         char formatbuffer[MAXBUF];
365         va_list argsPtr;
366         va_start (argsPtr, text);
367         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
368         va_end(argsPtr);
369
370         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
371
372         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
373         {
374                 User* t = *i;
375                 t->WriteServ(std::string(formatbuffer));
376         }
377 }
378
379 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
380 {
381         char textbuffer[MAXBUF];
382         int modelen;
383         va_list argsPtr;
384
385         if (!text || !modes || !flags)
386         {
387                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
388                 return;
389         }
390
391         va_start(argsPtr, text);
392         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
393         va_end(argsPtr);
394         modelen = strlen(modes);
395
396         if (flags == WM_AND)
397         {
398                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
399                 {
400                         User* t = *i;
401                         bool send_to_user = true;
402
403                         for (int n = 0; n < modelen; n++)
404                         {
405                                 if (!t->IsModeSet(modes[n]))
406                                 {
407                                         send_to_user = false;
408                                         break;
409                                 }
410                         }
411                         if (send_to_user)
412                         {
413                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
414                         }
415                 }
416         }
417         else if (flags == WM_OR)
418         {
419                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
420                 {
421                         User* t = *i;
422                         bool send_to_user = false;
423
424                         for (int n = 0; n < modelen; n++)
425                         {
426                                 if (t->IsModeSet(modes[n]))
427                                 {
428                                         send_to_user = true;
429                                         break;
430                                 }
431                         }
432
433                         if (send_to_user)
434                         {
435                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
436                         }
437                 }
438         }
439 }
440
441 /* return how many users have a given mode e.g. 'a' */
442 int UserManager::ModeCount(const char mode)
443 {
444         ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
445
446         if (mh)
447                 return mh->GetCount();
448         else
449                 return 0;
450 }
451
452
453