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

Annotation of src/usr.bin/ssh/auth1.c, Revision 1.25

1.1       markus      1: /*
                      2:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      3:  *                    All rights reserved
1.4       deraadt     4:  *
                      5:  * As far as I am concerned, the code I have written for this software
                      6:  * can be used freely for any purpose.  Any derived versions of this
                      7:  * software must be clearly marked as such, and if the derived work is
                      8:  * incompatible with the protocol description in the RFC file, it must be
                      9:  * called by a name other than "ssh" or "Secure Shell".
1.1       markus     10:  */
                     11:
                     12: #include "includes.h"
1.25    ! dugsong    13: RCSID("$OpenBSD: auth1.c,v 1.24 2001/06/23 15:12:17 itojun Exp $");
1.1       markus     14:
                     15: #include "xmalloc.h"
                     16: #include "rsa.h"
1.12      markus     17: #include "ssh1.h"
1.1       markus     18: #include "packet.h"
                     19: #include "buffer.h"
                     20: #include "mpaux.h"
1.13      markus     21: #include "log.h"
1.1       markus     22: #include "servconf.h"
                     23: #include "compat.h"
                     24: #include "auth.h"
                     25: #include "session.h"
1.18      markus     26: #include "misc.h"
1.25    ! dugsong    27: #include "uidswap.h"
1.1       markus     28:
                     29: /* import */
                     30: extern ServerOptions options;
                     31:
                     32: /*
                     33:  * convert ssh auth msg type into description
                     34:  */
