Mercurial > hg > toybox
annotate toys/id.c @ 533:31215cc6c9f2
Consolidate headers.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 07 Mar 2012 19:04:50 -0600 |
parents | 42a322adbd17 |
children | 06763d1a2f9d |
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 | |
28 #include "toys.h" | |
29 | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
30 #define FLAG_n (1<<4) |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
31 #define FLAG_G (1<<3) |
413 | 32 #define FLAG_g (1<<2) |
33 #define FLAG_r (1<<1) | |
34 #define FLAG_u 1 | |
35 | |
533 | 36 void pretty_print(struct passwd *pw, struct group *grp, struct group **grps, |
37 int n) | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
38 { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
39 int i; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
40 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
|
41 grp->gr_gid, grp->gr_name); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
42 if (n) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
43 printf(" groups= "); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
44 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
45 for (i = 0; i < n; i++) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
46 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
|
47 (i < n-1) ? ",": ""); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
48 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
49 printf("\n"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
50 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
51 |
413 | 52 void id_main(void) |
53 { | |
54 int flags = toys.optflags; | |
55 | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
56 struct passwd *pw; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
57 struct group *grp; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
58 struct group **grps; |
413 | 59 uid_t uid; |
60 gid_t gid; | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
61 gid_t *groups; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
62 int i; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
63 int ngroups; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
64 char *username; |
413 | 65 |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
66 /* check if a username is given */ |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
67 if (*toys.optargs) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
68 username = *(toys.optargs); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
69 pw = getpwnam(username); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
70 if (!pw) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
71 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
|
72 toys.exitval = 1; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
73 return; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
74 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
75 uid = pw->pw_uid; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
76 gid = pw->pw_gid; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
77 } else { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
78 /* show effective, unless user specifies real */ |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
79 if (flags & FLAG_r) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
80 uid = getuid(); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
81 gid = getgid(); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
82 } else { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
83 uid = geteuid(); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
84 gid = getegid(); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
85 } |
413 | 86 } |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
87 |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
88 pw = getpwuid(uid); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
89 if (!pw) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
90 perror("id"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
91 toys.exitval = 1; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
92 return; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
93 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
94 |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
95 grp = getgrgid(pw->pw_gid); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
96 if (!grp) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
97 perror("id"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
98 toys.exitval = 1; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
99 return; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
100 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
101 |
413 | 102 if (flags & FLAG_u) { |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
103 if (flags & FLAG_n) |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
104 printf("%s\n", pw->pw_name); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
105 else |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
106 printf("%d\n", pw->pw_uid); |
413 | 107 return; |
108 } | |
109 if (flags & FLAG_g) { | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
110 if (flags & FLAG_n) |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
111 printf("%s\n", grp->gr_name); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
112 else |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
113 printf("%d\n", grp->gr_gid); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
114 return; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
115 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
116 |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
117 |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
118 if (flags & FLAG_r) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
119 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
|
120 toys.exitval = 1; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
121 return; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
122 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
123 ngroups = sysconf(_SC_NGROUPS_MAX); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
124 /* fallback for number of groups to 32 */ |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
125 if (ngroups < 0) |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
126 ngroups = 32; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
127 groups = malloc(ngroups * sizeof(gid_t)); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
128 if (!groups) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
129 perror("id"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
130 toys.exitval = 1; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
131 return; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
132 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
133 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
|
134 perror("id"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
135 toys.exitval = 1; |
413 | 136 return; |
137 } | |
493
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
138 grps = malloc(ngroups * sizeof(struct group *)); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
139 for (i = 0; i < ngroups; i++) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
140 struct group *tmp; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
141 grps[i] = malloc(sizeof(struct group)); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
142 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
|
143 char *buf = malloc(f); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
144 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
|
145 perror("id"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
146 continue; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
147 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
148 if (tmp == NULL) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
149 perror("id"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
150 continue; |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
151 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
152 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
153 |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
154 if (flags & FLAG_G) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
155 for (i = 0; i < ngroups; i++) { |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
156 if (flags & FLAG_n) |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
157 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
|
158 else |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
159 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
|
160 } |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
161 printf("\n"); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
162 return; |
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 |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
165 pretty_print(pw, grp, grps, ngroups); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
166 for (i=0; i < ngroups; i++) |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
167 free(grps[i]); |
42a322adbd17
update id to be SUS compliant
Daniel Walter <d.walter@0x90.at>
parents:
430
diff
changeset
|
168 |
413 | 169 } |