grp.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <grp.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <sys/mman.h>
  32. #include <unistd.h>
  33. extern "C" {
  34. #define GRDB_STR_MAX_LEN 256
  35. struct group_with_strings : public group {
  36. char name_buffer[GRDB_STR_MAX_LEN];
  37. char passwd_buffer[GRDB_STR_MAX_LEN];
  38. char* members[32];
  39. char members_buffer[32][32];
  40. };
  41. static FILE* __grdb_stream = nullptr;
  42. static unsigned __grdb_line_number = 0;
  43. static struct group_with_strings* __grdb_entry = nullptr;
  44. void setgrent()
  45. {
  46. __grdb_line_number = 0;
  47. if (__grdb_stream) {
  48. rewind(__grdb_stream);
  49. } else {
  50. __grdb_stream = fopen("/etc/group", "r");
  51. if (!__grdb_stream) {
  52. perror("open /etc/group");
  53. }
  54. assert(__grdb_stream);
  55. __grdb_entry = (struct group_with_strings*)mmap_with_name(nullptr, getpagesize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0, "setgrent");
  56. }
  57. }
  58. void endgrent()
  59. {
  60. __grdb_line_number = 0;
  61. if (__grdb_stream) {
  62. fclose(__grdb_stream);
  63. __grdb_stream = nullptr;
  64. }
  65. if (__grdb_entry) {
  66. munmap(__grdb_entry, getpagesize());
  67. __grdb_entry = nullptr;
  68. }
  69. }
  70. struct group* getgrgid(gid_t gid)
  71. {
  72. setgrent();
  73. while (auto* gr = getgrent()) {
  74. if (gr->gr_gid == gid)
  75. return gr;
  76. }
  77. return nullptr;
  78. }
  79. struct group* getgrnam(const char* name)
  80. {
  81. setgrent();
  82. while (auto* gr = getgrent()) {
  83. if (!strcmp(gr->gr_name, name))
  84. return gr;
  85. }
  86. return nullptr;
  87. }
  88. struct group* getgrent()
  89. {
  90. if (!__grdb_stream)
  91. setgrent();
  92. assert(__grdb_stream);
  93. if (feof(__grdb_stream))
  94. return nullptr;
  95. next_entry:
  96. char buffer[1024];
  97. ++__grdb_line_number;
  98. char* s = fgets(buffer, sizeof(buffer), __grdb_stream);
  99. if (!s)
  100. return nullptr;
  101. assert(__grdb_stream);
  102. if (feof(__grdb_stream))
  103. return nullptr;
  104. String line(s, Chomp);
  105. auto parts = line.split(':', true);
  106. if (parts.size() != 4) {
  107. fprintf(stderr, "getgrent(): Malformed entry on line %u: '%s' has %zu parts\n", __grdb_line_number, line.characters(), parts.size());
  108. goto next_entry;
  109. }
  110. auto& e_name = parts[0];
  111. auto& e_passwd = parts[1];
  112. auto& e_gid_string = parts[2];
  113. auto& e_members_string = parts[3];
  114. bool ok;
  115. gid_t e_gid = e_gid_string.to_uint(ok);
  116. if (!ok) {
  117. fprintf(stderr, "getgrent(): Malformed GID on line %u\n", __grdb_line_number);
  118. goto next_entry;
  119. }
  120. auto members = e_members_string.split(',');
  121. __grdb_entry->gr_gid = e_gid;
  122. __grdb_entry->gr_name = __grdb_entry->name_buffer;
  123. __grdb_entry->gr_passwd = __grdb_entry->passwd_buffer;
  124. for (size_t i = 0; i < members.size(); ++i) {
  125. __grdb_entry->members[i] = __grdb_entry->members_buffer[i];
  126. strcpy(__grdb_entry->members_buffer[i], members[i].characters());
  127. }
  128. __grdb_entry->members[members.size()] = nullptr;
  129. __grdb_entry->gr_mem = __grdb_entry->members;
  130. strncpy(__grdb_entry->name_buffer, e_name.characters(), GRDB_STR_MAX_LEN);
  131. strncpy(__grdb_entry->passwd_buffer, e_passwd.characters(), GRDB_STR_MAX_LEN);
  132. return __grdb_entry;
  133. }
  134. int initgroups(const char* user, gid_t extra_gid)
  135. {
  136. size_t count = 0;
  137. gid_t gids[32];
  138. bool extra_gid_added = false;
  139. setgrent();
  140. while (auto* gr = getgrent()) {
  141. for (auto* mem = gr->gr_mem; *mem; ++mem) {
  142. if (!strcmp(*mem, user)) {
  143. gids[count++] = gr->gr_gid;
  144. if (gr->gr_gid == extra_gid)
  145. extra_gid_added = true;
  146. break;
  147. }
  148. }
  149. }
  150. endgrent();
  151. if (!extra_gid_added)
  152. gids[count++] = extra_gid;
  153. return setgroups(count, gids);
  154. }
  155. }