3
0

id.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini id implementation for busybox
  4. *
  5. * Copyright (C) 2000 by Randolph Chung <tausq@debian.org>
  6. * Copyright (C) 2008 by Tito Ragusa <farmatito@tiscali.it>
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. /* BB_AUDIT SUSv3 compliant. */
  11. /* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever
  12. * length and to be more similar to GNU id.
  13. * -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp>
  14. * Added -G option Tito Ragusa (C) 2008 for SUSv3.
  15. */
  16. #include "libbb.h"
  17. /* This is a NOEXEC applet. Be very careful! */
  18. #if !ENABLE_USE_BB_PWD_GRP
  19. #if defined(__UCLIBC_MAJOR__) && (__UCLIBC_MAJOR__ == 0)
  20. #if (__UCLIBC_MINOR__ < 9) || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ < 30)
  21. #error "Sorry, you need at least uClibc version 0.9.30 for id applet to build"
  22. #endif
  23. #endif
  24. #endif
  25. enum {
  26. PRINT_REAL = (1 << 0),
  27. NAME_NOT_NUMBER = (1 << 1),
  28. JUST_USER = (1 << 2),
  29. JUST_GROUP = (1 << 3),
  30. JUST_ALL_GROUPS = (1 << 4),
  31. #if ENABLE_SELINUX
  32. JUST_CONTEXT = (1 << 5),
  33. #endif
  34. };
  35. static int print_common(unsigned id, const char *name, const char *prefix)
  36. {
  37. if (prefix) {
  38. printf("%s", prefix);
  39. }
  40. if (!(option_mask32 & NAME_NOT_NUMBER) || !name) {
  41. printf("%u", id);
  42. }
  43. if (!option_mask32 || (option_mask32 & NAME_NOT_NUMBER)) {
  44. if (name) {
  45. printf(option_mask32 ? "%s" : "(%s)", name);
  46. } else {
  47. /* Don't set error status flag in default mode */
  48. if (option_mask32) {
  49. if (ENABLE_DESKTOP)
  50. bb_error_msg("unknown ID %u", id);
  51. return EXIT_FAILURE;
  52. }
  53. }
  54. }
  55. return EXIT_SUCCESS;
  56. }
  57. static int print_group(gid_t id, const char *prefix)
  58. {
  59. return print_common(id, gid2group(id), prefix);
  60. }
  61. static int print_user(uid_t id, const char *prefix)
  62. {
  63. return print_common(id, uid2uname(id), prefix);
  64. }
  65. /* On error set *n < 0 and return >= 0
  66. * If *n is too small, update it and return < 0
  67. * (ok to trash groups[] in both cases)
  68. * Otherwise fill in groups[] and return >= 0
  69. */
  70. static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n)
  71. {
  72. int m;
  73. if (username) {
  74. /* If the user is a member of more than
  75. * *n groups, then -1 is returned. Otherwise >= 0.
  76. * (and no defined way of detecting errors?!) */
  77. m = getgrouplist(username, rgid, groups, n);
  78. /* I guess *n < 0 might indicate error. Anyway,
  79. * malloc'ing -1 bytes won't be good, so: */
  80. //if (*n < 0)
  81. // return 0;
  82. //return m;
  83. //commented out here, happens below anyway
  84. } else {
  85. /* On error -1 is returned, which ends up in *n */
  86. int nn = getgroups(*n, groups);
  87. /* 0: nn <= *n, groups[] was big enough; -1 otherwise */
  88. m = - (nn > *n);
  89. *n = nn;
  90. }
  91. if (*n < 0)
  92. return 0; /* error, don't return < 0! */
  93. return m;
  94. }
  95. int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  96. int id_main(int argc UNUSED_PARAM, char **argv)
  97. {
  98. uid_t ruid;
  99. gid_t rgid;
  100. uid_t euid;
  101. gid_t egid;
  102. unsigned opt;
  103. int i;
  104. int status = EXIT_SUCCESS;
  105. const char *prefix;
  106. const char *username;
  107. #if ENABLE_SELINUX
  108. security_context_t scontext = NULL;
  109. #endif
  110. /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/
  111. /* Don't allow more than one username */
  112. opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG"
  113. IF_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G");
  114. opt = getopt32(argv, "rnugG" IF_SELINUX("Z"));
  115. username = argv[optind];
  116. if (username) {
  117. struct passwd *p = xgetpwnam(username);
  118. euid = ruid = p->pw_uid;
  119. egid = rgid = p->pw_gid;
  120. } else {
  121. egid = getegid();
  122. rgid = getgid();
  123. euid = geteuid();
  124. ruid = getuid();
  125. }
  126. /* JUST_ALL_GROUPS ignores -r PRINT_REAL flag even if man page for */
  127. /* id says: print the real ID instead of the effective ID, with -ugG */
  128. /* in fact in this case egid is always printed if egid != rgid */
  129. if (!opt || (opt & JUST_ALL_GROUPS)) {
  130. gid_t *groups;
  131. int n;
  132. if (!opt) {
  133. /* Default Mode */
  134. status |= print_user(ruid, "uid=");
  135. status |= print_group(rgid, " gid=");
  136. if (euid != ruid)
  137. status |= print_user(euid, " euid=");
  138. if (egid != rgid)
  139. status |= print_group(egid, " egid=");
  140. } else {
  141. /* JUST_ALL_GROUPS */
  142. status |= print_group(rgid, NULL);
  143. if (egid != rgid)
  144. status |= print_group(egid, " ");
  145. }
  146. /* We are supplying largish buffer, trying
  147. * to not run get_groups() twice. That might be slow
  148. * ("user database in remote SQL server" case) */
  149. groups = xmalloc(64 * sizeof(gid_t));
  150. n = 64;
  151. if (get_groups(username, rgid, groups, &n) < 0) {
  152. /* Need bigger buffer after all */
  153. groups = xrealloc(groups, n * sizeof(gid_t));
  154. get_groups(username, rgid, groups, &n);
  155. }
  156. if (n > 0) {
  157. /* Print the list */
  158. prefix = " groups=";
  159. for (i = 0; i < n; i++) {
  160. if (opt && (groups[i] == rgid || groups[i] == egid))
  161. continue;
  162. status |= print_group(groups[i], opt ? " " : prefix);
  163. prefix = ",";
  164. }
  165. } else if (n < 0) { /* error in get_groups() */
  166. if (!ENABLE_DESKTOP)
  167. bb_error_msg_and_die("can't get groups");
  168. else
  169. return EXIT_FAILURE;
  170. }
  171. if (ENABLE_FEATURE_CLEAN_UP)
  172. free(groups);
  173. #if ENABLE_SELINUX
  174. if (is_selinux_enabled()) {
  175. if (getcon(&scontext) == 0)
  176. printf(" context=%s", scontext);
  177. }
  178. #endif
  179. } else if (opt & PRINT_REAL) {
  180. euid = ruid;
  181. egid = rgid;
  182. }
  183. if (opt & JUST_USER)
  184. status |= print_user(euid, NULL);
  185. else if (opt & JUST_GROUP)
  186. status |= print_group(egid, NULL);
  187. #if ENABLE_SELINUX
  188. else if (opt & JUST_CONTEXT) {
  189. selinux_or_die();
  190. if (username || getcon(&scontext)) {
  191. bb_error_msg_and_die("can't get process context%s",
  192. username ? " for a different user" : "");
  193. }
  194. fputs(scontext, stdout);
  195. }
  196. /* freecon(NULL) seems to be harmless */
  197. if (ENABLE_FEATURE_CLEAN_UP)
  198. freecon(scontext);
  199. #endif
  200. bb_putchar('\n');
  201. fflush_stdout_and_exit(status);
  202. }