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

1.4     ! dm          1: /*     $OpenBSD: klogin.c,v 1.3 1996/06/26 05:36:00 deraadt 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.4     ! dm         41: static char rcsid[] = "$OpenBSD: klogin.c,v 1.3 1996/06/26 05:36:00 deraadt Exp $";
1.1       deraadt    42: #endif /* not lint */
                     43:
                     44: #ifdef KERBEROS
                     45: #include <sys/param.h>
                     46: #include <sys/syslog.h>
                     47: #include <kerberosIV/des.h>
                     48: #include <kerberosIV/krb.h>
                     49:
                     50: #include <err.h>
                     51: #include <netdb.h>
                     52: #include <pwd.h>
                     53: #include <stdio.h>
                     54: #include <stdlib.h>
                     55: #include <string.h>
                     56: #include <unistd.h>
                     57:
                     58: #include <sys/types.h>
                     59: #include <sys/stat.h>
                     60: #include <fcntl.h>
                     61:
                     62: #define        INITIAL_TICKET  "krbtgt"
                     63: #define        VERIFY_SERVICE  "rcmd"
                     64:
                     65: extern int notickets;
                     66: extern char *krbtkfile_env;
                     67: extern char *tty;
                     68:
                     69: static char tkt_location[MAXPATHLEN];  /* a pointer to this is returned... */
                     70:
                     71: /*
                     72:  * Attempt to log the user in using Kerberos authentication
                     73:  *
                     74:  * return 0 on success (will be logged in)
                     75:  *       1 if Kerberos failed (try local password in login)
                     76:  */
                     77: int
                     78: klogin(pw, instance, localhost, password)
                     79:        struct passwd *pw;
                     80:        char *instance, *localhost, *password;
                     81: {
                     82:        int kerror;
                     83:        AUTH_DAT authdata;
                     84:        KTEXT_ST ticket;
                     85:        struct hostent *hp;
                     86:        unsigned long faddr;
                     87:        char realm[REALM_SZ], savehost[MAXHOSTNAMELEN];
                     88:        char *krb_get_phost();
                     89:
                     90: #ifdef SKEY
                     91:        /*
                     92:         * We don't do s/key challenge and Kerberos at the same time
                     93:         */
                     94:        if (strcasecmp(password, "s/key") == 0) {
                     95:            return (1);
                     96:        }
                     97: #endif
                     98:
                     99:        /*
1.4     ! dm        100:         * Root logins don't use Kerberos (or at least shouldn't be
        !           101:         * sending kerberos passwords around in cleartext), so don't
        !           102:         * allow any root logins here (keeping in mind that we only
        !           103:         * get here with a password).
        !           104:         *
1.1       deraadt   105:         * If we have a realm, try getting a ticket-granting ticket
                    106:         * and using it to authenticate.  Otherwise, return
                    107:         * failure so that we can try the normal passwd file
                    108:         * for a password.  If that's ok, log the user in
                    109:         * without issuing any tickets.
                    110:         */
1.4     ! dm        111:        if (pw->pw_uid == 0 || krb_get_lrealm(realm, 0) != KSUCCESS)
1.1       deraadt   112:                return (1);
                    113:
                    114:        /*
                    115:         * get TGT for local realm
                    116:         * tickets are stored in a file named TKT_ROOT plus uid plus tty
                    117:         * except for user.root tickets.
                    118:         */
                    119:
                    120:        if (strcmp(instance, "root") != 0)
                    121:                (void)sprintf(tkt_location, "%s%d.%s",
                    122:                              TKT_ROOT, pw->pw_uid, tty);
                    123:        else
                    124:                (void)sprintf(tkt_location, "%s_root_%d.%s",
                    125:                              TKT_ROOT, pw->pw_uid, tty);
                    126:        krbtkfile_env = tkt_location;
                    127:        (void)krb_set_tkt_string(tkt_location);
                    128:
                    129:        /*
                    130:         * Set real as well as effective ID to 0 for the moment,
                    131:         * to make the kerberos library do the right thing.
                    132:         */
                    133:        if (setuid(0) < 0) {
                    134:                warnx("setuid");
                    135:                return (1);
                    136:        }
                    137:        kerror = krb_get_pw_in_tkt(pw->pw_name, instance,
                    138:                    realm, INITIAL_TICKET, realm, DEFAULT_TKT_LIFE, password);
                    139:        /*
                    140:         * If we got a TGT, get a local "rcmd" ticket and check it so as to
                    141:         * ensure that we are not talking to a bogus Kerberos server.
                    142:         *
                    143:         * There are 2 cases where we still allow a login:
                    144:         *      1: the VERIFY_SERVICE doesn't exist in the KDC
                    145:         *      2: local host has no srvtab, as (hopefully) indicated by a
                    146:         *         return value of RD_AP_UNDEC from krb_rd_req().
                    147:         */
                    148:        if (kerror != INTK_OK) {
                    149:                if (kerror != INTK_BADPW && kerror != KDC_PR_UNKNOWN) {
                    150:                        syslog(LOG_ERR, "Kerberos intkt error: %s",
                    151:                            krb_err_txt[kerror]);
                    152:                        dest_tkt();
                    153:                }
                    154:                return (1);
                    155:        }
                    156:
                    157:        if (chown(TKT_FILE, pw->pw_uid, pw->pw_gid) < 0)
                    158:                syslog(LOG_ERR, "chown tkfile (%s): %m", TKT_FILE);
                    159:
                    160:        (void)strncpy(savehost, krb_get_phost(localhost), sizeof(savehost));
                    161:        savehost[sizeof(savehost)-1] = NULL;
                    162:
                    163:        /*
                    164:         * if the "VERIFY_SERVICE" doesn't exist in the KDC for this host,
                    165:         * still allow login with tickets, but log the error condition.
                    166:         */
                    167:
                    168:        kerror = krb_mk_req(&ticket, VERIFY_SERVICE, savehost, realm, 33);
                    169:        if (kerror == KDC_PR_UNKNOWN) {
                    170:                syslog(LOG_NOTICE,
                    171:                    "warning: TGT not verified (%s); %s.%s not registered, or srvtab is wrong?",
                    172:                    krb_err_txt[kerror], VERIFY_SERVICE, savehost);
                    173:                notickets = 0;
                    174:                /*
                    175:                 * but for security, don't allow root instances in under
                    176:                 * this condition!
                    177:                 */
                    178:                if (strcmp(instance, "root") == 0) {
                    179:                  syslog(LOG_ERR, "Kerberos %s root instance login refused\n",
                    180:                         pw->pw_name);
                    181:                  dest_tkt();
                    182:                  return (1);
                    183:                }
1.4     ! dm        184:                /* Otherwise, leave ticket around, but make sure
        !           185:                 * password matches the Unix password. */
        !           186:                return (1);
1.1       deraadt   187:        }
                    188:
                    189:        if (kerror != KSUCCESS) {
                    190:                warnx("unable to use TGT: (%s)", krb_err_txt[kerror]);
                    191:                syslog(LOG_NOTICE, "unable to use TGT: (%s)",
                    192:                    krb_err_txt[kerror]);
                    193:                dest_tkt();
                    194:                return (1);
                    195:        }
                    196:
                    197:        if (!(hp = gethostbyname(localhost))) {
                    198:                syslog(LOG_ERR, "couldn't get local host address");
                    199:                dest_tkt();
                    200:                return (1);
                    201:        }
                    202:
                    203:        memmove((void *)&faddr, (void *)hp->h_addr, sizeof(faddr));
                    204:
                    205:        kerror = krb_rd_req(&ticket, VERIFY_SERVICE, savehost, faddr,
                    206:            &authdata, "");
                    207:
                    208:        if (kerror == KSUCCESS) {
                    209:                notickets = 0;
                    210:                return (0);
                    211:        }
                    212:
                    213:        /* undecipherable: probably didn't have a srvtab on the local host */
1.2       deraadt   214:        if (kerror == RD_AP_UNDEC) {
1.1       deraadt   215:                syslog(LOG_NOTICE, "krb_rd_req: (%s)\n", krb_err_txt[kerror]);
                    216:                dest_tkt();
                    217:                return (1);
                    218:        }
                    219:        /* failed for some other reason */
                    220:        warnx("unable to verify %s ticket: (%s)", VERIFY_SERVICE,
                    221:            krb_err_txt[kerror]);
                    222:        syslog(LOG_NOTICE, "couldn't verify %s ticket: %s", VERIFY_SERVICE,
                    223:            krb_err_txt[kerror]);
                    224:        dest_tkt();
                    225:        return (1);
                    226: }
                    227:
                    228: void
                    229: kdestroy()
                    230: {
                    231:         char *file = krbtkfile_env;
                    232:        int i, fd;
                    233:        extern int errno;
                    234:        struct stat statb;
                    235:        char buf[BUFSIZ];
                    236: #ifdef TKT_SHMEM
                    237:        char shmidname[MAXPATHLEN];
                    238: #endif /* TKT_SHMEM */
                    239:
                    240:        if (krbtkfile_env == NULL)
                    241:            return;
                    242:
                    243:        errno = 0;
                    244:        if (lstat(file, &statb) < 0)
                    245:            goto out;
                    246:
                    247:        if (!(statb.st_mode & S_IFREG)
                    248: #ifdef notdef
                    249:            || statb.st_mode & 077
                    250: #endif
                    251:            )
                    252:                goto out;
                    253:
                    254:        if ((fd = open(file, O_RDWR, 0)) < 0)
                    255:            goto out;
                    256:
                    257:        bzero(buf, BUFSIZ);
                    258:
                    259:        for (i = 0; i < statb.st_size; i += BUFSIZ)
                    260:            if (write(fd, buf, BUFSIZ) != BUFSIZ) {
                    261:                (void) fsync(fd);
                    262:                (void) close(fd);
                    263:                goto out;
                    264:            }
                    265:
                    266:        (void) fsync(fd);
                    267:        (void) close(fd);
                    268:
                    269:        (void) unlink(file);
                    270:
                    271: out:
                    272:        if (errno != 0) return;
                    273: #ifdef TKT_SHMEM
                    274:        /*
                    275:         * handle the shared memory case
                    276:         */
                    277:        (void) strcpy(shmidname, file);
                    278:        (void) strcat(shmidname, ".shm");
                    279:        if (krb_shm_dest(shmidname) != KSUCCESS)
                    280:            return;
                    281: #endif /* TKT_SHMEM */
                    282:        return;
                    283: }
                    284: #endif