From 89555608014283157f8fb7f197d5a6a930d50ead Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Mon, 26 Jun 2023 20:30:44 +0100 Subject: [PATCH] useradd: Allow group name or number to be used with the `-g` option --- Base/usr/share/man/man8/useradd.md | 2 +- Userland/Utilities/useradd.cpp | 49 +++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/Base/usr/share/man/man8/useradd.md b/Base/usr/share/man/man8/useradd.md index 4829ffd707..c24784f260 100644 --- a/Base/usr/share/man/man8/useradd.md +++ b/Base/usr/share/man/man8/useradd.md @@ -19,7 +19,7 @@ This program must be run as root. ## Options * `-u`, `--uid` _uid_: The user identifier for the new user. If not specified, an unused UID above `1000` will be auto-generated. -* `-g`, `--gid` _gid_: The group identifier for the new user. If not specified, it will default to 100 (the **users** group). +* `-g`, `--gid` _group_: The group name or identifier for the new user. If not specified, it will default to 100 (the **users** group). * `-p`, `--password` _password_: The encrypted password for the new user. If not specified, it will default to blank. * `-s`, `--shell` _path-to-shell_: The shell binary for this login. The default is `/bin/Shell`. * `-m`, `--create-home`: Create the specified home directory for this new user. diff --git a/Userland/Utilities/useradd.cpp b/Userland/Utilities/useradd.cpp index 8f3e67b387..51fc5201e3 100644 --- a/Userland/Utilities/useradd.cpp +++ b/Userland/Utilities/useradd.cpp @@ -2,6 +2,7 @@ * Copyright (c) 2019-2020, Jesse Buhagiar * Copyright (c) 2021, Brandon Pruitt * Copyright (c) 2022, Umut İnan Erdoğan + * Copyright (c) 2023, Tim Ledbetter * * SPDX-License-Identifier: BSD-2-Clause */ @@ -28,13 +29,34 @@ constexpr uid_t BASE_UID = 1000; constexpr gid_t USERS_GID = 100; constexpr auto DEFAULT_SHELL = "/bin/sh"sv; +static Optional group_string_to_gid(StringView group) +{ + auto maybe_gid = group.to_uint(); + auto maybe_group_or_error = maybe_gid.has_value() + ? Core::System::getgrgid(maybe_gid.value()) + : Core::System::getgrnam(group); + + if (maybe_group_or_error.is_error()) { + warnln("Error resolving group '{}': {}", group, maybe_group_or_error.release_error()); + return {}; + } + + auto maybe_group = maybe_group_or_error.release_value(); + if (!maybe_group.has_value()) { + warnln("Group '{}' does not exist", group); + return {}; + } + + return maybe_group->gr_gid; +} + ErrorOr serenity_main(Main::Arguments arguments) { TRY(Core::System::pledge("stdio wpath rpath cpath chown")); StringView home_path; int uid = 0; - int gid = USERS_GID; + gid_t gid = USERS_GID; bool create_home_dir = false; DeprecatedString password = ""; DeprecatedString shell = DEFAULT_SHELL; @@ -44,14 +66,30 @@ ErrorOr serenity_main(Main::Arguments arguments) Core::ArgsParser args_parser; args_parser.add_option(home_path, "Home directory for the new user", "home-dir", 'd', "path"); args_parser.add_option(uid, "User ID (uid) for the new user", "uid", 'u', "uid"); - args_parser.add_option(gid, "Group ID (gid) for the new user", "gid", 'g', "gid"); + args_parser.add_option(Core::ArgsParser::Option { + .argument_mode = Core::ArgsParser::OptionArgumentMode::Required, + .help_string = "Group name or number (gid) for the new user", + .long_name = "gid", + .short_name = 'g', + .value_name = "group", + .accept_value = [&gid](StringView group) { + auto maybe_gid = group_string_to_gid(group); + if (maybe_gid.has_value()) + gid = maybe_gid.value(); + + return maybe_gid.has_value(); + }, + }); args_parser.add_option(password, "Encrypted password of the new user", "password", 'p', "password"); args_parser.add_option(create_home_dir, "Create home directory if it does not exist", "create-home", 'm'); args_parser.add_option(shell, "Path to the default shell binary for the new user", "shell", 's', "path-to-shell"); args_parser.add_option(gecos, "GECOS name of the new user", "gecos", 'n', "general-info"); args_parser.add_positional_argument(username, "Login user identity (username)", "login"); - args_parser.parse(arguments); + if (!args_parser.parse(arguments, Core::ArgsParser::FailureBehavior::Ignore)) { + args_parser.print_usage(stderr, arguments.strings[0]); + return 3; + } // Let's run a quick sanity check on username if (username.find_any_of("\\/!@#$%^&*()~+=`:\n"sv, DeprecatedString::SearchDirection::Forward).has_value()) { @@ -91,11 +129,6 @@ ErrorOr serenity_main(Main::Arguments arguments) } } - if (gid < 0) { - warnln("invalid gid {}", gid); - return 3; - } - FILE* pwfile = fopen("/etc/passwd", "a"); if (!pwfile) { perror("failed to open /etc/passwd");