1.24      itojun     35: static char *
1.1       markus     36: get_authname(int type)
                     37: {
                     38:        static char buf[1024];
                     39:        switch (type) {
                     40:        case SSH_CMSG_AUTH_PASSWORD:
                     41:                return "password";
                     42:        case SSH_CMSG_AUTH_RSA:
                     43:                return "rsa";
                     44:        case SSH_CMSG_AUTH_RHOSTS_RSA:
                     45:                return "rhosts-rsa";
                     46:        case SSH_CMSG_AUTH_RHOSTS:
                     47:                return "rhosts";
1.11      markus     48:        case SSH_CMSG_AUTH_TIS:
                     49:        case SSH_CMSG_AUTH_TIS_RESPONSE:
                     50:                return "challenge-response";
1.25    ! dugsong    51: #if defined(KRB4) || defined(KRB5)
1.1       markus     52:        case SSH_CMSG_AUTH_KERBEROS:
                     53:                return "kerberos";
                     54: #endif
                     55:        }
                     56:        snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
                     57:        return buf;
                     58: }
                     59:
                     60: /*
1.11      markus     61:  * read packets, try to authenticate the user and
                     62:  * return only if authentication is successful
1.1       markus     63:  */
1.24      itojun     64: static void
1.11      markus     65: do_authloop(Authctxt *authctxt)
1.1       markus     66: {
1.5       markus     67:        int authenticated = 0;
1.8       markus     68:        u_int bits;
1.1       markus     69:        RSA *client_host_key;
                     70:        BIGNUM *n;
                     71:        char *client_user, *password;
1.11      markus     72:        char info[1024];
1.8       markus     73:        u_int dlen;
1.1       markus     74:        int plen, nlen, elen;
1.8       markus     75:        u_int ulen;
1.1       markus     76:        int type = 0;
1.11      markus     77:        struct passwd *pw = authctxt->pw;
                     78:
                     79:        debug("Attempting authentication for %s%.100s.",
                     80:             authctxt->valid ? "" : "illegal user ", authctxt->user);
                     81:
                     82:        /* If the user has no password, accept authentication immediately. */
                     83:        if (options.password_authentication &&
1.25    ! dugsong    84: #if defined(KRB4) || defined(KRB5)
1.11      markus     85:            (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
                     86: #endif
1.20      markus     87:            auth_password(authctxt, "")) {
1.11      markus     88:                auth_log(authctxt, 1, "without authentication", "");
                     89:                return;
                     90:        }
1.25    ! dugsong    91:
1.1       markus     92:        /* Indicate that authentication is needed. */
                     93:        packet_start(SSH_SMSG_FAILURE);
                     94:        packet_send();
                     95:        packet_write_wait();
                     96:
1.11      markus     97:        for (;;) {
1.5       markus     98:                /* default to fail */
                     99:                authenticated = 0;
                    100:
1.11      markus    101:                info[0] = '\0';
1.1       markus    102:
                    103:                /* Get a packet from the client. */
                    104:                type = packet_read(&plen);
                    105:
                    106:                /* Process the packet. */
                    107:                switch (type) {
                    108:
1.25    ! dugsong   109: #if defined(KRB4) || defined(KRB5)
1.1       markus    110:                case SSH_CMSG_AUTH_KERBEROS:
                    111:                        if (!options.kerberos_authentication) {
                    112:                                verbose("Kerberos authentication disabled.");
                    113:                        } else {
1.25    ! dugsong   114:                                char *kdata = packet_get_string(&dlen);
        !           115:
        !           116:                                packet_integrity_check(plen, 4 + dlen, type);
        !           117:
        !           118:                                if (kdata[0] == 4) { /* KRB_PROT_VERSION */
        !           119: #ifdef KRB4
        !           120:                                        KTEXT_ST tkt;
        !           121:
        !           122:                                        tkt.length = dlen;
        !           123:                                        if (tkt.length < MAX_KTXT_LEN)
        !           124:                                                memcpy(tkt.dat, kdata, tkt.length);
        !           125:
        !           126:                                        if (auth_krb4(authctxt, &tkt, &client_user)) {
        !           127:                                                authenticated = 1;
        !           128:                                                snprintf(info, sizeof(info),
        !           129:                                                    " tktuser %.100s",
        !           130:                                                    client_user);
        !           131:                                                xfree(client_user);
1.5       markus    132:                                        }
1.25    ! dugsong   133: #endif /* KRB4 */
        !           134:                                } else {
        !           135: #ifdef KRB5
        !           136:                                        krb5_data tkt;
        !           137:                                        tkt.length = dlen;
        !           138:                                        tkt.data = kdata;
        !           139:
        !           140:                                        if (auth_krb5(authctxt, &tkt, &client_user)) {
        !           141:                                                authenticated = 1;
        !           142:                                                snprintf(info, sizeof(info),
        !           143:                                                    " tktuser %.100s",
        !           144:                                                    client_user);
        !           145:                                                xfree(client_user);
        !           146:                                        }
        !           147: #endif /* KRB5 */
1.1       markus    148:                                }
1.11      markus    149:                                xfree(kdata);
1.1       markus    150:                        }
                    151:                        break;
1.25    ! dugsong   152: #endif /* KRB4 || KRB5 */
        !           153:
        !           154: #if defined(AFS) || defined(KRB5)
        !           155:                        /* XXX - punt on backward compatibility here. */
        !           156:                case SSH_CMSG_HAVE_KERBEROS_TGT:
        !           157:                        packet_send_debug("Kerberos TGT passing disabled before authentication.");
        !           158:                        break;
        !           159: #ifdef AFS
        !           160:                case SSH_CMSG_HAVE_AFS_TOKEN:
        !           161:                        packet_send_debug("AFS token passing disabled before authentication.");
        !           162:                        break;
        !           163: #endif /* AFS */
        !           164: #endif /* AFS || KRB5 */
        !           165:
1.1       markus    166:                case SSH_CMSG_AUTH_RHOSTS:
                    167:                        if (!options.rhosts_authentication) {
                    168:                                verbose("Rhosts authentication disabled.");
                    169:                                break;
                    170:                        }
                    171:                        /*
                    172:                         * Get client user name.  Note that we just have to
                    173:                         * trust the client; this is one reason why rhosts
                    174:                         * authentication is insecure. (Another is
                    175:                         * IP-spoofing on a local network.)
                    176:                         */
                    177:                        client_user = packet_get_string(&ulen);
                    178:                        packet_integrity_check(plen, 4 + ulen, type);
                    179:
1.5       markus    180:                        /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
1.1       markus    181:                        authenticated = auth_rhosts(pw, client_user);
                    182:
1.11      markus    183:                        snprintf(info, sizeof info, " ruser %.100s", client_user);
1.1       markus    184:                        xfree(client_user);
                    185:                        break;
                    186:
                    187:                case SSH_CMSG_AUTH_RHOSTS_RSA:
                    188:                        if (!options.rhosts_rsa_authentication) {
                    189:                                verbose("Rhosts with RSA authentication disabled.");
                    190:                                break;
                    191:                        }
                    192:                        /*
                    193:                         * Get client user name.  Note that we just have to
                    194:                         * trust the client; root on the client machine can
                    195:                         * claim to be any user.
                    196:                         */
                    197:                        client_user = packet_get_string(&ulen);
                    198:
                    199:                        /* Get the client host key. */
                    200:                        client_host_key = RSA_new();
                    201:                        if (client_host_key == NULL)
                    202:                                fatal("RSA_new failed");
                    203:                        client_host_key->e = BN_new();
                    204:                        client_host_key->n = BN_new();
                    205:                        if (client_host_key->e == NULL || client_host_key->n == NULL)
                    206:                                fatal("BN_new failed");
                    207:                        bits = packet_get_int();
                    208:                        packet_get_bignum(client_host_key->e, &elen);
                    209:                        packet_get_bignum(client_host_key->n, &nlen);
                    210:
                    211:                        if (bits != BN_num_bits(client_host_key->n))
1.5       markus    212:                                verbose("Warning: keysize mismatch for client_host_key: "
1.2       markus    213:                                    "actual %d, announced %d", BN_num_bits(client_host_key->n), bits);
1.1       markus    214:                        packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type);
                    215:
                    216:                        authenticated = auth_rhosts_rsa(pw, client_user, client_host_key);
                    217:                        RSA_free(client_host_key);
                    218:
1.11      markus    219:                        snprintf(info, sizeof info, " ruser %.100s", client_user);
1.1       markus    220:                        xfree(client_user);
                    221:                        break;
                    222:
                    223:                case SSH_CMSG_AUTH_RSA:
                    224:                        if (!options.rsa_authentication) {
                    225:                                verbose("RSA authentication disabled.");
                    226:                                break;
                    227:                        }
                    228:                        /* RSA authentication requested. */
                    229:                        n = BN_new();
                    230:                        packet_get_bignum(n, &nlen);
                    231:                        packet_integrity_check(plen, nlen, type);
                    232:                        authenticated = auth_rsa(pw, n);
                    233:                        BN_clear_free(n);
                    234:                        break;
                    235:
                    236:                case SSH_CMSG_AUTH_PASSWORD:
                    237:                        if (!options.password_authentication) {
                    238:                                verbose("Password authentication disabled.");
                    239:                                break;
                    240:                        }
                    241:                        /*
                    242:                         * Read user password.  It is in plain text, but was
                    243:                         * transmitted over the encrypted channel so it is
                    244:                         * not visible to an outside observer.
                    245:                         */
                    246:                        password = packet_get_string(&dlen);
                    247:                        packet_integrity_check(plen, 4 + dlen, type);
                    248:
                    249:                        /* Try authentication with the password. */
1.20      markus    250:                        authenticated = auth_password(authctxt, password);
1.1       markus    251:
                    252:                        memset(password, 0, strlen(password));
                    253:                        xfree(password);
                    254:                        break;
                    255:
                    256:                case SSH_CMSG_AUTH_TIS:
                    257:                        debug("rcvd SSH_CMSG_AUTH_TIS");
1.23      markus    258:                        if (options.challenge_response_authentication == 1) {
                    259:                                char *challenge = get_challenge(authctxt);
1.11      markus    260:                                if (challenge != NULL) {
                    261:                                        debug("sending challenge '%s'", challenge);
1.1       markus    262:                                        packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
1.11      markus    263:                                        packet_put_cstring(challenge);
1.23      markus    264:                                        xfree(challenge);
1.1       markus    265:                                        packet_send();
                    266:                                        packet_write_wait();
                    267:                                        continue;
                    268:                                }
                    269:                        }
                    270:                        break;
                    271:                case SSH_CMSG_AUTH_TIS_RESPONSE:
                    272:                        debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
1.23      markus    273:                        if (options.challenge_response_authentication == 1) {
1.1       markus    274:                                char *response = packet_get_string(&dlen);
1.11      markus    275:                                debug("got response '%s'", response);
1.1       markus    276:                                packet_integrity_check(plen, 4 + dlen, type);
1.11      markus    277:                                authenticated = verify_response(authctxt, response);
                    278:                                memset(response, 'r', dlen);
1.1       markus    279:                                xfree(response);
                    280:                        }
                    281:                        break;
                    282:
                    283:                default:
                    284:                        /*
                    285:                         * Any unknown messages will be ignored (and failure
                    286:                         * returned) during authentication.
                    287:                         */
                    288:                        log("Unknown message during authentication: type %d", type);
                    289:                        break;
                    290:                }
1.20      markus    291: #ifdef BSD_AUTH
                    292:                if (authctxt->as) {
                    293:                        auth_close(authctxt->as);
                    294:                        authctxt->as = NULL;
                    295:                }
                    296: #endif
1.11      markus    297:                if (!authctxt->valid && authenticated)
                    298:                        fatal("INTERNAL ERROR: authenticated invalid user %s",
                    299:                            authctxt->user);
                    300:
                    301:                /* Special handling for root */
1.16      markus    302:                if (authenticated && authctxt->pw->pw_uid == 0 &&
                    303:                    !auth_root_allowed(get_authname(type)))
1.11      markus    304:                        authenticated = 0;
1.1       markus    305:
1.11      markus    306:                /* Log before sending the reply */
                    307:                auth_log(authctxt, authenticated, get_authname(type), info);
1.1       markus    308:
                    309:                if (authenticated)
                    310:                        return;
                    311:
1.11      markus    312:                if (authctxt->failures++ > AUTH_FAIL_MAX)
                    313:                        packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
1.1       markus    314:
                    315:                packet_start(SSH_SMSG_FAILURE);
                    316:                packet_send();
                    317:                packet_write_wait();
                    318:        }
                    319: }
                    320:
                    321: /*
                    322:  * Performs authentication of an incoming connection.  Session key has already
                    323:  * been exchanged and encryption is enabled.
                    324:  */
                    325: void
                    326: do_authentication()
                    327: {
1.11      markus    328:        Authctxt *authctxt;
                    329:        struct passwd *pw;
1.1       markus    330:        int plen;
1.8       markus    331:        u_int ulen;
1.25    ! dugsong   332:        char *p, *user, *style = NULL;
1.1       markus    333:
                    334:        /* Get the name of the user that we wish to log in as. */
                    335:        packet_read_expect(&plen, SSH_CMSG_USER);
                    336:
                    337:        /* Get the user name. */
                    338:        user = packet_get_string(&ulen);
                    339:        packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER);
                    340:
