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