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

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