1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
21 * This is now version 3 of the XLine subsystem, let's see if we can get it as nice and
22 * efficient as we can this time so we can close this file and never ever touch it again ..
25 * Version 1 stored all line types in one list (one for g, one for z, etc). This was fine,
26 * but both version 1 and 2 suck at applying lines efficiently. That is, every time a new line
27 * was added, it iterated every existing line for every existing user. Ow. Expiry was also
28 * expensive, as the lists were NOT sorted.
30 * Version 2 moved permanent lines into a seperate list from non-permanent to help optimize
31 * matching speed, but matched in the same way.
32 * Expiry was also sped up by sorting the list by expiry (meaning just remove the items at the
33 * head of the list that are outdated.)
35 * This was fine and good, but it looked less than ideal in code, and matching was still slower
36 * than it could have been, something which we address here.
39 * All lines are (as in v1) stored together -- no seperation of perm and non-perm. They are stored in
40 * a map of maps (first map is line type, second map is for quick lookup on add/delete/etc).
42 * Expiry is *no longer* performed on a timer, and no longer uses a sorted list of any variety. This
43 * is now done by only checking for expiry when a line is accessed, meaning that expiry is no longer
44 * a resource intensive problem.
46 * Application no longer tries to apply every single line on every single user - instead, now only lines
47 * added since the previous application are applied. This keeps S2S ADDLINE during burst nice and fast,
48 * while at the same time not slowing things the fuck down when we try adding a ban with lots of preexisting
52 bool XLine::Matches(User *u)
58 * Checks what users match a given vector of ELines and sets their ban exempt flag accordingly.
60 void XLineManager::CheckELines()
62 ContainerIter n = lookup_lines.find("E");
64 if (n == lookup_lines.end())
67 XLineLookup& ELines = n->second;
72 for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
74 User* u = (User*)(*u2);
76 /* This uses safe iteration to ensure that if a line expires here, it doenst trash the iterator */
79 for (LookupIter i = ELines.begin(); i != ELines.end(); )
85 u->exempt = e->Matches(u);
93 XLineLookup* XLineManager::GetAll(const std::string &type)
95 ContainerIter n = lookup_lines.find(type);
97 if (n == lookup_lines.end())
101 const time_t current = ServerInstance->Time();
103 /* Expire any dead ones, before sending */
104 for (LookupIter x = n->second.begin(); x != n->second.end(); )
108 if (x->second->duration && current > x->second->expiry)
118 void XLineManager::DelAll(const std::string &type)
120 ContainerIter n = lookup_lines.find(type);
122 if (n == lookup_lines.end())
127 /* Delete all of a given type (this should probably use DelLine, but oh well) */
128 while ((x = n->second.begin()) != n->second.end())
134 std::vector<std::string> XLineManager::GetAllTypes()
136 std::vector<std::string> items;
137 for (ContainerIter x = lookup_lines.begin(); x != lookup_lines.end(); ++x)
138 items.push_back(x->first);
142 IdentHostPair XLineManager::IdentSplit(const std::string &ident_and_host)
144 IdentHostPair n = std::make_pair<std::string,std::string>("*","*");
145 std::string::size_type x = ident_and_host.find('@');
146 if (x != std::string::npos)
148 n.second = ident_and_host.substr(x + 1,ident_and_host.length());
149 n.first = ident_and_host.substr(0, x);
150 if (!n.first.length())
152 if (!n.second.length())
153 n.second.assign("*");
158 n.second = ident_and_host;
166 bool XLineManager::AddLine(XLine* line, User* user)
168 ServerInstance->BanCache->RemoveEntries(line->type, false); // XXX perhaps remove ELines here?
170 if (line->duration && ServerInstance->Time() > line->expiry)
171 return false; // Don't apply expired XLines.
173 /* Don't apply duplicate xlines */
174 ContainerIter x = lookup_lines.find(line->type);
175 if (x != lookup_lines.end())
177 LookupIter i = x->second.find(line->Displayable());
178 if (i != x->second.end())
184 /*ELine* item = new ELine(ServerInstance, ServerInstance->Time(), duration, source, reason, ih.first.c_str(), ih.second.c_str());*/
185 XLineFactory* xlf = GetFactory(line->type);
189 if (xlf->AutoApplyToUserList(line))
190 pending_lines.push_back(line);
192 lookup_lines[line->type][line->Displayable()] = line;
195 FOREACH_MOD(I_OnAddLine,OnAddLine(user, line));
200 // deletes a line, returns true if the line existed and was removed
202 bool XLineManager::DelLine(const char* hostmask, const std::string &type, User* user, bool simulate)
204 ContainerIter x = lookup_lines.find(type);
206 if (x == lookup_lines.end())
209 LookupIter y = x->second.find(hostmask);
211 if (y == x->second.end())
217 ServerInstance->BanCache->RemoveEntries(y->second->type, true);
219 FOREACH_MOD(I_OnDelLine,OnDelLine(user, y->second));
223 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), y->second);
224 if (pptr != pending_lines.end())
225 pending_lines.erase(pptr);
236 /* remove exempt from everyone and force recheck after deleting eline */
237 for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
239 User* u = (User*)(*u2);
243 ServerInstance->XLines->CheckELines();
246 // returns a pointer to the reason if a nickname matches a qline, NULL if it didnt match
248 XLine* XLineManager::MatchesLine(const std::string &type, User* user)
250 ContainerIter x = lookup_lines.find(type);
252 if (x == lookup_lines.end())
255 const time_t current = ServerInstance->Time();
259 for (LookupIter i = x->second.begin(); i != x->second.end(); )
264 if (i->second->duration && current > i->second->expiry)
266 /* Expire the line, proceed to next one */
272 if (i->second->Matches(user))
282 XLine* XLineManager::MatchesLine(const std::string &type, const std::string &pattern)
284 ContainerIter x = lookup_lines.find(type);
286 if (x == lookup_lines.end())
289 const time_t current = ServerInstance->Time();
293 for (LookupIter i = x->second.begin(); i != x->second.end(); )
298 if (i->second->Matches(pattern))
300 if (i->second->duration && current > i->second->expiry)
302 /* Expire the line, return nothing */
317 // removes lines that have expired
318 void XLineManager::ExpireLine(ContainerIter container, LookupIter item)
320 FOREACH_MOD(I_OnExpireLine, OnExpireLine(item->second));
322 item->second->DisplayExpiry();
323 item->second->Unset();
325 /* TODO: Can we skip this loop by having a 'pending' field in the XLine class, which is set when a line
326 * is pending, cleared when it is no longer pending, so we skip over this loop if its not pending?
329 std::vector<XLine*>::iterator pptr = std::find(pending_lines.begin(), pending_lines.end(), item->second);
330 if (pptr != pending_lines.end())
331 pending_lines.erase(pptr);
334 container->second.erase(item);
338 // applies lines, removing clients and changing nicks etc as applicable
339 void XLineManager::ApplyLines()
341 for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
343 User* u = (User*)(*u2);
345 // Don't ban people who are exempt.
349 for (std::vector<XLine *>::iterator i = pending_lines.begin(); i != pending_lines.end(); i++)
357 pending_lines.clear();
360 void XLineManager::InvokeStats(const std::string &type, int numeric, User* user, string_list &results)
362 std::string sn = ServerInstance->Config->ServerName;
364 ContainerIter n = lookup_lines.find(type);
366 time_t current = ServerInstance->Time();
370 if (n != lookup_lines.end())
372 XLineLookup& list = n->second;
373 for (LookupIter i = list.begin(); i != list.end(); )
378 if (i->second->duration && current > i->second->expiry)
383 results.push_back(sn+" "+ConvToStr(numeric)+" "+user->nick+" :"+i->second->Displayable()+" "+
384 ConvToStr(i->second->set_time)+" "+ConvToStr(i->second->duration)+" "+std::string(i->second->source)+" :"+(i->second->reason));
391 XLineManager::XLineManager(InspIRCd* Instance) : ServerInstance(Instance)
393 GFact = new GLineFactory(Instance);
394 EFact = new ELineFactory(Instance);
395 KFact = new KLineFactory(Instance);
396 QFact = new QLineFactory(Instance);
397 ZFact = new ZLineFactory(Instance);
399 RegisterFactory(GFact);
400 RegisterFactory(EFact);
401 RegisterFactory(KFact);
402 RegisterFactory(QFact);
403 RegisterFactory(ZFact);
406 XLineManager::~XLineManager()
408 UnregisterFactory(GFact);
409 UnregisterFactory(EFact);
410 UnregisterFactory(KFact);
411 UnregisterFactory(QFact);
412 UnregisterFactory(ZFact);
420 // Delete all existing XLines
421 for (XLineContainer::iterator i = lookup_lines.begin(); i != lookup_lines.end(); i++)
423 for (XLineLookup::iterator j = i->second.begin(); j != i->second.end(); j++)
429 lookup_lines.clear();
433 void XLine::Apply(User* u)
437 bool XLine::IsBurstable()
442 void XLine::DefaultApply(User* u, const std::string &line, bool bancache)
444 char sreason[MAXBUF];
445 snprintf(sreason, MAXBUF, "%s-Lined: %s", line.c_str(), this->reason.c_str());
446 if (*ServerInstance->Config->MoronBanner)
447 u->WriteServ("NOTICE %s :*** %s", u->nick.c_str(), ServerInstance->Config->MoronBanner);
449 if (ServerInstance->Config->HideBans)
450 ServerInstance->Users->QuitUser(u, line + "-Lined", sreason);
452 ServerInstance->Users->QuitUser(u, sreason);
457 ServerInstance->Logs->Log("BANCACHE", DEBUG, std::string("BanCache: Adding positive hit (") + line + ") for " + u->GetIPString());
458 if (this->duration > 0)
459 ServerInstance->BanCache->AddHit(u->GetIPString(), this->type, line + "-Lined: " + this->reason, this->duration);
461 ServerInstance->BanCache->AddHit(u->GetIPString(), this->type, line + "-Lined: " + this->reason);
465 bool KLine::Matches(User *u)
470 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
472 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
473 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
482 void KLine::Apply(User* u)
484 DefaultApply(u, "K", (this->identmask == "*") ? true : false);
487 bool GLine::Matches(User *u)
492 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
494 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
495 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
504 void GLine::Apply(User* u)
506 DefaultApply(u, "G", (this->identmask == "*") ? true : false);
509 bool ELine::Matches(User *u)
514 if (InspIRCd::Match(u->ident, this->identmask, ascii_case_insensitive_map))
516 if (InspIRCd::MatchCIDR(u->host, this->hostmask, ascii_case_insensitive_map) ||
517 InspIRCd::MatchCIDR(u->GetIPString(), this->hostmask, ascii_case_insensitive_map))
526 bool ZLine::Matches(User *u)
531 if (InspIRCd::MatchCIDR(u->GetIPString(), this->ipaddr))
537 void ZLine::Apply(User* u)
539 DefaultApply(u, "Z", true);
543 bool QLine::Matches(User *u)
545 if (InspIRCd::Match(u->nick, this->nick))
551 void QLine::Apply(User* u)
553 /* Force to uuid on apply of qline, no need to disconnect any more :) */
554 u->ForceNickChange(u->uuid.c_str());
558 bool ZLine::Matches(const std::string &str)
560 if (InspIRCd::MatchCIDR(str, this->ipaddr))
566 bool QLine::Matches(const std::string &str)
568 if (InspIRCd::Match(str, this->nick))
574 bool ELine::Matches(const std::string &str)
576 return (InspIRCd::MatchCIDR(str, matchtext));
579 bool KLine::Matches(const std::string &str)
581 return (InspIRCd::MatchCIDR(str.c_str(), matchtext));
584 bool GLine::Matches(const std::string &str)
586 return (InspIRCd::MatchCIDR(str, matchtext));
591 /* When adding one eline, only check the one eline */
592 for (std::vector<User*>::const_iterator u2 = ServerInstance->Users->local_users.begin(); u2 != ServerInstance->Users->local_users.end(); u2++)
594 User* u = (User*)(*u2);
595 if (this->Matches(u))
600 void ELine::DisplayExpiry()
602 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired E-Line %s@%s (set by %s %ld seconds ago)",
603 identmask.c_str(),hostmask.c_str(),source.c_str(),(long)(ServerInstance->Time() - this->set_time));
606 void QLine::DisplayExpiry()
608 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired Q-Line %s (set by %s %ld seconds ago)",
609 nick.c_str(),source.c_str(),(long)(ServerInstance->Time() - this->set_time));
612 void ZLine::DisplayExpiry()
614 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired Z-Line %s (set by %s %ld seconds ago)",
615 ipaddr.c_str(),source.c_str(),(long)(ServerInstance->Time() - this->set_time));
618 void KLine::DisplayExpiry()
620 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired K-Line %s@%s (set by %s %ld seconds ago)",
621 identmask.c_str(),hostmask.c_str(),source.c_str(),(long)(ServerInstance->Time() - this->set_time));
624 void GLine::DisplayExpiry()
626 ServerInstance->SNO->WriteToSnoMask('x',"Removing expired G-Line %s@%s (set by %s %ld seconds ago)",
627 identmask.c_str(),hostmask.c_str(),source.c_str(),(long)(ServerInstance->Time() - this->set_time));
630 const char* ELine::Displayable()
632 return matchtext.c_str();
635 const char* KLine::Displayable()
637 return matchtext.c_str();
640 const char* GLine::Displayable()
642 return matchtext.c_str();
645 const char* ZLine::Displayable()
647 return ipaddr.c_str();
650 const char* QLine::Displayable()
655 bool KLine::IsBurstable()
660 bool XLineManager::RegisterFactory(XLineFactory* xlf)
662 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
664 if (n != line_factory.end())
667 line_factory[xlf->GetType()] = xlf;
672 bool XLineManager::UnregisterFactory(XLineFactory* xlf)
674 XLineFactMap::iterator n = line_factory.find(xlf->GetType());
676 if (n == line_factory.end())
679 line_factory.erase(n);
684 XLineFactory* XLineManager::GetFactory(const std::string &type)
686 XLineFactMap::iterator n = line_factory.find(type);
688 if (n == line_factory.end())