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

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