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 for (LocalUserList::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
162 /* This uses safe iteration to ensure that if a line expires here, it doenst trash the iterator */
165 for (LookupIter i = ELines.begin(); i != ELines.end(); )
170 XLine *e = i->second;
171 u->exempt = e->Matches(u);
179 XLineLookup* XLineManager::GetAll(const std::string &type)
181 ContainerIter n = lookup_lines.find(type);
183 if (n == lookup_lines.end())
187 const time_t current = ServerInstance->Time();
189 /* Expire any dead ones, before sending */
190 for (LookupIter x = n->second.begin(); x != n->second.end(); )
194 if (x->second->duration && current > x->second->expiry)
204 void XLineManager::DelAll(const std::string &type)
206 ContainerIter n = lookup_lines.find(type);
208 if (n == lookup_lines.end())
213 /* Delete all of a given type (this should probably use DelLine, but oh well) */
214 while ((x = n->second.begin()) != n->second.end())
220 std::vector<std::string> XLineManager::GetAllTypes()
222 std::vector<std::string> items;
223 for (ContainerIter x = lookup_lines.begin(); x != lookup_lines.end(); ++x)
224 items.push_back(x->first);
228 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
230 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
231 std::string::size_type x = ident_and_host.find('@');
232 if (x != std::string::npos)
234 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
235 n.first = ident_and_host.substr(0, x);
236 if (!n.first.length())
238 if (!n.second.length())
239 n.second.assign("*");
244 n.second = ident_and_host;
252 bool XLineManager::AddLine(XLine* line, User* user)
254 if (line->duration && ServerInstance->Time() > line->expiry)
255 return false; // Don't apply expired XLines.
257 /* Don't apply duplicate xlines */
258 ContainerIter x = lookup_lines.find(line->type);
259 if (x != lookup_lines.end())
261 LookupIter i = x->second.find(line->Displayable().c_str());
262 if (i != x->second.end())
264 // XLine propagation bug was here, if the line to be added already exists and
265 // it's expired then expire it and add the new one instead of returning false
266 if ((!i->second->duration) || (ServerInstance->Time() < i->second->expiry))
273 /*ELine* item = new ELine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
274 XLineFactory* xlf = GetFactory(line->type);
278 ServerInstance->BanCache.RemoveEntries(line->type, false); // XXX perhaps remove ELines here?
280 if (xlf->AutoApplyToUserList(line))
281 pending_lines.push_back(line);
283 lookup_lines[line->type][line->Displayable().c_str()] = line;
286 FOREACH_MOD(OnAddLine, (user, line));
291 // deletes a line, returns true if the line existed and was removed
293 bool XLineManager::DelLine(const char* hostmask, const std::string &type, User* user, bool simulate)
295 ContainerIter x = lookup_lines.find(type);
297 if (x == lookup_lines.end())
300 LookupIter y = x->second.find(hostmask);
302 if (y == x->second.end())
308 ServerInstance->BanCache.RemoveEntries(y->second->type, true);
310 FOREACH_MOD(OnDelLine, (user, y->second));
314 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), y->second);
315 if (pptr != pending_lines.end())
316 pending_lines.erase(pptr);
327 /* remove exempt from everyone and force recheck after deleting eline */
328 for (LocalUserList::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.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 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
421 if (pptr != pending_lines.end())
422 pending_lines.erase(pptr);
425 container->second.erase(item);
429 // applies lines, removing clients and changing nicks etc as applicable
430 void XLineManager::ApplyLines()
432 LocalUserList& list = ServerInstance->Users->local_users;
433 for (LocalUserList::iterator j = list.begin(); j != list.end(); ++j)
437 // Don't ban people who are exempt.
441 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
449 pending_lines.clear();
452 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
454 ContainerIter n = lookup_lines.find(type);
456 time_t current = ServerInstance->Time();
460 if (n != lookup_lines.end())
462 XLineLookup& list = n->second;
463 for (LookupIter i = list.begin(); i != list.end(); )
468 if (i->second->duration && current > i->second->expiry)
473 results.push_back(ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
474 ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+i->second->source+" :"+i->second->reason);
481 XLineManager::XLineManager()
490 GFact = new GLineFactory;
491 EFact = new ELineFactory;
492 KFact = new KLineFactory;
493 QFact = new QLineFactory;
494 ZFact = new ZLineFactory;
496 RegisterFactory(GFact);
497 RegisterFactory(EFact);
498 RegisterFactory(KFact);
499 RegisterFactory(QFact);
500 RegisterFactory(ZFact);
503 XLineManager::~XLineManager()
505 const char gekqz[] = "GEKQZ";
506 for(unsigned int i=0; i < sizeof(gekqz); i++)
508 XLineFactory* xlf = GetFactory(std::string(1, gekqz[i]));
512 // Delete all existing XLines
513 for (XLineContainer::iterator i = lookup_lines.begin(); i != lookup_lines.end(); i++)
515 for (XLineLookup::iterator j = i->second.begin(); j != i->second.end(); j++)
522 void XLine::Apply(User* u)
526 bool XLine::IsBurstable()
531 void XLine::DefaultApply(User* u, const std::string &line, bool bancache)
533 const std::string banReason = line + "-Lined: " + reason;
535 if (!ServerInstance->Config->XLineMessage.empty())
536 u->WriteNotice("*** " + ServerInstance->Config->XLineMessage);
538 if (ServerInstance->Config->HideBans)
539 ServerInstance->Users->QuitUser(u, line + "-Lined", &banReason);
541 ServerInstance->Users->QuitUser(u, banReason);
546 ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Adding positive hit (" + line + ") for " + u->GetIPString());
547 ServerInstance->BanCache.AddHit(u->GetIPString(), this->type, banReason, this->duration);
551 bool KLine::Matches(User *u)
553 LocalUser* lu = IS_LOCAL(u);
554 if (lu && lu->exempt)
557 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
559 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
560 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
569 void KLine::Apply(User* u)
571 DefaultApply(u, "K", (this->identmask == "*") ? true : false);
574 bool GLine::Matches(User *u)
576 LocalUser* lu = IS_LOCAL(u);
577 if (lu && lu->exempt)
580 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
582 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
583 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
592 void GLine::Apply(User* u)
594 DefaultApply(u, "G", (this->identmask == "*") ? true : false);
597 bool ELine::Matches(User *u)
599 LocalUser* lu = IS_LOCAL(u);
600 if (lu && lu->exempt)
603 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
605 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
606 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
615 bool ZLine::Matches(User *u)
617 LocalUser* lu = IS_LOCAL(u);
618 if (lu && lu->exempt)
621 if (InspIRCd::MatchCIDR(u->GetIPString(), this->ipaddr))
627 void ZLine::Apply(User* u)
629 DefaultApply(u, "Z", true);
633 bool QLine::Matches(User *u)
635 if (InspIRCd::Match(u->nick, this->nick))
641 void QLine::Apply(User* u)
643 /* Force to uuid on apply of qline, no need to disconnect any more :) */
644 u->ChangeNick(u->uuid);
648 bool ZLine::Matches(const std::string &str)
650 if (InspIRCd::MatchCIDR(str, this->ipaddr))
656 bool QLine::Matches(const std::string &str)
658 if (InspIRCd::Match(str, this->nick))
664 bool ELine::Matches(const std::string &str)
666 return (InspIRCd::MatchCIDR(str, matchtext));
669 bool KLine::Matches(const std::string &str)
671 return (InspIRCd::MatchCIDR(str.c_str(), matchtext));
674 bool GLine::Matches(const std::string &str)
676 return (InspIRCd::MatchCIDR(str, matchtext));
681 /* When adding one eline, only check the one eline */
682 for (LocalUserList::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
685 if (this->Matches(u))
690 void XLine::DisplayExpiry()
692 bool onechar = (type.length() == 1);
693 ServerInstance->SNO->WriteToSnoMask('x', "Removing expired %s%s %s (set by %s %ld seconds ago)",
694 type.c_str(), (onechar ? "-Line" : ""), Displayable().c_str(), source.c_str(), (long)(ServerInstance->Time() - set_time));
697 const std::string& ELine::Displayable()
702 const std::string& KLine::Displayable()
707 const std::string& GLine::Displayable()
712 const std::string& ZLine::Displayable()
717 const std::string& QLine::Displayable()
722 bool KLine::IsBurstable()
727 bool XLineManager::RegisterFactory(XLineFactory* xlf)
729 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
731 if (n != line_factory.end())
734 line_factory[xlf->GetType()] = xlf;
739 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
741 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
743 if (n == line_factory.end())
746 line_factory.erase(n);
751 XLineFactory* XLineManager::GetFactory(const std::string &type)
753 XLineFactMap::iterator n = line_factory.find(type);
755 if (n == line_factory.end())