Skip to content

Commit

Permalink
[newgrp] requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Wandalen committed Oct 23, 2024
1 parent 337225a commit 642699c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 48 deletions.
2 changes: 1 addition & 1 deletion users/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ gettext-rs.workspace = true
libc.workspace = true
chrono.workspace = true
syslog = "6.1"
libcrypt-rs = "0.1.2"
libcrypt-rs = "0.1"
thiserror = "1.0"
binrw = "0.14"

Expand Down
65 changes: 18 additions & 47 deletions users/newgrp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,20 @@ const MAX_GROUPS: usize = libc::KERN_NGROUPS_MAX as usize;
#[cfg(target_os = "macos")]
const MAX_GROUPS: usize = 16;

/// newgrp — change to a new group
#[derive(Parser, Debug)]
#[command(author, version, about, long_about)]
#[derive(Parser)]
#[command(version, about = "newgrp — change to a new group")]
struct Args {
/// Change the environment to what would be expected if the user actually logged in again (letter `l`).
#[arg(short = 'l')]
#[arg(
short = 'l',
help = "Change the environment to what would be expected if the user actually logged in again (letter 'l')."
)]
login: bool,

/// Specifies the group ID or group name. This is a positional argument that must be provided.
#[arg(value_name = "GROUP", required = true)]
#[arg(
value_name = "GROUP",
required = true,
help = "Specifies the group ID or group name. This is a positional argument that must be provided."
)]
group: String,
}

Expand Down Expand Up @@ -496,10 +500,10 @@ fn find_matching_group(group_identifier: &str, groups: &Vec<Group>) -> Option<Gr
///
fn logger(name: &str, gid: u32) {
// Get the current login name
let loginname = get_current_login().unwrap_or("???".to_string());
let loginname = plib::curuser::login_name();

// Get the current tty device name
let tty = get_current_tty().unwrap_or("???".to_string());
let tty = plib::curuser::tty();

// Log the message
eprintln!(
Expand All @@ -508,39 +512,6 @@ fn logger(name: &str, gid: u32) {
);
}

/// Retrieves the current login name.
fn get_current_login() -> Option<String> {
unsafe {
let login_ptr = libc::getlogin();
if !login_ptr.is_null() {
CStr::from_ptr(login_ptr)
.to_str()
.ok()
.map(|s| s.to_string())
} else {
None
}
}
}

/// Retrieves the current tty name.
fn get_current_tty() -> Option<String> {
unsafe {
let tty_ptr = libc::ttyname(0);
if !tty_ptr.is_null() {
CStr::from_ptr(tty_ptr).to_str().ok().map(|s| {
if s.starts_with("/dev/") {
s[5..].to_string()
} else {
s.to_string()
}
})
} else {
None
}
}
}

/// Checks permissions for accessing a specified group based on the provided user credentials.
///
/// This function determines whether the user associated with the given password structure
Expand Down Expand Up @@ -822,6 +793,11 @@ fn set_login_environment(user: &str) -> Result<(), io::Error> {
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
setlocale(LocaleCategory::LcAll, "");
textdomain(PROJECT_NAME)?;
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
let mut exit_code = 0;

let args = Args::try_parse().unwrap_or_else(|err| {
if err.kind() == ErrorKind::DisplayHelp || err.kind() == ErrorKind::DisplayVersion {
// Print help or version message
Expand All @@ -835,11 +811,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
std::process::exit(1);
});

setlocale(LocaleCategory::LcAll, "");
textdomain(PROJECT_NAME)?;
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;
let mut exit_code = 0;

if let Err(err) = newgrp(args) {
exit_code = 1;
eprint!("{}", err);
Expand Down

0 comments on commit 642699c

Please sign in to comment.