Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of unified privileges checking for CLI commands #1504

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dnf5-plugins/builddep_plugin/builddep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void BuildDepCommand::set_argument_parser() {
allow_erasing = std::make_unique<AllowErasingOption>(*this);
auto skip_unavailable = std::make_unique<SkipUnavailableOption>(*this);
create_allow_downgrade_options(*this);
create_store_option(*this);
}

void BuildDepCommand::configure() {
Expand Down
1 change: 0 additions & 1 deletion dnf5/commands/clean/clean.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ void CleanCommand::set_argument_parser() {
});

cmd.register_positional_arg(cache_types);
create_store_option(*this);
}

void CleanCommand::run() {
Expand Down
1 change: 1 addition & 0 deletions dnf5/commands/distro-sync/distro-sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ void DistroSyncCommand::set_argument_parser() {
allow_erasing = std::make_unique<AllowErasingOption>(*this);
auto skip_broken = std::make_unique<SkipBrokenOption>(*this);
auto skip_unavailable = std::make_unique<SkipUnavailableOption>(*this);
create_downloadonly_option(*this);
create_offline_option(*this);
create_store_option(*this);
}
Expand Down
66 changes: 66 additions & 0 deletions dnf5/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include <libdnf5-cli/utils/userconfirm.hpp>
#include <libdnf5/base/base.hpp>
#include <libdnf5/common/xdg.hpp>
#include <libdnf5/conf/const.hpp>
#include <libdnf5/logger/factory.hpp>
#include <libdnf5/logger/global_logger.hpp>
#include <libdnf5/logger/memory_buffer_logger.hpp>
#include <libdnf5/repo/repo_cache.hpp>
#include <libdnf5/rpm/arch.hpp>
#include <libdnf5/rpm/package_query.hpp>
#include <libdnf5/utils/bgettext/bgettext-mark-domain.h>
#include <libdnf5/utils/locker.hpp>
#include <libdnf5/version.hpp>
#include <locale.h>
#include <string.h>
Expand Down Expand Up @@ -1081,6 +1083,63 @@ static void print_no_match_libdnf_plugin_patterns(dnf5::Context & context) {
}
}

static bool cmd_requires_privileges(dnf5::Context & context) {
// the main, dnf5 command, is allowed
auto cmd = context.get_selected_command();
auto arg_cmd = cmd->get_argument_parser_command();
if (arg_cmd->get_parent() == nullptr) {
return false;
}

// first a hard-coded list of commands that always need to be run with elevated privileges
auto main_arg_cmd = cmd->get_parent_command() != context.get_root_command() ? arg_cmd->get_parent() : arg_cmd;
std::vector<std::string> privileged_cmds = {"automatic", "offline", "system-upgrade"};
if (std::find(privileged_cmds.begin(), privileged_cmds.end(), main_arg_cmd->get_id()) != privileged_cmds.end()) {
return true;
}

// when assumeno is set, system should not be modified
auto & config = context.get_base().get_config();
if (config.get_assumeno_option().get_value()) {
return false;
}

auto all_cmd_args = arg_cmd->get_named_args();
if (main_arg_cmd != arg_cmd) {
all_cmd_args.insert(
all_cmd_args.end(), main_arg_cmd->get_named_args().begin(), main_arg_cmd->get_named_args().end());
}

// when downloadonly is defined and set, system should not be modified
auto it_downloadonly = std::find_if(
all_cmd_args.begin(), all_cmd_args.end(), [](auto arg) { return arg->get_long_name() == "downloadonly"; });
if (it_downloadonly != all_cmd_args.end() &&
((libdnf5::OptionBool *)(*it_downloadonly)->get_linked_value())->get_value()) {
return false;
}

// otherwise, transactional cmds with store option defined are expected to modify the system
auto it_store = std::find_if(
all_cmd_args.begin(), all_cmd_args.end(), [](auto arg) { return arg->get_long_name() == "store"; });
return it_store != all_cmd_args.end();
}

static bool user_has_privileges(dnf5::Context & context) {
std::filesystem::path lock_file_path = context.get_base().get_config().get_installroot_option().get_value();
lock_file_path /= std::filesystem::path(libdnf5::TRANSACTION_LOCK_FILEPATH).relative_path();
lock_file_path += ".tmp";

try {
std::filesystem::create_directories(lock_file_path.parent_path());
libdnf5::utils::Locker locker(lock_file_path);
return locker.write_lock();
} catch (libdnf5::SystemError & ex) {
return false;
} catch (std::filesystem::filesystem_error & ex) {
return false;
}
}

