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

1.72    ! djm         1: /* $OpenBSD: auth1.c,v 1.71 2007/09/21 08:15:29 djm Exp $ */
1.1       markus      2: /*
                      3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
1.4       deraadt     5:  *
                      6:  * As far as I am concerned, the code I have written for this software
                      7:  * can be used freely for any purpose.  Any derived versions of this
                      8:  * software must be clearly marked as such, and if the derived work is
                      9:  * incompatible with the protocol description in the RFC file, it must be
                     10:  * called by a name other than "ssh" or "Secure Shell".
1.1       markus     11:  */
                     12:
1.67      stevesk    13: #include <sys/types.h>
1.72    ! djm        14: #include <sys/queue.h>
1.67      stevesk    15:
1.69      stevesk    16: #include <stdio.h>
1.68      stevesk    17: #include <string.h>
1.67      stevesk    18: #include <unistd.h>
1.70      deraadt    19: #include <pwd.h>
1.1       markus     20:
                     21: #include "xmalloc.h"
                     22: #include "rsa.h"
1.12      markus     23: #include "ssh1.h"
1.1       markus     24: #include "packet.h"
                     25: #include "buffer.h"
1.13      markus     26: #include "log.h"
1.1       markus     27: #include "servconf.h"
                     28: #include "compat.h"
1.70      deraadt    29: #include "key.h"
                     30: #include "hostfile.h"
1.1       markus     31: #include "auth.h"
1.35      markus     32: #include "channels.h"
1.1       markus     33: #include "session.h"
1.25      dugsong    34: #include "uidswap.h"
1.70      deraadt    35: #ifdef GSSAPI
                     36: #include "ssh-gss.h"
                     37: #endif
1.38      provos     38: #include "monitor_wrap.h"
1.1       markus     39:
                     40: /* import */
                     41: extern ServerOptions options;
                     42:
1.60      djm        43: static int auth1_process_password(Authctxt *, char *, size_t);
                     44: static int auth1_process_rsa(Authctxt *, char *, size_t);
                     45: static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
                     46: static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
                     47: static int auth1_process_tis_response(Authctxt *, char *, size_t);
                     48:
                     49: struct AuthMethod1 {
                     50:        int type;
                     51:        char *name;
                     52:        int *enabled;
                     53:        int (*method)(Authctxt *, char *, size_t);
                     54: };
                     55:
                     56: const struct AuthMethod1 auth1_methods[] = {
                     57:        {
                     58:                SSH_CMSG_AUTH_PASSWORD, "password",
                     59:                &options.password_authentication, auth1_process_password
                     60:        },
                     61:        {
                     62:                SSH_CMSG_AUTH_RSA, "rsa",
                     63:                &options.rsa_authentication, auth1_process_rsa
                     64:        },
                     65:        {
                     66:                SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
                     67:                &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
                     68:        },
                     69:        {
                     70:                SSH_CMSG_AUTH_TIS, "challenge-response",
                     71:                &options.challenge_response_authentication,
                     72:                auth1_process_tis_challenge
                     73:        },
                     74:        {
                     75:                SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
                     76:                &options.challenge_response_authentication,
                     77:                auth1_process_tis_response
                     78:        },
                     79:        { -1, NULL, NULL, NULL}
                     80: };
                     81:
                     82: static const struct AuthMethod1
                     83: *lookup_authmethod1(int type)
                     84: {
                     85:        int i;
                     86:
1.64      deraadt    87:        for (i = 0; auth1_methods[i].name != NULL; i++)
1.60      djm        88:                if (auth1_methods[i].type == type)
                     89:                        return (&(auth1_methods[i]));
                     90:
                     91:        return (NULL);
                     92: }
                     93:
1.24      itojun     94: static char *
1.1       markus     95: get_authname(int type)
                     96: {
1.60      djm        97:        const struct AuthMethod1 *a;
                     98:        static char buf[64];
                     99:
                    100:        if ((a = lookup_authmethod1(type)) != NULL)
                    101:                return (a->name);
                    102:        snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
                    103:        return (buf);
                    104: }
                    105:
1.65      deraadt   106: /*ARGSUSED*/
1.60      djm       107: static int
                    108: auth1_process_password(Authctxt *authctxt, char *info, size_t infolen)
                    109: {
                    110:        int authenticated = 0;
                    111:        char *password;
                    112:        u_int dlen;
                    113:
                    114:        /*
                    115:         * Read user password.  It is in plain text, but was
                    116:         * transmitted over the encrypted channel so it is
                    117:         * not visible to an outside observer.
                    118:         */
                    119:        password = packet_get_string(&dlen);
                    120:        packet_check_eom();
                    121:
                    122:        /* Try authentication with the password. */
                    123:        authenticated = PRIVSEP(auth_password(authctxt, password));
                    124:
                    125:        memset(password, 0, dlen);
                    126:        xfree(password);
                    127:
                    128:        return (authenticated);
                    129: }
                    130:
