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

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