2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2018-10-31 17:54:59 +01:00
|
|
|
* Copyright 2003-2018 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"
|
2018-07-17 23:00:42 +02:00
|
|
|
#include "config/Data.hxx"
|
2018-07-16 19:50:07 +02:00
|
|
|
#include "config/Option.hxx"
|
2019-04-03 21:47:03 +02:00
|
|
|
#include "util/IterableSplitString.hxx"
|
2017-08-11 09:33:59 +02:00
|
|
|
#include "util/RuntimeError.hxx"
|
2019-04-03 21:45:01 +02:00
|
|
|
#include "util/StringView.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
|
|
|
|
2018-07-15 21:54:43 +02:00
|
|
|
#ifdef HAVE_UN
|
|
|
|
static unsigned local_permissions;
|
|
|
|
#endif
|
|
|
|
|
2013-10-19 16:46:07 +02:00
|
|
|
static unsigned
|
2019-04-03 21:45:01 +02:00
|
|
|
ParsePermission(StringView s)
|
2013-10-19 16:46:07 +02:00
|
|
|
{
|
|
|
|
for (auto i = permission_names; i->name != nullptr; ++i)
|
2019-04-03 21:45:01 +02:00
|
|
|
if (s.Equals(i->name))
|
2013-10-19 16:46:07 +02:00
|
|
|
return i->value;
|
|
|
|
|
2019-04-03 21:45:01 +02:00
|
|
|
throw FormatRuntimeError("unknown permission \"%.*s\"",
|
|
|
|
int(s.size), s.data);
|
2013-10-19 16:46:07 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
unsigned permission = 0;
|
2019-04-03 21:47:03 +02:00
|
|
|
|
|
|
|
for (const auto i : IterableSplitString(string, PERMISSION_SEPARATOR))
|
|
|
|
if (!i.empty())
|
|
|
|
permission |= ParsePermission(i);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return permission;
|
|
|
|
}
|
|
|
|
|
2018-07-17 23:00:42 +02:00
|
|
|
void
|
|
|
|
initPermissions(const ConfigData &config)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-11-01 14:33:25 +01:00
|
|
|
unsigned permission;
|
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
|
|
|
|
2018-07-18 11:03:19 +02:00
|
|
|
for (const auto ¶m : config.GetParamList(ConfigOption::PASSWORD)) {
|
2006-07-20 18:02:40 +02:00
|
|
|
permission_default = 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2018-07-18 11:03:19 +02:00
|
|
|
const char *separator = strchr(param.value.c_str(),
|
|
|
|
PERMISSION_PASSWORD_CHAR);
|
2009-01-03 13:20:06 +01:00
|
|
|
|
2018-07-18 11:03:19 +02:00
|
|
|
if (separator == NULL)
|
|
|
|
throw FormatRuntimeError("\"%c\" not found in password string "
|
|
|
|
"\"%s\", line %i",
|
|
|
|
PERMISSION_PASSWORD_CHAR,
|
|
|
|
param.value.c_str(),
|
|
|
|
param.line);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2018-07-18 11:03:19 +02:00
|
|
|
std::string password(param.value.c_str(), separator);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2018-07-18 11:03:19 +02:00
|
|
|
permission = parsePermissions(separator + 1);
|
2006-07-14 22:14:06 +02:00
|
|
|
|
2018-07-18 11:03:19 +02:00
|
|
|
permission_passwords.insert(std::make_pair(std::move(password),
|
|
|
|
permission));
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2018-07-18 11:03:19 +02:00
|
|
|
const ConfigParam *param;
|
2018-07-17 23:00:42 +02:00
|
|
|
param = config.GetParam(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());
|
2018-07-15 21:54:43 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_UN
|
2018-07-17 23:00:42 +02:00
|
|
|
param = config.GetParam(ConfigOption::LOCAL_PERMISSIONS);
|
2018-07-15 21:54:43 +02:00
|
|
|
if (param != nullptr)
|
|
|
|
local_permissions = parsePermissions(param->value.c_str());
|
|
|
|
else
|
|
|
|
local_permissions = permission_default;
|
|
|
|
#endif
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2019-04-03 21:42:31 +02:00
|
|
|
int
|
|
|
|
getPermissionFromPassword(const char *password, unsigned *permission) noexcept
|
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
|
|
|
}
|
|
|
|
|
2019-04-03 21:42:31 +02:00
|
|
|
unsigned
|
|
|
|
getDefaultPermissions() noexcept
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
return permission_default;
|
|
|
|
}
|
2018-07-15 21:54:43 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_UN
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
GetLocalPermissions() noexcept
|
|
|
|
{
|
|
|
|
return local_permissions;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|