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

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.17    ! markus     13: RCSID("$OpenBSD: auth1.c,v 1.16 2001/02/12 16:16:23 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"
                     25: #include "session.h"
                     26:
                     27: /* import */
                     28: extern ServerOptions options;
                     29: extern char *forced_command;
                     30:
                     31: /*
                     32:  * convert ssh auth msg type into description
                     33:  */
                     34: char *
                     35: get_authname(int type)
                     36: {
                     37:        static char buf[1024];
                     38:        switch (type) {
                     39:        case SSH_CMSG_AUTH_PASSWORD:
                     40:                return "password";
                     41:        case SSH_CMSG_AUTH_RSA:
                     42:                return "rsa";
                     43:        case SSH_CMSG_AUTH_RHOSTS_RSA:
                     44:                return "rhosts-rsa";
                     45:        case SSH_CMSG_AUTH_RHOSTS:
                     46:                return "rhosts";
1.11      markus     47:        case SSH_CMSG_AUTH_TIS:
                     48:        case SSH_CMSG_AUTH_TIS_RESPONSE:
                     49:                return "challenge-response";
1.1       markus     50: #ifdef KRB4
                     51:        case SSH_CMSG_AUTH_KERBEROS:
                     52:                return "kerberos";
                     53: #endif
                     54:        }
                     55:        snprintf(buf, sizeof buf, "bad-auth-msg-%d", type);
                     56:        return buf;
                     57: }
                     58:
                     59: /*
1.11      markus     60:  * read packets, try to authenticate the user and
                     61:  * return only if authentication is successful
1.1       markus     62:  */
                     63: void
1.11      markus     64: do_authloop(Authctxt *authctxt)
1.1       markus     65: {
1.5       markus     66:        int authenticated = 0;
1.8       markus     67:        u_int bits;
1.1       markus     68:        RSA *client_host_key;
                     69:        BIGNUM *n;
                     70:        char *client_user, *password;
1.11      markus     71:        char info[1024];
1.8       markus     72:        u_int dlen;
1.1       markus     73:        int plen, nlen, elen;
1.8       markus     74:        u_int ulen;
1.1       markus     75:        int type = 0;
1.11      markus     76:        struct passwd *pw = authctxt->pw;
                     77:
                     78:        debug("Attempting authentication for %s%.100s.",
                     79:             authctxt->valid ? "" : "illegal user ", authctxt->user);
                     80:
                     81:        /* If the user has no password, accept authentication immediately. */
                     82:        if (options.password_authentication &&
                     83: #ifdef KRB4
                     84:            (!options.kerberos_authentication || options.kerberos_or_local_passwd) &&
                     85: #endif
                     86:            auth_password(pw, "")) {
                     87:                auth_log(authctxt, 1, "without authentication", "");
                     88:                return;
                     89:        }
1.1       markus     90:
                     91:        /* Indicate that authentication is needed. */
                     92:        packet_start(SSH_SMSG_FAILURE);
                     93:        packet_send();
                     94:        packet_write_wait();
                     95:
1.11      markus     96:        for (;;) {
1.5       markus     97:                /* default to fail */
                     98:                authenticated = 0;
                     99:
1.11      markus    100:                info[0] = '\0';
1.1       markus    101:
                    102:                /* Get a packet from the client. */
                    103:                type = packet_read(&plen);
                    104:
                    105:                /* Process the packet. */
                    106:                switch (type) {
                    107: #ifdef AFS
                    108:                case SSH_CMSG_HAVE_KERBEROS_TGT:
                    109:                        if (!options.kerberos_tgt_passing) {
                    110:                                verbose("Kerberos tgt passing disabled.");
                    111:                                break;
                    112:                        } else {
                    113:                                /* Accept Kerberos tgt. */
                    114:                                char *tgt = packet_get_string(&dlen);
                    115:                                packet_integrity_check(plen, 4 + dlen, type);
                    116:                                if (!auth_kerberos_tgt(pw, tgt))
1.11      markus    117:                                        verbose("Kerberos tgt REFUSED for %.100s", authctxt->user);
1.1       markus    118:                                xfree(tgt);
                    119:                        }
                    120:                        continue;
                    121:
                    122:                case SSH_CMSG_HAVE_AFS_TOKEN:
                    123:                        if (!options.afs_token_passing || !k_hasafs()) {
                    124:                                verbose("AFS token passing disabled.");
                    125:                                break;
                    126:                        } else {
                    127:                                /* Accept AFS token. */
                    128:                                char *token_string = packet_get_string(&dlen);
                    129:                                packet_integrity_check(plen, 4 + dlen, type);
                    130:                                if (!auth_afs_token(pw, token_string))
1.11      markus    131:                                        verbose("AFS token REFUSED for %.100s", authctxt->user);
1.1       markus    132:                                xfree(token_string);
                    133:                        }
                    134:                        continue;
                    135: #endif /* AFS */
                    136: #ifdef KRB4
                    137:                case SSH_CMSG_AUTH_KERBEROS:
                    138:                        if (!options.kerberos_authentication) {
                    139:                                verbose("Kerberos authentication disabled.");
                    140:                                break;
                    141:                        } else {
                    142:                                /* Try Kerberos v4 authentication. */
                    143:                                KTEXT_ST auth;
                    144:                                char *tkt_user = NULL;
1.8       markus    145:                                char *kdata = packet_get_string((u_int *) &auth.length);
1.1       markus    146:                                packet_integrity_check(plen, 4 + auth.length, type);
                    147:
1.11      markus    148:                                if (authctxt->valid) {
                    149:                                        if (auth.length < MAX_KTXT_LEN)
                    150:                                                memcpy(auth.dat, kdata, auth.length);
1.5       markus    151:                                        authenticated = auth_krb4(pw->pw_name, &auth, &tkt_user);
                    152:                                        if (authenticated) {
1.11      markus    153:                                                snprintf(info, sizeof info,
                    154:                                                    " tktuser %.100s", tkt_user);
1.5       markus    155:                                                xfree(tkt_user);
                    156:                                        }
1.1       markus    157:                                }
1.11      markus    158:                                xfree(kdata);
1.1       markus    159:                        }
                    160:                        break;
                    161: #endif /* KRB4 */
                    162:
                    163:                case SSH_CMSG_AUTH_RHOSTS:
                    164:                        if (!options.rhosts_authentication) {
                    165:                                verbose("Rhosts authentication disabled.");
                    166:                                break;
                    167:                        }
                    168:                        /*
                    169:                         * Get client user name.  Note that we just have to
                    170:                         * trust the client; this is one reason why rhosts
                    171:                         * authentication is insecure. (Another is
                    172:                         * IP-spoofing on a local network.)
                    173:                         */
                    174:                        client_user = packet_get_string(&ulen);
                    175:                        packet_integrity_check(plen, 4 + ulen, type);
                    176:
1.5       markus    177:                        /* Try to authenticate using /etc/hosts.equiv and .rhosts. */
1.1       markus    178:                        authenticated = auth_rhosts(pw, client_user);
                    179:
1.11      markus    180:                        snprintf(info, sizeof info, " ruser %.100s", client_user);
1.1       markus    181:                        xfree(client_user);
                    182:                        break;
                    183:
                    184:                case SSH_CMSG_AUTH_RHOSTS_RSA:
                    185:                        if (!options.rhosts_rsa_authentication) {
                    186:                                verbose("Rhosts with RSA authentication disabled.");
                    187:                                break;
                    188:                        }
                    189:                        /*
                    190:                         * Get client user name.  Note that we just have to
                    191:                         * trust the client; root on the client machine can
                    192:                         * claim to be any user.
                    193:                         */
                    194:                        client_user = packet_get_string(&ulen);
                    195:
                    196:                        /* Get the client host key. */
                    197:                        client_host_key = RSA_new();
                    198:                        if (client_host_key == NULL)
                    199:                                fatal("RSA_new failed");
                    200:                        client_host_key->e = BN_new();
                    201:                        client_host_key->n = BN_new();
                    202:                        if (client_host_key->e == NULL || client_host_key->n == NULL)
                    203:                                fatal("BN_new failed");
                    204:                        bits = packet_get_int();
                    205:                        packet_get_bignum(client_host_key->e, &elen);
                    206:                        packet_get_bignum(client_host_key->n, &nlen);
                    207:
                    208:                        if (bits != BN_num_bits(client_host_key->n))
1.5       markus    209:                                verbose("Warning: keysize mismatch for client_host_key: "
1.2       markus    210:                                    "actual %d, announced %d", BN_num_bits(client_host_key->n), bits);
1.1       markus    211:                        packet_integrity_check(plen, (4 + ulen) + 4 + elen + nlen, type);
                    212:
                    213:                        authenticated = auth_rhosts_rsa(pw, client_user, client_host_key);
                    214:                        RSA_free(client_host_key);
                    215:
1.11      markus    216:                        snprintf(info, sizeof info, " ruser %.100s", client_user);
1.1       markus    217:                        xfree(client_user);
                    218:                        break;
                    219:
                    220:                case SSH_CMSG_AUTH_RSA:
                    221:                        if (!options.rsa_authentication) {
                    222:                                verbose("RSA authentication disabled.");
                    223:                                break;
                    224:                        }
                    225:                        /* RSA authentication requested. */
                    226:                        n = BN_new();
                    227:                        packet_get_bignum(n, &nlen);
                    228:                        packet_integrity_check(plen, nlen, type);
                    229:                        authenticated = auth_rsa(pw, n);
                    230:                        BN_clear_free(n);
                    231:                        break;
                    232:
                    233:                case SSH_CMSG_AUTH_PASSWORD:
                    234:                        if (!options.password_authentication) {
                    235:                                verbose("Password authentication disabled.");
                    236:                                break;
                    237:                        }
                    238:                        /*
                    239:                         * Read user password.  It is in plain text, but was
                    240:                         * transmitted over the encrypted channel so it is
                    241:                         * not visible to an outside observer.
                    242:                         */
                    243:                        password = packet_get_string(&dlen);
                    244:                        packet_integrity_check(plen, 4 + dlen, type);
                    245:
                    246:                        /* Try authentication with the password. */
                    247:                        authenticated = auth_password(pw, password);
                    248:
                    249:                        memset(password, 0, strlen(password));
                    250:                        xfree(password);
                    251:                        break;
                    252:
                    253:                case SSH_CMSG_AUTH_TIS:
                    254:                        debug("rcvd SSH_CMSG_AUTH_TIS");
1.14      markus    255:                        if (options.challenge_reponse_authentication == 1) {
1.11      markus    256:                                char *challenge = get_challenge(authctxt, authctxt->style);
                    257:                                if (challenge != NULL) {
                    258:                                        debug("sending challenge '%s'", challenge);
1.1       markus    259:                                        packet_start(SSH_SMSG_AUTH_TIS_CHALLENGE);
1.11      markus    260:                                        packet_put_cstring(challenge);
1.1       markus    261:                                        packet_send();
                    262:                                        packet_write_wait();
                    263:                                        continue;
                    264:                                }
                    265:                        }
                    266:                        break;
                    267:                case SSH_CMSG_AUTH_TIS_RESPONSE:
                    268:                        debug("rcvd SSH_CMSG_AUTH_TIS_RESPONSE");
1.14      markus    269:                        if (options.challenge_reponse_authentication == 1) {
1.1       markus    270:                                char *response = packet_get_string(&dlen);
1.11      markus    271:                                debug("got response '%s'", response);
1.1       markus    272:                                packet_integrity_check(plen, 4 + dlen, type);
1.11      markus    273:                                authenticated = verify_response(authctxt, response);
                    274:                                memset(response, 'r', dlen);
1.1       markus    275:                                xfree(response);
                    276:                        }
                    277:                        break;
                    278:
                    279:                default:
                    280:                        /*
                    281:                         * Any unknown messages will be ignored (and failure
                    282:                         * returned) during authentication.
                    283:                         */
                    284:                        log("Unknown message during authentication: type %d", type);
                    285:                        break;
                    286:                }
1.11      markus    287:                if (!authctxt->valid && authenticated)
                    288:                        fatal("INTERNAL ERROR: authenticated invalid user %s",
                    289:                            authctxt->user);
                    290:
                    291:                /* Special handling for root */
1.16      markus    292:                if (authenticated && authctxt->pw->pw_uid == 0 &&
                    293:                    !auth_root_allowed(get_authname(type)))
1.11      markus    294:                        authenticated = 0;
1.1       markus    295:
1.11      markus    296:                /* Log before sending the reply */
                    297:                auth_log(authctxt, authenticated, get_authname(type), info);
1.1       markus    298:
                    299:                if (authenticated)
                    300:                        return;
                    301:
1.11      markus    302:                if (authctxt->failures++ > AUTH_FAIL_MAX)
                    303:                        packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
1.1       markus    304:
                    305:                packet_start(SSH_SMSG_FAILURE);
                    306:                packet_send();
                    307:                packet_write_wait();
                    308:        }
                    309: }
                    310:
                    311: /*
                    312:  * Performs authentication of an incoming connection.  Session key has already
                    313:  * been exchanged and encryption is enabled.
                    314:  */
                    315: void
                    316: do_authentication()
                    317: {
1.11      markus    318:        Authctxt *authctxt;
                    319:        struct passwd *pw;
1.1       markus    320:        int plen;
1.8       markus    321:        u_int ulen;
1.11      markus    322:        char *user, *style = NULL;
1.1       markus    323:
                    324:        /* Get the name of the user that we wish to log in as. */
                    325:        packet_read_expect(&plen, SSH_CMSG_USER);
                    326:
                    327:        /* Get the user name. */
                    328:        user = packet_get_string(&ulen);
                    329:        packet_integrity_check(plen, (4 + ulen), SSH_CMSG_USER);
                    330:
1.11      markus    331:        if ((style = strchr(user, ':')) != NULL)
                    332:                *style++ = 0;
                    333:
                    334:        authctxt = authctxt_new();
                    335:        authctxt->user = user;
                    336:        authctxt->style = style;
                    337:
1.1       markus    338:        /* Verify that the user is a valid user. */
                    339:        pw = getpwnam(user);
1.5       markus    340:        if (pw && allowed_user(pw)) {
1.11      markus    341:                authctxt->valid = 1;
                    342:                pw = pwcopy(pw);
1.5       markus    343:        } else {
1.11      markus    344:                debug("do_authentication: illegal user %s", user);
1.5       markus    345:                pw = NULL;
                    346:        }
1.11      markus    347:        authctxt->pw = pw;
1.17    ! markus    348:
        !           349:        setproctitle("%s", pw ? user : "unknown");
1.1       markus    350:
                    351:        /*
                    352:         * If we are not running as root, the user must have the same uid as
                    353:         * the server.
                    354:         */
1.5       markus    355:        if (getuid() != 0 && pw && pw->pw_uid != getuid())
1.1       markus    356:                packet_disconnect("Cannot change user when server not running as root.");
                    357:
1.11      markus    358:        /*
                    359:         * Loop until the user has been authenticated or the connection is
                    360:         * closed, do_authloop() returns only if authentication is successful
                    361:         */
                    362:        do_authloop(authctxt);
1.1       markus    363:
                    364:        /* The user has been authenticated and accepted. */
                    365:        packet_start(SSH_SMSG_SUCCESS);
                    366:        packet_send();
                    367:        packet_write_wait();
1.11      markus    368:
                    369:        xfree(authctxt->user);
                    370:        xfree(authctxt);
1.1       markus    371:
                    372:        /* Perform session preparation. */
                    373:        do_authenticated(pw);
                    374: }