From f5d3838e750d2ecfdbcddd06cde175983c82d315 Mon Sep 17 00:00:00 2001 From: Konstantin Stepanov Date: Tue, 21 Jul 2015 11:22:58 +0300 Subject: [PATCH] add methods to get effective user and group info --- src/lib.rs | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 88882b0..f02a228 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -151,6 +151,24 @@ pub trait Users { /// Return the username of the user running the process. fn get_current_username(&mut self) -> Option; + + /// Return the group ID for the user running the process. + fn get_current_gid(&mut self) -> gid_t; + + /// Return the group name of the user running the process. + fn get_current_groupname(&mut self) -> Option; + + /// Return the effective user id. + fn get_effective_uid(&mut self) -> uid_t; + + /// Return the effective group id. + fn get_effective_gid(&mut self) -> gid_t; + + /// Return the effective username. + fn get_effective_username(&mut self) -> Option; + + /// Return the effective group name. + fn get_effective_groupname(&mut self) -> Option; } #[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "dragonfly"))] @@ -255,6 +273,9 @@ pub struct OSUsers { groups_back: HashMap>, uid: Option, + gid: Option, + euid: Option, + egid: Option, } unsafe fn from_raw_buf(p: *const i8) -> String { @@ -437,6 +458,54 @@ impl Users for OSUsers { let uid = self.get_current_uid(); self.get_user_by_uid(uid).map(|u| u.name) } + + fn get_current_gid(&mut self) -> gid_t { + match self.gid { + Some(gid) => gid, + None => { + let gid = unsafe { getgid() }; + self.gid = Some(gid); + gid + } + } + } + + fn get_current_groupname(&mut self) -> Option { + let gid = self.get_current_gid(); + self.get_group_by_gid(gid).map(|g| g.name) + } + + fn get_effective_gid(&mut self) -> gid_t { + match self.egid { + Some(gid) => gid, + None => { + let gid = unsafe { getegid() }; + self.egid = Some(gid); + gid + } + } + } + + fn get_effective_groupname(&mut self) -> Option { + let gid = self.get_effective_gid(); + self.get_group_by_gid(gid).map(|g| g.name) + } + + fn get_effective_uid(&mut self) -> uid_t { + match self.euid { + Some(uid) => uid, + None => { + let uid = unsafe { geteuid() }; + self.euid = Some(uid); + uid + } + } + } + + fn get_effective_username(&mut self) -> Option { + let uid = self.get_effective_uid(); + self.get_user_by_uid(uid).map(|u| u.name) + } } impl OSUsers { @@ -482,6 +551,36 @@ pub fn get_current_username() -> Option { OSUsers::empty_cache().get_current_username() } +/// Return the user ID for the effective user running the process. +pub fn get_effective_uid() -> uid_t { + OSUsers::empty_cache().get_effective_uid() +} + +/// Return the username of the effective user running the process. +pub fn get_effective_username() -> Option { + OSUsers::empty_cache().get_effective_username() +} + +/// Return the group ID for the user running the process. +pub fn get_current_gid() -> gid_t { + OSUsers::empty_cache().get_current_gid() +} + +/// Return the groupname of the user running the process. +pub fn get_current_groupname() -> Option { + OSUsers::empty_cache().get_current_groupname() +} + +/// Return the group ID for the effective user running the process. +pub fn get_effective_gid() -> gid_t { + OSUsers::empty_cache().get_effective_gid() +} + +/// Return the groupname of the effective user running the process. +pub fn get_effective_groupname() -> Option { + OSUsers::empty_cache().get_effective_groupname() +} + #[cfg(test)] mod test { use super::{Users, OSUsers, get_current_username};