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

1.9     ! millert     1: /*     $OpenBSD: su.c,v 1.8 1996/10/12 20:40:37 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.9     ! millert    44: static char rcsid[] = "$OpenBSD: su.c,v 1.8 1996/10/12 20:40:37 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;
1.9     ! millert    90:        uid_t ruid;
1.1       deraadt    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.9     ! millert   140:        if ((username = strdup(pwd->pw_name)) == NULL)
        !           141:                err(1, "can't allocate memory");
1.1       deraadt   142:        if (asme)
                    143:                if (pwd->pw_shell && *pwd->pw_shell)
1.9     ! millert   144:                        shell = strcpy(shellbuf, pwd->pw_shell);
1.1       deraadt   145:                else {
                    146:                        shell = _PATH_BSHELL;
                    147:                        iscsh = NO;
                    148:                }
                    149:
                    150:        /* get target login information, default to root */
                    151:        user = *argv ? *argv : "root";
                    152:        np = *argv ? argv : argv-1;
                    153:
1.8       millert   154:        if ((pwd = getpwnam(user)) == NULL)
                    155:                errx(1, "unknown login %s", user);
1.1       deraadt   156:
                    157:        if (ruid) {
                    158: #ifdef KERBEROS
                    159:            if (!use_kerberos || kerberos(username, user, pwd->pw_uid))
                    160: #endif
                    161:            {
                    162:                /* only allow those in group zero to su to root. */
1.3       deraadt   163:                if (pwd->pw_uid == 0 && (gr = getgrgid((gid_t)0))
                    164:                    && gr->gr_mem && *(gr->gr_mem))
1.1       deraadt   165:                        for (g = gr->gr_mem;; ++g) {
1.8       millert   166:                                if (!*g)
                    167:                                        errx(1, "you are not in the correct group to su %s.", user);
                    168:                                if (strcmp(username, *g) == 0)
1.1       deraadt   169:                                        break;
                    170:                }
                    171:                /* if target requires a password, verify it */
                    172:                if (*pwd->pw_passwd) {
                    173:                        p = getpass("Password:");
                    174: #ifdef SKEY
                    175:                        if (strcasecmp(p, "s/key") == 0) {
1.8       millert   176:                                if (skey_haskey(user))
                    177:                                        errx(1, "Sorry, you have no s/key.");
                    178:                                else if (skey_authenticate(user))
                    179:                                        goto badlogin;
1.1       deraadt   180:                        } else
                    181: #endif
                    182:                        if (strcmp(pwd->pw_passwd, crypt(p, pwd->pw_passwd))) {
                    183: badlogin:
                    184:                                fprintf(stderr, "Sorry\n");
                    185:                                syslog(LOG_AUTH|LOG_WARNING,
                    186:                                        "BAD SU %s to %s%s", username,
                    187:                                        user, ontty());
                    188:                                exit(1);
                    189:                        }
                    190:                }
                    191:            }
1.8       millert   192:            if (pwd->pw_expire && time(NULL) >= pwd->pw_expire) {
                    193:                    fprintf(stderr, "Sorry - account expired\n");
                    194:                    syslog(LOG_AUTH|LOG_WARNING, "BAD SU %s to %s%s", username,
                    195:                            user, ontty());
                    196:                    exit(1);
                    197:            }
1.1       deraadt   198:        }
                    199:
                    200:        if (asme) {
                    201:                /* if asme and non-standard target shell, must be root */
1.8       millert   202:                if (!chshell(pwd->pw_shell) && ruid)
                    203:                        errx(1, "permission denied (shell).");
1.1       deraadt   204:        } else if (pwd->pw_shell && *pwd->pw_shell) {
                    205:                shell = pwd->pw_shell;
                    206:                iscsh = UNSET;
                    207:        } else {
                    208:                shell = _PATH_BSHELL;
                    209:                iscsh = NO;
                    210:        }
                    211:
1.8       millert   212:        if (p = strrchr(shell, '/'))
1.1       deraadt   213:                avshell = p+1;
                    214:        else
                    215:                avshell = shell;
                    216:
                    217:        /* if we're forking a csh, we want to slightly muck the args */
                    218:        if (iscsh == UNSET)
                    219:                iscsh = strcmp(avshell, "csh") ? NO : YES;
                    220:
                    221:        /* set permissions */
1.8       millert   222:        if (setgid(pwd->pw_gid) < 0)
                    223:                err(1, "setgid");
                    224:        if (initgroups(user, pwd->pw_gid))
                    225:                err(1, "initgroups failed");
                    226:        if (setuid(pwd->pw_uid) < 0)
                    227:                err(1, "setuid");
