summaryrefslogtreecommitdiff
path: root/modules/encryption/enc_old.cpp
blob: 4b6dc438ab173235da7ad0bb993dc2e27484af6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* Include file for high-level encryption routines.
 *
 * (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/encryption.h"

class EOld final
	: public Module
{
private:
	ServiceReference<Encryption::Provider> md5;

	Anope::string EncryptInternal(const Anope::string &src)
	{
		if (!md5)
			return {};

		char digest[32];
		memset(digest, 0, sizeof(digest));

		auto hash = md5->Encrypt(src);
		if (hash.length() != sizeof(digest))
			return {}; // Probably a bug?
		memcpy(digest, hash.data(), hash.length());

		char digest2[16];
		for (size_t i = 0; i < sizeof(digest); i += 2)
			digest2[i / 2] = XTOI(digest[i]) << 4 | XTOI(digest[i + 1]);

		return Anope::Hex(digest2, sizeof(digest2));
	}

	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)
		, 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");

		ModuleManager::LoadModule("enc_md5", User::Find(creator, true));
		if (!md5)
			throw ModuleException("Unable to find md5 reference");
	}

	void OnCheckAuthentication(User *, IdentifyRequest *req) override
	{
		const auto *na = NickAlias::Find(req->GetAccount());
		if (!na)
			return;

		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;

		auto enc = EncryptInternal(req->GetPassword());
		if (!enc.empty() && nc->pass.equals_cs(enc))
		{
			// 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);
		}
	}
};

MODULE_INIT(EOld)