grp.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <errno_numbers.h>
  29. #include <grp.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. extern "C" {
  35. static FILE* s_stream = nullptr;
  36. static unsigned s_line_number = 0;
  37. static struct group s_group;
  38. static String s_name;
  39. static String s_passwd;
  40. static Vector<String> s_members;
  41. static Vector<const char*> s_members_ptrs;
  42. void setgrent()
  43. {
  44. s_line_number = 0;
  45. if (s_stream) {
  46. rewind(s_stream);
  47. } else {
  48. s_stream = fopen("/etc/group", "r");
  49. if (!s_stream) {
  50. perror("open /etc/group");
  51. }
  52. }
  53. }
  54. void endgrent()
  55. {
  56. s_line_number = 0;
  57. if (s_stream) {
  58. fclose(s_stream);
  59. s_stream = nullptr;
  60. }
  61. memset(&s_group, 0, sizeof(s_group));
  62. s_name = {};
  63. s_passwd = {};
  64. s_members = {};
  65. s_members_ptrs = {};
  66. }
  67. struct group* getgrgid(gid_t gid)
  68. {
  69. setgrent();
  70. while (auto* gr = getgrent()) {
  71. if (gr->gr_gid == gid)
  72. return gr;
  73. }
  74. return nullptr;
  75. }
  76. struct group* getgrnam(const char* name)
  77. {
  78. setgrent();
  79. while (auto* gr = getgrent()) {
  80. if (!strcmp(gr->gr_name, name))
  81. return gr;
  82. }
  83. return nullptr;
  84. }
  85. static bool parse_grpdb_entry(const String& line)
  86. {
  87. auto parts = line.split_view(':', true);
  88. if (parts.size() != 4) {
  89. fprintf(stderr, "getgrent(): Malformed entry on line %u: '%s' has %zu parts\n", s_line_number, line.characters(), parts.size());
  90. return false;
  91. }
  92. s_name = parts[0];
  93. s_passwd = parts[1];
  94. auto& gid_string = parts[2];
  95. String members_string = parts[3];
  96. auto gid = gid_string.to_uint();
  97. if (!gid.has_value()) {
  98. fprintf(stderr, "getgrent(): Malformed GID on line %u\n", s_line_number);
  99. return false;
  100. }
  101. s_members = members_string.split(',');
  102. s_members_ptrs.clear_with_capacity();
  103. s_members_ptrs.ensure_capacity(s_members.size() + 1);
  104. for (auto& member : s_members) {
  105. s_members_ptrs.append(member.characters());
  106. }
  107. s_members_ptrs.append(nullptr);
  108. s_group.gr_gid = gid.value();
  109. s_group.gr_name = const_cast<char*>(s_name.characters());
  110. s_group.gr_passwd = const_cast<char*>(s_passwd.characters());
  111. s_group.gr_mem = const_cast<char**>(s_members_ptrs.data());
  112. return true;
  113. }
  114. struct group* getgrent()
  115. {
  116. if (!s_stream)
  117. setgrent();
  118. while (true) {
  119. if (!s_stream || feof(s_stream))
  120. return nullptr;
  121. if (ferror(s_stream)) {
  122. fprintf(stderr, "getgrent(): Read error: %s\n", strerror(ferror(s_stream)));
  123. return nullptr;
  124. }
  125. char buffer[1024];
  126. ++s_line_number;
  127. char* s = fgets(buffer, sizeof(buffer), s_stream);
  128. // Silently tolerate an empty line at the end.
  129. if ((!s || !s[0]) && feof(s_stream))
  130. return nullptr;
  131. String line(s, Chomp);
  132. if (parse_grpdb_entry(line))
  133. return &s_group;
  134. // Otherwise, proceed to the next line.
  135. }
  136. }
  137. int initgroups(const char* user, gid_t extra_gid)
  138. {
  139. size_t count = 0;
  140. gid_t gids[32];
  141. bool extra_gid_added = false;
  142. setgrent();
  143. while (auto* gr = getgrent()) {
  144. for (auto* mem = gr->gr_mem; *mem; ++mem) {
  145. if (!strcmp(*mem, user)) {
  146. gids[count++] = gr->gr_gid;
  147. if (gr->gr_gid == extra_gid)
  148. extra_gid_added = true;
  149. break;
  150. }
  151. }
  152. }
  153. endgrent();
  154. if (!extra_gid_added)
  155. gids[count++] = extra_gid;
  156. return setgroups(count, gids);
  157. }
  158. }