Browse Source

AsRef in SetuidCommandRunner

master
Adrian Heine 5 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::error::Error;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::io::{Result as IoResult, Write}; use std::io::{Result as IoResult, Write};
@ -73,19 +72,13 @@ impl CommandRunner for StdCommandRunner {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct SetuidCommandRunner<'a, C>
where
C: 'a + CommandRunner,
{
pub struct SetuidCommandRunner<'a, U: AsRef<str>, C: CommandRunner> {
command_runner: &'a C, 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 { SetuidCommandRunner {
command_runner, command_runner,
user_name, 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( fn run_with_args_and_stdin(
&self, &self,
program: &str, program: &str,
args: &[&OsStr], args: &[&OsStr],
input: &str, input: &str,
) -> IoResult<Output> { ) -> 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") .expect("User does not exist")
.uid(); .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)); let set_dbus = TempSetEnv::new("XDG_RUNTIME_DIR", format!("/run/user/{}", uid));
//println!("{} {:?}", program, args); //println!("{} {:?}", program, args);
let mut child = Command::new(program) let mut child = Command::new(program)
@ -192,8 +182,7 @@ where
) -> IoResult<Output> { ) -> IoResult<Output> {
let raw_new_args = [self.user_name, "-s", "/usr/bin/env", "--", program]; 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 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 self
.command_runner .command_runner
.run_with_args_and_stdin("su", &new_args, input) .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> { pub struct SymbolFactory<'a, C: 'a + CommandRunner, R: 'a + SymbolRunner, P: 'a + Policy> {
command_runner: &'a C, command_runner: &'a C,
acme_command_runner: SetuidCommandRunner<'a, C>,
acme_command_runner: SetuidCommandRunner<'a, Cow<'a, str>, C>,
symbol_runner: &'a R, symbol_runner: &'a R,
policy: &'a P, 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>, 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>, home: Cow<'a, Path>,
user_name: Cow<'a, str>, user_name: Cow<'a, str>,
service_name: &'a str, service_name: &'a str,

Loading…
Cancel
Save