libreoffice-online/common/CommandControl.cpp
Rash419 50678c5191 wsd: introduce remote configuration
fetch JSON from remote server and apply new config without restarting coolwsd
- Extended the feature_locked configuration & functionality so that it can be set per wopi host ranges

Signed-off-by: Rash419 <rashesh.padia@collabora.com>
Change-Id: Id0e2b4b524a80de88b2b3559dd18c4a95b8163fb
2022-02-25 18:36:06 +03:00

165 lines
4.8 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <config.h>
#include <string>
#include <unordered_set>
#include "ConfigUtil.hpp"
#include "Util.hpp"
#include "CommandControl.hpp"
namespace CommandControl
{
bool LockManager::_isLockedUser = false;
bool LockManager::_isHostReadOnly = false;
std::unordered_set<std::string> LockManager::LockedCommandList;
std::string LockManager::LockedCommandListString;
Util::RegexListMatcher LockManager::readOnlyWopiHosts;
Util::RegexListMatcher LockManager::disabledCommandWopiHosts;
bool LockManager::lockHostEnabled = false;
LockManager::LockManager() {}
void LockManager::generateLockedCommandList()
{
#ifdef ENABLE_FEATURE_LOCK
LockedCommandListString = config::getString("feature_lock.locked_commands", "");
Util::trim(LockedCommandListString);
StringVector commandList = Util::tokenize(LockedCommandListString);
std::string command;
for (std::size_t i = 0; i < commandList.size(); i++)
{
// just an extra check to make sure any whitespace does not sniff in command
// or else command will not be recognized
command = Util::trim_whitespace(commandList[i]);
if (!command.empty())
{
LockedCommandList.emplace(command);
}
}
#endif
}
const std::unordered_set<std::string>& LockManager::getLockedCommandList()
{
if (LockedCommandList.empty())
generateLockedCommandList();
return LockedCommandList;
}
const std::string LockManager::getLockedCommandListString()
{
if (LockedCommandListString.empty())
generateLockedCommandList();
return LockedCommandListString;
}
void LockManager::parseLockedHost(Poco::Util::LayeredConfiguration& conf)
{
readOnlyWopiHosts.clear();
disabledCommandWopiHosts.clear();
lockHostEnabled = config::getBool("feature_lock.locked_hosts[@allow]", false);
if (lockHostEnabled)
{
for (size_t i = 0;; i++)
{
const std::string path = "feature_lock.locked_hosts.host[" + std::to_string(i) + ']';
const std::string host = conf.getString(path, "");
if (!host.empty())
{
if (conf.getBool(path + "[@read_only]", false))
{
readOnlyWopiHosts.allow(host);
}
else
{
readOnlyWopiHosts.deny(host);
}
if (conf.getBool(path + "[@disabled_commands]", false))
{
disabledCommandWopiHosts.allow(host);
}
else
{
disabledCommandWopiHosts.deny(host);
}
}
else if (!conf.has(path))
{
break;
}
}
}
}
bool LockManager::isHostReadOnly(const std::string& host)
{
return LockManager::lockHostEnabled && LockManager::readOnlyWopiHosts.match(host);
}
bool LockManager::isHostCommandDisabled(const std::string& host)
{
return LockManager::lockHostEnabled && LockManager::disabledCommandWopiHosts.match(host);
}
bool LockManager::hostExist(const std::string& host)
{
return LockManager::lockHostEnabled && LockManager::readOnlyWopiHosts.matchExist(host);
}
bool RestrictionManager::_isRestrictedUser = false;
std::unordered_set<std::string> RestrictionManager::RestrictedCommandList;
std::string RestrictionManager::RestrictedCommandListString;
RestrictionManager::RestrictionManager() {}
void RestrictionManager::generateRestrictedCommandList()
{
#ifdef ENABLE_FEATURE_RESTRICTION
RestrictedCommandListString = config::getString("restricted_commands", "");
Util::trim(RestrictedCommandListString);
StringVector commandList = Util::tokenize(RestrictedCommandListString);
std::string command;
for (std::size_t i = 0; i < commandList.size(); i++)
{
// just an extra check to make sure any whitespace does not sniff in command
// or else command will not be recognized
command = Util::trim_whitespace(commandList[i]);
if (!command.empty())
{
RestrictedCommandList.emplace(command);
}
}
#endif
}
const std::unordered_set<std::string>& RestrictionManager::getRestrictedCommandList()
{
if (RestrictedCommandList.empty())
generateRestrictedCommandList();
return RestrictedCommandList;
}
const std::string RestrictionManager::getRestrictedCommandListString()
{
if (RestrictedCommandListString.empty())
generateRestrictedCommandList();
return RestrictedCommandListString;
}
} // namespace CommandControl
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */