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