Browse Source

AsRef in SetuidCommandRunner

master
Adrian Heine 3 years ago
parent
commit
466b4cb994
  1. 27
      src/command_runner.rs
  2. 2
      src/symbols/factory.rs
  3. 4
      src/symbols/systemd/user_service.rs

27
src/command_runner.rs

@ -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)

2
src/symbols/factory.rs

@ -38,7 +38,7 @@ impl Policy for DefaultPolicy {}
pub struct SymbolFactory<'a, C: 'a + CommandRunner, R: 'a + SymbolRunner, P: 'a + Policy> {
command_runner: &'a C,
acme_command_runner: SetuidCommandRunner<'a, C>,
acme_command_runner: SetuidCommandRunner<'a, Cow<'a, str>, C>,
symbol_runner: &'a R,
policy: &'a P,
}

4
src/symbols/systemd/user_service.rs

@ -62,8 +62,8 @@ pub struct UserService<'a, C: AsRef<str>, R: CommandRunner> {
config: FileSymbol<C, PathBuf>,
}
impl<'a, R: CommandRunner> UserService<'a, String, SetuidCommandRunner<'a, R>> {
pub fn new_nodejs<'b: 'a>(
impl<'a, R: CommandRunner> UserService<'a, String, SetuidCommandRunner<'a, Cow<'a, str>, R>> {
pub fn new_nodejs(
home: Cow<'a, Path>,
user_name: Cow<'a, str>,
service_name: &'a str,

Loading…
Cancel
Save