3
0

id.c 7.2 KB

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