id.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* id.c - print real and effective user and group IDs
  2. *
  3. * Copyright 2012 Sony Network Entertainment, Inc.
  4. *
  5. * by Tim Bird <tim.bird@am.sony.com>
  6. *
  7. * See http://opengroup.org/onlinepubs/9699919799/utilities/id.html
  8. USE_ID(NEWTOY(id, ">1"USE_ID_Z("Z")"nGgru[!"USE_ID_Z("Z")"Ggu]", TOYFLAG_USR|TOYFLAG_BIN))
  9. USE_GROUPS(NEWTOY(groups, NULL, TOYFLAG_USR|TOYFLAG_BIN))
  10. USE_LOGNAME(NEWTOY(logname, ">0", TOYFLAG_USR|TOYFLAG_BIN))
  11. USE_WHOAMI(OLDTOY(whoami, logname, TOYFLAG_USR|TOYFLAG_BIN))
  12. config ID
  13. bool "id"
  14. default y
  15. help
  16. usage: id [-nGgru] [USER...]
  17. Print user and group ID.
  18. -G Show all group IDs
  19. -g Show only the effective group ID
  20. -n Print names instead of numeric IDs (to be used with -Ggu)
  21. -r Show real ID instead of effective ID
  22. -u Show only the effective user ID
  23. config ID_Z
  24. bool
  25. default y
  26. depends on ID && !TOYBOX_LSM_NONE
  27. help
  28. usage: id [-Z]
  29. -Z Show only security context
  30. config GROUPS
  31. bool "groups"
  32. default y
  33. help
  34. usage: groups [user]
  35. Print the groups a user is in.
  36. config LOGNAME
  37. bool "logname"
  38. default y
  39. help
  40. usage: logname
  41. Print the current user name.
  42. config WHOAMI
  43. bool "whoami"
  44. default y
  45. help
  46. usage: whoami
  47. Print the current user name.
  48. */
  49. #define FOR_id
  50. #define FORCE_FLAGS
  51. #include "toys.h"
  52. GLOBALS(
  53. int is_groups;
  54. )
  55. static void showone(char *prefix, char *s, unsigned u, int done)
  56. {
  57. if (FLAG(n)) printf("%s%s", prefix, s);
  58. else printf("%s%u", prefix, u);
  59. if (done) {
  60. xputc('\n');
  61. xexit();
  62. }
  63. }
  64. static void showid(char *prefix, unsigned u, char *s)
  65. {
  66. printf("%s%u(%s)", prefix, u, s);
  67. }
  68. static void do_id(char *username)
  69. {
  70. struct passwd *pw;
  71. struct group *grp;
  72. uid_t uid = getuid(), euid = geteuid();
  73. gid_t gid = getgid(), egid = getegid();
  74. gid_t *groups = (gid_t *)toybuf;
  75. int i = sizeof(toybuf)/sizeof(gid_t), ngroups;
  76. // check if a username is given
  77. if (username) {
  78. pw = getpwnam(username);
  79. if (!pw) {
  80. uid = atolx_range(username, 0, INT_MAX);
  81. if ((pw = getpwuid(uid))) username = pw->pw_name;
  82. }
  83. if (!pw) error_exit("no such user '%s'", username);
  84. uid = euid = pw->pw_uid;
  85. gid = egid = pw->pw_gid;
  86. if (TT.is_groups) printf("%s : ", pw->pw_name);
  87. }
  88. pw = xgetpwuid(FLAG(r) ? uid : euid);
  89. if (FLAG(u)) showone("", pw->pw_name, pw->pw_uid, 1);
  90. grp = xgetgrgid(FLAG(r) ? gid : egid);
  91. if (FLAG(g)) showone("", grp->gr_name, grp->gr_gid, 1);
  92. ngroups = username ? getgrouplist(username, gid, groups, &i)
  93. : getgroups(i, groups);
  94. if (ngroups<0) perror_exit("getgroups");
  95. if (FLAG(G)) {
  96. showone("", grp->gr_name, grp->gr_gid, 0);
  97. for (i = 0; i<ngroups; i++) {
  98. if (groups[i] != egid) {
  99. if ((grp=getgrgid(groups[i]))) showone(" ",grp->gr_name,grp->gr_gid,0);
  100. else printf(" %u", groups[i]);
  101. }
  102. }
  103. xputc('\n');
  104. return;
  105. }
  106. if (!FLAG(Z)) {
  107. showid("uid=", pw->pw_uid, pw->pw_name);
  108. showid(" gid=", grp->gr_gid, grp->gr_name);
  109. if (!FLAG(r)) {
  110. if (uid != euid) {
  111. pw = xgetpwuid(euid);
  112. showid(" euid=", pw->pw_uid, pw->pw_name);
  113. }
  114. if (gid != egid) {
  115. grp = xgetgrgid(egid);
  116. showid(" egid=", grp->gr_gid, grp->gr_name);
  117. }
  118. }
  119. showid(" groups=", gid, grp->gr_name);
  120. for (i = 0; i<ngroups; i++) {
  121. if (groups[i] != egid) {
  122. if ((grp=getgrgid(groups[i]))) showid(",", grp->gr_gid, grp->gr_name);
  123. else printf(",%u", groups[i]);
  124. }
  125. }
  126. }
  127. if (!CFG_TOYBOX_LSM_NONE) {
  128. if (lsm_enabled()) {
  129. char *context = lsm_context();
  130. printf("%s%s", FLAG(Z) ? "" : " context=", context);
  131. if (CFG_TOYBOX_FREE) free(context);
  132. } else if (FLAG(Z)) error_exit("%s disabled", lsm_name());
  133. }
  134. xputc('\n');
  135. }
  136. void id_main(void)
  137. {
  138. if (toys.optc) while(*toys.optargs) do_id(*toys.optargs++);
  139. else do_id(NULL);
  140. }
  141. void groups_main(void)
  142. {
  143. TT.is_groups = 1;
  144. toys.optflags = FLAG_G|FLAG_n;
  145. id_main();
  146. }
  147. void logname_main(void)
  148. {
  149. toys.optflags = FLAG_u|FLAG_n;
  150. id_main();
  151. }