diff options
author | Elliott Hughes <enh@google.com> | 2019-11-04 10:25:35 -0800 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2019-11-04 22:14:00 -0600 |
commit | a0a51dee7cc2b39154f4272430a963f3ebbf222e (patch) | |
tree | 7d40283ec51ef67d464b6a870666e96f524a0c82 | |
parent | 126c317d5ceee7802d52fc6be1b695f759aa6725 (diff) | |
download | toybox-a0a51dee7cc2b39154f4272430a963f3ebbf222e.tar.gz |
id: various fixes.
Handle unknown groups (fixes #117).
Fix -G to show *all* groups, not just all supplementary groups.
Fix -Z output to not include "context=".
-rwxr-xr-x | tests/id.test | 3 | ||||
-rw-r--r-- | toys/posix/id.c | 53 |
2 files changed, 26 insertions, 30 deletions
diff --git a/tests/id.test b/tests/id.test index 6d0f63a8..f62938f0 100755 --- a/tests/id.test +++ b/tests/id.test @@ -6,5 +6,8 @@ testing "id 0" "id 0" "uid=0(root) gid=0(root) groups=0(root)\n" "" "" testing "id root" "id root" "uid=0(root) gid=0(root) groups=0(root)\n" "" "" +testing "id -G root" "id -G root" "0\n" "" "" +testing "id -g root" "id -g root" "0\n" "" "" +testing "id -u root" "id -u root" "0\n" "" "" testing "id no-such-user" "id no-such-user 2>/dev/null ; echo \$?" "1\n" "" "" testing "id 9999999" "id 9999999 2>/dev/null ; echo \$?" "1\n" "" "" diff --git a/toys/posix/id.c b/toys/posix/id.c index b3be1e6e..b1f303bf 100644 --- a/toys/posix/id.c +++ b/toys/posix/id.c @@ -19,9 +19,9 @@ config ID Print user and group ID. - -n Print names instead of numeric IDs (to be used with -Ggu) - -G Show only the group IDs + -G Show all group IDs -g Show only the effective group ID + -n Print names instead of numeric IDs (to be used with -Ggu) -r Show real ID instead of effective ID -u Show only the effective user ID @@ -65,23 +65,17 @@ config WHOAMI GLOBALS( int is_groups; - int separator; ) -static void s_or_u(char *s, unsigned u, int done) +static void showone(char *s, unsigned u) { - if (TT.separator) xputc(TT.separator); - if (FLAG(n)) printf("%s", s); - else printf("%u", u); - if (done) { - xputc('\n'); - xexit(); - } + if (FLAG(n)) printf("%s\n", s); + else printf("%u\n", u); + xexit(); } static void showid(char *header, unsigned u, char *s) { - if (TT.separator) xputc(TT.separator); printf("%s%u(%s)", header, u, s); } @@ -106,10 +100,10 @@ static void do_id(char *username) } pw = xgetpwuid(FLAG(r) ? uid : euid); - if (FLAG(u)) s_or_u(pw->pw_name, pw->pw_uid, 1); + if (FLAG(u)) showone(pw->pw_name, pw->pw_uid); grp = xgetgrgid(FLAG(r) ? gid : egid); - if (FLAG(g)) s_or_u(grp->gr_name, grp->gr_gid, 1); + if (FLAG(g)) showone(grp->gr_name, grp->gr_gid); if (!(toys.optflags&(FLAG_G|FLAG_g|FLAG_Z))) { showid("uid=", pw->pw_uid, pw->pw_name); @@ -125,36 +119,35 @@ static void do_id(char *username) showid(" egid=", grp->gr_gid, grp->gr_name); } } - - showid(" groups=", grp->gr_gid, grp->gr_name); } - if (!FLAG(Z)) { + if (!(toys.optflags&(FLAG_g|FLAG_Z))) { gid_t *groups = (gid_t *)toybuf; int i = sizeof(toybuf)/sizeof(gid_t), ngroups; ngroups = username ? getgrouplist(username, gid, groups, &i) : getgroups(i, groups); - if (ngroups<0) perror_exit(0); - - for (i = 0; i<ngroups; i++) { - TT.separator = FLAG(G) ? ' ' : ','; - if (!(grp = getgrgid(groups[i]))) perror_msg(0); - else if (FLAG(G)) s_or_u(grp->gr_name, grp->gr_gid, 0); - else if (grp->gr_gid != egid) showid("", grp->gr_gid, grp->gr_name); - else TT.separator = 0; // Because we didn't show anything this time. - } + if (ngroups<0) perror_exit("getgroups"); if (FLAG(G)) { - xputc('\n'); - xexit(); + printf("%u", grp->gr_gid); + for (i = 0; i<ngroups; i++) + if (groups[i] != egid) printf(" %u", groups[i]); + } else { + showid(" groups=", gid, grp->gr_name); + for (i = 0; i<ngroups; i++) { + if (groups[i] != egid) { + if ((grp=getgrgid(groups[i]))) showid(",", grp->gr_gid, grp->gr_name); + else printf(",%u", groups[i]); + } + } } } - if (!CFG_TOYBOX_LSM_NONE) { + if (!CFG_TOYBOX_LSM_NONE && !FLAG(G)) { if (lsm_enabled()) { char *context = lsm_context(); - printf(" context=%s"+!!FLAG(Z), context); + printf("%s%s", FLAG(Z) ? "" : " context=", context); if (CFG_TOYBOX_FREE) free(context); } else if (FLAG(Z)) error_exit("%s disabled", lsm_name()); } |