id.c 5.6 KB

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