Skip to content

Commit

Permalink
chown: allow setting arbitrary numeric user ID
Browse files Browse the repository at this point in the history
Update `chown` to allow setting the owner of a file to a numeric user
ID regardless of whether a corresponding username exists on the
system.

For example,

    $ touch f && sudo chown 12345 f

succeeds even though there is no named user with ID 12345.

Fixes #3380.
  • Loading branch information
jfinkels committed Apr 23, 2022
1 parent 5088ddc commit b01fe3f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/uu/chown/src/chown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,17 @@ fn parse_spec(spec: &str, sep: char) -> UResult<(Option<u32>, Option<u32>)> {
// So, try to parse it this way
return parse_spec(spec, '.');
} else {
return Err(USimpleError::new(
1,
format!("invalid user: {}", spec.quote()),
));
// It's possible that the `user` string contains a
// numeric user ID, in which case, we respect that.
match user.parse() {
Ok(uid) => uid,
Err(_) => {
return Err(USimpleError::new(
1,
format!("invalid user: {}", spec.quote()),
))
}
}
}
}
})
Expand Down
21 changes: 21 additions & 0 deletions tests/by-util/test_chown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,27 @@ fn test_chown_only_user_id() {
.stderr_contains(&"failed to change");
}

/// Test for setting the owner to a user ID for a user that does not exist.
///
/// For example:
///
/// $ touch f && chown 12345 f
///
/// succeeds with exit status 0 and outputs nothing. The owner of the
/// file is set to 12345, even though no user with that ID exists.
///
/// This test must be run as root, because only the root user can
/// transfer ownership of a file.
#[test]
fn test_chown_only_user_id_nonexistent_user() {
let (at, mut ucmd) = at_and_ucmd!();
at.touch("f");
ucmd.args(&["12345", "f"])
.succeeds()
.no_stdout()
.no_stderr();
}

#[test]
// FixME: stderr = chown: ownership of 'test_chown_file1' retained as cuuser:wheel
#[cfg(not(target_os = "freebsd"))]
Expand Down

0 comments on commit b01fe3f

Please sign in to comment.