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_SELINUX("Z")"nGgru[!"USE_ID_SELINUX("Z")"Ggu]", TOYFLAG_BIN))
10 USE_GROUPS(NEWTOY(groups, NULL, TOYFLAG_USR|TOYFLAG_BIN))
11 USE_LOGNAME(NEWTOY(logname, ">0", TOYFLAG_BIN))
12 USE_WHOAMI(OLDTOY(whoami, logname, 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_SELINUX
35 -Z Show only SELinux context
43 Print the groups a user is in.
51 Print the current user name.
59 Print the current user name.
66 int do_u, do_n, do_G, do_Z, is_groups;
69 static void s_or_u(char *s, unsigned u, int done)
71 if (TT.do_n) printf("%s", s);
79 static void showid(char *header, unsigned u, char *s)
81 printf("%s%u(%s)", header, u, s);
84 void do_id(char *username)
86 int flags, i, ngroups;
89 uid_t uid = getuid(), euid = geteuid();
90 gid_t gid = getgid(), egid = getegid(), *groups;
92 flags = toys.optflags;
94 // check if a username is given
96 pw = xgetpwnam(username);
97 uid = euid = pw->pw_uid;
98 gid = egid = pw->pw_gid;
99 if (TT.is_groups) printf("%s : ", pw->pw_name);
103 pw = xgetpwuid(i ? uid : euid);
104 if (TT.do_u) s_or_u(pw->pw_name, pw->pw_uid, 1);
106 grp = xgetgrgid(i ? gid : egid);
107 if (flags & FLAG_g) s_or_u(grp->gr_name, grp->gr_gid, 1);
109 if (!TT.do_G && !TT.do_Z) {
110 showid("uid=", pw->pw_uid, pw->pw_name);
111 showid(" gid=", grp->gr_gid, grp->gr_name);
115 pw = xgetpwuid(euid);
116 showid(" euid=", pw->pw_uid, pw->pw_name);
119 grp = xgetgrgid(egid);
120 showid(" egid=", grp->gr_gid, grp->gr_name);
124 showid(" groups=", grp->gr_gid, grp->gr_name);
128 groups = (gid_t *)toybuf;
129 i = sizeof(toybuf)/sizeof(gid_t);
130 ngroups = username ? getgrouplist(username, gid, groups, &i)
131 : getgroups(i, groups);
132 if (ngroups<0) perror_exit(0);
134 int show_separator = !TT.do_G;
135 for (i = 0; i<ngroups; i++) {
136 if (show_separator) xputc(TT.do_G ? ' ' : ',');
138 if (!(grp = getgrgid(groups[i]))) perror_msg(0);
139 else if (TT.do_G) s_or_u(grp->gr_name, grp->gr_gid, 0);
140 else if (grp->gr_gid != egid) showid("", grp->gr_gid, grp->gr_name);
141 else show_separator = 0; // Because we didn't show anything this time.
149 #if CFG_TOYBOX_SELINUX
150 char *context = NULL;
151 if (is_selinux_enabled() < 1) {
153 error_exit("SELinux disabled");
154 } else if (getcon(&context) == 0) {
155 if (!TT.do_Z) xputc(' ');
156 printf("context=%s", context);
158 if (CFG_TOYBOX_FREE) free(context);
166 // FLAG macros can be 0 if "id" command not enabled, so snapshot them here.
167 if (FLAG_u) TT.do_u |= toys.optflags & FLAG_u;
168 if (FLAG_n) TT.do_n |= toys.optflags & FLAG_n;
169 if (FLAG_G) TT.do_G |= toys.optflags & FLAG_G;
170 if (FLAG_Z) TT.do_Z |= toys.optflags & FLAG_Z;
172 if (toys.optc) while(*toys.optargs) do_id(*toys.optargs++);
176 void groups_main(void)
179 TT.do_G = TT.do_n = 1;
183 void logname_main(void)
185 TT.do_u = TT.do_n = 1;