Permission.cxx 3.5 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * This program 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
14 15 16 17
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 19
 */

20
#include "config.h"
21
#include "Permission.hxx"
22
#include "config/Param.hxx"
23
#include "config/Data.hxx"
24
#include "config/Option.hxx"
25
#include "util/IterableSplitString.hxx"
26
#include "util/RuntimeError.hxx"
27
#include "util/StringView.hxx"
28

29
#include <cassert>
Rosen Penev's avatar
Rosen Penev committed
30
#include <cstring>
31 32
#include <map>
#include <string>
33
#include <utility>
34

35
static constexpr char PERMISSION_PASSWORD_CHAR = '@';
36
static constexpr char PERMISSION_SEPARATOR = ',';
Warren Dukes's avatar
Warren Dukes committed
37

38 39 40 41 42 43 44 45 46 47
static constexpr struct {
	const char *name;
	unsigned value;
} permission_names[] = {
	{ "read", PERMISSION_READ },
	{ "add", PERMISSION_ADD },
	{ "control", PERMISSION_CONTROL },
	{ "admin", PERMISSION_ADMIN },
	{ nullptr, 0 },
};
Warren Dukes's avatar
Warren Dukes committed
48

49
static std::map<std::string, unsigned> permission_passwords;
Warren Dukes's avatar
Warren Dukes committed
50

51
static unsigned permission_default;
Warren Dukes's avatar
Warren Dukes committed
52

53 54 55 56
#ifdef HAVE_UN
static unsigned local_permissions;
#endif

57
static unsigned
58
ParsePermission(StringView s)
59 60
{
	for (auto i = permission_names; i->name != nullptr; ++i)
61
		if (s.Equals(i->name))
62 63
			return i->value;

64 65
	throw FormatRuntimeError("unknown permission \"%.*s\"",
				 int(s.size), s.data);
66 67
}

68
static unsigned parsePermissions(const char *string)
Avuton Olrich's avatar
Avuton Olrich committed
69
{
70 71
	assert(string != nullptr);

72
	unsigned permission = 0;
73 74 75 76

	for (const auto i : IterableSplitString(string, PERMISSION_SEPARATOR))
		if (!i.empty())
			permission |= ParsePermission(i);
Warren Dukes's avatar
Warren Dukes committed
77 78 79 80

	return permission;
}

81 82
void
initPermissions(const ConfigData &config)
Avuton Olrich's avatar
Avuton Olrich committed
83 84 85
{
	permission_default = PERMISSION_READ | PERMISSION_ADD |
	    PERMISSION_CONTROL | PERMISSION_ADMIN;
Warren Dukes's avatar
Warren Dukes committed
86

87
	for (const auto &param : config.GetParamList(ConfigOption::PASSWORD)) {
Avuton Olrich's avatar
Avuton Olrich committed
88
		permission_default = 0;
Warren Dukes's avatar
Warren Dukes committed
89

90
		param.With([](const char *value){
Rosen Penev's avatar
Rosen Penev committed
91
			const char *separator = std::strchr(value,
92
						       PERMISSION_PASSWORD_CHAR);
Warren Dukes's avatar
Warren Dukes committed
93

94
			if (separator == nullptr)
95 96
				throw FormatRuntimeError("\"%c\" not found in password string",
							 PERMISSION_PASSWORD_CHAR);
Warren Dukes's avatar
Warren Dukes committed
97

98
			std::string password(value, separator);
99

100 101 102 103
			unsigned permission = parsePermissions(separator + 1);
			permission_passwords.insert(std::make_pair(std::move(password),
								   permission));
		});
Warren Dukes's avatar
Warren Dukes committed
104 105
	}

106 107 108 109
	config.With(ConfigOption::DEFAULT_PERMS, [](const char *value){
		if (value != nullptr)
			permission_default = parsePermissions(value);
	});
110 111

#ifdef HAVE_UN
112 113 114 115 116
	local_permissions = config.With(ConfigOption::LOCAL_PERMISSIONS, [](const char *value){
		return value != nullptr
			? parsePermissions(value)
			: permission_default;
	});
117
#endif
Warren Dukes's avatar
Warren Dukes committed
118 119
}

120 121
int
getPermissionFromPassword(const char *password, unsigned *permission) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
122
{
123 124
	auto i = permission_passwords.find(password);
	if (i == permission_passwords.end())
125
		return -1;
Warren Dukes's avatar
Warren Dukes committed
126

127
	*permission = i->second;
128
	return 0;
Warren Dukes's avatar
Warren Dukes committed
129 130
}

131 132
unsigned
getDefaultPermissions() noexcept
Avuton Olrich's avatar
Avuton Olrich committed
133
{
Warren Dukes's avatar
Warren Dukes committed
134 135
	return permission_default;
}
136 137 138 139 140 141 142 143 144 145

#ifdef HAVE_UN

unsigned
GetLocalPermissions() noexcept
{
	return local_permissions;
}

#endif