int main(int argc, char * argv[]) try {
dnf5::set_locale();

Expand Down Expand Up @@ -1257,6 +1316,13 @@ int main(int argc, char * argv[]) try {
dump_repository_configuration(context, repo_id_list);
}

if (cmd_requires_privileges(context) && !user_has_privileges(context)) {
throw libdnf5::cli::InsufficientPrivilegesError(
M_("The requested operation requires superuser privileges. Please log in as a user with elevated "
"rights, or use the \"--assumeno\" or \"--downloadonly\" options to run the command without "
"modifying the system state."));
}

{
if (context.get_load_available_repos() != dnf5::Context::LoadAvailableRepos::NONE) {
context.load_repos(context.get_load_system_repo());
Expand Down
3 changes: 3 additions & 0 deletions doc/commands/distro-sync.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Options
``--skip-unavailable``
| Allow skipping packages that are not possible to synchronize. All remaining packages will be synchronized.

``--downloadonly``
| Download the resolved package set without executing an RPM transaction.

``--offline``
| Store the transaction to be performed offline. See :manpage:`dnf5-offline(8)`, :ref:`Offline command <offline_command_ref-label>`.

Expand Down
8 changes: 8 additions & 0 deletions include/libdnf5-cli/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ class AbortedByUserError : public Error {
};


/// Exception is thrown when the user does not have enough privileges to perform requested operation.
class InsufficientPrivilegesError : public Error {
public:
using Error::Error;
const char * get_name() const noexcept override { return "InsufficientPrivilegesError"; }
};


/// Exception is thrown when libdnf5 fails to resolve the transaction.
class GoalResolveError : public Error {
public:
Expand Down
2 changes: 2 additions & 0 deletions include/libdnf5/conf/const.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const std::vector<std::string> REPOSITORY_CONF_DIRS{
"/etc/yum.repos.d", "/etc/distro.repos.d", "/usr/share/dnf5/repos.d"};
constexpr const char * REPOS_OVERRIDE_DIR = "/etc/dnf/repos.override.d";

constexpr const char * TRANSACTION_LOCK_FILEPATH = "/run/dnf/rpmtransaction.lock";

// More important varsdirs must be on the end of vector
const std::vector<std::string> VARS_DIRS{"/usr/share/dnf5/vars.d", "/etc/dnf/vars"};

Expand Down
16 changes: 15 additions & 1 deletion libdnf5/utils/locker.hpp → include/libdnf5/utils/locker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,26 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

namespace libdnf5::utils {

/// Object for implementing a simple file mutex mechanism
/// or checking read/write access on a given path.
class Locker {
public:
explicit Locker(const std::string & path) : path(path){};
/// Create a Locker object at a given path
explicit Locker(const std::string & path);
~Locker();

/// @brief Try to acquire read lock on a given file path
/// @return True if lock acquisition was successful, otherwise false
/// @throws libdnf5::SystemError if an unexpected error occurs when checking the lock state, like insufficient privileges
bool read_lock();

/// @brief Try to acquire write lock on a given file path
/// @return True if lock acquisition was successful, otherwise false
/// @throws libdnf5::SystemError if an unexpected error occurs when checking the lock state, like insufficient privileges
bool write_lock();

/// @brief Unlock the existing lock and remove the underlying lock file
/// @throws libdnf5::SystemError if an unexpected error occurs when unlocking
void unlock();

private:
Expand Down
5 changes: 3 additions & 2 deletions libdnf5/base/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,20 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "transaction_impl.hpp"
#include "transaction_module_impl.hpp"
#include "transaction_package_impl.hpp"
#include "utils/locker.hpp"
#include "utils/string.hpp"

#include "libdnf5/base/base.hpp"
#include "libdnf5/common/exception.hpp"
#include "libdnf5/common/sack/exclude_flags.hpp"
#include "libdnf5/common/sack/query_cmp.hpp"
#include "libdnf5/comps/group/query.hpp"
#include "libdnf5/conf/const.hpp"
#include "libdnf5/repo/package_downloader.hpp"
#include "libdnf5/rpm/package_query.hpp"
#include "libdnf5/utils/bgettext/bgettext-lib.h"
#include "libdnf5/utils/bgettext/bgettext-mark-domain.h"
#include "libdnf5/utils/format.hpp"
#include "libdnf5/utils/locker.hpp"

#include <fmt/format.h>
#include <unistd.h>
Expand Down Expand Up @@ -853,7 +854,7 @@ Transaction::TransactionRunResult Transaction::Impl::_run(

// acquire the lock
std::filesystem::path lock_file_path = config.get_installroot_option().get_value();
lock_file_path /= "run/dnf/rpmtransaction.lock";
lock_file_path /= std::filesystem::path(libdnf5::TRANSACTION_LOCK_FILEPATH).relative_path();
std::filesystem::create_directories(lock_file_path.parent_path());

libdnf5::utils::Locker locker(lock_file_path);
Expand Down
4 changes: 3 additions & 1 deletion libdnf5/utils/locker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.
*/


#include "locker.hpp"
#include "libdnf5/utils/locker.hpp"

#include "libdnf5/common/exception.hpp"
#include "libdnf5/utils/bgettext/bgettext-mark-domain.h"
Expand All @@ -29,6 +29,8 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

namespace libdnf5::utils {

Locker::Locker(const std::string & path) : path(path){};

bool Locker::read_lock() {
return lock(F_RDLCK);
}
Expand Down
Loading