| 1 | use std::{ |
| 2 | ffi::OsString, |
| 3 | ops, |
| 4 | os::unix::prelude::OsStrExt, |
| 5 | path::{Path, PathBuf}, |
| 6 | str, |
| 7 | }; |
| 8 | |
| 9 | use super::{Error, SudoString}; |
| 10 | |
| 11 | /// A `PathBuf` guaranteed to not contain null bytes and be UTF-8 encoded |
| 12 | #[derive(Clone, Debug, PartialEq)] |
| 13 | #[cfg_attr(test, derive(Eq))] |
| 14 | pub struct SudoPath { |
| 15 | inner: String, |
| 16 | } |
| 17 | |
| 18 | impl SudoPath { |
| 19 | pub fn new(path: PathBuf) -> Result<Self, Error> { |
| 20 | let bytes = path.as_os_str().as_bytes(); |
| 21 | if bytes.contains(&0) { |
| 22 | return Err(Error::PathValidation(path)); |
| 23 | } |
| 24 | |
| 25 | // check this through a reference so we can return `path` in the error case |
| 26 | if str::from_utf8(bytes).is_err() { |
| 27 | return Err(Error::PathValidation(path)); |
| 28 | } |
| 29 | |
| 30 | Ok(Self { |
| 31 | // NOTE(unwrap): UTF-8 encoding is checked above |
| 32 | inner: path.into_os_string().into_string().unwrap(), |
| 33 | }) |
| 34 | } |
| 35 | |
| 36 | pub fn from_cli_string(cli_string: impl Into<String>) -> Self { |
| 37 | Self::new(cli_string.into().into()) |
| 38 | .expect("strings that come in from CLI should not have interior null bytes") |
| 39 | } |
| 40 | |
| 41 | /// Resolve the use of a '~' that occurs in this `SudoPathBuf`; based on the sudoers context |
| 42 | pub fn expand_tilde_in_path(&self, default_username: &SudoString) -> Result<SudoPath, Error> { |
| 43 | if let Some(prefix) = self.inner.strip_prefix('~') { |
| 44 | let (username, relpath) = prefix.split_once('/').unwrap_or((prefix, "")); |
| 45 | |
| 46 | let username = if username.is_empty() { |
| 47 | default_username.clone() |
| 48 | } else { |
| 49 | SudoString::new(username.to_string()).unwrap() |
| 50 | }; |
| 51 | |
| 52 | let home_dir = crate::system::User::from_name(username.as_cstr()) |
| 53 | .ok() |
| 54 | .flatten() |
| 55 | .ok_or(Error::UserNotFound(username.to_string()))? |
| 56 | .home; |
| 57 | let path = home_dir.join(relpath); |
| 58 | |
| 59 | Self::new(path) |
| 60 | } else { |
| 61 | Ok(self.clone()) |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | impl From<SudoPath> for PathBuf { |
| 67 | fn from(value: SudoPath) -> Self { |
| 68 | value.inner.into() |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | impl AsRef<Path> for SudoPath { |
| 73 | fn as_ref(&self) -> &Path { |
| 74 | self.inner.as_ref() |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | impl ops::Deref for SudoPath { |
| 79 | type Target = Path; |
| 80 | |
| 81 | fn deref(&self) -> &Self::Target { |
| 82 | self.as_ref() |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | impl TryFrom<String> for SudoPath { |
| 87 | type Error = Error; |
| 88 | |
| 89 | fn try_from(value: String) -> Result<Self, Self::Error> { |
| 90 | Self::new(value.into()) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl From<SudoPath> for OsString { |
| 95 | fn from(value: SudoPath) -> Self { |
| 96 | value.inner.into() |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | #[cfg(test)] |
| 101 | impl From<&'_ str> for SudoPath { |
| 102 | fn from(value: &'_ str) -> Self { |
| 103 | Self::new(value.into()).unwrap() |
| 104 | } |
| 105 | } |