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