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: #if !defined(lint) && defined(DOSCCS)
  35: static char copyright[] =
  36: "@(#) Copyright (c) 1991, 1993\n\
  37: 	The Regents of the University of California.  All rights reserved.\n";
  38: 
  39: static char sccsid[] = "@(#)id.c	8.3.1 (2.11BSD) 97/6/25";
  40: #endif
  41: 
  42: #include <sys/param.h>
  43: 
  44: /* #include <err.h> */
  45: #include <errno.h>
  46: #include <grp.h>
  47: #include <pwd.h>
  48: #include <stdio.h>
  49: #include <stdlib.h>
  50: #include <string.h>
  51: #include <unistd.h>
  52: 
  53: #define __P(protos) ()
  54: 
  55: void    current __P((void));
  56: void    pretty __P((struct passwd *));
  57: void    group __P((struct passwd *, int));
  58: void    usage __P((void));
  59: void    user __P((struct passwd *));
  60: struct passwd *
  61:     who __P((char *));
  62: 
  63: int
  64: main(argc, argv)
  65:     int argc;
  66:     char *argv[];
  67: {
  68:     struct group *gr;
  69:     struct passwd *pw;
  70:     int Gflag, ch, gflag, id, nflag, pflag, rflag, uflag;
  71: 
  72:     Gflag = gflag = nflag = pflag = rflag = uflag = 0;
  73:     while ((ch = getopt(argc, argv, "Ggnpru")) != EOF)
  74:         switch(ch) {
  75:         case 'G':
  76:             Gflag = 1;
  77:             break;
  78:         case 'g':
  79:             gflag = 1;
  80:             break;
  81:         case 'n':
  82:             nflag = 1;
  83:             break;
  84:         case 'p':
  85:             pflag = 1;
  86:             break;
  87:         case 'r':
  88:             rflag = 1;
  89:             break;
  90:         case 'u':
  91:             uflag = 1;
  92:             break;
  93:         case '?':
  94:         default:
  95:             usage();
  96:         }
  97:     argc -= optind;
  98:     argv += optind;
  99: 
 100:     switch(Gflag + gflag + pflag + uflag) {
 101:     case 1:
 102:         break;
 103:     case 0:
 104:         if (!nflag && !rflag)
 105:             break;
 106:         /* FALLTHROUGH */
 107:     default:
 108:         usage();
 109:     }
 110: 
 111:     pw = *argv ? who(*argv) : NULL;
 112: 
 113:     if (gflag) {
 114:         id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
 115:         if (nflag && (gr = getgrgid(id)))
 116:             (void)printf("%s\n", gr->gr_name);
 117:         else
 118:             (void)printf("%u\n", id);
 119:         exit(0);
 120:     }
 121: 
 122:     if (uflag) {
 123:         id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
 124:         if (nflag && (pw = getpwuid(id)))
 125:             (void)printf("%s\n", pw->pw_name);
 126:         else
 127:             (void)printf("%u\n", id);
 128:         exit(0);
 129:     }
 130: 
 131:     if (Gflag) {
 132:         group(pw, nflag);
 133:         exit(0);
 134:     }
 135: 
 136:     if (pflag) {
 137:         pretty(pw);
 138:         exit(0);
 139:     }
 140: 
 141:     if (pw)
 142:         user(pw);
 143:     else
 144:         current();
 145:     exit(0);
 146: }
 147: 
 148: void
 149: pretty(pw)
 150:     struct passwd *pw;
 151: {
 152:     struct group *gr;
 153:     u_int eid, rid;
 154:     char *login;
 155: 
 156:     if (pw) {
 157:         (void)printf("uid\t%s\n", pw->pw_name);
 158:         (void)printf("groups\t");
 159:         group(pw, 1);
 160:     } else {
 161:         if ((login = getlogin()) == NULL)
 162:             err(1, "getlogin");
 163: 
 164:         pw = getpwuid(rid = getuid());
 165:         if (pw == NULL || strcmp(login, pw->pw_name))
 166:             (void)printf("login\t%s\n", login);
 167:         if (pw)
 168:             (void)printf("uid\t%s\n", pw->pw_name);
 169:         else
 170:             (void)printf("uid\t%u\n", rid);
 171: 
 172:         if ((eid = geteuid()) != rid)
 173:             if (pw = getpwuid(eid))
 174:                 (void)printf("euid\t%s", pw->pw_name);
 175:             else
 176:                 (void)printf("euid\t%u", eid);
 177:         if ((rid = getgid()) != (eid = getegid()))
 178:             if (gr = getgrgid(rid))
 179:                 (void)printf("rgid\t%s\n", gr->gr_name);
 180:             else
 181:                 (void)printf("rgid\t%u\n", rid);
 182:         (void)printf("groups\t");
 183:         group(NULL, 1);
 184:     }
 185: }
 186: 
 187: void
 188: current()
 189: {
 190:     struct group *gr;
 191:     struct passwd *pw;
 192:     int cnt, id, eid, lastid, ngroups;
 193:     gid_t groups[NGROUPS];
 194:     char *fmt;
 195: 
 196:     id = getuid();
 197:     (void)printf("uid=%u", id);
 198:     if (pw = getpwuid(id))
 199:         (void)printf("(%s)", pw->pw_name);
 200:     if ((eid = geteuid()) != id) {
 201:         (void)printf(" euid=%u", eid);
 202:         if (pw = getpwuid(eid))
 203:             (void)printf("(%s)", pw->pw_name);
 204:     }
 205:     id = getgid();
 206:     (void)printf(" gid=%u", id);
 207:     if (gr = getgrgid(id))
 208:         (void)printf("(%s)", gr->gr_name);
 209:     if ((eid = getegid()) != id) {
 210:         (void)printf(" egid=%u", eid);
 211:         if (gr = getgrgid(eid))
 212:             (void)printf("(%s)", gr->gr_name);
 213:     }
 214:     ngroups = getgroups(NGROUPS, groups);
 215:     if (ngroups > 0)
 216:         for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
 217:             fmt = ", %u", lastid = id) {
 218:             id = groups[cnt++];
 219:             if (lastid == id)
 220:                 continue;
 221:             (void)printf(fmt, id);
 222:             if (gr = getgrgid(id))
 223:                 (void)printf("(%s)", gr->gr_name);
 224:         }
 225:     (void)printf("\n");
 226: }
 227: 
 228: void
 229: user(pw)
 230:     register struct passwd *pw;
 231: {
 232:     struct group *gr;
 233:     gid_t groups[NGROUPS + 1];
 234:     int cnt, id, lastid, ngroups;
 235:     char *fmt;
 236: 
 237:     id = pw->pw_uid;
 238:     (void)printf("uid=%u(%s)", id, pw->pw_name);
 239:     (void)printf(" gid=%u", pw->pw_gid);
 240:     if ((gr = getgrgid(pw->pw_gid)) != NULL)
 241:         (void)printf("(%s)", gr->gr_name);
 242:     ngroups = NGROUPS + 1;
 243:     (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
 244:     fmt = " groups=%u";
 245:     for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
 246:         if (lastid == (id = groups[cnt]))
 247:             continue;
 248:         (void)printf(fmt, id);
 249:         fmt = " %u";
 250:         if (gr = getgrgid(id))
 251:             (void)printf("(%s)", gr->gr_name);
 252:         lastid = id;
 253:     }
 254:     (void)printf("\n");
 255: }
 256: 
 257: void
 258: group(pw, nflag)
 259:     struct passwd *pw;
 260:     int nflag;
 261: {
 262:     struct group *gr;
 263:     int cnt, id, lastid, ngroups;
 264:     gid_t groups[NGROUPS + 1];
 265:     char *fmt;
 266: 
 267:     if (pw) {
 268:         ngroups = NGROUPS + 1;
 269:         (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
 270:     } else {
 271:         groups[0] = getgid();
 272:         ngroups = getgroups(NGROUPS, groups + 1) + 1;
 273:     }
 274:     fmt = nflag ? "%s" : "%u";
 275:     for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
 276:         if (lastid == (id = groups[cnt]))
 277:             continue;
 278:         if (nflag) {
 279:             if (gr = getgrgid(id))
 280:                 (void)printf(fmt, gr->gr_name);
 281:             else
 282:                 (void)printf(*fmt == ' ' ? " %u" : "%u",
 283:                     id);
 284:             fmt = " %s";
 285:         } else {
 286:             (void)printf(fmt, id);
 287:             fmt = " %u";
 288:         }
 289:         lastid = id;
 290:     }
 291:     (void)printf("\n");
 292: }
 293: 
 294: struct passwd *
 295: who(u)
 296:     char *u;
 297: {
 298:     struct passwd *pw;
 299:     long id;
 300:     char *ep;
 301: 
 302:     /*
 303: 	 * Translate user argument into a pw pointer.  First, try to
 304: 	 * get it as specified.  If that fails, try it as a number.
 305: 	 */
 306:     if (pw = getpwnam(u))
 307:         return(pw);
 308:     id = strtol(u, &ep, 10);
 309:     if (*u && !*ep && (pw = getpwuid(id)))
 310:         return(pw);
 311:     errx(1, "%s: No such user", u);
 312: }
 313: 
 314: void
 315: usage()
 316: {
 317:     (void)fprintf(stderr, "usage: id [user]\n");
 318:     (void)fprintf(stderr, "       id -G [-n] [user]\n");
 319:     (void)fprintf(stderr, "       id -g [-nr] [user]\n");
 320:     (void)fprintf(stderr, "       id -u [-nr] [user]\n");
 321:     exit(1);
 322: }

Defined functions

current defined in line 187; used 1 times
group defined in line 257; used 3 times
main defined in line 63; never used
pretty defined in line 148; used 1 times
usage defined in line 314; used 2 times
user defined in line 228; used 1 times
who defined in line 294; used 1 times

Defined variables

copyright defined in line 35; never used
sccsid defined in line 39; never used

Defined macros

__P defined in line 53; used 6 times
Last modified: 1997-06-25
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 2880
Valid CSS Valid XHTML 1.0 Strict