mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 09:48:11 +00:00
usermod: Allow group name or number to be used with the -g
option
This commit is contained in:
parent
99f763cab3
commit
d7fccfc237
2 changed files with 39 additions and 12 deletions
|
@ -5,7 +5,7 @@ usermod - modify a user account
|
||||||
## Synopsis
|
## Synopsis
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ usermod [--uid uid] [--gid gid] [--lock] [--unlock] [--home new-home] [--move] [--shell path-to-shell] [--gecos general-info] <username>
|
$ usermod [--uid uid] [--gid group] [--lock] [--unlock] [--home new-home] [--move] [--shell path-to-shell] [--gecos general-info] <username>
|
||||||
```
|
```
|
||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
@ -18,7 +18,7 @@ This program must be run as root.
|
||||||
* `--help`: Display help message and exit
|
* `--help`: Display help message and exit
|
||||||
* `--version`: Print version
|
* `--version`: Print version
|
||||||
* `-u uid`, `--uid uid`: The new numerical value of the user's ID
|
* `-u uid`, `--uid uid`: The new numerical value of the user's ID
|
||||||
* `-g gid`, `--gid gid`: The group number of the user's new initial login group
|
* `-g group`, `--gid group`: The group name or number of the user's new initial login group
|
||||||
* `-L`, `--lock`: Lock password
|
* `-L`, `--lock`: Lock password
|
||||||
* `-U`, `--unlock`: Unlock password
|
* `-U`, `--unlock`: Unlock password
|
||||||
* `-d new-home`, `--home new-home`: The user's new login directory
|
* `-d new-home`, `--home new-home`: The user's new login directory
|
||||||
|
|
|
@ -15,6 +15,27 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
static Optional<gid_t> group_string_to_gid(StringView group)
|
||||||
|
{
|
||||||
|
auto maybe_gid = group.to_uint<gid_t>();
|
||||||
|
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<int> serenity_main(Main::Arguments arguments)
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
{
|
{
|
||||||
if (geteuid() != 0) {
|
if (geteuid() != 0) {
|
||||||
|
@ -28,7 +49,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
TRY(Core::System::unveil("/etc", "rwc"));
|
TRY(Core::System::unveil("/etc", "rwc"));
|
||||||
|
|
||||||
uid_t uid = 0;
|
uid_t uid = 0;
|
||||||
int gid = 0;
|
Optional<gid_t> gid;
|
||||||
bool lock = false;
|
bool lock = false;
|
||||||
bool unlock = false;
|
bool unlock = false;
|
||||||
StringView new_home_directory;
|
StringView new_home_directory;
|
||||||
|
@ -40,7 +61,19 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
auto args_parser = Core::ArgsParser();
|
auto args_parser = Core::ArgsParser();
|
||||||
args_parser.set_general_help("Modify a user account");
|
args_parser.set_general_help("Modify a user account");
|
||||||
args_parser.add_option(uid, "The new numerical value of the user's ID", "uid", 'u', "uid");
|
args_parser.add_option(uid, "The new numerical value of the user's ID", "uid", 'u', "uid");
|
||||||
args_parser.add_option(gid, "The group number of the user's new initial login group", "gid", 'g', "gid");
|
args_parser.add_option(Core::ArgsParser::Option {
|
||||||
|
.argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
|
||||||
|
.help_string = "The group name or number of the user's new initial login group",
|
||||||
|
.long_name = "gid",
|
||||||
|
.short_name = 'g',
|
||||||
|
.value_name = "group",
|
||||||
|
.accept_value = [&gid](StringView group) {
|
||||||
|
if (auto maybe_gid = group_string_to_gid(group); maybe_gid.has_value())
|
||||||
|
gid = move(maybe_gid);
|
||||||
|
|
||||||
|
return gid.has_value();
|
||||||
|
},
|
||||||
|
});
|
||||||
args_parser.add_option(lock, "Lock password", "lock", 'L');
|
args_parser.add_option(lock, "Lock password", "lock", 'L');
|
||||||
args_parser.add_option(unlock, "Unlock password", "unlock", 'U');
|
args_parser.add_option(unlock, "Unlock password", "unlock", 'U');
|
||||||
args_parser.add_option(new_home_directory, "The user's new login directory", "home", 'd', "new-home");
|
args_parser.add_option(new_home_directory, "The user's new login directory", "home", 'd', "new-home");
|
||||||
|
@ -83,14 +116,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
target_account.set_uid(uid);
|
target_account.set_uid(uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (gid) {
|
if (gid.has_value())
|
||||||
if (gid < 0) {
|
target_account.set_gid(gid.value());
|
||||||
warnln("invalid gid {}", gid);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
target_account.set_gid(gid);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (lock) {
|
if (lock) {
|
||||||
target_account.set_password_enabled(false);
|
target_account.set_password_enabled(false);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue