Merge the cloak_account and cloak_nick modules.

These modules are 90% identical and were only separate because
before commit 78cd3898f5 we could not conditionally regenerate
cloaks.
This commit is contained in:
Sadie Powell 2023-06-20 15:29:17 +01:00
parent d866bd8791
commit 7f6ffa0bda
3 changed files with 124 additions and 255 deletions

View File

@ -516,47 +516,6 @@
# load the conn_umodes module and add "x" to <connect:modes>.
#<module name="cloak">
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# Account cloak module: Adds the "account" (services account name) and
# "account-id" (services account identifier) cloak methods.
#<module name="cloak_account">
#
#-#-#-#-#-#-#-#-#-#- ACCOUNT CLOAK CONFIGURATION -#-#-#-#-#-#-#-#-#-#-#
# To use the cloak_account module you must define a <cloak> tag. This #
# tag can have the following fields. #
# #
# class - If non-empty then a comma-delimited list of connect #
# class names that a user has to be in to get the cloak #
# from this tag. #
# #
# prefix - A freeform value to prefix cloaks with. This must not #
# contain spaces. #
# #
# suffix - A freeform value to suffix IPv4/IPv6 cloaks with. This #
# must not contain spaces. #
# #
# sanitize - If enabled then any characters in the account name/id #
# that are not valid in a hostname will be removed rather #
# than skipping the cloak method. Defaults to yes. #
# #
# IMPORTANT: Changing these details will break all of your existing #
# bans. If you do not want this to happen you can define multiple #
# cloak tags. The first will be used for hostnames and the rest will #
# be used for checking if a user is banned in a channel. #
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#
#<cloak method="account"
# class=""
# prefix="MyNet"
# suffix="ip"
# sanitize="yes">
#
#<cloak method="account-id"
# class=""
# prefix="MyNet"
# suffix="ip"
# sanitize="yes">
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# MD5 cloak module: Adds the "half" and "full" cloak methods. These
# methods are obsolete and should only be used on a network which is
@ -612,40 +571,6 @@
# prefix="MyNet-"
# suffix=".IP">
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# Nickname cloak module: Adds the "nick" (user nickname) cloak method.
#<module name="cloak_nick">
#
#-#-#-#-#-#-#-#-#-#- ACCOUNT CLOAK CONFIGURATION -#-#-#-#-#-#-#-#-#-#-#
# To use the cloak_nick module you must define a <cloak> tag. This #
# tag can have the following fields. #
# #
# class - If non-empty then a comma-delimited list of connect #
# class names that a user has to be in to get the cloak #
# from this tag. #
# #
# prefix - A freeform value to prefix cloaks with. This must not #
# contain spaces. #
# #
# suffix - A freeform value to suffix cloaks with. This must not #
# contain spaces. #
# #
# sanitize - If enabled then any characters in the nickname that are #
# not valid in a hostname will be removed rather than #
# than skipping the cloak method. #
# #
# IMPORTANT: Changing these details will break all of your existing #
# bans. If you do not want this to happen you can define multiple #
# cloak tags. The first will be used for hostnames and the rest will #
# be used for checking if a user is banned in a channel. #
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#
#<cloak method="nick"
# class=""
# prefix="MyNet"
# suffix="ip"
# sanitize="yes">
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# HMAC-SHA256 cloak module: Adds the "hmac-sha256" (hostname or IP) and
# "hmac-sha256-ip" (IP only) cloak methods. This is the recommended
@ -715,7 +640,7 @@
# Static cloak module: Adds the "static" (fixed value) cloak method.
#<module name="cloak_static">
#
#-#-#-#-#-#-#-#-#-#- ACCOUNT CLOAK CONFIGURATION -#-#-#-#-#-#-#-#-#-#-#
#-#-#-#-#-#-#-#-#-#- STATIC CLOAK CONFIGURATION -#-#-#-#-#-#-#-#-#-#-#
# To use the cloak_static module you must define a <cloak> tag. This #
# tag can have the following fields. #
# #
@ -734,6 +659,55 @@
# class=""
# cloak="some.fixed.value">
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# User data cloak module: Adds the "account" (services account name),
# "account-id" (services account identifier), and "nick" (nickname)
# cloak methods.
#<module name="cloak_user">
#
#-#-#-#-#-#-#-#-#-#-#- USER CLOAK CONFIGURATION -#-#-#-#-#-#-#-#-#-#-#
# To use the cloak_user module you must define a <cloak> tag. This #
# tag can have the following fields. #
# #
# class - If non-empty then a comma-delimited list of connect #
# class names that a user has to be in to get the cloak #
# from this tag. #
# #
# prefix - A freeform value to prefix cloaks with. This must not #
# contain spaces. #
# #
# suffix - A freeform value to suffix IPv4/IPv6 cloaks with. This #
# must not contain spaces. #
# #
# sanitize - If enabled then any characters in the account name, #
# account id or nickname that are not valid in a hostname #
# will be removed rather than skipping the cloak method. #
# Defaults to yes. #
# #
# IMPORTANT: Changing these details will break all of your existing #
# bans. If you do not want this to happen you can define multiple #
# cloak tags. The first will be used for hostnames and the rest will #
# be used for checking if a user is banned in a channel. #
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
#
#<cloak method="account"
# class=""
# prefix=""
# suffix=".users.example.com"
# sanitize="yes">
#
#<cloak method="account-id"
# class=""
# prefix=""
# suffix=".users.example.com"
# sanitize="yes">
#
#<cloak method="nick"
# class=""
# prefix="Users/"
# suffix=""
# sanitize="yes">
#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
# Codepage module: Allows using a custom 8-bit codepage for nicknames
# and case mapping.

