id.c revision 162571
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 35static 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 42static char sccsid[] = "@(#)id.c 8.2 (Berkeley) 2/16/94"; 43#endif 44#endif /* not lint */ 45#include <sys/cdefs.h> 46__FBSDID("$FreeBSD: head/usr.bin/id/id.c 162571 2006-09-23 12:30:31Z rwatson $"); 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 64void id_print(struct passwd *, int, int, int); 65void pline(struct passwd *); 66void pretty(struct passwd *); 67void auditid(void); 68void group(struct passwd *, int); 69void maclabel(void); 70void usage(void); 71struct passwd *who(char *); 72 73int isgroups, iswhoami; 74 75int 76main(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) ? "" : "PGMagnpru")) != -1) 100 switch(ch) { 101 case 'G': 102 Gflag = 1; 103 break; 104 case 'M': 105 Mflag = 1; 106 break; 107 case 'P': 108 Pflag = 1; 109 break; 110 case 'a': 111 aflag = 1; 112 break; 113 case 'g': 114 gflag = 1; 115 break; 116 case 'n': 117 nflag = 1; 118 break; 119 case 'p': 120 pflag = 1; 121 break; 122 case 'r': 123 rflag = 1; 124 break; 125 case 'u': 126 uflag = 1; 127 break; 128 case '?': 129 default: 130 usage(); 131 } 132 argc -= optind; 133 argv += optind; 134 135 if (iswhoami && argc > 0) 136 usage(); 137 138 switch(Gflag + Mflag + Pflag + aflag + gflag + pflag + uflag) { 139 case 1: 140 break; 141 case 0: 142 if (!nflag && !rflag) 143 break; 144 /* FALLTHROUGH */ 145 default: 146 usage(); 147 } 148 149 pw = *argv ? who(*argv) : NULL; 150 151 if (Mflag && pw != NULL) 152 usage(); 153 154#ifdef USE_BSM_AUDIT 155 if (aflag) { 156 auditid(); 157 exit(0); 158 } 159#else 160 if (aflag) 161 usage(); 162#endif 163 164 if (gflag) { 165 id = pw ? pw->pw_gid : rflag ? getgid() : getegid(); 166 if (nflag && (gr = getgrgid(id))) 167 (void)printf("%s\n", gr->gr_name); 168 else 169 (void)printf("%u\n", id); 170 exit(0); 171 } 172 173 if (uflag) { 174 id = pw ? pw->pw_uid : rflag ? getuid() : geteuid(); 175 if (nflag && (pw = getpwuid(id))) 176 (void)printf("%s\n", pw->pw_name); 177 else 178 (void)printf("%u\n", id); 179 exit(0); 180 } 181 182 if (Gflag) { 183 group(pw, nflag); 184 exit(0); 185 } 186 187 if (Mflag) { 188 maclabel(); 189 exit(0); 190 } 191 192 if (Pflag) { 193 pline(pw); 194 exit(0); 195 } 196 197 if (pflag) { 198 pretty(pw); 199 exit(0); 200 } 201 202 if (pw) { 203 id_print(pw, 1, 0, 0); 204 } 205 else { 206 id = getuid(); 207 if ((pw = getpwuid(id)) != NULL) 208 id_print(pw, 0, 1, 1); 209 } 210 exit(0); 211} 212 213void 214pretty(struct passwd *pw) 215{ 216 struct group *gr; 217 u_int eid, rid; 218 char *login; 219 220 if (pw) { 221 (void)printf("uid\t%s\n", pw->pw_name); 222 (void)printf("groups\t"); 223 group(pw, 1); 224 } else { 225 if ((login = getlogin()) == NULL) 226 err(1, "getlogin"); 227 228 pw = getpwuid(rid = getuid()); 229 if (pw == NULL || strcmp(login, pw->pw_name)) 230 (void)printf("login\t%s\n", login); 231 if (pw) 232 (void)printf("uid\t%s\n", pw->pw_name); 233 else 234 (void)printf("uid\t%u\n", rid); 235 236 if ((eid = geteuid()) != rid) { 237 if ((pw = getpwuid(eid))) 238 (void)printf("euid\t%s\n", pw->pw_name); 239 else 240 (void)printf("euid\t%u\n", eid); 241 } 242 if ((rid = getgid()) != (eid = getegid())) { 243 if ((gr = getgrgid(rid))) 244 (void)printf("rgid\t%s\n", gr->gr_name); 245 else 246 (void)printf("rgid\t%u\n", rid); 247 } 248 (void)printf("groups\t"); 249 group(NULL, 1); 250 } 251} 252 253void 254id_print(struct passwd *pw, int use_ggl, int p_euid, int p_egid) 255{ 256 struct group *gr; 257 gid_t gid, egid, lastgid; 258 uid_t uid, euid; 259 int cnt, ngroups; 260 gid_t groups[NGROUPS + 1]; 261 const char *fmt; 262 263 uid = pw->pw_uid; 264 gid = pw->pw_gid; 265 266 if (use_ggl) { 267 ngroups = NGROUPS + 1; 268 getgrouplist(pw->pw_name, gid, groups, &ngroups); 269 } 270 else { 271 ngroups = getgroups(NGROUPS + 1, groups); 272 } 273 274 printf("uid=%u(%s)", uid, pw->pw_name); 275 printf(" gid=%u", gid); 276 if ((gr = getgrgid(gid))) 277 (void)printf("(%s)", gr->gr_name); 278 if (p_euid && (euid = geteuid()) != uid) { 279 (void)printf(" euid=%u", euid); 280 if ((pw = getpwuid(euid))) 281 (void)printf("(%s)", pw->pw_name); 282 } 283 if (p_egid && (egid = getegid()) != gid) { 284 (void)printf(" egid=%u", egid); 285 if ((gr = getgrgid(egid))) 286 (void)printf("(%s)", gr->gr_name); 287 } 288 fmt = " groups=%u"; 289 for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) { 290 if (lastgid == (gid = groups[cnt])) 291 continue; 292 printf(fmt, gid); 293 fmt = ", %u"; 294 if ((gr = getgrgid(gid))) 295 printf("(%s)", gr->gr_name); 296 lastgid = gid; 297 } 298 printf("\n"); 299} 300 301#ifdef USE_BSM_AUDIT 302void 303auditid(void) 304{ 305 auditinfo_t auditinfo; 306 307 if (getaudit(&auditinfo) < 0) 308 err(-1, "getauditinfo"); 309 printf("auid=%d\n", auditinfo.ai_auid); 310 printf("mask.success=0x%08x\n", auditinfo.ai_mask.am_success); 311 printf("mask.failure=0x%08x\n", auditinfo.ai_mask.am_failure); 312 printf("termid.port=0x%08x\n", auditinfo.ai_termid.port); 313 printf("asid=%d\n", auditinfo.ai_asid); 314} 315#endif 316 317void 318group(struct passwd *pw, int nflag) 319{ 320 struct group *gr; 321 int cnt, id, lastid, ngroups; 322 gid_t groups[NGROUPS + 1]; 323 const char *fmt; 324 325 if (pw) { 326 ngroups = NGROUPS + 1; 327 (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); 328 } else { 329 groups[0] = getgid(); 330 ngroups = getgroups(NGROUPS, groups + 1) + 1; 331 } 332 fmt = nflag ? "%s" : "%u"; 333 for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) { 334 if (lastid == (id = groups[cnt])) 335 continue; 336 if (nflag) { 337 if ((gr = getgrgid(id))) 338 (void)printf(fmt, gr->gr_name); 339 else 340 (void)printf(*fmt == ' ' ? " %u" : "%u", 341 id); 342 fmt = " %s"; 343 } else { 344 (void)printf(fmt, id); 345 fmt = " %u"; 346 } 347 lastid = id; 348 } 349 (void)printf("\n"); 350} 351 352void 353maclabel(void) 354{ 355 char *string; 356 mac_t label; 357 int error; 358 359 error = mac_prepare_process_label(&label); 360 if (error == -1) 361 errx(1, "mac_prepare_type: %s", strerror(errno)); 362 363 error = mac_get_proc(label); 364 if (error == -1) 365 errx(1, "mac_get_proc: %s", strerror(errno)); 366 367 error = mac_to_text(label, &string); 368 if (error == -1) 369 errx(1, "mac_to_text: %s", strerror(errno)); 370 371 (void)printf("%s\n", string); 372 mac_free(label); 373 free(string); 374} 375 376struct passwd * 377who(char *u) 378{ 379 struct passwd *pw; 380 long id; 381 char *ep; 382 383 /* 384 * Translate user argument into a pw pointer. First, try to 385 * get it as specified. If that fails, try it as a number. 386 */ 387 if ((pw = getpwnam(u))) 388 return(pw); 389 id = strtol(u, &ep, 10); 390 if (*u && !*ep && (pw = getpwuid(id))) 391 return(pw); 392 errx(1, "%s: no such user", u); 393 /* NOTREACHED */ 394} 395 396void 397pline(struct passwd *pw) 398{ 399 400 if (!pw) { 401 if ((pw = getpwuid(getuid())) == NULL) 402 err(1, "getpwuid"); 403 } 404 405 (void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw->pw_name, 406 pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class, 407 (long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos, 408 pw->pw_dir, pw->pw_shell); 409} 410 411 412void 413usage(void) 414{ 415 416 if (isgroups) 417 (void)fprintf(stderr, "usage: groups [user]\n"); 418 else if (iswhoami) 419 (void)fprintf(stderr, "usage: whoami\n"); 420 else 421 (void)fprintf(stderr, "%s\n%s\n%s\n%s%s\n%s\n%s\n%s\n", 422 "usage: id [user]", 423 " id -G [-n] [user]", 424 " id -M", 425 " id -P [user]", 426#ifdef USE_BSM_AUDIT 427 " id -a\n", 428#else 429 "", 430#endif 431 " id -g [-nr] [user]", 432 " id -p [user]", 433 " id -u [-nr] [user]"); 434 exit(1); 435} 436