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/>.
28 /** An XLineFactory specialized to generate GLine* pointers
30 class GLineFactory : public XLineFactory
33 GLineFactory() : XLineFactory("G") { }
37 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
39 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
40 return new GLine(set_time, duration, source, reason, ih.first, ih.second);
44 /** An XLineFactory specialized to generate ELine* pointers
46 class ELineFactory : public XLineFactory
49 ELineFactory() : XLineFactory("E") { }
53 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
55 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
56 return new ELine(set_time, duration, source, reason, ih.first, ih.second);
60 /** An XLineFactory specialized to generate KLine* pointers
62 class KLineFactory : public XLineFactory
65 KLineFactory() : XLineFactory("K") { }
69 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
71 IdentHostPair ih = ServerInstance->XLines->IdentSplit(xline_specific_mask);
72 return new KLine(set_time, duration, source, reason, ih.first, ih.second);
76 /** An XLineFactory specialized to generate QLine* pointers
78 class QLineFactory : public XLineFactory
81 QLineFactory() : XLineFactory("Q") { }
85 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
87 return new QLine(set_time, duration, source, reason, xline_specific_mask);
91 /** An XLineFactory specialized to generate ZLine* pointers
93 class ZLineFactory : public XLineFactory
96 ZLineFactory() : XLineFactory("Z") { }
100 XLine* Generate(time_t set_time, long duration, std::string source, std::string reason, std::string xline_specific_mask)
102 return new ZLine(set_time, duration, source, reason, xline_specific_mask);
108 * This is now version 3 of the XLine subsystem, let's see if we can get it as nice and
109 * efficient as we can this time so we can close this file and never ever touch it again ..
112 * Version 1 stored all line types in one list (one for g, one for z, etc). This was fine,
113 * but both version 1 and 2 suck at applying lines efficiently. That is, every time a new line
114 * was added, it iterated every existing line for every existing user. Ow. Expiry was also
115 * expensive, as the lists were NOT sorted.
117 * Version 2 moved permanent lines into a seperate list from non-permanent to help optimize
118 * matching speed, but matched in the same way.
119 * Expiry was also sped up by sorting the list by expiry (meaning just remove the items at the
120 * head of the list that are outdated.)
122 * This was fine and good, but it looked less than ideal in code, and matching was still slower
123 * than it could have been, something which we address here.
126 * All lines are (as in v1) stored together -- no seperation of perm and non-perm. They are stored in
127 * a map of maps (first map is line type, second map is for quick lookup on add/delete/etc).
129 * Expiry is *no longer* performed on a timer, and no longer uses a sorted list of any variety. This
130 * is now done by only checking for expiry when a line is accessed, meaning that expiry is no longer
131 * a resource intensive problem.
133 * Application no longer tries to apply every single line on every single user - instead, now only lines
134 * added since the previous application are applied. This keeps S2S ADDLINE during burst nice and fast,
135 * while at the same time not slowing things the fuck down when we try adding a ban with lots of preexisting
139 bool XLine::Matches(User *u)
145 * Checks what users match a given vector of ELines and sets their ban exempt flag accordingly.
147 void XLineManager::CheckELines()
149 ContainerIter n = lookup_lines.find("E");
151 if (n == lookup_lines.end())
154 XLineLookup& ELines = n->second;
159 for (std::vector<LocalUser*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
161 User* u = (User*)(*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())
269 /*ELine* item = new ELine(ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
270 XLineFactory* xlf = GetFactory(line->type);
274 ServerInstance->BanCache->RemoveEntries(line->type, false); // XXX perhaps remove ELines here?
276 if (xlf->AutoApplyToUserList(line))
277 pending_lines.push_back(line);
279 lookup_lines[line->type][line->Displayable()] = line;
282 FOREACH_MOD(I_OnAddLine,OnAddLine(user, line));
287 // deletes a line, returns true if the line existed and was removed
289 bool XLineManager::DelLine(const char* hostmask, const std::string &type, User* user, bool simulate)
291 ContainerIter x = lookup_lines.find(type);
293 if (x == lookup_lines.end())
296 LookupIter y = x->second.find(hostmask);
298 if (y == x->second.end())
304 ServerInstance->BanCache->RemoveEntries(y->second->type, true);
306 FOREACH_MOD(I_OnDelLine,OnDelLine(user, y->second));
310 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), y->second);
311 if (pptr != pending_lines.end())
312 pending_lines.erase(pptr);
323 /* remove exempt from everyone and force recheck after deleting eline */
324 for (std::vector<LocalUser*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
326 User* u = (User*)(*u2);
330 ServerInstance->XLines->CheckELines();
333 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
335 XLine* XLineManager::MatchesLine(const std::string &type, User* user)
337 ContainerIter x = lookup_lines.find(type);
339 if (x == lookup_lines.end())
342 const time_t current = ServerInstance->Time();
346 for (LookupIter i = x->second.begin(); i != x->second.end(); )
351 if (i->second->duration && current > i->second->expiry)
353 /* Expire the line, proceed to next one */
359 if (i->second->Matches(user))
369 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
371 ContainerIter x = lookup_lines.find(type);
373 if (x == lookup_lines.end())
376 const time_t current = ServerInstance->Time();
380 for (LookupIter i = x->second.begin(); i != x->second.end(); )
385 if (i->second->Matches(pattern))
387 if (i->second->duration && current > i->second->expiry)
389 /* Expire the line, return nothing */
404 // removes lines that have expired
405 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
407 FOREACH_MOD(I_OnExpireLine, OnExpireLine(item->second));
409 item->second->DisplayExpiry();
410 item->second->Unset();
412 /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
413 * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
416 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
417 if (pptr != pending_lines.end())
418 pending_lines.erase(pptr);
421 container->second.erase(item);
425 // applies lines, removing clients and changing nicks etc as applicable
426 void XLineManager::ApplyLines()
428 std::vector<LocalUser*>::reverse_iterator u2 = ServerInstance->Users->local_users.rbegin();
429 while (u2 != ServerInstance->Users->local_users.rend())
433 // Don't ban people who are exempt.
437 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
445 pending_lines.clear();
448 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
450 ContainerIter n = lookup_lines.find(type);
452 time_t current = ServerInstance->Time();
456 if (n != lookup_lines.end())
458 XLineLookup& list = n->second;
459 for (LookupIter i = list.begin(); i != list.end(); )
464 if (i->second->duration && current > i->second->expiry)
469 results.push_back(ServerInstance->Config->ServerName+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
470 ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+i->second->source+" :"+i->second->reason);
477 XLineManager::XLineManager()
486 GFact = new GLineFactory;
487 EFact = new ELineFactory;
488 KFact = new KLineFactory;
489 QFact = new QLineFactory;
490 ZFact = new ZLineFactory;
492 RegisterFactory(GFact);
493 RegisterFactory(EFact);
494 RegisterFactory(KFact);
495 RegisterFactory(QFact);
496 RegisterFactory(ZFact);
499 XLineManager::~XLineManager()
501 const char gekqz[] = "GEKQZ";
502 for(unsigned int i=0; i < sizeof(gekqz); i++)
504 XLineFactory* xlf = GetFactory(std::string(1, gekqz[i]));
508 // Delete all existing XLines
509 for (XLineContainer::iterator i = lookup_lines.begin(); i != lookup_lines.end(); i++)
511 for (XLineLookup::iterator j = i->second.begin(); j != i->second.end(); j++)
518 void XLine::Apply(User* u)
522 bool XLine::IsBurstable()
527 void XLine::DefaultApply(User* u, const std::string &line, bool bancache)
529 char sreason[MAXBUF];
530 snprintf(sreason, MAXBUF, "%s-Lined: %s", line.c_str(), this->reason.c_str());
531 if (!ServerInstance->Config->MoronBanner.empty())
532 u->WriteServ("NOTICE %s :*** %s", u->nick.c_str(), ServerInstance->Config->MoronBanner.c_str());
534 if (ServerInstance->Config->HideBans)
535 ServerInstance->Users->QuitUser(u, line + "-Lined", sreason);
537 ServerInstance->Users->QuitUser(u, sreason);
542 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCache: Adding positive hit (" + line + ") for " + u->GetIPString());
543 if (this->duration > 0)
544 ServerInstance->BanCache->AddHit(u->GetIPString(), this->type, line + "-Lined: " + this->reason, this->duration);
546 ServerInstance->BanCache->AddHit(u->GetIPString(), this->type, line + "-Lined: " + this->reason);
550 bool KLine::Matches(User *u)
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)
577 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
579 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
580 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
589 void GLine::Apply(User* u)
591 DefaultApply(u, "G", (this->identmask == "*") ? true : false);
594 bool ELine::Matches(User *u)
599 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
601 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
602 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
611 bool ZLine::Matches(User *u)
616 if (InspIRCd::MatchCIDR(u->GetIPString(), this->ipaddr))
622 void ZLine::Apply(User* u)
624 DefaultApply(u, "Z", true);
628 bool QLine::Matches(User *u)
630 if (InspIRCd::Match(u->nick, this->nick))
636 void QLine::Apply(User* u)
638 /* Force to uuid on apply of qline, no need to disconnect any more :) */
639 u->ForceNickChange(u->uuid.c_str());
643 bool ZLine::Matches(const std::string &str)
645 if (InspIRCd::MatchCIDR(str, this->ipaddr))
651 bool QLine::Matches(const std::string &str)
653 if (InspIRCd::Match(str, this->nick))
659 bool ELine::Matches(const std::string &str)
661 return (InspIRCd::MatchCIDR(str, matchtext));
664 bool KLine::Matches(const std::string &str)
666 return (InspIRCd::MatchCIDR(str.c_str(), matchtext));
669 bool GLine::Matches(const std::string &str)
671 return (InspIRCd::MatchCIDR(str, matchtext));
676 /* When adding one eline, only check the one eline */
677 for (std::vector<LocalUser*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
679 User* u = (User*)(*u2);
680 if (this->Matches(u))
685 void ELine::DisplayExpiry()
687 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired E-Line %s@%s (set by %s %ld seconds ago)",
688 identmask.c_str(),hostmask.c_str(),source.c_str(),(long)(ServerInstance->Time() - this->set_time));
691 void QLine::DisplayExpiry()
693 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired Q-Line %s (set by %s %ld seconds ago)",
694 nick.c_str(),source.c_str(),(long)(ServerInstance->Time() - this->set_time));
697 void ZLine::DisplayExpiry()
699 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired Z-Line %s (set by %s %ld seconds ago)",
700 ipaddr.c_str(),source.c_str(),(long)(ServerInstance->Time() - this->set_time));
703 void KLine::DisplayExpiry()
705 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired K-Line %s@%s (set by %s %ld seconds ago)",
706 identmask.c_str(),hostmask.c_str(),source.c_str(),(long)(ServerInstance->Time() - this->set_time));
709 void GLine::DisplayExpiry()
711 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired G-Line %s@%s (set by %s %ld seconds ago)",
712 identmask.c_str(),hostmask.c_str(),source.c_str(),(long)(ServerInstance->Time() - this->set_time));
715 const char* ELine::Displayable()
717 return matchtext.c_str();
720 const char* KLine::Displayable()
722 return matchtext.c_str();
725 const char* GLine::Displayable()
727 return matchtext.c_str();
730 const char* ZLine::Displayable()
732 return ipaddr.c_str();
735 const char* QLine::Displayable()
740 bool KLine::IsBurstable()
745 bool XLineManager::RegisterFactory(XLineFactory* xlf)
747 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
749 if (n != line_factory.end())
752 line_factory[xlf->GetType()] = xlf;
757 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
759 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
761 if (n == line_factory.end())
764 line_factory.erase(n);
769 XLineFactory* XLineManager::GetFactory(const std::string &type)
771 XLineFactMap::iterator n = line_factory.find(type);
773 if (n == line_factory.end())