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

Annotation of src/usr.bin/login/klogin.c, Revision 1.6

1.6     ! art         1: /*     $OpenBSD: klogin.c,v 1.5 1997/06/29 11:10:33 provos Exp $       */
1.2       deraadt     2: /*     $NetBSD: klogin.c,v 1.7 1996/05/21 22:07:04 mrg Exp $   */
1.1       deraadt     3:
                      4: /*-
                      5:  * Copyright (c) 1990, 1993, 1994
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: #if 0
                     39: static char sccsid[] = "@(#)klogin.c   8.3 (Berkeley) 4/2/94";
                     40: #endif
1.6     ! art        41: static char rcsid[] = "$OpenBSD: klogin.c,v 1.5 1997/06/29 11:10:33 provos Exp $";
1.1       deraadt    42: #endif /* not lint */
                     43:
                     44: #ifdef KERBEROS
                     45: #include <sys/param.h>
                     46: #include <sys/syslog.h>
1.5       provos     47: #include <des.h>
1.1       deraadt    48: #include <kerberosIV/krb.h>
1.6     ! art        49: #include <kerberosIV/kafs.h>
1.1       deraadt    50:
                     51: #include <err.h>
                     52: #include <netdb.h>
                     53: #include <pwd.h>
                     54: #include <stdio.h>
                     55: #include <stdlib.h>
                     56: #include <string.h>
                     57: #include <unistd.h>
                     58:
                     59: #include <sys/types.h>
                     60: #include <sys/stat.h>
                     61: #include <fcntl.h>
                     62:
                     63: #define        INITIAL_TICKET  "krbtgt"
                     64: #define        VERIFY_SERVICE  "rcmd"
                     65:
                     66: extern int notickets;
                     67: extern char *krbtkfile_env;
                     68: extern char *tty;
                     69:
                     70: static char tkt_location[MAXPATHLEN];  /* a pointer to this is returned... */
                     71:
                     72: /*
                     73:  * Attempt to log the user in using Kerberos authentication
                     74:  *
                     75:  * return 0 on success (will be logged in)
                     76:  *       1 if Kerberos failed (try local password in login)
                     77:  */
                     78: int
                     79: klogin(pw, instance, localhost, password)
                     80:        struct passwd *pw;
                     81:        char *instance, *localhost, *password;
                     82: {
                     83:        int kerror;
                     84:        AUTH_DAT authdata;
                     85:        KTEXT_ST ticket;
                     86:        struct hostent *hp;
                     87:        unsigned long faddr;
                     88:        char realm[REALM_SZ], savehost[MAXHOSTNAMELEN];
                     89:        char *krb_get_phost();
                     90:
                     91: #ifdef SKEY
                     92:        /*
                     93:         * We don't do s/key challenge and Kerberos at the same time
                     94:         */
                     95:        if (strcasecmp(password, "s/key") == 0) {
                     96:            return (1);
                     97:        }
                     98: #endif
                     99:
                    100:        /*
1.4       dm        101:         * Root logins don't use Kerberos (or at least shouldn't be
                    102:         * sending kerberos passwords around in cleartext), so don't
                    103:         * allow any root logins here (keeping in mind that we only
                    104:         * get here with a password).
                    105:         *
1.1       deraadt   106:         * If we have a realm, try getting a ticket-granting ticket
                    107:         * and using it to authenticate.  Otherwise, return
                    108:         * failure so that we can try the normal passwd file
                    109:         * for a password.  If that's ok, log the user in
                    110:         * without issuing any tickets.
                    111:         */
1.4       dm        112:        if (pw->pw_uid == 0 || krb_get_lrealm(realm, 0) != KSUCCESS)
1.1       deraadt   113:                return (1);
                    114:
                    115:        /*
                    116:         * get TGT for local realm
                    117:         * tickets are stored in a file named TKT_ROOT plus uid plus tty
                    118:         * except for user.root tickets.
                    119:         */
                    120:
                    121:        if (strcmp(instance, "root") != 0)
                    122:                (void)sprintf(tkt_location, "%s%d.%s",
                    123:                              TKT_ROOT, pw->pw_uid, tty);
                    124:        else
                    125:                (void)sprintf(tkt_location, "%s_root_%d.%s",
                    126:                              TKT_ROOT, pw->pw_uid, tty);
                    127:        krbtkfile_env = tkt_location;
                    128:        (void)krb_set_tkt_string(tkt_location);
                    129:
                    130:        /*
                    131:         * Set real as well as effective ID to 0 for the moment,
                    132:         * to make the kerberos library do the right thing.
                    133:         */
                    134:        if (setuid(0) < 0) {
                    135:                warnx("setuid");
                    136:                return (1);
                    137:        }
                    138:        kerror = krb_get_pw_in_tkt(pw->pw_name, instance,
                    139:                    realm, INITIAL_TICKET, realm, DEFAULT_TKT_LIFE, password);
                    140:        /*
                    141:         * If we got a TGT, get a local "rcmd" ticket and check it so as to
                    142:         * ensure that we are not talking to a bogus Kerberos server.
                    143:         *
                    144:         * There are 2 cases where we still allow a login:
                    145:         *      1: the VERIFY_SERVICE doesn't exist in the KDC
                    146:         *      2: local host has no srvtab, as (hopefully) indicated by a
                    147:         *         return value of RD_AP_UNDEC from krb_rd_req().
                    148:         */
                    149:        if (kerror != INTK_OK) {
                    150:                if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN) {
                    151:                        syslog(LOG_ERR, "Kerberos intkt error: %s",
                    152:                            krb_err_txt[kerror]);
                    153:                        dest_tkt();
                    154:                }
                    155:                return (1);
                    156:        }
                    157:
                    158:        if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0)
                    159:                syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE);
                    160:
                    161:        (void)strncpy(savehost, krb_get_phost(localhost), sizeof(savehost));
                    162:        savehost[sizeof(savehost)-1] = NULL;
                    163:
                    164:        /*
                    165:         * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host,
                    166:         * still allow login with tickets, but log the error condition.
                    167:         */
                    168:
                    169:        kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
                    170:        if (kerror == KDC_PR_UNKNOWN) {
                    171:                syslog(LOG_NOTICE,
                    172:                    "warning: TGT not verified (%s); %s.%s not registered, or srvtab is wrong?",
                    173:                    krb_err_txt[kerror], VERIFY_SERVICE, savehost);
                    174:                notickets = 0;
                    175:                /*
                    176:                 * but for security, don't allow root instances in under
                    177:                 * this condition!
                    178:                 */
                    179:                if (strcmp(instance, "root") == 0) {
                    180:                  syslog(LOG_ERR, "Kerberos %s root instance login refused\n",
                    181:                         pw->pw_name);
                    182:                  dest_tkt();
                    183:                  return (1);
                    184:                }
1.4       dm        185:                /* Otherwise, leave ticket around, but make sure
                    186:                 * password matches the Unix password. */
                    187:                return (1);
1.1       deraadt   188:        }
                    189:
                    190:        if (kerror != KSUCCESS) {
                    191:                warnx("unable to use TGT: (%s)", krb_err_txt[kerror]);
                    192:                syslog(LOG_NOTICE, "unable to use TGT: (%s)",
                    193:                    krb_err_txt[kerror]);
                    194:                dest_tkt();
                    195:                return (1);
                    196:        }
                    197:
                    198:        if (!(hp = gethostbyname(localhost))) {
                    199:                syslog(LOG_ERR, "couldn't get local host address");
                    200:                dest_tkt();
                    201:                return (1);
                    202:        }
                    203:
                    204:        memmove((void *)&faddr, (void *)hp->h_addr, sizeof(faddr));
                    205:
                    206:        kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
                    207:            &authdata, "");
                    208:
                    209:        if (kerror == KSUCCESS) {
                    210:                notickets = 0;
                    211:                return (0);
                    212:        }
                    213:
                    214:        /* undecipherable: probably didn't have a srvtab on the local host */
