]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/usermanager.cpp
Fix logged error when user is denied by connect class
[user/henk/code/inspircd.git] / src / usermanager.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "xline.h"
16 #include "bancache.h"
17
18 /* add a client connection to the sockets list */
19 void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server)
20 {
21         /* NOTE: Calling this one parameter constructor for User automatically
22          * allocates a new UUID and places it in the hash_map.
23          */
24         LocalUser* New = NULL;
25         try
26         {
27                 New = new LocalUser(socket, client, server);
28         }
29         catch (...)
30         {
31                 ServerInstance->Logs->Log("USERS", DEFAULT,"*** WTF *** Duplicated UUID! -- Crack smoking monkies have been unleashed.");
32                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING *** Duplicate UUID allocated!");
33                 return;
34         }
35         UserIOHandler* eh = &New->eh;
36
37         /* Give each of the modules an attempt to hook the user for I/O */
38         FOREACH_MOD(I_OnHookIO, OnHookIO(eh, via));
39
40         if (eh->GetIOHook())
41         {
42                 try
43                 {
44                         eh->GetIOHook()->OnStreamSocketAccept(eh, client, server);
45                 }
46                 catch (CoreException& modexcept)
47                 {
48                         ServerInstance->Logs->Log("SOCKET", DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
49                 }
50         }
51
52         ServerInstance->Logs->Log("USERS", DEBUG,"New user fd: %d", socket);
53
54         this->unregistered_count++;
55
56         /* The users default nick is their UUID */
57         New->nick.assign(New->uuid, 0, ServerInstance->Config->Limits.NickMax);
58         (*(this->clientlist))[New->nick] = New;
59
60         New->ident.assign("unknown");
61
62         New->registered = REG_NONE;
63         New->signon = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
64         New->lastping = 1;
65
66         /* Smarter than your average bear^H^H^H^Hset of strlcpys. */
67         New->dhost.assign(New->GetIPString(), 0, 64);
68         New->host.assign(New->GetIPString(), 0, 64);
69
70         ServerInstance->Users->AddLocalClone(New);
71         ServerInstance->Users->AddGlobalClone(New);
72
73         this->local_users.push_back(New);
74
75         if ((this->local_users.size() > ServerInstance->Config->SoftLimit) || (this->local_users.size() >= (unsigned int)ServerInstance->SE->GetMaxFds()))
76         {
77                 ServerInstance->SNO->WriteToSnoMask('a', "Warning: softlimit value has been reached: %d clients", ServerInstance->Config->SoftLimit);
78                 this->QuitUser(New,"No more connections allowed");
79                 return;
80         }
81
82         /*
83          * First class check. We do this again in FullConnect after DNS is done, and NICK/USER is recieved.
84          * See my note down there for why this is required. DO NOT REMOVE. :) -- w00t
85          */
86         New->SetClass();
87
88         /*
89          * Check connect class settings and initialise settings into User.
90          * This will be done again after DNS resolution. -- w00t
91          */
92         New->CheckClass();
93         if (New->quitting)
94                 return;
95
96         /*
97          * even with bancache, we still have to keep User::exempt current.
98          * besides that, if we get a positive bancache hit, we still won't fuck
99          * them over if they are exempt. -- w00t
100          */
101         New->exempt = (ServerInstance->XLines->MatchesLine("E",New) != NULL);
102
103         if (BanCacheHit *b = ServerInstance->BanCache->GetHit(New->GetIPString()))
104         {
105                 if (!b->Type.empty() && !New->exempt)
106                 {
107                         /* user banned */
108                         ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Positive hit for ") + New->GetIPString());
109                         if (!ServerInstance->Config->MoronBanner.empty())
110                                 New->WriteServ("NOTICE %s :*** %s", New->nick.c_str(), ServerInstance->Config->MoronBanner.c_str());
111                         this->QuitUser(New, b->Reason);
112                         return;
113                 }
114                 else
115                 {
116                         ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Negative hit for ") + New->GetIPString());
117                 }
118         }
119         else
120         {
121                 if (!New->exempt)
122                 {
123                         XLine* r = ServerInstance->XLines->MatchesLine("Z",New);
124
125                         if (r)
126                         {
127                                 r->Apply(New);
128                                 return;
129                         }
130                 }
131         }
132
133         if (!ServerInstance->SE->AddFd(eh, FD_WANT_FAST_READ | FD_WANT_EDGE_WRITE))
134         {
135                 ServerInstance->Logs->Log("USERS", DEBUG,"Internal error on new connection");
136                 this->QuitUser(New, "Internal error handling connection");
137         }
138
139         /* NOTE: even if dns lookups are *off*, we still need to display this.
140          * BOPM and other stuff requires it.
141          */
142         New->WriteServ("NOTICE Auth :*** Looking up your hostname...");
143
144         FOREACH_MOD(I_OnUserInit,OnUserInit(New));
145
146         if (ServerInstance->Config->NoUserDns)
147         {
148                 New->WriteServ("NOTICE %s :*** Skipping host resolution (disabled by server administrator)", New->nick.c_str());
149                 New->dns_done = true;
150         }
151         else
152         {
153                 New->StartDNSLookup();
154         }
155 }
156
157 void UserManager::QuitUser(User *user, const std::string &quitreason, const char* operreason)
158 {
159         if (user->quitting)
160         {
161                 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a user (%s) twice. Did your module call QuitUser twice?", user->nick.c_str());
162                 return;
163         }
164
165         if (IS_SERVER(user))
166         {
167                 ServerInstance->Logs->Log("CULLLIST",DEBUG, "*** Warning *** - You tried to quit a fake user (%s)", user->nick.c_str());
168                 return;
169         }
170
171         user->quitting = true;
172
173         ServerInstance->Logs->Log("USERS", DEBUG, "QuitUser: %s=%s '%s'", user->uuid.c_str(), user->nick.c_str(), quitreason.c_str());
174         user->Write("ERROR :Closing link: (%s@%s) [%s]", user->ident.c_str(), user->host.c_str(), *operreason ? operreason : quitreason.c_str());
175
176         std::string reason;
177         std::string oper_reason;
178         reason.assign(quitreason, 0, ServerInstance->Config->Limits.MaxQuit);
179         if (operreason && *operreason)
180                 oper_reason.assign(operreason, 0, ServerInstance->Config->Limits.MaxQuit);
181         else
182                 oper_reason = quitreason;
183
184         ServerInstance->GlobalCulls.AddItem(user);
185
186         if (user->registered == REG_ALL)
187         {
188                 FOREACH_MOD(I_OnUserQuit,OnUserQuit(user, reason, oper_reason));
189                 user->WriteCommonQuit(reason, oper_reason);
190         }
191
192         if (user->registered != REG_ALL)
193                 if (ServerInstance->Users->unregistered_count)
194                         ServerInstance->Users->unregistered_count--;
195
196         if (IS_LOCAL(user))
197         {
198                 LocalUser* lu = IS_LOCAL(user);
199                 FOREACH_MOD(I_OnUserDisconnect,OnUserDisconnect(lu));
200                 lu->eh.Close();
201         }
202
203         /*
204          * this must come before the ServerInstance->SNO->WriteToSnoMaskso that it doesnt try to fill their buffer with anything
205          * if they were an oper with +sn +qQ.
206          */
207         if (user->registered == REG_ALL)
208         {
209                 if (IS_LOCAL(user))
210                 {
211                         if (!user->quietquit)
212                         {
213                                 ServerInstance->SNO->WriteToSnoMask('q',"Client exiting: %s!%s@%s [%s] (%s)",
214                                         user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str(), user->GetIPString());
215                         }
216                 }
217                 else
218                 {
219                         if ((!ServerInstance->SilentULine(user->server)) && (!user->quietquit))
220                         {
221                                 ServerInstance->SNO->WriteToSnoMask('Q',"Client exiting on server %s: %s!%s@%s [%s] (%s)",
222                                         user->server.c_str(), user->nick.c_str(), user->ident.c_str(), user->host.c_str(), oper_reason.c_str(), user->GetIPString());
223                         }
224                 }
225                 user->AddToWhoWas();
226         }
227
228         user_hash::iterator iter = this->clientlist->find(user->nick);
229
230         if (iter != this->clientlist->end())
231                 this->clientlist->erase(iter);
232         else
233                 ServerInstance->Logs->Log("USERS", DEBUG, "iter == clientlist->end, can't remove them from hash... problematic..");
234
235         ServerInstance->Users->uuidlist->erase(user->uuid);
236 }
237
238
239 void UserManager::AddLocalClone(User *user)
240 {
241         clonemap::iterator x;
242         x = local_clones.find(user->GetCIDRMask());
243         if (x != local_clones.end())
244                 x->second++;
245         else
246                 local_clones[user->GetCIDRMask()] = 1;
247 }
248
249 void UserManager::AddGlobalClone(User *user)
250 {
251         clonemap::iterator x;
252
253         x = global_clones.find(user->GetCIDRMask());
254         if (x != global_clones.end())
255                 x->second++;
256         else
257                 global_clones[user->GetCIDRMask()] = 1;
258 }
259
260 void UserManager::RemoveCloneCounts(User *user)
261 {
262         if (IS_LOCAL(user))
263         {
264                 clonemap::iterator x = local_clones.find(user->GetCIDRMask());
265                 if (x != local_clones.end())
266                 {
267                         x->second--;
268                         if (!x->second)
269                         {
270                                 local_clones.erase(x);
271                         }
272                 }
273         }
274
275         clonemap::iterator y = global_clones.find(user->GetCIDRMask());
276         if (y != global_clones.end())
277         {
278                 y->second--;
279                 if (!y->second)
280                 {
281                         global_clones.erase(y);
282                 }
283         }
284 }
285
286 unsigned long UserManager::GlobalCloneCount(User *user)
287 {
288         clonemap::iterator x = global_clones.find(user->GetCIDRMask());
289         if (x != global_clones.end())
290                 return x->second;
291         else
292                 return 0;
293 }
294
295 unsigned long UserManager::LocalCloneCount(User *user)
296 {
297         clonemap::iterator x = local_clones.find(user->GetCIDRMask());
298         if (x != local_clones.end())
299                 return x->second;
300         else
301                 return 0;
302 }
303
304 /* this function counts all users connected, wether they are registered or NOT. */
305 unsigned int UserManager::UserCount()
306 {
307         /*
308          * XXX: Todo:
309          *  As part of this restructuring, move clientlist/etc fields into usermanager.
310          *      -- w00t
311          */
312         return this->clientlist->size();
313 }
314
315 /* this counts only registered users, so that the percentages in /MAP don't mess up */
316 unsigned int UserManager::RegisteredUserCount()
317 {
318         return this->clientlist->size() - this->UnregisteredUserCount();
319 }
320
321 /* return how many users are opered */
322 unsigned int UserManager::OperCount()
323 {
324         return this->all_opers.size();
325 }
326
327 /* return how many users are unregistered */
328 unsigned int UserManager::UnregisteredUserCount()
329 {
330         return this->unregistered_count;
331 }
332
333 /* return how many local registered users there are */
334 unsigned int UserManager::LocalUserCount()
335 {
336         /* Doesnt count unregistered clients */
337         return (this->local_users.size() - this->UnregisteredUserCount());
338 }
339
340 void UserManager::ServerNoticeAll(const char* text, ...)
341 {
342         if (!text)
343                 return;
344
345         char textbuffer[MAXBUF];
346         char formatbuffer[MAXBUF];
347         va_list argsPtr;
348         va_start (argsPtr, text);
349         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
350         va_end(argsPtr);
351
352         snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
353
354         for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
355         {
356                 User* t = *i;
357                 t->WriteServ(std::string(formatbuffer));
358         }
359 }
360
361 void UserManager::ServerPrivmsgAll(const char* text, ...)
362 {
363         if (!text)
364                 return;
365
366         char textbuffer[MAXBUF];
367         char formatbuffer[MAXBUF];
368         va_list argsPtr;
369         va_start (argsPtr, text);
370         vsnprintf(textbuffer, MAXBUF, text, argsPtr);
371         va_end(argsPtr);
372
373         snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s", ServerInstance->Config->ServerName.c_str(), textbuffer);
374
375         for (std::vector<LocalUser*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
376         {
377                 User* t = *i;
378                 t->WriteServ(std::string(formatbuffer));
379         }
380 }
381
382
383 /* return how many users have a given mode e.g. 'a' */
384 int UserManager::ModeCount(const char mode)
385 {
386         int c = 0;
387         for(user_hash::iterator i = clientlist->begin(); i != clientlist->end(); ++i)
388         {
389                 User* u = i->second;
390                 if (u->modes[mode-65])
391                         c++;
392         }
393         return c;
394 }