1.1       deraadt   228:
                    229:        if (!asme) {
                    230:                if (asthem) {
                    231:                        p = getenv("TERM");
                    232:                        cleanenv[0] = NULL;
                    233:                        environ = cleanenv;
                    234:                        (void)setenv("PATH", _PATH_DEFPATH, 1);
1.6       deraadt   235:                        if (p)
                    236:                                (void)setenv("TERM", p, 1);
1.5       deraadt   237:
                    238:                        seteuid(pwd->pw_uid);
                    239:                        setegid(pwd->pw_gid);
1.8       millert   240:                        if (chdir(pwd->pw_dir) < 0)
                    241:                                errx(1, "no directory");
1.5       deraadt   242:                        seteuid(0);
                    243:                        setegid(0);     /* XXX use a saved gid instead? */
1.1       deraadt   244:                }
                    245:                if (asthem || pwd->pw_uid)
                    246:                        (void)setenv("USER", pwd->pw_name, 1);
                    247:                (void)setenv("HOME", pwd->pw_dir, 1);
                    248:                (void)setenv("SHELL", shell, 1);
                    249:        }
                    250:
                    251:        if (iscsh == YES) {
                    252:                if (fastlogin)
                    253:                        *np-- = "-f";
                    254:                if (asme)
                    255:                        *np-- = "-m";
                    256:        }
                    257:
                    258:        if (asthem) {
                    259:                avshellbuf[0] = '-';
                    260:                strcpy(avshellbuf+1, avshell);
                    261:                avshell = avshellbuf;
                    262:        } else if (iscsh == YES) {
                    263:                /* csh strips the first character... */
                    264:                avshellbuf[0] = '_';
                    265:                strcpy(avshellbuf+1, avshell);
                    266:                avshell = avshellbuf;
                    267:        }
                    268:
                    269:        *np = avshell;
                    270:
                    271:        if (ruid != 0)
                    272:                syslog(LOG_NOTICE|LOG_AUTH, "%s to %s%s",
                    273:                    username, user, ontty());
                    274:
                    275:        (void)setpriority(PRIO_PROCESS, 0, prio);
                    276:
                    277:        execv(shell, np);
