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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/uu/id/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ doctest = false

[dependencies]
clap = { workspace = true }
rustix = { workspace = true }
uucore = { workspace = true, features = ["entries", "process"] }
selinux = { workspace = true, optional = true }
fluent = { workspace = true }
Expand Down
30 changes: 15 additions & 15 deletions src/uu/id/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ use uucore::entries::{self, Group, Locate, Passwd};
use uucore::error::UResult;
use uucore::error::{USimpleError, set_exit_code};
pub use uucore::libc;
use uucore::libc::{getlogin, uid_t};
use uucore::libc::getlogin;
use uucore::line_ending::LineEnding;
use uucore::translate;

use uucore::process::{getegid, geteuid, getgid, getuid};
use rustix::process::{Uid, getegid, geteuid, getgid, getuid};
use uucore::{format_usage, show_error};

macro_rules! cstr2cow {
Expand Down Expand Up @@ -245,7 +245,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
// GNU's `id` does not support the flags: -p/-P/-A.
if matches.get_flag(options::OPT_PASSWORD) {
// BSD's `id` ignores all but the first specified user
pline(possible_pw.as_ref().map(|v| v.uid))?;
pline(possible_pw.as_ref().map(|v| Uid::from_raw(v.uid)))?;
return Ok(());
}
if matches.get_flag(options::OPT_HUMAN_READABLE) {
Expand All @@ -263,18 +263,18 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
{
let use_effective = !state.rflag && (state.uflag || state.gflag || state.gsflag);
if use_effective {
(geteuid(), getegid())
(geteuid().as_raw(), getegid().as_raw())
} else {
(getuid(), getgid())
(getuid().as_raw(), getgid().as_raw())
}
},
|p| (p.uid, p.gid),
);
state.ids = Some(Ids {
uid,
gid,
euid: geteuid(),
egid: getegid(),
euid: geteuid().as_raw(),
egid: getegid().as_raw(),
});

if state.gflag {
Expand Down Expand Up @@ -499,7 +499,7 @@ fn pretty(possible_pw: Option<Passwd>) -> io::Result<()> {
)?;
} else {
let login = cstr2cow!(getlogin().cast_const());
let uid = getuid();
let uid = getuid().as_raw();
if let Ok(p) = Passwd::locate(uid) {
if let Some(user_name) = login {
writeln!(lock, "{}\t{user_name}", translate!("id-output-login"))?;
Expand All @@ -509,7 +509,7 @@ fn pretty(possible_pw: Option<Passwd>) -> io::Result<()> {
writeln!(lock, "{}\t{uid}", translate!("id-output-uid"))?;
}

let euid = geteuid();
let euid = geteuid().as_raw();
if euid != uid {
if let Ok(p) = Passwd::locate(euid) {
writeln!(lock, "{}\t{}", translate!("id-output-euid"), p.name)?;
Expand All @@ -518,8 +518,8 @@ fn pretty(possible_pw: Option<Passwd>) -> io::Result<()> {
}
}

let rgid = getgid();
let egid = getegid();
let rgid = getgid().as_raw();
let egid = getegid().as_raw();
if egid != rgid {
if let Ok(g) = Group::locate(rgid) {
writeln!(lock, "{}\t{}", translate!("id-output-rgid"), g.name)?;
Expand All @@ -544,9 +544,9 @@ fn pretty(possible_pw: Option<Passwd>) -> io::Result<()> {
}

#[cfg(any(target_vendor = "apple", target_os = "freebsd"))]
fn pline(possible_uid: Option<uid_t>) -> io::Result<()> {
fn pline(possible_uid: Option<Uid>) -> io::Result<()> {
let uid = possible_uid.unwrap_or_else(getuid);
let pw = Passwd::locate(uid)?;
let pw = Passwd::locate(uid.as_raw())?;

writeln!(
io::stdout().lock(),
Expand All @@ -571,9 +571,9 @@ fn pline(possible_uid: Option<uid_t>) -> io::Result<()> {
target_os = "cygwin",
target_os = "netbsd"
))]
fn pline(possible_uid: Option<uid_t>) -> io::Result<()> {
fn pline(possible_uid: Option<Uid>) -> io::Result<()> {
let uid = possible_uid.unwrap_or_else(getuid);
let pw = Passwd::locate(uid)?;
let pw = Passwd::locate(uid.as_raw())?;

writeln!(
io::stdout().lock(),
Expand Down
6 changes: 2 additions & 4 deletions tests/by-util/test_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,23 +483,21 @@ fn test_id_pretty_print_password_record() {
#[test]
#[cfg(all(feature = "chmod", feature = "chown"))]
fn test_id_pretty_print_suid_binary() {
use uucore::process::{getgid, getuid};

if let Some(suid_coreutils_path) = create_root_owned_suid_coreutils_binary() {
let result = TestScenario::new(util_name!())
.cmd(suid_coreutils_path.to_str().unwrap())
.args(&[util_name!(), "-p"])
.succeeds();

// The `euid` line should be present only if the real UID does not belong to `root`
if getuid() == 0 {
if rustix::process::getuid().is_root() {
result.stdout_does_not_contain("euid\t");
} else {
result.stdout_contains_line("euid\troot");
}

// The `rgid` line should be present only if the real GID does not belong to `root`
if getgid() == 0 {
if rustix::process::getgid().is_root() {
result.stdout_does_not_contain("rgid\t");
} else {
result.stdout_contains("rgid\t");
Expand Down
Loading