Permission.cxx 3.49 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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 30
#include <map>
#include <string>
31
#include <utility>
32

33
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
34
#include <string.h>
35

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

39 40 41 42 43 44 45 46 47 48
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
49

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

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

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

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

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

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

73
	unsigned permission = 0;
74 75 76 77

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

	return permission;
}

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

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

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

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

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

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

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

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

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

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

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

#ifdef HAVE_UN

unsigned
GetLocalPermissions() noexcept
{
	return local_permissions;
}

#endif