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

Annotation of src/usr.bin/su/su.c, Revision 1.8

1.8     ! millert     1: /*     $OpenBSD: su.c,v 1.7 1996/10/12 17:13:57 millert Exp $  */
1.4       deraadt     2:
1.1       deraadt     3: /*
                      4:  * Copyright (c) 1988 The Regents of the University of California.
                      5:  * 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: char copyright[] =
                     38: "@(#) Copyright (c) 1988 The Regents of the University of California.\n\
                     39:  All rights reserved.\n";
                     40: #endif /* not lint */
                     41:
                     42: #ifndef lint
                     43: /*static char sccsid[] = "from: @(#)su.c       5.26 (Berkeley) 7/6/91";*/
1.8     ! millert    44: static char rcsid[] = "$OpenBSD: su.c,v 1.7 1996/10/12 17:13:57 millert Exp $";
1.1       deraadt    45: #endif /* not lint */
                     46:
                     47: #include <sys/param.h>
                     48: #include <sys/time.h>
                     49: #include <sys/resource.h>
1.8     ! millert    50:
        !            51: #include <err.h>
        !            52: #include <errno.h>
        !            53: #include <grp.h>
        !            54: #include <paths.h>
        !            55: #include <pwd.h>
1.1       deraadt    56: #include <stdio.h>
                     57: #include <stdlib.h>
                     58: #include <string.h>
1.8     ! millert    59: #include <syslog.h>
1.1       deraadt    60: #include <unistd.h>
1.8     ! millert    61:
        !            62: #ifdef  SKEY
        !            63: #include <skey.h>
        !            64: #endif
1.1       deraadt    65:
                     66: #ifdef KERBEROS
                     67: #include <kerberosIV/des.h>
                     68: #include <kerberosIV/krb.h>
                     69: #include <netdb.h>
                     70:
                     71: #define        ARGSTR  "-Kflm"
                     72:
                     73: int use_kerberos = 1;
                     74: #else
                     75: #define        ARGSTR  "-flm"
                     76: #endif
                     77:
1.8     ! millert    78: char   *ontty __P((void));
        !            79: int    chshell __P((char *));
