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

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