From 70786ebf40088dad0a43c03212b01f6e28ed029d Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Sun, 26 Dec 2021 01:44:46 +0100 Subject: [PATCH] Simplify SetuidCommandRunner --- src/builder.rs | 19 +++++-------------- src/command_runner.rs | 14 +++++--------- src/symbols/systemd/user_service.rs | 19 +++++++------------ 3 files changed, 17 insertions(+), 35 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 9ec4fb1..4d5ab61 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -115,12 +115,8 @@ impl ImplementationBuilder> for DefaultBuilder { ) } - type Implementation = CertSymbol< - SetuidCommandRunner<'static, String, StdCommandRunner>, - SetuidCommandRunner<'static, String, StdCommandRunner>, - D, - PathBuf, - >; + type Implementation = + CertSymbol, SetuidCommandRunner, D, PathBuf>; fn create( resource: &Cert, target: & as Resource>::Artifact, @@ -128,7 +124,7 @@ impl ImplementationBuilder> for DefaultBuilder { ) -> Self::Implementation { CertSymbol::new( resource.0.clone(), - SetuidCommandRunner::new(user_name.0, &StdCommandRunner), + SetuidCommandRunner::new(user_name.0), root_cert.into(), account_key.into(), challenges_dir.into(), @@ -448,7 +444,7 @@ impl> ImplementationBuilder> for De FileSymbol, SystemdUserSessionSymbol<'static, String, StdCommandRunner>, OwnerSymbol, - UserServiceSymbol<'static, PathBuf, String, StdCommandRunner>, + UserServiceSymbol<'static, PathBuf, String>, ); fn create( resource: &SystemdSocketService, @@ -475,12 +471,7 @@ impl> ImplementationBuilder> for De user_name.0.clone(), StdCommandRunner, ), - UserServiceSymbol::new( - socket_path.clone().into(), - user_name.0.clone(), - resource.1, - &StdCommandRunner, - ), + UserServiceSymbol::new(socket_path.clone().into(), user_name.0.clone(), resource.1), ) } } diff --git a/src/command_runner.rs b/src/command_runner.rs index 5be8ddc..fb6b7c0 100644 --- a/src/command_runner.rs +++ b/src/command_runner.rs @@ -78,17 +78,13 @@ impl CommandRunner for StdCommandRunner { } #[derive(Debug)] -pub struct SetuidCommandRunner<'a, U: AsRef, C: CommandRunner> { - command_runner: &'a C, +pub struct SetuidCommandRunner> { user_name: U, } -impl<'a, U: AsRef, C: CommandRunner> SetuidCommandRunner<'a, U, C> { - pub fn new(user_name: U, command_runner: &'a C) -> Self { - SetuidCommandRunner { - command_runner, - user_name, - } +impl<'a, U: AsRef> SetuidCommandRunner { + pub fn new(user_name: U) -> Self { + Self { user_name } } } @@ -121,7 +117,7 @@ impl Drop for TempSetEnv<'_> { } #[async_trait(?Send)] -impl, C: CommandRunner> CommandRunner for SetuidCommandRunner<'_, U, C> { +impl> CommandRunner for SetuidCommandRunner { async fn run(&self, program: &str, args: &[&OsStr], input: &[u8]) -> IoResult { let uid = get_user_by_name(self.user_name.as_ref()) .expect("User does not exist") diff --git a/src/symbols/systemd/user_service.rs b/src/symbols/systemd/user_service.rs index f795786..9559eea 100644 --- a/src/symbols/systemd/user_service.rs +++ b/src/symbols/systemd/user_service.rs @@ -8,28 +8,23 @@ use std::path::Path; use std::time::Duration; #[derive(Debug)] -pub struct UserService<'a, S: AsRef, U: AsRef, R: CommandRunner> { +pub struct UserService<'a, S: AsRef, U: AsRef> { socket_path: S, service_name: &'a str, - command_runner: SetuidCommandRunner<'a, U, R>, + command_runner: SetuidCommandRunner, } -impl, U: AsRef, R: CommandRunner> UserService<'static, S, U, R> { - pub fn new( - socket_path: S, - user_name: U, - service_name: &'static str, - command_runner: &'static R, - ) -> Self { +impl, U: AsRef> UserService<'static, S, U> { + pub fn new(socket_path: S, user_name: U, service_name: &'static str) -> Self { Self { socket_path, service_name, - command_runner: SetuidCommandRunner::new(user_name, command_runner), + command_runner: SetuidCommandRunner::new(user_name), } } } -impl, U: AsRef, R: CommandRunner> UserService<'_, S, U, R> { +impl, U: AsRef> UserService<'_, S, U> { async fn systemctl_wait_for_dbus(&self, args: &[&OsStr]) -> Result> { let mut tries = 5; loop { @@ -85,7 +80,7 @@ impl, U: AsRef, R: CommandRunner> UserService<'_, S, U, R> { } #[async_trait(?Send)] -impl, U: AsRef, R: CommandRunner> Symbol for UserService<'_, S, U, R> { +impl, U: AsRef> Symbol for UserService<'_, S, U> { async fn target_reached(&self) -> Result> { self.check_if_service().await }