Mercurial > hg > toybox
annotate toys/id.c @ 493:42a322adbd17
update id to be SUS compliant
* add -n and -G flag
* allow a username to be given as argument
* display complete list of groups
* include it in default build
author | Daniel Walter <d.walter@0x90.at> |
---|---|
date | Tue, 21 Feb 2012 21:39:20 -0600 |
parents | 74f152c5fa30 |
children | 31215cc6c9f2 |
rev | line source |
---|---|
413 | 1 /* vi: set sw=4 ts=4: |
2 * | |
3 * id.c - print real and effective user and group IDs | |
4 * | |
5 * Copyright 2012 Sony Network Entertainment, Inc. | |
6 * | |
7 * by Tim Bird <tim.bird@am.sony.com> | |
8 * | |
9 * See http://www.opengroup.org/onlinepubs/009695399/utilities/id.html | |
10 | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
11 USE_ID(NEWTOY(id, "nGgru", TOYFLAG_BIN)) |
413 | 12 |
13 config ID | |
14 bool "id" | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
15 default y |
413 | 16 help |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
17 usage: id [-nGgru] |
413 | 18 |
19 Print user and group ID. | |
20 | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
21 -n print names instead of numeric IDs (to be used with -Ggu) |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
22 -G Show only the group IDs |
413 | 23 -g Show only the effective group ID |
24 -r Show real ID instead of effective ID | |
25 -u Show only the effective user ID | |
26 */ | |
27 | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
28 #include <sys/types.h> |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
29 #include <pwd.h> |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
30 #include <grp.h> |
413 | 31 #include "toys.h" |
32 | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
33 #define FLAG_n (1<<4) |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
34 #define FLAG_G (1<<3) |
413 | 35 #define FLAG_g (1<<2) |
36 #define FLAG_r (1<<1) | |
37 #define FLAG_u 1 | |
38 | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
39 void |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
40 pretty_print(struct passwd *pw, struct group *grp, struct group **grps, int n) |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
41 { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
42 int i; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
43 printf("uid= %d(%s) gid= %d(%s)", pw->pw_uid, pw->pw_name, |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
44 grp->gr_gid, grp->gr_name); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
45 if (n) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
46 printf(" groups= "); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
47 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
48 for (i = 0; i < n; i++) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
49 printf("%d(%s)%s", grps[i]->gr_gid, grps[i]->gr_name, |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
50 (i < n-1) ? ",": ""); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
51 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
52 printf("\n"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
53 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
54 |
413 | 55 void id_main(void) |
56 { | |
57 int flags = toys.optflags; | |
58 | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
59 struct passwd *pw; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
60 struct group *grp; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
61 struct group **grps; |
413 | 62 uid_t uid; |
63 gid_t gid; | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
64 gid_t *groups; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
65 int i; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
66 int ngroups; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
67 char *username; |
413 | 68 |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
69 /* check if a username is given */ |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
70 if (*toys.optargs) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
71 username = *(toys.optargs); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
72 pw = getpwnam(username); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
73 if (!pw) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
74 printf("id: %s: no such user\n", username); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
75 toys.exitval = 1; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
76 return; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
77 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
78 uid = pw->pw_uid; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
79 gid = pw->pw_gid; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
80 } else { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
81 /* show effective, unless user specifies real */ |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
82 if (flags & FLAG_r) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
83 uid = getuid(); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
84 gid = getgid(); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
85 } else { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
86 uid = geteuid(); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
87 gid = getegid(); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
88 } |
413 | 89 } |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
90 |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
91 pw = getpwuid(uid); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
92 if (!pw) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
93 perror("id"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
94 toys.exitval = 1; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
95 return; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
96 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
97 |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
98 grp = getgrgid(pw->pw_gid); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
99 if (!grp) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
100 perror("id"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
101 toys.exitval = 1; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
102 return; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
103 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
104 |
413 | 105 if (flags & FLAG_u) { |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
106 if (flags & FLAG_n) |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
107 printf("%s\n", pw->pw_name); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
108 else |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
109 printf("%d\n", pw->pw_uid); |
413 | 110 return; |
111 } | |
112 if (flags & FLAG_g) { | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
113 if (flags & FLAG_n) |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
114 printf("%s\n", grp->gr_name); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
115 else |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
116 printf("%d\n", grp->gr_gid); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
117 return; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
118 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
119 |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
120 |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
121 if (flags & FLAG_r) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
122 printf("-r can only be used in combination with -u or -g\n"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
123 toys.exitval = 1; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
124 return; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
125 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
126 ngroups = sysconf(_SC_NGROUPS_MAX); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
127 /* fallback for number of groups to 32 */ |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
128 if (ngroups < 0) |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
129 ngroups = 32; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
130 groups = malloc(ngroups * sizeof(gid_t)); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
131 if (!groups) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
132 perror("id"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
133 toys.exitval = 1; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
134 return; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
135 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
136 if (getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups) < 0) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
137 perror("id"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
138 toys.exitval = 1; |
413 | 139 return; |
140 } | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
141 grps = malloc(ngroups * sizeof(struct group *)); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
142 for (i = 0; i < ngroups; i++) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
143 struct group *tmp; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
144 grps[i] = malloc(sizeof(struct group)); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
145 size_t f = sysconf(_SC_GETGR_R_SIZE_MAX); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
146 char *buf = malloc(f); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
147 if (getgrgid_r(groups[i], grps[i], buf, f, &tmp) < 0) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
148 perror("id"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
149 continue; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
150 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
151 if (tmp == NULL) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
152 perror("id"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
153 continue; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
154 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
155 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
156 |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
157 if (flags & FLAG_G) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
158 for (i = 0; i < ngroups; i++) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
159 if (flags & FLAG_n) |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
160 printf("%s%s", !i ? "" : " ", grps[i]->gr_name); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
161 else |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
162 printf("%s%d", !i ? "" : " ", grps[i]->gr_gid); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
163 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
164 printf("\n"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
165 return; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
166 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
167 |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
168 pretty_print(pw, grp, grps, ngroups); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
169 for (i=0; i < ngroups; i++) |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
170 free(grps[i]); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
171 |
413 | 172 } |