groupdel.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2020, Fei Wu <f.eiwu@yahoo.com>
  3. * Copyright (c) 2021, Brandon Pruitt <brapru@pm.me>
  4. * Copyright (c) 2021, Maxime Friess <M4x1me@pm.me>
  5. * Copyright (c) 2022, Umut İnan Erdoğan <umutinanerdogan62@gmail.com>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <LibCore/ArgsParser.h>
  10. #include <LibCore/File.h>
  11. #include <LibCore/System.h>
  12. #include <LibMain/Main.h>
  13. #include <grp.h>
  14. #include <pwd.h>
  15. #include <unistd.h>
  16. ErrorOr<int> serenity_main(Main::Arguments arguments)
  17. {
  18. TRY(Core::System::pledge("stdio wpath rpath cpath fattr proc exec"));
  19. TRY(Core::System::unveil("/etc/", "rwc"));
  20. TRY(Core::System::unveil("/bin/rm", "x"));
  21. char const* groupname = nullptr;
  22. Core::ArgsParser args_parser;
  23. args_parser.add_positional_argument(groupname, "Group name", "group");
  24. args_parser.parse(arguments);
  25. setgrent();
  26. auto* g = getgrnam(groupname);
  27. // Check if the group exists
  28. if (!g) {
  29. warnln("group {} does not exist", groupname);
  30. return 6;
  31. }
  32. auto gid = g->gr_gid;
  33. endgrent();
  34. // Search if the group is the primary group of an user
  35. setpwent();
  36. struct passwd* pw;
  37. for (pw = getpwent(); pw; pw = getpwent()) {
  38. if (pw->pw_gid == gid)
  39. break;
  40. }
  41. // If pw is not NULL it means we ended prematurely, aka. the group was found as primary group of an user
  42. if (pw) {
  43. warnln("cannot remove the primary group of user '{}'", pw->pw_name);
  44. endpwent();
  45. return 8;
  46. }
  47. endpwent();
  48. // We can now safely delete the group
  49. // Create a temporary group file
  50. char temp_group[] = "/etc/group.XXXXXX";
  51. StringView temp_group_view { temp_group, strlen(temp_group) };
  52. auto unlink_temp_files = [&] {
  53. if (Core::System::unlink(temp_group_view).is_error())
  54. perror("unlink");
  55. };
  56. ArmedScopeGuard unlink_temp_files_guard = [&] {
  57. unlink_temp_files();
  58. };
  59. int temp_group_fd = TRY(Core::System::mkstemp(temp_group));
  60. FILE* temp_group_file = fdopen(temp_group_fd, "w");
  61. if (!temp_group_file) {
  62. perror("fdopen");
  63. return 1;
  64. }
  65. setgrent();
  66. for (auto* gr = getgrent(); gr; gr = getgrent()) {
  67. if (gr->gr_gid != gid) {
  68. if (putgrent(gr, temp_group_file) != 0) {
  69. perror("failed to put an entry in the temporary group file");
  70. return 1;
  71. }
  72. }
  73. }
  74. endgrent();
  75. if (fclose(temp_group_file)) {
  76. perror("fclose");
  77. return 1;
  78. }
  79. TRY(Core::System::chmod(temp_group_view, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
  80. TRY(Core::System::rename(temp_group_view, "/etc/group"sv));
  81. unlink_temp_files_guard.disarm();
  82. return 0;
  83. }