usermod: Add -a and -r options to append or remove extra groups

This commit is contained in:
Tim Ledbetter 2023-06-16 20:59:24 +01:00 committed by Andreas Kling
parent caf55c0b2d
commit b17aa47769
Notes: sideshowbarker 2024-07-17 00:47:29 +09:00
2 changed files with 33 additions and 2 deletions

View file

@ -5,7 +5,7 @@ usermod - modify a user account
## Synopsis
```sh
$ usermod [--uid uid] [--gid group] [--groups groups] [--lock] [--unlock] [--home new-home] [--move] [--shell path-to-shell] [--gecos general-info] <username>
$ usermod [--append] [--uid uid] [--gid group] [--groups groups] [--lock] [--remove] [--unlock] [--home new-home] [--move] [--shell path-to-shell] [--gecos general-info] <username>
```
## Description
@ -17,10 +17,12 @@ This program must be run as root.
* `--help`: Display help message and exit
* `--version`: Print version
* `-a`, `--append`: Append the supplementary groups specified with the -G option to the user
* `-u uid`, `--uid uid`: The new numerical value of the user's ID
* `-g group`, `--gid group`: The group name or number of the user's new initial login group
* `-G groups`, `--groups groups`: Set the user's supplementary groups. Groups are specified with a comma-separated list. Group names or numbers may be used
* `-L`, `--lock`: Lock password
* `-r`, `--remove`: Remove the supplementary groups specified with the -G option from the user
* `-U`, `--unlock`: Unlock password
* `-d new-home`, `--home new-home`: The user's new login directory
* `-m`, `--move`: Move the content of the user's home directory to the new location

View file

@ -49,8 +49,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(Core::System::unveil("/etc", "rwc"));
uid_t uid = 0;
bool append_extra_gids = false;
Optional<gid_t> gid;
bool lock = false;
bool remove_extra_gids = false;
bool unlock = false;
StringView new_home_directory;
bool move_home = false;
@ -61,6 +63,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto args_parser = Core::ArgsParser();
args_parser.set_general_help("Modify a user account");
args_parser.add_option(append_extra_gids, "Append the supplementary groups specified with the -G option to the user", "append", 'a');
args_parser.add_option(uid, "The new numerical value of the user's ID", "uid", 'u', "uid");
args_parser.add_option(Core::ArgsParser::Option {
.argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
@ -91,6 +94,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
},
});
args_parser.add_option(lock, "Lock password", "lock", 'L');
args_parser.add_option(remove_extra_gids, "Remove the supplementary groups specified with the -G option from the user", "remove", 'r');
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(move_home, "Move the content of the user's home directory to the new location", "move", 'm');
@ -100,6 +104,18 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
args_parser.parse(arguments);
if (extra_gids.is_empty() && (append_extra_gids || remove_extra_gids)) {
warnln("The -a and -r options can only be used with the -G option");
args_parser.print_usage(stderr, arguments.strings[0]);
return 1;
}
if (append_extra_gids && remove_extra_gids) {
warnln("The -a and -r options are mutually exclusive");
args_parser.print_usage(stderr, arguments.strings[0]);
return 1;
}
if (lock && unlock) {
warnln("The -L and -U options are mutually exclusive");
args_parser.print_usage(stderr, arguments.strings[0]);
@ -178,7 +194,20 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
target_account.set_gecos(gecos);
}
if (!extra_gids.is_empty()) {
if (append_extra_gids) {
for (auto gid : target_account.extra_gids())
extra_gids.append(gid);
}
if (remove_extra_gids) {
Vector<gid_t> current_extra_gids = target_account.extra_gids();
for (auto gid : extra_gids)
current_extra_gids.remove_all_matching([gid](auto current_gid) { return current_gid == gid; });
extra_gids = move(current_extra_gids);
}
if (!extra_gids.is_empty() || remove_extra_gids) {
target_account.set_extra_gids(extra_gids);
}