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

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.54    ! djm        13: RCSID("$OpenBSD: auth1.c,v 1.53 2003/09/23 20:17:11 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"
                     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"
1.35      markus     25: #include "channels.h"
1.1       markus     26: #include "session.h"
1.25      dugsong    27: #include "uidswap.h"
1.38      provos     28: #include "monitor_wrap.h"
1.1       markus     29:
                     30: /* import */
                     31: extern ServerOptions options;
                     32:
                     33: /*
                     34:  * convert ssh auth msg type into description
                     35:  */
1.24      itojun     36: static char *
1.1       markus     37: get_authname(int type)
                     38: {
                     39:        static char buf[1024];
                     40:        switch (type) {
                     41:        case SSH_CMSG_AUTH_PASSWORD:
                     42:                return "password";
                     43:        case SSH_CMSG_AUTH_RSA:
                     44:                return "rsa";
                     45:        case SSH_CMSG_AUTH_RHOSTS_RSA:
                     46:                return "rhosts-rsa";
                     47:        case SSH_CMSG_AUTH_RHOSTS:
                     48:                return "rhosts";
1.11      markus     49:        case SSH_CMSG_AUTH_TIS:
                     50:        case SSH_CMSG_AUTH_TIS_RESPONSE:
                     51:                return "challenge-response";
1.1       markus     52:        }
                     53:        snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
                     54:        return buf;
                     55: }
                     56:
                     57: /*
1.11      markus     58:  * read packets, try to authenticate the user and
                     59:  * return only if authentication is successful
1.1       markus     60:  */
1.24      itojun     61: static void
1.11      markus     62: do_authloop(Authctxt *authctxt)
1.1       markus     63: {
1.5       markus     64:        int authenticated = 0;
1.8       markus     65:        u_int bits;
1.29      markus     66:        Key *client_host_key;
1.1       markus     67:        BIGNUM *n;
                     68:        char *client_user, *password;
1.11      markus     69:        char info[1024];
1.8       markus     70:        u_int dlen;
                     71:        u_int ulen;
1.1       markus     72:        int type = 0;
1.11      markus     73:        struct passwd *pw = authctxt->pw;
                     74:
                     75:        debug("Attempting authentication for %s%.100s.",
1.27      deraadt    76:            authctxt->valid ? "" : "illegal user ", authctxt->user);
1.11      markus     77:
                     78:        /* If the user has no password, accept authentication immediately. */
                     79:        if (options.password_authentication &&
1.49      markus     80: #ifdef KRB5
1.11      markus     81:            (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
                     82: #endif
1.38      provos     83:            PRIVSEP(auth_password(authctxt, ""))) {
1.11      markus     84:                auth_log(authctxt, 1, "without authentication", "");
                     85:                return;
                     86:        }
1.27      deraadt    87:
1.1       markus     88:        /* Indicate that authentication is needed. */
                     89:        packet_start(SSH_SMSG_FAILURE);
                     90:        packet_send();
                     91:        packet_write_wait();
                     92:
1.11      markus     93:        for (;;) {
1.5       markus     94:                /* default to fail */
                     95:                authenticated = 0;
                     96:
1.11      markus     97:                info[0] = '\0';
1.1       markus     98:
                     99:                /* Get a packet from the client. */
1.34      markus    100:                type = packet_read();
1.1       markus    101:
                    102:                /* Process the packet. */
                    103:                switch (type) {
                    104:                case SSH_CMSG_AUTH_RHOSTS_RSA:
                    105:                        if (!options.rhosts_rsa_authentication) {
                    106:                                verbose("Rhosts with RSA authentication disabled.");
                    107:                                break;
                    108:                        }
                    109:                        /*
                    110:                         * Get client user name.  Note that we just have to
                    111:                         * trust the client; root on the client machine can
                    112:                         * claim to be any user.
                    113:                         */
                    114:                        client_user = packet_get_string(&ulen);
                    115:
                    116:                        /* Get the client host key. */
1.29      markus    117:                        client_host_key = key_new(KEY_RSA1);
1.1       markus    118:                        bits = packet_get_int();
1.33      markus    119:                        packet_get_bignum(client_host_key->rsa->e);
                    120:                        packet_get_bignum(client_host_key->rsa->n);
1.1       markus    121:
1.29      markus    122:                        if (bits != BN_num_bits(client_host_key->rsa->n))
1.5       markus    123:                                verbose("Warning: keysize mismatch for client_host_key: "
1.29      markus    124:                                    "actual %d, announced %d",
1.41      deraadt   125:                                    BN_num_bits(client_host_key->rsa->n), bits);
1.32      markus    126:                        packet_check_eom();
1.1       markus    127:
1.54    ! djm       128:                        authenticated = auth_rhosts_rsa(authctxt, client_user,
1.30      markus    129:                            client_host_key);
1.29      markus    130:                        key_free(client_host_key);
1.1       markus    131:
1.11      markus    132:                        snprintf(info, sizeof info, " ruser %.100s", client_user);
1.1       markus    133:                        xfree(client_user);
                    134:                        break;
                    135:
                    136:                case SSH_CMSG_AUTH_RSA:
                    137:                        if (!options.rsa_authentication) {
                    138:                                verbose("RSA authentication disabled.");
                    139:                                break;
                    140:                        }
                    141:                        /* RSA authentication requested. */
1.29      markus    142:                        if ((n = BN_new()) == NULL)
                    143:                                fatal("do_authloop: BN_new failed");
1.33      markus    144:                        packet_get_bignum(n);
1.32      markus    145:                        packet_check_eom();
1.54    ! djm       146:                        authenticated = auth_rsa(authctxt, n);
1.1       markus    147:                        BN_clear_free(n);
                    148:                        break;
                    149:
                    150:                case SSH_CMSG_AUTH_PASSWORD:
                    151:                        if (!options.password_authentication) {
                    152:                                verbose("Password authentication disabled.");
                    153:                                break;
                    154:                        }
                    155:                        /*
                    156:                         * Read user password.  It is in plain text, but was
                    157:                         * transmitted over the encrypted channel so it is
                    158:                         * not visible to an outside observer.
                    159:                         */
                    160:                        password = packet_get_string(&dlen);
1.32      markus    161:                        packet_check_eom();
1.1       markus    162:
                    163:                        /* Try authentication with the password. */
1.38      provos    164:                        authenticated = PRIVSEP(auth_password(authctxt, password));
1.1       markus    165:
                    166:                        memset(password, 0, strlen(password));
                    167:                        xfree(password);
                    168:                        break;
                    169:
                    170:                case SSH_CMSG_AUTH_TIS:
                    171:                        debug("rcvd SSH_CMSG_AUTH_TIS");
1.23      markus    172:                        if (options.challenge_response_authentication == 1) {
                    173:                                char *challenge = get_challenge(authctxt);
1.11      markus    174:                                if (challenge != NULL) {
                    175:                                        debug("sending challenge '%s'", challenge);
1.1       markus    176:                                        packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
1.11      markus    177:                                        packet_put_cstring(challenge);
1.23      markus    178:                                        xfree(challenge);
1.1       markus    179:                                        packet_send();
                    180:                                        packet_write_wait();
                    181:                                        continue;
                    182:                                }
                    183:                        }
                    184:                        break;
                    185:                case SSH_CMSG_AUTH_TIS_RESPONSE:
                    186:                        debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
1.23      markus    187:                        if (options.challenge_response_authentication == 1) {
1.1       markus    188:                                char *response = packet_get_string(&dlen);
1.32      markus    189:                                packet_check_eom();
1.11      markus    190:                                authenticated = verify_response(authctxt, response);
                    191:                                memset(response, 'r', dlen);
1.1       markus    192:                                xfree(response);
                    193:                        }
                    194:                        break;
                    195:
                    196:                default:
                    197:                        /*
                    198:                         * Any unknown messages will be ignored (and failure
                    199:                         * returned) during authentication.
                    200:                         */
1.48      itojun    201:                        logit("Unknown message during authentication: type %d", type);
1.1       markus    202:                        break;
                    203:                }
1.20      markus    204: #ifdef BSD_AUTH
                    205:                if (authctxt->as) {
                    206:                        auth_close(authctxt->as);
                    207:                        authctxt->as = NULL;
                    208:                }
                    209: #endif
1.11      markus    210:                if (!authctxt->valid && authenticated)
                    211:                        fatal("INTERNAL ERROR: authenticated invalid user %s",
                    212:                            authctxt->user);
                    213:
                    214:                /* Special handling for root */
1.47      markus    215:                if (authenticated && authctxt->pw->pw_uid == 0 &&
1.16      markus    216:                    !auth_root_allowed(get_authname(type)))
1.11      markus    217:                        authenticated = 0;
1.1       markus    218:
1.11      markus    219:                /* Log before sending the reply */
                    220:                auth_log(authctxt, authenticated, get_authname(type), info);
1.1       markus    221:
                    222:                if (authenticated)
                    223:                        return;
                    224:
1.11      markus    225:                if (authctxt->failures++ > AUTH_FAIL_MAX)
                    226:                        packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
1.1       markus    227:
                    228:                packet_start(SSH_SMSG_FAILURE);
                    229:                packet_send();
                    230:                packet_write_wait();
                    231:        }
                    232: }
                    233:
                    234: /*
                    235:  * Performs authentication of an incoming connection.  Session key has already
                    236:  * been exchanged and encryption is enabled.
                    237:  */
1.53      markus    238: void
                    239: do_authentication(Authctxt *authctxt)
1.1       markus    240: {
1.8       markus    241:        u_int ulen;
1.40      markus    242:        char *user, *style = NULL;
1.1       markus    243:
                    244:        /* Get the name of the user that we wish to log in as. */
1.34      markus    245:        packet_read_expect(SSH_CMSG_USER);
1.1       markus    246:
                    247:        /* Get the user name. */
                    248:        user = packet_get_string(&ulen);
1.32      markus    249:        packet_check_eom();
1.1       markus    250:
1.11      markus    251:        if ((style = strchr(user, ':')) != NULL)
1.25      dugsong   252:                *style++ = '\0';
1.27      deraadt   253:
1.11      markus    254:        authctxt->user = user;
                    255:        authctxt->style = style;
                    256:
1.1       markus    257:        /* Verify that the user is a valid user. */
1.39      markus    258:        if ((authctxt->pw = PRIVSEP(getpwnamallow(user))) != NULL)
1.11      markus    259:                authctxt->valid = 1;
1.51      markus    260:        else {
1.11      markus    261:                debug("do_authentication: illegal user %s", user);
1.51      markus    262:                authctxt->pw = fakepw();
                    263:        }
1.17      markus    264:
1.39      markus    265:        setproctitle("%s%s", authctxt->pw ? user : "unknown",
1.38      provos    266:            use_privsep ? " [net]" : "");
1.1       markus    267:
                    268:        /*
                    269:         * If we are not running as root, the user must have the same uid as
                    270:         * the server.
                    271:         */
1.39      markus    272:        if (!use_privsep && getuid() != 0 && authctxt->pw &&
                    273:            authctxt->pw->pw_uid != getuid())
1.1       markus    274:                packet_disconnect("Cannot change user when server not running as root.");
                    275:
1.11      markus    276:        /*
                    277:         * Loop until the user has been authenticated or the connection is
                    278:         * closed, do_authloop() returns only if authentication is successful
                    279:         */
                    280:        do_authloop(authctxt);
1.1       markus    281:
                    282:        /* The user has been authenticated and accepted. */
                    283:        packet_start(SSH_SMSG_SUCCESS);
                    284:        packet_send();
                    285:        packet_write_wait();
                    286: }