|
Lines 1-530
Link Here
|
| 1 |
/*- |
1 |
/*- |
| 2 |
* SPDX-License-Identifier: BSD-3-Clause |
2 |
* SPDX-License-Identifier: BSD-3-Clause |
| 3 |
* |
3 |
* |
| 4 |
* Copyright (c) 1991, 1993 |
4 |
* Copyright (c) 1991, 1993 |
| 5 |
* The Regents of the University of California. All rights reserved. |
5 |
* The Regents of the University of California. All rights reserved. |
| 6 |
* |
6 |
* |
| 7 |
* Redistribution and use in source and binary forms, with or without |
7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions |
8 |
* modification, are permitted provided that the following conditions |
| 9 |
* are met: |
9 |
* are met: |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
12 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* 3. Neither the name of the University nor the names of its contributors |
15 |
* 3. Neither the name of the University nor the names of its contributors |
| 16 |
* may be used to endorse or promote products derived from this software |
16 |
* may be used to endorse or promote products derived from this software |
| 17 |
* without specific prior written permission. |
17 |
* without specific prior written permission. |
| 18 |
* |
18 |
* |
| 19 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
19 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 20 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
22 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 23 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 24 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
24 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 25 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
25 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
26 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 27 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
27 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 28 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
28 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 29 |
* SUCH DAMAGE. |
29 |
* SUCH DAMAGE. |
| 30 |
*/ |
30 |
*/ |
| 31 |
|
31 |
|
| 32 |
#include <sys/param.h> |
32 |
#include <sys/param.h> |
| 33 |
#include <sys/mac.h> |
33 |
#include <sys/mac.h> |
| 34 |
|
34 |
|
| 35 |
#ifdef USE_BSM_AUDIT |
35 |
#ifdef USE_BSM_AUDIT |
| 36 |
#include <bsm/audit.h> |
36 |
#include <bsm/audit.h> |
| 37 |
#endif |
37 |
#endif |
| 38 |
|
38 |
|
| 39 |
#include <err.h> |
39 |
#include <err.h> |
| 40 |
#include <errno.h> |
40 |
#include <errno.h> |
| 41 |
#include <grp.h> |
41 |
#include <grp.h> |
| 42 |
#include <pwd.h> |
42 |
#include <pwd.h> |
| 43 |
#include <stdbool.h> |
43 |
#include <stdbool.h> |
| 44 |
#include <stdint.h> |
44 |
#include <stdint.h> |
| 45 |
#include <stdio.h> |
45 |
#include <stdio.h> |
| 46 |
#include <stdlib.h> |
46 |
#include <stdlib.h> |
| 47 |
#include <string.h> |
47 |
#include <string.h> |
| 48 |
#include <unistd.h> |
48 |
#include <unistd.h> |
| 49 |
|
49 |
|
| 50 |
static void id_print(struct passwd *); |
50 |
static void id_print(struct passwd *); |
| 51 |
static void pline(struct passwd *); |
51 |
static void pline(struct passwd *); |
| 52 |
static void pretty(struct passwd *); |
52 |
static void pretty(struct passwd *); |
| 53 |
#ifdef USE_BSM_AUDIT |
53 |
#ifdef USE_BSM_AUDIT |
| 54 |
static void auditid(void); |
54 |
static void auditid(void); |
| 55 |
#endif |
55 |
#endif |
| 56 |
static void group(struct passwd *, bool); |
56 |
static void group(struct passwd *, bool); |
| 57 |
static void maclabel(void); |
57 |
static void maclabel(void); |
| 58 |
static void dir(struct passwd *); |
58 |
static void dir(struct passwd *); |
| 59 |
static void shell(struct passwd *); |
59 |
static void shell(struct passwd *); |
| 60 |
static void usage(void); |
60 |
static void usage(void); |
| 61 |
static struct passwd *who(char *); |
61 |
static struct passwd *who(char *); |
| 62 |
|
62 |
|
| 63 |
static bool isgroups, iswhoami; |
63 |
static bool isgroups, iswhoami; |
| 64 |
|
64 |
|
| 65 |
int |
65 |
int |
| 66 |
main(int argc, char *argv[]) |
66 |
main(int argc, char *argv[]) |
| 67 |
{ |
67 |
{ |
| 68 |
struct group *gr; |
68 |
struct group *gr; |
| 69 |
struct passwd *pw; |
69 |
struct passwd *pw; |
| 70 |
#ifdef USE_BSM_AUDIT |
70 |
#ifdef USE_BSM_AUDIT |
| 71 |
bool Aflag; |
71 |
bool Aflag; |
| 72 |
#endif |
72 |
#endif |
| 73 |
bool Gflag, Mflag, Pflag; |
73 |
bool Gflag, Mflag, Pflag; |
| 74 |
bool cflag, dflag, gflag, nflag, pflag, rflag, sflag, uflag; |
74 |
bool cflag, dflag, gflag, nflag, pflag, rflag, sflag, uflag; |
| 75 |
int ch, combo, error, id; |
75 |
int ch, combo, error, id; |
| 76 |
const char *myname, *optstr; |
76 |
const char *myname, *optstr; |
| 77 |
char loginclass[MAXLOGNAME]; |
77 |
char loginclass[MAXLOGNAME]; |
| 78 |
|
78 |
|
| 79 |
#ifdef USE_BSM_AUDIT |
79 |
#ifdef USE_BSM_AUDIT |
| 80 |
Aflag = false; |
80 |
Aflag = false; |
| 81 |
#endif |
81 |
#endif |
| 82 |
Gflag = Mflag = Pflag = false; |
82 |
Gflag = Mflag = Pflag = false; |
| 83 |
cflag = dflag = gflag = nflag = pflag = rflag = sflag = uflag = false; |
83 |
cflag = dflag = gflag = nflag = pflag = rflag = sflag = uflag = false; |
| 84 |
|
84 |
|
| 85 |
myname = getprogname(); |
85 |
myname = getprogname(); |
| 86 |
optstr = "AGMPacdgnprsu"; |
86 |
optstr = "AGMPacdgnprsu"; |
| 87 |
if (strcmp(myname, "groups") == 0) { |
87 |
if (strcmp(myname, "groups") == 0) { |
| 88 |
isgroups = true; |
88 |
isgroups = true; |
| 89 |
optstr = ""; |
89 |
optstr = ""; |
| 90 |
Gflag = nflag = true; |
90 |
Gflag = nflag = true; |
| 91 |
} |
91 |
} |
| 92 |
else if (strcmp(myname, "whoami") == 0) { |
92 |
else if (strcmp(myname, "whoami") == 0) { |
| 93 |
iswhoami = true; |
93 |
iswhoami = true; |
| 94 |
optstr = ""; |
94 |
optstr = ""; |
| 95 |
uflag = nflag = true; |
95 |
uflag = nflag = true; |
| 96 |
} |
96 |
} |
| 97 |
|
97 |
|
| 98 |
while ((ch = getopt(argc, argv, optstr)) != -1) { |
98 |
while ((ch = getopt(argc, argv, optstr)) != -1) { |
| 99 |
switch(ch) { |
99 |
switch(ch) { |
| 100 |
#ifdef USE_BSM_AUDIT |
100 |
#ifdef USE_BSM_AUDIT |
| 101 |
case 'A': |
101 |
case 'A': |
| 102 |
Aflag = true; |
102 |
Aflag = true; |
| 103 |
break; |
103 |
break; |
| 104 |
#endif |
104 |
#endif |
| 105 |
case 'G': |
105 |
case 'G': |
| 106 |
Gflag = true; |
106 |
Gflag = true; |
| 107 |
break; |
107 |
break; |
| 108 |
case 'M': |
108 |
case 'M': |
| 109 |
Mflag = true; |
109 |
Mflag = true; |
| 110 |
break; |
110 |
break; |
| 111 |
case 'P': |
111 |
case 'P': |
| 112 |
Pflag = true; |
112 |
Pflag = true; |
| 113 |
break; |
113 |
break; |
| 114 |
case 'a': |
114 |
case 'a': |
| 115 |
break; |
115 |
break; |
| 116 |
case 'c': |
116 |
case 'c': |
| 117 |
cflag = true; |
117 |
cflag = true; |
| 118 |
break; |
118 |
break; |
| 119 |
case 'd': |
119 |
case 'd': |
| 120 |
dflag = true; |
120 |
dflag = true; |
| 121 |
break; |
121 |
break; |
| 122 |
case 'g': |
122 |
case 'g': |
| 123 |
gflag = true; |
123 |
gflag = true; |
| 124 |
break; |
124 |
break; |
| 125 |
case 'n': |
125 |
case 'n': |
| 126 |
nflag = true; |
126 |
nflag = true; |
| 127 |
break; |
127 |
break; |
| 128 |
case 'p': |
128 |
case 'p': |
| 129 |
pflag = true; |
129 |
pflag = true; |
| 130 |
break; |
130 |
break; |
| 131 |
case 'r': |
131 |
case 'r': |
| 132 |
rflag = true; |
132 |
rflag = true; |
| 133 |
break; |
133 |
break; |
| 134 |
case 's': |
134 |
case 's': |
| 135 |
sflag = true; |
135 |
sflag = true; |
| 136 |
break; |
136 |
break; |
| 137 |
case 'u': |
137 |
case 'u': |
| 138 |
uflag = true; |
138 |
uflag = true; |
| 139 |
break; |
139 |
break; |
| 140 |
default: |
140 |
default: |
| 141 |
usage(); |
141 |
usage(); |
| 142 |
} |
142 |
} |
| 143 |
} |
143 |
} |
| 144 |
argc -= optind; |
144 |
argc -= optind; |
| 145 |
argv += optind; |
145 |
argv += optind; |
| 146 |
|
146 |
|
| 147 |
if (iswhoami && argc > 0) |
147 |
if (iswhoami && argc > 0) |
| 148 |
usage(); |
148 |
usage(); |
| 149 |
if ((cflag || Aflag || Mflag) && argc > 0) |
149 |
if ((cflag || |
|
|
150 |
#ifdef USE_BSM_AUDIT |
| 151 |
Aflag || |
| 152 |
#endif |
| 153 |
Mflag) && argc > 0) |
| 150 |
usage(); |
154 |
usage(); |
| 151 |
|
155 |
|
| 152 |
combo = Aflag + Gflag + Mflag + Pflag + gflag + pflag + uflag; |
156 |
combo = |
|
|
157 |
#ifdef USE_BSM_AUDIT |
| 158 |
Aflag + |
| 159 |
#endif |
| 160 |
Gflag + Mflag + Pflag + gflag + pflag + uflag; |
| 153 |
if (combo + dflag + sflag > 1) |
161 |
if (combo + dflag + sflag > 1) |
| 154 |
usage(); |
162 |
usage(); |
| 155 |
if (combo > 1) |
163 |
if (combo > 1) |
| 156 |
usage(); |
164 |
usage(); |
| 157 |
if (combo == 0 && (nflag || rflag)) |
165 |
if (combo == 0 && (nflag || rflag)) |
| 158 |
usage(); |
166 |
usage(); |
| 159 |
|
167 |
|
| 160 |
pw = *argv ? who(*argv) : NULL; |
168 |
pw = *argv ? who(*argv) : NULL; |
| 161 |
|
169 |
|
| 162 |
if (Mflag && pw != NULL) |
170 |
if (Mflag && pw != NULL) |
| 163 |
usage(); |
171 |
usage(); |
| 164 |
|
172 |
|
| 165 |
#ifdef USE_BSM_AUDIT |
173 |
#ifdef USE_BSM_AUDIT |
| 166 |
if (Aflag) { |
174 |
if (Aflag) { |
| 167 |
auditid(); |
175 |
auditid(); |
| 168 |
exit(0); |
176 |
exit(0); |
| 169 |
} |
177 |
} |
| 170 |
#endif |
178 |
#endif |
| 171 |
|
179 |
|
| 172 |
if (cflag) { |
180 |
if (cflag) { |
| 173 |
error = getloginclass(loginclass, sizeof(loginclass)); |
181 |
error = getloginclass(loginclass, sizeof(loginclass)); |
| 174 |
if (error != 0) |
182 |
if (error != 0) |
| 175 |
err(1, "loginclass"); |
183 |
err(1, "loginclass"); |
| 176 |
(void)printf("%s\n", loginclass); |
184 |
(void)printf("%s\n", loginclass); |
| 177 |
exit(0); |
185 |
exit(0); |
| 178 |
} |
186 |
} |
| 179 |
|
187 |
|
| 180 |
if (gflag) { |
188 |
if (gflag) { |
| 181 |
id = pw ? pw->pw_gid : rflag ? getgid() : getegid(); |
189 |
id = pw ? pw->pw_gid : rflag ? getgid() : getegid(); |
| 182 |
if (nflag && (gr = getgrgid(id))) |
190 |
if (nflag && (gr = getgrgid(id))) |
| 183 |
(void)printf("%s\n", gr->gr_name); |
191 |
(void)printf("%s\n", gr->gr_name); |
| 184 |
else |
192 |
else |
| 185 |
(void)printf("%u\n", id); |
193 |
(void)printf("%u\n", id); |
| 186 |
exit(0); |
194 |
exit(0); |
| 187 |
} |
195 |
} |
| 188 |
|
196 |
|
| 189 |
if (uflag) { |
197 |
if (uflag) { |
| 190 |
id = pw ? pw->pw_uid : rflag ? getuid() : geteuid(); |
198 |
id = pw ? pw->pw_uid : rflag ? getuid() : geteuid(); |
| 191 |
if (nflag && (pw = getpwuid(id))) |
199 |
if (nflag && (pw = getpwuid(id))) |
| 192 |
(void)printf("%s\n", pw->pw_name); |
200 |
(void)printf("%s\n", pw->pw_name); |
| 193 |
else |
201 |
else |
| 194 |
(void)printf("%u\n", id); |
202 |
(void)printf("%u\n", id); |
| 195 |
exit(0); |
203 |
exit(0); |
| 196 |
} |
204 |
} |
| 197 |
|
205 |
|
| 198 |
if (dflag) { |
206 |
if (dflag) { |
| 199 |
dir(pw); |
207 |
dir(pw); |
| 200 |
exit(0); |
208 |
exit(0); |
| 201 |
} |
209 |
} |
| 202 |
|
210 |
|
| 203 |
if (Gflag) { |
211 |
if (Gflag) { |
| 204 |
group(pw, nflag); |
212 |
group(pw, nflag); |
| 205 |
exit(0); |
213 |
exit(0); |
| 206 |
} |
214 |
} |
| 207 |
|
215 |
|
| 208 |
if (Mflag) { |
216 |
if (Mflag) { |
| 209 |
maclabel(); |
217 |
maclabel(); |
| 210 |
exit(0); |
218 |
exit(0); |
| 211 |
} |
219 |
} |
| 212 |
|
220 |
|
| 213 |
if (Pflag) { |
221 |
if (Pflag) { |
| 214 |
pline(pw); |
222 |
pline(pw); |
| 215 |
exit(0); |
223 |
exit(0); |
| 216 |
} |
224 |
} |
| 217 |
|
225 |
|
| 218 |
if (pflag) { |
226 |
if (pflag) { |
| 219 |
pretty(pw); |
227 |
pretty(pw); |
| 220 |
exit(0); |
228 |
exit(0); |
| 221 |
} |
229 |
} |
| 222 |
|
230 |
|
| 223 |
if (sflag) { |
231 |
if (sflag) { |
| 224 |
shell(pw); |
232 |
shell(pw); |
| 225 |
exit(0); |
233 |
exit(0); |
| 226 |
} |
234 |
} |
| 227 |
|
235 |
|
| 228 |
id_print(pw); |
236 |
id_print(pw); |
| 229 |
exit(0); |
237 |
exit(0); |
| 230 |
} |
238 |
} |
| 231 |
|
239 |
|
| 232 |
static void |
240 |
static void |
| 233 |
pretty(struct passwd *pw) |
241 |
pretty(struct passwd *pw) |
| 234 |
{ |
242 |
{ |
| 235 |
struct group *gr; |
243 |
struct group *gr; |
| 236 |
u_int eid, rid; |
244 |
u_int eid, rid; |
| 237 |
char *login; |
245 |
char *login; |
| 238 |
|
246 |
|
| 239 |
if (pw) { |
247 |
if (pw) { |
| 240 |
(void)printf("uid\t%s\n", pw->pw_name); |
248 |
(void)printf("uid\t%s\n", pw->pw_name); |
| 241 |
(void)printf("groups\t"); |
249 |
(void)printf("groups\t"); |
| 242 |
group(pw, true); |
250 |
group(pw, true); |
| 243 |
} else { |
251 |
} else { |
| 244 |
if ((login = getlogin()) == NULL) |
252 |
if ((login = getlogin()) == NULL) |
| 245 |
err(1, "getlogin"); |
253 |
err(1, "getlogin"); |
| 246 |
|
254 |
|
| 247 |
pw = getpwuid(rid = getuid()); |
255 |
pw = getpwuid(rid = getuid()); |
| 248 |
if (pw == NULL || strcmp(login, pw->pw_name)) |
256 |
if (pw == NULL || strcmp(login, pw->pw_name)) |
| 249 |
(void)printf("login\t%s\n", login); |
257 |
(void)printf("login\t%s\n", login); |
| 250 |
if (pw) |
258 |
if (pw) |
| 251 |
(void)printf("uid\t%s\n", pw->pw_name); |
259 |
(void)printf("uid\t%s\n", pw->pw_name); |
| 252 |
else |
260 |
else |
| 253 |
(void)printf("uid\t%u\n", rid); |
261 |
(void)printf("uid\t%u\n", rid); |
| 254 |
|
262 |
|
| 255 |
if ((eid = geteuid()) != rid) { |
263 |
if ((eid = geteuid()) != rid) { |
| 256 |
if ((pw = getpwuid(eid))) |
264 |
if ((pw = getpwuid(eid))) |
| 257 |
(void)printf("euid\t%s\n", pw->pw_name); |
265 |
(void)printf("euid\t%s\n", pw->pw_name); |
| 258 |
else |
266 |
else |
| 259 |
(void)printf("euid\t%u\n", eid); |
267 |
(void)printf("euid\t%u\n", eid); |
| 260 |
} |
268 |
} |
| 261 |
if ((rid = getgid()) != (eid = getegid())) { |
269 |
if ((rid = getgid()) != (eid = getegid())) { |
| 262 |
if ((gr = getgrgid(rid))) |
270 |
if ((gr = getgrgid(rid))) |
| 263 |
(void)printf("rgid\t%s\n", gr->gr_name); |
271 |
(void)printf("rgid\t%s\n", gr->gr_name); |
| 264 |
else |
272 |
else |
| 265 |
(void)printf("rgid\t%u\n", rid); |
273 |
(void)printf("rgid\t%u\n", rid); |
| 266 |
} |
274 |
} |
| 267 |
(void)printf("groups\t"); |
275 |
(void)printf("groups\t"); |
| 268 |
group(NULL, true); |
276 |
group(NULL, true); |
| 269 |
} |
277 |
} |
| 270 |
} |
278 |
} |
| 271 |
|
279 |
|
| 272 |
static void |
280 |
static void |
| 273 |
id_print(struct passwd *pw) |
281 |
id_print(struct passwd *pw) |
| 274 |
{ |
282 |
{ |
| 275 |
struct group *gr; |
283 |
struct group *gr; |
| 276 |
gid_t gid, egid, lastgid; |
284 |
gid_t gid, egid, lastgid; |
| 277 |
uid_t uid, euid; |
285 |
uid_t uid, euid; |
| 278 |
int cnt, ngroups; |
286 |
int cnt, ngroups; |
| 279 |
long ngroups_max; |
287 |
long ngroups_max; |
| 280 |
gid_t *groups; |
288 |
gid_t *groups; |
| 281 |
const char *fmt; |
289 |
const char *fmt; |
| 282 |
bool print_dbinfo; |
290 |
bool print_dbinfo; |
| 283 |
|
291 |
|
| 284 |
print_dbinfo = pw != NULL; |
292 |
print_dbinfo = pw != NULL; |
| 285 |
if (print_dbinfo) { |
293 |
if (print_dbinfo) { |
| 286 |
uid = pw->pw_uid; |
294 |
uid = pw->pw_uid; |
| 287 |
gid = pw->pw_gid; |
295 |
gid = pw->pw_gid; |
| 288 |
} |
296 |
} |
| 289 |
else { |
297 |
else { |
| 290 |
uid = getuid(); |
298 |
uid = getuid(); |
| 291 |
gid = getgid(); |
299 |
gid = getgid(); |
| 292 |
pw = getpwuid(uid); |
300 |
pw = getpwuid(uid); |
| 293 |
} |
301 |
} |
| 294 |
|
302 |
|
| 295 |
ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1; |
303 |
ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1; |
| 296 |
if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL) |
304 |
if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL) |
| 297 |
err(1, "malloc"); |
305 |
err(1, "malloc"); |
| 298 |
|
306 |
|
| 299 |
if (print_dbinfo) { |
307 |
if (print_dbinfo) { |
| 300 |
ngroups = ngroups_max; |
308 |
ngroups = ngroups_max; |
| 301 |
getgrouplist(pw->pw_name, gid, groups, &ngroups); |
309 |
getgrouplist(pw->pw_name, gid, groups, &ngroups); |
| 302 |
} |
310 |
} |
| 303 |
else { |
311 |
else { |
| 304 |
ngroups = getgroups(ngroups_max, groups); |
312 |
ngroups = getgroups(ngroups_max, groups); |
| 305 |
} |
313 |
} |
| 306 |
|
314 |
|
| 307 |
/* |
315 |
/* |
| 308 |
* We always resolve uids and gids where we can to a name, even if we |
316 |
* We always resolve uids and gids where we can to a name, even if we |
| 309 |
* are printing the running process credentials, to be nice. |
317 |
* are printing the running process credentials, to be nice. |
| 310 |
*/ |
318 |
*/ |
| 311 |
if (pw != NULL) |
319 |
if (pw != NULL) |
| 312 |
printf("uid=%u(%s)", uid, pw->pw_name); |
320 |
printf("uid=%u(%s)", uid, pw->pw_name); |
| 313 |
else |
321 |
else |
| 314 |
printf("uid=%u", uid); |
322 |
printf("uid=%u", uid); |
| 315 |
printf(" gid=%u", gid); |
323 |
printf(" gid=%u", gid); |
| 316 |
if ((gr = getgrgid(gid))) |
324 |
if ((gr = getgrgid(gid))) |
| 317 |
(void)printf("(%s)", gr->gr_name); |
325 |
(void)printf("(%s)", gr->gr_name); |
| 318 |
if (!print_dbinfo && (euid = geteuid()) != uid) { |
326 |
if (!print_dbinfo && (euid = geteuid()) != uid) { |
| 319 |
(void)printf(" euid=%u", euid); |
327 |
(void)printf(" euid=%u", euid); |
| 320 |
if ((pw = getpwuid(euid))) |
328 |
if ((pw = getpwuid(euid))) |
| 321 |
(void)printf("(%s)", pw->pw_name); |
329 |
(void)printf("(%s)", pw->pw_name); |
| 322 |
} |
330 |
} |
| 323 |
if (!print_dbinfo && (egid = getegid()) != gid) { |
331 |
if (!print_dbinfo && (egid = getegid()) != gid) { |
| 324 |
(void)printf(" egid=%u", egid); |
332 |
(void)printf(" egid=%u", egid); |
| 325 |
if ((gr = getgrgid(egid))) |
333 |
if ((gr = getgrgid(egid))) |
| 326 |
(void)printf("(%s)", gr->gr_name); |
334 |
(void)printf("(%s)", gr->gr_name); |
| 327 |
} |
335 |
} |
| 328 |
fmt = " groups=%u"; |
336 |
fmt = " groups=%u"; |
| 329 |
for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) { |
337 |
for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) { |
| 330 |
if (lastgid == (gid = groups[cnt])) |
338 |
if (lastgid == (gid = groups[cnt])) |
| 331 |
continue; |
339 |
continue; |
| 332 |
printf(fmt, gid); |
340 |
printf(fmt, gid); |
| 333 |
fmt = ",%u"; |
341 |
fmt = ",%u"; |
| 334 |
if ((gr = getgrgid(gid))) |
342 |
if ((gr = getgrgid(gid))) |
| 335 |
printf("(%s)", gr->gr_name); |
343 |
printf("(%s)", gr->gr_name); |
| 336 |
lastgid = gid; |
344 |
lastgid = gid; |
| 337 |
} |
345 |
} |
| 338 |
printf("\n"); |
346 |
printf("\n"); |
| 339 |
free(groups); |
347 |
free(groups); |
| 340 |
} |
348 |
} |
| 341 |
|
349 |
|
| 342 |
#ifdef USE_BSM_AUDIT |
350 |
#ifdef USE_BSM_AUDIT |
| 343 |
static void |
351 |
static void |
| 344 |
auditid(void) |
352 |
auditid(void) |
| 345 |
{ |
353 |
{ |
| 346 |
auditinfo_t auditinfo; |
354 |
auditinfo_t auditinfo; |
| 347 |
auditinfo_addr_t ainfo_addr; |
355 |
auditinfo_addr_t ainfo_addr; |
| 348 |
int ret, extended; |
356 |
int ret, extended; |
| 349 |
|
357 |
|
| 350 |
extended = 0; |
358 |
extended = 0; |
| 351 |
ret = getaudit(&auditinfo); |
359 |
ret = getaudit(&auditinfo); |
| 352 |
if (ret < 0 && errno == E2BIG) { |
360 |
if (ret < 0 && errno == E2BIG) { |
| 353 |
if (getaudit_addr(&ainfo_addr, sizeof(ainfo_addr)) < 0) |
361 |
if (getaudit_addr(&ainfo_addr, sizeof(ainfo_addr)) < 0) |
| 354 |
err(1, "getaudit_addr"); |
362 |
err(1, "getaudit_addr"); |
| 355 |
extended = 1; |
363 |
extended = 1; |
| 356 |
} else if (ret < 0) |
364 |
} else if (ret < 0) |
| 357 |
err(1, "getaudit"); |
365 |
err(1, "getaudit"); |
| 358 |
if (extended != 0) { |
366 |
if (extended != 0) { |
| 359 |
(void) printf("auid=%d\n" |
367 |
(void) printf("auid=%d\n" |
| 360 |
"mask.success=0x%08x\n" |
368 |
"mask.success=0x%08x\n" |
| 361 |
"mask.failure=0x%08x\n" |
369 |
"mask.failure=0x%08x\n" |
| 362 |
"asid=%d\n" |
370 |
"asid=%d\n" |
| 363 |
"termid_addr.port=0x%08jx\n" |
371 |
"termid_addr.port=0x%08jx\n" |
| 364 |
"termid_addr.addr[0]=0x%08x\n" |
372 |
"termid_addr.addr[0]=0x%08x\n" |
| 365 |
"termid_addr.addr[1]=0x%08x\n" |
373 |
"termid_addr.addr[1]=0x%08x\n" |
| 366 |
"termid_addr.addr[2]=0x%08x\n" |
374 |
"termid_addr.addr[2]=0x%08x\n" |
| 367 |
"termid_addr.addr[3]=0x%08x\n", |
375 |
"termid_addr.addr[3]=0x%08x\n", |
| 368 |
ainfo_addr.ai_auid, ainfo_addr.ai_mask.am_success, |
376 |
ainfo_addr.ai_auid, ainfo_addr.ai_mask.am_success, |
| 369 |
ainfo_addr.ai_mask.am_failure, ainfo_addr.ai_asid, |
377 |
ainfo_addr.ai_mask.am_failure, ainfo_addr.ai_asid, |
| 370 |
(uintmax_t)ainfo_addr.ai_termid.at_port, |
378 |
(uintmax_t)ainfo_addr.ai_termid.at_port, |
| 371 |
ainfo_addr.ai_termid.at_addr[0], |
379 |
ainfo_addr.ai_termid.at_addr[0], |
| 372 |
ainfo_addr.ai_termid.at_addr[1], |
380 |
ainfo_addr.ai_termid.at_addr[1], |
| 373 |
ainfo_addr.ai_termid.at_addr[2], |
381 |
ainfo_addr.ai_termid.at_addr[2], |
| 374 |
ainfo_addr.ai_termid.at_addr[3]); |
382 |
ainfo_addr.ai_termid.at_addr[3]); |
| 375 |
} else { |
383 |
} else { |
| 376 |
(void) printf("auid=%d\n" |
384 |
(void) printf("auid=%d\n" |
| 377 |
"mask.success=0x%08x\n" |
385 |
"mask.success=0x%08x\n" |
| 378 |
"mask.failure=0x%08x\n" |
386 |
"mask.failure=0x%08x\n" |
| 379 |
"asid=%d\n" |
387 |
"asid=%d\n" |
| 380 |
"termid.port=0x%08jx\n" |
388 |
"termid.port=0x%08jx\n" |
| 381 |
"termid.machine=0x%08x\n", |
389 |
"termid.machine=0x%08x\n", |
| 382 |
auditinfo.ai_auid, auditinfo.ai_mask.am_success, |
390 |
auditinfo.ai_auid, auditinfo.ai_mask.am_success, |
| 383 |
auditinfo.ai_mask.am_failure, |
391 |
auditinfo.ai_mask.am_failure, |
| 384 |
auditinfo.ai_asid, (uintmax_t)auditinfo.ai_termid.port, |
392 |
auditinfo.ai_asid, (uintmax_t)auditinfo.ai_termid.port, |
| 385 |
auditinfo.ai_termid.machine); |
393 |
auditinfo.ai_termid.machine); |
| 386 |
} |
394 |
} |
| 387 |
} |
395 |
} |
| 388 |
#endif |
396 |
#endif |
| 389 |
|
397 |
|
| 390 |
static void |
398 |
static void |
| 391 |
group(struct passwd *pw, bool nflag) |
399 |
group(struct passwd *pw, bool nflag) |
| 392 |
{ |
400 |
{ |
| 393 |
struct group *gr; |
401 |
struct group *gr; |
| 394 |
int cnt, id, lastid, ngroups; |
402 |
int cnt, id, lastid, ngroups; |
| 395 |
long ngroups_max; |
403 |
long ngroups_max; |
| 396 |
gid_t *groups; |
404 |
gid_t *groups; |
| 397 |
const char *fmt; |
405 |
const char *fmt; |
| 398 |
|
406 |
|
| 399 |
ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1; |
407 |
ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1; |
| 400 |
if ((groups = malloc(sizeof(gid_t) * (ngroups_max))) == NULL) |
408 |
if ((groups = malloc(sizeof(gid_t) * (ngroups_max))) == NULL) |
| 401 |
err(1, "malloc"); |
409 |
err(1, "malloc"); |
| 402 |
|
410 |
|
| 403 |
if (pw) { |
411 |
if (pw) { |
| 404 |
ngroups = ngroups_max; |
412 |
ngroups = ngroups_max; |
| 405 |
(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); |
413 |
(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); |
| 406 |
} else { |
414 |
} else { |
| 407 |
ngroups = getgroups(ngroups_max, groups); |
415 |
ngroups = getgroups(ngroups_max, groups); |
| 408 |
} |
416 |
} |
| 409 |
fmt = nflag ? "%s" : "%u"; |
417 |
fmt = nflag ? "%s" : "%u"; |
| 410 |
for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) { |
418 |
for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) { |
| 411 |
if (lastid == (id = groups[cnt])) |
419 |
if (lastid == (id = groups[cnt])) |
| 412 |
continue; |
420 |
continue; |
| 413 |
if (nflag) { |
421 |
if (nflag) { |
| 414 |
if ((gr = getgrgid(id))) |
422 |
if ((gr = getgrgid(id))) |
| 415 |
(void)printf(fmt, gr->gr_name); |
423 |
(void)printf(fmt, gr->gr_name); |
| 416 |
else |
424 |
else |
| 417 |
(void)printf(*fmt == ' ' ? " %u" : "%u", |
425 |
(void)printf(*fmt == ' ' ? " %u" : "%u", |
| 418 |
id); |
426 |
id); |
| 419 |
fmt = " %s"; |
427 |
fmt = " %s"; |
| 420 |
} else { |
428 |
} else { |
| 421 |
(void)printf(fmt, id); |
429 |
(void)printf(fmt, id); |
| 422 |
fmt = " %u"; |
430 |
fmt = " %u"; |
| 423 |
} |
431 |
} |
| 424 |
lastid = id; |
432 |
lastid = id; |
| 425 |
} |
433 |
} |
| 426 |
(void)printf("\n"); |
434 |
(void)printf("\n"); |
| 427 |
free(groups); |
435 |
free(groups); |
| 428 |
} |
436 |
} |
| 429 |
|
437 |
|
| 430 |
static void |
438 |
static void |
| 431 |
maclabel(void) |
439 |
maclabel(void) |
| 432 |
{ |
440 |
{ |
| 433 |
char *string; |
441 |
char *string; |
| 434 |
mac_t label; |
442 |
mac_t label; |
| 435 |
int error; |
443 |
int error; |
| 436 |
|
444 |
|
| 437 |
error = mac_prepare_process_label(&label); |
445 |
error = mac_prepare_process_label(&label); |
| 438 |
if (error == -1) |
446 |
if (error == -1) |
| 439 |
errx(1, "mac_prepare_type: %s", strerror(errno)); |
447 |
errx(1, "mac_prepare_type: %s", strerror(errno)); |
| 440 |
|
448 |
|
| 441 |
error = mac_get_proc(label); |
449 |
error = mac_get_proc(label); |
| 442 |
if (error == -1) |
450 |
if (error == -1) |
| 443 |
errx(1, "mac_get_proc: %s", strerror(errno)); |
451 |
errx(1, "mac_get_proc: %s", strerror(errno)); |
| 444 |
|
452 |
|
| 445 |
error = mac_to_text(label, &string); |
453 |
error = mac_to_text(label, &string); |
| 446 |
if (error == -1) |
454 |
if (error == -1) |
| 447 |
errx(1, "mac_to_text: %s", strerror(errno)); |
455 |
errx(1, "mac_to_text: %s", strerror(errno)); |
| 448 |
|
456 |
|
| 449 |
(void)printf("%s\n", string); |
457 |
(void)printf("%s\n", string); |
| 450 |
mac_free(label); |
458 |
mac_free(label); |
| 451 |
free(string); |
459 |
free(string); |
| 452 |
} |
460 |
} |
| 453 |
|
461 |
|
| 454 |
static struct passwd * |
462 |
static struct passwd * |
| 455 |
who(char *u) |
463 |
who(char *u) |
| 456 |
{ |
464 |
{ |
| 457 |
struct passwd *pw; |
465 |
struct passwd *pw; |
| 458 |
long id; |
466 |
long id; |
| 459 |
char *ep; |
467 |
char *ep; |
| 460 |
|
468 |
|
| 461 |
/* |
469 |
/* |
| 462 |
* Translate user argument into a pw pointer. First, try to |
470 |
* Translate user argument into a pw pointer. First, try to |
| 463 |
* get it as specified. If that fails, try it as a number. |
471 |
* get it as specified. If that fails, try it as a number. |
| 464 |
*/ |
472 |
*/ |
| 465 |
if ((pw = getpwnam(u))) |
473 |
if ((pw = getpwnam(u))) |
| 466 |
return(pw); |
474 |
return(pw); |
| 467 |
id = strtol(u, &ep, 10); |
475 |
id = strtol(u, &ep, 10); |
| 468 |
if (*u && !*ep && (pw = getpwuid(id))) |
476 |
if (*u && !*ep && (pw = getpwuid(id))) |
| 469 |
return(pw); |
477 |
return(pw); |
| 470 |
errx(1, "%s: no such user", u); |
478 |
errx(1, "%s: no such user", u); |
| 471 |
/* NOTREACHED */ |
479 |
/* NOTREACHED */ |
| 472 |
} |
480 |
} |
| 473 |
|
481 |
|
| 474 |
static void |
482 |
static void |
| 475 |
pline(struct passwd *pw) |
483 |
pline(struct passwd *pw) |
| 476 |
{ |
484 |
{ |
| 477 |
if (pw == NULL) { |
485 |
if (pw == NULL) { |
| 478 |
if ((pw = getpwuid(getuid())) == NULL) |
486 |
if ((pw = getpwuid(getuid())) == NULL) |
| 479 |
err(1, "getpwuid"); |
487 |
err(1, "getpwuid"); |
| 480 |
} |
488 |
} |
| 481 |
(void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw->pw_name, |
489 |
(void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw->pw_name, |
| 482 |
pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class, |
490 |
pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class, |
| 483 |
(long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos, |
491 |
(long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos, |
| 484 |
pw->pw_dir, pw->pw_shell); |
492 |
pw->pw_dir, pw->pw_shell); |
| 485 |
} |
493 |
} |
| 486 |
|
494 |
|
| 487 |
static void |
495 |
static void |
| 488 |
dir(struct passwd *pw) |
496 |
dir(struct passwd *pw) |
| 489 |
{ |
497 |
{ |
| 490 |
if (pw == NULL) { |
498 |
if (pw == NULL) { |
| 491 |
if ((pw = getpwuid(getuid())) == NULL) |
499 |
if ((pw = getpwuid(getuid())) == NULL) |
| 492 |
err(1, "getpwuid"); |
500 |
err(1, "getpwuid"); |
| 493 |
} |
501 |
} |
| 494 |
printf("%s\n", pw->pw_dir); |
502 |
printf("%s\n", pw->pw_dir); |
| 495 |
} |
503 |
} |
| 496 |
|
504 |
|
| 497 |
static void |
505 |
static void |
| 498 |
shell(struct passwd *pw) |
506 |
shell(struct passwd *pw) |
| 499 |
{ |
507 |
{ |
| 500 |
if (pw == NULL) { |
508 |
if (pw == NULL) { |
| 501 |
if ((pw = getpwuid(getuid())) == NULL) |
509 |
if ((pw = getpwuid(getuid())) == NULL) |
| 502 |
err(1, "getpwuid"); |
510 |
err(1, "getpwuid"); |
| 503 |
} |
511 |
} |
| 504 |
printf("%s\n", pw->pw_shell); |
512 |
printf("%s\n", pw->pw_shell); |
| 505 |
} |
513 |
} |
| 506 |
|
514 |
|
| 507 |
static void |
515 |
static void |
| 508 |
usage(void) |
516 |
usage(void) |
| 509 |
{ |
517 |
{ |
| 510 |
if (isgroups) |
518 |
if (isgroups) |
| 511 |
(void)fprintf(stderr, "usage: groups [user]\n"); |
519 |
(void)fprintf(stderr, "usage: groups [user]\n"); |
| 512 |
else if (iswhoami) |
520 |
else if (iswhoami) |
| 513 |
(void)fprintf(stderr, "usage: whoami\n"); |
521 |
(void)fprintf(stderr, "usage: whoami\n"); |
| 514 |
else |
522 |
else |
| 515 |
(void)fprintf(stderr, |
523 |
(void)fprintf(stderr, |
| 516 |
"usage: id [user]\n" |
524 |
"usage: id [user]\n" |
| 517 |
#ifdef USE_BSM_AUDIT |
525 |
#ifdef USE_BSM_AUDIT |
| 518 |
" id -A\n" |
526 |
" id -A\n" |
| 519 |
#endif |
527 |
#endif |
| 520 |
" id -G [-n] [user]\n" |
528 |
" id -G [-n] [user]\n" |
| 521 |
" id -M\n" |
529 |
" id -M\n" |
| 522 |
" id -P [user]\n" |
530 |
" id -P [user]\n" |
| 523 |
" id -c\n" |
531 |
" id -c\n" |
| 524 |
" id -d [user]\n" |
532 |
" id -d [user]\n" |
| 525 |
" id -g [-nr] [user]\n" |
533 |
" id -g [-nr] [user]\n" |
| 526 |
" id -p [user]\n" |
534 |
" id -p [user]\n" |
| 527 |
" id -s [user]\n" |
535 |
" id -s [user]\n" |
| 528 |
" id -u [-nr] [user]\n"); |
536 |
" id -u [-nr] [user]\n"); |
| 529 |
exit(1); |
537 |
exit(1); |
| 530 |
} |
538 |
} |