]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
834ff3e20ac944ef267b2125972463638e334232
[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: libIRCDusermanager */
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->io)
53         {
54                 try
55                 {
56                         New->io->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         int j = 0;
67
68         this->unregistered_count++;
69
70         (*(this->clientlist))[New->uuid] = New;
71
72         /* The users default nick is their UUID */
73         New->nick.assign(New->uuid, 0, NICKMAX - 1);
74
75         New->server = Instance->FindServerNamePtr(Instance->Config->ServerName);
76         New->ident.assign("unknown");
77
78         New->registered = REG_NONE;
79         New->signon = Instance->Time() + Instance->Config->dns_timeout;
80         New->lastping = 1;
81
82         /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
83         for (const char* temp = New->GetIPString(); *temp && j < 64; temp++, j++)
84                 New->dhost[j] = New->host[j] = *temp;
85         New->dhost[j] = New->host[j] = 0;
86
87         Instance->Users->AddLocalClone(New);
88         Instance->Users->AddGlobalClone(New);
89
90         /*
91          * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
92          * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
93          */
94         ConnectClass* i = New->SetClass();
95
96         if (!i)
97         {
98                 this->QuitUser(New, "Access denied by configuration");
99                 return;
100         }
101
102         /*
103          * Check connect class settings and initialise settings into User.
104          * This will be done again after DNS resolution. -- w00t
105          */
106         New->CheckClass();
107
108         this->local_users.push_back(New);
109
110         if ((this->local_users.size() > Instance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)Instance->SE->GetMaxFds()))
111         {
112                 Instance->SNO->WriteToSnoMask('A', "Warning: softlimit value has been reached: %d clients", Instance->Config->SoftLimit);
113                 this->QuitUser(New,"No more connections allowed");
114                 return;
115         }
116
117         /*
118          * XXX -
119          * this is done as a safety check to keep the file descriptors within range of fd_ref_table.
120          * its a pretty big but for the moment valid assumption:
121          * file descriptors are handed out starting at 0, and are recycled as theyre freed.
122          * therefore if there is ever an fd over 65535, 65536 clients must be connected to the
123          * irc server at once (or the irc server otherwise initiating this many connections, files etc)
124          * which for the time being is a physical impossibility (even the largest networks dont have more
125          * than about 10,000 users on ONE server!)
126          */
127         if (socket >= Instance->SE->GetMaxFds())
128         {
129                 this->QuitUser(New, "Server is full");
130                 return;
131         }
132
133         /*
134          * even with bancache, we still have to keep User::exempt current.
135          * besides that, if we get a positive bancache hit, we still won't fuck
136          * them over if they are exempt. -- w00t
137          */
138         New->exempt = (Instance->XLines->MatchesLine("E",New) != NULL);
139
140         if (BanCacheHit *b = Instance->BanCache->GetHit(New->GetIPString()))
141         {
142                 if (!b->Type.empty() && !New->exempt)
143                 {
144                         /* user banned */
145                         Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
146                         if (*Instance->Config->MoronBanner)
147                                 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), Instance->Config->MoronBanner);
148                         this->QuitUser(New, b->Reason);
149                         return;
150                 }
151                 else
152                 {
153                         Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
154                 }
155         }
156         else
157         {
158                 if (!New->exempt)
159                 {
160                         XLine* r = Instance->XLines->MatchesLine("Z",New);
161
162                         if (r)
163                         {
164                                 r->Apply(New);
165                                 return;
166                         }
167                 }
168         }
169
170         if (!Instance->SE->AddFd(New))
171         {
172                 Instance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
173                 this->QuitUser(New, "Internal error handling connection");
174         }
175
176         /* NOTE: even if dns lookups are *off*, we still need to display this.
177          * BOPM and other stuff requires it.
178          */
179         New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
180
181         if (Instance->Config->NoUserDns)
182         {
183                 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
184                 New->dns_done = true;
185         }
186         else
187         {
188                 New->StartDNSLookup();
189         }
190 }
191
192 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
193 {
194         ServerInstance->Logs->Log("USERS", DEBUG,"QuitUser: %s '%s'", user->nick.c_str(), quitreason.c_str());
195         user->Write("ERROR :Closing link (%s@%s) [%s]", user->ident.c_str(), user->host, *operreason ? operreason : quitreason.c_str());
196         user->quietquit = false;
197         user->quitmsg = quitreason;
198
199         if (!*operreason)
200                 user->operquitmsg = quitreason;
201         else
202                 user->operquitmsg = operreason;
203
204         ServerInstance->GlobalCulls.AddItem(user);
205 }
206
207
208 void UserManager::AddLocalClone(User *user)
209 {
210         clonemap::iterator x = local_clones.find(user->GetIPString());
211         if (x != local_clones.end())
212                 x->second++;
213         else
214                 local_clones[user->GetIPString()] = 1;
215 }
216
217 void UserManager::AddGlobalClone(User *user)
218 {
219         clonemap::iterator y = global_clones.find(user->GetIPString());
220         if (y != global_clones.end())
221                 y->second++;
222         else
223                 global_clones[user->GetIPString()] = 1;
224 }
225
226 void UserManager::RemoveCloneCounts(User *user)
227 {
228         clonemap::iterator x = local_clones.find(user->GetIPString());
229         if (x != local_clones.end())
230         {
231                 x->second--;
232                 if (!x->second)
233                 {
234                         local_clones.erase(x);
235                 }
236         }
237         
238         clonemap::iterator y = global_clones.find(user->GetIPString());
239         if (y != global_clones.end())
240         {
241                 y->second--;
242                 if (!y->second)
243                 {
244                         global_clones.erase(y);
245                 }
246         }
247 }
248
249 unsigned long UserManager::GlobalCloneCount(User *user)
250 {
251         clonemap::iterator x = global_clones.find(user->GetIPString());
252         if (x != global_clones.end())
253                 return x->second;
254         else
255                 return 0;
256 }
257
258 unsigned long UserManager::LocalCloneCount(User *user)
259 {
260         clonemap::iterator x = local_clones.find(user->GetIPString());
261         if (x != local_clones.end())
262                 return x->second;
263         else
264                 return 0;
265 }
266
267 /* this function counts all users connected, wether they are registered or NOT. */
268 unsigned int UserManager::UserCount()
269 {
270         /*
271          * XXX: Todo:
272          *  As part of this restructuring, move clientlist/etc fields into usermanager.
273          *      -- w00t
274          */
275         return this->clientlist->size();
276 }
277
278 /* this counts only registered users, so that the percentages in /MAP don't mess up */
279 unsigned int UserManager::RegisteredUserCount()
280 {
281         return this->clientlist->size() - this->UnregisteredUserCount();
282 }
283
284 /* return how many users are opered */
285 unsigned int UserManager::OperCount()
286 {
287         return this->all_opers.size();
288 }
289
290 /* return how many users are unregistered */
291 unsigned int UserManager::UnregisteredUserCount()
292 {
293         return this->unregistered_count;
294 }
295
296 /* return how many local registered users there are */
297 unsigned int UserManager::LocalUserCount()
298 {
299         /* Doesnt count unregistered clients */
300         return (this->local_users.size() - this->UnregisteredUserCount());
301 }
302
303 void UserManager::ServerNoticeAll(const char* text, ...)
304 {
305         if (!text)
306                 return;
307
308         char textbuffer[MAXBUF];
309         char formatbuffer[MAXBUF];
310         va_list argsPtr;
311         va_start (argsPtr, text);
312         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
313         va_end(argsPtr);
314
315         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
316
317         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
318         {
319                 User* t = *i;
320                 t->WriteServ(std::string(formatbuffer));
321         }
322 }
323
324 void UserManager::ServerPrivmsgAll(const char* text, ...)
325 {
326         if (!text)
327                 return;
328
329         char textbuffer[MAXBUF];
330         char formatbuffer[MAXBUF];
331         va_list argsPtr;
332         va_start (argsPtr, text);
333         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
334         va_end(argsPtr);
335
336         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
337
338         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
339         {
340                 User* t = *i;
341                 t->WriteServ(std::string(formatbuffer));
342         }
343 }
344
345 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
346 {
347         char textbuffer[MAXBUF];
348         int modelen;
349         va_list argsPtr;
350
351         if (!text || !modes || !flags)
352         {
353                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
354                 return;
355         }
356
357         va_start(argsPtr, text);
358         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
359         va_end(argsPtr);
360         modelen = strlen(modes);
361
362         if (flags == WM_AND)
363         {
364                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
365                 {
366                         User* t = *i;
367                         bool send_to_user = true;
368
369                         for (int n = 0; n < modelen; n++)
370                         {
371                                 if (!t->IsModeSet(modes[n]))
372                                 {
373                                         send_to_user = false;
374                                         break;
375                                 }
376                         }
377                         if (send_to_user)
378                         {
379                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
380                         }
381                 }
382         }
383         else if (flags == WM_OR)
384         {
385                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
386                 {
387                         User* t = *i;
388                         bool send_to_user = false;
389
390                         for (int n = 0; n < modelen; n++)
391                         {
392                                 if (t->IsModeSet(modes[n]))
393                                 {
394                                         send_to_user = true;
395                                         break;
396                                 }
397                         }
398
399                         if (send_to_user)
400                         {
401                                 t->WriteServ("NOTICE %s :%s", t->nick.c_str(), textbuffer);
402                         }
403                 }
404         }
405 }
406
407 /* return how many users have a given mode e.g. 'a' */
408 int UserManager::ModeCount(const char mode)
409 {
410         ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
411
412         if (mh)
413                 return mh->GetCount();
414         else
415                 return 0;
416 }
417
418
419