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/nickserv/nickserv.cpp | 569 ++++++++++++++ modules/nickserv/ns_access.cpp | 208 ++++++ modules/nickserv/ns_ajoin.cpp | 408 +++++++++++ modules/nickserv/ns_alist.cpp | 148 ++++ modules/nickserv/ns_cert.cpp | 412 +++++++++++ modules/nickserv/ns_drop.cpp | 88 +++ modules/nickserv/ns_getemail.cpp | 74 ++ modules/nickserv/ns_group.cpp | 399 ++++++++++ modules/nickserv/ns_identify.cpp | 131 ++++ modules/nickserv/ns_info.cpp | 287 ++++++++ modules/nickserv/ns_list.cpp | 303 ++++++++ modules/nickserv/ns_logout.cpp | 93 +++ modules/nickserv/ns_maxemail.cpp | 112 +++ modules/nickserv/ns_recover.cpp | 301 ++++++++ modules/nickserv/ns_register.cpp | 445 +++++++++++ modules/nickserv/ns_resetpass.cpp | 146 ++++ modules/nickserv/ns_set.cpp | 1464 +++++++++++++++++++++++++++++++++++++ modules/nickserv/ns_set_misc.cpp | 238 ++++++ modules/nickserv/ns_status.cpp | 89 +++ modules/nickserv/ns_suspend.cpp | 291 ++++++++ modules/nickserv/ns_update.cpp | 64 ++ 21 files changed, 6270 insertions(+) create mode 100644 modules/nickserv/nickserv.cpp create mode 100644 modules/nickserv/ns_access.cpp create mode 100644 modules/nickserv/ns_ajoin.cpp create mode 100644 modules/nickserv/ns_alist.cpp create mode 100644 modules/nickserv/ns_cert.cpp create mode 100644 modules/nickserv/ns_drop.cpp create mode 100644 modules/nickserv/ns_getemail.cpp create mode 100644 modules/nickserv/ns_group.cpp create mode 100644 modules/nickserv/ns_identify.cpp create mode 100644 modules/nickserv/ns_info.cpp create mode 100644 modules/nickserv/ns_list.cpp create mode 100644 modules/nickserv/ns_logout.cpp create mode 100644 modules/nickserv/ns_maxemail.cpp create mode 100644 modules/nickserv/ns_recover.cpp create mode 100644 modules/nickserv/ns_register.cpp create mode 100644 modules/nickserv/ns_resetpass.cpp create mode 100644 modules/nickserv/ns_set.cpp create mode 100644 modules/nickserv/ns_set_misc.cpp create mode 100644 modules/nickserv/ns_status.cpp create mode 100644 modules/nickserv/ns_suspend.cpp create mode 100644 modules/nickserv/ns_update.cpp (limited to 'modules/nickserv') diff --git a/modules/nickserv/nickserv.cpp b/modules/nickserv/nickserv.cpp new file mode 100644 index 000000000..0f4313c0e --- /dev/null +++ b/modules/nickserv/nickserv.cpp @@ -0,0 +1,569 @@ +/* NickServ 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 NickServCollide; +static std::set collides; + +/** Timer for colliding nicks to force people off of nicknames + */ +class NickServCollide final + : public Timer +{ + NickServService *service; + Reference u; + time_t ts; + Reference na; + +public: + NickServCollide(Module *me, NickServService *nss, User *user, NickAlias *nick, time_t delay) : Timer(me, delay), service(nss), u(user), ts(user->timestamp), na(nick) + { + collides.insert(this); + } + + ~NickServCollide() override + { + collides.erase(this); + } + + User *GetUser() + { + return u; + } + + NickAlias *GetNick() + { + return na; + } + + void Tick(time_t t) override + { + if (!u || !na) + return; + /* If they identified or don't exist anymore, don't kill them. */ + if (u->Account() == na->nc || u->timestamp > ts) + return; + + service->Collide(u, na); + } +}; + +/** Timer for removing HELD status from nicks. + */ +class NickServHeld final + : public Timer +{ + Reference na; + Anope::string nick; +public: + NickServHeld(Module *me, NickAlias *n, long l) : Timer(me, l), na(n), nick(na->nick) + { + n->Extend("HELD"); + } + + void Tick(time_t) override + { + if (na) + na->Shrink("HELD"); + } +}; + +class NickServRelease; +static Anope::map NickServReleases; + +/** Timer for releasing nicks to be available for use + */ +class NickServRelease final + : public User + , public Timer +{ + Anope::string nick; + +public: + NickServRelease(Module *me, NickAlias *na, time_t delay) : User(na->nick, Config->GetModule("nickserv")->Get("enforceruser", "user"), + Config->GetModule("nickserv")->Get("enforcerhost", Me->GetName()), "", "", Me, "Services Enforcer", Anope::CurTime, "", IRCD->UID_Retrieve(), NULL), Timer(me, delay), nick(na->nick) + { + /* Erase the current release timer and use the new one */ + Anope::map::iterator nit = NickServReleases.find(this->nick); + if (nit != NickServReleases.end()) + { + IRCD->SendQuit(nit->second, ""); + delete nit->second; + } + + NickServReleases.emplace(this->nick, this); + + IRCD->SendClientIntroduction(this); + } + + ~NickServRelease() override + { + IRCD->SendQuit(this, ""); + NickServReleases.erase(this->nick); + } + + void Tick(time_t t) override { } +}; + +class NickServCore final + : public Module + , public NickServService +{ + Reference NickServ; + std::vector defaults; + ExtensibleItem held, collided; + + void OnCancel(User *u, NickAlias *na) + { + if (collided.HasExt(na)) + { + collided.Unset(na); + + new NickServHeld(this, na, Config->GetModule("nickserv")->Get("releasetimeout", "1m")); + + if (IRCD->CanSVSHold) + IRCD->SendSVSHold(na->nick, Config->GetModule("nickserv")->Get("releasetimeout", "1m")); + else + new NickServRelease(this, na, Config->GetModule("nickserv")->Get("releasetimeout", "1m")); + } + } + +public: + NickServCore(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, PSEUDOCLIENT | VENDOR), + NickServService(this), held(this, "HELD"), collided(this, "COLLIDED") + { + } + + ~NickServCore() override + { + OnShutdown(); + } + + void OnShutdown() override + { + /* On shutdown, restart, or mod unload, remove all of our holds for nicks (svshold or qlines) + * because some IRCds do not allow us to have these automatically expire + */ + for (const auto &[_, na] : *NickAliasList) + this->Release(na); + } + + void OnRestart() override + { + OnShutdown(); + } + + void Validate(User *u) override + { + NickAlias *na = NickAlias::Find(u->nick); + if (!na) + return; + + EventReturn MOD_RESULT; + FOREACH_RESULT(OnNickValidate, MOD_RESULT, (u, na)); + if (MOD_RESULT == EVENT_STOP) + { + this->Collide(u, na); + return; + } + else if (MOD_RESULT == EVENT_ALLOW) + return; + + if (!na->nc->HasExt("NS_SECURE") && u->IsRecognized()) + { + na->last_seen = Anope::CurTime; + na->last_usermask = u->GetIdent() + "@" + u->GetDisplayedHost(); + na->last_realname = u->realname; + return; + } + + if (Config->GetModule("nickserv")->Get("nonicknameownership")) + return; + + bool on_access = u->IsRecognized(false); + + if (on_access || !na->nc->HasExt("KILL_IMMED")) + { + if (na->nc->HasExt("NS_SECURE")) + u->SendMessage(NickServ, NICK_IS_SECURE, Config->StrictPrivmsg.c_str(), NickServ->nick.c_str()); + else + u->SendMessage(NickServ, NICK_IS_REGISTERED, Config->StrictPrivmsg.c_str(), NickServ->nick.c_str()); + } + if (na->nc->HasExt("KILLPROTECT") && !on_access) + { + if (na->nc->HasExt("KILL_IMMED")) + { + u->SendMessage(NickServ, FORCENICKCHANGE_NOW); + this->Collide(u, na); + } + else if (na->nc->HasExt("KILL_QUICK")) + { + time_t killquick = Config->GetModule("nickserv")->Get("killquick", "20s"); + u->SendMessage(NickServ, _("If you do not change within %s, I will change your nick."), Anope::Duration(killquick, u->Account()).c_str()); + new NickServCollide(this, this, u, na, killquick); + } + else + { + time_t kill = Config->GetModule("nickserv")->Get("kill", "60s"); + u->SendMessage(NickServ, _("If you do not change within %s, I will change your nick."), Anope::Duration(kill, u->Account()).c_str()); + new NickServCollide(this, this, u, na, kill); + } + } + + } + + void OnUserLogin(User *u) override + { + NickAlias *na = NickAlias::Find(u->nick); + if (na && *na->nc == u->Account() && !Config->GetModule("nickserv")->Get("nonicknameownership") && !na->nc->HasExt("UNCONFIRMED")) + u->SetMode(NickServ, "REGISTERED"); + + const Anope::string &modesonid = Config->GetModule(this)->Get("modesonid"); + if (!modesonid.empty()) + u->SetModes(NickServ, modesonid); + } + + void Collide(User *u, NickAlias *na) override + { + if (na) + collided.Set(na); + + if (IRCD->CanSVSNick) + { + unsigned nicklen = Config->GetBlock("networkinfo")->Get("nicklen"); + const Anope::string &guestprefix = Config->GetModule("nickserv")->Get("guestnickprefix", "Guest"); + + Anope::string guestnick; + + int i = 0; + do + { + guestnick = guestprefix + stringify(static_cast(rand())); + if (guestnick.length() > nicklen) + guestnick = guestnick.substr(0, nicklen); + } + while (User::Find(guestnick) && i++ < 10); + + if (i == 11) + u->Kill(*NickServ, "Services nickname-enforcer kill"); + else + { + u->SendMessage(*NickServ, _("Your nickname is now being changed to \002%s\002"), guestnick.c_str()); + IRCD->SendForceNickChange(u, guestnick, Anope::CurTime); + } + } + else + u->Kill(*NickServ, "Services nickname-enforcer kill"); + } + + void Release(NickAlias *na) override + { + if (held.HasExt(na)) + { + if (IRCD->CanSVSHold) + IRCD->SendSVSHoldDel(na->nick); + else + { + User *u = User::Find(na->nick); + if (u && u->server == Me) + { + u->Quit(); + } + } + + held.Unset(na); + } + collided.Unset(na); /* clear pending collide */ + } + + void OnReload(Configuration::Conf *conf) override + { + const Anope::string &nsnick = conf->GetModule(this)->Get("client"); + + if (nsnick.empty()) + throw ConfigException(Module::name + ": must be defined"); + + BotInfo *bi = BotInfo::Find(nsnick, true); + if (!bi) + throw ConfigException(Module::name + ": no bot named " + nsnick); + + NickServ = bi; + + spacesepstream(conf->GetModule(this)->Get("defaults", "ns_secure memo_signon memo_receive")).GetTokens(defaults); + if (defaults.empty()) + { + defaults.emplace_back("NS_SECURE"); + defaults.emplace_back("MEMO_SIGNON"); + defaults.emplace_back("MEMO_RECEIVE"); + } + else if (defaults[0].equals_ci("none")) + defaults.clear(); + } + + void OnDelNick(NickAlias *na) override + { + User *u = User::Find(na->nick); + if (u && u->Account() == na->nc) + { + IRCD->SendLogout(u); + u->RemoveMode(NickServ, "REGISTERED"); + u->Logout(); + } + } + + void OnDelCore(NickCore *nc) override + { + Log(NickServ, "nick") << "Deleting nickname group " << nc->display; + + /* Clean up this nick core from any users online */ + for (std::list::iterator it = nc->users.begin(); it != nc->users.end();) + { + User *user = *it++; + IRCD->SendLogout(user); + user->RemoveMode(NickServ, "REGISTERED"); + user->Logout(); + FOREACH_MOD(OnNickLogout, (user)); + } + nc->users.clear(); + } + + void OnChangeCoreDisplay(NickCore *nc, const Anope::string &newdisplay) override + { + Log(LOG_NORMAL, "nick", NickServ) << "Changing " << nc->display << " nickname group display to " << newdisplay; + } + + void OnNickIdentify(User *u) override + { + Configuration::Block *block = Config->GetModule(this); + + if (block->Get("modeonid", "yes")) + { + for (const auto &[_, cc] : u->chans) + { + Channel *c = cc->chan; + if (c) + c->SetCorrectModes(u, true); + } + } + + const Anope::string &modesonid = block->Get("modesonid"); + if (!modesonid.empty()) + u->SetModes(NickServ, modesonid); + + if (block->Get("forceemail", "yes") && u->Account()->email.empty()) + { + u->SendMessage(NickServ, _("You must now supply an e-mail for your nick.\n" + "This e-mail will allow you to retrieve your password in\n" + "case you forget it.")); + u->SendMessage(NickServ, _("Type \002%s%s SET EMAIL \037e-mail\037\002 in order to set your e-mail.\n" + "Your privacy is respected; this e-mail won't be given to\n" + "any third-party person."), Config->StrictPrivmsg.c_str(), NickServ->nick.c_str()); + } + + for (auto *c : collides) + { + if (c->GetUser() == u && c->GetNick() && c->GetNick()->nc == u->Account()) + { + delete c; + break; + } + } + } + + void OnNickGroup(User *u, NickAlias *target) override + { + if (!target->nc->HasExt("UNCONFIRMED")) + u->SetMode(NickServ, "REGISTERED"); + } + + void OnNickUpdate(User *u) override + { + for (const auto &[_, cc] : u->chans) + { + Channel *c = cc->chan; + if (c) + c->SetCorrectModes(u, true); + } + } + + void OnUserConnect(User *u, bool &exempt) override + { + if (u->Quitting() || !u->server->IsSynced() || u->server->IsULined()) + return; + + const NickAlias *na = NickAlias::Find(u->nick); + + const Anope::string &unregistered_notice = Config->GetModule(this)->Get("unregistered_notice"); + if (!Config->GetModule("nickserv")->Get("nonicknameownership") && !unregistered_notice.empty() && !na && !u->Account()) + u->SendMessage(NickServ, unregistered_notice.replace_all_cs("%n", u->nick)); + else if (na && !u->IsIdentified(true)) + this->Validate(u); + } + + void OnPostUserLogoff(User *u) override + { + NickAlias *na = NickAlias::Find(u->nick); + if (na) + OnCancel(u, na); + } + + void OnServerSync(Server *s) override + { + for (const auto &[_, u] : UserListByNick) + { + if (u->server == s) + { + if (u->HasMode("REGISTERED") && !u->IsIdentified(true)) + u->RemoveMode(NickServ, "REGISTERED"); + if (!u->IsIdentified()) + this->Validate(u); + } + } + } + + void OnUserNickChange(User *u, const Anope::string &oldnick) override + { + NickAlias *old_na = NickAlias::Find(oldnick), *na = NickAlias::Find(u->nick); + /* If the new nick isn't registered or it's registered and not yours */ + if (!na || na->nc != u->Account()) + { + /* Remove +r, but keep an account associated with the user */ + u->RemoveMode(NickServ, "REGISTERED"); + + this->Validate(u); + } + else + { + /* Reset +r and re-send account (even though it really should be set at this point) */ + IRCD->SendLogin(u, na); + if (!Config->GetModule("nickserv")->Get("nonicknameownership") && na->nc == u->Account() && !na->nc->HasExt("UNCONFIRMED")) + u->SetMode(NickServ, "REGISTERED"); + Log(u, "", NickServ) << u->GetMask() << " automatically identified for group " << u->Account()->display; + } + + if (!u->nick.equals_ci(oldnick) && old_na) + OnCancel(u, old_na); + } + + void OnUserModeSet(const MessageSource &setter, User *u, const Anope::string &mname) override + { + if (u->server->IsSynced() && mname == "REGISTERED" && !u->IsIdentified(true)) + u->RemoveMode(NickServ, mname); + } + + EventReturn OnPreHelp(CommandSource &source, const std::vector ¶ms) override + { + if (!params.empty() || source.c || source.service != *NickServ) + return EVENT_CONTINUE; + if (!Config->GetModule("nickserv")->Get("nonicknameownership")) + source.Reply(_("\002%s\002 allows you to register a nickname and\n" + "prevent others from using it. The following\n" + "commands allow for registration and maintenance of\n" + "nicknames; to use them, type \002%s%s \037command\037\002.\n" + "For more information on a specific command, type\n" + "\002%s%s %s \037command\037\002.\n"), NickServ->nick.c_str(), Config->StrictPrivmsg.c_str(), NickServ->nick.c_str(), Config->StrictPrivmsg.c_str(), NickServ->nick.c_str(), source.command.c_str()); + else + source.Reply(_("\002%s\002 allows you to register an account.\n" + "The following commands allow for registration and maintenance of\n" + "accounts; to use them, type \002%s%s \037command\037\002.\n" + "For more information on a specific command, type\n" + "\002%s%s %s \037command\037\002.\n"), NickServ->nick.c_str(), Config->StrictPrivmsg.c_str(), NickServ->nick.c_str(), Config->StrictPrivmsg.c_str(), NickServ->nick.c_str(), source.command.c_str()); + return EVENT_CONTINUE; + } + + void OnPostHelp(CommandSource &source, const std::vector ¶ms) override + { + if (!params.empty() || source.c || source.service != *NickServ) + return; + if (source.IsServicesOper()) + source.Reply(_(" \n" + "Services Operators can also drop any nickname without needing\n" + "to identify for the nick, and may view the access list for\n" + "any nickname.")); + time_t nickserv_expire = Config->GetModule(this)->Get("expire", "90d"); + if (nickserv_expire >= 86400) + source.Reply(_(" \n" + "Accounts that are not used anymore are subject to\n" + "the automatic expiration, i.e. they will be deleted\n" + "after %lu days if not used."), (unsigned long)nickserv_expire / 86400); + } + + void OnNickCoreCreate(NickCore *nc) override + { + /* Set default flags */ + for (const auto &def : defaults) + nc->Extend(def.upper()); + } + + void OnUserQuit(User *u, const Anope::string &msg) override + { + if (u->server && !u->server->GetQuitReason().empty() && Config->GetModule(this)->Get("hidenetsplitquit")) + return; + + /* Update last quit and last seen for the user */ + NickAlias *na = NickAlias::Find(u->nick); + if (na && !na->nc->HasExt("NS_SUSPENDED") && (u->IsRecognized() || u->IsIdentified(true))) + { + na->last_seen = Anope::CurTime; + na->last_quit = msg; + } + } + + void OnExpireTick() override + { + if (Anope::NoExpire || Anope::ReadOnly) + return; + + time_t nickserv_expire = Config->GetModule(this)->Get("expire", "90d"); + + for (nickalias_map::const_iterator it = NickAliasList->begin(), it_end = NickAliasList->end(); it != it_end; ) + { + NickAlias *na = it->second; + ++it; + + User *u = User::Find(na->nick, true); + if (u && (u->IsIdentified(true) || u->IsRecognized())) + na->last_seen = Anope::CurTime; + + bool expire = false; + + if (nickserv_expire && Anope::CurTime - na->last_seen >= nickserv_expire) + expire = true; + + FOREACH_MOD(OnPreNickExpire, (na, expire)); + + if (expire) + { + Log(LOG_NORMAL, "nickserv/expire", NickServ) << "Expiring nickname " << na->nick << " (group: " << na->nc->display << ") (e-mail: " << (na->nc->email.empty() ? "none" : na->nc->email) << ")"; + FOREACH_MOD(OnNickExpire, (na)); + delete na; + } + } + } + + void OnNickInfo(CommandSource &source, NickAlias *na, InfoFormatter &info, bool show_hidden) override + { + if (!na->nc->HasExt("UNCONFIRMED")) + { + time_t nickserv_expire = Config->GetModule(this)->Get("expire", "90d"); + if (!na->HasExt("NS_NO_EXPIRE") && nickserv_expire && !Anope::NoExpire && (source.HasPriv("nickserv/auspex") || na->last_seen != Anope::CurTime)) + info[_("Expires")] = Anope::strftime(na->last_seen + nickserv_expire, source.GetAccount()); + } + else + { + time_t unconfirmed_expire = Config->GetModule("ns_register")->Get("unconfirmedexpire", "1d"); + info[_("Expires")] = Anope::strftime(na->time_registered + unconfirmed_expire, source.GetAccount()); + } + } +}; + +MODULE_INIT(NickServCore) diff --git a/modules/nickserv/ns_access.cpp b/modules/nickserv/ns_access.cpp new file mode 100644 index 000000000..1e0c99ce4 --- /dev/null +++ b/modules/nickserv/ns_access.cpp @@ -0,0 +1,208 @@ +/* NickServ 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 CommandNSAccess final + : public Command +{ +private: + void DoAdd(CommandSource &source, NickCore *nc, const Anope::string &mask) + { + if (mask.empty()) + { + this->OnSyntaxError(source, "ADD"); + return; + } + + if (Anope::ReadOnly) + { + source.Reply(READ_ONLY_MODE); + return; + } + + if (nc->access.size() >= Config->GetModule(this->owner)->Get("accessmax", "32")) + { + source.Reply(_("Sorry, the maximum of %d access entries has been reached."), Config->GetModule(this->owner)->Get("accessmax")); + return; + } + + if (nc->FindAccess(mask)) + { + source.Reply(_("Mask \002%s\002 already present on %s's access list."), mask.c_str(), nc->display.c_str()); + return; + } + + nc->AddAccess(mask); + Log(nc == source.GetAccount() ? LOG_COMMAND : LOG_ADMIN, source, this) << "to ADD mask " << mask << " to " << nc->display; + source.Reply(_("\002%s\002 added to %s's access list."), mask.c_str(), nc->display.c_str()); + + return; + } + + void DoDel(CommandSource &source, NickCore *nc, const Anope::string &mask) + { + if (mask.empty()) + { + this->OnSyntaxError(source, "DEL"); + return; + } + + if (Anope::ReadOnly) + { + source.Reply(READ_ONLY_MODE); + return; + } + + if (!nc->FindAccess(mask)) + { + source.Reply(_("\002%s\002 not found on %s's access list."), mask.c_str(), nc->display.c_str()); + return; + } + + nc->EraseAccess(mask); + Log(nc == source.GetAccount() ? LOG_COMMAND : LOG_ADMIN, source, this) << "to DELETE mask " << mask << " from " << nc->display; + source.Reply(_("\002%s\002 deleted from %s's access list."), mask.c_str(), nc->display.c_str()); + + return; + } + + void DoList(CommandSource &source, NickCore *nc, const Anope::string &mask) + { + unsigned i, end; + + if (nc->access.empty()) + { + source.Reply(_("%s's access list is empty."), nc->display.c_str()); + return; + } + + source.Reply(_("Access list for %s:"), nc->display.c_str()); + for (i = 0, end = nc->access.size(); i < end; ++i) + { + Anope::string access = nc->GetAccess(i); + if (!mask.empty() && !Anope::Match(access, mask)) + continue; + source.Reply(" %s", access.c_str()); + } + + return; + } +public: + CommandNSAccess(Module *creator) : Command(creator, "nickserv/access", 1, 3) + { + this->SetDesc(_("Modify the list of authorized addresses")); + this->SetSyntax(_("ADD [\037nickname\037] \037mask\037")); + this->SetSyntax(_("DEL [\037nickname\037] \037mask\037")); + this->SetSyntax(_("LIST [\037nickname\037]")); + } + + void Execute(CommandSource &source, const std::vector ¶ms) override + { + const Anope::string &cmd = params[0]; + Anope::string nick, mask; + + if (cmd.equals_ci("LIST")) + nick = params.size() > 1 ? params[1] : ""; + else + { + nick = params.size() == 3 ? params[1] : ""; + mask = params.size() > 1 ? params[params.size() - 1] : ""; + } + + NickCore *nc; + if (!nick.empty()) + { + const NickAlias *na = NickAlias::Find(nick); + if (na == NULL) + { + source.Reply(NICK_X_NOT_REGISTERED, nick.c_str()); + return; + } + else if (na->nc != source.GetAccount() && !source.HasPriv("nickserv/access")) + { + source.Reply(ACCESS_DENIED); + return; + } + else if (Config->GetModule("nickserv")->Get("secureadmins", "yes") && source.GetAccount() != na->nc && na->nc->IsServicesOper() && !cmd.equals_ci("LIST")) + { + source.Reply(_("You may view but not modify the access list of other Services Operators.")); + return; + } + + nc = na->nc; + } + else + nc = source.nc; + + if (!mask.empty() && (mask.find('@') == Anope::string::npos || mask.find('!') != Anope::string::npos)) + { + source.Reply(BAD_USERHOST_MASK); + source.Reply(MORE_INFO, Config->StrictPrivmsg.c_str(), source.service->nick.c_str(), source.command.c_str()); + } + else if (cmd.equals_ci("LIST")) + return this->DoList(source, nc, mask); + else if (nc->HasExt("NS_SUSPENDED")) + source.Reply(NICK_X_SUSPENDED, nc->display.c_str()); + else if (cmd.equals_ci("ADD")) + return this->DoAdd(source, nc, mask); + else if (cmd.equals_ci("DEL")) + return this->DoDel(source, nc, mask); + else + this->OnSyntaxError(source, ""); + } + + bool OnHelp(CommandSource &source, const Anope::string &subcommand) override + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("Modifies or displays the access list for your nick. This\n" + "is the list of addresses which will be automatically\n" + "recognized by %s as allowed to use the nick. If\n" + "you want to use the nick from a different address, you\n" + "need to send an \002IDENTIFY\002 command to make %s\n" + "recognize you. Services Operators may provide a nick\n" + "to modify other users' access lists.\n" + " \n" + "Examples:\n" + " \n" + " \002ACCESS ADD anyone@*.bepeg.com\002\n" + " Allows access to user \002anyone\002 from any machine in\n" + " the \002bepeg.com\002 domain.\n" + " \n" + " \002ACCESS DEL anyone@*.bepeg.com\002\n" + " Reverses the previous command.\n" + " \n" + " \002ACCESS LIST\002\n" + " Displays the current access list."), source.service->nick.c_str(), source.service->nick.c_str()); + return true; + } +}; + +class NSAccess final + : public Module +{ + CommandNSAccess commandnsaccess; + +public: + NSAccess(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), + commandnsaccess(this) + { + } + + void OnNickRegister(User *u, NickAlias *na, const Anope::string &) override + { + if (u && Config->GetModule(this)->Get("addaccessonreg")) + na->nc->AddAccess(u->Mask()); + } +}; + +MODULE_INIT(NSAccess) diff --git a/modules/nickserv/ns_ajoin.cpp b/modules/nickserv/ns_ajoin.cpp new file mode 100644 index 000000000..8f4b14112 --- /dev/null +++ b/modules/nickserv/ns_ajoin.cpp @@ -0,0 +1,408 @@ +/* NickServ 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" + +struct AJoinEntry; + +struct AJoinList final + : Serialize::Checker > +{ + AJoinList(Extensible *) : Serialize::Checker >("AJoinEntry") { } + ~AJoinList(); +}; + +struct AJoinEntry final + : Serializable +{ + Serialize::Reference owner; + Anope::string channel; + Anope::string key; + + AJoinEntry(Extensible *) : Serializable("AJoinEntry") { } + + ~AJoinEntry() override + { + AJoinList *channels = owner->GetExt("ajoinlist"); + if (channels) + { + std::vector::iterator it = std::find((*channels)->begin(), (*channels)->end(), this); + if (it != (*channels)->end()) + (*channels)->erase(it); + } + } + + void Serialize(Serialize::Data &sd) const override + { + if (!this->owner) + return; + + sd["owner"] << this->owner->display; + sd["channel"] << this->channel; + sd["key"] << this->key; + } + + static Serializable* Unserialize(Serializable *obj, Serialize::Data &sd) + { + Anope::string sowner; + + sd["owner"] >> sowner; + + NickCore *nc = NickCore::Find(sowner); + if (nc == NULL) + return NULL; + + AJoinEntry *aj; + if (obj) + aj = anope_dynamic_static_cast(obj); + else + { + aj = new AJoinEntry(nc); + aj->owner = nc; + } + + sd["channel"] >> aj->channel; + sd["key"] >> aj->key; + + if (!obj) + { + AJoinList *channels = nc->Require("ajoinlist"); + (*channels)->push_back(aj); + } + + return aj; + } +}; + +AJoinList::~AJoinList() +{ + for (const auto *ajoin : *(*this)) + delete ajoin; +} + +class CommandNSAJoin final + : public Command +{ + void DoList(CommandSource &source, NickCore *nc) + { + AJoinList *channels = nc->Require("ajoinlist"); + + if ((*channels)->empty()) + source.Reply(_("%s's auto join list is empty."), nc->display.c_str()); + else + { + ListFormatter list(source.GetAccount()); + list.AddColumn(_("Number")).AddColumn(_("Channel")).AddColumn(_("Key")); + for (unsigned i = 0; i < (*channels)->size(); ++i) + { + AJoinEntry *aj = (*channels)->at(i); + ListFormatter::ListEntry entry; + entry["Number"] = stringify(i + 1); + entry["Channel"] = aj->channel; + entry["Key"] = aj->key; + list.AddEntry(entry); + } + + source.Reply(_("%s's auto join list:"), nc->display.c_str()); + + std::vector replies; + list.Process(replies); + + for (const auto &reply : replies) + source.Reply(reply); + } + } + + void DoAdd(CommandSource &source, NickCore *nc, const Anope::string &chans, const Anope::string &keys) + { + AJoinList *channels = nc->Require("ajoinlist"); + + Anope::string addedchans; + Anope::string alreadyadded; + Anope::string invalidkey; + commasepstream ksep(keys, true); + commasepstream csep(chans); + for (Anope::string chan, key; csep.GetToken(chan);) + { + ksep.GetToken(key); + + unsigned i = 0; + for (; i < (*channels)->size(); ++i) + if ((*channels)->at(i)->channel.equals_ci(chan)) + break; + + if ((*channels)->size() >= Config->GetModule(this->owner)->Get("ajoinmax")) + { + source.Reply(_("Sorry, the maximum of %d auto join entries has been reached."), Config->GetModule(this->owner)->Get("ajoinmax")); + return; + } + else if (i != (*channels)->size()) + alreadyadded += chan + ", "; + else if (IRCD->IsChannelValid(chan) == false) + source.Reply(CHAN_X_INVALID, chan.c_str()); + else + { + Channel *c = Channel::Find(chan); + Anope::string k; + if (c && c->GetParam("KEY", k) && key != k) + { + invalidkey += chan + ", "; + continue; + } + + auto *entry = new AJoinEntry(nc); + entry->owner = nc; + entry->channel = chan; + entry->key = key; + (*channels)->push_back(entry); + addedchans += chan + ", "; + } + } + + if (!alreadyadded.empty()) + { + alreadyadded = alreadyadded.substr(0, alreadyadded.length() - 2); + source.Reply(_("%s is already on %s's auto join list."), alreadyadded.c_str(), nc->display.c_str()); + } + + if (!invalidkey.empty()) + { + invalidkey = invalidkey.substr(0, invalidkey.length() - 2); + source.Reply(_("%s had an invalid key specified, and was thus ignored."), invalidkey.c_str()); + } + + if (addedchans.empty()) + return; + + addedchans = addedchans.substr(0, addedchans.length() - 2); + Log(nc == source.GetAccount() ? LOG_COMMAND : LOG_ADMIN, source, this) << "to ADD channel " << addedchans << " to " << nc->display; + source.Reply(_("%s added to %s's auto join list."), addedchans.c_str(), nc->display.c_str()); + } + + void DoDel(CommandSource &source, NickCore *nc, const Anope::string &chans) + { + AJoinList *channels = nc->Require("ajoinlist"); + Anope::string delchans; + Anope::string notfoundchans; + commasepstream sep(chans); + + for (Anope::string chan; sep.GetToken(chan);) + { + unsigned i = 0; + for (; i < (*channels)->size(); ++i) + if ((*channels)->at(i)->channel.equals_ci(chan)) + break; + + if (i == (*channels)->size()) + notfoundchans += chan + ", "; + else + { + delete (*channels)->at(i); + delchans += chan + ", "; + } + } + + if (!notfoundchans.empty()) + { + notfoundchans = notfoundchans.substr(0, notfoundchans.length() - 2); + source.Reply(_("%s was not found on %s's auto join list."), notfoundchans.c_str(), nc->display.c_str()); + } + + if (delchans.empty()) + return; + + delchans = delchans.substr(0, delchans.length() - 2); + Log(nc == source.GetAccount() ? LOG_COMMAND : LOG_ADMIN, source, this) << "to DELETE channel " << delchans << " from " << nc->display; + source.Reply(_("%s was removed from %s's auto join list."), delchans.c_str(), nc->display.c_str()); + + if ((*channels)->empty()) + nc->Shrink("ajoinlist"); + } + +public: + CommandNSAJoin(Module *creator) : Command(creator, "nickserv/ajoin", 1, 4) + { + this->SetDesc(_("Manage your auto join list")); + this->SetSyntax(_("ADD [\037nickname\037] \037channel\037 [\037key\037]")); + this->SetSyntax(_("DEL [\037nickname\037] \037channel\037")); + this->SetSyntax(_("LIST [\037nickname\037]")); + } + + void Execute(CommandSource &source, const std::vector ¶ms) override + { + const Anope::string &cmd = params[0]; + Anope::string nick, param, param2; + + if (cmd.equals_ci("LIST")) + nick = params.size() > 1 ? params[1] : ""; + else + nick = (params.size() > 2 && IRCD->IsChannelValid(params[2])) ? params[1] : ""; + + NickCore *nc; + if (!nick.empty()) + { + const NickAlias *na = NickAlias::Find(nick); + if (na == NULL) + { + source.Reply(NICK_X_NOT_REGISTERED, nick.c_str()); + return; + } + else if (na->nc != source.GetAccount() && !source.HasCommand("nickserv/ajoin")) + { + source.Reply(ACCESS_DENIED); + return; + } + + nc = na->nc; + param = params.size() > 2 ? params[2] : ""; + param2 = params.size() > 3 ? params[3] : ""; + } + else + { + nc = source.nc; + param = params.size() > 1 ? params[1] : ""; + param2 = params.size() > 2 ? params[2] : ""; + } + + if (cmd.equals_ci("LIST")) + return this->DoList(source, nc); + else if (nc->HasExt("NS_SUSPENDED")) + source.Reply(NICK_X_SUSPENDED, nc->display.c_str()); + else if (param.empty()) + this->OnSyntaxError(source, ""); + else if (Anope::ReadOnly) + source.Reply(READ_ONLY_MODE); + else if (cmd.equals_ci("ADD")) + return this->DoAdd(source, nc, param, param2); + else if (cmd.equals_ci("DEL")) + return this->DoDel(source, nc, param); + else + this->OnSyntaxError(source, ""); + } + + bool OnHelp(CommandSource &source, const Anope::string &subcommand) override + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("This command manages your auto join list. When you identify\n" + "you will automatically join the channels on your auto join list.\n" + "Services Operators may provide a nick to modify other users'\n" + "auto join lists.")); + return true; + } +}; + +class NSAJoin final + : public Module +{ + CommandNSAJoin commandnsajoin; + ExtensibleItem ajoinlist; + Serialize::Type ajoinentry_type; + +public: + NSAJoin(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), + commandnsajoin(this), ajoinlist(this, "ajoinlist"), + ajoinentry_type("AJoinEntry", AJoinEntry::Unserialize) + { + + if (!IRCD || !IRCD->CanSVSJoin) + throw ModuleException("Your IRCd does not support SVSJOIN"); + + } + + void OnUserLogin(User *u) override + { + BotInfo *NickServ = Config->GetClient("NickServ"); + if (!NickServ) + return; + + AJoinList *channels = u->Account()->GetExt("ajoinlist"); + if (channels == NULL) + return; + + /* Set +r now, so we can ajoin users into +R channels */ + ModeManager::ProcessModes(); + + for (auto *entry : *(*channels)) + { + Channel *c = Channel::Find(entry->channel); + ChannelInfo *ci; + + if (c) + ci = c->ci; + else + ci = ChannelInfo::Find(entry->channel); + + bool need_invite = false; + Anope::string key = entry->key; + AccessGroup u_access; + + if (ci != NULL) + { + if (ci->HasExt("CS_SUSPENDED")) + continue; + u_access = ci->AccessFor(u); + } + if (c != NULL) + { + if (c->FindUser(u) != NULL) + continue; + else if (c->HasMode("OPERONLY") && !u->HasMode("OPER")) + continue; + else if (c->HasMode("ADMINONLY") && !u->HasMode("ADMIN")) + continue; + else if (c->HasMode("SSL") && !u->IsSecurelyConnected()) + continue; + else if (c->MatchesList(u, "BAN") == true && c->MatchesList(u, "EXCEPT") == false) + need_invite = true; + else if (c->HasMode("INVITE") && c->MatchesList(u, "INVITEOVERRIDE") == false) + need_invite = true; + + if (c->HasMode("KEY")) + { + Anope::string k; + if (c->GetParam("KEY", k)) + { + if (u_access.HasPriv("GETKEY")) + key = k; + else if (key != k) + need_invite = true; + } + } + if (c->HasMode("LIMIT")) + { + Anope::string l; + if (c->GetParam("LIMIT", l)) + { + try + { + unsigned limit = convertTo(l); + if (c->users.size() >= limit) + need_invite = true; + } + catch (const ConvertException &) { } + } + } + } + + if (need_invite && c != NULL) + { + if (!u_access.HasPriv("INVITE")) + continue; + IRCD->SendInvite(NickServ, c, u); + } + + IRCD->SendSVSJoin(NickServ, u, entry->channel, key); + } + } +}; + +MODULE_INIT(NSAJoin) diff --git a/modules/nickserv/ns_alist.cpp b/modules/nickserv/ns_alist.cpp new file mode 100644 index 000000000..e4adfb870 --- /dev/null +++ b/modules/nickserv/ns_alist.cpp @@ -0,0 +1,148 @@ +/* NickServ 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 CommandNSAList final + : public Command +{ + static bool ChannelSort(ChannelInfo *ci1, ChannelInfo *ci2) + { + return ci::less()(ci1->name, ci2->name); + } + +public: + CommandNSAList(Module *creator) : Command(creator, "nickserv/alist", 0, 2) + { + this->SetDesc(_("List channels you have access on")); + this->SetSyntax(_("[\037nickname\037]")); + } + + void Execute(CommandSource &source, const std::vector ¶ms) override + { + Anope::string nick = source.GetNick(); + NickCore *nc = source.nc; + + if (params.size() && source.HasPriv("nickserv/alist")) + { + nick = params[0]; + const NickAlias *na = NickAlias::Find(nick); + if (!na) + { + source.Reply(NICK_X_NOT_REGISTERED, nick.c_str()); + return; + } + nc = na->nc; + } + + ListFormatter list(source.GetAccount()); + int chan_count = 0; + + list.AddColumn(_("Number")).AddColumn(_("Channel")).AddColumn(_("Access")).AddColumn(_("Description")); + + std::deque queue; + nc->GetChannelReferences(queue); + std::sort(queue.begin(), queue.end(), ChannelSort); + + for (auto *ci : queue) + { + ListFormatter::ListEntry entry; + + if (ci->GetFounder() == nc) + { + ++chan_count; + entry["Number"] = stringify(chan_count); + entry["Channel"] = (ci->HasExt("CS_NO_EXPIRE") ? "!" : "") + ci->name; + entry["Access"] = Language::Translate(source.GetAccount(), _("Founder")); + entry["Description"] = ci->desc; + list.AddEntry(entry); + continue; + } + + if (ci->GetSuccessor() == nc) + { + ++chan_count; + entry["Number"] = stringify(chan_count); + entry["Channel"] = (ci->HasExt("CS_NO_EXPIRE") ? "!" : "") + ci->name; + entry["Access"] = Language::Translate(source.GetAccount(), _("Successor")); + entry["Description"] = ci->desc; + list.AddEntry(entry); + continue; + } + + AccessGroup access = ci->AccessFor(nc, false); + if (access.empty()) + continue; + + ++chan_count; + + entry["Number"] = stringify(chan_count); + entry["Channel"] = (ci->HasExt("CS_NO_EXPIRE") ? "!" : "") + ci->name; + for (auto &p : access.paths) + { + // not interested in indirect access + if (p.size() != 1) + continue; + + ChanAccess *a = p[0]; + entry["Access"] = entry["Access"] + ", " + a->AccessSerialize(); + } + entry["Access"] = entry["Access"].substr(2); + entry["Description"] = ci->desc; + list.AddEntry(entry); + } + + std::vector replies; + list.Process(replies); + + if (!chan_count) + { + source.Reply(_("\002%s\002 has no access in any channels."), nc->display.c_str()); + } + else + { + source.Reply(_("Channels that \002%s\002 has access on:"), nc->display.c_str()); + + for (const auto &reply : replies) + source.Reply(reply); + + source.Reply(_("End of list - %d channels shown."), chan_count); + } + } + + bool OnHelp(CommandSource &source, const Anope::string &subcommand) override + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("Lists all channels you have access on.\n" + " \n" + "Channels that have the \037NOEXPIRE\037 option set will be\n" + "prefixed by an exclamation mark. The nickname parameter is\n" + "limited to Services Operators")); + + return true; + } +}; + +class NSAList final + : public Module +{ + CommandNSAList commandnsalist; + +public: + NSAList(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), + commandnsalist(this) + { + + } +}; + +MODULE_INIT(NSAList) diff --git a/modules/nickserv/ns_cert.cpp b/modules/nickserv/ns_cert.cpp new file mode 100644 index 000000000..389b49cf5 --- /dev/null +++ b/modules/nickserv/ns_cert.cpp @@ -0,0 +1,412 @@ +/* NickServ 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/ns_cert.h" + +static Anope::hash_map certmap; + +struct CertServiceImpl final + : CertService +{ + CertServiceImpl(Module *o) : CertService(o) { } + + NickCore* FindAccountFromCert(const Anope::string &cert) override + { + Anope::hash_map::iterator it = certmap.find(cert); + if (it != certmap.end()) + return it->second; + return NULL; + } +}; + +struct NSCertListImpl final + : NSCertList +{ + Serialize::Reference nc; + std::vector certs; + +public: + NSCertListImpl(Extensible *obj) : nc(anope_dynamic_static_cast(obj)) { } + + ~NSCertListImpl() override + { + ClearCert(); + } + + /** Add an entry to the nick's certificate list + * + * @param entry The fingerprint to add to the cert list + * + * Adds a new entry into the cert list. + */ + void AddCert(const Anope::string &entry) override + { + this->certs.push_back(entry); + certmap[entry] = nc; + FOREACH_MOD(OnNickAddCert, (this->nc, entry)); + } + + /** Get an entry from the nick's cert list by index + * + * @param entry Index in the certificate list vector to retrieve + * @return The fingerprint entry of the given index if within bounds, an empty string if the vector is empty or the index is out of bounds + * + * Retrieves an entry from the certificate list corresponding to the given index. + */ + Anope::string GetCert(unsigned entry) const override + { + if (entry >= this->certs.size()) + return ""; + return this->certs[entry]; + } + + unsigned GetCertCount() const override + { + return this->certs.size(); + } + + /** Find an entry in the nick's cert list + * + * @param entry The fingerprint to search for + * @return True if the fingerprint is found in the cert list, false otherwise + * + * Search for an fingerprint within the cert list. + */ + bool FindCert(const Anope::string &entry) const override + { + return std::find(this->certs.begin(), this->certs.end(), entry) != this->certs.end(); + } + + /** Erase a fingerprint from the nick's certificate list + * + * @param entry The fingerprint to remove + * + * Removes the specified fingerprint from the cert list. + */ + void EraseCert(const Anope::string &entry) override + { + std::vector::iterator it = std::find(this->certs.begin(), this->certs.end(), entry); + if (it != this->certs.end()) + { + FOREACH_MOD(OnNickEraseCert, (this->nc, entry)); + certmap.erase(entry); + this->certs.erase(it); + } + } + + /** Clears the entire nick's cert list + * + * Deletes all the memory allocated in the certificate list vector and then clears the vector. + */ + void ClearCert() override + { + FOREACH_MOD(OnNickClearCert, (this->nc)); + for (const auto &cert : certs) + certmap.erase(cert); + this->certs.clear(); + } + + void Check() override + { + if (this->certs.empty()) + nc->Shrink("certificates"); + } + + struct ExtensibleItem final + : ::ExtensibleItem + { + ExtensibleItem(Module *m, const Anope::string &ename) : ::ExtensibleItem(m, ename) { } + + void ExtensibleSerialize(const Extensible *e, const Serializable *s, Serialize::Data &data) const override + { + if (s->GetSerializableType()->GetName() != "NickCore") + return; + + const NickCore *n = anope_dynamic_static_cast(e); + NSCertList *c = this->Get(n); + if (c == NULL || !c->GetCertCount()) + return; + + for (unsigned i = 0; i < c->GetCertCount(); ++i) + data["cert"] << c->GetCert(i) << " "; + } + + void ExtensibleUnserialize(Extensible *e, Serializable *s, Serialize::Data &data) override + { + if (s->GetSerializableType()->GetName() != "NickCore") + return; + + NickCore *n = anope_dynamic_static_cast(e); + NSCertListImpl *c = this->Require(n); + + Anope::string buf; + data["cert"] >> buf; + spacesepstream sep(buf); + for (const auto &cert : c->certs) + certmap.erase(cert); + c->certs.clear(); + while (sep.GetToken(buf)) + { + c->certs.push_back(buf); + certmap[buf] = n; + } + } + }; +}; + +class CommandNSCert final + : public Command +{ +private: + void DoAdd(CommandSource &source, NickCore *nc, Anope::string certfp) + { + NSCertList *cl = nc->Require("certificates"); + unsigned max = Config->GetModule(this->owner)->Get("max", "5"); + + if (cl->GetCertCount() >= max) + { + source.Reply(_("Sorry, the maximum of %d certificate entries has been reached."), max); + return; + } + + if (source.GetAccount() == nc) + { + User *u = source.GetUser(); + + if (!u || u->fingerprint.empty()) + { + source.Reply(_("You are not using a client certificate.")); + return; + } + + certfp = u->fingerprint; + } + + if (cl->FindCert(certfp)) + { + source.Reply(_("Fingerprint \002%s\002 already present on %s's certificate list."), certfp.c_str(), nc->display.c_str()); + return; + } + + if (certmap.find(certfp) != certmap.end()) + { + source.Reply(_("Fingerprint \002%s\002 is already in use."), certfp.c_str()); + return; + } + + cl->AddCert(certfp); + Log(nc == source.GetAccount() ? LOG_COMMAND : LOG_ADMIN, source, this) << "to ADD certificate fingerprint " << certfp << " to " << nc->display; + source.Reply(_("\002%s\002 added to %s's certificate list."), certfp.c_str(), nc->display.c_str()); + } + + void DoDel(CommandSource &source, NickCore *nc, Anope::string certfp) + { + NSCertList *cl = nc->Require("certificates"); + + if (certfp.empty()) + { + User *u = source.GetUser(); + if (u) + certfp = u->fingerprint; + } + + if (certfp.empty()) + { + this->OnSyntaxError(source, "DEL"); + return; + } + + if (!cl->FindCert(certfp)) + { + source.Reply(_("\002%s\002 not found on %s's certificate list."), certfp.c_str(), nc->display.c_str()); + return; + } + + cl->EraseCert(certfp); + cl->Check(); + Log(nc == source.GetAccount() ? LOG_COMMAND : LOG_ADMIN, source, this) << "to DELETE certificate fingerprint " << certfp << " from " << nc->display; + source.Reply(_("\002%s\002 deleted from %s's certificate list."), certfp.c_str(), nc->display.c_str()); + } + + void DoList(CommandSource &source, const NickCore *nc) + { + NSCertList *cl = nc->GetExt("certificates"); + + if (!cl || !cl->GetCertCount()) + { + source.Reply(_("%s's certificate list is empty."), nc->display.c_str()); + return; + } + + source.Reply(_("Certificate list for %s:"), nc->display.c_str()); + for (unsigned i = 0; i < cl->GetCertCount(); ++i) + { + Anope::string fingerprint = cl->GetCert(i); + source.Reply(" %s", fingerprint.c_str()); + } + } + +public: + CommandNSCert(Module *creator) : Command(creator, "nickserv/cert", 1, 3) + { + this->SetDesc(_("Modify the nickname client certificate list")); + this->SetSyntax(_("ADD [\037nickname\037] [\037fingerprint\037]")); + this->SetSyntax(_("DEL [\037nickname\037] \037fingerprint\037")); + this->SetSyntax(_("LIST [\037nickname\037]")); + } + + void Execute(CommandSource &source, const std::vector ¶ms) override + { + const Anope::string &cmd = params[0]; + Anope::string nick, certfp; + + if (cmd.equals_ci("LIST")) + nick = params.size() > 1 ? params[1] : ""; + else + { + nick = params.size() == 3 ? params[1] : ""; + certfp = params.size() > 1 ? params[params.size() - 1] : ""; + } + + NickCore *nc; + if (!nick.empty()) + { + const NickAlias *na = NickAlias::Find(nick); + if (na == NULL) + { + source.Reply(NICK_X_NOT_REGISTERED, nick.c_str()); + return; + } + else if (na->nc != source.GetAccount() && !source.HasPriv("nickserv/access")) + { + source.Reply(ACCESS_DENIED); + return; + } + else if (Config->GetModule("nickserv")->Get("secureadmins", "yes") && source.GetAccount() != na->nc && na->nc->IsServicesOper() && !cmd.equals_ci("LIST")) + { + source.Reply(_("You may view but not modify the certificate list of other Services Operators.")); + return; + } + + nc = na->nc; + } + else + nc = source.nc; + + if (cmd.equals_ci("LIST")) + return this->DoList(source, nc); + else if (nc->HasExt("NS_SUSPENDED")) + source.Reply(NICK_X_SUSPENDED, nc->display.c_str()); + else if (Anope::ReadOnly) + source.Reply(READ_ONLY_MODE); + else if (cmd.equals_ci("ADD")) + return this->DoAdd(source, nc, certfp); + else if (cmd.equals_ci("DEL")) + return this->DoDel(source, nc, certfp); + else + this->OnSyntaxError(source, ""); + } + + bool OnHelp(CommandSource &source, const Anope::string &subcommand) override + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("Modifies or displays the certificate list for your nick.\n" + "If you connect to IRC and provide a client certificate with a\n" + "matching fingerprint in the cert list, you will be\n" + "automatically identified to services. Services Operators\n" + "may provide a nick to modify other users' certificate lists.\n" + " \n")); + source.Reply(_("Examples:\n" + " \n" + " \002CERT ADD\002\n" + " Adds your current fingerprint to the certificate list and\n" + " automatically identifies you when you connect to IRC\n" + " using this fingerprint.\n" + " \n" + " \002CERT DEL \002\n" + " Removes the fingerprint from your certificate list.\n" + " \n" + " \002CERT LIST\002\n" + " Displays the current certificate list.")); + return true; + } +}; + +class NSCert final + : public Module +{ + CommandNSCert commandnscert; + NSCertListImpl::ExtensibleItem certs; + CertServiceImpl cs; + +public: + NSCert(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), + commandnscert(this), certs(this, "certificates"), cs(this) + { + if (!IRCD || !IRCD->CanCertFP) + throw ModuleException("Your IRCd does not support ssl client certificates"); + } + + void OnFingerprint(User *u) override + { + BotInfo *NickServ = Config->GetClient("NickServ"); + if (!NickServ || u->IsIdentified()) + return; + + NickCore *nc = cs.FindAccountFromCert(u->fingerprint); + if (!nc || nc->HasExt("NS_SUSPENDED")) + return; + + unsigned int maxlogins = Config->GetModule("ns_identify")->Get("maxlogins"); + if (maxlogins && nc->users.size() >= maxlogins) + { + u->SendMessage(NickServ, _("Account \002%s\002 has already reached the maximum number of simultaneous logins (%u)."), nc->display.c_str(), maxlogins); + return; + } + + NickAlias *na = NickAlias::Find(u->nick); + if (na && na->nc == nc) + u->Identify(na); + else + u->Login(nc); + + u->SendMessage(NickServ, _("SSL certificate fingerprint accepted, you are now identified to \002%s\002."), nc->display.c_str()); + Log(NickServ) << u->GetMask() << " automatically identified for account " << nc->display << " via SSL certificate fingerprint"; + } + + EventReturn OnNickValidate(User *u, NickAlias *na) override + { + NSCertList *cl = certs.Get(na->nc); + if (!u->fingerprint.empty() && cl && cl->FindCert(u->fingerprint)) + { + BotInfo *NickServ = Config->GetClient("NickServ"); + + unsigned int maxlogins = Config->GetModule("ns_identify")->Get("maxlogins"); + if (maxlogins && na->nc->users.size() >= maxlogins) + { + u->SendMessage(NickServ, _("Account \002%s\002 has already reached the maximum number of simultaneous logins (%u)."), na->nc->display.c_str(), maxlogins); + return EVENT_CONTINUE; + } + + u->Identify(na); + + u->SendMessage(NickServ, _("SSL certificate fingerprint accepted, you are now identified.")); + Log(NickServ) << u->GetMask() << " automatically identified for account " << na->nc->display << " via SSL certificate fingerprint"; + return EVENT_ALLOW; + } + + return EVENT_CONTINUE; + } +}; + +MODULE_INIT(NSCert) diff --git a/modules/nickserv/ns_drop.cpp b/modules/nickserv/ns_drop.cpp new file mode 100644 index 000000000..dbdbe09be --- /dev/null +++ b/modules/nickserv/ns_drop.cpp @@ -0,0 +1,88 @@ +/* NickServ 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 CommandNSDrop final + : public Command +{ +public: + CommandNSDrop(Module *creator) : Command(creator, "nickserv/drop", 1, 1) + { + this->SetSyntax(_("\037nickname\037")); + this->SetDesc(_("Cancel the registration of a nickname")); + } + + void Execute(CommandSource &source, const std::vector ¶ms) override + { + const Anope::string &nick = params[0]; + + if (Anope::ReadOnly && !source.HasPriv("nickserv/drop")) + { + source.Reply(_("Sorry, nickname de-registration is temporarily disabled.")); + return; + } + + NickAlias *na = NickAlias::Find(nick); + if (!na) + { + source.Reply(NICK_X_NOT_REGISTERED, nick.c_str()); + return; + } + + bool is_mine = source.GetAccount() == na->nc; + + if (!is_mine && !source.HasPriv("nickserv/drop")) + source.Reply(ACCESS_DENIED); + else if (Config->GetModule("nickserv")->Get("secureadmins", "yes") && !is_mine && na->nc->IsServicesOper()) + source.Reply(_("You may not drop other Services Operators' nicknames.")); + else + { + FOREACH_MOD(OnNickDrop, (source, na)); + + Log(!is_mine ? LOG_ADMIN : LOG_COMMAND, source, this) << "to drop nickname " << na->nick << " (group: " << na->nc->display << ") (email: " << (!na->nc->email.empty() ? na->nc->email : "none") << ")"; + delete na; + + source.Reply(_("Nickname \002%s\002 has been dropped."), nick.c_str()); + } + } + + bool OnHelp(CommandSource &source, const Anope::string &subcommand) override + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("Drops the given nick from the database. Once your nickname\n" + "is dropped you may lose all of your access and channels that\n" + "you may own. Any other user will be able to gain control of\n" + "this nick.")); + if (!source.HasPriv("nickserv/drop")) + source.Reply(_("You may drop any nick within your group.")); + else + source.Reply(_("As a Services Operator, you may drop any nick.")); + + return true; + } +}; + +class NSDrop final + : public Module +{ + CommandNSDrop commandnsdrop; + +public: + NSDrop(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), + commandnsdrop(this) + { + + } +}; + +MODULE_INIT(NSDrop) diff --git a/modules/nickserv/ns_getemail.cpp b/modules/nickserv/ns_getemail.cpp new file mode 100644 index 000000000..e85fc12d6 --- /dev/null +++ b/modules/nickserv/ns_getemail.cpp @@ -0,0 +1,74 @@ +/* NickServ 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. + * + * A simple call to check for all emails that a user may have registered + * with. It returns the nicks that match the email you provide. Wild + * Cards are not excepted. Must use user@email-host. + */ + +#include "module.h" + +class CommandNSGetEMail final + : public Command +{ +public: + CommandNSGetEMail(Module *creator) : Command(creator, "nickserv/getemail", 1, 1) + { + this->SetDesc(_("Matches and returns all users that registered using given email")); + this->SetSyntax(_("\037email\037")); + } + + void Execute(CommandSource &source, const std::vector ¶ms) override + { + const Anope::string &email = params[0]; + int j = 0; + + Log(LOG_ADMIN, source, this) << "on " << email; + + for (const auto &[_, nc] : *NickCoreList) + { + if (!nc->email.empty() && Anope::Match(nc->email, email)) + { + ++j; + source.Reply(_("Email matched: \002%s\002 (\002%s\002) to \002%s\002."), nc->display.c_str(), nc->email.c_str(), email.c_str()); + } + } + + if (j <= 0) + { + source.Reply(_("No registrations matching \002%s\002 were found."), email.c_str()); + return; + } + + return; + } + + bool OnHelp(CommandSource &source, const Anope::string &subcommand) override + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("Returns the matching accounts that used given email.")); + return true; + } +}; + +class NSGetEMail final + : public Module +{ + CommandNSGetEMail commandnsgetemail; +public: + NSGetEMail(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR), + commandnsgetemail(this) + { + + } +}; + +MODULE_INIT(NSGetEMail) diff --git a/modules/nickserv/ns_group.cpp b/modules/nickserv/ns_group.cpp new file mode 100644 index 000000000..2dfaeb9fc --- /dev/null +++ b/modules/nickserv/ns_group.cpp @@ -0,0 +1,399 @@ +/* NickServ 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/ns_cert.h" + +class NSGroupRequest final + : public IdentifyRequest +{ + CommandSource source; + Command *cmd; + Anope::string nick; + Reference target; + +public: + NSGroupRequest(Module *o, CommandSource &src, Command *c, const Anope::string &n, NickAlias *targ, const Anope::string &pass) : IdentifyRequest(o, targ->nc->display, pass), source(src), cmd(c), nick(n), target(targ) { } + + void OnSuccess() override + { + User *u = source.GetUser(); + + /* user changed nick? */ + if (u != NULL && u->nick != nick) + return; + + if (!target || !target->nc) + return; + + NickAlias *na = NickAlias::Find(nick); + /* If the nick is already registered, drop it. */ + if (na) + { + delete na; + } + + na = new NickAlias(nick, target->nc); + na->time_registered = na->last_seen = Anope::CurTime; + + if (u != NULL) + { + na->last_usermask = u->GetIdent() + "@" + u->GetDisplayedHost(); + na->last_realname = u->realname; + } + else + { + na->last_realname = source.GetNick(); + } + + if (u != NULL) + { + IRCD->SendLogin(u, na); // protocol modules prevent this on unconfirmed accounts + u->Login(target->nc); + FOREACH_MOD(OnNickGroup, (u, target)); + } + + Log(LOG_COMMAND, source, cmd) << "to make " << nick << " join group of " << target->nick << " (" << target->nc->display << ") (email: " << (!target->nc->email.empty() ? target->nc->email : "none") << ")"; + source.Reply(_("You are now in the group of \002%s\002."), target->nick.c_str()); + + if (u) + u->lastnickreg = Anope::CurTime; + } + + void OnFail() override + { + User *u = source.GetUser(); + + Log(LOG_COMMAND, source, cmd) << "and failed to group to " << target->nick; + if (NickAlias::Find(GetAccount()) != NULL) + { + source.Reply(PASSWORD_INCORRECT); + if (u) + u->BadPassword(); + } + else + source.Reply(NICK_X_NOT_REGISTERED, GetAccount().c_str()); + } +}; + +class CommandNSGroup final + : public Command +{ +public: + CommandNSGroup(Module *creator) : Command(creator, "nickserv/group", 0, 2) + { + this->SetDesc(_("Join a group")); + this->SetSyntax(_("\037[target]\037 \037[password]\037")); + this->AllowUnregistered(true); + } + + void Execute(CommandSource &source, const std::vector ¶ms) override + { + User *user = source.GetUser(); + + Anope::string nick; + if (params.empty()) + { + NickCore* core = source.GetAccount(); + if (core) + nick = core->display; + } + else + nick = params[0]; + + if (nick.empty()) + { + this->SendSyntax(source); + return; + } + + const Anope::string &pass = params.size() > 1 ? params[1] : ""; + + if (Anope::ReadOnly) + { + source.Reply(_("Sorry, nickname grouping is temporarily disabled.")); + return; + } + + if (!IRCD->IsNickValid(source.GetNick())) + { + source.Reply(NICK_CANNOT_BE_REGISTERED, source.GetNick().c_str()); + return; + } + + if (Config->GetModule("nickserv")->Get("restrictopernicks")) + { + for (auto *o : Oper::opers) + { + if (user != NULL && !user->HasMode("OPER") && user->nick.find_ci(o->name) != Anope::string::npos) + { + source.Reply(NICK_CANNOT_BE_REGISTERED, user->nick.c_str()); + return; + } + } + } + + NickAlias *target, *na = NickAlias::Find(source.GetNick()); + const Anope::string &guestnick = Config->GetModule("nickserv")->Get("guestnickprefix", "Guest"); + time_t reg_delay = Config->GetModule("nickserv")->Get("regdelay"); + unsigned maxaliases = Config->GetModule(this->owner)->Get("maxaliases"); + if (!(target = NickAlias::Find(nick))) + source.Reply(NICK_X_NOT_REGISTERED, nick.c_str()); + else if (user && Anope::CurTime < user->lastnickreg + reg_delay) + source.Reply(_("Please wait %lu seconds before using the GROUP command again."), (unsigned long)(reg_delay + user->lastnickreg) - Anope::CurTime); + else if (target->nc->HasExt("NS_SUSPENDED")) + { + Log(LOG_COMMAND, source, this) << "and tried to group to SUSPENDED nick " << target->nick; + source.Reply(NICK_X_SUSPENDED, target->nick.c_str()); + } + else if (na && Config->GetModule(this->owner)->Get("nogroupchange")) + source.Reply(_("Your nick is already registered.")); + else if (na && *target->nc == *na->nc) + source.Reply(_("You are already a member of the group of \002%s\002."), target->nick.c_str()); + else if (na && na->nc != source.GetAccount()) + source.Reply(NICK_IDENTIFY_REQUIRED); + else if (maxaliases && target->nc->aliases->size() >= maxaliases && !target->nc->IsServicesOper()) + source.Reply(_("There are too many nicks in your group.")); + else if (source.GetNick().length() <= guestnick.length() + 7 && + source.GetNick().length() >= guestnick.length() + 1 && + !source.GetNick().find_ci(guestnick) && !source.GetNick().substr(guestnick.length()).find_first_not_of("1234567890")) + { + source.Reply(NICK_CANNOT_BE_REGISTERED, source.GetNick().c_str()); + } + else + { + bool ok = false; + if (!na && source.GetAccount() == target->nc) + ok = true; + + NSCertList *cl = target->nc->GetExt("certificates"); + if (user != NULL && !user->fingerprint.empty() && cl && cl->FindCert(user->fingerprint)) + ok = true; + + if (ok == false && !pass.empty()) + { + auto *req = new NSGroupRequest(owner, source, this, source.GetNick(), target, pass); + FOREACH_MOD(OnCheckAuthentication, (source.GetUser(), req)); + req->Dispatch(); + } + else + { + NSGroupRequest req(owner, source, this, source.GetNick(), target, pass); + + if (ok) + req.OnSuccess(); + else + req.OnFail(); + } + } + } + + bool OnHelp(CommandSource &source, const Anope::string &subcommand) override + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("This command makes your nickname join the \037target\037 nickname's\n" + "group. \037password\037 is the password of the target nickname.\n" + " \n" + "Joining a group will allow you to share your configuration,\n" + "memos, and channel privileges with all the nicknames in the\n" + "group, and much more!\n" + " \n" + "A group exists as long as it is useful. This means that even\n" + "if a nick of the group is dropped, you won't lose the\n" + "shared things described above, as long as there is at\n" + "least one nick remaining in the group.\n" + " \n" + "You may be able to use this command even if you have not registered\n" + "your nick yet. If your nick is already registered, you'll\n" + "need to identify yourself be