From 5d14eb6a7660270cac20761cbbe9a925cab87bf2 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 15 Feb 2019 15:33:12 +0100 Subject: [PATCH] tools.rs: new helper to get uid/gid for the sepcified system user. Or is there an easier way to get that info? --- src/tools.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/tools.rs b/src/tools.rs index f97831f7..85c93da0 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -48,6 +48,7 @@ macro_rules! try_block { { $($token:tt)* } => {{ (|| -> Result<_,_> { $($token)* })() }} } + /// The `BufferedReader` trait provides a single function /// `buffered_read`. It returns a reference to an internal buffer. The /// purpose of this traid is to avoid unnecessary data copies. @@ -277,7 +278,19 @@ pub fn file_chunker( Ok(()) } -/// Returns the hosts node name (UTS node name) +// Returns the Unix uid/gid for the sepcified system user. +pub fn getpwnam_ugid(username: &str) -> Result<(libc::uid_t,libc::gid_t), Error> { + let info = unsafe { libc::getpwnam(std::ffi::CString::new(username).unwrap().as_ptr()) }; + if info == std::ptr::null_mut() { + bail!("getwpnam '{}' failed", username); + } + + let info = unsafe { *info }; + + Ok((info.pw_uid, info.pw_gid)) +} + +// Returns the hosts node name (UTS node name) pub fn nodename() -> &'static str { lazy_static!{