diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/shirabe-php-shim/src/process.rs | 58 | ||||
| -rw-r--r-- | crates/shirabe/src/util/platform.rs | 30 |
2 files changed, 35 insertions, 53 deletions
diff --git a/crates/shirabe-php-shim/src/process.rs b/crates/shirabe-php-shim/src/process.rs index e150d649..55cb7d00 100644 --- a/crates/shirabe-php-shim/src/process.rs +++ b/crates/shirabe-php-shim/src/process.rs @@ -396,42 +396,36 @@ pub fn getmypid() -> i64 { std::process::id() as i64 } -pub fn posix_getuid() -> i64 { - nix::unistd::getuid().as_raw() as i64 +pub fn posix_getuid() -> u32 { + nix::unistd::getuid().as_raw() } -pub fn posix_geteuid() -> i64 { - nix::unistd::geteuid().as_raw() as i64 +pub fn posix_geteuid() -> u32 { + nix::unistd::geteuid().as_raw() } -/// Looks up the passwd entry for `uid` and returns it in the shape of PHP's `posix_getpwuid` -/// associative array, or `false` when no entry matches. -pub fn posix_getpwuid(uid: i64) -> PhpMixed { - let user = match nix::unistd::User::from_uid(nix::unistd::Uid::from_raw(uid as u32)) { - Ok(Some(user)) => user, - _ => return PhpMixed::Bool(false), - }; - let mut entry = indexmap::IndexMap::new(); - entry.insert("name".to_string(), PhpMixed::String(user.name)); - entry.insert( - "passwd".to_string(), - PhpMixed::String(user.passwd.to_string_lossy().into_owned()), - ); - entry.insert("uid".to_string(), PhpMixed::Int(user.uid.as_raw() as i64)); - entry.insert("gid".to_string(), PhpMixed::Int(user.gid.as_raw() as i64)); - entry.insert( - "gecos".to_string(), - PhpMixed::String(user.gecos.to_string_lossy().into_owned()), - ); - entry.insert( - "dir".to_string(), - PhpMixed::String(user.dir.to_string_lossy().into_owned()), - ); - entry.insert( - "shell".to_string(), - PhpMixed::String(user.shell.to_string_lossy().into_owned()), - ); - PhpMixed::Array(entry) +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct PasswdEntry { + pub name: String, + pub passwd: String, + pub uid: i64, + pub gid: i64, + pub gecos: String, + pub dir: String, + pub shell: String, +} + +pub fn posix_getpwuid(uid: u32) -> Option<PasswdEntry> { + let user = nix::unistd::User::from_uid(nix::unistd::Uid::from_raw(uid)).ok()??; + Some(PasswdEntry { + name: user.name, + passwd: user.passwd.to_string_lossy().into_owned(), + uid: user.uid.as_raw() as i64, + gid: user.gid.as_raw() as i64, + gecos: user.gecos.to_string_lossy().into_owned(), + dir: user.dir.to_string_lossy().into_owned(), + shell: user.shell.to_string_lossy().into_owned(), + }) } pub fn posix_isatty(stream: PhpResource) -> bool { diff --git a/crates/shirabe/src/util/platform.rs b/crates/shirabe/src/util/platform.rs index ccdd30b6..2cef4109 100644 --- a/crates/shirabe/src/util/platform.rs +++ b/crates/shirabe/src/util/platform.rs @@ -5,9 +5,9 @@ use crate::util::Silencer; use shirabe_pcre::Preg; use shirabe_php_shim::{ PHP_ENV, PHP_SERVER, PhpMixed, PhpResource, RuntimeException, defined, file_exists, - file_get_contents, fstat, function_exists, getcwd, getenv, ini_get, is_array, is_readable, - mb_strlen, php_os_family, php_regex, posix_geteuid, posix_getpwuid, posix_getuid, posix_isatty, - putenv, putenv_clear, realpath, stream_isatty, stripos, strlen, strtoupper, substr, usleep, + file_get_contents, fstat, function_exists, getcwd, getenv, ini_get, is_readable, mb_strlen, + php_os_family, php_regex, posix_geteuid, posix_getpwuid, posix_getuid, posix_isatty, putenv, + putenv_clear, realpath, stream_isatty, stripos, strlen, strtoupper, substr, usleep, }; use std::sync::Mutex; @@ -146,16 +146,11 @@ impl Platform { return Ok(home); } - if function_exists("posix_getuid") && function_exists("posix_getpwuid") { - let info = posix_getpwuid(posix_getuid()); - - if is_array(&info) - && let Some(arr) = info.as_array() - && let Some(dir) = arr.get("dir") - && let Some(s) = dir.as_string() - { - return Ok(s.to_string()); - } + if function_exists("posix_getuid") + && function_exists("posix_getpwuid") + && let Some(info) = posix_getpwuid(posix_getuid()) + { + return Ok(info.dir); } Err(RuntimeException::new("Could not determine user directory".to_string()).into()) @@ -332,14 +327,7 @@ impl Platform { if function_exists("posix_getpwuid") && function_exists("posix_geteuid") { let process_user = posix_getpwuid(posix_geteuid()); - if is_array(&process_user) - && let Some(arr) = process_user.as_array() - && arr - .get("name") - .and_then(|v| v.as_string()) - .map(|s| s == "vagrant") - .unwrap_or(false) - { + if process_user.is_some_and(|process_user| process_user.name == "vagrant") { *cached = Some(true); return true; } |
