From 484160eb4ed560eeda97c94a42dd8e31431ab251 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Wed, 24 Jan 2024 12:34:03 +0000 Subject: Shuffle modules around a bit. --- modules/operserv/operserv.cpp | 300 ++++++++++++ modules/operserv/os_akill.cpp | 487 ++++++++++++++++++++ modules/operserv/os_chankill.cpp | 119 +++++ modules/operserv/os_config.cpp | 149 ++++++ modules/operserv/os_defcon.cpp | 603 ++++++++++++++++++++++++ modules/operserv/os_dns.cpp | 944 ++++++++++++++++++++++++++++++++++++++ modules/operserv/os_forbid.cpp | 573 +++++++++++++++++++++++ modules/operserv/os_ignore.cpp | 421 +++++++++++++++++ modules/operserv/os_info.cpp | 291 ++++++++++++ modules/operserv/os_jupe.cpp | 82 ++++ modules/operserv/os_kick.cpp | 86 ++++ modules/operserv/os_kill.cpp | 69 +++ modules/operserv/os_list.cpp | 289 ++++++++++++ modules/operserv/os_login.cpp | 135 ++++++ modules/operserv/os_logsearch.cpp | 187 ++++++++ modules/operserv/os_mode.cpp | 192 ++++++++ modules/operserv/os_modinfo.cpp | 211 +++++++++ modules/operserv/os_module.cpp | 192 ++++++++ modules/operserv/os_news.cpp | 478 +++++++++++++++++++ modules/operserv/os_noop.cpp | 100 ++++ modules/operserv/os_oper.cpp | 292 ++++++++++++ modules/operserv/os_reload.cpp | 69 +++ modules/operserv/os_session.cpp | 738 +++++++++++++++++++++++++++++ modules/operserv/os_set.cpp | 269 +++++++++++ modules/operserv/os_shutdown.cpp | 112 +++++ modules/operserv/os_stats.cpp | 281 ++++++++++++ modules/operserv/os_svs.cpp | 182 ++++++++ modules/operserv/os_sxline.cpp | 731 +++++++++++++++++++++++++++++ modules/operserv/os_update.cpp | 54 +++ 29 files changed, 8636 insertions(+) create mode 100644 modules/operserv/operserv.cpp create mode 100644 modules/operserv/os_akill.cpp create mode 100644 modules/operserv/os_chankill.cpp create mode 100644 modules/operserv/os_config.cpp create mode 100644 modules/operserv/os_defcon.cpp create mode 100644 modules/operserv/os_dns.cpp create mode 100644 modules/operserv/os_forbid.cpp create mode 100644 modules/operserv/os_ignore.cpp create mode 100644 modules/operserv/os_info.cpp create mode 100644 modules/operserv/os_jupe.cpp create mode 100644 modules/operserv/os_kick.cpp create mode 100644 modules/operserv/os_kill.cpp create mode 100644 modules/operserv/os_list.cpp create mode 100644 modules/operserv/os_login.cpp create mode 100644 modules/operserv/os_logsearch.cpp create mode 100644 modules/operserv/os_mode.cpp create mode 100644 modules/operserv/os_modinfo.cpp create mode 100644 modules/operserv/os_module.cpp create mode 100644 modules/operserv/os_news.cpp create mode 100644 modules/operserv/os_noop.cpp create mode 100644 modules/operserv/os_oper.cpp create mode 100644 modules/operserv/os_reload.cpp create mode 100644 modules/operserv/os_session.cpp create mode 100644 modules/operserv/os_set.cpp create mode 100644 modules/operserv/os_shutdown.cpp create mode 100644 modules/operserv/os_stats.cpp create mode 100644 modules/operserv/os_svs.cpp create mode 100644 modules/operserv/os_sxline.cpp create mode 100644 modules/operserv/os_update.cpp (limited to 'modules/operserv') diff --git a/modules/operserv/operserv.cpp b/modules/operserv/operserv.cpp new file mode 100644 index 000000000..2bda06206 --- /dev/null +++ b/modules/operserv/operserv.cpp @@ -0,0 +1,300 @@ +/* OperServ core functions + * + * (C) 2003-2024 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + +#include "module.h" + +class SGLineManager final + : public XLineManager +{ +public: + SGLineManager(Module *creator) : XLineManager(creator, "xlinemanager/sgline", 'G') { } + + void OnMatch(User *u, XLine *x) override + { + this->Send(u, x); + } + + void OnExpire(const XLine *x) override + { + Log(Config->GetClient("OperServ"), "expire/akill") << "AKILL on \002" << x->mask << "\002 has expired"; + } + + void Send(User *u, XLine *x) override + { + IRCD->SendAkill(u, x); + } + + void SendDel(XLine *x) override + { + IRCD->SendAkillDel(x); + } + + bool Check(User *u, const XLine *x) override + { + if (x->regex) + { + Anope::string uh = u->GetIdent() + "@" + u->host, nuhr = u->nick + "!" + uh + "#" + u->realname; + return x->regex->Matches(uh) || x->regex->Matches(nuhr); + } + + if (!x->GetNick().empty() && !Anope::Match(u->nick, x->GetNick())) + return false; + + if (!x->GetUser().empty() && !Anope::Match(u->GetIdent(), x->GetUser())) + return false; + + if (!x->GetReal().empty() && !Anope::Match(u->realname, x->GetReal())) + return false; + + if (x->c && x->c->match(u->ip)) + return true; + + if (x->GetHost().empty() || Anope::Match(u->host, x->GetHost()) || Anope::Match(u->ip.addr(), x->GetHost())) + return true; + + return false; + } +}; + +class SQLineManager final + : public XLineManager +{ + ServiceReference nickserv; + +public: + SQLineManager(Module *creator) : XLineManager(creator, "xlinemanager/sqline", 'Q'), nickserv("NickServService", "NickServ") { } + + void OnMatch(User *u, XLine *x) override + { + this->Send(u, x); + } + + void OnExpire(const XLine *x) override + { + Log(Config->GetClient("OperServ"), "expire/sqline") << "SQLINE on \002" << x->mask << "\002 has expired"; + } + + void Send(User *u, XLine *x) override + { + if (!IRCD->CanSQLine) + { + if (!u) + ; + else if (nickserv) + nickserv->Collide(u, NULL); + else + u->Kill(Config->GetClient("OperServ"), "Q-Lined: " + x->reason); + } + else if (x->IsRegex()) + { + if (u) + u->Kill(Config->GetClient("OperServ"), "Q-Lined: " + x->reason); + } + else if (x->mask[0] != '#' || IRCD->CanSQLineChannel) + { + IRCD->SendSQLine(u, x); + /* If it is an oper, assume they're walking it, otherwise kill for good measure */ + if (u && !u->HasMode("OPER")) + u->Kill(Config->GetClient("OperServ"), "Q-Lined: " + x->reason); + } + } + + void SendDel(XLine *x) override + { + if (!IRCD->CanSQLine || x->IsRegex()) + ; + else if (x->mask[0] != '#' || IRCD->CanSQLineChannel) + IRCD->SendSQLineDel(x); + } + + bool Check(User *u, const XLine *x) override + { + if (x->regex) + return x->regex->Matches(u->nick); + return Anope::Match(u->nick, x->mask); + } + + XLine *CheckChannel(Channel *c) + { + for (auto *x : this->GetList()) + { + if (x->regex) + { + if (x->regex->Matches(c->name)) + return x; + } + else + { + if (x->mask.empty() || x->mask[0] != '#') + continue; + + if (Anope::Match(c->name, x->mask, false, true)) + return x; + } + } + return NULL; + } +}; + +class SNLineManager final + : public XLineManager +{ +public: + SNLineManager(Module *creator) : XLineManager(creator, "xlinemanager/snline", 'N') { } + + void OnMatch(User *u, XLine *x) override + { + this->Send(u, x); + } + + void OnExpire(const XLine *x) override + { + Log(Config->GetClient("OperServ"), "expire/snline") << "SNLINE on \002" << x->mask << "\002 has expired"; + } + + void Send(User *u, XLine *x) override + { + if (IRCD->CanSNLine && !x->IsRegex()) + IRCD->SendSGLine(u, x); + + if (u) + u->Kill(Config->GetClient("OperServ"), "SNLined: " + x->reason); + } + + void SendDel(XLine *x) override + { + if (IRCD->CanSNLine && !x->IsRegex()) + IRCD->SendSGLineDel(x); + } + + bool Check(User *u, const XLine *x) override + { + if (x->regex) + return x->regex->Matches(u->realname); + return Anope::Match(u->realname, x->mask, false, true); + } +}; + +class OperServCore final + : public Module +{ + Reference OperServ; + SGLineManager sglines; + SQLineManager sqlines; + SNLineManager snlines; + +public: + OperServCore(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, PSEUDOCLIENT | VENDOR), + sglines(this), sqlines(this), snlines(this) + { + + /* Yes, these are in this order for a reason. Most violent->least violent. */ + XLineManager::RegisterXLineManager(&sglines); + XLineManager::RegisterXLineManager(&sqlines); + XLineManager::RegisterXLineManager(&snlines); + } + + ~OperServCore() override + { + this->sglines.Clear(); + this->sqlines.Clear(); + this->snlines.Clear(); + + XLineManager::UnregisterXLineManager(&sglines); + XLineManager::UnregisterXLineManager(&sqlines); + XLineManager::UnregisterXLineManager(&snlines); + } + + void OnReload(Configuration::Conf *conf) override + { + const Anope::string &osnick = conf->GetModule(this)->Get("client"); + + if (osnick.empty()) + throw ConfigException(this->name + ": must be defined"); + + BotInfo *bi = BotInfo::Find(osnick, true); + if (!bi) + throw ConfigException(this->name + ": no bot named " + osnick); + + OperServ = bi; + } + + EventReturn OnBotPrivmsg(User *u, BotInfo *bi, Anope::string &message) override + { + if (bi == OperServ && !u->HasMode("OPER") && Config->GetModule(this)->Get("opersonly")) + { + u->SendMessage(bi, ACCESS_DENIED); + Log(bi, "bados") << "Denied access to " << bi->nick << " from " << u->GetMask() << " (non-oper)"; + return EVENT_STOP; + } + + return EVENT_CONTINUE; + } + + void OnServerQuit(Server *server) override + { + if (server->IsJuped()) + Log(server, "squit", OperServ) << "Received SQUIT for juped server " << server->GetName(); + } + + void OnUserModeSet(const MessageSource &setter, User *u, const Anope::string &mname) override + { + if (mname == "OPER") + Log(u, "oper", OperServ) << "is now an IRC operator."; + } + + void OnUserModeUnset(const MessageSource &setter, User *u, const Anope::string &mname) override + { + if (mname == "OPER") + Log(u, "oper", OperServ) << "is no longer an IRC operator"; + } + + void OnUserConnect(User *u, bool &exempt) override + { + if (!u->Quitting() && !exempt) + XLineManager::CheckAll(u); + } + + void OnUserNickChange(User *u, const Anope::string &oldnick) override + { + if (!u->HasMode("OPER")) + this->sqlines.CheckAllXLines(u); + } + + EventReturn OnCheckKick(User *u, Channel *c, Anope::string &mask, Anope::string &reason) override + { + XLine *x = this->sqlines.CheckChannel(c); + if (x) + { + this->sqlines.OnMatch(u, x); + reason = x->reason; + return EVENT_STOP; + } + + return EVENT_CONTINUE; + } + + EventReturn OnPreHelp(CommandSource &source, const std::vector ¶ms) override + { + if (!params.empty() || source.c || source.service != *OperServ) + return EVENT_CONTINUE; + source.Reply(_("%s commands:"), OperServ->nick.c_str()); + return EVENT_CONTINUE; + } + + void OnLog(Log *l) override + { + if (l->type == LOG_SERVER) + l->bi = OperServ; + } +}; + +MODULE_INIT(OperServCore) diff --git a/modules/operserv/os_akill.cpp b/modules/operserv/os_akill.cpp new file mode 100644 index 000000000..3f5e9c7d2 --- /dev/null +++ b/modules/operserv/os_akill.cpp @@ -0,0 +1,487 @@ +/* OperServ core functions + * + * (C) 2003-2024 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + +#include "module.h" + +static ServiceReference akills("XLineManager", "xlinemanager/sgline"); + +class AkillDelCallback final + : public NumberList +{ + CommandSource &source; + unsigned deleted = 0; + Command *cmd; +public: + AkillDelCallback(CommandSource &_source, const Anope::string &numlist, Command *c) : NumberList(numlist, true), source(_source), cmd(c) + { + } + + ~AkillDelCallback() override + { + if (!deleted) + source.Reply(_("No matching entries on the AKILL list.")); + else if (deleted == 1) + source.Reply(_("Deleted 1 entry from the AKILL list.")); + else + source.Reply(_("Deleted %d entries from the AKILL list."), deleted); + } + + void HandleNumber(unsigned number) override + { + if (!number) + return; + + XLine *x = akills->GetEntry(number - 1); + + if (!x) + return; + + Log(LOG_ADMIN, source, cmd) << "to remove " << x->mask << " from the list"; + + ++deleted; + DoDel(source, x); + } + + static void DoDel(CommandSource &source, XLine *x) + { + akills->DelXLine(x); + } +}; + +class CommandOSAKill final + : public Command +{ +private: + void DoAdd(CommandSource &source, const std::vector ¶ms) + { + Anope::string expiry, mask; + + if (params.size() < 2) + { + this->OnSyntaxError(source, "ADD"); + return; + } + + spacesepstream sep(params[1]); + sep.GetToken(mask); + + if (mask[0] == '+') + { + expiry = mask; + sep.GetToken(mask); + } + + time_t expires = !expiry.empty() ? Anope::DoTime(expiry) : Config->GetModule("operserv")->Get("autokillexpiry", "30d"); + /* If the expiry given does not contain a final letter, it's in days, + * said the doc. Ah well. + */ + if (!expiry.empty() && isdigit(expiry[expiry.length() - 1])) + expires *= 86400; + /* Do not allow less than a minute expiry time */ + if (expires && expires < 60) + { + source.Reply(BAD_EXPIRY_TIME); + return; + } + else if (expires > 0) + expires += Anope::CurTime; + + if (sep.StreamEnd()) + { + this->OnSyntaxError(source, "ADD"); + return; + } + + Anope::string reason; + if (mask.find('#') != Anope::string::npos) + { + Anope::string remaining = sep.GetRemaining(); + + size_t co = remaining[0] == ':' ? 0 : remaining.rfind(" :"); + if (co == Anope::string::npos) + { + this->OnSyntaxError(source, "ADD"); + return; + } + + if (co != 0) + ++co; + + reason = remaining.substr(co + 1); + mask += " " + remaining.substr(0, co); + mask.trim(); + } + else + reason = sep.GetRemaining(); + + if (mask[0] == '/' && mask[mask.length() - 1] == '/') + { + const Anope::string ®exengine = Config->GetBlock("options")->Get("regexengine"); + + if (regexengine.empty()) + { + source.Reply(_("Regex is disabled.")); + return; + } + + ServiceReference provider("Regex", regexengine); + if (!provider) + { + source.Reply(_("Unable to find regex engine %s."), regexengine.c_str()); + return; + } + + try + { + Anope::string stripped_mask = mask.substr(1, mask.length() - 2); + delete provider->Compile(stripped_mask); + } + catch (const RegexException &ex) + { + source.Reply("%s", ex.GetReason().c_str()); + return; + } + } + + User *targ = User::Find(mask, true); + if (targ) + mask = "*@" + targ->host; + + if (Config->GetModule("operserv")->Get("addakiller", "yes") && !source.GetNick().empty()) + reason = "[" + source.GetNick() + "] " + reason; + + if (mask.find_first_not_of("/~@.*?") == Anope::string::npos) + { + source.Reply(USERHOST_MASK_TOO_WIDE, mask.c_str()); + return; + } + else if (mask.find('@') == Anope::string::npos) + { + source.Reply(BAD_USERHOST_MASK); + return; + } + + auto *x = new XLine(mask, source.GetNick(), expires, reason); + if (Config->GetModule("operserv")->Get("akillids")) + x->id = XLineManager::GenerateUID(); + + unsigned int affected = 0; + for (const auto &[_, user] : UserListByNick) + if (akills->Check(user, x)) + ++affected; + float percent = static_cast(affected) / static_cast(UserListByNick.size()) * 100.0; + + if (percent > 95) + { + source.Reply(USERHOST_MASK_TOO_WIDE, mask.c_str()); + Log(LOG_ADMIN, source, this) << "tried to akill " << percent << "% of the network (" << affected << " users)"; + delete x; + return; + } + + if (!akills->CanAdd(source, mask, expires, reason)) + return; + + EventReturn MOD_RESULT; + FOREACH_RESULT(OnAddXLine, MOD_RESULT, (source, x, akills)); + if (MOD_RESULT == EVENT_STOP) + { + delete x; + return; + } + + akills->AddXLine(x); + if (Config->GetModule("operserv")->Get("akillonadd")) + akills->Send(NULL, x); + + source.Reply(_("\002%s\002 added to the AKILL list."), mask.c_str()); + + Log(LOG_ADMIN, source, this) << "on " << mask << " (" << x->reason << "), expires in " << (expires ? Anope::Duration(expires - Anope::CurTime) : "never") << " [affects " << affected << " user(s) (" << percent << "%)]"; + if (Anope::ReadOnly) + source.Reply(READ_ONLY_MODE); + } + + void DoDel(CommandSource &source, const std::vector ¶ms) + { + const Anope::string &mask = params.size() > 1 ? params[1] : ""; + + if (mask.empty()) + { + this->OnSyntaxError(source, "DEL"); + return; + } + + if (akills->GetList().empty()) + { + source.Reply(_("AKILL list is empty.")); + return; + } + + if (isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos) + { + AkillDelCallback list(source, mask, this); + list.Process(); + } + else + { + XLine *x = akills->HasEntry(mask); + + if (!x) + { + source.Reply(_("\002%s\002 not found on the AKILL list."), mask.c_str()); + return; + } + + do + { + FOREACH_MOD(OnDelXLine, (source, x, akills)); + + Log(LOG_ADMIN, source, this) << "to remove " << x->mask << " from the list"; + source.Reply(_("\002%s\002 deleted from the AKILL list."), x->mask.c_str()); + AkillDelCallback::DoDel(source, x); + } + while ((x = akills->HasEntry(mask))); + + } + + if (Anope::ReadOnly) + source.Reply(READ_ONLY_MODE); + + return; + } + + void ProcessList(CommandSource &source, const std::vector ¶ms, ListFormatter &list) + { + const Anope::string &mask = params.size() > 1 ? params[1] : ""; + + if (!mask.empty() && isdigit(mask[0]) && mask.find_first_not_of("1234567890,-") == Anope::string::npos) + { + class ListCallback final + : public NumberList + { + CommandSource &source; + ListFormatter &list; + public: + ListCallback(CommandSource &_source, ListFormatter &_list, const Anope::string &numstr) : NumberList(numstr, false), source(_source), list(_list) + { + } + + void HandleNumber(unsigned number) override + { + if (!number) + return; + + const XLine *x = akills->GetEntry(number - 1); + + if (!x) + return; + + ListFormatter::ListEntry entry; + entry["Number"] = stringify(number); + entry["Mask"] = x->mask; + entry["Creator"] = x->by; + entry["Created"] = Anope::strftime(x->created, NULL, true); + entry["Expires"] = Anope::Expires(x->expires, source.nc); + entry["ID"] = x->id; + entry["Reason"] = x->reason; + this->list.AddEntry(entry); + } + } + nl_list(source, list, mask); + nl_list.Process(); + } + else + { + for (unsigned i = 0, end = akills->GetCount(); i < end; ++i) + { + const XLine *x = akills->GetEntry(i); + + if (mask.empty() || mask.equals_ci(x->mask) || mask == x->id || Anope::Match(x->mask, mask, false, true)) + { + ListFormatter::ListEntry entry; + entry["Number"] = stringify(i + 1); + entry["Mask"] = x->mask; + entry["Creator"] = x->by; + entry["Created"] = Anope::strftime(x->created, NULL, true); + entry["Expires"] = Anope::Expires(x->expires, source.nc); + entry["ID"] = x->id; + entry["Reason"] = x->reason; + list.AddEntry(entry); + } + } + } + + if (list.IsEmpty()) + source.Reply(_("No matching entries on the AKILL list.")); + else + { + source.Reply(_("Current AKILL list:")); + + std::vector replies; + list.Process(replies); + + for (const auto &reply : replies) + source.Reply(reply); + + source.Reply(_("End of AKILL list.")); + } + } + + void DoList(CommandSource &source, const std::vector ¶ms) + { + if (akills->GetList().empty()) + { + source.Reply(_("AKILL list is empty.")); + return; + } + + ListFormatter list(source.GetAccount()); + list.AddColumn(_("Number")).AddColumn(_("Mask")).AddColumn(_("Reason")); + + this->ProcessList(source, params, list); + } + + void DoView(CommandSource &source, const std::vector ¶ms) + { + if (akills->GetList().empty()) + { + source.Reply(_("AKILL list is empty.")); + return; + } + + ListFormatter list(source.GetAccount()); + list.AddColumn(_("Number")).AddColumn(_("Mask")).AddColumn(_("Creator")).AddColumn(_("Created")).AddColumn(_("Expires")); + if (Config->GetModule("operserv")->Get("akillids")) + list.AddColumn(_("ID")); + list.AddColumn(_("Reason")); + + this->ProcessList(source, params, list); + } + + void DoClear(CommandSource &source) + { + + for (unsigned i = akills->GetCount(); i > 0; --i) + { + XLine *x = akills->GetEntry(i - 1); + FOREACH_MOD(OnDelXLine, (source, x, akills)); + akills->DelXLine(x); + } + + Log(LOG_ADMIN, source, this) << "to CLEAR the list"; + source.Reply(_("The AKILL list has been cleared.")); + + if (Anope::ReadOnly) + source.Reply(READ_ONLY_MODE); + } +public: + CommandOSAKill(Module *creator) : Command(creator, "operserv/akill", 1, 2) + { + this->SetDesc(_("Manipulate the AKILL list")); + this->SetSyntax(_("ADD [+\037expiry\037] \037mask\037 \037reason\037")); + this->SetSyntax(_("DEL {\037mask\037 | \037entry-num\037 | \037list\037 | \037id\037}")); + this->SetSyntax(_("LIST [\037mask\037 | \037list\037 | \037id\037]")); + this->SetSyntax(_("VIEW [\037mask\037 | \037list\037 | \037id\037]")); + this->SetSyntax("CLEAR"); + } + + void Execute(CommandSource &source, const std::vector ¶ms) override + { + const Anope::string &cmd = params[0]; + + if (!akills) + return; + + if (cmd.equals_ci("ADD")) + return this->DoAdd(source, params); + else if (cmd.equals_ci("DEL")) + return this->DoDel(source, params); + else if (cmd.equals_ci("LIST")) + return this->DoList(source, params); + else if (cmd.equals_ci("VIEW")) + return this->DoView(source, params); + else if (cmd.equals_ci("CLEAR")) + return this->DoClear(source); + else + this->OnSyntaxError(source, ""); + + return; + } + + bool OnHelp(CommandSource &source, const Anope::string &subcommand) override + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("Allows Services Operators to manipulate the AKILL list. If\n" + "a user matching an AKILL mask attempts to connect, services\n" + "will issue a KILL for that user and, on supported server\n" + "types, will instruct all servers to add a ban for the mask\n" + "which the user matched.\n" + " \n" + "\002AKILL ADD\002 adds the given mask to the AKILL\n" + "list for the given reason, which \002must\002 be given.\n" + "Mask should be in the format of nick!user@host#real name,\n" + "though all that is required is user@host. If a real name is specified,\n" + "the reason must be prepended with a :.\n" + "\037expiry\037 is specified as an integer followed by one of \037d\037\n" + "(days), \037h\037 (hours), or \037m\037 (minutes). Combinations (such as\n" + "\0371h30m\037) are not permitted. If a unit specifier is not\n" + "included, the default is days (so \037+30\037 by itself means 30\n" + "days). To add an AKILL which does not expire, use \037+0\037. If the\n" + "usermask to be added starts with a \037+\037, an expiry time must\n" + "be given, even if it is the same as the default. The\n" + "current AKILL default expiry time can be found with the\n" + "\002STATS AKILL\002 command.")); + const Anope::string ®exengine = Config->GetBlock("options")->Get("regexengine"); + if (!regexengine.empty()) + { + source.Reply(" "); + source.Reply(_("Regex matches are also supported using the %s engine.\n" + "Enclose your mask in // if this is desired."), regexengine.c_str()); + } + source.Reply(_( + " \n" + "The \002AKILL DEL\002 command removes the given mask from the\n" + "AKILL list if it is present. If a list of entry numbers is\n" + "given, those entries are deleted. (See the example for LIST\n" + "below.)\n" + " \n" + "The \002AKILL LIST\002 command displays the AKILL list.\n" + "If a wildcard mask is given, only those entries matching the\n" + "mask are displayed. If a list of entry numbers is given,\n" + "only those entries are shown; for example:\n" + " \002AKILL LIST 2-5,7-9\002\n" + " Lists AKILL entries numbered 2 through 5 and 7\n" + " through 9.\n" + " \n" + "\002AKILL VIEW\002 is a more verbose version of \002AKILL LIST\002, and\n" + "will show who added an AKILL, the date it was added, and when\n" + "it expires, as well as the user@host/ip mask and reason.\n" + " \n" + "\002AKILL CLEAR\002 clears all entries of the AKILL list.")); + return true; + } +}; + +class OSAKill final + : public Module +{ + CommandOSAKill commandosakill; + +public: + OSAKill(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), + commandosakill(this) + { + + } +}; + +MODULE_INIT(OSAKill) diff --git a/modules/operserv/os_chankill.cpp b/modules/operserv/os_chankill.cpp new file mode 100644 index 000000000..311e8f3d1 --- /dev/null +++ b/modules/operserv/os_chankill.cpp @@ -0,0 +1,119 @@ +/* OperServ core functions + * + * (C) 2003-2024 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + +#include "module.h" + +static ServiceReference akills("XLineManager", "xlinemanager/sgline"); + +class CommandOSChanKill final + : public Command +{ +public: + CommandOSChanKill(Module *creator) : Command(creator, "operserv/chankill", 2, 3) + { + this->SetDesc(_("AKILL all users on a specific channel")); + this->SetSyntax(_("[+\037expiry\037] \037channel\037 \037reason\037")); + } + + void Execute(CommandSource &source, const std::vector ¶ms) override + { + if (!akills) + return; + + Anope::string expiry, channel; + unsigned last_param = 1; + Channel *c; + + channel = params[0]; + if (!channel.empty() && channel[0] == '+') + { + expiry = channel; + channel = params[1]; + last_param = 2; + } + + time_t expires = !expiry.empty() ? Anope::DoTime(expiry) : Config->GetModule("operserv")->Get("autokillexpiry", "30d"); + if (!expiry.empty() && isdigit(expiry[expiry.length() - 1])) + expires *= 86400; + if (expires && expires < 60) + { + source.Reply(BAD_EXPIRY_TIME); + return; + } + else if (expires > 0) + expires += Anope::CurTime; + + if (params.size() <= last_param) + { + this->OnSyntaxError(source, ""); + return; + } + + Anope::string reason = params[last_param]; + if (params.size() > last_param + 1) + reason += params[last_param + 1]; + if (!reason.empty()) + { + Anope::string realreason; + if (Config->GetModule("operserv")->Get("addakiller") && !source.GetNick().empty()) + realreason = "[" + source.GetNick() + "] " + reason; + else + realreason = reason; + + if ((c = Channel::Find(channel))) + { + for (const auto &[_, uc] : c->users) + { + if (uc->user->server == Me || uc->user->HasMode("OPER")) + continue; + + Anope::string akillmask = "*@" + uc->user->host; + if (akills->HasEntry(akillmask)) + continue; + + auto *x = new XLine(akillmask, source.GetNick(), expires, realreason, XLineManager::GenerateUID()); + akills->AddXLine(x); + akills->OnMatch(uc->user, x); + } + + Log(LOG_ADMIN, source, this) << "on " << c->name << " (" << realreason << ")"; + } + else + source.Reply(CHAN_X_NOT_IN_USE, channel.c_str()); + } + return; + } + + bool OnHelp(CommandSource &source, const Anope::string &subcommand) override + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("Puts an AKILL for every nick on the specified channel. It\n" + "uses the entire real ident@host for every nick, and\n" + "then enforces the AKILL.")); + return true; + } +}; + +class OSChanKill final + : public Module +{ + CommandOSChanKill commandoschankill; + +public: + OSChanKill(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), + commandoschankill(this) + { + + } +}; + +MODULE_INIT(OSChanKill) diff --git a/modules/operserv/os_config.cpp b/modules/operserv/os_config.cpp new file mode 100644 index 000000000..2a9ede9e7 --- /dev/null +++ b/modules/operserv/os_config.cpp @@ -0,0 +1,149 @@ +/* OperServ core functions + * + * (C) 2003-2024 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + +#include "module.h" + +class CommandOSConfig final + : public Command +{ +public: + CommandOSConfig(Module *creator) : Command(creator, "operserv/config", 1, 4) + { + this->SetDesc(_("View and change configuration file settings")); + this->SetSyntax(_("{\037MODIFY\037|\037VIEW\037} [\037block name\037 \037item name\037 \037item value\037]")); + } + + void Execute(CommandSource &source, const std::vector ¶ms) override + { + const Anope::string &what = params[0]; + + if (what.equals_ci("MODIFY") && params.size() > 3) + { + if (!source.HasPriv("operserv/config")) + { + source.Reply(ACCESS_DENIED); + return; + } + + Configuration::MutableBlock *block = Config->GetMutableBlock(params[1]); + if (!block) + block = Config->GetModule(params[1]); + + if (!block) + { + source.Reply(_("There is no such configuration block %s."), params[1].c_str()); + return; + } + + block->Set(params[2], params[3]); + + Log(LOG_ADMIN, source, this) << "to change the configuration value of " << params[1] << ":" << params[2] << " to " << params[3]; + source.Reply(_("Value of %s:%s changed to %s"), params[1].c_str(), params[2].c_str(), params[3].c_str()); + } + else if (what.equals_ci("VIEW")) + { + /* Blocks we should show */ + const Anope::string show_blocks[] = { "serverinfo", "networkinfo", "options", "" }; + + Log(LOG_ADMIN, source, this) << "VIEW"; + + for (unsigned i = 0; !show_blocks[i].empty(); ++i) + { + Configuration::Block *block = Config->GetBlock(show_blocks[i]); + const Configuration::Block::item_map &items = block->GetItems(); + + ListFormatter lflist(source.GetAccount()); + lflist.AddColumn(_("Name")).AddColumn(_("Value")); + + for (const auto &[name, value] : items) + { + ListFormatter::ListEntry entry; + entry["Name"] = name; + entry["Value"] = value; + lflist.AddEntry(entry); + } + + std::vector replies; + lflist.Process(replies); + + source.Reply(_("%s settings:"), block->GetName().c_str()); + + for (const auto &reply : replies) + source.Reply(reply); + + source.Reply(" "); + } + + ListFormatter lflist(source.GetAccount()); + lflist.AddColumn(_("Module Name")).AddColumn(_("Name")).AddColumn(_("Value")); + + for (int i = 0; i < Config->CountBlock("module"); ++i) + { + Configuration::Block *block = Config->GetBlock("module", i); + const Configuration::Block::item_map &items = block->GetItems(); + + if (items.size() <= 1) + continue; + + ListFormatter::ListEntry entry; + entry["Module Name"] = block->Get("name"); + + for (const auto &[name, value] : items) + { + entry["Name"] = name; + entry["Value"] = value; + lflist.AddEntry(entry); + } + } + + std::vector replies; + lflist.Process(replies); + + source.Reply(_("Module settings:")); + + for (const auto &reply : replies) + source.Reply(reply); + + source.Reply(_("End of configuration.")); + } + else + this->OnSyntaxError(source, what); + } + + bool OnHelp(CommandSource &source, const Anope::string &subcommand) override + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("Allows you to change and view configuration settings.\n" + "Settings changed by this command are temporary and will not be reflected\n" + "back into the configuration file, and will be lost if Anope is shut down,\n" + "restarted, or the configuration is reloaded.\n" + " \n" + "Example:\n" + " \002MODIFY nickserv forcemail no\002")); + return true; + } +}; + +class OSConfig final + : public Module +{ + CommandOSConfig commandosconfig; + +public: + OSConfig(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), + commandosconfig(this) + { + + } +}; + +MODULE_INIT(OSConfig) diff --git a/modules/operserv/os_defcon.cpp b/modules/operserv/os_defcon.cpp new file mode 100644 index 000000000..5664f5ffb --- /dev/null +++ b/modules/operserv/os_defcon.cpp @@ -0,0 +1,603 @@ +/* OperServ core functions + * + * (C) 2003-2024 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + */ + +#include "module.h" +#include "modules/os_session.h" + +enum DefconLevel +{ + DEFCON_NO_NEW_CHANNELS, + DEFCON_NO_NEW_NICKS, + DEFCON_NO_MLOCK_CHANGE, + DEFCON_FORCE_CHAN_MODES, + DEFCON_REDUCE_SESSION, + DEFCON_NO_NEW_CLIENTS, + DEFCON_OPER_ONLY, + DEFCON_SILENT_OPER_ONLY, + DEFCON_AKILL_NEW_CLIENTS, + DEFCON_NO_NEW_MEMOS +}; + +bool DefConModesSet = false; + +struct DefconConfig final +{ + std::vector > DefCon; + std::set DefConModesOn, DefConModesOff; + std::map DefConModesOnParams; + + int defaultlevel, sessionlimit; + Anope::string chanmodes, message, offmessage, akillreason; + std::vector defcons; + time_t akillexpire, timeout; + bool globalondefcon; + + unsigned max_session_kill; + time_t session_autokill_expiry; + Anope::string sle_reason, sle_detailsloc; + + DefconConfig() + { + this->DefCon.resize(6); + this->defcons.resize(5); + } + + bool Check(DefconLevel level) + { + return this->Check(this->defaultlevel, level); + } + + bool Check(int dlevel, DefconLevel level) + { + return this->DefCon[dlevel].test(level); + } + + void Add(int dlevel, DefconLevel level) + { + this->DefCon[dlevel][level] = true; + } + + void Del(int dlevel, DefconLevel level) + { + this->DefCon[dlevel][level] = false; + } + + bool SetDefConParam(const Anope::string &name, const Anope::string &buf) + { + return DefConModesOnParams.emplace(name, buf).second; + } + + void UnsetDefConParam(const Anope::string &name) + { + DefConModesOnParams.erase(name); + } + + bool GetDefConParam(const Anope::string &name, Anope::string &buf) + { + std::map::iterator it = DefConModesOnParams.find(name); + + buf.clear(); + + if (it != DefConModesOnParams.end()) + { + buf = it->second; + return true; + } + + return false; + } +}; + +static DefconConfig DConfig; + +static void runDefCon(); +static Anope::string defconReverseModes(const Anope::string &modes); + +static ServiceReference GlobalService("GlobalService", "Global"); + +static Timer *timeout; + +class DefConTimeout final + : public Timer +{ + int level; + +public: + DefConTimeout(Module *mod, int newlevel) : Timer(mod, DConfig.timeout), level(newlevel) + { + timeout = this; + } + + ~DefConTimeout() override + { + timeout = NULL; + } + + void Tick(time_t) override + { + if (DConfig.defaultlevel != level) + { + DConfig.defaultlevel = level; + FOREACH_MOD(OnDefconLevel, (level)); + Log(Config->GetClient("OperServ"), "operserv/defcon") << "Defcon level timeout, returning to level " << level; + + if (DConfig.globalondefcon) + { + if (!DConfig.offmessage.empty()) + GlobalService->SendGlobal(NULL, "", DConfig.offmessage); + else + GlobalService->SendGlobal(NULL, "", Anope::printf(Language::Translate(_("The Defcon level is now at: \002%d\002")), DConfig.defaultlevel)); + + if (!DConfig.message.empty()) + GlobalService->SendGlobal(NULL, "", DConfig.message); + } + + runDefCon(); + } + } +}; + +class CommandOSDefcon final + : public Command +{ + void SendLevels(CommandSource &source) + { + if (DConfig.Check(DEFCON_NO_NEW_CHANNELS)) + source.Reply(_("* No new channel registrations")); + if (DConfig.Check(DEFCON_NO_NEW_NICKS)) + source.Reply(_("* No new nick registrations")); + if (DConfig.Check(DEFCON_NO_MLOCK_CHANGE)) + source.Reply(_("* No mode lock changes")); + if (DConfig.Check(DEFCON_FORCE_CHAN_MODES) && !DConfig.chanmodes.empty()) + source.Reply(_("* Force channel modes (%s) to be set on all channels"), DConfig.chanmodes.c_str()); + if (DConfig.Check(DEFCON_REDUCE_SESSION)) + source.Reply(_("* Use the reduced session limit of %d"), DConfig.sessionlimit); + if (DConfig.Check(DEFCON_NO_NEW_CLIENTS)) + source.Reply(_("* Kill any new clients connecting")); + if (DConfig.Check(DEFCON_OPER_ONLY)) + source.Reply(_("* Ignore non-opers with a message")); + if (DConfig.Check(DEFCON_SILENT_OPER_ONLY)) + source.Reply(_("* Silently ignore non-opers")); + if (DConfig.Check(DEFCON_AKILL_NEW_CLIENTS)) + source.Reply(_("* AKILL any new clients connecting")); + if (DConfig.Check(DEFCON_NO_NEW_MEMOS)) + source.Reply(_("* No new memos sent")); + } + +public: + CommandOSDefcon(Module *creator) : Command(creator, "operserv/defcon", 1, 1) + { + this->SetDesc(_("Manipulate the DefCon system")); + this->SetSyntax(_("[\0021\002|\0022\002|\0023\002|\0024\002|\0025\002]")); + } + + void Execute(CommandSource &source, const std::vector ¶ms) override + { + const Anope::string &lvl = params[0]; + + if (lvl.empty()) + { + source.Reply(_("Services are now at DEFCON \002%d\002."), DConfig.defaultlevel); + this->SendLevels(source); + return; + } + + int newLevel = 0; + try + { + newLevel = convertTo(lvl); + } + catch (const ConvertException &) { } + + if (newLevel < 1 || newLevel > 5) + { + this->OnSyntaxError(source, ""); + return; + } + + DConfig.defaultlevel = newLevel; + + FOREACH_MOD(OnDefconLevel, (newLevel)); + + delete timeout; + + if (DConfig.timeout) + timeout = new DefConTimeout(this->module, 5); + + source.Reply(_("Services are now at DEFCON \002%d\002."), DConfig.defaultlevel); + this->SendLevels(source); + Log(LOG_ADMIN, source, this) << "to change defcon level to " << newLevel; + + /* Global notice the user what is happening. Also any Message that + the Admin would like to add. Set in config file. */ + if (DConfig.globalondefcon) + { + if (DConfig.defaultlevel == 5 && !DConfig.offmessage.empty()) + GlobalService->SendGlobal(NULL, "", DConfig.offmessage); + else if (DConfig.defaultlevel != 5) + { + GlobalService->SendGlobal(NULL, "", Anope::printf(_("The Defcon level is now at: \002%d\002"), DConfig.defaultlevel)); + if (!DConfig.message.empty()) + GlobalService->SendGlobal(NULL, "", DConfig.message); + } + } + + /* Run any defcon functions, e.g. FORCE CHAN MODE */ + runDefCon(); + return; + } + + bool OnHelp(CommandSource &source, const Anope::string &subcommand) override + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("The defcon system can be used to implement a pre-defined\n" + "set of restrictions to services useful during an attempted\n" + "attack on the network.")); + return true; + } +}; + +class OSDefcon final + : public Module +{ + ServiceReference session_service; + ServiceReference akills; + CommandOSDefcon commandosdefcon; + + void ParseModeString() + { + int add = -1; /* 1 if adding, 0 if deleting, -1 if neither */ + ChannelMode *cm; + ChannelModeParam *cmp; + Anope::string modes, param; + + spacesepstream ss(DConfig.chanmodes); + + DConfig.DefConModesOn.clear(); + DConfig.DefConModesOff.clear(); + ss.GetToken(modes); + + /* Loop while there are modes to set */ + for (auto mode : modes) + { + switch (mode) + { + case '+': + add = 1; + continue; + case '-': + add = 0; + continue; + default: + if (add < 0) + continue; + } + + if ((cm = ModeManager::FindChannelModeByChar(mode))) + { + if (cm->type == MODE_STATUS || cm->type == MODE_LIST) + { + Log(this) << "DefConChanModes mode character '" << mode << "' cannot be locked"; + continue; + } + else if (add) + { + DConfig.DefConModesOn.insert(cm->name); + DConfig.DefConModesOff.erase(cm->name); + + if (cm->type == MODE_PARAM) + { + cmp = anope_dynamic_static_cast(cm); + + if (!ss.GetToken(param)) + { + Log(this) << "DefConChanModes mode character '" << mode << "' has no parameter while one is expected"; + continue; + } + + if (!cmp->IsValid(param)) + continue; + + DConfig.SetDefConParam(cmp->name, param); + } + } + else if (DConfig.DefConModesOn.count(cm->name)) + { + DConfig.DefConModesOn.erase(cm->name); + + if (cm->type == MODE_PARAM) + DConfig.UnsetDefConParam(cm->name); + } + } + } + + /* We can't mlock +L if +l is not mlocked as well. */ + if ((cm = ModeManager::FindChannelModeByName("REDIRECT")) && DConfig.DefConModesOn.count(cm->name) && !DConfig.DefConModesOn.count("LIMIT")) + { + DConfig.DefConModesOn.erase("REDIRECT"); + + Log(this) << "DefConChanModes must lock mode +l as well to lock mode +L"; + } + } + +public: + OSDefcon(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), session_service("SessionService", "session"), akills("XLineManager", "xlinemanager/sgline"), commandosdefcon(this) + { + + } + + void OnReload(Configuration::Conf *conf) override + { + Configuration::Block *block = conf->GetModule(this); + DefconConfig dconfig; + + dconfig.defaultlevel = block->Get("defaultlevel"); + dconfig.defcons[4] = block->Get("level4"); + dconfig.defcons[3] = block->Get("level3"); + dconfig.defcons[2] = block->Get("level2"); + dconfig.defcons[1] = block->Get("level1"); + dconfig.sessionlimit = block->Get("sessionlimit"); + dconfig.akillreason = block->Get("akillreason"); + dconfig.akillexpire = block->Get("akillexpire"); + dconfig.chanmodes = block->Get("chanmodes"); + dconfig.timeout = block->Get("timeout"); + dconfig.globalondefcon = block->Get("globalondefcon"); + dconfig.message = block->Get("message"); + dconfig.offmessage = block->Get("offmessage"); + + block = conf->GetModule("os_session"); + + dconfig.max_session_kill = block->Get("maxsessionkill"); + dconfig.session_autokill_expiry = block->Get("sessionautokillexpiry"); + dconfig.sle_reason = block->Get("sessionlimitexceeded"); + dconfig.sle_detailsloc = block->Get("sessionlimitdetailsloc"); + + if (dconfig.defaultlevel < 1 || dconfig.defaultlevel > 5) + throw ConfigException("The value for must be between 1 and 5"); + else if (dconfig.akillexpire <= 0) + throw ConfigException("The value for must be greater than zero!"); + + for (unsigned level = 1; level < 5; ++level) + { + spacesepstream operations(dconfig.defcons[level]); + Anope::string operation; + while (operations.GetToken(operation)) + { + if (operation.equals_ci("nonewchannels")) + dconfig.Add(level, DEFCON_NO_NEW_CHANNELS); + else if (operation.equals_ci("nonewnicks")) + dconfig.Add(level, DEFCON_NO_NEW_NICKS); + else if (operation.equals_ci("nomlockchanges")) + dconfig.Add(level, DEFCON_NO_MLOCK_CHANGE); + else if (operation.equals_ci("forcechanmodes")) + dconfig.Add(level, DEFCON_FORCE_CHAN_MODES); + else if (operation.equals_ci("reducedsessions")) + dconfig.Add(level, DEFCON_REDUCE_SESSION); + else if (operation.equals_ci("nonewclients")) + dconfig.Add(level, DEFCON_NO_NEW_CLIENTS); + else if (operation.equals_ci("operonly")) + dconfig.Add(level, DEFCON_OPER_ONLY); + else if (operation.equals_ci("silentoperonly")) + dconfig.Add(level, DEFCON_SILENT_OPER_ONLY); + else if (operation.equals_ci("akillnewclients")) + dconfig.Add(level, DEFCON_AKILL_NEW_CLIENTS); + else if (operation.equals_ci("nonewmemos")) + dconfig.Add(level, DEFCON_NO_NEW_MEMOS); + } + + if (dconfig.Check(level, DEFCON_REDUCE_SESSION) && dconfig.sessionlimit <= 0) + throw ConfigException("The value for must be greater than zero!"); + else if (dconfig.Check(level, DEFCON_AKILL_NEW_CLIENTS) && dconfig.akillreason.empty()) + throw ConfigException("The value for must not be empty!"); + else if (dconfig.Check(level, DEFCON_FORCE_CHAN_MODES) && dconfig.chanmodes.empty()) + throw ConfigException("The value for must not be empty!"); + } + + DConfig = dconfig; + this->ParseModeString(); + } + + EventReturn OnChannelModeSet(Channel *c, MessageSource &source, ChannelMode *mode, const Anope::string ¶m) override + { + if (DConfig.Check(DEFCON_FORCE_CHAN_MODES) && DConfig.DefConModesOff.count(mode->name) && source.GetUser() && !source.GetBot()) + { + c->RemoveMode(Config->GetClient("OperServ"), mode, param); + + return EVENT_STOP; + } + + return EVENT_CONTINUE; + } + + EventReturn OnChannelModeUnset(Channel *c, MessageSource &source, ChannelMode *mode, const Anope::string &) override + { + if (DConfig.Check(DEFCON_FORCE_CHAN_MODES) && DConfig.DefConModesOn.count(mode->name) && source.GetUser() && !source.GetBot()) + { + Anope::string param; + + if (DConfig.GetDefConParam(mode->name, param)) + c->SetMode(Config->GetClient("OperServ"), mode, param); + else + c->SetMode(Config->GetClient("OperServ"), mode); + + return EVENT_STOP; + + } + + return EVENT_CONTINUE; + } + + EventReturn OnPreCommand(CommandSource &source, Command *command, std::vector ¶ms) override + { + if (DConfig.Check(DEFCON_OPER_ONLY) && !source.IsOper()) + { + source.Reply(_("Services are in DefCon mode, please try again later.")); + return EVENT_STOP; + } + else if (DConfig.Check(DEFCON_SILENT_OPER_ONLY) && !source.IsOper()) + { + return EVENT_STOP; + } + else if (command->name == "nickserv/register" || command->name == "nickserv/group") + { + if (DConfig.Check(DEFCON_NO_NEW_NICKS)) + { + source.Reply(_("Services are in DefCon mode, please try again later.")); + return EVENT_STOP; + } + } + else if (command->name == "chanserv/mode" && params.size() > 1 && params[1].equals_ci("LOCK")) + { + if (DConfig.Check(DEFCON_NO_MLOCK_CHANGE)) + { + source.Reply(_("Services are in DefCon mode, please try again later.")); + return EVENT_STOP; + } + } + else if (command->name == "chanserv/register") + { + if (DConfig.Check(DEFCON_NO_NEW_CHANNELS)) + { + source.Reply(_("Services are in DefCon mode, please try again later.")); + return EVENT_STOP; + } + } + else if (command->name == "memoserv/send") + { + if (DConfig.Check(DEFCON_NO_NEW_MEMOS)) + { + source.Reply(_("Services are in DefCon mode, please try again later.")); + return EVENT_STOP; + } + } + + return EVENT_CONTINUE; + } + + void OnUserConnect(User *u, bool &exempt) override + { + if (exempt || u->Quitting() || !u->server->IsSynced() || u->server->IsULined()) + return; + + BotInfo *OperServ = Config->GetClient("OperServ"); + if (DConfig.Check(DEFCON_AKILL_NEW_CLIENTS) && akills) + { + Log(OperServ, "operserv/defcon") << "DEFCON: adding akill for *@" << u->host; + XLine x("*@" + u->host, OperServ ? OperServ->nick : "defcon", Anope::CurTime + DConfig.akillexpire, DConfig.akillreason, XLineManager::GenerateUID()); + akills->Send(NULL, &x); + } + + if (DConfig.Check(DEFCON_NO_NEW_CLIENTS) || DConfig.Check(DEFCON_AKILL_NEW_CLIENTS)) + { + u->Kill(OperServ, DConfig.akillreason); + return; + } + + if (DConfig.sessionlimit <= 0 || !session_service) + return; + + Session *session = session_service->FindSession(u->ip.addr()); + Exception *exception = session_service->FindException(u); + + if (DConfig.Check(DEFCON_REDUCE_SESSION) && !exception) + { + if (session && session->count > static_cast(DConfig.sessionlimit)) + { + if (!DConfig.sle_reason.empty()) + { + Anope::string message = DConfig.sle_reason.replace_all_cs("%IP%", u->ip.addr()); + u->SendMessage(OperServ, message); + } + if (!DConfig.sle_detailsloc.empty()) + u->SendMessage(OperServ, DConfig.sle_detailsloc); + + ++session->hits; + if (akills && DConfig.max_session_kill && session->hits >= DConfig.max_session_kill) + { + XLine x("*@" + session->addr.mask(), OperServ ? OperServ->nick : "", Anope::CurTime + DConfig.session_autokill_expiry, "Defcon session limit exceeded", XLineManager::GenerateUID()); + akills->Send(NULL, &x); + Log(OperServ, "akill/defcon") << "[DEFCON] Added a temporary AKILL for \002*@" << session->addr.mask() << "\002 due to excessive connections"; + } + else + { + u->Kill(OperServ, "Defcon session limit exceeded"); + } + } + } + } + + void OnChannelModeAdd(ChannelMode *cm) override + { + if (DConfig.chanmodes.find(cm->mchar) != Anope::string::npos) + this->ParseModeString(); + } + + void OnChannelSync(Channel *c) override + { + if (DConfig.Check(DEFCON_FORCE_CHAN_MODES)) + c->SetModes(Config->GetClient("OperServ"), false, DConfig.chanmodes); + } +}; + +static void runDefCon() +{ + BotInfo *OperServ = Config->GetClient("OperServ"); + if (DConfig.Check(DEFCON_FORCE_CHAN_MODES)) + { + if (!DConfig.chanmodes.empty() && !DefConModesSet) + { + if (DConfig.chanmodes[0] == '+' || DConfig.chanmodes[0] == '-') + { + Log(OperServ, "operserv/defcon") << "DEFCON: setting " << DConfig.chanmodes << " on all channels"; + DefConModesSet = true; + for (const auto &[_, chan] : ChannelList) + chan->SetModes(OperServ, false, DConfig.chanmodes); + } + } + } + else + { + if (!DConfig.chanmodes.empty() && DefConModesSet) + { + if (DConfig.chanmodes[0] == '+' || DConfig.chanmodes[0] == '-') + { + DefConModesSet = false; + Anope::string newmodes = defconReverseModes(DConfig.chanmodes); + if (!newmodes.empty()) + { + Log(OperServ, "operserv/defcon") << "DEFCON: setting " << newmodes << " on all channels"; + for (const auto &[_, chan] : ChannelList) + chan->SetModes(OperServ, true, newmodes); + } + } + } + } +} + +static Anope::string defconReverseModes(const Anope::string &modes) +{ + if (modes.empty()) + return ""; + Anope::string newmodes; + for (auto mode : modes) + { + if (mode == '+') + newmodes += '-'; + else if (mode == '-') + newmodes += '+'; + else + newmodes += mode; + } + return newmodes; +} + +MODULE_INIT(OSDefcon) diff --git a/modules/operserv/os_dns.cpp b/modules/operserv/os_dns.cpp new file mode 100644 index 000000000..79f4bf43e --- /dev/null +++ b/modules/operserv/os_dns.cpp @@ -0,0 +1,944 @@ +/* + * + * (C) 2003-2024 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + */ + +#include "module.h" +#include "modules/dns.h" + +static ServiceReference dnsmanager("DNS::Manager", "dns/manager"); + +struct DNSZone; +class DNSServer; + +static Serialize::Checker > zones("DNSZone"); +static Serialize::Checker > dns_servers("DNSServer"); + +static std::map > server_quit_times; + +struct DNSZone final + : Serializable +{ + Anope::string name; + std::set servers; + + DNSZone(const Anope::string &n) : Serializable("DNSZone"), name(n) + { + zones->push_back(this); + } + + ~DNSZone() override + { + std::vector::iterator it = std::find(zones->begin(), zones->end(), this); + if (it != zones->end()) + zones->erase(it); + } + + void Serialize(Serialize::Data &data) const override + { + data["name"] << name; + unsigned count = 0; + for (const auto &server : servers) + data["server" + stringify(count++)] << server; + } + + static Serializable* Unserialize(Serializable *obj, Serialize::Data &data) + { + DNSZone *zone; + Anope::string zone_name; + + data["name"] >> zone_name; + + if (obj) + { + zone = anope_dynamic_static_cast(obj); + data["name"] >> zone->name; + } + else + zone = new DNSZone(zone_name); + + zone->servers.clear(); + for (unsigned count = 0; true; ++count) + { + Anope::string server_str; + data["server" + stringify(count)] >> server_str; + if (server_str.empty()) + break; + zone->servers.insert(server_str); + } + + return zone; + } + + static DNSZone *Find(const Anope::string &name) + { + for (auto *zone : *zones) + { + if (zone->name.equals_ci(name)) + { + zone->QueueUpdate(); + return zone; + } + } + return NULL; + } +}; + +class DNSServer final + : public Serializable +{ + Anope::string server_name; + std::vector ips; + unsigned limit = 0; + /* wants to be in the pool */ + bool pooled = false; + /* is actually in the pool */ + bool active = false; + +public: + std::set zones; + time_t repool = 0; + + DNSServer(const Anope::string &sn) : Serializable("DNSServer"), server_name(sn) + { + dns_servers->push_back(this); + } + + ~DNSServer() override + { + std::vector::iterator it = std::find(dns_servers->begin(), dns_servers->end(), this); + if (it != dns_servers->end()) + dns_servers->erase(it); + } + + const Anope::string &GetName() const { return server_name; } + std::vector &GetIPs() { return ips; } + unsigned GetLimit() const { return limit; } + void SetLimit(unsigned l) { limit = l; } + + bool Pooled() const { return pooled; } + void Pool(bool p) + { + if (!p) + this->SetActive(p); + pooled = p; + } + + bool Active() const { return pooled && active; } + void SetActive(bool p) + { + if (p) + this->Pool(p); + active = p; + + if (dnsmanager) + { + dnsmanager->UpdateSerial(); + for (const auto &zone : zones) + dnsmanager->Notify(zone); + } + } + + void Serialize(Serialize::Data &data) const override + { + data["server_name"] << server_name; + for (unsigned i = 0; i < ips.size(); ++i) + data["ip" + stringify(i)] << ips[i]; + data["limit"] << limit; + data["pooled"] << pooled; + unsigned count = 0; + for (const auto &zone : zones) + data["zone" + stringify(count++)] << zone; + } + + static Serializable* Unserialize(Serializable *obj, Serialize::Data &data) + { + DNSServer *req; + Anope::string server_name; + + data["server_name"] >> server_name; + + if (obj) + { + req = anope_dynamic_static_cast(obj); + req->server_name = server_name; + } + else + req = new DNSServer(server_name); + + for (unsigned i = 0; true; ++i) + { + Anope::string ip_str; + data["ip" + stringify(i)] >> ip_str; + if (ip_str.empty()) + break; + req->ips.push_back(ip_str); + } + + data["limit"] >> req->limit; + data["pooled"] >> req->pooled; + + req->zones.clear(); + for (unsigned i = 0; true; ++i) + { + Anope::string zone_str; + data["zone" + stringify(i)] >> zone_str; + if (zone_str.empty()) + break; + req->zones.insert(zone_str); + } + + return req; + } + + static DNSServer *Find(const Anope::string &s) + { + for (auto *serv : *dns_servers) + if (serv->GetName().equals_ci(s)) + { + serv->QueueUpdate(); + return serv; + } + return NULL; + } +}; + +class CommandOSDNS final + : public Command +{ + void DisplayPoolState(CommandSource &source) + { + if (dns_servers->empty()) + { + source.Reply(_("There are no configured servers.")); + return; + } + + ListFormatter lf(source.GetAccount()); + lf.AddColumn(_("Server")).AddColumn(_("IP")).AddColumn(_("Limit")).AddColumn(_("State")); + for (auto *s : *dns_servers) + { + Server *srv = Server::Find(s->GetName(), true); + + ListFormatter::ListEntry entry; + entry["Server"] = s->GetName(); + entry["Limit"] = s->GetLimit() ? stringify(s->GetLimit()) : Language::Translate(source.GetAccount(), _("None")); + + Anope::string ip_str; + for (const auto &ip : s->GetIPs()) + ip_str += ip + " "; + ip_str.trim(); + if (ip_str.empty()) + ip_str = "None"; + entry["IP"] = ip_str; + + if (s->Active()) + entry["State"] = Language::Translate(source.GetAccount(), _("Pooled/Active")); + else if (s->Pooled()) + entry["State"] = Language::Translate(source.GetAccount(), _("Pooled/Not Active")); + else + entry["State"] = Language::Translate(source.GetAccount(), _("Unpooled")); + + if (!srv) + entry["State"] += Anope::string(" ") + Language::Translate(source.GetAccount(), _("(Split)")); + + lf.AddEntry(entry); + } + + std::vector replies; + lf.Process(replies); + + if (!zones->empty()) + { + ListFormatter lf2(source.GetAccount()); + lf2.AddColumn(_("Zone")).AddColumn(_("Servers")); + + for (auto *z : *zones) + { + ListFormatter::ListEntry entry; + entry["Zone"] = z->name; + + Anope::string server_str; + for (const auto &server : z->servers) + server_str += server + " "; + server_str.trim(); + + if (server_str.empty()) + server_str = "None"; + + entry["Servers"] = server_str; + + lf2.AddEntry(entry); + } + + lf2.Process(replies); + } + + for (const auto &reply : replies) + source.Reply(reply); + } + + void AddZone(CommandSource &source, const std::vector ¶ms) + { + const Anope::string &zone = params[1]; + + if (DNSZone::Find(zone)) + { + source.Reply(_("Zone %s already exists."), zone.c_str()); + return; + } + + if (Anope::ReadOnly) + source.Reply(READ_ONLY_MODE); + + Log(LOG_ADMIN, source, this) << "to add zone " << zone; + + new DNSZone(zone); + source.Reply(_("Added zone %s."), zone.c_str()); + } + + void DelZone(CommandSource &source, const std::vector ¶ms) + { + const Anope::string &zone = params[1]; + + DNSZone *z = DNSZone::Find(zone); + if (!z) + { + source.Reply(_("Zone %s does not exist."), zone.c_str()); + return; + } + + if (Anope::ReadOnly) + source.Reply(READ_ONLY_MODE); + + Log(LOG_ADMIN, source, this) << "to delete zone " << z->name; + + for (const auto &server : z->servers) + { + DNSServer *s = DNSServer::Find(server); + if (s) + s->zones.erase(z->name); + } + + if (dnsmanager) + { + dnsmanager->UpdateSerial(); + dnsmanager->Notify(z->name); + } + + source.Reply(_("Zone %s removed."), z->name.c_str()); + delete z; + } + + void AddServer(CommandSource &source, const std::vector ¶ms) + { + DNSServer *s = DNSServer::Find(params[1]); + const Anope::string &zone = params.size() > 2 ? params[2] : ""; + + if (s) + { + if (zone.empty()) + { + source.Reply(_("Server %s already exists."), s->GetName().c_str()); + } + else + { + DNSZone *z = DNSZone::Find(zone); + if (!z) + { + source.Reply(_("Zone %s does not exist."), zone.c_str()); + return; + } + else if (z->servers.count(s->GetName())) + { + source.Reply(_("Server %s is already in zone %s."), s->GetName().c_str(), z->name.c_str()); + return; + } + + if (Anope::ReadOnly) + source.Reply(READ_ONLY_MODE); + + z->servers.insert(s->GetName()); + s->zones.insert(zone); + + if (dnsmanager) + { + dnsmanager->UpdateSerial(); + dnsmanager->Notify(zone); + } + + Log(LOG_ADMIN, source, this) << "to add server " << s->GetName() << " to zone " << z->name; + + source.Reply(_("Server %s added to zone %s."), s->GetName().c_str(), z->name.c_str()); + } + + return; + } + + Server *serv = Server::Find(params[1], true); + if (!serv || serv == Me || serv->IsJuped()) + { + source.Reply(_("Server %s is not linked to the network."), params[1].c_str()); + return; + } + + s = new DNSServer(params[1]); + if (zone.empty()) + { + if (Anope::ReadOnly) + source.Reply(READ_ONLY_MODE); + + Log(LOG_ADMIN, source, this) << "to add server " << s->GetName(); + source.Reply(_("Added server %s."), s->GetName().c_str()); + } + else + { + DNSZone *z = DNSZone::Find(zone); + if (!z) + { + source.Reply(_("Zone %s does not exist."), zone.c_str()); + delete s; + return; + } + + if (Anope::ReadOnly) + source.Reply(READ_ONLY_MODE); + + Log(LOG_ADMIN, source, this) << "to add server " << s->GetName() << " to zone " << zone; + + z->servers.insert(s->GetName()); + s->zones.insert(z->name); + + if (dnsmanager) + { + dnsmanager->UpdateSerial(); + dnsmanager->Notify(z->name); + } + } + } + + void DelServer(CommandSource &source, const std::vector ¶ms) + { + DNSServer *s = DNSServer::Find(params[1]); + const Anope::string &zone = params.size() > 2 ? params[2] : ""; + + if (!s) + { + source.Reply(_("Server %s does not exist."), params[1].c_str()); + return; + } + else if (!zone.empty()) + { + DNSZone *z = DNSZone::Find(zone); + if (!z) + { + source.Reply(_("Zone %s does not exist."), zone.c_str()); + return; + } + else if (!z->servers.count(s->GetName())) + { + source.Reply(_("Server %s is not in zone %s."), s->GetName().c_str(), z->name.c_str()); + return; + } + + if (Anope::ReadOnly) + source.Reply(READ_ONLY_MODE); + + Log(LOG_ADMIN, source, this) << "to remove server " << s->GetName() << " from zone " << z->name; + + if (dnsmanager) + { + dnsmanager->UpdateSerial(); + dnsmanager->Notify(z->name); + } + + z->servers.erase(s->GetName()); + s->zones.erase(z->name); + source.Reply(_("Removed server %s from zone %s."), s->GetName().c_str(), z->name.c_str()); + return; + } + else if (Server::Find(s->GetName(), true)) + { + source.Reply(_("Server %s must be quit before it can be deleted."), s->GetName().c_str()); + return; + } + + for (const auto &zone : s->zones) + { + DNSZone *z = DNSZone::Find(zone); + if (z) + z->servers.erase(s->GetName()); + } + + if (Anope::ReadOnly) + source.Reply(READ_ONLY_MODE); + + if (dnsmanager) + dnsmanager->UpdateSerial(); + + Log(LOG_ADMIN, source, this) << "to delete server " << s->GetName(); + source.Reply(_("Removed server %s."), s->GetName().c_str()); + delete s; + } + + void AddIP(CommandSource &source, const std::vector ¶ms) + { + DNSServer *s = DNSServer::Find(params[1]); + + if (!s) + { + source.Reply(_("Server %s does not exist."), params[1].c_str()); + return; + } + + for (const auto &ip : s->GetIPs()) + { + if (params[2].equals_ci(ip)) + { + source.Reply(_("IP %s already exists for %s."), ip.c_str(), s->GetName().c_str()); + return; + } + } + + sockaddrs addr(params[2]); + if (!addr.valid()) + { + source.Reply(_("%s is not a valid IP address."), params[2].c_str()); + return; + } + + if (Anope::ReadOnly) + source.Reply(READ_ONLY_MODE); + + s->GetIPs().push_back(params[2]); + source.Reply(_("Added IP %s to %s."), params[2].c_str(), s->GetName().c_str()); + Log(LOG_ADMIN, source, this) << "to add IP " << params[2] << " to " << s->GetName(); + + if (s->Active() && dnsmanager) + { + dnsmanager->UpdateSerial(); + for (const auto &zone : s->zones) + dnsmanager->Notify(zone); + } + } + + void DelIP(CommandSource &source, const std::vector ¶ms) + { + DNSServer *s = DNSServer::Find(params[1]); + + if (!s) + { + source.Reply(_("Server %s does not exist."), params[1].c_str()); + return; + } + + if (Anope::ReadOnly) + source.Reply(READ_ONLY_MODE); + + for (unsigned i = 0; i < s->GetIPs().size(); ++i) + if (params[2].equals_ci(s->GetIPs()[i])) + { + s->GetIPs().erase(s->GetIPs().begin() + i); + source.Reply(_("Removed IP %s from %s."), params[2].c_str(), s->GetName().c_str()); + Log(LOG_ADMIN, source, this) << "to remove IP " << params[2] << " from " << s->GetName(); + + if (s->GetIPs().empty()) + { + s->repool = 0; + s->Pool(false); + } + + if (s->Active() && dnsmanager) + { + dnsmanager->UpdateSerial(); + for (const auto &zone : s->zones) + dnsmanager->Notify(zone); + } + + return; + } + + source.Reply(_("IP %s does not exist for %s."), params[2].c_str(), s->GetName().c_str()); + } + + void OnSet(CommandSource &source, const std::vector ¶ms) + { + DNSServer *s = DNSServer::Find(params[1]); + + if (!s) + { + source.Reply(_("Server %s does not exist."), params[1].c_str()); + return; + } + + if (Anope::ReadOnly) + source.Reply(READ_ONLY_MODE); + + if (params[2].equals_ci("LIMIT")) + { + try + { + unsigned l = convertTo(params[3]); + s->SetLimit(l); + if (l) + source.Reply(_("User limit for %s set to %d."), s->GetName().c_str(), l); + else + source.Reply(_("User limit for %s rem