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

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