[BACK]Return to id.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / id

Annotation of src/usr.bin/id/id.c, Revision 1.1.1.1

1.1       deraadt     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 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: /*static char sccsid[] = "@(#)id.c     8.3 (Berkeley) 4/28/95";*/
                     42: static char rcsid[] = "$NetBSD: id.c,v 1.7 1995/09/28 08:05:42 perry Exp $";
                     43: #endif /* not lint */
                     44:
                     45: #include <sys/param.h>
                     46:
                     47: #include <errno.h>
                     48: #include <grp.h>
                     49: #include <pwd.h>
                     50: #include <stdio.h>
                     51: #include <stdlib.h>
                     52: #include <string.h>
                     53: #include <unistd.h>
                     54:
                     55: void   current __P((void));
                     56: void   err __P((const char *, ...));
                     57: void   pretty __P((struct passwd *));
                     58: void   group __P((struct passwd *, int));
                     59: void   usage __P((void));
                     60: void   user __P((struct passwd *));
                     61: struct passwd *
                     62:        who __P((char *));
                     63:
                     64: int
                     65: main(argc, argv)
                     66:        int argc;
                     67:        char *argv[];
                     68: {
                     69:        struct group *gr;
                     70:        struct passwd *pw;
                     71:        int Gflag, ch, gflag, id, nflag, pflag, rflag, uflag;
                     72:
                     73:        Gflag = gflag = nflag = pflag = rflag = uflag = 0;
                     74:        while ((ch = getopt(argc, argv, "Ggnpru")) != EOF)
                     75:                switch(ch) {
                     76:                case 'G':
                     77:                        Gflag = 1;
                     78:                        break;
                     79:                case 'g':
                     80:                        gflag = 1;
                     81:                        break;
                     82:                case 'n':
                     83:                        nflag = 1;
                     84:                        break;
                     85:                case 'p':
                     86:                        pflag = 1;
                     87:                        break;
                     88:                case 'r':
                     89:                        rflag = 1;
                     90:                        break;
                     91:                case 'u':
                     92:                        uflag = 1;
                     93:                        break;
                     94:                case '?':
                     95:                default:
                     96:                        usage();
                     97:                }
                     98:        argc -= optind;
                     99:        argv += optind;
                    100:
                    101:        switch(Gflag + gflag + pflag + uflag) {
                    102:        case 1:
                    103:                break;
                    104:        case 0:
                    105:                if (!nflag && !rflag)
                    106:                        break;
                    107:                /* FALLTHROUGH */
                    108:        default:
                    109:                usage();
                    110:        }
                    111:
                    112:        pw = *argv ? who(*argv) : NULL;
                    113:
                    114:        if (gflag) {
                    115:                id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
                    116:                if (nflag && (gr = getgrgid(id)))
                    117:                        (void)printf("%s\n", gr->gr_name);
                    118:                else
                    119:                        (void)printf("%u\n", id);
                    120:                exit(0);
                    121:        }
                    122:
                    123:        if (uflag) {
                    124:                id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
                    125:                if (nflag && (pw = getpwuid(id)))
                    126:                        (void)printf("%s\n", pw->pw_name);
                    127:                else
                    128:                        (void)printf("%u\n", id);
                    129:                exit(0);
                    130:        }
                    131:
                    132:        if (Gflag) {
                    133:                group(pw, nflag);
                    134:                exit(0);
                    135:        }
                    136:
                    137:        if (pflag) {
                    138:                pretty(pw);
                    139:                exit(0);
                    140:        }
                    141:
                    142:        if (pw)
                    143:                user(pw);
                    144:        else
                    145:                current();
                    146:        exit(0);
                    147: }
                    148:
                    149: void
                    150: pretty(pw)
                    151:        struct passwd *pw;
                    152: {
                    153:        struct group *gr;
                    154:        u_int eid, rid;
                    155:        char *login;
                    156:
                    157:        if (pw) {
                    158:                (void)printf("uid\t%s\n", pw->pw_name);
                    159:                (void)printf("groups\t");
                    160:                group(pw, 1);
                    161:        } else {
                    162:                if ((login = getlogin()) == NULL)
                    163:                        err("getlogin: %s", strerror(errno));
                    164:
                    165:                pw = getpwuid(rid = getuid());
                    166:                if (pw == NULL || strcmp(login, pw->pw_name))
                    167:                        (void)printf("login\t%s\n", login);
                    168:                if (pw)
                    169:                        (void)printf("uid\t%s\n", pw->pw_name);
                    170:                else
                    171:                        (void)printf("uid\t%u\n", rid);
                    172:
                    173:                if ((eid = geteuid()) != rid)
                    174:                        if (pw = getpwuid(eid))
                    175:                                (void)printf("euid\t%s", pw->pw_name);
                    176:                        else
                    177:                                (void)printf("euid\t%u", eid);
                    178:                if ((rid = getgid()) != (eid = getegid()))
                    179:                        if (gr = getgrgid(rid))
                    180:                                (void)printf("rgid\t%s\n", gr->gr_name);
                    181:                        else
                    182:                                (void)printf("rgid\t%u\n", rid);
                    183:                (void)printf("groups\t");
                    184:                group(NULL, 1);
                    185:        }
                    186: }
                    187:
                    188: void
                    189: current()
                    190: {
                    191:        struct group *gr;
                    192:        struct passwd *pw;
                    193:        int cnt, id, eid, lastid, ngroups;
                    194:        gid_t groups[NGROUPS];
                    195:        char *fmt;
                    196:
                    197:        id = getuid();
                    198:        (void)printf("uid=%u", id);
                    199:        if (pw = getpwuid(id))
                    200:                (void)printf("(%s)", pw->pw_name);
                    201:        if ((eid = geteuid()) != id) {
                    202:                (void)printf(" euid=%u", eid);
                    203:                if (pw = getpwuid(eid))
                    204:                        (void)printf("(%s)", pw->pw_name);
                    205:        }
                    206:        id = getgid();
                    207:        (void)printf(" gid=%u", id);
                    208:        if (gr = getgrgid(id))
                    209:                (void)printf("(%s)", gr->gr_name);
                    210:        if ((eid = getegid()) != id) {
                    211:                (void)printf(" egid=%u", eid);
                    212:                if (gr = getgrgid(eid))
                    213:                        (void)printf("(%s)", gr->gr_name);
                    214:        }
                    215:        if (ngroups = getgroups(NGROUPS, groups)) {
                    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:        }
                    226:        (void)printf("\n");
                    227: }
                    228:
                    229: void
                    230: user(pw)
                    231:        register struct passwd *pw;
                    232: {
                    233:        register struct group *gr;
                    234:        register char *fmt, **p;
                    235:        int cnt, id, lastid, ngroups, groups[NGROUPS + 1];
                    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))
                    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:        err("%s: No such user", u);
                    312:        /* NOTREACHED */
                    313: }
                    314:
                    315: #if __STDC__
                    316: #include <stdarg.h>
                    317: #else
                    318: #include <varargs.h>
                    319: #endif
                    320:
                    321: void
                    322: #if __STDC__
                    323: err(const char *fmt, ...)
                    324: #else
                    325: err(fmt, va_alist)
                    326:        char *fmt;
                    327:         va_dcl
                    328: #endif
                    329: {
                    330:        va_list ap;
                    331: #if __STDC__
                    332:        va_start(ap, fmt);
                    333: #else
                    334:        va_start(ap);
                    335: #endif
                    336:        (void)fprintf(stderr, "id: ");
                    337:        (void)vfprintf(stderr, fmt, ap);
                    338:        va_end(ap);
                    339:        (void)fprintf(stderr, "\n");
                    340:        exit(1);
                    341:        /* NOTREACHED */
                    342: }
                    343:
                    344: void
                    345: usage()
                    346: {
                    347:        (void)fprintf(stderr, "usage: id [user]\n");
                    348:        (void)fprintf(stderr, "       id -G [-n] [user]\n");
                    349:        (void)fprintf(stderr, "       id -g [-nr] [user]\n");
                    350:        (void)fprintf(stderr, "       id -u [-nr] [user]\n");
                    351:        exit(1);
                    352: }