2021-07-09 18:31:23 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Fei Wu <f.eiwu@yahoo.com>
|
|
|
|
* Copyright (c) 2021, Brandon Pruitt <brapru@pm.me>
|
|
|
|
* Copyright (c) 2021, Maxime Friess <M4x1me@pm.me>
|
2022-01-15 20:08:56 +03:00
|
|
|
* Copyright (c) 2022, Umut İnan Erdoğan <umutinanerdogan62@gmail.com>
|
2021-07-09 18:31:23 +03:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-01-15 20:08:56 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2021-07-09 18:31:23 +03:00
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-01-15 20:08:56 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2021-07-09 18:31:23 +03:00
|
|
|
{
|
2022-01-15 20:08:56 +03:00
|
|
|
TRY(Core::System::pledge("stdio wpath rpath cpath fattr proc exec"));
|
|
|
|
TRY(Core::System::unveil("/etc/", "rwc"));
|
|
|
|
TRY(Core::System::unveil("/bin/rm", "x"));
|
2023-06-19 19:50:42 +03:00
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
2021-07-09 18:31:23 +03:00
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString groupname;
|
2021-07-09 18:31:23 +03:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_positional_argument(groupname, "Group name", "group");
|
2022-01-15 20:08:56 +03:00
|
|
|
args_parser.parse(arguments);
|
2021-07-09 18:31:23 +03:00
|
|
|
|
|
|
|
setgrent();
|
2023-02-28 23:41:43 +03:00
|
|
|
auto* g = getgrnam(groupname.characters());
|
2021-07-09 18:31:23 +03:00
|
|
|
|
|
|
|
// Check if the group exists
|
|
|
|
if (!g) {
|
|
|
|
warnln("group {} does not exist", groupname);
|
|
|
|
return 6;
|
|
|
|
}
|
2021-10-10 11:11:03 +03:00
|
|
|
auto gid = g->gr_gid;
|
|
|
|
endgrent();
|
2021-07-09 18:31:23 +03:00
|
|
|
|
|
|
|
// Search if the group is the primary group of an user
|
|
|
|
setpwent();
|
|
|
|
struct passwd* pw;
|
|
|
|
for (pw = getpwent(); pw; pw = getpwent()) {
|
|
|
|
if (pw->pw_gid == gid)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-01-07 15:04:05 +03:00
|
|
|
// If pw is not NULL it means we ended prematurely, aka. the group was found as primary group of an user
|
2021-07-09 18:31:23 +03:00
|
|
|
if (pw) {
|
|
|
|
warnln("cannot remove the primary group of user '{}'", pw->pw_name);
|
|
|
|
endpwent();
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
endpwent();
|
|
|
|
// We can now safely delete the group
|
|
|
|
|
|
|
|
// Create a temporary group file
|
|
|
|
char temp_group[] = "/etc/group.XXXXXX";
|
2022-07-11 22:53:29 +03:00
|
|
|
StringView temp_group_view { temp_group, strlen(temp_group) };
|
2021-07-09 18:31:23 +03:00
|
|
|
|
|
|
|
auto unlink_temp_files = [&] {
|
2022-07-11 22:53:29 +03:00
|
|
|
if (Core::System::unlink(temp_group_view).is_error())
|
2021-07-09 18:31:23 +03:00
|
|
|
perror("unlink");
|
|
|
|
};
|
|
|
|
|
|
|
|
ArmedScopeGuard unlink_temp_files_guard = [&] {
|
|
|
|
unlink_temp_files();
|
|
|
|
};
|
|
|
|
|
2022-01-15 20:08:56 +03:00
|
|
|
int temp_group_fd = TRY(Core::System::mkstemp(temp_group));
|
2021-07-09 18:31:23 +03:00
|
|
|
|
|
|
|
FILE* temp_group_file = fdopen(temp_group_fd, "w");
|
|
|
|
if (!temp_group_file) {
|
|
|
|
perror("fdopen");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
setgrent();
|
|
|
|
for (auto* gr = getgrent(); gr; gr = getgrent()) {
|
|
|
|
if (gr->gr_gid != gid) {
|
|
|
|
if (putgrent(gr, temp_group_file) != 0) {
|
|
|
|
perror("failed to put an entry in the temporary group file");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
endgrent();
|
|
|
|
|
|
|
|
if (fclose(temp_group_file)) {
|
|
|
|
perror("fclose");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-07-11 22:53:29 +03:00
|
|
|
TRY(Core::System::chmod(temp_group_view, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
|
|
|
|
TRY(Core::System::rename(temp_group_view, "/etc/group"sv));
|
2021-07-09 18:31:23 +03:00
|
|
|
|
|
|
|
unlink_temp_files_guard.disarm();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|