1.1       deraadt    80:
                     81: int
                     82: main(argc, argv)
                     83:        int argc;
                     84:        char **argv;
                     85: {
                     86:        extern char **environ;
                     87:        register struct passwd *pwd;
                     88:        register char *p, **g;
                     89:        struct group *gr;
                     90:        uid_t ruid, getuid();
                     91:        int asme, ch, asthem, fastlogin, prio;
                     92:        enum { UNSET, YES, NO } iscsh = UNSET;
                     93:        char *user, *shell, *avshell, *username, *cleanenv[10], **np;
                     94:        char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
                     95:
                     96:        asme = asthem = fastlogin = 0;
                     97:        while ((ch = getopt(argc, argv, ARGSTR)) != EOF)
                     98:                switch((char)ch) {
                     99: #ifdef KERBEROS
                    100:                case 'K':
                    101:                        use_kerberos = 0;
                    102:                        break;
                    103: #endif
                    104:                case 'f':
                    105:                        fastlogin = 1;
                    106:                        break;
                    107:                case '-':
                    108:                case 'l':
                    109:                        asme = 0;
                    110:                        asthem = 1;
                    111:                        break;
                    112:                case 'm':
                    113:                        asme = 1;
                    114:                        asthem = 0;
                    115:                        break;
                    116:                case '?':
                    117:                default:
1.7       millert   118:                        (void)fprintf(stderr,
                    119:                            "usage: su [%s] [login [shell arguments]]\n",
1.1       deraadt   120:                            ARGSTR);
                    121:                        exit(1);
                    122:                }
                    123:        argv += optind;
                    124:
                    125:        errno = 0;
                    126:        prio = getpriority(PRIO_PROCESS, 0);
                    127:        if (errno)
                    128:                prio = 0;
                    129:        (void)setpriority(PRIO_PROCESS, 0, -2);
                    130:        openlog("su", LOG_CONS, 0);
                    131:
                    132:        /* get current login name and shell */
                    133:        ruid = getuid();
                    134:        username = getlogin();
                    135:        if (username == NULL || (pwd = getpwnam(username)) == NULL ||
                    136:            pwd->pw_uid != ruid)
                    137:                pwd = getpwuid(ruid);
1.8     ! millert   138:        if (pwd == NULL)
        !           139:                errx(1, "who are you?");
1.1       deraadt   140:        username = strdup(pwd->pw_name);
                    141:        if (asme)
                    142:                if (pwd->pw_shell && *pwd->pw_shell)
                    143:                        shell = strcpy(shellbuf,  pwd->pw_shell);
                    144:                else {
                    145:                        shell = _PATH_BSHELL;
                    146:                        iscsh = NO;
                    147:                }
                    148:
                    149:        /* get target login information, default to root */
                    150:        user = *argv ? *argv : "root";
                    151:        np = *argv ? argv : argv-1;
                    152:
1.8     ! millert   153:        if ((pwd = getpwnam(user)) == NULL)
        !           154:                errx(1, "unknown login %s", user);
1.1       deraadt   155:
                    156:        if (ruid) {
                    157: #ifdef KERBEROS
                    158:            if (!use_kerberos || kerberos(username, user, pwd->pw_uid))
                    159: #endif
                    160:            {
                    161:                /* only allow those in group zero to su to root. */
1.3       deraadt   162:                if (pwd->pw_uid == 0 && (gr = getgrgid((gid_t)0))
                    163:                    && gr->gr_mem && *(gr->gr_mem))
1.1       deraadt   164:                        for (g = gr->gr_mem;; ++g) {
1.8     ! millert   165:                                if (!*g)
        !           166:                                        errx(1, "you are not in the correct group to su %s.", user);
        !           167:                                if (strcmp(username, *g) == 0)
1.1       deraadt   168:                                        break;
                    169:                }
                    170:                /* if target requires a password, verify it */
                    171:                if (*pwd->pw_passwd) {
                    172:                        p = getpass("Password:");
                    173: #ifdef SKEY
                    174:                        if (strcasecmp(p, "s/key") == 0) {
1.8     ! millert   175:                                if (skey_haskey(user))
        !           176:                                        errx(1, "Sorry, you have no s/key.");
        !           177:                                else if (skey_authenticate(user))
        !           178:                                        goto badlogin;
1.1       deraadt   179:                        } else
                    180: #endif
                    181:                        if (strcmp(pwd->pw_passwd, crypt(p, pwd->pw_passwd))) {
                    182: badlogin:
                    183:                                fprintf(stderr, "Sorry\n");
                    184:                                syslog(LOG_AUTH|LOG_WARNING,
                    185:                                        "BAD SU %s to %s%s", username,
                    186:                                        user, ontty());
                    187:                                exit(1);
                    188:                        }
                    189:                }
                    190:            }
1.8     ! millert   191:            if (pwd->pw_expire && time(NULL) >= pwd->pw_expire) {
        !           192:                    fprintf(stderr, "Sorry - account expired\n");
        !           193:                    syslog(LOG_AUTH|LOG_WARNING, "BAD SU %s to %s%s", username,
        !           194:                            user, ontty());
        !           195:                    exit(1);
        !           196:            }
1.1       deraadt   197:        }
                    198:
                    199:        if (asme) {
                    200:                /* if asme and non-standard target shell, must be root */
1.8     ! millert   201:                if (!chshell(pwd->pw_shell) && ruid)
        !           202:                        errx(1, "permission denied (shell).");
1.1       deraadt   203:        } else if (pwd->pw_shell && *pwd->pw_shell) {
                    204:                shell = pwd->pw_shell;
                    205:                iscsh = UNSET;
                    206:        } else {
                    207:                shell = _PATH_BSHELL;
                    208:                iscsh = NO;
                    209:        }
                    210:
1.8     ! millert   211:        if (p = strrchr(shell, '/'))
1.1       deraadt   212:                avshell = p+1;
                    213:        else
                    214:                avshell = shell;
                    215:
                    216:        /* if we're forking a csh, we want to slightly muck the args */
                    217:        if (iscsh == UNSET)
                    218:                iscsh = strcmp(avshell, "csh") ? NO : YES;
                    219:
                    220:        /* set permissions */
1.8     ! millert   221:        if (setgid(pwd->pw_gid) < 0)
        !           222:                err(1, "setgid");
        !           223:        if (initgroups(user, pwd->pw_gid))
        !           224:                err(1, "initgroups failed");
        !           225:        if (setuid(pwd->pw_uid) < 0)
        !           226:                err(1, "setuid");
1.1       deraadt   227:
                    228:        if (!asme) {
                    229:                if (asthem) {
                    230:                        p = getenv("TERM");
                    231:                        cleanenv[0] = NULL;
                    232:                        environ = cleanenv;
                    233:                        (void)setenv("PATH", _PATH_DEFPATH, 1);
1.6       deraadt   234:                        if (p)
                    235:                                (void)setenv("TERM", p, 1);
1.5       deraadt   236:
                    237:                        seteuid(pwd->pw_uid);
                    238:                        setegid(pwd->pw_gid);
1.8     ! millert   239:                        if (chdir(pwd->pw_dir) < 0)
        !           240:                                errx(1, "no directory");
1.5       deraadt   241:                        seteuid(0);
                    242:                        setegid(0);     /* XXX use a saved gid instead? */
1.1       deraadt   243:                }
                    244:                if (asthem || pwd->pw_uid)
                    245:                        (void)setenv("USER", pwd->pw_name, 1);
                    246:                (void)setenv("HOME", pwd->pw_dir, 1);
                    247:                (void)setenv("SHELL", shell, 1);
                    248:        }
                    249:
                    250:        if (iscsh == YES) {
                    251:                if (fastlogin)
                    252:                        *np-- = "-f";
                    253:                if (asme)
                    254:                        *np-- = "-m";
                    255:        }
                    256:
                    257:        if (asthem) {
                    258:                avshellbuf[0] = '-';
                    259:                strcpy(avshellbuf+1, avshell);
                    260:                avshell = avshellbuf;
                    261:        } else if (iscsh == YES) {
                    262:                /* csh strips the first character... */
                    263:                avshellbuf[0] = '_';
                    264:                strcpy(avshellbuf+1, avshell);
                    265:                avshell = avshellbuf;
                    266:        }
                    267:
                    268:        *np = avshell;
                    269:
                    270:        if (ruid != 0)
                    271:                syslog(LOG_NOTICE|LOG_AUTH, "%s to %s%s",
                    272:                    username, user, ontty());
                    273:
                    274:        (void)setpriority(PRIO_PROCESS, 0, prio);
                    275:
                    276:        execv(shell, np);
1.8     ! millert   277:        err(1, "%s", shell);
1.1       deraadt   278: }
                    279:
                    280: int
                    281: chshell(sh)
                    282:        char *sh;
                    283: {
                    284:        register char *cp;
                    285:
                    286:        while ((cp = getusershell()) != NULL)
1.8     ! millert   287:                if (strcmp(cp, sh) == 0)
1.1       deraadt   288:                        return (1);
                    289:        return (0);
                    290: }
                    291:
                    292: char *
                    293: ontty()
                    294: {
                    295:        char *p, *ttyname();
                    296:        static char buf[MAXPATHLEN + 4];
                    297:
                    298:        buf[0] = 0;
                    299:        if (p = ttyname(STDERR_FILENO))
1.8     ! millert   300:                snprintf(buf, sizeof(buf), " on %s", p);
1.1       deraadt   301:        return (buf);
                    302: }
                    303:
                    304: #ifdef KERBEROS
                    305: kerberos(username, user, uid)
                    306:        char *username, *user;
                    307:        int uid;
                    308: {
                    309:        KTEXT_ST ticket;
                    310:        AUTH_DAT authdata;
                    311:        struct hostent *hp;
                    312:        register char *p;
                    313:        int kerno;
                    314:        u_long faddr;
                    315:        char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN];
                    316:        char hostname[MAXHOSTNAMELEN], savehost[MAXHOSTNAMELEN];
                    317:        char *ontty(), *krb_get_phost();
                    318:
                    319:        if (krb_get_lrealm(lrealm, 1) != KSUCCESS)
                    320:                return (1);
                    321:        if (koktologin(username, lrealm, user) && !uid) {
                    322:                (void)fprintf(stderr, "kerberos su: not in %s's ACL.\n", user);
                    323:                return (1);
                    324:        }
