executePrivileged: support multiple privesc commands, support sudo

This commit is contained in:
Max Headroom 2023-01-07 15:25:52 +01:00
parent d7e702b121
commit 8a11cfdda8

View file

@ -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();
}
throw SysError("unable to execute '%s'", exe);
throw SysError("unable to execute privilege elevation helper (tried %s)", concatStringsSep(", ", privCmds));
}
struct SystemCommand : InstallableCommand