2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
27 /** An XLineFactory specialized to generate GLine* pointers
29 class GLineFactory : public XLineFactory
32 GLineFactory() : XLineFactory("G") { }
36 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask) CXX11_OVERRIDE
38 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
39 return new GLine(set_time, duration, source, reason, ih.first, ih.second);
43 /** An XLineFactory specialized to generate ELine* pointers
45 class ELineFactory : public XLineFactory
48 ELineFactory() : XLineFactory("E") { }
52 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask) CXX11_OVERRIDE
54 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
55 return new ELine(set_time, duration, source, reason, ih.first, ih.second);
59 /** An XLineFactory specialized to generate KLine* pointers
61 class KLineFactory : public XLineFactory
64 KLineFactory() : XLineFactory("K") { }
68 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask) CXX11_OVERRIDE
70 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
71 return new KLine(set_time, duration, source, reason, ih.first, ih.second);
75 /** An XLineFactory specialized to generate QLine* pointers
77 class QLineFactory : public XLineFactory
80 QLineFactory() : XLineFactory("Q") { }
84 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask) CXX11_OVERRIDE
86 return new QLine(set_time, duration, source, reason, xline_specific_mask);
90 /** An XLineFactory specialized to generate ZLine* pointers
92 class ZLineFactory : public XLineFactory
95 ZLineFactory() : XLineFactory("Z") { }
99 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask) CXX11_OVERRIDE
101 return new ZLine(set_time, duration, source, reason, xline_specific_mask);
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 ..
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.
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.)
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.
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).
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.
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
138 bool XLine::Matches(User *u)
144 * Checks what users match a given vector of ELines and sets their ban exempt flag accordingly.
146 void XLineManager::CheckELines()
148 ContainerIter n = lookup_lines.find("E");
150 if (n == lookup_lines.end())
153 XLineLookup& ELines = n->second;
158 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
159 for (UserManager::LocalList::const_iterator u2 = list.begin(); u2 != list.end(); u2++)
164 /* This uses safe iteration to ensure that if a line expires here, it doenst trash the iterator */
167 for (LookupIter i = ELines.begin(); i != ELines.end(); )
172 XLine *e = i->second;
173 if ((!e->duration || ServerInstance->Time() < e->expiry) && e->Matches(u))
182 XLineLookup* XLineManager::GetAll(const std::string &type)
184 ContainerIter n = lookup_lines.find(type);
186 if (n == lookup_lines.end())
190 const time_t current = ServerInstance->Time();
192 /* Expire any dead ones, before sending */
193 for (LookupIter x = n->second.begin(); x != n->second.end(); )
197 if (x->second->duration && current > x->second->expiry)
207 void XLineManager::DelAll(const std::string &type)
209 ContainerIter n = lookup_lines.find(type);
211 if (n == lookup_lines.end())
216 /* Delete all of a given type (this should probably use DelLine, but oh well) */
217 while ((x = n->second.begin()) != n->second.end())
223 std::vector<std::string> XLineManager::GetAllTypes()
225 std::vector<std::string> items;
226 for (ContainerIter x = lookup_lines.begin(); x != lookup_lines.end(); ++x)
227 items.push_back(x->first);
231 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
233 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
234 std::string::size_type x = ident_and_host.find('@');
235 if (x != std::string::npos)
237 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
238 n.first = ident_and_host.substr(0, x);
239 if (!n.first.length())
241 if (!n.second.length())
242 n.second.assign("*");
247 n.second = ident_and_host;
255 bool XLineManager::AddLine(XLine* line, User* user)
257 if (line->duration && ServerInstance->Time() > line->expiry)
258 return false; // Don't apply expired XLines.
260 /* Don't apply duplicate xlines */
261 ContainerIter x = lookup_lines.find(line->type);
262 if (x != lookup_lines.end())
264 LookupIter i = x->second.find(line->Displayable());
265 if (i != x->second.end())
267 // XLine propagation bug was here, if the line to be added already exists and
268 // it's expired then expire it and add the new one instead of returning false
269 if ((!i->second->duration) || (ServerInstance->Time() < i->second->expiry))
276 /*ELine* item = new ELine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
277 XLineFactory* xlf = GetFactory(line->type);
281 ServerInstance->BanCache.RemoveEntries(line->type, false); // XXX perhaps remove ELines here?
283 if (xlf->AutoApplyToUserList(line))
284 pending_lines.push_back(line);
286 lookup_lines[line->type][line->Displayable()] = line;
289 FOREACH_MOD(OnAddLine, (user, line));
294 // deletes a line, returns true if the line existed and was removed
296 bool XLineManager::DelLine(const char* hostmask, const std::string &type, User* user, bool simulate)
298 ContainerIter x = lookup_lines.find(type);
300 if (x == lookup_lines.end())
303 LookupIter y = x->second.find(hostmask);
305 if (y == x->second.end())
311 ServerInstance->BanCache.RemoveEntries(y->second->type, true);
313 FOREACH_MOD(OnDelLine, (user, y->second));
317 stdalgo::erase(pending_lines, y->second);
328 ServerInstance->XLines->CheckELines();
331 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
333 XLine* XLineManager::MatchesLine(const std::string &type, User* user)
335 ContainerIter x = lookup_lines.find(type);
337 if (x == lookup_lines.end())
340 const time_t current = ServerInstance->Time();
344 for (LookupIter i = x->second.begin(); i != x->second.end(); )
349 if (i->second->duration && current > i->second->expiry)
351 /* Expire the line, proceed to next one */
357 if (i->second->Matches(user))
367 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
369 ContainerIter x = lookup_lines.find(type);
371 if (x == lookup_lines.end())
374 const time_t current = ServerInstance->Time();
378 for (LookupIter i = x->second.begin(); i != x->second.end(); )
383 if (i->second->Matches(pattern))
385 if (i->second->duration && current > i->second->expiry)
387 /* Expire the line, return nothing */
402 // removes lines that have expired
403 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
405 FOREACH_MOD(OnExpireLine, (item->second));
407 item->second->DisplayExpiry();
408 item->second->Unset();
410 /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
411 * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
414 stdalgo::erase(pending_lines, item->second);
417 container->second.erase(item);
421 // applies lines, removing clients and changing nicks etc as applicable
422 void XLineManager::ApplyLines()
424 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
425 for (UserManager::LocalList::const_iterator j = list.begin(); j != list.end(); ++j)
429 // Don't ban people who are exempt.
433 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
441 pending_lines.clear();
444 void XLineManager::InvokeStats(const std::string& type, unsigned int numeric, Stats::Context& stats)
446 ContainerIter n = lookup_lines.find(type);
448 time_t current = ServerInstance->Time();
452 if (n != lookup_lines.end())
454 XLineLookup& list = n->second;
455 for (LookupIter i = list.begin(); i != list.end(); )
460 if (i->second->duration && current > i->second->expiry)
465 stats.AddRow(numeric, i->second->Displayable()+" "+
466 ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+i->second->source+" :"+i->second->reason);
473 XLineManager::XLineManager()
482 GFact = new GLineFactory;
483 EFact = new ELineFactory;
484 KFact = new KLineFactory;
485 QFact = new QLineFactory;
486 ZFact = new ZLineFactory;
488 RegisterFactory(GFact);
489 RegisterFactory(EFact);
490 RegisterFactory(KFact);
491 RegisterFactory(QFact);
492 RegisterFactory(ZFact);
495 XLineManager::~XLineManager()
497 const char gekqz[] = "GEKQZ";
498 for(unsigned int i=0; i < sizeof(gekqz); i++)
500 XLineFactory* xlf = GetFactory(std::string(1, gekqz[i]));
504 // Delete all existing XLines
505 for (XLineContainer::iterator i = lookup_lines.begin(); i != lookup_lines.end(); i++)
507 for (XLineLookup::iterator j = i->second.begin(); j != i->second.end(); j++)
514 void XLine::Apply(User* u)
518 bool XLine::IsBurstable()
523 void XLine::DefaultApply(User* u, const std::string &line, bool bancache)
525 const std::string banReason = line + "-Lined: " + reason;
527 if (!ServerInstance->Config->XLineMessage.empty())
528 u->WriteNumeric(ERR_YOUREBANNEDCREEP, ServerInstance->Config->XLineMessage);
530 if (ServerInstance->Config->HideBans)
531 ServerInstance->Users->QuitUser(u, line + "-Lined", &banReason);
533 ServerInstance->Users->QuitUser(u, banReason);
538 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Adding positive hit (" + line + ") for " + u->GetIPString());
539 ServerInstance->BanCache.AddHit(u->GetIPString(), this->type, banReason, this->duration);
543 bool KLine::Matches(User *u)
545 LocalUser* lu = IS_LOCAL(u);
546 if (lu && lu->exempt)
549 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
551 if (InspIRCd::MatchCIDR(u->GetRealHost(), this->hostmask, ascii_case_insensitive_map) ||
552 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
561 void KLine::Apply(User* u)
563 DefaultApply(u, "K", (this->identmask == "*") ? true : false);
566 bool GLine::Matches(User *u)
568 LocalUser* lu = IS_LOCAL(u);
569 if (lu && lu->exempt)
572 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
574 if (InspIRCd::MatchCIDR(u->GetRealHost(), this->hostmask, ascii_case_insensitive_map) ||
575 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
584 void GLine::Apply(User* u)
586 DefaultApply(u, "G", (this->identmask == "*") ? true : false);
589 bool ELine::Matches(User *u)
591 LocalUser* lu = IS_LOCAL(u);
592 if (lu && lu->exempt)
595 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
597 if (InspIRCd::MatchCIDR(u->GetRealHost(), this->hostmask, ascii_case_insensitive_map) ||
598 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
607 bool ZLine::Matches(User *u)
609 LocalUser* lu = IS_LOCAL(u);
610 if (lu && lu->exempt)
613 if (InspIRCd::MatchCIDR(u->GetIPString(), this->ipaddr))
619 void ZLine::Apply(User* u)
621 DefaultApply(u, "Z", true);
625 bool QLine::Matches(User *u)
627 if (InspIRCd::Match(u->nick, this->nick))
633 void QLine::Apply(User* u)
635 /* Force to uuid on apply of qline, no need to disconnect any more :) */
636 u->ChangeNick(u->uuid);
640 bool ZLine::Matches(const std::string &str)
642 if (InspIRCd::MatchCIDR(str, this->ipaddr))
648 bool QLine::Matches(const std::string &str)
650 if (InspIRCd::Match(str, this->nick))
656 bool ELine::Matches(const std::string &str)
658 return (InspIRCd::MatchCIDR(str, matchtext));
661 bool KLine::Matches(const std::string &str)
663 return (InspIRCd::MatchCIDR(str.c_str(), matchtext));
666 bool GLine::Matches(const std::string &str)
668 return (InspIRCd::MatchCIDR(str, matchtext));
673 /* When adding one eline, only check the one eline */
674 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
675 for (UserManager::LocalList::const_iterator u2 = list.begin(); u2 != list.end(); u2++)
678 if (this->Matches(u))
683 void XLine::DisplayExpiry()
685 bool onechar = (type.length() == 1);
686 ServerInstance->SNO->WriteToSnoMask('x', "Removing expired %s%s %s (set by %s %ld seconds ago)",
687 type.c_str(), (onechar ? "-Line" : ""), Displayable().c_str(), source.c_str(), (long)(ServerInstance->Time() - set_time));
690 const std::string& ELine::Displayable()
695 const std::string& KLine::Displayable()
700 const std::string& GLine::Displayable()
705 const std::string& ZLine::Displayable()
710 const std::string& QLine::Displayable()
715 bool KLine::IsBurstable()
720 bool XLineManager::RegisterFactory(XLineFactory* xlf)
722 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
724 if (n != line_factory.end())
727 line_factory[xlf->GetType()] = xlf;
732 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
734 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
736 if (n == line_factory.end())
739 line_factory.erase(n);
744 XLineFactory* XLineManager::GetFactory(const std::string &type)
746 XLineFactMap::iterator n = line_factory.find(type);
748 if (n == line_factory.end())