1.65      deraadt   131: /*ARGSUSED*/
1.60      djm       132: static int
                    133: auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
                    134: {
                    135:        int authenticated = 0;
                    136:        BIGNUM *n;
                    137:
                    138:        /* RSA authentication requested. */
                    139:        if ((n = BN_new()) == NULL)
                    140:                fatal("do_authloop: BN_new failed");
                    141:        packet_get_bignum(n);
                    142:        packet_check_eom();
                    143:        authenticated = auth_rsa(authctxt, n);
                    144:        BN_clear_free(n);
                    145:
                    146:        return (authenticated);
                    147: }
                    148:
1.65      deraadt   149: /*ARGSUSED*/
1.60      djm       150: static int
                    151: auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
                    152: {
1.61      djm       153:        int keybits, authenticated = 0;
1.60      djm       154:        u_int bits;
                    155:        char *client_user;
                    156:        Key *client_host_key;
                    157:        u_int ulen;
                    158:
                    159:        /*
                    160:         * Get client user name.  Note that we just have to
                    161:         * trust the client; root on the client machine can
                    162:         * claim to be any user.
                    163:         */
                    164:        client_user = packet_get_string(&ulen);
                    165:
                    166:        /* Get the client host key. */
                    167:        client_host_key = key_new(KEY_RSA1);
                    168:        bits = packet_get_int();
                    169:        packet_get_bignum(client_host_key->rsa->e);
                    170:        packet_get_bignum(client_host_key->rsa->n);
                    171:
1.61      djm       172:        keybits = BN_num_bits(client_host_key->rsa->n);
                    173:        if (keybits < 0 || bits != (u_int)keybits) {
1.60      djm       174:                verbose("Warning: keysize mismatch for client_host_key: "
                    175:                    "actual %d, announced %d",
                    176:                    BN_num_bits(client_host_key->rsa->n), bits);
1.1       markus    177:        }
1.60      djm       178:        packet_check_eom();
                    179:
                    180:        authenticated = auth_rhosts_rsa(authctxt, client_user,
                    181:            client_host_key);
                    182:        key_free(client_host_key);
                    183:
                    184:        snprintf(info, infolen, " ruser %.100s", client_user);
                    185:        xfree(client_user);
1.62      djm       186:
1.60      djm       187:        return (authenticated);
                    188: }
                    189:
1.65      deraadt   190: /*ARGSUSED*/
1.60      djm       191: static int
                    192: auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
                    193: {
                    194:        char *challenge;
1.62      djm       195:
1.60      djm       196:        if ((challenge = get_challenge(authctxt)) == NULL)
                    197:                return (0);
                    198:
                    199:        debug("sending challenge '%s'", challenge);
                    200:        packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
                    201:        packet_put_cstring(challenge);
                    202:        xfree(challenge);
                    203:        packet_send();
                    204:        packet_write_wait();
                    205:
                    206:        return (-1);
                    207: }
                    208:
