]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/xline.cpp
Move some stuff to usermanager, remove a little header insanity, remove trace because...
[user/henk/code/inspircd.git] / src / xline.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: libIRCDxline */
15
16 #include "inspircd.h"
17 #include "wildcard.h"
18 #include "xline.h"
19 #include "bancache.h"
20
21 /*
22  * This is now version 3 of the XLine subsystem, let's see if we can get it as nice and 
23  * efficient as we can this time so we can close this file and never ever touch it again ..
24  *
25  * Background:
26  *  Version 1 stored all line types in one list (one for g, one for z, etc). This was fine,
27  *  but both version 1 and 2 suck at applying lines efficiently. That is, every time a new line
28  *  was added, it iterated every existing line for every existing user. Ow. Expiry was also
29  *  expensive, as the lists were NOT sorted.
30  *
31  *  Version 2 moved permanent lines into a seperate list from non-permanent to help optimize
32  *  matching speed, but matched in the same way.
33  *  Expiry was also sped up by sorting the list by expiry (meaning just remove the items at the
34  *  head of the list that are outdated.)
35  *
36  * This was fine and good, but it looked less than ideal in code, and matching was still slower
37  * than it could have been, something which we address here.
38  *
39  * VERSION 3:
40  *  All lines are (as in v1) stored together -- no seperation of perm and non-perm. They are stored in
41  *  a map of maps (first map is line type, second map is for quick lookup on add/delete/etc).
42  *
43  *  Expiry is *no longer* performed on a timer, and no longer uses a sorted list of any variety. This
44  *  is now done by only checking for expiry when a line is accessed, meaning that expiry is no longer
45  *  a resource intensive problem.
46  *
47  *  Application no longer tries to apply every single line on every single user - instead, now only lines
48  *  added since the previous application are applied. This keeps S2S ADDLINE during burst nice and fast,
49  *  while at the same time not slowing things the fuck down when we try adding a ban with lots of preexisting
50  *  bans. :)
51  */
52
53 bool XLine::Matches(User *u)
54 {
55         return false;
56 }
57
58 /*
59  * Checks what users match a given vector of ELines and sets their ban exempt flag accordingly.
60  */
61 void XLineManager::CheckELines()
62 {
63         ContainerIter n = lookup_lines.find("E");
64
65         if (n == lookup_lines.end())
66                 return;
67
68         XLineLookup& ELines = n->second;
69
70         if (ELines.empty())
71                 return;
72
73         for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
74         {
75                 User* u = (User*)(*u2);
76
77                 /* This uses safe iteration to ensure that if a line expires here, it doenst trash the iterator */
78                 LookupIter safei;
79
80                 for (LookupIter i = ELines.begin(); i != ELines.end(); )
81                 {
82                         safei = i;
83                         safei++;
84
85                         XLine *e = i->second;
86                         u->exempt = e->Matches(u);
87
88                         i = safei;
89                 }
90         }
91 }
92
93
94 XLineLookup* XLineManager::GetAll(const std::string &type)
95 {
96         ContainerIter n = lookup_lines.find(type);
97
98         if (n == lookup_lines.end())
99                 return NULL;
100
101         LookupIter safei;
102         const time_t current = ServerInstance->Time();
103
104         /* Expire any dead ones, before sending */
105         for (LookupIter x = n->second.begin(); x != n->second.end(); )
106         {
107                 safei = x;
108                 safei++;
109                 if (x->second->duration && current > x->second->expiry)
110                 {
111                         ExpireLine(n, x);
112                 }
113                 x = safei;
114         }
115
116         return &(n->second);
117 }
118
119 std::vector<std::string> XLineManager::GetAllTypes()
120 {
121         std::vector<std::string> items;
122         for (ContainerIter x = lookup_lines.begin(); x != lookup_lines.end(); ++x)
123                 items.push_back(x->first);
124         return items;
125 }
126
127 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
128 {
129         IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
130         std::string::size_type x = ident_and_host.find('@');
131         if (x != std::string::npos)
132         {
133                 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
134                 n.first = ident_and_host.substr(0, x);
135                 if (!n.first.length())
136                         n.first.assign("*");
137                 if (!n.second.length())
138                         n.second.assign("*");
139         }
140         else
141         {
142                 n.second = ident_and_host;
143         }
144
145         return n;
146 }
147
148 // adds a line
149
150 bool XLineManager::AddLine(XLine* line, User* user)
151 {
152         /*IdentHostPair ih = IdentSplit(hostmask);*/
153
154         ServerInstance->BanCache->RemoveEntries(line->type, false); // XXX perhaps remove ELines here?
155
156         if (DelLine(line->Displayable(), line->type, user, true))
157                 return false;
158
159         /*ELine* item = new ELine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
160         pending_lines.push_back(line);
161         lookup_lines[line->type][line->Displayable()] = line;
162         line->OnAdd();
163
164         FOREACH_MOD(I_OnAddLine,OnAddLine(user, line)); 
165
166         return true;
167 }
168
169 // deletes a line, returns true if the line existed and was removed
170
171 bool XLineManager::DelLine(const char* hostmask, const std::string &type, User* user, bool simulate)
172 {
173         ContainerIter x = lookup_lines.find(type);
174
175         if (x == lookup_lines.end())
176                 return false;
177
178         LookupIter y = x->second.find(hostmask);
179
180         if (y == x->second.end())
181                 return false;
182
183         if (simulate)
184                 return true;
185
186         ServerInstance->BanCache->RemoveEntries(y->second->type, true);
187
188         FOREACH_MOD(I_OnDelLine,OnDelLine(user, y->second));
189
190         y->second->Unset();
191
192         std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), y->second);
193         if (pptr != pending_lines.end())
194                 pending_lines.erase(pptr);
195
196         delete y->second;
197         x->second.erase(y);
198
199         return true;
200 }
201
202
203 void ELine::Unset()
204 {
205         /* remove exempt from everyone and force recheck after deleting eline */
206         for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
207         {
208                 User* u = (User*)(*u2);
209                 u->exempt = false;
210         }
211
212         ServerInstance->XLines->CheckELines();
213 }
214
215 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
216
217 XLine* XLineManager::MatchesLine(const std::string &type, User* user)
218 {
219         ContainerIter x = lookup_lines.find(type);
220
221         if (x == lookup_lines.end())
222                 return NULL;
223
224         const time_t current = ServerInstance->Time();
225
226         LookupIter safei;
227
228         for (LookupIter i = x->second.begin(); i != x->second.end(); )
229         {
230                 safei = i;
231                 safei++;
232
233                 if (i->second->Matches(user))
234                 {
235                         if (i->second->duration && current > i->second->expiry)
236                         {
237                                 /* Expire the line, return nothing */
238                                 ExpireLine(x, i);
239                                 /* Continue, there may be another that matches
240                                  * (thanks aquanight)
241                                  */
242                                 i = safei;
243                                 continue;
244                         }
245                         else
246                                 return i->second;
247                 }
248
249                 i = safei;
250         }
251         return NULL;
252 }
253
254 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
255 {
256         ContainerIter x = lookup_lines.find(type);
257
258         if (x == lookup_lines.end())
259                 return NULL;
260
261         const time_t current = ServerInstance->Time();
262
263          LookupIter safei;
264
265         for (LookupIter i = x->second.begin(); i != x->second.end(); )
266         {
267                 safei = i;
268                 safei++;
269
270                 if (i->second->Matches(pattern))
271                 {
272                         if (i->second->duration && current > i->second->expiry)
273                         {
274                                 /* Expire the line, return nothing */
275                                 ExpireLine(x, i);
276                                 /* See above */
277                                 i = safei;
278                                 continue;
279                         }
280                         else
281                                 return i->second;
282                 }
283
284                 i = safei;
285         }
286         return NULL;
287 }
288
289 // removes lines that have expired
290 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
291 {
292         FOREACH_MOD(I_OnExpireLine, OnExpireLine(item->second));
293
294         item->second->DisplayExpiry();
295         item->second->Unset();
296
297         /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
298          * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
299          * -- Brain
300          */
301         std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
302         if (pptr != pending_lines.end())
303                 pending_lines.erase(pptr);
304
305         delete item->second;
306         container->second.erase(item);
307 }
308
309
310 // applies lines, removing clients and changing nicks etc as applicable
311 void XLineManager::ApplyLines()
312 {
313         for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
314         {
315                 User* u = (User*)(*u2);
316
317                 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
318                 {
319                         XLine *x = *i;
320                         if (x->Matches(u))
321                                 x->Apply(u);
322                 }
323         }
324
325         pending_lines.clear();
326 }
327
328 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
329 {
330         std::string sn = ServerInstance->Config->ServerName;
331
332         ContainerIter n = lookup_lines.find(type);
333
334         time_t current = ServerInstance->Time();
335
336         LookupIter safei;
337
338         if (n != lookup_lines.end())
339         {
340                 XLineLookup& list = n->second;
341                 for (LookupIter i = list.begin(); i != list.end(); )
342                 {
343                         safei = i;
344                         safei++;
345
346                         if (i->second->duration && current > i->second->expiry)
347                         {
348                                 ExpireLine(n, i);
349                         }
350                         else
351                                 results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
352                                         ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
353                         i = safei;
354                 }
355         }
356 }
357
358
359 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
360 {
361         GFact = new GLineFactory(Instance);
362         EFact = new ELineFactory(Instance);
363         KFact = new KLineFactory(Instance);
364         QFact = new QLineFactory(Instance);
365         ZFact = new ZLineFactory(Instance);
366
367         RegisterFactory(GFact);
368         RegisterFactory(EFact);
369         RegisterFactory(KFact);
370         RegisterFactory(QFact);
371         RegisterFactory(ZFact);
372 }
373
374 XLineManager::~XLineManager()
375 {
376         UnregisterFactory(GFact);
377         UnregisterFactory(EFact);
378         UnregisterFactory(KFact);
379         UnregisterFactory(QFact);
380         UnregisterFactory(ZFact);
381
382         delete GFact;
383         delete EFact;
384         delete KFact;
385         delete QFact;
386         delete ZFact;
387 }
388
389 void XLine::Apply(User* u)
390 {
391 }
392
393 void XLine::DefaultApply(User* u, const std::string &line, bool bancache)
394 {
395         char reason[MAXBUF];
396         snprintf(reason, MAXBUF, "%s-Lined: %s", line.c_str(), this->reason);
397         if (*ServerInstance->Config->MoronBanner)
398                 u->WriteServ("NOTICE %s :*** %s", u->nick, ServerInstance->Config->MoronBanner);
399         if (ServerInstance->Config->HideBans)
400                 User::QuitUser(ServerInstance, u, line + "-Lined", reason);
401         else
402                 User::QuitUser(ServerInstance, u, reason);
403
404
405         if (bancache)
406         {
407                 ServerInstance->Log(DEBUG, std::string("BanCache: Adding positive hit (") + line + ") for " + u->GetIPString());
408                 ServerInstance->BanCache->AddHit(u->GetIPString(), this->type, line + "-Lined: " + this->reason);
409         }
410 }
411
412 bool KLine::Matches(User *u)
413 {
414         if (u->exempt)
415                 return false;
416
417         if ((match(u->ident, this->identmask)))
418         {
419                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
420                 {
421                         return true;
422                 }
423         }
424
425         return false;
426 }
427
428 void KLine::Apply(User* u)
429 {
430         DefaultApply(u, "K", (strcmp(this->identmask, "*") == 0) ? true : false);
431 }
432
433 bool GLine::Matches(User *u)
434 {
435         if (u->exempt)
436                 return false;
437
438         if ((match(u->ident, this->identmask)))
439         {
440                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
441                 {
442                         return true;
443                 }
444         }
445
446         return false;
447 }
448
449 void GLine::Apply(User* u)
450 {       
451         DefaultApply(u, "G", (strcmp(this->identmask, "*") == 0) ? true : false);
452 }
453
454 bool ELine::Matches(User *u)
455 {
456         if (u->exempt)
457                 return false;
458
459         if ((match(u->ident, this->identmask)))
460         {
461                 if ((match(u->host, this->hostmask, true)) || (match(u->GetIPString(), this->hostmask, true)))
462                 {
463                         return true;
464                 }
465         }
466
467         return false;
468 }
469
470 bool ZLine::Matches(User *u)
471 {
472         if (u->exempt)
473                 return false;
474
475         if (match(u->GetIPString(), this->ipaddr, true))
476                 return true;
477         else
478                 return false;
479 }
480
481 void ZLine::Apply(User* u)
482 {       
483         DefaultApply(u, "Z", true);
484 }
485
486
487 bool QLine::Matches(User *u)
488 {
489         if (u->exempt)
490                 return false;
491
492         if (match(u->nick, this->nick))
493                 return true;
494
495         return false;
496 }
497
498 void QLine::Apply(User* u)
499 {       
500         /* Force to uuid on apply of qline, no need to disconnect any more :) */
501         u->ForceNickChange(u->uuid);
502 }
503
504
505 bool ZLine::Matches(const std::string &str)
506 {
507         if (match(str.c_str(), this->ipaddr, true))
508                 return true;
509         else
510                 return false;
511 }
512
513 bool QLine::Matches(const std::string &str)
514 {
515         if (match(str.c_str(), this->nick))
516                 return true;
517
518         return false;
519 }
520
521 bool ELine::Matches(const std::string &str)
522 {
523         return ((match(str.c_str(), matchtext.c_str(), true)));
524 }
525
526 bool KLine::Matches(const std::string &str)
527 {
528         return ((match(str.c_str(), matchtext.c_str(), true)));
529 }
530
531 bool GLine::Matches(const std::string &str)
532 {
533         return ((match(str.c_str(), matchtext.c_str(), true)));
534 }
535
536 void ELine::OnAdd()
537 {
538         /* When adding one eline, only check the one eline */
539         for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
540         {
541                 User* u = (User*)(*u2);
542                 if (this->Matches(u))
543                         u->exempt = true;
544         }
545 }
546
547 void ELine::DisplayExpiry()
548 {
549         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed E-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
550 }
551
552 void QLine::DisplayExpiry()
553 {
554         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Q-Line %s (set by %s %d seconds ago)",this->nick,this->source,this->duration);
555 }
556
557 void ZLine::DisplayExpiry()
558 {
559         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed Z-Line %s (set by %s %d seconds ago)",this->ipaddr,this->source,this->duration);
560 }
561
562 void KLine::DisplayExpiry()
563 {
564         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed K-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
565 }
566
567 void GLine::DisplayExpiry()
568 {
569         ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed G-Line %s@%s (set by %s %d seconds ago)",this->identmask,this->hostmask,this->source,this->duration);
570 }
571
572 const char* ELine::Displayable()
573 {
574         return matchtext.c_str();
575 }
576
577 const char* KLine::Displayable()
578 {
579         return matchtext.c_str();
580 }
581
582 const char* GLine::Displayable()
583 {
584         return matchtext.c_str();
585 }
586
587 const char* ZLine::Displayable()
588 {
589         return ipaddr;
590 }
591
592 const char* QLine::Displayable()
593 {
594         return nick;
595 }
596
597 bool XLineManager::RegisterFactory(XLineFactory* xlf)
598 {
599         XLineFactMap::iterator n = line_factory.find(xlf->GetType());
600
601         if (n != line_factory.end())
602                 return false;
603
604         line_factory[xlf->GetType()] = xlf;
605
606         return true;
607 }
608
609 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
610 {
611         XLineFactMap::iterator n = line_factory.find(xlf->GetType());
612
613         if (n == line_factory.end())
614                 return false;
615
616         line_factory.erase(n);
617
618         return true;
619 }
620
621 XLineFactory* XLineManager::GetFactory(const std::string &type)
622 {
623         XLineFactMap::iterator n = line_factory.find(type);
624
625         if (n == line_factory.end())
626                 return NULL;
627
628         return n->second;
629 }
630