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