id.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 (7 kb)"
  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 (6.7 kb)"
  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 = getopt32(argv, "^"
  157. "rnugG" IF_SELINUX("Z")
  158. "\0"
  159. "?1:u--g:g--u:G--u:u--G:g--G:G--g:r?ugG:n?ugG"
  160. IF_SELINUX(":u--Z:Z--u:g--Z:Z--g:G--Z:Z--G")
  161. );
  162. }
  163. username = argv[optind];
  164. if (username) {
  165. struct passwd *p = xgetpwnam(username);
  166. euid = ruid = p->pw_uid;
  167. egid = rgid = p->pw_gid;
  168. } else {
  169. egid = getegid();
  170. rgid = getgid();
  171. euid = geteuid();
  172. ruid = getuid();
  173. }
  174. /* JUST_ALL_GROUPS ignores -r PRINT_REAL flag even if man page for */
  175. /* id says: print the real ID instead of the effective ID, with -ugG */
  176. /* in fact in this case egid is always printed if egid != rgid */
  177. if (!opt || (opt & JUST_ALL_GROUPS)) {
  178. gid_t *groups;
  179. int n;
  180. if (!opt) {
  181. /* Default Mode */
  182. status |= print_user(ruid, "uid=");
  183. status |= print_group(rgid, " gid=");
  184. if (euid != ruid)
  185. status |= print_user(euid, " euid=");
  186. if (egid != rgid)
  187. status |= print_group(egid, " egid=");
  188. } else {
  189. /* JUST_ALL_GROUPS */
  190. status |= print_group(rgid, NULL);
  191. if (egid != rgid)
  192. status |= print_group(egid, " ");
  193. }
  194. /* We are supplying largish buffer, trying
  195. * to not run get_groups() twice. That might be slow
  196. * ("user database in remote SQL server" case) */
  197. groups = xmalloc(64 * sizeof(groups[0]));
  198. n = 64;
  199. if (get_groups(username, rgid, groups, &n) < 0) {
  200. /* Need bigger buffer after all */
  201. groups = xrealloc(groups, n * sizeof(groups[0]));
  202. get_groups(username, rgid, groups, &n);
  203. }
  204. if (n > 0) {
  205. /* Print the list */
  206. prefix = " groups=";
  207. for (i = 0; i < n; i++) {
  208. if (opt && (groups[i] == rgid || groups[i] == egid))
  209. continue;
  210. status |= print_group(groups[i], opt ? " " : prefix);
  211. prefix = ",";
  212. }
  213. } else if (n < 0) { /* error in get_groups() */
  214. if (ENABLE_DESKTOP)
  215. bb_simple_error_msg_and_die("can't get groups");
  216. return EXIT_FAILURE;
  217. }
  218. if (ENABLE_FEATURE_CLEAN_UP)
  219. free(groups);
  220. #if ENABLE_SELINUX
  221. if (is_selinux_enabled()) {
  222. if (getcon(&scontext) == 0)
  223. printf(" context=%s", scontext);
  224. }
  225. #endif
  226. } else if (opt & PRINT_REAL) {
  227. euid = ruid;
  228. egid = rgid;
  229. }
  230. if (opt & JUST_USER)
  231. status |= print_user(euid, NULL);
  232. else if (opt & JUST_GROUP)
  233. status |= print_group(egid, NULL);
  234. #if ENABLE_SELINUX
  235. else if (opt & JUST_CONTEXT) {
  236. selinux_or_die();
  237. if (username || getcon(&scontext)) {
  238. bb_error_msg_and_die("can't get process context%s",
  239. username ? " for a different user" : "");
  240. }
  241. fputs(scontext, stdout);
  242. }
  243. /* freecon(NULL) seems to be harmless */
  244. if (ENABLE_FEATURE_CLEAN_UP)
  245. freecon(scontext);
  246. #endif
  247. bb_putchar('\n');
  248. fflush_stdout_and_exit(status);
  249. }