1.65      deraadt   209: /*ARGSUSED*/
1.60      djm       210: static int
                    211: auth1_process_tis_response(Authctxt *authctxt, char *info, size_t infolen)
                    212: {
                    213:        int authenticated = 0;
                    214:        char *response;
                    215:        u_int dlen;
                    216:
                    217:        response = packet_get_string(&dlen);
                    218:        packet_check_eom();
                    219:        authenticated = verify_response(authctxt, response);
                    220:        memset(response, 'r', dlen);
                    221:        xfree(response);
                    222:
                    223:        return (authenticated);
1.1       markus    224: }
                    225:
                    226: /*
1.11      markus    227:  * read packets, try to authenticate the user and
                    228:  * return only if authentication is successful
1.1       markus    229:  */
1.24      itojun    230: static void
1.11      markus    231: do_authloop(Authctxt *authctxt)
1.1       markus    232: {
1.5       markus    233:        int authenticated = 0;
1.11      markus    234:        char info[1024];
1.1       markus    235:        int type = 0;
1.60      djm       236:        const struct AuthMethod1 *meth;
1.11      markus    237:
                    238:        debug("Attempting authentication for %s%.100s.",
1.59      markus    239:            authctxt->valid ? "" : "invalid user ", authctxt->user);
1.11      markus    240:
                    241:        /* If the user has no password, accept authentication immediately. */
                    242:        if (options.password_authentication &&
1.49      markus    243: #ifdef KRB5
1.11      markus    244:            (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
                    245: #endif
1.38      provos    246:            PRIVSEP(auth_password(authctxt, ""))) {
1.11      markus    247:                auth_log(authctxt, 1, "without authentication", "");
                    248:                return;
                    249:        }
1.27      deraadt   250:
1.1       markus    251:        /* Indicate that authentication is needed. */
                    252:        packet_start(SSH_SMSG_FAILURE);
                    253:        packet_send();
                    254:        packet_write_wait();
                    255:
1.11      markus    256:        for (;;) {
1.5       markus    257:                /* default to fail */
                    258:                authenticated = 0;
                    259:
1.11      markus    260:                info[0] = '\0';
1.1       markus    261:
                    262:                /* Get a packet from the client. */
1.34      markus    263:                type = packet_read();
1.60      djm       264:                if ((meth = lookup_authmethod1(type)) == NULL) {
                    265:                        logit("Unknown message during authentication: "
                    266:                            "type %d", type);
                    267:                        goto skip;
                    268:                }
1.1       markus    269:
1.60      djm       270:                if (!*(meth->enabled)) {
                    271:                        verbose("%s authentication disabled.", meth->name);
                    272:                        goto skip;
1.1       markus    273:                }
1.60      djm       274:
                    275:                authenticated = meth->method(authctxt, info, sizeof(info));
                    276:                if (authenticated == -1)
                    277:                        continue; /* "postponed" */
                    278:
1.20      markus    279:                if (authctxt->as) {
                    280:                        auth_close(authctxt->as);
                    281:                        authctxt->as = NULL;
                    282:                }
1.11      markus    283:                if (!authctxt->valid && authenticated)
                    284:                        fatal("INTERNAL ERROR: authenticated invalid user %s",
                    285:                            authctxt->user);
                    286:
                    287:                /* Special handling for root */
1.47      markus    288:                if (authenticated && authctxt->pw->pw_uid == 0 &&
1.60      djm       289:                    !auth_root_allowed(meth->name))
1.11      markus    290:                        authenticated = 0;
1.1       markus    291:
1.60      djm       292:  skip:
1.11      markus    293:                /* Log before sending the reply */
                    294:                auth_log(authctxt, authenticated, get_authname(type), info);
1.1       markus    295:
                    296:                if (authenticated)
                    297:                        return;
                    298:
1.57      dtucker   299:                if (authctxt->failures++ > options.max_authtries)
1.11      markus    300:                        packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
1.1       markus    301:
                    302:                packet_start(SSH_SMSG_FAILURE);
                    303:                packet_send();
                    304:                packet_write_wait();
                    305:        }
                    306: }
                    307:
                    308: /*
                    309:  * Performs authentication of an incoming connection.  Session key has already
                    310:  * been exchanged and encryption is enabled.
                    311:  */
1.53      markus    312: void
                    313: do_authentication(Authctxt *authctxt)
1.1       markus    314: {
1.8       markus    315:        u_int ulen;
1.40      markus    316:        char *user, *style = NULL;
1.1       markus    317:
                    318:        /* Get the name of the user that we wish to log in as. */
1.34      markus    319:        packet_read_expect(SSH_CMSG_USER);
1.1       markus    320:
                    321:        /* Get the user name. */
                    322:        user = packet_get_string(&ulen);
1.32      markus    323:        packet_check_eom();
1.1       markus    324:
1.11      markus    325:        if ((style = strchr(user, ':')) != NULL)
1.25      dugsong   326:                *style++ = '\0';
1.27      deraadt   327:
1.11      markus    328:        authctxt->user = user;
                    329:        authctxt->style = style;
                    330:
1.1       markus    331:        /* Verify that the user is a valid user. */
1.39      markus    332:        if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
1.11      markus    333:                authctxt->valid = 1;
1.51      markus    334:        else {
1.59      markus    335:                debug("do_authentication: invalid user %s", user);
1.51      markus    336:                authctxt->pw = fakepw();
                    337:        }
1.17      markus    338:
1.58      djm       339:        setproctitle("%s%s", authctxt->valid ? user : "unknown",
1.38      provos    340:            use_privsep ? " [net]" : "");
1.1       markus    341:
                    342:        /*
                    343:         * If we are not running as root, the user must have the same uid as
                    344:         * the server.
                    345:         */
1.39      markus    346:        if (!use_privsep && getuid() != 0 && authctxt->pw &&
                    347:            authctxt->pw->pw_uid != getuid())
1.1       markus    348:                packet_disconnect("Cannot change user when server not running as root.");
                    349:
1.11      markus    350:        /*
                    351:         * Loop until the user has been authenticated or the connection is
                    352:         * closed, do_authloop() returns only if authentication is successful
                    353:         */
                    354:        do_authloop(authctxt);
1.1       markus    355:
                    356:        /* The user has been authenticated and accepted. */
                    357:        packet_start(SSH_SMSG_SUCCESS);
                    358:        packet_send();
                    359:        packet_write_wait();
                    360: }