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