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
14 changes: 11 additions & 3 deletions src/uu/useradd/src/useradd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ mod exit_codes {
pub const USERNAME_IN_USE: i32 = 9;
pub const CANNOT_UPDATE_GROUP: i32 = 10;
pub const CANNOT_CREATE_HOME: i32 = 12;
pub const BAD_NAME: i32 = 19;
}

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -105,6 +106,8 @@ enum UseraddError {
CannotUpdateGroup(String),
/// Exit 12 -- cannot create home directory.
CannotCreateHome(String),
/// Exit 19 -- invalid user or group name.
BadName(String),
}

impl fmt::Display for UseraddError {
Expand All @@ -117,7 +120,8 @@ impl fmt::Display for UseraddError {
| Self::GroupNotExist(msg)
| Self::UsernameInUse(msg)
| Self::CannotUpdateGroup(msg)
| Self::CannotCreateHome(msg) => f.write_str(msg),
| Self::CannotCreateHome(msg)
| Self::BadName(msg) => f.write_str(msg),
}
}
}
Expand All @@ -135,6 +139,7 @@ impl UError for UseraddError {
Self::UsernameInUse(_) => 9,
Self::CannotUpdateGroup(_) => 10,
Self::CannotCreateHome(_) => 12,
Self::BadName(_) => 19,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please add a BAD_NAME: i32 = 19 to exit_codes and to test_error_codes, like the others?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 5038265, BAD_NAME: i32 = 19 is in exit_codes and checked in test_error_codes.

}
}
}
Expand Down Expand Up @@ -545,8 +550,7 @@ fn parse_options(matches: &clap::ArgMatches) -> Result<UseraddOptions, UseraddEr
#[allow(clippy::too_many_lines)]
fn do_useradd(opts: &UseraddOptions) -> UResult<()> {
// Step 1: Validate username.
validate::validate_username(&opts.login)
.map_err(|e| UseraddError::BadArgument(format!("{e}")))?;
validate::validate_username(&opts.login).map_err(|e| UseraddError::BadName(format!("{e}")))?;

// Step 2: Block signals for the lock→write critical section only.
// Dropped after file writes complete so home creation remains interruptible.
Expand Down Expand Up @@ -1684,6 +1688,10 @@ mod tests {
UseraddError::CannotCreateHome(String::new()).code(),
exit_codes::CANNOT_CREATE_HOME
);
assert_eq!(
UseraddError::BadName(String::new()).code(),
exit_codes::BAD_NAME
);
}

// -----------------------------------------------------------------------
Expand Down
18 changes: 18 additions & 0 deletions tests/by-util/test_useradd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,3 +813,21 @@ fn test_unusable_shadow_file_leaves_passwd_untouched() {
"passwd was written although shadow could not be: {passwd:?}"
);
}

#[test]
fn test_invalid_name_exits_19() {
if crate::common::skip_unless_root() {
return;
}

let dir = setup_root_dir();
for name in ["bad:name", "1234"] {
let code = run_with_root(&dir, &["-M", "-N", name]);
assert_eq!(
code, 19,
"useradd(8): invalid user name should exit 19 ({name})"
);
}
let passwd = std::fs::read_to_string(dir.path().join("etc/passwd")).unwrap();
assert_eq!(passwd, "root:x:0:0:root:/root:/bin/bash\n");
}
Loading