mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:08:10 +00:00
usermod: Add -a
and -r
options to append or remove extra groups
This commit is contained in:
parent
caf55c0b2d
commit
b17aa47769
2 changed files with 33 additions and 2 deletions
|
@ -5,7 +5,7 @@ usermod - modify a user account
|
||||||
## Synopsis
|
## Synopsis
|
||||||
|
|
||||||
```sh
|
```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
|
## Description
|
||||||
|
@ -17,10 +17,12 @@ 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
|
||||||
|
* `-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
|
* `-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 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
|
* `-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
|
* `-L`, `--lock`: Lock password
|
||||||
|
* `-r`, `--remove`: Remove the supplementary groups specified with the -G option from the user
|
||||||
* `-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
|
||||||
* `-m`, `--move`: Move the content of the user's home directory to the new location
|
* `-m`, `--move`: Move the content of the user's home directory to the new location
|
||||||
|
|
|
@ -49,8 +49,10 @@ 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;
|
||||||
|
bool append_extra_gids = false;
|
||||||
Optional<gid_t> gid;
|
Optional<gid_t> gid;
|
||||||
bool lock = false;
|
bool lock = false;
|
||||||
|
bool remove_extra_gids = false;
|
||||||
bool unlock = false;
|
bool unlock = false;
|
||||||
StringView new_home_directory;
|
StringView new_home_directory;
|
||||||
bool move_home = false;
|
bool move_home = false;
|
||||||
|
@ -61,6 +63,7 @@ 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(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(uid, "The new numerical value of the user's ID", "uid", 'u', "uid");
|
||||||
args_parser.add_option(Core::ArgsParser::Option {
|
args_parser.add_option(Core::ArgsParser::Option {
|
||||||
.argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
|
.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(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(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");
|
||||||
args_parser.add_option(move_home, "Move the content of the user's home directory to the new location", "move", 'm');
|
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);
|
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) {
|
if (lock && unlock) {
|
||||||
warnln("The -L and -U options are mutually exclusive");
|
warnln("The -L and -U options are mutually exclusive");
|
||||||
args_parser.print_usage(stderr, arguments.strings[0]);
|
args_parser.print_usage(stderr, arguments.strings[0]);
|
||||||
|
@ -178,7 +194,20 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
target_account.set_gecos(gecos);
|
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);
|
target_account.set_extra_gids(extra_gids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue