pwd.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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/Vector.h>
  28. #include <pwd.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <sys/mman.h>
  32. #include <unistd.h>
  33. extern "C" {
  34. #define PWDB_STR_MAX_LEN 256
  35. struct passwd_with_strings : public passwd {
  36. char name_buffer[PWDB_STR_MAX_LEN];
  37. char passwd_buffer[PWDB_STR_MAX_LEN];
  38. char gecos_buffer[PWDB_STR_MAX_LEN];
  39. char dir_buffer[PWDB_STR_MAX_LEN];
  40. char shell_buffer[PWDB_STR_MAX_LEN];
  41. };
  42. static FILE* __pwdb_stream = nullptr;
  43. static unsigned __pwdb_line_number = 0;
  44. static struct passwd_with_strings* __pwdb_entry = nullptr;
  45. void setpwent()
  46. {
  47. __pwdb_line_number = 0;
  48. if (__pwdb_stream) {
  49. rewind(__pwdb_stream);
  50. } else {
  51. __pwdb_stream = fopen("/etc/passwd", "r");
  52. if (!__pwdb_stream) {
  53. perror("open /etc/passwd");
  54. }
  55. assert(__pwdb_stream);
  56. __pwdb_entry = (struct passwd_with_strings*)mmap_with_name(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, "setpwent");
  57. }
  58. }
  59. void endpwent()
  60. {
  61. __pwdb_line_number = 0;
  62. if (__pwdb_stream) {
  63. fclose(__pwdb_stream);
  64. __pwdb_stream = nullptr;
  65. }
  66. if (__pwdb_entry) {
  67. munmap(__pwdb_entry, getpagesize());
  68. __pwdb_entry = nullptr;
  69. }
  70. }
  71. struct passwd* getpwuid(uid_t uid)
  72. {
  73. setpwent();
  74. while (auto* pw = getpwent()) {
  75. if (pw->pw_uid == uid)
  76. return pw;
  77. }
  78. return nullptr;
  79. }
  80. struct passwd* getpwnam(const char* name)
  81. {
  82. setpwent();
  83. while (auto* pw = getpwent()) {
  84. if (!strcmp(pw->pw_name, name))
  85. return pw;
  86. }
  87. return nullptr;
  88. }
  89. struct passwd* getpwent()
  90. {
  91. if (!__pwdb_stream)
  92. setpwent();
  93. assert(__pwdb_stream);
  94. if (feof(__pwdb_stream))
  95. return nullptr;
  96. next_entry:
  97. char buffer[1024];
  98. ++__pwdb_line_number;
  99. char* s = fgets(buffer, sizeof(buffer), __pwdb_stream);
  100. if (!s)
  101. return nullptr;
  102. assert(__pwdb_stream);
  103. if (feof(__pwdb_stream))
  104. return nullptr;
  105. String line(s, Chomp);
  106. auto parts = line.split(':', true);
  107. if (parts.size() != 7) {
  108. fprintf(stderr, "getpwent(): Malformed entry on line %u\n", __pwdb_line_number);
  109. goto next_entry;
  110. }
  111. auto& e_name = parts[0];
  112. auto& e_passwd = parts[1];
  113. auto& e_uid_string = parts[2];
  114. auto& e_gid_string = parts[3];
  115. auto& e_gecos = parts[4];
  116. auto& e_dir = parts[5];
  117. auto& e_shell = parts[6];
  118. bool ok;
  119. uid_t e_uid = e_uid_string.to_uint(ok);
  120. if (!ok) {
  121. fprintf(stderr, "getpwent(): Malformed UID on line %u\n", __pwdb_line_number);
  122. goto next_entry;
  123. }
  124. gid_t e_gid = e_gid_string.to_uint(ok);
  125. if (!ok) {
  126. fprintf(stderr, "getpwent(): Malformed GID on line %u\n", __pwdb_line_number);
  127. goto next_entry;
  128. }
  129. __pwdb_entry->pw_uid = e_uid;
  130. __pwdb_entry->pw_gid = e_gid;
  131. __pwdb_entry->pw_name = __pwdb_entry->name_buffer;
  132. __pwdb_entry->pw_passwd = __pwdb_entry->passwd_buffer;
  133. __pwdb_entry->pw_gecos = __pwdb_entry->gecos_buffer;
  134. __pwdb_entry->pw_dir = __pwdb_entry->dir_buffer;
  135. __pwdb_entry->pw_shell = __pwdb_entry->shell_buffer;
  136. strncpy(__pwdb_entry->name_buffer, e_name.characters(), PWDB_STR_MAX_LEN);
  137. strncpy(__pwdb_entry->passwd_buffer, e_passwd.characters(), PWDB_STR_MAX_LEN);
  138. strncpy(__pwdb_entry->gecos_buffer, e_gecos.characters(), PWDB_STR_MAX_LEN);
  139. strncpy(__pwdb_entry->dir_buffer, e_dir.characters(), PWDB_STR_MAX_LEN);
  140. strncpy(__pwdb_entry->shell_buffer, e_shell.characters(), PWDB_STR_MAX_LEN);
  141. return __pwdb_entry;
  142. }
  143. int putpwent(const struct passwd* p, FILE* stream)
  144. {
  145. if (!p || !stream || !p->pw_name || !p->pw_dir || !p->pw_gecos || !p->pw_shell) {
  146. errno = EINVAL;
  147. return -1;
  148. }
  149. auto is_valid_field = [](const char* str) {
  150. return str && !strpbrk(str, ":\n");
  151. };
  152. 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)) {
  153. errno = EINVAL;
  154. return -1;
  155. }
  156. int nwritten = fprintf(stream, "%s:x:%u:%u:%s,,,:%s:%s\n", p->pw_name, p->pw_uid, p->pw_gid, p->pw_gecos, p->pw_dir, p->pw_shell);
  157. if (!nwritten || nwritten < 0) {
  158. errno = ferror(stream);
  159. return -1;
  160. }
  161. return 0;
  162. }
  163. }