1.11      markus    341:        if ((style = strchr(user, ':')) != NULL)
1.25    ! dugsong   342:                *style++ = '\0';
1.11      markus    343:
1.25    ! dugsong   344:        /* XXX - SSH.com Kerberos v5 braindeath. */
        !           345:        if ((p = strchr(user, '@')) != NULL)
        !           346:                *p = '\0';
        !           347:
1.11      markus    348:        authctxt = authctxt_new();
                    349:        authctxt->user = user;
                    350:        authctxt->style = style;
                    351:
1.1       markus    352:        /* Verify that the user is a valid user. */
                    353:        pw = getpwnam(user);
1.5       markus    354:        if (pw && allowed_user(pw)) {
1.11      markus    355:                authctxt->valid = 1;
                    356:                pw = pwcopy(pw);
1.5       markus    357:        } else {
1.11      markus    358:                debug("do_authentication: illegal user %s", user);
1.5       markus    359:                pw = NULL;
                    360:        }
1.11      markus    361:        authctxt->pw = pw;
1.17      markus    362:
                    363:        setproctitle("%s", pw ? user : "unknown");
1.1       markus    364:
                    365:        /*
                    366:         * If we are not running as root, the user must have the same uid as
                    367:         * the server.
                    368:         */
1.5       markus    369:        if (getuid() != 0 && pw && pw->pw_uid != getuid())
1.1       markus    370:                packet_disconnect("Cannot change user when server not running as root.");
                    371:
1.11      markus    372:        /*
                    373:         * Loop until the user has been authenticated or the connection is
                    374:         * closed, do_authloop() returns only if authentication is successful
                    375:         */
                    376:        do_authloop(authctxt);
1.1       markus    377:
                    378:        /* The user has been authenticated and accepted. */
                    379:        packet_start(SSH_SMSG_SUCCESS);
                    380:        packet_send();
                    381:        packet_write_wait();
                    382:
                    383:        /* Perform session preparation. */
1.21      markus    384:        do_authenticated(authctxt);
1.1       markus    385: }