|
|
@ -20,14 +20,14 @@ impl CommandRunner for StdCommandRunner { |
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct UserCommandRunner<'a, C> where C: 'a + CommandRunner {
|
|
|
|
pub struct SetuidCommandRunner<'a, C> where C: 'a + CommandRunner {
|
|
|
|
command_runner: &'a C,
|
|
|
|
user_name: &'a str
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, C> UserCommandRunner<'a, C> where C: 'a + CommandRunner {
|
|
|
|
pub fn new(user_name: &'a str, command_runner: &'a C) -> UserCommandRunner<'a, C> {
|
|
|
|
UserCommandRunner {
|
|
|
|
impl<'a, C> SetuidCommandRunner<'a, C> where C: 'a + CommandRunner {
|
|
|
|
pub fn new(user_name: &'a str, command_runner: &'a C) -> SetuidCommandRunner<'a, C> {
|
|
|
|
SetuidCommandRunner {
|
|
|
|
command_runner: command_runner,
|
|
|
|
user_name: user_name
|
|
|
|
}
|
|
|
@ -57,16 +57,43 @@ impl<'a> Drop for TempSetEnv<'a> { |
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, C> CommandRunner for UserCommandRunner<'a, C> where C: 'a + CommandRunner {
|
|
|
|
impl<'a, C> CommandRunner for SetuidCommandRunner<'a, C> where C: 'a + CommandRunner {
|
|
|
|
fn run_with_args(&self, program: &str, args: &[&str]) -> IoResult<Output> {
|
|
|
|
let uid = get_user_by_name(self.user_name).unwrap().uid();
|
|
|
|
let set_home = TempSetEnv::new("HOME", format!("/home/{}", self.user_name));
|
|
|
|
let set_dbus = TempSetEnv::new("DBUS_SESSION_BUS_ADDRESS", format!("unix:path=/run/user/{}/bus", uid));
|
|
|
|
let set_dbus = TempSetEnv::new("XDG_RUNTIME_DIR", format!("/run/user/{}", uid));
|
|
|
|
println!("{} {:?}", program, args);
|
|
|
|
let res = Command::new(program).uid(uid).gid(uid).args(args).output();
|
|
|
|
println!("{:?}", res);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct SuCommandRunner<'a, C> where C: 'a + CommandRunner {
|
|
|
|
command_runner: &'a C,
|
|
|
|
user_name: &'a str
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, C> SuCommandRunner<'a, C> where C: 'a + CommandRunner {
|
|
|
|
pub fn new(user_name: &'a str, command_runner: &'a C) -> SuCommandRunner<'a, C> {
|
|
|
|
SuCommandRunner {
|
|
|
|
command_runner: command_runner,
|
|
|
|
user_name: user_name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Su doesn't set XDG_RUNTIME_DIR
|
|
|
|
// https://github.com/systemd/systemd/blob/master/src/login/pam_systemd.c#L439
|
|
|
|
impl<'a, C> CommandRunner for SuCommandRunner<'a, C> where C: 'a + CommandRunner {
|
|
|
|
fn run_with_args(&self, program: &str, args: &[&str]) -> IoResult<Output> {
|
|
|
|
let mut new_args = vec![self.user_name, "-s", "/usr/bin/env", "--", program];
|
|
|
|
new_args.extend_from_slice(args);
|
|
|
|
self.command_runner.run_with_args("su", &new_args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
}
|