|
|
@ -1,4 +1,3 @@ |
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::error::Error;
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
use std::io::{Result as IoResult, Write};
|
|
|
@ -73,19 +72,13 @@ impl CommandRunner for StdCommandRunner { |
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct SetuidCommandRunner<'a, C>
|
|
|
|
where
|
|
|
|
C: 'a + CommandRunner,
|
|
|
|
{
|
|
|
|
pub struct SetuidCommandRunner<'a, U: AsRef<str>, C: CommandRunner> {
|
|
|
|
command_runner: &'a C,
|
|
|
|
user_name: Cow<'a, str>,
|
|
|
|
user_name: U,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, C> SetuidCommandRunner<'a, C>
|
|
|
|
where
|
|
|
|
C: 'a + CommandRunner,
|
|
|
|
{
|
|
|
|
pub fn new(user_name: Cow<'a, str>, command_runner: &'a C) -> SetuidCommandRunner<'a, C> {
|
|
|
|
impl<'a, U: AsRef<str>, C: CommandRunner> SetuidCommandRunner<'a, U, C> {
|
|
|
|
pub fn new(user_name: U, command_runner: &'a C) -> Self {
|
|
|
|
SetuidCommandRunner {
|
|
|
|
command_runner,
|
|
|
|
user_name,
|
|
|
@ -122,20 +115,17 @@ impl<'a> Drop for TempSetEnv<'a> { |
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, C> CommandRunner for SetuidCommandRunner<'a, C>
|
|
|
|
where
|
|
|
|
C: 'a + CommandRunner,
|
|
|
|
{
|
|
|
|
impl<'a, U: AsRef<str>, C: CommandRunner> CommandRunner for SetuidCommandRunner<'a, U, C> {
|
|
|
|
fn run_with_args_and_stdin(
|
|
|
|
&self,
|
|
|
|
program: &str,
|
|
|
|
args: &[&OsStr],
|
|
|
|
input: &str,
|
|
|
|
) -> IoResult<Output> {
|
|
|
|
let uid = get_user_by_name(&self.user_name)
|
|
|
|
let uid = get_user_by_name(self.user_name.as_ref())
|
|
|
|
.expect("User does not exist")
|
|
|
|
.uid();
|
|
|
|
let set_home = TempSetEnv::new("HOME", format!("/home/{}", self.user_name));
|
|
|
|
let set_home = TempSetEnv::new("HOME", format!("/home/{}", self.user_name.as_ref()));
|
|
|
|
let set_dbus = TempSetEnv::new("XDG_RUNTIME_DIR", format!("/run/user/{}", uid));
|
|
|
|
//println!("{} {:?}", program, args);
|
|
|
|
let mut child = Command::new(program)
|
|
|
@ -192,8 +182,7 @@ where |
|
|
|
) -> IoResult<Output> {
|
|
|
|
let raw_new_args = [self.user_name, "-s", "/usr/bin/env", "--", program];
|
|
|
|
let mut new_args: Vec<&OsStr> = raw_new_args.iter().map(|s| s.as_ref()).collect();
|
|
|
|
let old_args: Vec<&OsStr> = args.iter().map(|s| s.as_ref()).collect();
|
|
|
|
new_args.extend_from_slice(&old_args);
|
|
|
|
new_args.extend_from_slice(args);
|
|
|
|
self
|
|
|
|
.command_runner
|
|
|
|
.run_with_args_and_stdin("su", &new_args, input)
|
|
|
|