View File

@ -1,143 +0,0 @@
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2023 Sadie Powell <sadie@witchery.services>
*
* This file is part of InspIRCd. InspIRCd is free software: you can
* redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "inspircd.h"
#include "modules/cloak.h"
class NickMethod final
: public Cloak::Method
{
protected:
// The characters which are valid in a hostname.
const CharState& hostmap;
// The prefix for cloaks (e.g. MyNet).
const std::string prefix;
// Whether to strip non-host characters from the cloak.
const bool sanitize;
// The suffix for IP cloaks (e.g. IP).
const std::string suffix;
public:
NickMethod(const Cloak::Engine* engine, const std::shared_ptr<ConfigTag>& tag, const CharState& hm) ATTR_NOT_NULL(2)
: Cloak::Method(engine, tag)
, hostmap(hm)
, prefix(tag->getString("prefix"))
, sanitize(tag->getBool("sanitize", true))
, suffix(tag->getString("suffix"))
{
}
std::string Generate(LocalUser* user) override ATTR_NOT_NULL(2)
{
if (!MatchesUser(user))
return {}; // We shouldn't cloak this user.
std::string safenick;
safenick.reserve(user->nick.length());
for (const auto chr : user->nick)
{
if (!hostmap.test(static_cast<unsigned char>(chr)))
{
if (!sanitize)
return {}; // Contains invalid characters.
continue;
}
safenick.push_back(chr);
}
if (safenick.empty())
return {}; // No cloak.
return prefix + safenick + suffix;
}
std::string Generate(const std::string& hostip) override
{
// We can't generate nick cloaks without a user.
return {};
}
void GetLinkData(Module::LinkData& data, std::string& compatdata) override
{
data["prefix"] = prefix;
data["sanitize"] = sanitize ? "yes" : "no";
data["suffix"] = suffix;
}
};
class NickEngine final
: public Cloak::Engine
{
public:
// The characters which are valid in a hostname.
CharState hostmap;
NickEngine(Module* Creator)
: Cloak::Engine(Creator, "nick")
{
}
Cloak::MethodPtr Create(const std::shared_ptr<ConfigTag>& tag, bool primary) override
{
return std::make_shared<NickMethod>(this, tag, hostmap);
}
};
class ModuleCloakNick final
: public Module
{
private:
Cloak::API cloakapi;
NickEngine nickcloak;
public:
ModuleCloakNick()
: Module(VF_VENDOR, "Adds the nick cloaking method for use with the cloak module.")
, cloakapi(this)
, nickcloak(this)
{
}
void ReadConfig(ConfigStatus& status) override
{
CharState newhostmap;
const auto& tag = ServerInstance->Config->ConfValue("hostname");
for (const auto chr : tag->getString("charmap", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_/0123456789", 1))
{
// A hostname can not contain NUL, LF, CR, or SPACE.
if (chr == 0x00 || chr == 0x0A || chr == 0x0D || chr == 0x20)
throw ModuleException(this, INSP_FORMAT("<hostname:charmap> can not contain character 0x{:02X} ({})", chr, chr));
newhostmap.set(static_cast<unsigned char>(chr));
}
std::swap(newhostmap, nickcloak.hostmap);
}
void OnUserPostNick(User* user, const std::string& oldnick) override
{
LocalUser* luser = IS_LOCAL(user);
if (luser && cloakapi && cloakapi->IsActiveCloak(nickcloak))
cloakapi->ResetCloaks(luser, true);
}
};
MODULE_INIT(ModuleCloakNick)

View File

@ -20,13 +20,10 @@
#include "modules/account.h"
#include "modules/cloak.h"
class AccountMethod
class UserMethod
: public Cloak::Method
{
protected:
// Dynamic reference to the account api.
Account::API& accountapi;
// The characters which are valid in a hostname.
const CharState& hostmap;
@ -39,17 +36,8 @@ protected:
// The suffix for IP cloaks (e.g. .example.org).
const std::string suffix;
// Retrieves the middle segment of the cloak.
virtual std::string GetMiddle(LocalUser* user)
{
const std::string* accountname = accountapi ? accountapi->GetAccountName(user) : nullptr;
return accountname ? *accountname : "";
}
public:
AccountMethod(const Cloak::Engine* engine, const std::shared_ptr<ConfigTag>& tag, const CharState& hm, Account::API& api) ATTR_NOT_NULL(2)
UserMethod(const Cloak::Engine* engine, const std::shared_ptr<ConfigTag>& tag, const CharState& hm) ATTR_NOT_NULL(2)
: Cloak::Method(engine, tag)
, accountapi(api)
, hostmap(hm)
, prefix(tag->getString("prefix"))
, sanitize(tag->getBool("sanitize", true))
@ -57,6 +45,10 @@ public:
{
}
// Retrieves the middle segment of the cloak.
virtual std::string GetMiddle(LocalUser* user) = 0;
public:
std::string Generate(LocalUser* user) override ATTR_NOT_NULL(2)
{
if (!MatchesUser(user))
@ -101,10 +93,35 @@ public:
}
};
class AccountIdMethod final
: public AccountMethod
class AccountMethod final
: public UserMethod
{
protected:
private:
// Dynamic reference to the account api.
Account::API accountapi;
// Retrieves the middle segment of the cloak.
std::string GetMiddle(LocalUser* user) override
{
const std::string* accountname = accountapi ? accountapi->GetAccountName(user) : nullptr;
return accountname ? *accountname : "";
}
public:
AccountMethod(const Cloak::Engine* engine, const std::shared_ptr<ConfigTag>& tag, const CharState& hm) ATTR_NOT_NULL(2)
: UserMethod(engine, tag, hm)
, accountapi(engine->creator)
{
}
};
class AccountIdMethod final
: public UserMethod
{
private:
// Dynamic reference to the account api.
Account::API accountapi;
// Retrieves the middle segment of the cloak.
std::string GetMiddle(LocalUser* user) override
{
@ -113,55 +130,69 @@ protected:
}
public:
AccountIdMethod(const Cloak::Engine* engine, const std::shared_ptr<ConfigTag>& tag, const CharState& hm, Account::API& api) ATTR_NOT_NULL(2)
: AccountMethod(engine, tag, hm, api)
AccountIdMethod(const Cloak::Engine* engine, const std::shared_ptr<ConfigTag>& tag, const CharState& hm) ATTR_NOT_NULL(2)
: UserMethod(engine, tag, hm)
, accountapi(engine->creator)
{
}
};
class NickMethod final
: public UserMethod
{
private:
// Retrieves the middle segment of the cloak.
std::string GetMiddle(LocalUser* user) override
{
return user->nick;
}
public:
NickMethod(const Cloak::Engine* engine, const std::shared_ptr<ConfigTag>& tag, const CharState& hm) ATTR_NOT_NULL(2)
: UserMethod(engine, tag, hm)
{
}
};
template <typename Method>
class AccountEngine final
class UserEngine final
: public Cloak::Engine
{
private:
// Dynamic reference to the account api.
Account::API& accountapi;
// The characters which are valid in a hostname.
const CharState& hostmap;
public:
AccountEngine(Module* Creator, const std::string& Name, const CharState& hm, Account::API& api)
UserEngine(Module* Creator, const std::string& Name, const CharState& hm)
: Cloak::Engine(Creator, Name)
, accountapi(api)
, hostmap(hm)
{
}
Cloak::MethodPtr Create(const std::shared_ptr<ConfigTag>& tag, bool primary) override
{
return std::make_shared<Method>(this, tag, hostmap, accountapi);
return std::make_shared<Method>(this, tag, hostmap);
}
};
class ModuleCloakAccount final
class ModuleCloakUser final
: public Module
, public Account::EventListener
{
private:
Account::API accountapi;
AccountEngine<AccountMethod> accountcloak;
AccountEngine<AccountIdMethod> accountidcloak;
UserEngine<AccountMethod> accountcloak;
UserEngine<AccountIdMethod> accountidcloak;
UserEngine<NickMethod> nickcloak;
Cloak::API cloakapi;
CharState hostmap;
public:
ModuleCloakAccount()
: Module(VF_VENDOR, "Adds the account and account-id cloaking methods for use with the cloak module.")
ModuleCloakUser()
: Module(VF_VENDOR, "Adds the account, account-id, nick cloaking methods for use with the cloak module.")
, Account::EventListener(this)
, accountapi(this)
, accountcloak(this, "account", hostmap, accountapi)
, accountidcloak(this, "account-id", hostmap, accountapi)
, accountcloak(this, "account", hostmap)
, accountidcloak(this, "account-id", hostmap)
, nickcloak(this, "nick", hostmap)
, cloakapi(this)
{
}
@ -189,6 +220,13 @@ public:
if (cloakapi->IsActiveCloak(accountcloak) || cloakapi->IsActiveCloak(accountidcloak))
cloakapi->ResetCloaks(luser, true);
}
void OnUserPostNick(User* user, const std::string& oldnick) override
{
LocalUser* luser = IS_LOCAL(user);
if (luser && cloakapi && cloakapi->IsActiveCloak(nickcloak))
cloakapi->ResetCloaks(luser, true);
}
};
MODULE_INIT(ModuleCloakAccount)
MODULE_INIT(ModuleCloakUser)