pwd.cpp 6.7 KB

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