pwd.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/String.h>
  7. #include <AK/TemporaryChange.h>
  8. #include <AK/Vector.h>
  9. #include <errno.h>
  10. #include <pwd.h>
  11. #include <stdio.h>
  12. #include <stdlib.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. static struct passwd s_passwd_entry;
  19. static String s_name;
  20. static String s_passwd;
  21. static String s_gecos;
  22. static String s_dir;
  23. static String s_shell;
  24. void setpwent()
  25. {
  26. s_line_number = 0;
  27. if (s_stream) {
  28. rewind(s_stream);
  29. } else {
  30. s_stream = fopen("/etc/passwd", "r");
  31. if (!s_stream) {
  32. perror("open /etc/passwd");
  33. }
  34. }
  35. }
  36. void endpwent()
  37. {
  38. s_line_number = 0;
  39. if (s_stream) {
  40. fclose(s_stream);
  41. s_stream = nullptr;
  42. }
  43. }
  44. struct passwd* getpwuid(uid_t uid)
  45. {
  46. setpwent();
  47. while (auto* pw = getpwent()) {
  48. if (pw->pw_uid == uid)
  49. return pw;
  50. }
  51. return nullptr;
  52. }
  53. struct passwd* getpwnam(char const* name)
  54. {
  55. setpwent();
  56. while (auto* pw = getpwent()) {
  57. if (!strcmp(pw->pw_name, name))
  58. return pw;
  59. }
  60. return nullptr;
  61. }
  62. static bool parse_pwddb_entry(String const& line)
  63. {
  64. auto parts = line.split_view(':', true);
  65. if (parts.size() != 7) {
  66. dbgln("getpwent(): Malformed entry on line {}", s_line_number);
  67. return false;
  68. }
  69. s_name = parts[0];
  70. s_passwd = parts[1];
  71. auto& uid_string = parts[2];
  72. auto& gid_string = parts[3];
  73. s_gecos = parts[4];
  74. s_dir = parts[5];
  75. s_shell = parts[6];
  76. auto uid = uid_string.to_uint();
  77. if (!uid.has_value()) {
  78. dbgln("getpwent(): Malformed UID on line {}", s_line_number);
  79. return false;
  80. }
  81. auto gid = gid_string.to_uint();
  82. if (!gid.has_value()) {
  83. dbgln("getpwent(): Malformed GID on line {}", s_line_number);
  84. return false;
  85. }
  86. s_passwd_entry.pw_name = const_cast<char*>(s_name.characters());
  87. s_passwd_entry.pw_passwd = const_cast<char*>(s_passwd.characters());
  88. s_passwd_entry.pw_uid = uid.value();
  89. s_passwd_entry.pw_gid = gid.value();
  90. s_passwd_entry.pw_gecos = const_cast<char*>(s_gecos.characters());
  91. s_passwd_entry.pw_dir = const_cast<char*>(s_dir.characters());
  92. s_passwd_entry.pw_shell = const_cast<char*>(s_shell.characters());
  93. return true;
  94. }
  95. struct passwd* getpwent()
  96. {
  97. if (!s_stream)
  98. setpwent();
  99. while (true) {
  100. if (!s_stream || feof(s_stream))
  101. return nullptr;
  102. if (ferror(s_stream)) {
  103. dbgln("getpwent(): Read error: {}", strerror(ferror(s_stream)));
  104. return nullptr;
  105. }
  106. char buffer[1024];
  107. ++s_line_number;
  108. char* s = fgets(buffer, sizeof(buffer), s_stream);
  109. // Silently tolerate an empty line at the end.
  110. if ((!s || !s[0]) && feof(s_stream))
  111. return nullptr;
  112. String line(s, Chomp);
  113. if (parse_pwddb_entry(line))
  114. return &s_passwd_entry;
  115. // Otherwise, proceed to the next line.
  116. }
  117. }
  118. static void construct_pwd(struct passwd* pwd, char* buf, struct passwd** result)
  119. {
  120. auto* buf_name = &buf[0];
  121. auto* buf_passwd = &buf[s_name.length() + 1];
  122. auto* buf_gecos = &buf[s_name.length() + 1 + s_gecos.length() + 1];
  123. auto* buf_dir = &buf[s_gecos.length() + 1 + s_name.length() + 1 + s_gecos.length() + 1];
  124. auto* buf_shell = &buf[s_dir.length() + 1 + s_gecos.length() + 1 + s_name.length() + 1 + s_gecos.length() + 1];
  125. bool ok = true;
  126. ok = ok && s_name.copy_characters_to_buffer(buf_name, s_name.length() + 1);
  127. ok = ok && s_passwd.copy_characters_to_buffer(buf_passwd, s_passwd.length() + 1);
  128. ok = ok && s_gecos.copy_characters_to_buffer(buf_gecos, s_gecos.length() + 1);
  129. ok = ok && s_dir.copy_characters_to_buffer(buf_dir, s_dir.length() + 1);
  130. ok = ok && s_shell.copy_characters_to_buffer(buf_shell, s_shell.length() + 1);
  131. VERIFY(ok);
  132. *result = pwd;
  133. pwd->pw_name = buf_name;
  134. pwd->pw_passwd = buf_passwd;
  135. pwd->pw_gecos = buf_gecos;
  136. pwd->pw_dir = buf_dir;
  137. pwd->pw_shell = buf_shell;
  138. }
  139. int getpwnam_r(char const* name, struct passwd* pwd, char* buf, size_t buflen, struct passwd** result)
  140. {
  141. // FIXME: This is a HACK!
  142. TemporaryChange name_change { s_name, {} };
  143. TemporaryChange passwd_change { s_passwd, {} };
  144. TemporaryChange gecos_change { s_gecos, {} };
  145. TemporaryChange dir_change { s_dir, {} };
  146. TemporaryChange shell_change { s_shell, {} };
  147. setpwent();
  148. bool found = false;
  149. while (auto* pw = getpwent()) {
  150. if (!strcmp(pw->pw_name, name)) {
  151. found = true;
  152. break;
  153. }
  154. }
  155. if (!found) {
  156. *result = nullptr;
  157. return 0;
  158. }
  159. auto const total_buffer_length = s_name.length() + s_passwd.length() + s_gecos.length() + s_dir.length() + s_shell.length() + 5;
  160. if (buflen < total_buffer_length)
  161. return ERANGE;
  162. construct_pwd(pwd, buf, result);
  163. return 0;
  164. }
  165. int getpwuid_r(uid_t uid, struct passwd* pwd, char* buf, size_t buflen, struct passwd** result)
  166. {
  167. // FIXME: This is a HACK!
  168. TemporaryChange name_change { s_name, {} };
  169. TemporaryChange passwd_change { s_passwd, {} };
  170. TemporaryChange gecos_change { s_gecos, {} };
  171. TemporaryChange dir_change { s_dir, {} };
  172. TemporaryChange shell_change { s_shell, {} };
  173. setpwent();
  174. bool found = false;
  175. while (auto* pw = getpwent()) {
  176. if (pw->pw_uid == uid) {
  177. found = true;
  178. break;
  179. }
  180. }
  181. if (!found) {
  182. *result = nullptr;
  183. return 0;
  184. }
  185. auto const total_buffer_length = s_name.length() + s_passwd.length() + s_gecos.length() + s_dir.length() + s_shell.length() + 5;
  186. if (buflen < total_buffer_length)
  187. return ERANGE;
  188. construct_pwd(pwd, buf, result);
  189. return 0;
  190. }
  191. int putpwent(const struct passwd* p, FILE* stream)
  192. {
  193. if (!p || !stream || !p->pw_passwd || !p->pw_name || !p->pw_dir || !p->pw_gecos || !p->pw_shell) {
  194. errno = EINVAL;
  195. return -1;
  196. }
  197. auto is_valid_field = [](char const* str) {
  198. return str && !strpbrk(str, ":\n");
  199. };
  200. 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)) {
  201. errno = EINVAL;
  202. return -1;
  203. }
  204. 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);
  205. if (!nwritten || nwritten < 0) {
  206. errno = ferror(stream);
  207. return -1;
  208. }
  209. return 0;
  210. }
  211. }