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)
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)
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)
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)
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)
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++)
163 /* This uses safe iteration to ensure that if a line expires here, it doenst trash the iterator */
166 for (LookupIter i = ELines.begin(); i != ELines.end(); )
171 XLine *e = i->second;
172 u->exempt = e->Matches(u);
180 XLineLookup* XLineManager::GetAll(const std::string &type)
182 ContainerIter n = lookup_lines.find(type);
184 if (n == lookup_lines.end())
188 const time_t current = ServerInstance->Time();
190 /* Expire any dead ones, before sending */
191 for (LookupIter x = n->second.begin(); x != n->second.end(); )
195 if (x->second->duration && current > x->second->expiry)
205 void XLineManager::DelAll(const std::string &type)
207 ContainerIter n = lookup_lines.find(type);
209 if (n == lookup_lines.end())
214 /* Delete all of a given type (this should probably use DelLine, but oh well) */
215 while ((x = n->second.begin()) != n->second.end())
221 std::vector<std::string> XLineManager::GetAllTypes()
223 std::vector<std::string> items;
224 for (ContainerIter x = lookup_lines.begin(); x != lookup_lines.end(); ++x)
225 items.push_back(x->first);
229 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
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)
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())
239 if (!n.second.length())
240 n.second.assign("*");
245 n.second = ident_and_host;
253 bool XLineManager::AddLine(XLine* line, User* user)
255 if (line->duration && ServerInstance->Time() > line->expiry)
256 return false; // Don't apply expired XLines.
258 /* Don't apply duplicate xlines */
259 ContainerIter x = lookup_lines.find(line->type);
260 if (x != lookup_lines.end())
262 LookupIter i = x->second.find(line->Displayable());
263 if (i != x->second.end())
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))
274 /*ELine* item = new ELine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
275 XLineFactory* xlf = GetFactory(line->type);
279 ServerInstance->BanCache.RemoveEntries(line->type, false); // XXX perhaps remove ELines here?
281 if (xlf->AutoApplyToUserList(line))
282 pending_lines.push_back(line);
284 lookup_lines[line->type][line->Displayable()] = line;
287 FOREACH_MOD(OnAddLine, (user, line));
292 // deletes a line, returns true if the line existed and was removed
294 bool XLineManager::DelLine(const char* hostmask, const std::string &type, User* user, bool simulate)
296 ContainerIter x = lookup_lines.find(type);
298 if (x == lookup_lines.end())
301 LookupIter y = x->second.find(hostmask);
303 if (y == x->second.end())
309 ServerInstance->BanCache.RemoveEntries(y->second->type, true);
311 FOREACH_MOD(OnDelLine, (user, y->second));
315 stdalgo::erase(pending_lines, y->second);
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++)
334 ServerInstance->XLines->CheckELines();
337 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
339 XLine* XLineManager::MatchesLine(const std::string &type, User* user)
341 ContainerIter x = lookup_lines.find(type);
343 if (x == lookup_lines.end())
346 const time_t current = ServerInstance->Time();
350 for (LookupIter i = x->second.begin(); i != x->second.end(); )
355 if (i->second->duration && current > i->second->expiry)
357 /* Expire the line, proceed to next one */
363 if (i->second->Matches(user))
373 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
375 ContainerIter x = lookup_lines.find(type);
377 if (x == lookup_lines.end())
380 const time_t current = ServerInstance->Time();
384 for (LookupIter i = x->second.begin(); i != x->second.end(); )
389 if (i->second->Matches(pattern))
391 if (i->second->duration && current > i->second->expiry)
393 /* Expire the line, return nothing */
408 // removes lines that have expired
409 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
411 FOREACH_MOD(OnExpireLine, (item->second));
413 item->second->DisplayExpiry();
414 item->second->Unset();
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?
420 stdalgo::erase(pending_lines, item->second);
423 container->second.erase(item);
427 // applies lines, removing clients and changing nicks etc as applicable
428 void XLineManager::ApplyLines()
430 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
431 for (UserManager::LocalList::const_iterator j = list.begin(); j != list.end(); ++j)
435 // Don't ban people who are exempt.
439 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
447 pending_lines.clear();
450 void XLineManager::InvokeStats(const std::string& type, unsigned int numeric, Stats::Context& stats)
452 ContainerIter n = lookup_lines.find(type);
454 time_t current = ServerInstance->Time();
458 if (n != lookup_lines.end())
460 XLineLookup& list = n->second;
461 for (LookupIter i = list.begin(); i != list.end(); )
466 if (i->second->duration && current > i->second->expiry)
471 stats.AddRow(numeric, i->second->Displayable()+" "+
472 ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+i->second->source+" :"+i->second->reason);
479 XLineManager::XLineManager()
488 GFact = new GLineFactory;
489 EFact = new ELineFactory;
490 KFact = new KLineFactory;
491 QFact = new QLineFactory;
492 ZFact = new ZLineFactory;
494 RegisterFactory(GFact);
495 RegisterFactory(EFact);
496 RegisterFactory(KFact);
497 RegisterFactory(QFact);
498 RegisterFactory(ZFact);
501 XLineManager::~XLineManager()
503 const char gekqz[] = "GEKQZ";
504 for(unsigned int i=0; i < sizeof(gekqz); i++)
506 XLineFactory* xlf = GetFactory(std::string(1, gekqz[i]));
510 // Delete all existing XLines
511 for (XLineContainer::iterator i = lookup_lines.begin(); i != lookup_lines.end(); i++)
513 for (XLineLookup::iterator j = i->second.begin(); j != i->second.end(); j++)
520 void XLine::Apply(User* u)
524 bool XLine::IsBurstable()
529 void XLine::DefaultApply(User* u, const std::string &line, bool bancache)
531 const std::string banReason = line + "-Lined: " + reason;
533 if (!ServerInstance->Config->XLineMessage.empty())
534 u->WriteNumeric(ERR_YOUREBANNEDCREEP, ServerInstance->Config->XLineMessage);
536 if (ServerInstance->Config->HideBans)
537 ServerInstance->Users->QuitUser(u, line + "-Lined", &banReason);
539 ServerInstance->Users->QuitUser(u, banReason);
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);
549 bool KLine::Matches(User *u)
551 LocalUser* lu = IS_LOCAL(u);
552 if (lu && lu->exempt)
555 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
557 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
558 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
567 void KLine::Apply(User* u)
569 DefaultApply(u, "K", (this->identmask == "*") ? true : false);
572 bool GLine::Matches(User *u)
574 LocalUser* lu = IS_LOCAL(u);
575 if (lu && lu->exempt)
578 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
580 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
581 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
590 void GLine::Apply(User* u)
592 DefaultApply(u, "G", (this->identmask == "*") ? true : false);
595 bool ELine::Matches(User *u)
597 LocalUser* lu = IS_LOCAL(u);
598 if (lu && lu->exempt)
601 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
603 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
604 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
613 bool ZLine::Matches(User *u)
615 LocalUser* lu = IS_LOCAL(u);
616 if (lu && lu->exempt)
619 if (InspIRCd::MatchCIDR(u->GetIPString(), this->ipaddr))
625 void ZLine::Apply(User* u)
627 DefaultApply(u, "Z", true);
631 bool QLine::Matches(User *u)
633 if (InspIRCd::Match(u->nick, this->nick))
639 void QLine::Apply(User* u)
641 /* Force to uuid on apply of qline, no need to disconnect any more :) */
642 u->ChangeNick(u->uuid);
646 bool ZLine::Matches(const std::string &str)
648 if (InspIRCd::MatchCIDR(str, this->ipaddr))
654 bool QLine::Matches(const std::string &str)
656 if (InspIRCd::Match(str, this->nick))
662 bool ELine::Matches(const std::string &str)
664 return (InspIRCd::MatchCIDR(str, matchtext));
667 bool KLine::Matches(const std::string &str)
669 return (InspIRCd::MatchCIDR(str.c_str(), matchtext));
672 bool GLine::Matches(const std::string &str)
674 return (InspIRCd::MatchCIDR(str, matchtext));
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++)
684 if (this->Matches(u))
689 void XLine::DisplayExpiry()
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));
696 const std::string& ELine::Displayable()
701 const std::string& KLine::Displayable()
706 const std::string& GLine::Displayable()
711 const std::string& ZLine::Displayable()
716 const std::string& QLine::Displayable()
721 bool KLine::IsBurstable()
726 bool XLineManager::RegisterFactory(XLineFactory* xlf)
728 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
730 if (n != line_factory.end())
733 line_factory[xlf->GetType()] = xlf;
738 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
740 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
742 if (n == line_factory.end())
745 line_factory.erase(n);
750 XLineFactory* XLineManager::GetFactory(const std::string &type)
752 XLineFactMap::iterator n = line_factory.find(type);
754 if (n == line_factory.end())