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

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.60    ! djm        13: RCSID("$OpenBSD: auth1.c,v 1.59 2004/07/28 09:40:29 markus 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"
1.13      markus     20: #include "log.h"
1.1       markus     21: #include "servconf.h"
                     22: #include "compat.h"
                     23: #include "auth.h"
1.35      markus     24: #include "channels.h"
1.1       markus     25: #include "session.h"
1.25      dugsong    26: #include "uidswap.h"
1.38      provos     27: #include "monitor_wrap.h"
1.1       markus     28:
                     29: /* import */
                     30: extern ServerOptions options;
                     31:
1.60    ! djm        32: static int auth1_process_password(Authctxt *, char *, size_t);
        !            33: static int auth1_process_rsa(Authctxt *, char *, size_t);
        !            34: static int auth1_process_rhosts_rsa(Authctxt *, char *, size_t);
        !            35: static int auth1_process_tis_challenge(Authctxt *, char *, size_t);
        !            36: static int auth1_process_tis_response(Authctxt *, char *, size_t);
        !            37:
        !            38: struct AuthMethod1 {
        !            39:        int type;
        !            40:        char *name;
        !            41:        int *enabled;
        !            42:        int (*method)(Authctxt *, char *, size_t);
        !            43: };
        !            44:
        !            45: const struct AuthMethod1 auth1_methods[] = {
        !            46:        {
        !            47:                SSH_CMSG_AUTH_PASSWORD, "password",
        !            48:                &options.password_authentication, auth1_process_password
        !            49:        },
        !            50:        {
        !            51:                SSH_CMSG_AUTH_RSA, "rsa",
        !            52:                &options.rsa_authentication, auth1_process_rsa
        !            53:        },
        !            54:        {
        !            55:                SSH_CMSG_AUTH_RHOSTS_RSA, "rhosts-rsa",
        !            56:                &options.rhosts_rsa_authentication, auth1_process_rhosts_rsa
        !            57:        },
        !            58:        {
        !            59:                SSH_CMSG_AUTH_TIS, "challenge-response",
        !            60:                &options.challenge_response_authentication,
        !            61:                auth1_process_tis_challenge
        !            62:        },
        !            63:        {
        !            64:                SSH_CMSG_AUTH_TIS_RESPONSE, "challenge-response",
        !            65:                &options.challenge_response_authentication,
        !            66:                auth1_process_tis_response
        !            67:        },
        !            68:        { -1, NULL, NULL, NULL}
        !            69: };
        !            70:
        !            71: static const struct AuthMethod1
        !            72: *lookup_authmethod1(int type)
        !            73: {
        !            74:        int i;
        !            75:
        !            76:        for(i = 0; auth1_methods[i].name != NULL; i++)
        !            77:                if (auth1_methods[i].type == type)
        !            78:                        return (&(auth1_methods[i]));
        !            79:
        !            80:        return (NULL);
        !            81: }
        !            82:
1.24      itojun     83: static char *
1.1       markus     84: get_authname(int type)
                     85: {
1.60    ! djm        86:        const struct AuthMethod1 *a;
        !            87:        static char buf[64];
        !            88:
        !            89:        if ((a = lookup_authmethod1(type)) != NULL)
        !            90:                return (a->name);
        !            91:        snprintf(buf, sizeof(buf), "bad-auth-msg-%d", type);
        !            92:        return (buf);
        !            93: }
        !            94:
        !            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:
        !           119: static int
        !           120: auth1_process_rsa(Authctxt *authctxt, char *info, size_t infolen)
        !           121: {
        !           122:        int authenticated = 0;
        !           123:        BIGNUM *n;
        !           124:
        !           125:        /* RSA authentication requested. */
        !           126:        if ((n = BN_new()) == NULL)
        !           127:                fatal("do_authloop: BN_new failed");
        !           128:        packet_get_bignum(n);
        !           129:        packet_check_eom();
        !           130:        authenticated = auth_rsa(authctxt, n);
        !           131:        BN_clear_free(n);
        !           132:
        !           133:        return (authenticated);
        !           134: }
        !           135:
        !           136: static int
        !           137: auth1_process_rhosts_rsa(Authctxt *authctxt, char *info, size_t infolen)
        !           138: {
        !           139:        int authenticated = 0;
        !           140:        u_int bits;
        !           141:        char *client_user;
        !           142:        Key *client_host_key;
        !           143:        u_int ulen;
        !           144:
        !           145:        /*
        !           146:         * Get client user name.  Note that we just have to
        !           147:         * trust the client; root on the client machine can
        !           148:         * claim to be any user.
        !           149:         */
        !           150:        client_user = packet_get_string(&ulen);
        !           151:
        !           152:        /* Get the client host key. */
        !           153:        client_host_key = key_new(KEY_RSA1);
        !           154:        bits = packet_get_int();
        !           155:        packet_get_bignum(client_host_key->rsa->e);
        !           156:        packet_get_bignum(client_host_key->rsa->n);
        !           157:
        !           158:        if (bits != BN_num_bits(client_host_key->rsa->n)) {
        !           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);
        !           171:
        !           172:        return (authenticated);
        !           173: }
        !           174:
        !           175: static int
        !           176: auth1_process_tis_challenge(Authctxt *authctxt, char *info, size_t infolen)
        !           177: {
        !           178:        char *challenge;
        !           179:
        !           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: }