id.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. /* Hacked by Tito Ragusa (C) 2004 to handle usernames of whatever
  11. * length and to be more similar to GNU id.
  12. * -Z option support: by Yuichi Nakamura <ynakam@hitachisoft.jp>
  13. * Added -G option Tito Ragusa (C) 2008 for SUSv3.
  14. */
  15. //config:config ID
  16. //config: bool "id"
  17. //config: default y
  18. //config: help
  19. //config: id displays the current user and group ID names.
  20. //config:
  21. //config:config GROUPS
  22. //config: bool "groups"
  23. //config: default y
  24. //config: help
  25. //config: Print the group names associated with current user id.
  26. //applet:IF_GROUPS(APPLET_NOEXEC(groups, id, BB_DIR_USR_BIN, BB_SUID_DROP, groups))
  27. //applet:IF_ID( APPLET_NOEXEC(id, id, BB_DIR_USR_BIN, BB_SUID_DROP, id ))
  28. //kbuild:lib-$(CONFIG_GROUPS) += id.o
  29. //kbuild:lib-$(CONFIG_ID) += id.o
  30. /* BB_AUDIT SUSv3 compliant. */
  31. //usage:#define id_trivial_usage
  32. //usage: "[OPTIONS] [USER]"
  33. //usage:#define id_full_usage "\n\n"
  34. //usage: "Print information about USER or the current user\n"
  35. //usage: IF_SELINUX(
  36. //usage: "\n -Z Security context"
  37. //usage: )
  38. //usage: "\n -u User ID"
  39. //usage: "\n -g Group ID"
  40. //usage: "\n -G Supplementary group IDs"
  41. //usage: "\n -n Print names instead of numbers"
  42. //usage: "\n -r Print real ID instead of effective ID"
  43. //usage:
  44. //usage:#define id_example_usage
  45. //usage: "$ id\n"
  46. //usage: "uid=1000(andersen) gid=1000(andersen)\n"
  47. //usage:#define groups_trivial_usage
  48. //usage: "[USER]"
  49. //usage:#define groups_full_usage "\n\n"
  50. //usage: "Print the group memberships of USER or for the current process"
  51. //usage:
  52. //usage:#define groups_example_usage
  53. //usage: "$ groups\n"
  54. //usage: "andersen lp dialout cdrom floppy\n"
  55. #include "libbb.h"
  56. /* This is a NOEXEC applet. Be very careful! */
  57. #if !ENABLE_USE_BB_PWD_GRP
  58. #if defined(__UCLIBC__) && UCLIBC_VERSION < KERNEL_VERSION(0, 9, 30)
  59. #error "Sorry, you need at least uClibc version 0.9.30 for id applet to build"
  60. #endif
  61. #endif
  62. enum {
  63. PRINT_REAL = (1 << 0),
  64. NAME_NOT_NUMBER = (1 << 1),
  65. JUST_USER = (1 << 2),
  66. JUST_GROUP = (1 << 3),
  67. JUST_ALL_GROUPS = (1 << 4),
  68. #if ENABLE_SELINUX
  69. JUST_CONTEXT = (1 << 5),
  70. #endif
  71. };
  72. static int print_common(unsigned id, const char *name, const char *prefix)
  73. {
  74. if (prefix) {
  75. printf("%s", prefix);
  76. }
  77. if (!(option_mask32 & NAME_NOT_NUMBER) || !name) {
  78. printf("%u", id);
  79. }
  80. if (!option_mask32 || (option_mask32 & NAME_NOT_NUMBER)) {
  81. if (name) {
  82. printf(option_mask32 ? "%s" : "(%s)", name);
  83. } else {
  84. /* Don't set error status flag in default mode */
  85. if (option_mask32) {
  86. if (ENABLE_DESKTOP)
  87. bb_error_msg("unknown ID %u", id);
  88. return EXIT_FAILURE;
  89. }
  90. }
  91. }
  92. return EXIT_SUCCESS;
  93. }
  94. static int print_group(gid_t id, const char *prefix)
  95. {
  96. return print_common(id, gid2group(id), prefix);
  97. }
  98. static int print_user(uid_t id, const char *prefix)
  99. {
  100. return print_common(id, uid2uname(id), prefix);
  101. }
  102. /* On error set *n < 0 and return >= 0
  103. * If *n is too small, update it and return < 0
  104. * (ok to trash groups[] in both cases)
  105. * Otherwise fill in groups[] and return >= 0
  106. */
  107. static int get_groups(const char *username, gid_t rgid, gid_t *groups, int *n)
  108. {
  109. int m;
  110. if (username) {
  111. /* If the user is a member of more than
  112. * *n groups, then -1 is returned. Otherwise >= 0.
  113. * (and no defined way of detecting errors?!) */
  114. m = getgrouplist(username, rgid, groups, n);
  115. /* I guess *n < 0 might indicate error. Anyway,
  116. * malloc'ing -1 bytes won't be good, so: */
  117. if (*n < 0)
  118. return 0;
  119. return m;
  120. }
  121. *n = getgroups(*n, groups);
  122. if (*n >= 0)
  123. return *n;
  124. /* Error */
  125. if (errno == EINVAL) /* *n is too small? */
  126. *n = getgroups(0, groups); /* get needed *n */
  127. /* if *n >= 0, return -1 (got new *n), else return 0 (error): */
  128. return -(*n >= 0);
  129. }
  130. int id_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  131. int id_main(int argc UNUSED_PARAM, char **argv)
  132. {
  133. uid_t ruid;
  134. gid_t rgid;
  135. uid_t euid;
  136. gid_t egid;
  137. unsigned opt;
  138. int i;
  139. int status = EXIT_SUCCESS;
  140. const char *prefix;
  141. const char *username;
  142. #if ENABLE_SELINUX
  143. security_context_t scontext = NULL;
  144. #endif
  145. if (ENABLE_GROUPS && (!ENABLE_ID || applet_name[0] == 'g')) {
  146. /* TODO: coreutils groups prepend "USER : " prefix,
  147. * and accept many usernames. Example:
  148. * # groups root root
  149. * root : root
  150. * root : root
  151. */
  152. opt = option_mask32 = getopt32(argv, "") | JUST_ALL_GROUPS | NAME_NOT_NUMBER;
  153. } else {
  154. /* Don't allow -n -r -nr -ug -rug -nug -rnug -uZ -gZ -GZ*/
  155. /* Don't allow more than one username */
  156. opt_complementary = "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG"
  157. IF_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G");
  158. opt = getopt32(argv, "rnugG" IF_SELINUX("Z"));
  159. }
  160. username = argv[optind];
  161. if (username) {
  162. struct passwd *p = xgetpwnam(username);
  163. euid = ruid = p->pw_uid;
  164. egid = rgid = p->pw_gid;
  165. } else {
  166. egid = getegid();
  167. rgid = getgid();
  168. euid = geteuid();
  169. ruid = getuid();
  170. }
  171. /* JUST_ALL_GROUPS ignores -r PRINT_REAL flag even if man page for */
  172. /* id says: print the real ID instead of the effective ID, with -ugG */
  173. /* in fact in this case egid is always printed if egid != rgid */
  174. if (!opt || (opt & JUST_ALL_GROUPS)) {
  175. gid_t *groups;
  176. int n;
  177. if (!opt) {
  178. /* Default Mode */
  179. status |= print_user(ruid, "uid=");
  180. status |= print_group(rgid, " gid=");
  181. if (euid != ruid)
  182. status |= print_user(euid, " euid=");
  183. if (egid != rgid)
  184. status |= print_group(egid, " egid=");
  185. } else {
  186. /* JUST_ALL_GROUPS */
  187. status |= print_group(rgid, NULL);
  188. if (egid != rgid)
  189. status |= print_group(egid, " ");
  190. }
  191. /* We are supplying largish buffer, trying
  192. * to not run get_groups() twice. That might be slow
  193. * ("user database in remote SQL server" case) */
  194. groups = xmalloc(64 * sizeof(groups[0]));
  195. n = 64;
  196. if (get_groups(username, rgid, groups, &n) < 0) {
  197. /* Need bigger buffer after all */
  198. groups = xrealloc(groups, n * sizeof(groups[0]));
  199. get_groups(username, rgid, groups, &n);
  200. }
  201. if (n > 0) {
  202. /* Print the list */
  203. prefix = " groups=";
  204. for (i = 0; i < n; i++) {
  205. if (opt && (groups[i] == rgid || groups[i] == egid))
  206. continue;
  207. status |= print_group(groups[i], opt ? " " : prefix);
  208. prefix = ",";
  209. }
  210. } else if (n < 0) { /* error in get_groups() */
  211. if (ENABLE_DESKTOP)
  212. bb_error_msg_and_die("can't get groups");
  213. return EXIT_FAILURE;
  214. }
  215. if (ENABLE_FEATURE_CLEAN_UP)
  216. free(groups);
  217. #if ENABLE_SELINUX
  218. if (is_selinux_enabled()) {
  219. if (getcon(&scontext) == 0)
  220. printf(" context=%s", scontext);
  221. }
  222. #endif
  223. } else if (opt & PRINT_REAL) {
  224. euid = ruid;
  225. egid = rgid;
  226. }
  227. if (opt & JUST_USER)
  228. status |= print_user(euid, NULL);
  229. else if (opt & JUST_GROUP)
  230. status |= print_group(egid, NULL);
  231. #if ENABLE_SELINUX
  232. else if (opt & JUST_CONTEXT) {
  233. selinux_or_die();
  234. if (username || getcon(&scontext)) {
  235. bb_error_msg_and_die("can't get process context%s",
  236. username ? " for a different user" : "");
  237. }
  238. fputs(scontext, stdout);
  239. }
  240. /* freecon(NULL) seems to be harmless */
  241. if (ENABLE_FEATURE_CLEAN_UP)
  242. freecon(scontext);
  243. #endif
  244. bb_putchar('\n');
  245. fflush_stdout_and_exit(status);
  246. }