Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions plib/src/group.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//
// Copyright (c) 2024 Jeff Garzik
//
// This file is part of the posixutils-rs project covered under
// the MIT License. For the full license text, please see the LICENSE
// file in the root directory of this project.
// SPDX-License-Identifier: MIT
//

extern crate libc;
use libc::{endgrent, getgrent, setgrent};
use std::ffi::CStr;
use std::ptr;

pub struct Group {
pub name: String,
pub passwd: String,
pub gid: libc::gid_t,
pub members: Vec<String>,
}

pub fn load() -> Vec<Group> {
let mut groups = Vec::new();

unsafe {
setgrent(); // Initialize the group entry stream
let mut groupent = getgrent(); // Get the first group entry

// Loop through all group entries
while !groupent.is_null() {
let group = &*groupent;

// copy basic group fields: name, passwd, gid
let name = CStr::from_ptr(group.gr_name).to_string_lossy().to_string();
let passwd = CStr::from_ptr(group.gr_passwd)
.to_string_lossy()
.to_string();
let gid = group.gr_gid;

// copy group members, a null-terminated array of C strings
let mut members = Vec::new();
if !group.gr_mem.is_null() {
let mut member_ptr_arr = group.gr_mem;

// Loop through all group members
// read_unaligned is necessary on MacOS, possibly
// other platforms, to avoid alignment issues
while !ptr::read_unaligned(member_ptr_arr).is_null() {
let member_ptr = ptr::read_unaligned(member_ptr_arr);
let member = CStr::from_ptr(member_ptr).to_string_lossy().to_string();
members.push(member);
member_ptr_arr = member_ptr_arr.add(1);
}
}

// Add the group to the returned list of groups
groups.push(Group {
name,
passwd,
gid,
members,
});

groupent = getgrent(); // Move to the next group entry
}

endgrent(); // Close the group entry stream
}

groups
}
1 change: 1 addition & 0 deletions plib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use std::io::Write;
use std::process::{Command, Stdio};

pub mod group;
pub mod modestr;

pub const PROJECT_NAME: &'static str = "posixutils-rs";
Expand Down
44 changes: 17 additions & 27 deletions users/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,35 +125,25 @@ fn get_user_info(args: &Args) -> Result<UserInfo, Box<dyn std::error::Error>> {
Ok(userinfo)
}

fn load_group_db() -> Vec<libc::group> {
let mut groups = Vec::new();

unsafe { libc::setgrent() };

loop {
let group = unsafe { libc::getgrent() };
if group.is_null() {
break;
}

groups.push(unsafe { *group });
}

unsafe { libc::endgrent() };

groups
}

fn get_group_info(userinfo: &mut UserInfo) -> Result<(), Box<dyn std::error::Error>> {
let groups = load_group_db();
let groups = plib::group::load();

for group in &groups {
userinfo.groups.push(group.gr_gid);
userinfo.group_names.insert(group.gr_gid, unsafe {
std::ffi::CStr::from_ptr(group.gr_name)
.to_string_lossy()
.to_string()
});
// skip groups that the user is not a member of
let mut found = false;
for member in &group.members {
if *member == userinfo.username {
found = true;
break;
}
}
if !found && group.gid != userinfo.gid {
continue;
}

// add group to user's group list
userinfo.groups.push(group.gid);
userinfo.group_names.insert(group.gid, group.name.clone());
}

Ok(())
Expand Down Expand Up @@ -190,7 +180,7 @@ fn display_user_info(args: &Args, userinfo: &UserInfo) {
userinfo.uid, userinfo.username, userinfo.gid, group_name, userinfo.egid
);
for gid in &userinfo.groups {
print!("{},", userinfo.group_names[gid]);
print!("{}({}),", gid, userinfo.group_names[gid]);
}
println!();
return;
Expand Down