Permission.cxx 3.68 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/RuntimeError.hxx"
26

27
#include <algorithm>
28 29 30
#include <map>
#include <string>

31
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
32
#include <string.h>
33

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

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

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

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

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

56 57 58 59 60 61 62
static unsigned
ParsePermission(const char *p)
{
	for (auto i = permission_names; i->name != nullptr; ++i)
		if (strcmp(p, i->name) == 0)
			return i->value;

63
	throw FormatRuntimeError("unknown permission \"%s\"", p);
64 65
}

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

70
	const char *const end = string + strlen(string);
Warren Dukes's avatar
Warren Dukes committed
71

72 73 74 75 76 77 78 79 80 81 82 83 84
	unsigned permission = 0;
	while (true) {
		const char *comma = std::find(string, end,
					      PERMISSION_SEPARATOR);
		if (comma > string) {
			const std::string name(string, comma);
			permission |= ParsePermission(name.c_str());
		}

		if (comma == end)
			break;

		string = comma + 1;
Warren Dukes's avatar
Warren Dukes committed
85 86 87 88 89
	}

	return permission;
}

90 91
void
initPermissions(const ConfigData &config)
Avuton Olrich's avatar
Avuton Olrich committed
92
{
93
	unsigned permission;
Warren Dukes's avatar
Warren Dukes committed
94

Avuton Olrich's avatar
Avuton Olrich committed
95 96
	permission_default = PERMISSION_READ | PERMISSION_ADD |
	    PERMISSION_CONTROL | PERMISSION_ADMIN;
Warren Dukes's avatar
Warren Dukes committed
97

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

101 102
		const char *separator = strchr(param.value.c_str(),
					       PERMISSION_PASSWORD_CHAR);
103

104 105 106 107 108 109
		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
110

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

113
		permission = parsePermissions(separator + 1);
114

115 116
		permission_passwords.insert(std::make_pair(std::move(password),
							   permission));
Warren Dukes's avatar
Warren Dukes committed
117 118
	}

119
	const ConfigParam *param;
120
	param = config.GetParam(ConfigOption::DEFAULT_PERMS);
Warren Dukes's avatar
Warren Dukes committed
121

Avuton Olrich's avatar
Avuton Olrich committed
122
	if (param)
123
		permission_default = parsePermissions(param->value.c_str());
124 125

#ifdef HAVE_UN
126
	param = config.GetParam(ConfigOption::LOCAL_PERMISSIONS);
127 128 129 130 131
	if (param != nullptr)
		local_permissions = parsePermissions(param->value.c_str());
	else
		local_permissions = permission_default;
#endif
Warren Dukes's avatar
Warren Dukes committed
132 133
}

134
int getPermissionFromPassword(char const* password, unsigned* permission)
Avuton Olrich's avatar
Avuton Olrich committed
135
{
136 137
	auto i = permission_passwords.find(password);
	if (i == permission_passwords.end())
138
		return -1;
Warren Dukes's avatar
Warren Dukes committed
139

140
	*permission = i->second;
141
	return 0;
Warren Dukes's avatar
Warren Dukes committed
142 143
}

144
unsigned getDefaultPermissions(void)
Avuton Olrich's avatar
Avuton Olrich committed
145
{
Warren Dukes's avatar
Warren Dukes committed
146 147
	return permission_default;
}
148 149 150 151 152 153 154 155 156 157

#ifdef HAVE_UN

unsigned
GetLocalPermissions() noexcept
{
	return local_permissions;
}

#endif