Permission.cxx 3.36 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 23 24
#include "config/ConfigData.hxx"
#include "config/ConfigGlobal.hxx"
#include "config/ConfigOption.hxx"
25
#include "system/FatalError.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 56 57 58 59 60 61 62
gcc_pure
static unsigned
ParsePermission(const char *p)
{
	for (auto i = permission_names; i->name != nullptr; ++i)
		if (strcmp(p, i->name) == 0)
			return i->value;

	FormatFatalError("unknown permission \"%s\"", p);
}

63
static unsigned parsePermissions(const char *string)
Avuton Olrich's avatar
Avuton Olrich committed
64
{
65 66
	assert(string != nullptr);

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

69 70 71 72 73 74 75 76 77 78 79 80 81
	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
82 83 84 85 86
	}

	return permission;
}

Avuton Olrich's avatar
Avuton Olrich committed
87 88
void initPermissions(void)
{
89
	unsigned permission;
90
	const struct config_param *param;
Warren Dukes's avatar
Warren Dukes committed
91

Avuton Olrich's avatar
Avuton Olrich committed
92 93
	permission_default = PERMISSION_READ | PERMISSION_ADD |
	    PERMISSION_CONTROL | PERMISSION_ADMIN;
Warren Dukes's avatar
Warren Dukes committed
94

95
	param = config_get_param(CONF_PASSWORD);
Warren Dukes's avatar
Warren Dukes committed
96

Avuton Olrich's avatar
Avuton Olrich committed
97 98
	if (param) {
		permission_default = 0;
Warren Dukes's avatar
Warren Dukes committed
99

100
		do {
101
			const char *separator =
102 103
				strchr(param->value.c_str(),
				       PERMISSION_PASSWORD_CHAR);
104 105

			if (separator == NULL)
106 107 108
				FormatFatalError("\"%c\" not found in password string "
						 "\"%s\", line %i",
						 PERMISSION_PASSWORD_CHAR,
109 110
						 param->value.c_str(),
						 param->line);
Warren Dukes's avatar
Warren Dukes committed
111

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

114
			permission = parsePermissions(separator + 1);
115

116
			permission_passwords.insert(std::make_pair(std::move(password),
117
								   permission));
118
		} while ((param = param->next) != nullptr);
Warren Dukes's avatar
Warren Dukes committed
119 120
	}

121
	param = config_get_param(CONF_DEFAULT_PERMS);
Warren Dukes's avatar
Warren Dukes committed
122

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

127
int getPermissionFromPassword(char const* password, unsigned* permission)
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
unsigned getDefaultPermissions(void)
Avuton Olrich's avatar
Avuton Olrich committed
138
{
Warren Dukes's avatar
Warren Dukes committed
139 140
	return permission_default;
}