[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.8

1.8     ! aaron       1: /*     $OpenBSD: id.c,v 1.7 1999/07/23 19:24:33 aaron Exp $    */
1.2       deraadt     2:
1.1       deraadt     3: /*-
                      4:  * Copyright (c) 1991, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. All advertising materials mentioning features or use of this software
                     16:  *    must display the following acknowledgement:
                     17:  *     This product includes software developed by the University of
                     18:  *     California, Berkeley and its contributors.
                     19:  * 4. Neither the name of the University nor the names of its contributors
                     20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
                     37: static char copyright[] =
                     38: "@(#) Copyright (c) 1991, 1993\n\
                     39:        The Regents of the University of California.  All rights reserved.\n";
                     40: #endif /* not lint */
                     41:
                     42: #ifndef lint
                     43: /*static char sccsid[] = "@(#)id.c     8.3 (Berkeley) 4/28/95";*/
1.8     ! aaron      44: static char rcsid[] = "$OpenBSD: id.c,v 1.7 1999/07/23 19:24:33 aaron Exp $";
1.1       deraadt    45: #endif /* not lint */
                     46:
                     47: #include <sys/param.h>
                     48:
                     49: #include <errno.h>
                     50: #include <grp.h>
                     51: #include <pwd.h>
                     52: #include <stdio.h>
                     53: #include <stdlib.h>
                     54: #include <string.h>
                     55: #include <unistd.h>
1.6       mickey     56: #include <err.h>
1.1       deraadt    57:
                     58: void   current __P((void));
                     59: void   pretty __P((struct passwd *));
                     60: void   group __P((struct passwd *, int));
                     61: void   usage __P((void));
                     62: void   user __P((struct passwd *));
                     63: struct passwd *
                     64:        who __P((char *));
                     65:
                     66: int
                     67: main(argc, argv)
                     68:        int argc;
                     69:        char *argv[];
                     70: {
                     71:        struct group *gr;
                     72:        struct passwd *pw;
                     73:        int Gflag, ch, gflag, id, nflag, pflag, rflag, uflag;
                     74:
                     75:        Gflag = gflag = nflag = pflag = rflag = uflag = 0;
1.3       millert    76:        while ((ch = getopt(argc, argv, "Ggnpru")) != -1)
1.1       deraadt    77:                switch(ch) {
                     78:                case 'G':
                     79:                        Gflag = 1;
                     80:                        break;
                     81:                case 'g':
                     82:                        gflag = 1;
                     83:                        break;
                     84:                case 'n':
                     85:                        nflag = 1;
                     86:                        break;
                     87:                case 'p':
                     88:                        pflag = 1;
                     89:                        break;
                     90:                case 'r':
                     91:                        rflag = 1;
                     92:                        break;
                     93:                case 'u':
                     94:                        uflag = 1;
                     95:                        break;
                     96:                case '?':
                     97:                default:
                     98:                        usage();
                     99:                }
                    100:        argc -= optind;
                    101:        argv += optind;
                    102:
                    103:        switch(Gflag + gflag + pflag + uflag) {
                    104:        case 1:
                    105:                break;
                    106:        case 0:
                    107:                if (!nflag && !rflag)
                    108:                        break;
                    109:                /* FALLTHROUGH */
                    110:        default:
                    111:                usage();
                    112:        }
                    113:
                    114:        pw = *argv ? who(*argv) : NULL;
                    115:
                    116:        if (gflag) {
                    117:                id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
                    118:                if (nflag && (gr = getgrgid(id)))
                    119:                        (void)printf("%s\n", gr->gr_name);
                    120:                else
                    121:                        (void)printf("%u\n", id);
                    122:                exit(0);
                    123:        }
                    124:
                    125:        if (uflag) {
                    126:                id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
                    127:                if (nflag && (pw = getpwuid(id)))
                    128:                        (void)printf("%s\n", pw->pw_name);
                    129:                else
                    130:                        (void)printf("%u\n", id);
                    131:                exit(0);
                    132:        }
                    133:
                    134:        if (Gflag) {
                    135:                group(pw, nflag);
                    136:                exit(0);
                    137:        }
                    138:
                    139:        if (pflag) {
                    140:                pretty(pw);
                    141:                exit(0);
                    142:        }
                    143:
                    144:        if (pw)
                    145:                user(pw);
                    146:        else
                    147:                current();
                    148:        exit(0);
                    149: }
                    150:
                    151: void
                    152: pretty(pw)
                    153:        struct passwd *pw;
                    154: {
                    155:        struct group *gr;
1.4       deraadt   156:        uid_t eid, rid;
1.1       deraadt   157:        char *login;
                    158:
                    159:        if (pw) {
                    160:                (void)printf("uid\t%s\n", pw->pw_name);
                    161:                (void)printf("groups\t");
                    162:                group(pw, 1);
                    163:        } else {
                    164:                if ((login = getlogin()) == NULL)
1.6       mickey    165:                        err(1, "getlogin");
1.1       deraadt   166:
                    167:                pw = getpwuid(rid = getuid());
                    168:                if (pw == NULL || strcmp(login, pw->pw_name))
                    169:                        (void)printf("login\t%s\n", login);
                    170:                if (pw)
                    171:                        (void)printf("uid\t%s\n", pw->pw_name);
                    172:                else
                    173:                        (void)printf("uid\t%u\n", rid);
                    174:
                    175:                if ((eid = geteuid()) != rid)
1.4       deraadt   176:                        if ((pw = getpwuid(eid)))
1.7       aaron     177:                                (void)printf("euid\t%s\n", pw->pw_name);
1.1       deraadt   178:                        else
1.7       aaron     179:                                (void)printf("euid\t%u\n", eid);
1.1       deraadt   180:                if ((rid = getgid()) != (eid = getegid()))
1.4       deraadt   181:                        if ((gr = getgrgid(rid)))
1.1       deraadt   182:                                (void)printf("rgid\t%s\n", gr->gr_name);
                    183:                        else
                    184:                                (void)printf("rgid\t%u\n", rid);
                    185:                (void)printf("groups\t");
                    186:                group(NULL, 1);
                    187:        }
                    188: }
                    189:
                    190: void
                    191: current()
                    192: {
                    193:        struct group *gr;
                    194:        struct passwd *pw;
1.4       deraadt   195:        int cnt, ngroups;
                    196:        uid_t id, eid;
                    197:        gid_t groups[NGROUPS], gid, lastgid;
1.1       deraadt   198:        char *fmt;
                    199:
                    200:        id = getuid();
                    201:        (void)printf("uid=%u", id);
1.4       deraadt   202:        if ((pw = getpwuid(id)))
1.1       deraadt   203:                (void)printf("(%s)", pw->pw_name);
                    204:        if ((eid = geteuid()) != id) {
                    205:                (void)printf(" euid=%u", eid);
1.4       deraadt   206:                if ((pw = getpwuid(eid)))
1.1       deraadt   207:                        (void)printf("(%s)", pw->pw_name);
                    208:        }
                    209:        id = getgid();
                    210:        (void)printf(" gid=%u", id);
1.4       deraadt   211:        if ((gr = getgrgid(id)))
1.1       deraadt   212:                (void)printf("(%s)", gr->gr_name);
                    213:        if ((eid = getegid()) != id) {
                    214:                (void)printf(" egid=%u", eid);
1.4       deraadt   215:                if ((gr = getgrgid(eid)))
1.1       deraadt   216:                        (void)printf("(%s)", gr->gr_name);
                    217:        }
1.4       deraadt   218:        if ((ngroups = getgroups(NGROUPS, groups))) {
                    219:                for (fmt = " groups=%u", lastgid = (gid_t)-1, cnt = 0; cnt < ngroups;
                    220:                    fmt = ", %u", lastgid = gid) {
                    221:                        gid = groups[cnt++];
                    222:                        if (lastgid == gid)
1.1       deraadt   223:                                continue;
1.4       deraadt   224:                        (void)printf(fmt, gid);
                    225:                        if ((gr = getgrgid(gid)))
1.1       deraadt   226:                                (void)printf("(%s)", gr->gr_name);
                    227:                }
                    228:        }
                    229:        (void)printf("\n");
                    230: }
                    231:
                    232: void
                    233: user(pw)
                    234:        register struct passwd *pw;
                    235: {
                    236:        register struct group *gr;
1.4       deraadt   237:        register char *fmt;
1.1       deraadt   238:        int cnt, id, lastid, ngroups, groups[NGROUPS + 1];
                    239:
                    240:        id = pw->pw_uid;
                    241:        (void)printf("uid=%u(%s)", id, pw->pw_name);
                    242:        (void)printf(" gid=%u", pw->pw_gid);
1.4       deraadt   243:        if ((gr = getgrgid(pw->pw_gid)))
1.1       deraadt   244:                (void)printf("(%s)", gr->gr_name);
                    245:        ngroups = NGROUPS + 1;
                    246:        (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
                    247:        fmt = " groups=%u";
                    248:        for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
                    249:                if (lastid == (id = groups[cnt]))
                    250:                        continue;
                    251:                (void)printf(fmt, id);
1.8     ! aaron     252:                fmt = ", %u";
1.4       deraadt   253:                if ((gr = getgrgid(id)))
1.1       deraadt   254:                        (void)printf("(%s)", gr->gr_name);
                    255:                lastid = id;
                    256:        }
                    257:        (void)printf("\n");
                    258: }
                    259:
                    260: void
                    261: group(pw, nflag)
                    262:        struct passwd *pw;
                    263:        int nflag;
                    264: {
                    265:        struct group *gr;
                    266:        int cnt, id, lastid, ngroups;
                    267:        gid_t groups[NGROUPS + 1];
                    268:        char *fmt;
                    269:
                    270:        if (pw) {
                    271:                ngroups = NGROUPS + 1;
                    272:                (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
                    273:        } else {
                    274:                groups[0] = getgid();
                    275:                ngroups = getgroups(NGROUPS, groups + 1) + 1;
                    276:        }
                    277:        fmt = nflag ? "%s" : "%u";
                    278:        for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
                    279:                if (lastid == (id = groups[cnt]))
                    280:                        continue;
                    281:                if (nflag) {
1.4       deraadt   282:                        if ((gr = getgrgid(id)))
1.1       deraadt   283:                                (void)printf(fmt, gr->gr_name);
                    284:                        else
                    285:                                (void)printf(*fmt == ' ' ? " %u" : "%u",
                    286:                                    id);
                    287:                        fmt = " %s";
                    288:                } else {
                    289:                        (void)printf(fmt, id);
                    290:                        fmt = " %u";
                    291:                }
                    292:                lastid = id;
                    293:        }
                    294:        (void)printf("\n");
                    295: }
                    296:
                    297: struct passwd *
                    298: who(u)
                    299:        char *u;
                    300: {
                    301:        struct passwd *pw;
1.4       deraadt   302:        uid_t id;
1.1       deraadt   303:        char *ep;
                    304:
                    305:        /*
                    306:         * Translate user argument into a pw pointer.  First, try to
                    307:         * get it as specified.  If that fails, try it as a number.
                    308:         */
1.4       deraadt   309:        if ((pw = getpwnam(u)))
1.1       deraadt   310:                return(pw);
1.4       deraadt   311:        id = strtoul(u, &ep, 10);
1.1       deraadt   312:        if (*u && !*ep && (pw = getpwuid(id)))
                    313:                return(pw);
1.6       mickey    314:        errx(1, "%s: No such user", u);
1.1       deraadt   315:        /* NOTREACHED */
                    316: }
                    317:
                    318: void
                    319: usage()
                    320: {
1.6       mickey    321:        (void)fprintf(stderr, "usage: id [user]\n"
                    322:                              "       id -G [-n] [user]\n"
                    323:                              "       id -g [-nr] [user]\n"
1.7       aaron     324:                              "       id -p\n"
1.6       mickey    325:                              "       id -u [-nr] [user]\n");
1.1       deraadt   326:        exit(1);
                    327: }