pwd.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Format.h>
  7. #include <AK/ScopeGuard.h>
  8. #include <AK/TemporaryChange.h>
  9. #include <AK/Vector.h>
  10. #include <errno.h>
  11. #include <pwd.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. extern "C" {
  16. static FILE* s_stream = nullptr;
  17. static unsigned s_line_number = 0;
  18. void setpwent()
  19. {
  20. s_line_number = 0;
  21. if (s_stream) {
  22. rewind(s_stream);
  23. } else {
  24. s_stream = fopen("/etc/passwd", "r");
  25. if (!s_stream) {
  26. perror("open /etc/passwd");
  27. }
  28. }
  29. }
  30. void endpwent()
  31. {
  32. s_line_number = 0;
  33. if (s_stream) {
  34. fclose(s_stream);
  35. s_stream = nullptr;
  36. }
  37. }
  38. struct passwd* getpwuid(uid_t uid)
  39. {
  40. setpwent();
  41. ScopeGuard guard = [] { endpwent(); };
  42. while (auto* pw = getpwent()) {
  43. if (pw->pw_uid == uid)
  44. return pw;
  45. }
  46. return nullptr;
  47. }
  48. struct passwd* getpwnam(char const* name)
  49. {
  50. setpwent();
  51. ScopeGuard guard = [] { endpwent(); };
  52. while (auto* pw = getpwent()) {
  53. if (!strcmp(pw->pw_name, name))
  54. return pw;
  55. }
  56. return nullptr;
  57. }
  58. static bool parse_pwddb_entry(char* raw_line, struct passwd& passwd_entry)
  59. {
  60. size_t line_length = strlen(raw_line);
  61. for (size_t i = 0; i < line_length; ++i) {
  62. auto& ch = raw_line[i];
  63. if (ch == '\r' || ch == '\n')
  64. line_length = i;
  65. if (ch == ':' || ch == '\r' || ch == '\n')
  66. ch = '\0';
  67. }
  68. auto line = StringView { raw_line, line_length };
  69. auto parts = line.split_view('\0', SplitBehavior::KeepEmpty);
  70. if (parts.size() != 7) {
  71. dbgln("getpwent(): Malformed entry on line {}", s_line_number);
  72. return false;
  73. }
  74. auto& name = parts[0];
  75. auto& passwd = parts[1];
  76. auto& uid_string = parts[2];
  77. auto& gid_string = parts[3];
  78. auto& gecos = parts[4];
  79. auto& dir = parts[5];
  80. auto& shell = parts[6];
  81. auto uid = uid_string.to_uint();
  82. if (!uid.has_value()) {
  83. dbgln("getpwent(): Malformed UID on line {}", s_line_number);
  84. return false;
  85. }
  86. auto gid = gid_string.to_uint();
  87. if (!gid.has_value()) {
  88. dbgln("getpwent(): Malformed GID on line {}", s_line_number);
  89. return false;
  90. }
  91. passwd_entry.pw_name = const_cast<char*>(name.characters_without_null_termination());
  92. passwd_entry.pw_passwd = const_cast<char*>(passwd.characters_without_null_termination());
  93. passwd_entry.pw_uid = uid.value();
  94. passwd_entry.pw_gid = gid.value();
  95. passwd_entry.pw_gecos = const_cast<char*>(gecos.characters_without_null_termination());
  96. passwd_entry.pw_dir = const_cast<char*>(dir.characters_without_null_termination());
  97. passwd_entry.pw_shell = const_cast<char*>(shell.characters_without_null_termination());
  98. return true;
  99. }
  100. struct passwd* getpwent()
  101. {
  102. static struct passwd passwd_entry;
  103. static char buffer[1024];
  104. struct passwd* result;
  105. if (getpwent_r(&passwd_entry, buffer, sizeof(buffer), &result) < 0)
  106. return nullptr;
  107. return result;
  108. }
  109. int getpwent_r(struct passwd* passwd_buf, char* buffer, size_t buffer_size, struct passwd** passwd_entry_ptr)
  110. {
  111. if (!s_stream)
  112. setpwent();
  113. while (true) {
  114. if (!s_stream || feof(s_stream)) {
  115. *passwd_entry_ptr = nullptr;
  116. return ENOENT;
  117. }
  118. if (ferror(s_stream)) {
  119. *passwd_entry_ptr = nullptr;
  120. return ferror(s_stream);
  121. }
  122. ++s_line_number;
  123. char* s = fgets(buffer, buffer_size, s_stream);
  124. if ((!s || !s[0]) && feof(s_stream)) {
  125. *passwd_entry_ptr = nullptr;
  126. return ENOENT;
  127. }
  128. if (strlen(s) == buffer_size - 1) {
  129. *passwd_entry_ptr = nullptr;
  130. return ERANGE;
  131. }
  132. if (parse_pwddb_entry(buffer, *passwd_buf)) {
  133. *passwd_entry_ptr = passwd_buf;
  134. return 0;
  135. }
  136. }
  137. }
  138. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpwnam.html
  139. int getpwnam_r(char const* name, struct passwd* pwd, char* buffer, size_t bufsize, struct passwd** result)
  140. {
  141. setpwent();
  142. for (;;) {
  143. if (auto rc = getpwent_r(pwd, buffer, bufsize, result); rc != 0)
  144. return rc;
  145. if (strcmp(pwd->pw_name, name) == 0)
  146. return 0;
  147. }
  148. }
  149. // https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpwuid.html
  150. int getpwuid_r(uid_t uid, struct passwd* pwd, char* buffer, size_t bufsize, struct passwd** result)
  151. {
  152. setpwent();
  153. for (;;) {
  154. if (auto rc = getpwent_r(pwd, buffer, bufsize, result); rc != 0)
  155. return rc;
  156. if (pwd->pw_uid == uid)
  157. return 0;
  158. }
  159. }
  160. int putpwent(const struct passwd* p, FILE* stream)
  161. {
  162. if (!p || !stream || !p->pw_passwd || !p->pw_name || !p->pw_dir || !p->pw_gecos || !p->pw_shell) {
  163. errno = EINVAL;
  164. return -1;
  165. }
  166. auto is_valid_field = [](char const* str) {
  167. return str && !strpbrk(str, ":\n");
  168. };
  169. if (!is_valid_field(p->pw_name) || !is_valid_field(p->pw_dir) || !is_valid_field(p->pw_gecos) || !is_valid_field(p->pw_shell)) {
  170. errno = EINVAL;
  171. return -1;
  172. }
  173. int nwritten = fprintf(stream, "%s:%s:%u:%u:%s,,,:%s:%s\n", p->pw_name, p->pw_passwd, p->pw_uid, p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell);
  174. if (!nwritten || nwritten < 0) {
  175. errno = ferror(stream);
  176. return -1;
  177. }
  178. return 0;
  179. }
  180. }