userdel.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2020, Fei Wu <f.eiwu@yahoo.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/String.h>
  27. #include <AK/StringBuilder.h>
  28. #include <LibCore/ArgsParser.h>
  29. #include <ctype.h>
  30. #include <dirent.h>
  31. #include <errno.h>
  32. #include <pwd.h>
  33. #include <spawn.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <sys/stat.h>
  38. #include <sys/types.h>
  39. #include <sys/wait.h>
  40. #include <unistd.h>
  41. int main(int argc, char** argv)
  42. {
  43. const char* username = nullptr;
  44. bool remove_home = false;
  45. Core::ArgsParser args_parser;
  46. args_parser.add_option(remove_home, "Remove home directory", "remove", 'r');
  47. args_parser.add_positional_argument(username, "Login user identity (username)", "login");
  48. args_parser.parse(argc, argv);
  49. char temp_filename[] = "/etc/passwd.XXXXXX";
  50. auto fd = mkstemp(temp_filename);
  51. if (fd == -1) {
  52. perror("failed to create temporary file");
  53. return 1;
  54. }
  55. FILE* temp_file = fdopen(fd, "w");
  56. if (!temp_file) {
  57. perror("fdopen");
  58. if (unlink(temp_filename) < 0) {
  59. perror("unlink");
  60. }
  61. return 1;
  62. }
  63. bool user_exists = false;
  64. String home_directory;
  65. int rc = 0;
  66. setpwent();
  67. for (auto* pw = getpwent(); pw; pw = getpwent()) {
  68. if (strcmp(pw->pw_name, username)) {
  69. if (putpwent(pw, temp_file) != 0) {
  70. perror("failed to put an entry in the temporary passwd file");
  71. rc = 1;
  72. break;
  73. }
  74. } else {
  75. user_exists = true;
  76. if (remove_home)
  77. home_directory = pw->pw_dir;
  78. }
  79. }
  80. endpwent();
  81. if (fclose(temp_file)) {
  82. perror("fclose");
  83. if (!rc)
  84. rc = 1;
  85. }
  86. if (rc == 0 && !user_exists) {
  87. fprintf(stderr, "specified user doesn't exist\n");
  88. rc = 6;
  89. }
  90. if (rc == 0 && chmod(temp_filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) {
  91. perror("chmod");
  92. rc = 1;
  93. }
  94. if (rc == 0 && rename(temp_filename, "/etc/passwd") < 0) {
  95. perror("failed to rename the temporary passwd file");
  96. rc = 1;
  97. }
  98. if (rc) {
  99. if (unlink(temp_filename) < 0) {
  100. perror("unlink");
  101. }
  102. return rc;
  103. }
  104. if (remove_home) {
  105. if (home_directory == "/") {
  106. fprintf(stderr, "home directory is /, not deleted!\n");
  107. return 12;
  108. }
  109. if (access(home_directory.characters(), F_OK) != -1) {
  110. pid_t child;
  111. const char* argv[] = { "rm", "-r", home_directory.characters(), nullptr };
  112. if ((errno = posix_spawn(&child, "/bin/rm", nullptr, nullptr, const_cast<char**>(argv), environ))) {
  113. perror("posix_spawn");
  114. return 12;
  115. }
  116. int wstatus;
  117. if (waitpid(child, &wstatus, 0) < 0) {
  118. perror("waitpid");
  119. return 12;
  120. }
  121. if (WEXITSTATUS(wstatus)) {
  122. fprintf(stderr, "failed to remove the home directory\n");
  123. return 12;
  124. }
  125. }
  126. }
  127. return 0;
  128. }