From 8a11cfdda89891541e9bbaa676397368ce7f3230 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 7 Jan 2023 15:25:52 +0100 Subject: [PATCH] executePrivileged: support multiple privesc commands, support sudo --- src/nix/system.cc | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/nix/system.cc b/src/nix/system.cc index e381a3a11..4c3fe59b1 100644 --- a/src/nix/system.cc +++ b/src/nix/system.cc @@ -34,13 +34,22 @@ using namespace nix; void executePrivileged(std::string program, Strings args) { args.push_front(program); auto exe = program; - if(getuid() != 0) { - args.push_front("doas"); - exe = "doas"; + auto privCmds = Strings { + "doas", + "sudo" + }; + bool isRoot = getuid() == 0; + for (auto privCmd : privCmds) { + if(!isRoot) { + args.push_front(privCmd); + exe = privCmd; + } + execvp(exe.c_str(), stringsToCharPtrs(args).data()); + if(!isRoot) + args.pop_front(); } - execvp(exe.c_str(), stringsToCharPtrs(args).data()); - throw SysError("unable to execute '%s'", exe); + throw SysError("unable to execute privilege elevation helper (tried %s)", concatStringsSep(", ", privCmds)); } struct SystemCommand : InstallableCommand