1.8     ! millert   325:        (void)snprintf(krbtkfile, sizeof(krbtkfile), "%s_%s_%d", TKT_ROOT,
        !           326:                user, getuid());
1.1       deraadt   327:
                    328:        (void)setenv("KRBTKFILE", krbtkfile, 1);
                    329:        (void)krb_set_tkt_string(krbtkfile);
                    330:        /*
                    331:         * Set real as well as effective ID to 0 for the moment,
                    332:         * to make the kerberos library do the right thing.
                    333:         */
                    334:        if (setuid(0) < 0) {
1.8     ! millert   335:                warn("setuid");
1.1       deraadt   336:                return (1);
                    337:        }
                    338:
                    339:        /*
                    340:         * Little trick here -- if we are su'ing to root,
                    341:         * we need to get a ticket for "xxx.root", where xxx represents
                    342:         * the name of the person su'ing.  Otherwise (non-root case),
                    343:         * we need to get a ticket for "yyy.", where yyy represents
                    344:         * the name of the person being su'd to, and the instance is null
                    345:         *
                    346:         * We should have a way to set the ticket lifetime,
                    347:         * with a system default for root.
                    348:         */
                    349:        kerno = krb_get_pw_in_tkt((uid == 0 ? username : user),
                    350:                (uid == 0 ? "root" : ""), lrealm,
                    351:                "krbtgt", lrealm, DEFAULT_TKT_LIFE, 0);
                    352:
                    353:        if (kerno != KSUCCESS) {
                    354:                if (kerno == KDC_PR_UNKNOWN) {
1.8     ! millert   355:                        warnx("kerberos principal unknown: %s.%s@%s",
1.1       deraadt   356:                                (uid == 0 ? username : user),
                    357:                                (uid == 0 ? "root" : ""), lrealm);
                    358:                        return (1);
                    359:                }
1.8     ! millert   360:                warnx("unable to su: %s", krb_err_txt[kerno]);
1.1       deraadt   361:                syslog(LOG_NOTICE|LOG_AUTH,
                    362:                    "BAD Kerberos SU: %s to %s%s: %s",
                    363:                    username, user, ontty(), krb_err_txt[kerno]);
                    364:                return (1);
                    365:        }
                    366:
                    367:        if (chown(krbtkfile, uid, -1) < 0) {
1.8     ! millert   368:                warn("chown");
1.1       deraadt   369:                (void)unlink(krbtkfile);
                    370:                return (1);
                    371:        }
                    372:
                    373:        (void)setpriority(PRIO_PROCESS, 0, -2);
                    374:
                    375:        if (gethostname(hostname, sizeof(hostname)) == -1) {
1.8     ! millert   376:                warn("gethostname");
1.1       deraadt   377:                dest_tkt();
                    378:                return (1);
                    379:        }
                    380:
                    381:        (void)strncpy(savehost, krb_get_phost(hostname), sizeof(savehost));
                    382:        savehost[sizeof(savehost) - 1] = '\0';
                    383:
                    384:        kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
                    385:
                    386:        if (kerno == KDC_PR_UNKNOWN) {
1.8     ! millert   387:                warnx("Warning: TGT not verified.");
1.1       deraadt   388:                syslog(LOG_NOTICE|LOG_AUTH,
                    389:                    "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
                    390:                    username, user, ontty(), krb_err_txt[kerno],
                    391:                    "rcmd", savehost);
                    392:        } else if (kerno != KSUCCESS) {
1.8     ! millert   393:                warnx("Unable to use TGT: %s", krb_err_txt[kerno]);
1.1       deraadt   394:                syslog(LOG_NOTICE|LOG_AUTH, "failed su: %s to %s%s: %s",
                    395:                    username, user, ontty(), krb_err_txt[kerno]);
                    396:                dest_tkt();
                    397:                return (1);
                    398:        } else {
                    399:                if (!(hp = gethostbyname(hostname))) {
1.8     ! millert   400:                        warnx("can't get addr of %s", hostname);
1.1       deraadt   401:                        dest_tkt();
                    402:                        return (1);
                    403:                }
                    404:                (void)bcopy((char *)hp->h_addr, (char *)&faddr, sizeof(faddr));
                    405:
                    406:                if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
                    407:                    &authdata, "")) != KSUCCESS) {
1.8     ! millert   408:                        warnx("unable to verify rcmd ticket: %s",
        !           409:                              krb_err_txt[kerno]);
1.1       deraadt   410:                        syslog(LOG_NOTICE|LOG_AUTH,
                    411:                            "failed su: %s to %s%s: %s", username,
                    412:                             user, ontty(), krb_err_txt[kerno]);
                    413:                        dest_tkt();
                    414:                        return (1);
                    415:                }
                    416:        }
                    417:        return (0);
                    418: }
                    419:
                    420: koktologin(name, realm, toname)
                    421:        char *name, *realm, *toname;
                    422: {
                    423:        register AUTH_DAT *kdata;
                    424:        AUTH_DAT kdata_st;
                    425:
                    426:        kdata = &kdata_st;
                    427:        bzero((caddr_t) kdata, sizeof(*kdata));
                    428:        (void)strcpy(kdata->pname, name);
                    429:        (void)strcpy(kdata->pinst,
                    430:            ((strcmp(toname, "root") == 0) ? "root" : ""));
                    431:        (void)strcpy(kdata->prealm, realm);
                    432:        return (kuserok(kdata, toname));
                    433: }
                    434: #endif