id.cpp 5.3 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 <LibCore/ArgsParser.h>
  27. #include <alloca.h>
  28. #include <grp.h>
  29. #include <pwd.h>
  30. #include <stdio.h>
  31. #include <unistd.h>
  32. static int print_id_objects();
  33. static bool flag_print_uid = false;
  34. static bool flag_print_gid = false;
  35. static bool flag_print_name = false;
  36. static bool flag_print_gid_all = false;
  37. int main(int argc, char** argv)
  38. {
  39. if (unveil("/etc/passwd", "r") < 0) {
  40. perror("unveil");
  41. return 1;
  42. }
  43. if (unveil("/etc/group", "r") < 0) {
  44. perror("unveil");
  45. return 1;
  46. }
  47. if (unveil(nullptr, nullptr) < 0) {
  48. perror("unveil");
  49. return 1;
  50. }
  51. if (pledge("stdio rpath", nullptr) < 0) {
  52. perror("pledge");
  53. return 1;
  54. }
  55. Core::ArgsParser args_parser;
  56. args_parser.add_option(flag_print_uid, "Print UID", nullptr, 'u');
  57. args_parser.add_option(flag_print_gid, "Print GID", nullptr, 'g');
  58. args_parser.add_option(flag_print_gid_all, "Print all GIDs", nullptr, 'G');
  59. args_parser.add_option(flag_print_name, "Print name", nullptr, 'n');
  60. args_parser.parse(argc, argv);
  61. if (flag_print_name && !(flag_print_uid || flag_print_gid || flag_print_gid_all)) {
  62. fprintf(stderr, "cannot print only names or real IDs in default format\n");
  63. return 1;
  64. }
  65. if (flag_print_uid + flag_print_gid + flag_print_gid_all > 1) {
  66. fprintf(stderr, "cannot print \"only\" of more than one choice\n");
  67. return 1;
  68. }
  69. int status = print_id_objects();
  70. return status;
  71. }
  72. static bool print_uid_object(uid_t uid)
  73. {
  74. if (flag_print_name) {
  75. struct passwd* pw = getpwuid(uid);
  76. printf("%s", pw ? pw->pw_name : "n/a");
  77. } else
  78. printf("%u", uid);
  79. return true;
  80. }
  81. static bool print_gid_object(gid_t gid)
  82. {
  83. if (flag_print_name) {
  84. struct group* gr = getgrgid(gid);
  85. printf("%s", gr ? gr->gr_name : "n/a");
  86. } else
  87. printf("%u", gid);
  88. return true;
  89. }
  90. static bool print_gid_list()
  91. {
  92. int extra_gid_count = getgroups(0, nullptr);
  93. if (extra_gid_count) {
  94. auto* extra_gids = (gid_t*)alloca(extra_gid_count * sizeof(gid_t));
  95. int rc = getgroups(extra_gid_count, extra_gids);
  96. if (rc < 0) {
  97. perror("\ngetgroups");
  98. return false;
  99. }
  100. for (int g = 0; g < extra_gid_count; ++g) {
  101. auto* gr = getgrgid(extra_gids[g]);
  102. if (flag_print_name && gr)
  103. printf("%s", gr->gr_name);
  104. else
  105. printf("%u", extra_gids[g]);
  106. if (g != extra_gid_count - 1)
  107. printf(" ");
  108. }
  109. }
  110. return true;
  111. }
  112. static bool print_full_id_list()
  113. {
  114. uid_t uid = getuid();
  115. gid_t gid = getgid();
  116. struct passwd* pw = getpwuid(uid);
  117. struct group* gr = getgrgid(gid);
  118. printf("uid=%u(%s) gid=%u(%s)", uid, pw ? pw->pw_name : "n/a", gid, gr ? gr->gr_name : "n/a");
  119. int extra_gid_count = getgroups(0, nullptr);
  120. if (extra_gid_count) {
  121. auto* extra_gids = (gid_t*)alloca(extra_gid_count * sizeof(gid_t));
  122. int rc = getgroups(extra_gid_count, extra_gids);
  123. if (rc < 0) {
  124. perror("\ngetgroups");
  125. return false;
  126. }
  127. printf(" groups=");
  128. for (int g = 0; g < extra_gid_count; ++g) {
  129. auto* gr = getgrgid(extra_gids[g]);
  130. if (gr)
  131. printf("%u(%s)", extra_gids[g], gr->gr_name);
  132. else
  133. printf("%u", extra_gids[g]);
  134. if (g != extra_gid_count - 1)
  135. printf(",");
  136. }
  137. }
  138. return true;
  139. }
  140. static int print_id_objects()
  141. {
  142. if (flag_print_uid) {
  143. if (!print_uid_object(getuid()))
  144. return 1;
  145. } else if (flag_print_gid) {
  146. if (!print_gid_object(getgid()))
  147. return 1;
  148. } else if (flag_print_gid_all) {
  149. if (!print_gid_list())
  150. return 1;
  151. } else {
  152. if (!print_full_id_list())
  153. return 1;
  154. }
  155. printf("\n");
  156. return 0;
  157. }