Permission.cxx 3.59 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 <algorithm>
30 31 32
#include <map>
#include <string>

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
	unsigned permission;
Warren Dukes's avatar
Warren Dukes committed
86

Avuton Olrich's avatar
Avuton Olrich committed
87 88
	permission_default = PERMISSION_READ | PERMISSION_ADD |
	    PERMISSION_CONTROL | PERMISSION_ADMIN;
Warren Dukes's avatar
Warren Dukes committed
89

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

93 94
		const char *separator = strchr(param.value.c_str(),
					       PERMISSION_PASSWORD_CHAR);
95

96 97 98 99 100 101
		if (separator == NULL)
			throw FormatRuntimeError("\"%c\" not found in password string "
						 "\"%s\", line %i",
						 PERMISSION_PASSWORD_CHAR,
						 param.value.c_str(),
						 param.line);
Warren Dukes's avatar
Warren Dukes committed
102

103
		std::string password(param.value.c_str(), separator);
Warren Dukes's avatar
Warren Dukes committed
104

105
		permission = parsePermissions(separator + 1);
106

107 108
		permission_passwords.insert(std::make_pair(std::move(password),
							   permission));
Warren Dukes's avatar
Warren Dukes committed
109 110
	}

111
	const ConfigParam *param;
112
	param = config.GetParam(ConfigOption::DEFAULT_PERMS);
Warren Dukes's avatar
Warren Dukes committed
113

Avuton Olrich's avatar
Avuton Olrich committed
114
	if (param)
115
		permission_default = parsePermissions(param->value.c_str());
116 117

#ifdef HAVE_UN
118
	param = config.GetParam(ConfigOption::LOCAL_PERMISSIONS);
119 120 121 122 123
	if (param != nullptr)
		local_permissions = parsePermissions(param->value.c_str());
	else
		local_permissions = permission_default;
#endif
Warren Dukes's avatar
Warren Dukes committed
124 125
}

126 127
int
getPermissionFromPassword(const char *password, unsigned *permission) noexcept
Avuton Olrich's avatar
Avuton Olrich committed
128
{
129 130
	auto i = permission_passwords.find(password);
	if (i == permission_passwords.end())
131
		return -1;
Warren Dukes's avatar
Warren Dukes committed
132

133
	*permission = i->second;
134
	return 0;
Warren Dukes's avatar
Warren Dukes committed
135 136
}

137 138
unsigned
getDefaultPermissions() noexcept
Avuton Olrich's avatar
Avuton Olrich committed
139
{
Warren Dukes's avatar
Warren Dukes committed
140 141
	return permission_default;
}
142 143 144 145 146 147 148 149 150 151

#ifdef HAVE_UN

unsigned
GetLocalPermissions() noexcept
{
	return local_permissions;
}

#endif