1 /* id.c - print real and effective user and group IDs
3 * Copyright 2012 Sony Network Entertainment, Inc.
5 * by Tim Bird <tim.bird@am.sony.com>
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/id.html
9 USE_ID(NEWTOY(id, ">1"USE_ID_Z("Z")"nGgru[!"USE_ID_Z("Z")"Ggu]", TOYFLAG_USR|TOYFLAG_BIN))
10 USE_GROUPS(NEWTOY(groups, NULL, TOYFLAG_USR|TOYFLAG_BIN))
11 USE_LOGNAME(NEWTOY(logname, ">0", TOYFLAG_USR|TOYFLAG_BIN))
12 USE_WHOAMI(OLDTOY(whoami, logname, TOYFLAG_USR|TOYFLAG_BIN))
20 Print user and group ID.
22 -n print names instead of numeric IDs (to be used with -Ggu)
23 -G Show only the group IDs
24 -g Show only the effective group ID
25 -r Show real ID instead of effective ID
26 -u Show only the effective user ID
31 depends on ID && !TOYBOX_LSM_NONE
35 -Z Show only security context
43 Print the groups a user is in.
51 Print the current user name.
59 Print the current user name.
70 static void s_or_u(char *s, unsigned u, int done)
72 if (toys.optflags&FLAG_n) printf("%s", s);
80 static void showid(char *header, unsigned u, char *s)
82 printf("%s%u(%s)", header, u, s);
85 void do_id(char *username)
87 int flags, i, ngroups;
90 uid_t uid = getuid(), euid = geteuid();
91 gid_t gid = getgid(), egid = getegid(), *groups;
93 flags = toys.optflags;
95 // check if a username is given
97 pw = xgetpwnam(username);
98 uid = euid = pw->pw_uid;
99 gid = egid = pw->pw_gid;
100 if (TT.is_groups) printf("%s : ", pw->pw_name);
104 pw = xgetpwuid(i ? uid : euid);
105 if (toys.optflags&FLAG_u) s_or_u(pw->pw_name, pw->pw_uid, 1);
107 grp = xgetgrgid(i ? gid : egid);
108 if (flags & FLAG_g) s_or_u(grp->gr_name, grp->gr_gid, 1);
110 if (!(toys.optflags&(FLAG_g|FLAG_Z))) {
111 showid("uid=", pw->pw_uid, pw->pw_name);
112 showid(" gid=", grp->gr_gid, grp->gr_name);
116 pw = xgetpwuid(euid);
117 showid(" euid=", pw->pw_uid, pw->pw_name);
120 grp = xgetgrgid(egid);
121 showid(" egid=", grp->gr_gid, grp->gr_name);
125 showid(" groups=", grp->gr_gid, grp->gr_name);
128 if (!(toys.optflags&FLAG_Z)) {
129 groups = (gid_t *)toybuf;
130 i = sizeof(toybuf)/sizeof(gid_t);
131 ngroups = username ? getgrouplist(username, gid, groups, &i)
132 : getgroups(i, groups);
133 if (ngroups<0) perror_exit(0);
135 int show_separator = !(toys.optflags&FLAG_G);
136 for (i = 0; i<ngroups; i++) {
137 if (show_separator) xputc((toys.optflags&FLAG_G) ? ' ' : ',');
139 if (!(grp = getgrgid(groups[i]))) perror_msg(0);
140 else if (toys.optflags&FLAG_G) s_or_u(grp->gr_name, grp->gr_gid, 0);
141 else if (grp->gr_gid != egid) showid("", grp->gr_gid, grp->gr_name);
142 else show_separator = 0; // Because we didn't show anything this time.
144 if (toys.optflags&FLAG_G) {
150 if (!CFG_TOYBOX_LSM_NONE) {
152 char *context = lsm_context();
154 printf(" context=%s"+!!(toys.optflags&FLAG_Z), context);
155 if (CFG_TOYBOX_FREE) free(context);
156 } else if (toys.optflags&FLAG_Z) error_exit("%s disabled", lsm_name());
164 if (toys.optc) while(*toys.optargs) do_id(*toys.optargs++);
168 void groups_main(void)
171 toys.optflags = FLAG_G|FLAG_n;
175 void logname_main(void)
177 toys.optflags = FLAG_u|FLAG_n;