summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorSadie Powell <sadie@witchery.services>2024-03-10 16:20:35 +0000
committerSadie Powell <sadie@witchery.services>2024-03-10 20:28:00 +0000
commita849a81ac3005aae933cb5a673b873fbd6125c2a (patch)
treec118e5287e7ec780943960fd0267b182084e8120 /modules
parentf919bb0748fe1ba09114f22841efb5af7c5bb37d (diff)
Refactor the enc_old module.
Diffstat (limited to 'modules')
-rw-r--r--modules/encryption/enc_old.cpp60
1 files changed, 23 insertions, 37 deletions
diff --git a/modules/encryption/enc_old.cpp b/modules/encryption/enc_old.cpp
index f2344aeff..a3b4c98c5 100644
--- a/modules/encryption/enc_old.cpp
+++ b/modules/encryption/enc_old.cpp
@@ -12,35 +12,21 @@
#include "module.h"
#include "modules/encryption.h"
-static ServiceReference<Encryption::Provider> md5("Encryption::Provider", "md5");
-
-class OldMD5Provider final
- : public Encryption::Provider
-{
-public:
- OldMD5Provider(Module *creator)
- : Encryption::Provider(creator, "oldmd5", 16, 64)
- {
- }
-
- std::unique_ptr<Encryption::Context> CreateContext() override
- {
- if (md5)
- return md5->CreateContext();
- return nullptr;
- }
-};
-
class EOld final
: public Module
{
- OldMD5Provider oldmd5provider;
+private:
+ ServiceReference<Encryption::Provider> md5;
- inline static char XTOI(char c) { return c > 9 ? c - 'A' + 10 : c - '0'; }
+ inline static char XTOI(char c)
+ {
+ return c > 9 ? c - 'A' + 10 : c - '0';
+ }
public:
- EOld(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, ENCRYPTION | VENDOR),
- oldmd5provider(this)
+ EOld(const Anope::string &modname, const Anope::string &creator)
+ : Module(modname, creator, ENCRYPTION | VENDOR)
+ , md5("Encryption::Provider", "md5")
{
if (ModuleManager::FindFirstOf(ENCRYPTION) == this)
throw ModuleException("enc_old is deprecated and can not be used as a primary encryption method");
@@ -48,7 +34,6 @@ public:
ModuleManager::LoadModule("enc_md5", User::Find(creator, true));
if (!md5)
throw ModuleException("Unable to find md5 reference");
-
}
EventReturn OnEncrypt(const Anope::string &src, Anope::string &dest) override
@@ -56,33 +41,35 @@ public:
if (!md5)
return EVENT_CONTINUE;
- char digest[32], digest2[16];
+ char digest[32];
memset(digest, 0, sizeof(digest));
+
auto hash = md5->Encrypt(src);
- if (hash.length() > sizeof(digest))
- throw CoreException("Hash too large");
+ if (hash.length() != sizeof(digest))
+ return EVENT_CONTINUE; // Probably a bug?
memcpy(digest, hash.data(), hash.length());
- for (int i = 0; i < 32; i += 2)
+ char digest2[16];
+ for (size_t i = 0; i < sizeof(digest); i += 2)
digest2[i / 2] = XTOI(digest[i]) << 4 | XTOI(digest[i + 1]);
- Anope::string buf = "oldmd5:" + Anope::Hex(digest2, sizeof(digest2));
-
- Log(LOG_DEBUG_2) << "(enc_old) hashed password from [" << src << "] to [" << buf << "]";
- dest = buf;
+ auto enc = "oldmd5:" + Anope::Hex(digest2, sizeof(digest2));
+ Log(LOG_DEBUG_2) << "(enc_old) hashed password from [" << src << "] to [" << enc << "]";
+ dest = enc;
return EVENT_ALLOW;
}
void OnCheckAuthentication(User *, IdentifyRequest *req) override
{
const NickAlias *na = NickAlias::Find(req->GetAccount());
- if (na == NULL)
+ if (!na)
return;
- NickCore *nc = na->nc;
+ NickCore *nc = na->nc;
size_t pos = nc->pass.find(':');
if (pos == Anope::string::npos)
return;
+
Anope::string hash_method(nc->pass.begin(), nc->pass.begin() + pos);
if (!hash_method.equals_cs("oldmd5"))
return;
@@ -91,9 +78,8 @@ public:
this->OnEncrypt(req->GetPassword(), buf);
if (nc->pass.equals_cs(buf))
{
- /* if we are NOT the first module in the list,
- * we want to re-encrypt the pass with the new encryption
- */
+ // If we are NOT the first encryption module we want to re-encrypt
+ // the password with the primary encryption method.
if (ModuleManager::FindFirstOf(ENCRYPTION) != this)
Anope::Encrypt(req->GetPassword(), nc->pass);
req->Success(this);