1.2       deraadt   215:        if (kerror == RD_AP_UNDEC) {
1.1       deraadt   216:                syslog(LOG_NOTICE, "krb_rd_req: (%s)\n", krb_err_txt[kerror]);
                    217:                dest_tkt();
                    218:                return (1);
                    219:        }
                    220:        /* failed for some other reason */
                    221:        warnx("unable to verify %s ticket: (%s)", VERIFY_SERVICE,
                    222:            krb_err_txt[kerror]);
                    223:        syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE,
                    224:            krb_err_txt[kerror]);
                    225:        dest_tkt();
                    226:        return (1);
                    227: }
                    228:
                    229: void
1.6     ! art       230: kgettokens(homedir)
        !           231:        char *homedir;
        !           232: {
        !           233:        /* buy AFS-tokens for homedir */
        !           234:        if (k_hasafs()) {
        !           235:                char cell[128];
        !           236:                k_setpag();
        !           237:                if (k_afs_cell_of_file(homedir,
        !           238:                                       cell, sizeof(cell)) == 0)
        !           239:                        krb_afslog(cell, 0);
        !           240:                krb_afslog(0, 0);
        !           241:        }
        !           242: }
        !           243:
        !           244: void
1.1       deraadt   245: kdestroy()
                    246: {
                    247:         char *file = krbtkfile_env;
                    248:        int i, fd;
                    249:        extern int errno;
                    250:        struct stat statb;
                    251:        char buf[BUFSIZ];
                    252: #ifdef TKT_SHMEM
                    253:        char shmidname[MAXPATHLEN];
                    254: #endif /* TKT_SHMEM */
1.6     ! art       255:
        !           256:        if (k_hasafs())
        !           257:            k_unlog();
1.1       deraadt   258:
                    259:        if (krbtkfile_env == NULL)
                    260:            return;
                    261:
                    262:        errno = 0;
                    263:        if (lstat(file, &statb) < 0)
                    264:            goto out;
                    265:
                    266:        if (!(statb.st_mode & S_IFREG)
                    267: #ifdef notdef
                    268:            || statb.st_mode & 077
                    269: #endif
                    270:            )
                    271:                goto out;
                    272:
                    273:        if ((fd = open(file, O_RDWR, 0)) < 0)
                    274:            goto out;
                    275:
                    276:        bzero(buf, BUFSIZ);
                    277:
                    278:        for (i = 0; i < statb.st_size; i += BUFSIZ)
                    279:            if (write(fd, buf, BUFSIZ) != BUFSIZ) {
                    280:                (void) fsync(fd);
                    281:                (void) close(fd);
                    282:                goto out;
                    283:            }
                    284:
                    285:        (void) fsync(fd);
                    286:        (void) close(fd);
                    287:
                    288:        (void) unlink(file);
                    289:
                    290: out:
                    291:        if (errno != 0) return;
                    292: #ifdef TKT_SHMEM
                    293:        /*
                    294:         * handle the shared memory case
                    295:         */
                    296:        (void) strcpy(shmidname, file);
                    297:        (void) strcat(shmidname, ".shm");
                    298:        if (krb_shm_dest(shmidname) != KSUCCESS)
                    299:            return;
                    300: #endif /* TKT_SHMEM */
                    301:        return;
                    302: }
                    303: #endif