]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
139ccb4c482d505c29e719ca6205b1107747d3a1
[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         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                 this->QuitUser(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() >= (unsigned int)Instance->SE->GetMaxFds()))
112         {
113                 Instance->SNO->WriteToSnoMask('A', "Warning: softlimit value has been reached: %d clients", Instance->Config->SoftLimit);
114                 this->QuitUser(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         if (socket >= Instance->SE->GetMaxFds())
129         {
130                 this->QuitUser(New, "Server is full");
131                 return;
132         }
133
134         /*
135          * even with bancache, we still have to keep User::exempt current.
136          * besides that, if we get a positive bancache hit, we still won't fuck
137          * them over if they are exempt. -- w00t
138          */
139         New->exempt = (Instance->XLines->MatchesLine("E",New) != NULL);
140
141         if (BanCacheHit *b = Instance->BanCache->GetHit(New->GetIPString()))
142         {
143                 if (!b->Type.empty() && !New->exempt)
144                 {
145                         /* user banned */
146                         Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
147                         if (*Instance->Config->MoronBanner)
148                                 New->WriteServ("NOTICE %s :*** %s", New->nick, Instance->Config->MoronBanner);
149                         this->QuitUser(New, b->Reason);
150                         return;
151                 }
152                 else
153                 {
154                         Instance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
155                 }
156         }
157         else
158         {
159                 if (!New->exempt)
160                 {
161                         XLine* r = Instance->XLines->MatchesLine("Z",New);
162
163                         if (r)
164                         {
165                                 r->Apply(New);
166                                 return;
167                         }
168                 }
169         }
170
171         if (!Instance->SE->AddFd(New))
172         {
173                 Instance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
174                 this->QuitUser(New, "Internal error handling connection");
175         }
176
177         /* NOTE: even if dns lookups are *off*, we still need to display this.
178          * BOPM and other stuff requires it.
179          */
180         New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
181
182         if (Instance->Config->NoUserDns)
183         {
184                 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick);
185                 New->dns_done = true;
186         }
187         else
188         {
189                 New->StartDNSLookup();
190         }
191 }
192
193 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
194 {
195         ServerInstance->Logs->Log("USERS", DEBUG,"QuitUser: %s '%s'", user->nick, quitreason.c_str());
196         user->Write("ERROR :Closing link (%s@%s) [%s]", user->ident, user->host, *operreason ? operreason : quitreason.c_str());
197         user->quietquit = false;
198         user->quitmsg = quitreason;
199
200         if (!*operreason)
201                 user->operquitmsg = quitreason;
202         else
203                 user->operquitmsg = operreason;
204
205         ServerInstance->GlobalCulls.AddItem(user);
206 }
207
208
209 void UserManager::AddLocalClone(User *user)
210 {
211         clonemap::iterator x = local_clones.find(user->GetIPString());
212         if (x != local_clones.end())
213                 x->second++;
214         else
215                 local_clones[user->GetIPString()] = 1;
216 }
217
218 void UserManager::AddGlobalClone(User *user)
219 {
220         clonemap::iterator y = global_clones.find(user->GetIPString());
221         if (y != global_clones.end())
222                 y->second++;
223         else
224                 global_clones[user->GetIPString()] = 1;
225 }
226
227 void UserManager::RemoveCloneCounts(User *user)
228 {
229         clonemap::iterator x = local_clones.find(user->GetIPString());
230         if (x != local_clones.end())
231         {
232                 x->second--;
233                 if (!x->second)
234                 {
235                         local_clones.erase(x);
236                 }
237         }
238         
239         clonemap::iterator y = global_clones.find(user->GetIPString());
240         if (y != global_clones.end())
241         {
242                 y->second--;
243                 if (!y->second)
244                 {
245                         global_clones.erase(y);
246                 }
247         }
248 }
249
250 unsigned long UserManager::GlobalCloneCount(User *user)
251 {
252         clonemap::iterator x = global_clones.find(user->GetIPString());
253         if (x != global_clones.end())
254                 return x->second;
255         else
256                 return 0;
257 }
258
259 unsigned long UserManager::LocalCloneCount(User *user)
260 {
261         clonemap::iterator x = local_clones.find(user->GetIPString());
262         if (x != local_clones.end())
263                 return x->second;
264         else
265                 return 0;
266 }
267
268 /* this function counts all users connected, wether they are registered or NOT. */
269 unsigned int UserManager::UserCount()
270 {
271         /*
272          * XXX: Todo:
273          *  As part of this restructuring, move clientlist/etc fields into usermanager.
274          *      -- w00t
275          */
276         return this->clientlist->size();
277 }
278
279 /* this counts only registered users, so that the percentages in /MAP don't mess up */
280 unsigned int UserManager::RegisteredUserCount()
281 {
282         return this->clientlist->size() - this->UnregisteredUserCount();
283 }
284
285 /* return how many users are opered */
286 unsigned int UserManager::OperCount()
287 {
288         return this->all_opers.size();
289 }
290
291 /* return how many users are unregistered */
292 unsigned int UserManager::UnregisteredUserCount()
293 {
294         return this->unregistered_count;
295 }
296
297 /* return how many local registered users there are */
298 unsigned int UserManager::LocalUserCount()
299 {
300         /* Doesnt count unregistered clients */
301         return (this->local_users.size() - this->UnregisteredUserCount());
302 }
303
304 void UserManager::ServerNoticeAll(const char* text, ...)
305 {
306         if (!text)
307                 return;
308
309         char textbuffer[MAXBUF];
310         char formatbuffer[MAXBUF];
311         va_list argsPtr;
312         va_start (argsPtr, text);
313         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
314         va_end(argsPtr);
315
316         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName, textbuffer);
317
318         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
319         {
320                 User* t = *i;
321                 t->WriteServ(std::string(formatbuffer));
322         }
323 }
324
325 void UserManager::ServerPrivmsgAll(const char* text, ...)
326 {
327         if (!text)
328                 return;
329
330         char textbuffer[MAXBUF];
331         char formatbuffer[MAXBUF];
332         va_list argsPtr;
333         va_start (argsPtr, text);
334         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
335         va_end(argsPtr);
336
337         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName, textbuffer);
338
339         for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
340         {
341                 User* t = *i;
342                 t->WriteServ(std::string(formatbuffer));
343         }
344 }
345
346 void UserManager::WriteMode(const char* modes, int flags, const char* text, ...)
347 {
348         char textbuffer[MAXBUF];
349         int modelen;
350         va_list argsPtr;
351
352         if (!text || !modes || !flags)
353         {
354                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
355                 return;
356         }
357
358         va_start(argsPtr, text);
359         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
360         va_end(argsPtr);
361         modelen = strlen(modes);
362
363         if (flags == WM_AND)
364         {
365                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
366                 {
367                         User* t = *i;
368                         bool send_to_user = true;
369
370                         for (int n = 0; n < modelen; n++)
371                         {
372                                 if (!t->IsModeSet(modes[n]))
373                                 {
374                                         send_to_user = false;
375                                         break;
376                                 }
377                         }
378                         if (send_to_user)
379                         {
380                                 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
381                         }
382                 }
383         }
384         else if (flags == WM_OR)
385         {
386                 for (std::vector<User*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
387                 {
388                         User* t = *i;
389                         bool send_to_user = false;
390
391                         for (int n = 0; n < modelen; n++)
392                         {
393                                 if (t->IsModeSet(modes[n]))
394                                 {
395                                         send_to_user = true;
396                                         break;
397                                 }
398                         }
399
400                         if (send_to_user)
401                         {
402                                 t->WriteServ("NOTICE %s :%s", t->nick, textbuffer);
403                         }
404                 }
405         }
406 }
407
408 /* return how many users have a given mode e.g. 'a' */
409 int UserManager::ModeCount(const char mode)
410 {
411         ModeHandler* mh = this->ServerInstance->Modes->FindMode(mode, MODETYPE_USER);
412
413         if (mh)
414                 return mh->GetCount();
415         else
416                 return 0;
417 }
418
419
420