aboutsummaryrefslogtreecommitdiff
path: root/modules/mkpasswd.cpp
blob: 14eb3c9f8c2a3c29bdb015353f7d67c4e5f1d71d (about) (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2025 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/hash.h"
#include "modules/ircv3_replies.h"

class CommandMakePassword final
	: public SplitCommand
{
private:
	IRCv3::Replies::Fail failrpl;
	IRCv3::Replies::Note noterpl;
	IRCv3::Replies::CapReference stdrplcap;

public:
	CommandMakePassword(Module* mod)
		: SplitCommand(mod, "MKPASSWD", 2, 2)
		, failrpl(mod)
		, noterpl(mod)
		, stdrplcap(mod)
	{
		penalty = 5000;
		syntax = { "<hashtype> <plaintext>" };
	}

	CmdResult HandleLocal(LocalUser* user, const Params& parameters) override
	{
		auto* hp = ServerInstance->Modules.FindDataService<Hash::Provider>("hash/" + parameters[0]);
		if (!hp)
		{
			failrpl.SendIfCap(user, stdrplcap, this, "INVALID_HASH", parameters[0], FMT::format("{} is not a known hash algorithm!",
				parameters[0]));
			return CmdResult::FAILURE;
		}

		if (!hp->IsPasswordSafe())
		{
			failrpl.SendIfCap(user, stdrplcap, this, "INSECURE_HASH", hp->GetAlgorithm(), FMT::format("{} is not a secure password hashing algorithm!",
				hp->GetAlgorithm()));
			return CmdResult::FAILURE;
		}

		auto hash = hp->Hash(parameters[1]);
		if (hash.empty())
		{
			failrpl.SendIfCap(user, stdrplcap, this, "HASH_ERROR", hp->GetAlgorithm(), FMT::format("An error occurred whilst hashing your password with {}!",
				hp->GetAlgorithm()));
			return CmdResult::FAILURE;
		}

		auto phash = hp->ToPrintable(hash);
		noterpl.SendIfCap(user, stdrplcap, this, "HASH_RESULT", hp->GetAlgorithm(), phash, FMT::format("{} hashed password: {}",
			hp->GetAlgorithm(), phash));

		return CmdResult::SUCCESS;
	}
};

class ModuleMakePassword final
	: public Module
{
private:
	CommandMakePassword cmd;

public:
	ModuleMakePassword()
		: Module(VF_VENDOR, "Provides the /MKPASSWD command which allows the generation of hashed passwords for use in the server configuration.")
		, cmd(this)
	{
	}

	void ReadConfig(ConfigStatus& status) override
	{
		const auto& tag = ServerInstance->Config->ConfValue("mkpasswd");
		cmd.access_needed = tag->getBool("operonly") ? CmdAccess::OPERATOR : CmdAccess::NORMAL;
	}
};

MODULE_INIT(ModuleMakePassword)