2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 18:06:14 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2004-02-24 18:06:14 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-03 03:06:45 +01:00
|
|
|
#include "Permission.hxx"
|
2015-01-21 21:14:25 +01:00
|
|
|
#include "config/Param.hxx"
|
2014-01-24 00:20:01 +01:00
|
|
|
#include "config/ConfigGlobal.hxx"
|
|
|
|
#include "config/ConfigOption.hxx"
|
2013-09-05 18:20:52 +02:00
|
|
|
#include "system/FatalError.hxx"
|
2013-01-03 03:06:45 +01:00
|
|
|
|
2013-10-19 16:43:35 +02:00
|
|
|
#include <algorithm>
|
2013-01-03 03:06:35 +01:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
2013-10-19 16:40:40 +02:00
|
|
|
#include <assert.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <string.h>
|
2008-11-01 14:33:25 +01:00
|
|
|
|
2013-10-19 16:39:45 +02:00
|
|
|
static constexpr char PERMISSION_PASSWORD_CHAR = '@';
|
2013-10-19 16:43:35 +02:00
|
|
|
static constexpr char PERMISSION_SEPARATOR = ',';
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-10-19 16:46:07 +02:00
|
|
|
static constexpr struct {
|
|
|
|
const char *name;
|
|
|
|
unsigned value;
|
|
|
|
} permission_names[] = {
|
|
|
|
{ "read", PERMISSION_READ },
|
|
|
|
{ "add", PERMISSION_ADD },
|
|
|
|
{ "control", PERMISSION_CONTROL },
|
|
|
|
{ "admin", PERMISSION_ADMIN },
|
|
|
|
{ nullptr, 0 },
|
|
|
|
};
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-01-03 03:06:35 +01:00
|
|
|
static std::map<std::string, unsigned> permission_passwords;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-17 23:53:28 +02:00
|
|
|
static unsigned permission_default;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-10-19 16:46:07 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-01-03 13:20:10 +01:00
|
|
|
static unsigned parsePermissions(const char *string)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-10-19 16:40:40 +02:00
|
|
|
assert(string != nullptr);
|
|
|
|
|
2013-10-19 16:43:35 +02:00
|
|
|
const char *const end = string + strlen(string);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-10-19 16:43:35 +02:00
|
|
|
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;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return permission;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void initPermissions(void)
|
|
|
|
{
|
2008-11-01 14:33:25 +01:00
|
|
|
unsigned permission;
|
2016-10-28 11:38:37 +02:00
|
|
|
const ConfigParam *param;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
permission_default = PERMISSION_READ | PERMISSION_ADD |
|
|
|
|
PERMISSION_CONTROL | PERMISSION_ADMIN;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2015-01-21 22:36:13 +01:00
|
|
|
param = config_get_param(ConfigOption::PASSWORD);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (param) {
|
|
|
|
permission_default = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-14 22:14:06 +02:00
|
|
|
do {
|
2009-01-03 13:20:10 +01:00
|
|
|
const char *separator =
|
2013-10-15 22:32:39 +02:00
|
|
|
strchr(param->value.c_str(),
|
|
|
|
PERMISSION_PASSWORD_CHAR);
|
2009-01-03 13:20:06 +01:00
|
|
|
|
|
|
|
if (separator == NULL)
|
2013-09-05 18:20:52 +02:00
|
|
|
FormatFatalError("\"%c\" not found in password string "
|
|
|
|
"\"%s\", line %i",
|
|
|
|
PERMISSION_PASSWORD_CHAR,
|
2013-10-15 22:32:39 +02:00
|
|
|
param->value.c_str(),
|
|
|
|
param->line);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-10-15 22:32:39 +02:00
|
|
|
std::string password(param->value.c_str(), separator);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-01-03 13:20:06 +01:00
|
|
|
permission = parsePermissions(separator + 1);
|
2006-07-14 22:14:06 +02:00
|
|
|
|
2013-10-15 22:40:17 +02:00
|
|
|
permission_passwords.insert(std::make_pair(std::move(password),
|
2013-01-03 03:06:35 +01:00
|
|
|
permission));
|
2014-02-19 23:57:55 +01:00
|
|
|
} while ((param = param->next) != nullptr);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2015-01-21 22:36:13 +01:00
|
|
|
param = config_get_param(ConfigOption::DEFAULT_PERMS);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (param)
|
2013-10-15 22:32:39 +02:00
|
|
|
permission_default = parsePermissions(param->value.c_str());
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2009-10-22 15:10:01 +02:00
|
|
|
int getPermissionFromPassword(char const* password, unsigned* permission)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2013-01-03 03:06:35 +01:00
|
|
|
auto i = permission_passwords.find(password);
|
|
|
|
if (i == permission_passwords.end())
|
2008-11-01 14:33:25 +01:00
|
|
|
return -1;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2013-01-03 03:06:35 +01:00
|
|
|
*permission = i->second;
|
2008-11-01 14:33:25 +01:00
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-17 23:53:28 +02:00
|
|
|
unsigned getDefaultPermissions(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
return permission_default;
|
|
|
|
}
|