A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.9 KiB

7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
8 years ago
5 years ago
8 years ago
  1. use std::error::Error;
  2. use std::ffi::OsStr;
  3. use std::io::Result as IoResult;
  4. use std::process::Command;
  5. use std::process::Output;
  6. pub trait CommandRunner {
  7. fn run_with_args<S: AsRef<OsStr> + ?Sized>(&self, program: &str, args: &[&S]) -> IoResult<Output>;
  8. fn get_output<S: AsRef<OsStr> + ?Sized>(&self, program: &str, args: &[&S]) -> Result<Vec<u8>, Box<Error>> {
  9. let output = try!(self.run_with_args(program, args));
  10. if !output.status.success() {
  11. return Err(try!(String::from_utf8(output.stderr)).into());
  12. }
  13. Ok(output.stdout)
  14. }
  15. fn get_stderr<S: AsRef<OsStr> + ?Sized>(&self, program: &str, args: &[&S]) -> Result<Vec<u8>, Box<Error>> {
  16. let output = try!(self.run_with_args(program, args));
  17. if !output.status.success() {
  18. return Err(try!(String::from_utf8(output.stderr)).into());
  19. }
  20. Ok(output.stderr)
  21. }
  22. fn run_successfully<S: AsRef<OsStr> + ?Sized>(&self, program: &str, args: &[&S]) -> Result<(), Box<Error>> {
  23. let output = try!(self.run_with_args(program, args));
  24. if output.status.success() {
  25. Ok(())
  26. } else {
  27. Err(try!(String::from_utf8(output.stderr)).into())
  28. }
  29. }
  30. }
  31. #[derive(Debug)]
  32. pub struct StdCommandRunner;
  33. impl CommandRunner for StdCommandRunner {
  34. fn run_with_args<S: AsRef<OsStr> + ?Sized>(&self, program: &str, args: &[&S]) -> IoResult<Output> {
  35. // FIXME: logger
  36. //println!("{} {:?}", program, args);
  37. let res = Command::new(program).args(args).output();
  38. println!("{:?}", res);
  39. res
  40. }
  41. }
  42. #[derive(Debug)]
  43. pub struct SetuidCommandRunner<'a, C> where C: 'a + CommandRunner {
  44. command_runner: &'a C,
  45. user_name: &'a str
  46. }
  47. impl<'a, C> SetuidCommandRunner<'a, C> where C: 'a + CommandRunner {
  48. pub fn new(user_name: &'a str, command_runner: &'a C) -> SetuidCommandRunner<'a, C> {
  49. SetuidCommandRunner { command_runner, user_name }
  50. }
  51. }
  52. use std::os::unix::process::CommandExt;
  53. use std::env;
  54. use users::get_user_by_name;
  55. struct TempSetEnv<'a> { name: &'a str, old_value: Option<String> }
  56. impl<'a> TempSetEnv<'a> {
  57. fn new(name: &'a str, new_value: String) -> TempSetEnv<'a> {
  58. let old_value = env::var(name);
  59. env::set_var(name, new_value);
  60. TempSetEnv { name, old_value: old_value.ok() }
  61. }
  62. }
  63. impl<'a> Drop for TempSetEnv<'a> {
  64. fn drop(&mut self) {
  65. match self.old_value {
  66. Some(ref val) => env::set_var(self.name, val),
  67. None => env::remove_var(self.name)
  68. }
  69. }
  70. }
  71. impl<'a, C> CommandRunner for SetuidCommandRunner<'a, C> where C: 'a + CommandRunner {
  72. fn run_with_args<S: AsRef<OsStr> + ?Sized>(&self, program: &str, args: &[&S]) -> IoResult<Output> {
  73. let uid = get_user_by_name(self.user_name).expect("User does not exist").uid();
  74. let set_home = TempSetEnv::new("HOME", format!("/home/{}", self.user_name));
  75. let set_dbus = TempSetEnv::new("XDG_RUNTIME_DIR", format!("/run/user/{}", uid));
  76. //println!("{} {:?}", program, args);
  77. let res = Command::new(program).uid(uid).gid(uid).args(args).output();
  78. println!("{:?}", res);
  79. res
  80. }
  81. }
  82. #[derive(Debug)]
  83. pub struct SuCommandRunner<'a, C> where C: 'a + CommandRunner {
  84. command_runner: &'a C,
  85. user_name: &'a str
  86. }
  87. impl<'a, C> SuCommandRunner<'a, C> where C: 'a + CommandRunner {
  88. pub fn new(user_name: &'a str, command_runner: &'a C) -> SuCommandRunner<'a, C> {
  89. SuCommandRunner { command_runner, user_name }
  90. }
  91. }
  92. // Su doesn't set XDG_RUNTIME_DIR
  93. // https://github.com/systemd/systemd/blob/master/src/login/pam_systemd.c#L439
  94. impl<'a, C> CommandRunner for SuCommandRunner<'a, C> where C: 'a + CommandRunner {
  95. fn run_with_args<S: AsRef<OsStr> + ?Sized>(&self, program: &str, args: &[&S]) -> IoResult<Output> {
  96. let raw_new_args = [self.user_name, "-s", "/usr/bin/env", "--", program];
  97. let mut new_args: Vec<&OsStr> = raw_new_args.iter().map(|s| s.as_ref()).collect();
  98. let old_args: Vec<&OsStr> = args.iter().map(|s| s.as_ref()).collect();
  99. new_args.extend_from_slice(&old_args);
  100. self.command_runner.run_with_args("su", &new_args)
  101. }
  102. }
  103. #[cfg(test)]
  104. mod test {
  105. }