1.9     ! millert   278:        err(1, shell);
1.1       deraadt   279: }
                    280:
                    281: int
                    282: chshell(sh)
                    283:        char *sh;
                    284: {
                    285:        register char *cp;
                    286:
                    287:        while ((cp = getusershell()) != NULL)
1.8       millert   288:                if (strcmp(cp, sh) == 0)
1.1       deraadt   289:                        return (1);
                    290:        return (0);
                    291: }
                    292:
                    293: char *
                    294: ontty()
                    295: {
                    296:        char *p, *ttyname();
                    297:        static char buf[MAXPATHLEN + 4];
                    298:
                    299:        buf[0] = 0;
                    300:        if (p = ttyname(STDERR_FILENO))
1.8       millert   301:                snprintf(buf, sizeof(buf), " on %s", p);
1.1       deraadt   302:        return (buf);
                    303: }
                    304:
                    305: #ifdef KERBEROS
                    306: kerberos(username, user, uid)
                    307:        char *username, *user;
                    308:        int uid;
                    309: {
                    310:        KTEXT_ST ticket;
                    311:        AUTH_DAT authdata;
                    312:        struct hostent *hp;
                    313:        register char *p;
                    314:        int kerno;
                    315:        u_long faddr;
                    316:        char lrealm[REALM_SZ], krbtkfile[MAXPATHLEN];
                    317:        char hostname[MAXHOSTNAMELEN], savehost[MAXHOSTNAMELEN];
                    318:        char *ontty(), *krb_get_phost();
                    319:
                    320:        if (krb_get_lrealm(lrealm, 1) != KSUCCESS)
                    321:                return (1);
                    322:        if (koktologin(username, lrealm, user) && !uid) {
                    323:                (void)fprintf(stderr, "kerberos su: not in %s's ACL.\n", user);
                    324:                return (1);
                    325:        }
1.8       millert   326:        (void)snprintf(krbtkfile, sizeof(krbtkfile), "%s_%s_%d", TKT_ROOT,
                    327:                user, getuid());
1.1       deraadt   328:
                    329:        (void)setenv("KRBTKFILE", krbtkfile, 1);
                    330:        (void)krb_set_tkt_string(krbtkfile);
                    331:        /*
                    332:         * Set real as well as effective ID to 0 for the moment,
                    333:         * to make the kerberos library do the right thing.
                    334:         */
                    335:        if (setuid(0) < 0) {
1.8       millert   336:                warn("setuid");
1.1       deraadt   337:                return (1);
                    338:        }
                    339:
                    340:        /*
                    341:         * Little trick here -- if we are su'ing to root,
                    342:         * we need to get a ticket for "xxx.root", where xxx represents
                    343:         * the name of the person su'ing.  Otherwise (non-root case),
                    344:         * we need to get a ticket for "yyy.", where yyy represents
                    345:         * the name of the person being su'd to, and the instance is null
                    346:         *
                    347:         * We should have a way to set the ticket lifetime,
                    348:         * with a system default for root.
                    349:         */
                    350:        kerno = krb_get_pw_in_tkt((uid == 0 ? username : user),
                    351:                (uid == 0 ? "root" : ""), lrealm,
                    352:                "krbtgt", lrealm, DEFAULT_TKT_LIFE, 0);
                    353:
                    354:        if (kerno != KSUCCESS) {
                    355:                if (kerno == KDC_PR_UNKNOWN) {
1.8       millert   356:                        warnx("kerberos principal unknown: %s.%s@%s",
1.1       deraadt   357:                                (uid == 0 ? username : user),
                    358:                                (uid == 0 ? "root" : ""), lrealm);
                    359:                        return (1);
                    360:                }
1.8       millert   361:                warnx("unable to su: %s", krb_err_txt[kerno]);
1.1       deraadt   362:                syslog(LOG_NOTICE|LOG_AUTH,
                    363:                    "BAD Kerberos SU: %s to %s%s: %s",
                    364:                    username, user, ontty(), krb_err_txt[kerno]);
                    365:                return (1);
                    366:        }
                    367:
                    368:        if (chown(krbtkfile, uid, -1) < 0) {
1.8       millert   369:                warn("chown");
1.1       deraadt   370:                (void)unlink(krbtkfile);
                    371:                return (1);
                    372:        }
                    373:
                    374:        (void)setpriority(PRIO_PROCESS, 0, -2);
                    375:
                    376:        if (gethostname(hostname, sizeof(hostname)) == -1) {
1.8       millert   377:                warn("gethostname");
1.1       deraadt   378:                dest_tkt();
                    379:                return (1);
                    380:        }
                    381:
                    382:        (void)strncpy(savehost, krb_get_phost(hostname), sizeof(savehost));
                    383:        savehost[sizeof(savehost) - 1] = '\0';
                    384:
                    385:        kerno = krb_mk_req(&ticket, "rcmd", savehost, lrealm, 33);
                    386:
                    387:        if (kerno == KDC_PR_UNKNOWN) {
1.8       millert   388:                warnx("Warning: TGT not verified.");
1.1       deraadt   389:                syslog(LOG_NOTICE|LOG_AUTH,
                    390:                    "%s to %s%s, TGT not verified (%s); %s.%s not registered?",
                    391:                    username, user, ontty(), krb_err_txt[kerno],
                    392:                    "rcmd", savehost);
                    393:        } else if (kerno != KSUCCESS) {
1.8       millert   394:                warnx("Unable to use TGT: %s", krb_err_txt[kerno]);
1.1       deraadt   395:                syslog(LOG_NOTICE|LOG_AUTH, "failed su: %s to %s%s: %s",
                    396:                    username, user, ontty(), krb_err_txt[kerno]);
                    397:                dest_tkt();
                    398:                return (1);
                    399:        } else {
                    400:                if (!(hp = gethostbyname(hostname))) {
1.8       millert   401:                        warnx("can't get addr of %s", hostname);
1.1       deraadt   402:                        dest_tkt();
                    403:                        return (1);
                    404:                }
                    405:                (void)bcopy((char *)hp->h_addr, (char *)&faddr, sizeof(faddr));
                    406:
                    407:                if ((kerno = krb_rd_req(&ticket, "rcmd", savehost, faddr,
                    408:                    &authdata, "")) != KSUCCESS) {
1.8       millert   409:                        warnx("unable to verify rcmd ticket: %s",
                    410:                              krb_err_txt[kerno]);
1.1       deraadt   411:                        syslog(LOG_NOTICE|LOG_AUTH,
                    412:                            "failed su: %s to %s%s: %s", username,
                    413:                             user, ontty(), krb_err_txt[kerno]);
                    414:                        dest_tkt();
                    415:                        return (1);
                    416:                }
                    417:        }
                    418:        return (0);
                    419: }
                    420:
                    421: koktologin(name, realm, toname)
                    422:        char *name, *realm, *toname;
                    423: {
                    424:        register AUTH_DAT *kdata;
                    425:        AUTH_DAT kdata_st;
                    426:
                    427:        kdata = &kdata_st;
                    428:        bzero((caddr_t) kdata, sizeof(*kdata));
                    429:        (void)strcpy(kdata->pname, name);
                    430:        (void)strcpy(kdata->pinst,
                    431:            ((strcmp(toname, "root") == 0) ? "root" : ""));
                    432:        (void)strcpy(kdata->prealm, realm);
                    433:        return (kuserok(kdata, toname));
                    434: }
                    435: #endif