userdel.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2020, Fei Wu <f.eiwu@yahoo.com>
  3. * Copyright (c) 2021, Brandon Pruitt <brapru@pm.me>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/ScopeGuard.h>
  8. #include <AK/String.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibCore/ArgsParser.h>
  11. #include <LibCore/File.h>
  12. #include <ctype.h>
  13. #include <dirent.h>
  14. #include <errno.h>
  15. #include <pwd.h>
  16. #include <shadow.h>
  17. #include <spawn.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sys/stat.h>
  22. #include <sys/types.h>
  23. #include <sys/wait.h>
  24. #include <unistd.h>
  25. int main(int argc, char** argv)
  26. {
  27. if (pledge("stdio wpath rpath cpath fattr proc exec", nullptr) < 0) {
  28. perror("pledge");
  29. return 1;
  30. }
  31. if (unveil("/etc/", "rwc") < 0) {
  32. perror("unveil");
  33. return 1;
  34. }
  35. if (unveil("/bin/rm", "x") < 0) {
  36. perror("unveil");
  37. return 1;
  38. }
  39. unveil(nullptr, nullptr);
  40. const char* username = nullptr;
  41. bool remove_home = false;
  42. Core::ArgsParser args_parser;
  43. args_parser.add_option(remove_home, "Remove home directory", "remove", 'r');
  44. args_parser.add_positional_argument(username, "Login user identity (username)", "login");
  45. args_parser.parse(argc, argv);
  46. if (!remove_home) {
  47. if (pledge("stdio wpath rpath cpath fattr", nullptr) < 0) {
  48. perror("pledge");
  49. return 1;
  50. }
  51. }
  52. char temp_passwd[] = "/etc/passwd.XXXXXX";
  53. char temp_shadow[] = "/etc/shadow.XXXXXX";
  54. auto unlink_temp_files = [&] {
  55. if (unlink(temp_passwd) < 0)
  56. perror("unlink");
  57. if (unlink(temp_shadow) < 0)
  58. perror("unlink");
  59. };
  60. ArmedScopeGuard unlink_temp_files_guard = [&] {
  61. unlink_temp_files();
  62. };
  63. auto temp_passwd_fd = mkstemp(temp_passwd);
  64. if (temp_passwd_fd == -1) {
  65. perror("failed to create temporary passwd file");
  66. return 1;
  67. }
  68. auto temp_shadow_fd = mkstemp(temp_shadow);
  69. if (temp_shadow_fd == -1) {
  70. perror("failed to create temporary shadow file");
  71. return 1;
  72. }
  73. FILE* temp_passwd_file = fdopen(temp_passwd_fd, "w");
  74. if (!temp_passwd_file) {
  75. perror("fdopen");
  76. return 1;
  77. }
  78. FILE* temp_shadow_file = fdopen(temp_shadow_fd, "w");
  79. if (!temp_shadow_file) {
  80. perror("fdopen");
  81. return 1;
  82. }
  83. bool user_exists = false;
  84. String home_directory;
  85. setpwent();
  86. for (auto* pw = getpwent(); pw; pw = getpwent()) {
  87. if (strcmp(pw->pw_name, username)) {
  88. if (putpwent(pw, temp_passwd_file) != 0) {
  89. perror("failed to put an entry in the temporary passwd file");
  90. return 1;
  91. }
  92. } else {
  93. user_exists = true;
  94. if (remove_home)
  95. home_directory = pw->pw_dir;
  96. }
  97. }
  98. endpwent();
  99. setspent();
  100. for (auto* spwd = getspent(); spwd; spwd = getspent()) {
  101. if (strcmp(spwd->sp_namp, username)) {
  102. if (putspent(spwd, temp_shadow_file) != 0) {
  103. perror("failed to put an entry in the temporary shadow file");
  104. return 1;
  105. }
  106. }
  107. }
  108. endspent();
  109. if (fclose(temp_passwd_file)) {
  110. perror("fclose");
  111. return 1;
  112. }
  113. if (fclose(temp_shadow_file)) {
  114. perror("fclose");
  115. return 1;
  116. }
  117. if (!user_exists) {
  118. warnln("specified user doesn't exist");
  119. return 1;
  120. }
  121. if (chmod(temp_passwd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) {
  122. perror("chmod");
  123. return 1;
  124. }
  125. if (chmod(temp_shadow, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) {
  126. perror("chmod");
  127. return 1;
  128. }
  129. if (rename(temp_passwd, "/etc/passwd") < 0) {
  130. perror("failed to rename the temporary passwd file");
  131. return 1;
  132. }
  133. if (rename(temp_shadow, "/etc/shadow") < 0) {
  134. perror("failed to rename the temporary shadow file");
  135. return 1;
  136. }
  137. unlink_temp_files_guard.disarm();
  138. if (remove_home) {
  139. if (access(home_directory.characters(), F_OK) == -1)
  140. return 0;
  141. String real_path = Core::File::real_path_for(home_directory);
  142. if (real_path == "/") {
  143. warnln("home directory is /, not deleted!");
  144. return 12;
  145. }
  146. pid_t child;
  147. const char* argv[] = { "rm", "-r", home_directory.characters(), nullptr };
  148. if ((errno = posix_spawn(&child, "/bin/rm", nullptr, nullptr, const_cast<char**>(argv), environ))) {
  149. perror("posix_spawn");
  150. return 12;
  151. }
  152. int wstatus;
  153. if (waitpid(child, &wstatus, 0) < 0) {
  154. perror("waitpid");
  155. return 12;
  156. }
  157. if (WEXITSTATUS(wstatus)) {
  158. warnln("failed to remove the home directory");
  159. return 12;
  160. }
  161. }
  162. return 0;
  163. }