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

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