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().c_str());
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().c_str()] = 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 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), y->second);
316 if (pptr != pending_lines.end())
317 pending_lines.erase(pptr);
328 /* remove exempt from everyone and force recheck after deleting eline */
329 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
330 for (UserManager::LocalList::const_iterator u2 = list.begin(); u2 != list.end(); u2++)
336 ServerInstance->XLines->CheckELines();
339 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
341 XLine* XLineManager::MatchesLine(const std::string &type, User* user)
343 ContainerIter x = lookup_lines.find(type);
345 if (x == lookup_lines.end())
348 const time_t current = ServerInstance->Time();
352 for (LookupIter i = x->second.begin(); i != x->second.end(); )
357 if (i->second->duration && current > i->second->expiry)
359 /* Expire the line, proceed to next one */
365 if (i->second->Matches(user))
375 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
377 ContainerIter x = lookup_lines.find(type);
379 if (x == lookup_lines.end())
382 const time_t current = ServerInstance->Time();
386 for (LookupIter i = x->second.begin(); i != x->second.end(); )
391 if (i->second->Matches(pattern))
393 if (i->second->duration && current > i->second->expiry)
395 /* Expire the line, return nothing */
410 // removes lines that have expired
411 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
413 FOREACH_MOD(OnExpireLine, (item->second));
415 item->second->DisplayExpiry();
416 item->second->Unset();
418 /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
419 * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
422 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
423 if (pptr != pending_lines.end())
424 pending_lines.erase(pptr);
427 container->second.erase(item);
431 // applies lines, removing clients and changing nicks etc as applicable
432 void XLineManager::ApplyLines()
434 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
435 for (UserManager::LocalList::const_iterator j = list.begin(); j != list.end(); ++j)
439 // Don't ban people who are exempt.
443 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
451 pending_lines.clear();
454 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
456 ContainerIter n = lookup_lines.find(type);
458 time_t current = ServerInstance->Time();
462 if (n != lookup_lines.end())
464 XLineLookup& list = n->second;
465 for (LookupIter i = list.begin(); i != list.end(); )
470 if (i->second->duration && current > i->second->expiry)
475 results.push_back(ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
476 ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+i->second->source+" :"+i->second->reason);
483 XLineManager::XLineManager()
492 GFact = new GLineFactory;
493 EFact = new ELineFactory;
494 KFact = new KLineFactory;
495 QFact = new QLineFactory;
496 ZFact = new ZLineFactory;
498 RegisterFactory(GFact);
499 RegisterFactory(EFact);
500 RegisterFactory(KFact);
501 RegisterFactory(QFact);
502 RegisterFactory(ZFact);
505 XLineManager::~XLineManager()
507 const char gekqz[] = "GEKQZ";
508 for(unsigned int i=0; i < sizeof(gekqz); i++)
510 XLineFactory* xlf = GetFactory(std::string(1, gekqz[i]));
514 // Delete all existing XLines
515 for (XLineContainer::iterator i = lookup_lines.begin(); i != lookup_lines.end(); i++)
517 for (XLineLookup::iterator j = i->second.begin(); j != i->second.end(); j++)
524 void XLine::Apply(User* u)
528 bool XLine::IsBurstable()
533 void XLine::DefaultApply(User* u, const std::string &line, bool bancache)
535 const std::string banReason = line + "-Lined: " + reason;
537 if (!ServerInstance->Config->XLineMessage.empty())
538 u->WriteNotice("*** " + ServerInstance->Config->XLineMessage);
540 if (ServerInstance->Config->HideBans)
541 ServerInstance->Users->QuitUser(u, line + "-Lined", &banReason);
543 ServerInstance->Users->QuitUser(u, banReason);
548 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Adding positive hit (" + line + ") for " + u->GetIPString());
549 ServerInstance->BanCache.AddHit(u->GetIPString(), this->type, banReason, this->duration);
553 bool KLine::Matches(User *u)
555 LocalUser* lu = IS_LOCAL(u);
556 if (lu && lu->exempt)
559 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
561 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
562 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
571 void KLine::Apply(User* u)
573 DefaultApply(u, "K", (this->identmask == "*") ? true : false);
576 bool GLine::Matches(User *u)
578 LocalUser* lu = IS_LOCAL(u);
579 if (lu && lu->exempt)
582 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
584 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
585 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
594 void GLine::Apply(User* u)
596 DefaultApply(u, "G", (this->identmask == "*") ? true : false);
599 bool ELine::Matches(User *u)
601 LocalUser* lu = IS_LOCAL(u);
602 if (lu && lu->exempt)
605 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
607 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
608 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
617 bool ZLine::Matches(User *u)
619 LocalUser* lu = IS_LOCAL(u);
620 if (lu && lu->exempt)
623 if (InspIRCd::MatchCIDR(u->GetIPString(), this->ipaddr))
629 void ZLine::Apply(User* u)
631 DefaultApply(u, "Z", true);
635 bool QLine::Matches(User *u)
637 if (InspIRCd::Match(u->nick, this->nick))
643 void QLine::Apply(User* u)
645 /* Force to uuid on apply of qline, no need to disconnect any more :) */
646 u->ChangeNick(u->uuid);
650 bool ZLine::Matches(const std::string &str)
652 if (InspIRCd::MatchCIDR(str, this->ipaddr))
658 bool QLine::Matches(const std::string &str)
660 if (InspIRCd::Match(str, this->nick))
666 bool ELine::Matches(const std::string &str)
668 return (InspIRCd::MatchCIDR(str, matchtext));
671 bool KLine::Matches(const std::string &str)
673 return (InspIRCd::MatchCIDR(str.c_str(), matchtext));
676 bool GLine::Matches(const std::string &str)
678 return (InspIRCd::MatchCIDR(str, matchtext));
683 /* When adding one eline, only check the one eline */
684 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
685 for (UserManager::LocalList::const_iterator u2 = list.begin(); u2 != list.end(); u2++)
688 if (this->Matches(u))
693 void XLine::DisplayExpiry()
695 bool onechar = (type.length() == 1);
696 ServerInstance->SNO->WriteToSnoMask('x', "Removing expired %s%s %s (set by %s %ld seconds ago)",
697 type.c_str(), (onechar ? "-Line" : ""), Displayable().c_str(), source.c_str(), (long)(ServerInstance->Time() - set_time));
700 const std::string& ELine::Displayable()
705 const std::string& KLine::Displayable()
710 const std::string& GLine::Displayable()
715 const std::string& ZLine::Displayable()
720 const std::string& QLine::Displayable()
725 bool KLine::IsBurstable()
730 bool XLineManager::RegisterFactory(XLineFactory* xlf)
732 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
734 if (n != line_factory.end())
737 line_factory[xlf->GetType()] = xlf;
742 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
744 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
746 if (n == line_factory.end())
749 line_factory.erase(n);
754 XLineFactory* XLineManager::GetFactory(const std::string &type)
756 XLineFactMap::iterator n = line_factory.find(type);
758 if (n == line_factory.end())