[BACK]Return to sshconnect2.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/sshconnect2.c, Revision 1.136

1.1       markus      1: /*
                      2:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
                      3:  *
                      4:  * Redistribution and use in source and binary forms, with or without
                      5:  * modification, are permitted provided that the following conditions
                      6:  * are met:
                      7:  * 1. Redistributions of source code must retain the above copyright
                      8:  *    notice, this list of conditions and the following disclaimer.
                      9:  * 2. Redistributions in binary form must reproduce the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer in the
                     11:  *    documentation and/or other materials provided with the distribution.
                     12:  *
                     13:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     14:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     15:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     16:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     17:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     18:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     19:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     20:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     21:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     22:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     23:  */
                     24:
                     25: #include "includes.h"
1.136   ! henning    26: RCSID("$OpenBSD: sshconnect2.c,v 1.135 2004/03/05 10:53:58 markus Exp $");
1.1       markus     27:
                     28: #include "ssh.h"
1.37      markus     29: #include "ssh2.h"
1.1       markus     30: #include "xmalloc.h"
                     31: #include "buffer.h"
                     32: #include "packet.h"
                     33: #include "compat.h"
                     34: #include "bufaux.h"
1.37      markus     35: #include "cipher.h"
1.1       markus     36: #include "kex.h"
                     37: #include "myproposal.h"
                     38: #include "sshconnect.h"
                     39: #include "authfile.h"
1.57      provos     40: #include "dh.h"
1.17      markus     41: #include "authfd.h"
1.37      markus     42: #include "log.h"
                     43: #include "readconf.h"
                     44: #include "readpass.h"
1.53      markus     45: #include "match.h"
1.61      markus     46: #include "dispatch.h"
1.68      markus     47: #include "canohost.h"
1.100     markus     48: #include "msg.h"
                     49: #include "pathnames.h"
1.22      provos     50:
1.121     markus     51: #ifdef GSSAPI
                     52: #include "ssh-gss.h"
                     53: #endif
                     54:
1.1       markus     55: /* import */
                     56: extern char *client_version_string;
                     57: extern char *server_version_string;
                     58: extern Options options;
                     59:
                     60: /*
                     61:  * SSH2 key exchange
                     62:  */
                     63:
1.32      markus     64: u_char *session_id2 = NULL;
1.120     markus     65: u_int session_id2_len = 0;
1.1       markus     66:
1.61      markus     67: char *xxx_host;
                     68: struct sockaddr *xxx_hostaddr;
                     69:
1.63      markus     70: Kex *xxx_kex = NULL;
                     71:
1.76      itojun     72: static int
1.75      markus     73: verify_host_key_callback(Key *hostkey)
1.61      markus     74: {
1.75      markus     75:        if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
1.83      markus     76:                fatal("Host key verification failed.");
1.61      markus     77:        return 0;
                     78: }
                     79:
1.1       markus     80: void
1.22      provos     81: ssh_kex2(char *host, struct sockaddr *hostaddr)
                     82: {
                     83:        Kex *kex;
1.61      markus     84:
                     85:        xxx_host = host;
                     86:        xxx_hostaddr = hostaddr;
1.22      provos     87:
1.29      markus     88:        if (options.ciphers == (char *)-1) {
1.116     itojun     89:                logit("No valid ciphers for protocol version 2 given, using defaults.");
1.29      markus     90:                options.ciphers = NULL;
1.24      markus     91:        }
1.22      provos     92:        if (options.ciphers != NULL) {
                     93:                myproposal[PROPOSAL_ENC_ALGS_CTOS] =
                     94:                myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
                     95:        }
1.60      stevesk    96:        myproposal[PROPOSAL_ENC_ALGS_CTOS] =
                     97:            compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
                     98:        myproposal[PROPOSAL_ENC_ALGS_STOC] =
                     99:            compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
1.22      provos    100:        if (options.compression) {
1.47      markus    101:                myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1.107     markus    102:                myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
1.22      provos    103:        } else {
1.47      markus    104:                myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1.107     markus    105:                myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
1.47      markus    106:        }
                    107:        if (options.macs != NULL) {
                    108:                myproposal[PROPOSAL_MAC_ALGS_CTOS] =
                    109:                myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1.22      provos    110:        }
1.70      markus    111:        if (options.hostkeyalgorithms != NULL)
1.88      deraadt   112:                myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
1.70      markus    113:                    options.hostkeyalgorithms;
1.115     markus    114:
                    115:        if (options.rekey_limit)
                    116:                packet_set_rekey_limit(options.rekey_limit);
1.22      provos    117:
1.65      markus    118:        /* start key exchange */
1.64      markus    119:        kex = kex_setup(myproposal);
1.111     markus    120:        kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
                    121:        kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
1.61      markus    122:        kex->client_version_string=client_version_string;
                    123:        kex->server_version_string=server_version_string;
1.75      markus    124:        kex->verify_host_key=&verify_host_key_callback;
1.63      markus    125:
                    126:        xxx_kex = kex;
1.22      provos    127:
1.66      markus    128:        dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
1.62      markus    129:
                    130:        session_id2 = kex->session_id;
                    131:        session_id2_len = kex->session_id_len;
1.22      provos    132:
                    133: #ifdef DEBUG_KEXDH
                    134:        /* send 1st encrypted/maced/compressed message */
                    135:        packet_start(SSH2_MSG_IGNORE);
                    136:        packet_put_cstring("markus");
                    137:        packet_send();
                    138:        packet_write_wait();
                    139: #endif
1.1       markus    140: }
1.11      markus    141:
1.1       markus    142: /*
                    143:  * Authenticate user
                    144:  */
1.20      markus    145:
                    146: typedef struct Authctxt Authctxt;
                    147: typedef struct Authmethod Authmethod;
1.117     markus    148: typedef struct identity Identity;
                    149: typedef struct idlist Idlist;
1.20      markus    150:
1.117     markus    151: struct identity {
                    152:        TAILQ_ENTRY(identity) next;
                    153:        AuthenticationConnection *ac;   /* set if agent supports key */
                    154:        Key     *key;                   /* public/private key */
                    155:        char    *filename;              /* comment for agent-only keys */
                    156:        int     tried;
                    157:        int     isprivate;              /* key points to the private key */
                    158: };
                    159: TAILQ_HEAD(idlist, identity);
1.20      markus    160:
                    161: struct Authctxt {
                    162:        const char *server_user;
1.68      markus    163:        const char *local_user;
1.20      markus    164:        const char *host;
                    165:        const char *service;
1.23      markus    166:        Authmethod *method;
1.20      markus    167:        int success;
1.51      markus    168:        char *authlist;
1.68      markus    169:        /* pubkey */
1.117     markus    170:        Idlist keys;
1.68      markus    171:        AuthenticationConnection *agent;
                    172:        /* hostbased */
1.100     markus    173:        Sensitive *sensitive;
1.82      markus    174:        /* kbd-interactive */
                    175:        int info_req_seen;
1.121     markus    176:        /* generic */
                    177:        void *methoddata;
1.20      markus    178: };
                    179: struct Authmethod {
                    180:        char    *name;          /* string to compare against server's list */
                    181:        int     (*userauth)(Authctxt *authctxt);
                    182:        int     *enabled;       /* flag in option struct that enables method */
                    183:        int     *batch_flag;    /* flag in option struct that disables method */
                    184: };
                    185:
1.92      markus    186: void   input_userauth_success(int, u_int32_t, void *);
                    187: void   input_userauth_failure(int, u_int32_t, void *);
                    188: void   input_userauth_banner(int, u_int32_t, void *);
                    189: void   input_userauth_error(int, u_int32_t, void *);
                    190: void   input_userauth_info_req(int, u_int32_t, void *);
                    191: void   input_userauth_pk_ok(int, u_int32_t, void *);
1.99      markus    192: void   input_userauth_passwd_changereq(int, u_int32_t, void *);
1.86      itojun    193:
                    194: int    userauth_none(Authctxt *);
                    195: int    userauth_pubkey(Authctxt *);
                    196: int    userauth_passwd(Authctxt *);
                    197: int    userauth_kbdint(Authctxt *);
                    198: int    userauth_hostbased(Authctxt *);
1.118     markus    199: int    userauth_kerberos(Authctxt *);
1.20      markus    200:
1.121     markus    201: #ifdef GSSAPI
                    202: int    userauth_gssapi(Authctxt *authctxt);
                    203: void   input_gssapi_response(int type, u_int32_t, void *);
                    204: void   input_gssapi_token(int type, u_int32_t, void *);
                    205: void   input_gssapi_hash(int type, u_int32_t, void *);
                    206: void   input_gssapi_error(int, u_int32_t, void *);
                    207: void   input_gssapi_errtok(int, u_int32_t, void *);
                    208: #endif
                    209:
1.86      itojun    210: void   userauth(Authctxt *, char *);
1.51      markus    211:
1.117     markus    212: static int sign_and_send_pubkey(Authctxt *, Identity *);
                    213: static void pubkey_prepare(Authctxt *);
                    214: static void pubkey_cleanup(Authctxt *);
                    215: static Key *load_identity_file(char *);
1.76      itojun    216:
                    217: static Authmethod *authmethod_get(char *authlist);
                    218: static Authmethod *authmethod_lookup(const char *name);
                    219: static char *authmethods_get(void);
1.20      markus    220:
                    221: Authmethod authmethods[] = {
1.121     markus    222: #ifdef GSSAPI
1.132     markus    223:        {"gssapi-with-mic",
1.121     markus    224:                userauth_gssapi,
                    225:                &options.gss_authentication,
                    226:                NULL},
                    227: #endif
1.81      markus    228:        {"hostbased",
                    229:                userauth_hostbased,
                    230:                &options.hostbased_authentication,
                    231:                NULL},
1.20      markus    232:        {"publickey",
                    233:                userauth_pubkey,
1.28      markus    234:                &options.pubkey_authentication,
1.20      markus    235:                NULL},
1.81      markus    236:        {"keyboard-interactive",
                    237:                userauth_kbdint,
                    238:                &options.kbd_interactive_authentication,
                    239:                &options.batch_mode},
1.20      markus    240:        {"password",
                    241:                userauth_passwd,
                    242:                &options.password_authentication,
1.23      markus    243:                &options.batch_mode},
                    244:        {"none",
                    245:                userauth_none,
                    246:                NULL,
                    247:                NULL},
1.20      markus    248:        {NULL, NULL, NULL, NULL}
                    249: };
                    250:
                    251: void
1.68      markus    252: ssh_userauth2(const char *local_user, const char *server_user, char *host,
1.100     markus    253:     Sensitive *sensitive)
1.20      markus    254: {
                    255:        Authctxt authctxt;
                    256:        int type;
1.39      markus    257:
1.73      markus    258:        if (options.challenge_response_authentication)
1.39      markus    259:                options.kbd_interactive_authentication = 1;
1.20      markus    260:
                    261:        packet_start(SSH2_MSG_SERVICE_REQUEST);
                    262:        packet_put_cstring("ssh-userauth");
                    263:        packet_send();
1.108     markus    264:        debug("SSH2_MSG_SERVICE_REQUEST sent");
1.20      markus    265:        packet_write_wait();
1.91      markus    266:        type = packet_read();
1.108     markus    267:        if (type != SSH2_MSG_SERVICE_ACCEPT)
                    268:                fatal("Server denied authentication request: %d", type);
1.20      markus    269:        if (packet_remaining() > 0) {
1.91      markus    270:                char *reply = packet_get_string(NULL);
1.108     markus    271:                debug2("service_accept: %s", reply);
1.20      markus    272:                xfree(reply);
                    273:        } else {
1.109     markus    274:                debug2("buggy server: service_accept w/o service");
1.20      markus    275:        }
1.90      markus    276:        packet_check_eom();
1.108     markus    277:        debug("SSH2_MSG_SERVICE_ACCEPT received");
1.20      markus    278:
1.53      markus    279:        if (options.preferred_authentications == NULL)
                    280:                options.preferred_authentications = authmethods_get();
                    281:
1.20      markus    282:        /* setup authentication context */
1.82      markus    283:        memset(&authctxt, 0, sizeof(authctxt));
1.117     markus    284:        pubkey_prepare(&authctxt);
1.20      markus    285:        authctxt.server_user = server_user;
1.68      markus    286:        authctxt.local_user = local_user;
1.20      markus    287:        authctxt.host = host;
                    288:        authctxt.service = "ssh-connection";            /* service name */
                    289:        authctxt.success = 0;
1.23      markus    290:        authctxt.method = authmethod_lookup("none");
1.51      markus    291:        authctxt.authlist = NULL;
1.121     markus    292:        authctxt.methoddata = NULL;
1.100     markus    293:        authctxt.sensitive = sensitive;
1.82      markus    294:        authctxt.info_req_seen = 0;
1.23      markus    295:        if (authctxt.method == NULL)
                    296:                fatal("ssh_userauth2: internal error: cannot send userauth none request");
1.20      markus    297:
                    298:        /* initial userauth request */
1.23      markus    299:        userauth_none(&authctxt);
1.20      markus    300:
1.65      markus    301:        dispatch_init(&input_userauth_error);
1.20      markus    302:        dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
                    303:        dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
1.35      markus    304:        dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
1.20      markus    305:        dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt);     /* loop until success */
                    306:
1.117     markus    307:        pubkey_cleanup(&authctxt);
1.119     markus    308:        dispatch_range(SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
                    309:
1.109     markus    310:        debug("Authentication succeeded (%s).", authctxt.method->name);
1.20      markus    311: }
1.119     markus    312:
1.20      markus    313: void
1.51      markus    314: userauth(Authctxt *authctxt, char *authlist)
                    315: {
1.121     markus    316:        if (authctxt->methoddata) {
                    317:                xfree(authctxt->methoddata);
                    318:                authctxt->methoddata = NULL;
                    319:        }
1.51      markus    320:        if (authlist == NULL) {
                    321:                authlist = authctxt->authlist;
                    322:        } else {
                    323:                if (authctxt->authlist)
                    324:                        xfree(authctxt->authlist);
                    325:                authctxt->authlist = authlist;
                    326:        }
                    327:        for (;;) {
                    328:                Authmethod *method = authmethod_get(authlist);
                    329:                if (method == NULL)
                    330:                        fatal("Permission denied (%s).", authlist);
                    331:                authctxt->method = method;
1.119     markus    332:
                    333:                /* reset the per method handler */
                    334:                dispatch_range(SSH2_MSG_USERAUTH_PER_METHOD_MIN,
                    335:                    SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
                    336:
                    337:                /* and try new method */
1.51      markus    338:                if (method->userauth(authctxt) != 0) {
                    339:                        debug2("we sent a %s packet, wait for reply", method->name);
                    340:                        break;
                    341:                } else {
                    342:                        debug2("we did not send a packet, disable method");
                    343:                        method->enabled = NULL;
                    344:                }
                    345:        }
                    346: }
1.105     deraadt   347:
1.51      markus    348: void
1.92      markus    349: input_userauth_error(int type, u_int32_t seq, void *ctxt)
1.20      markus    350: {
1.35      markus    351:        fatal("input_userauth_error: bad message during authentication: "
                    352:           "type %d", type);
                    353: }
1.105     deraadt   354:
1.35      markus    355: void
1.92      markus    356: input_userauth_banner(int type, u_int32_t seq, void *ctxt)
1.35      markus    357: {
                    358:        char *msg, *lang;
1.126     deraadt   359:
1.35      markus    360:        debug3("input_userauth_banner");
                    361:        msg = packet_get_string(NULL);
                    362:        lang = packet_get_string(NULL);
1.125     dtucker   363:        if (options.log_level > SYSLOG_LEVEL_QUIET)
                    364:                fprintf(stderr, "%s", msg);
1.35      markus    365:        xfree(msg);
                    366:        xfree(lang);
1.20      markus    367: }
1.105     deraadt   368:
1.20      markus    369: void
1.92      markus    370: input_userauth_success(int type, u_int32_t seq, void *ctxt)
1.20      markus    371: {
                    372:        Authctxt *authctxt = ctxt;
                    373:        if (authctxt == NULL)
                    374:                fatal("input_userauth_success: no authentication context");
1.126     deraadt   375:        if (authctxt->authlist) {
1.51      markus    376:                xfree(authctxt->authlist);
1.126     deraadt   377:                authctxt->authlist = NULL;
                    378:        }
                    379:        if (authctxt->methoddata) {
1.121     markus    380:                xfree(authctxt->methoddata);
1.126     deraadt   381:                authctxt->methoddata = NULL;
                    382:        }
1.20      markus    383:        authctxt->success = 1;                  /* break out */
                    384: }
1.105     deraadt   385:
1.20      markus    386: void
1.92      markus    387: input_userauth_failure(int type, u_int32_t seq, void *ctxt)
1.20      markus    388: {
                    389:        Authctxt *authctxt = ctxt;
                    390:        char *authlist = NULL;
                    391:        int partial;
                    392:
                    393:        if (authctxt == NULL)
                    394:                fatal("input_userauth_failure: no authentication context");
                    395:
1.23      markus    396:        authlist = packet_get_string(NULL);
1.20      markus    397:        partial = packet_get_char();
1.90      markus    398:        packet_check_eom();
1.20      markus    399:
                    400:        if (partial != 0)
1.116     itojun    401:                logit("Authenticated with partial success.");
1.109     markus    402:        debug("Authentications that can continue: %s", authlist);
1.20      markus    403:
1.51      markus    404:        userauth(authctxt, authlist);
                    405: }
                    406: void
1.92      markus    407: input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
1.51      markus    408: {
                    409:        Authctxt *authctxt = ctxt;
                    410:        Key *key = NULL;
1.117     markus    411:        Identity *id = NULL;
1.51      markus    412:        Buffer b;
1.96      markus    413:        int pktype, sent = 0;
                    414:        u_int alen, blen;
                    415:        char *pkalg, *fp;
                    416:        u_char *pkblob;
1.51      markus    417:
                    418:        if (authctxt == NULL)
                    419:                fatal("input_userauth_pk_ok: no authentication context");
                    420:        if (datafellows & SSH_BUG_PKOK) {
                    421:                /* this is similar to SSH_BUG_PKAUTH */
                    422:                debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
                    423:                pkblob = packet_get_string(&blen);
                    424:                buffer_init(&b);
                    425:                buffer_append(&b, pkblob, blen);
                    426:                pkalg = buffer_get_string(&b, &alen);
                    427:                buffer_free(&b);
                    428:        } else {
                    429:                pkalg = packet_get_string(&alen);
                    430:                pkblob = packet_get_string(&blen);
                    431:        }
1.90      markus    432:        packet_check_eom();
1.51      markus    433:
1.117     markus    434:        debug("Server accepts key: pkalg %s blen %u", pkalg, blen);
1.51      markus    435:
1.117     markus    436:        if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
                    437:                debug("unknown pkalg %s", pkalg);
                    438:                goto done;
                    439:        }
                    440:        if ((key = key_from_blob(pkblob, blen)) == NULL) {
                    441:                debug("no key from blob. pkalg %s", pkalg);
                    442:                goto done;
                    443:        }
                    444:        if (key->type != pktype) {
                    445:                error("input_userauth_pk_ok: type mismatch "
                    446:                    "for decoded key (received %d, expected %d)",
                    447:                    key->type, pktype);
                    448:                goto done;
                    449:        }
                    450:        fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
                    451:        debug2("input_userauth_pk_ok: fp %s", fp);
                    452:        xfree(fp);
                    453:
1.127     markus    454:        /*
                    455:         * search keys in the reverse order, because last candidate has been
                    456:         * moved to the end of the queue.  this also avoids confusion by
                    457:         * duplicate keys
                    458:         */
1.136   ! henning   459:        TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
1.117     markus    460:                if (key_equal(key, id->key)) {
                    461:                        sent = sign_and_send_pubkey(authctxt, id);
1.51      markus    462:                        break;
                    463:                }
1.117     markus    464:        }
                    465: done:
1.51      markus    466:        if (key != NULL)
                    467:                key_free(key);
                    468:        xfree(pkalg);
                    469:        xfree(pkblob);
                    470:
1.106     deraadt   471:        /* try another method if we did not send a packet */
1.51      markus    472:        if (sent == 0)
                    473:                userauth(authctxt, NULL);
1.20      markus    474: }
1.121     markus    475:
                    476: #ifdef GSSAPI
1.133     djm       477: int
1.121     markus    478: userauth_gssapi(Authctxt *authctxt)
                    479: {
                    480:        Gssctxt *gssctxt = NULL;
1.128     avsm      481:        static gss_OID_set gss_supported = NULL;
1.121     markus    482:        static int mech = 0;
                    483:        OM_uint32 min;
                    484:        int ok = 0;
                    485:
                    486:        /* Try one GSSAPI method at a time, rather than sending them all at
                    487:         * once. */
                    488:
1.128     avsm      489:        if (gss_supported == NULL)
                    490:                gss_indicate_mechs(&min, &gss_supported);
1.121     markus    491:
                    492:        /* Check to see if the mechanism is usable before we offer it */
1.128     avsm      493:        while (mech < gss_supported->count && !ok) {
1.121     markus    494:                if (gssctxt)
                    495:                        ssh_gssapi_delete_ctx(&gssctxt);
                    496:                ssh_gssapi_build_ctx(&gssctxt);
1.128     avsm      497:                ssh_gssapi_set_oid(gssctxt, &gss_supported->elements[mech]);
1.121     markus    498:
                    499:                /* My DER encoding requires length<128 */
1.128     avsm      500:                if (gss_supported->elements[mech].length < 128 &&
1.121     markus    501:                    !GSS_ERROR(ssh_gssapi_import_name(gssctxt,
                    502:                    authctxt->host))) {
                    503:                        ok = 1; /* Mechanism works */
                    504:                } else {
                    505:                        mech++;
                    506:                }
                    507:        }
                    508:
                    509:        if (!ok) return 0;
                    510:
                    511:        authctxt->methoddata=(void *)gssctxt;
                    512:
                    513:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
                    514:        packet_put_cstring(authctxt->server_user);
                    515:        packet_put_cstring(authctxt->service);
                    516:        packet_put_cstring(authctxt->method->name);
                    517:
                    518:        packet_put_int(1);
                    519:
1.129     markus    520:        packet_put_int((gss_supported->elements[mech].length) + 2);
                    521:        packet_put_char(SSH_GSS_OIDTYPE);
                    522:        packet_put_char(gss_supported->elements[mech].length);
                    523:        packet_put_raw(gss_supported->elements[mech].elements,
                    524:            gss_supported->elements[mech].length);
1.121     markus    525:
                    526:        packet_send();
                    527:
                    528:        dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
                    529:        dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
                    530:        dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
                    531:        dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
                    532:
                    533:        mech++; /* Move along to next candidate */
                    534:
                    535:        return 1;
                    536: }
                    537:
1.130     markus    538: static OM_uint32
                    539: process_gssapi_token(void *ctxt, gss_buffer_t recv_tok)
                    540: {
                    541:        Authctxt *authctxt = ctxt;
                    542:        Gssctxt *gssctxt = authctxt->methoddata;
                    543:        gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
1.132     markus    544:        gss_buffer_desc gssbuf, mic;
                    545:        OM_uint32 status, ms, flags;
                    546:        Buffer b;
1.133     djm       547:
1.130     markus    548:        status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
1.132     markus    549:            recv_tok, &send_tok, &flags);
1.130     markus    550:
                    551:        if (send_tok.length > 0) {
                    552:                if (GSS_ERROR(status))
                    553:                        packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
                    554:                else
                    555:                        packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
1.133     djm       556:
1.130     markus    557:                packet_put_string(send_tok.value, send_tok.length);
                    558:                packet_send();
                    559:                gss_release_buffer(&ms, &send_tok);
                    560:        }
1.133     djm       561:
1.130     markus    562:        if (status == GSS_S_COMPLETE) {
1.132     markus    563:                /* send either complete or MIC, depending on mechanism */
                    564:                if (!(flags & GSS_C_INTEG_FLAG)) {
                    565:                        packet_start(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE);
                    566:                        packet_send();
                    567:                } else {
                    568:                        ssh_gssapi_buildmic(&b, authctxt->server_user,
                    569:                            authctxt->service, "gssapi-with-mic");
                    570:
                    571:                        gssbuf.value = buffer_ptr(&b);
                    572:                        gssbuf.length = buffer_len(&b);
1.133     djm       573:
1.132     markus    574:                        status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
1.133     djm       575:
1.132     markus    576:                        if (!GSS_ERROR(status)) {
                    577:                                packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
                    578:                                packet_put_string(mic.value, mic.length);
1.133     djm       579:
1.132     markus    580:                                packet_send();
                    581:                        }
1.133     djm       582:
1.132     markus    583:                        buffer_free(&b);
                    584:                        gss_release_buffer(&ms, &mic);
1.133     djm       585:                }
1.130     markus    586:        }
1.133     djm       587:
1.130     markus    588:        return status;
                    589: }
                    590:
1.121     markus    591: void
                    592: input_gssapi_response(int type, u_int32_t plen, void *ctxt)
                    593: {
                    594:        Authctxt *authctxt = ctxt;
                    595:        Gssctxt *gssctxt;
                    596:        int oidlen;
                    597:        char *oidv;
                    598:
                    599:        if (authctxt == NULL)
                    600:                fatal("input_gssapi_response: no authentication context");
                    601:        gssctxt = authctxt->methoddata;
                    602:
                    603:        /* Setup our OID */
                    604:        oidv = packet_get_string(&oidlen);
                    605:
1.129     markus    606:        if (oidlen <= 2 ||
                    607:            oidv[0] != SSH_GSS_OIDTYPE ||
                    608:            oidv[1] != oidlen - 2) {
1.130     markus    609:                xfree(oidv);
1.129     markus    610:                debug("Badly encoded mechanism OID received");
                    611:                userauth(authctxt, NULL);
                    612:                return;
1.121     markus    613:        }
1.129     markus    614:
                    615:        if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
                    616:                fatal("Server returned different OID than expected");
1.121     markus    617:
                    618:        packet_check_eom();
                    619:
                    620:        xfree(oidv);
                    621:
1.130     markus    622:        if (GSS_ERROR(process_gssapi_token(ctxt, GSS_C_NO_BUFFER))) {
1.121     markus    623:                /* Start again with next method on list */
                    624:                debug("Trying to start again");
                    625:                userauth(authctxt, NULL);
                    626:                return;
                    627:        }
                    628: }
                    629:
                    630: void
                    631: input_gssapi_token(int type, u_int32_t plen, void *ctxt)
                    632: {
                    633:        Authctxt *authctxt = ctxt;
                    634:        gss_buffer_desc recv_tok;
1.130     markus    635:        OM_uint32 status;
1.121     markus    636:        u_int slen;
                    637:
                    638:        if (authctxt == NULL)
                    639:                fatal("input_gssapi_response: no authentication context");
                    640:
                    641:        recv_tok.value = packet_get_string(&slen);
                    642:        recv_tok.length = slen; /* safe typecast */
                    643:
                    644:        packet_check_eom();
                    645:
1.130     markus    646:        status = process_gssapi_token(ctxt, &recv_tok);
1.121     markus    647:
                    648:        xfree(recv_tok.value);
                    649:
                    650:        if (GSS_ERROR(status)) {
                    651:                /* Start again with the next method in the list */
                    652:                userauth(authctxt, NULL);
                    653:                return;
                    654:        }
                    655: }
                    656:
                    657: void
                    658: input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
                    659: {
                    660:        Authctxt *authctxt = ctxt;
                    661:        Gssctxt *gssctxt;
                    662:        gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
                    663:        gss_buffer_desc recv_tok;
                    664:        OM_uint32 status, ms;
1.123     deraadt   665:        u_int len;
1.121     markus    666:
                    667:        if (authctxt == NULL)
                    668:                fatal("input_gssapi_response: no authentication context");
                    669:        gssctxt = authctxt->methoddata;
                    670:
1.123     deraadt   671:        recv_tok.value = packet_get_string(&len);
                    672:        recv_tok.length = len;
1.121     markus    673:
                    674:        packet_check_eom();
                    675:
                    676:        /* Stick it into GSSAPI and see what it says */
                    677:        status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
                    678:                                     &recv_tok, &send_tok, NULL);
                    679:
                    680:        xfree(recv_tok.value);
                    681:        gss_release_buffer(&ms, &send_tok);
                    682:
                    683:        /* Server will be returning a failed packet after this one */
                    684: }
                    685:
                    686: void
                    687: input_gssapi_error(int type, u_int32_t plen, void *ctxt)
                    688: {
                    689:        OM_uint32 maj, min;
                    690:        char *msg;
                    691:        char *lang;
                    692:
                    693:        maj=packet_get_int();
                    694:        min=packet_get_int();
                    695:        msg=packet_get_string(NULL);
                    696:        lang=packet_get_string(NULL);
                    697:
                    698:        packet_check_eom();
                    699:
                    700:        debug("Server GSSAPI Error:\n%s\n", msg);
                    701:        xfree(msg);
                    702:        xfree(lang);
                    703: }
                    704: #endif /* GSSAPI */
1.20      markus    705:
1.1       markus    706: int
1.23      markus    707: userauth_none(Authctxt *authctxt)
                    708: {
                    709:        /* initial userauth request */
                    710:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
                    711:        packet_put_cstring(authctxt->server_user);
                    712:        packet_put_cstring(authctxt->service);
                    713:        packet_put_cstring(authctxt->method->name);
                    714:        packet_send();
                    715:        return 1;
                    716: }
                    717:
                    718: int
1.20      markus    719: userauth_passwd(Authctxt *authctxt)
1.1       markus    720: {
1.6       markus    721:        static int attempt = 0;
1.99      markus    722:        char prompt[150];
1.1       markus    723:        char *password;
1.6       markus    724:
1.13      todd      725:        if (attempt++ >= options.number_of_password_prompts)
1.6       markus    726:                return 0;
1.13      todd      727:
1.87      deraadt   728:        if (attempt != 1)
1.13      todd      729:                error("Permission denied, please try again.");
1.1       markus    730:
1.43      itojun    731:        snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
1.20      markus    732:            authctxt->server_user, authctxt->host);
1.1       markus    733:        password = read_passphrase(prompt, 0);
                    734:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
1.20      markus    735:        packet_put_cstring(authctxt->server_user);
                    736:        packet_put_cstring(authctxt->service);
1.23      markus    737:        packet_put_cstring(authctxt->method->name);
1.1       markus    738:        packet_put_char(0);
1.49      markus    739:        packet_put_cstring(password);
1.1       markus    740:        memset(password, 0, strlen(password));
                    741:        xfree(password);
1.85      markus    742:        packet_add_padding(64);
1.1       markus    743:        packet_send();
1.99      markus    744:
1.104     deraadt   745:        dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99      markus    746:            &input_userauth_passwd_changereq);
                    747:
1.1       markus    748:        return 1;
                    749: }
1.99      markus    750: /*
                    751:  * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
                    752:  */
                    753: void
                    754: input_userauth_passwd_changereq(int type, uint32_t seqnr, void *ctxt)
                    755: {
                    756:        Authctxt *authctxt = ctxt;
                    757:        char *info, *lang, *password = NULL, *retype = NULL;
                    758:        char prompt[150];
                    759:
                    760:        debug2("input_userauth_passwd_changereq");
                    761:
                    762:        if (authctxt == NULL)
                    763:                fatal("input_userauth_passwd_changereq: "
                    764:                    "no authentication context");
                    765:
                    766:        info = packet_get_string(NULL);
                    767:        lang = packet_get_string(NULL);
                    768:        if (strlen(info) > 0)
1.116     itojun    769:                logit("%s", info);
1.99      markus    770:        xfree(info);
                    771:        xfree(lang);
                    772:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
                    773:        packet_put_cstring(authctxt->server_user);
                    774:        packet_put_cstring(authctxt->service);
                    775:        packet_put_cstring(authctxt->method->name);
                    776:        packet_put_char(1);                     /* additional info */
1.104     deraadt   777:        snprintf(prompt, sizeof(prompt),
1.99      markus    778:            "Enter %.30s@%.128s's old password: ",
                    779:            authctxt->server_user, authctxt->host);
                    780:        password = read_passphrase(prompt, 0);
                    781:        packet_put_cstring(password);
                    782:        memset(password, 0, strlen(password));
                    783:        xfree(password);
                    784:        password = NULL;
                    785:        while (password == NULL) {
1.104     deraadt   786:                snprintf(prompt, sizeof(prompt),
1.99      markus    787:                    "Enter %.30s@%.128s's new password: ",
                    788:                    authctxt->server_user, authctxt->host);
                    789:                password = read_passphrase(prompt, RP_ALLOW_EOF);
                    790:                if (password == NULL) {
                    791:                        /* bail out */
                    792:                        return;
                    793:                }
1.104     deraadt   794:                snprintf(prompt, sizeof(prompt),
1.99      markus    795:                    "Retype %.30s@%.128s's new password: ",
                    796:                    authctxt->server_user, authctxt->host);
                    797:                retype = read_passphrase(prompt, 0);
                    798:                if (strcmp(password, retype) != 0) {
                    799:                        memset(password, 0, strlen(password));
                    800:                        xfree(password);
1.116     itojun    801:                        logit("Mismatch; try again, EOF to quit.");
1.99      markus    802:                        password = NULL;
                    803:                }
                    804:                memset(retype, 0, strlen(retype));
                    805:                xfree(retype);
                    806:        }
                    807:        packet_put_cstring(password);
                    808:        memset(password, 0, strlen(password));
                    809:        xfree(password);
                    810:        packet_add_padding(64);
                    811:        packet_send();
1.104     deraadt   812:
                    813:        dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99      markus    814:            &input_userauth_passwd_changereq);
1.117     markus    815: }
                    816:
                    817: static int
                    818: identity_sign(Identity *id, u_char **sigp, u_int *lenp,
                    819:     u_char *data, u_int datalen)
                    820: {
                    821:        Key *prv;
                    822:        int ret;
1.99      markus    823:
1.117     markus    824:        /* the agent supports this key */
                    825:        if (id->ac)
                    826:                return (ssh_agent_sign(id->ac, id->key, sigp, lenp,
                    827:                    data, datalen));
                    828:        /*
                    829:         * we have already loaded the private key or
                    830:         * the private key is stored in external hardware
                    831:         */
                    832:        if (id->isprivate || (id->key->flags & KEY_FLAG_EXT))
                    833:                return (key_sign(id->key, sigp, lenp, data, datalen));
                    834:        /* load the private key from the file */
                    835:        if ((prv = load_identity_file(id->filename)) == NULL)
                    836:                return (-1);
                    837:        ret = key_sign(prv, sigp, lenp, data, datalen);
                    838:        key_free(prv);
                    839:        return (ret);
1.51      markus    840: }
                    841:
1.76      itojun    842: static int
1.117     markus    843: sign_and_send_pubkey(Authctxt *authctxt, Identity *id)
1.1       markus    844: {
                    845:        Buffer b;
1.32      markus    846:        u_char *blob, *signature;
1.96      markus    847:        u_int bloblen, slen;
1.120     markus    848:        u_int skip = 0;
1.17      markus    849:        int ret = -1;
1.23      markus    850:        int have_sig = 1;
1.1       markus    851:
1.30      markus    852:        debug3("sign_and_send_pubkey");
1.51      markus    853:
1.117     markus    854:        if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1.28      markus    855:                /* we cannot handle this key */
1.30      markus    856:                debug3("sign_and_send_pubkey: cannot handle key");
1.28      markus    857:                return 0;
                    858:        }
1.1       markus    859:        /* data to be signed */
                    860:        buffer_init(&b);
1.26      markus    861:        if (datafellows & SSH_OLD_SESSIONID) {
                    862:                buffer_append(&b, session_id2, session_id2_len);
1.41      stevesk   863:                skip = session_id2_len;
1.26      markus    864:        } else {
1.14      markus    865:                buffer_put_string(&b, session_id2, session_id2_len);
                    866:                skip = buffer_len(&b);
                    867:        }
1.1       markus    868:        buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.20      markus    869:        buffer_put_cstring(&b, authctxt->server_user);
1.10      markus    870:        buffer_put_cstring(&b,
1.30      markus    871:            datafellows & SSH_BUG_PKSERVICE ?
1.10      markus    872:            "ssh-userauth" :
1.20      markus    873:            authctxt->service);
1.30      markus    874:        if (datafellows & SSH_BUG_PKAUTH) {
                    875:                buffer_put_char(&b, have_sig);
                    876:        } else {
                    877:                buffer_put_cstring(&b, authctxt->method->name);
                    878:                buffer_put_char(&b, have_sig);
1.117     markus    879:                buffer_put_cstring(&b, key_ssh_name(id->key));
1.30      markus    880:        }
1.1       markus    881:        buffer_put_string(&b, blob, bloblen);
                    882:
                    883:        /* generate signature */
1.117     markus    884:        ret = identity_sign(id, &signature, &slen,
1.51      markus    885:            buffer_ptr(&b), buffer_len(&b));
1.17      markus    886:        if (ret == -1) {
                    887:                xfree(blob);
                    888:                buffer_free(&b);
                    889:                return 0;
                    890:        }
1.28      markus    891: #ifdef DEBUG_PK
1.1       markus    892:        buffer_dump(&b);
                    893: #endif
1.30      markus    894:        if (datafellows & SSH_BUG_PKSERVICE) {
1.10      markus    895:                buffer_clear(&b);
                    896:                buffer_append(&b, session_id2, session_id2_len);
1.51      markus    897:                skip = session_id2_len;
1.10      markus    898:                buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.20      markus    899:                buffer_put_cstring(&b, authctxt->server_user);
                    900:                buffer_put_cstring(&b, authctxt->service);
1.23      markus    901:                buffer_put_cstring(&b, authctxt->method->name);
                    902:                buffer_put_char(&b, have_sig);
1.30      markus    903:                if (!(datafellows & SSH_BUG_PKAUTH))
1.117     markus    904:                        buffer_put_cstring(&b, key_ssh_name(id->key));
1.10      markus    905:                buffer_put_string(&b, blob, bloblen);
                    906:        }
                    907:        xfree(blob);
1.51      markus    908:
1.1       markus    909:        /* append signature */
                    910:        buffer_put_string(&b, signature, slen);
                    911:        xfree(signature);
                    912:
                    913:        /* skip session id and packet type */
1.14      markus    914:        if (buffer_len(&b) < skip + 1)
1.20      markus    915:                fatal("userauth_pubkey: internal error");
1.14      markus    916:        buffer_consume(&b, skip + 1);
1.1       markus    917:
                    918:        /* put remaining data from buffer into packet */
                    919:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
                    920:        packet_put_raw(buffer_ptr(&b), buffer_len(&b));
                    921:        buffer_free(&b);
                    922:        packet_send();
1.17      markus    923:
                    924:        return 1;
1.16      markus    925: }
                    926:
1.76      itojun    927: static int
1.117     markus    928: send_pubkey_test(Authctxt *authctxt, Identity *id)
1.20      markus    929: {
1.51      markus    930:        u_char *blob;
1.97      markus    931:        u_int bloblen, have_sig = 0;
1.20      markus    932:
1.51      markus    933:        debug3("send_pubkey_test");
1.16      markus    934:
1.117     markus    935:        if (key_to_blob(id->key, &blob, &bloblen) == 0) {
1.51      markus    936:                /* we cannot handle this key */
                    937:                debug3("send_pubkey_test: cannot handle key");
1.16      markus    938:                return 0;
                    939:        }
1.51      markus    940:        /* register callback for USERAUTH_PK_OK message */
                    941:        dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
                    942:
                    943:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
                    944:        packet_put_cstring(authctxt->server_user);
                    945:        packet_put_cstring(authctxt->service);
                    946:        packet_put_cstring(authctxt->method->name);
                    947:        packet_put_char(have_sig);
                    948:        if (!(datafellows & SSH_BUG_PKAUTH))
1.117     markus    949:                packet_put_cstring(key_ssh_name(id->key));
1.51      markus    950:        packet_put_string(blob, bloblen);
                    951:        xfree(blob);
                    952:        packet_send();
                    953:        return 1;
                    954: }
                    955:
1.76      itojun    956: static Key *
1.51      markus    957: load_identity_file(char *filename)
                    958: {
                    959:        Key *private;
                    960:        char prompt[300], *passphrase;
1.56      markus    961:        int quit, i;
1.52      markus    962:        struct stat st;
1.16      markus    963:
1.52      markus    964:        if (stat(filename, &st) < 0) {
                    965:                debug3("no such identity: %s", filename);
                    966:                return NULL;
                    967:        }
1.56      markus    968:        private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
                    969:        if (private == NULL) {
                    970:                if (options.batch_mode)
1.51      markus    971:                        return NULL;
1.16      markus    972:                snprintf(prompt, sizeof prompt,
1.88      deraadt   973:                    "Enter passphrase for key '%.100s': ", filename);
1.20      markus    974:                for (i = 0; i < options.number_of_password_prompts; i++) {
                    975:                        passphrase = read_passphrase(prompt, 0);
                    976:                        if (strcmp(passphrase, "") != 0) {
1.56      markus    977:                                private = key_load_private_type(KEY_UNSPEC, filename,
                    978:                                    passphrase, NULL);
1.51      markus    979:                                quit = 0;
1.20      markus    980:                        } else {
                    981:                                debug2("no passphrase given, try next key");
1.51      markus    982:                                quit = 1;
1.20      markus    983:                        }
                    984:                        memset(passphrase, 0, strlen(passphrase));
                    985:                        xfree(passphrase);
1.56      markus    986:                        if (private != NULL || quit)
1.20      markus    987:                                break;
                    988:                        debug2("bad passphrase given, try again...");
1.16      markus    989:                }
                    990:        }
1.51      markus    991:        return private;
                    992: }
                    993:
1.117     markus    994: /*
                    995:  * try keys in the following order:
                    996:  *     1. agent keys that are found in the config file
                    997:  *     2. other agent keys
                    998:  *     3. keys that are only listed in the config file
                    999:  */
                   1000: static void
                   1001: pubkey_prepare(Authctxt *authctxt)
1.51      markus   1002: {
1.117     markus   1003:        Identity *id;
                   1004:        Idlist agent, files, *preferred;
                   1005:        Key *key;
                   1006:        AuthenticationConnection *ac;
                   1007:        char *comment;
                   1008:        int i, found;
1.51      markus   1009:
1.117     markus   1010:        TAILQ_INIT(&agent);     /* keys from the agent */
                   1011:        TAILQ_INIT(&files);     /* keys from the config file */
                   1012:        preferred = &authctxt->keys;
                   1013:        TAILQ_INIT(preferred);  /* preferred order of keys */
                   1014:
                   1015:        /* list of keys stored in the filesystem */
                   1016:        for (i = 0; i < options.num_identity_files; i++) {
                   1017:                key = options.identity_keys[i];
                   1018:                if (key && key->type == KEY_RSA1)
                   1019:                        continue;
                   1020:                options.identity_keys[i] = NULL;
                   1021:                id = xmalloc(sizeof(*id));
                   1022:                memset(id, 0, sizeof(*id));
                   1023:                id->key = key;
                   1024:                id->filename = xstrdup(options.identity_files[i]);
                   1025:                TAILQ_INSERT_TAIL(&files, id, next);
                   1026:        }
                   1027:        /* list of keys supported by the agent */
                   1028:        if ((ac = ssh_get_authentication_connection())) {
                   1029:                for (key = ssh_get_first_identity(ac, &comment, 2);
                   1030:                    key != NULL;
                   1031:                    key = ssh_get_next_identity(ac, &comment, 2)) {
                   1032:                        found = 0;
                   1033:                        TAILQ_FOREACH(id, &files, next) {
1.133     djm      1034:                                /* agent keys from the config file are preferred */
1.117     markus   1035:                                if (key_equal(key, id->key)) {
                   1036:                                        key_free(key);
                   1037:                                        xfree(comment);
                   1038:                                        TAILQ_REMOVE(&files, id, next);
                   1039:                                        TAILQ_INSERT_TAIL(preferred, id, next);
                   1040:                                        id->ac = ac;
                   1041:                                        found = 1;
                   1042:                                        break;
                   1043:                                }
                   1044:                        }
1.135     markus   1045:                        if (!found && !options.identities_only) {
1.117     markus   1046:                                id = xmalloc(sizeof(*id));
                   1047:                                memset(id, 0, sizeof(*id));
                   1048:                                id->key = key;
                   1049:                                id->filename = comment;
                   1050:                                id->ac = ac;
                   1051:                                TAILQ_INSERT_TAIL(&agent, id, next);
                   1052:                        }
                   1053:                }
                   1054:                /* append remaining agent keys */
                   1055:                for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
                   1056:                        TAILQ_REMOVE(&agent, id, next);
                   1057:                        TAILQ_INSERT_TAIL(preferred, id, next);
                   1058:                }
                   1059:                authctxt->agent = ac;
                   1060:        }
                   1061:        /* append remaining keys from the config file */
                   1062:        for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
                   1063:                TAILQ_REMOVE(&files, id, next);
                   1064:                TAILQ_INSERT_TAIL(preferred, id, next);
                   1065:        }
                   1066:        TAILQ_FOREACH(id, preferred, next) {
                   1067:                debug2("key: %s (%p)", id->filename, id->key);
                   1068:        }
1.17      markus   1069: }
                   1070:
1.117     markus   1071: static void
                   1072: pubkey_cleanup(Authctxt *authctxt)
1.51      markus   1073: {
1.117     markus   1074:        Identity *id;
1.51      markus   1075:
1.117     markus   1076:        if (authctxt->agent != NULL)
                   1077:                ssh_close_authentication_connection(authctxt->agent);
                   1078:        for (id = TAILQ_FIRST(&authctxt->keys); id;
                   1079:            id = TAILQ_FIRST(&authctxt->keys)) {
                   1080:                TAILQ_REMOVE(&authctxt->keys, id, next);
                   1081:                if (id->key)
                   1082:                        key_free(id->key);
                   1083:                if (id->filename)
                   1084:                        xfree(id->filename);
                   1085:                xfree(id);
                   1086:        }
1.1       markus   1087: }
                   1088:
1.20      markus   1089: int
                   1090: userauth_pubkey(Authctxt *authctxt)
                   1091: {
1.117     markus   1092:        Identity *id;
1.20      markus   1093:        int sent = 0;
                   1094:
1.117     markus   1095:        while ((id = TAILQ_FIRST(&authctxt->keys))) {
                   1096:                if (id->tried++)
                   1097:                        return (0);
1.127     markus   1098:                /* move key to the end of the queue */
1.117     markus   1099:                TAILQ_REMOVE(&authctxt->keys, id, next);
                   1100:                TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
                   1101:                /*
                   1102:                 * send a test message if we have the public key. for
                   1103:                 * encrypted keys we cannot do this and have to load the
                   1104:                 * private key instead
                   1105:                 */
                   1106:                if (id->key && id->key->type != KEY_RSA1) {
                   1107:                        debug("Offering public key: %s", id->filename);
                   1108:                        sent = send_pubkey_test(authctxt, id);
                   1109:                } else if (id->key == NULL) {
                   1110:                        debug("Trying private key: %s", id->filename);
                   1111:                        id->key = load_identity_file(id->filename);
                   1112:                        if (id->key != NULL) {
                   1113:                                id->isprivate = 1;
                   1114:                                sent = sign_and_send_pubkey(authctxt, id);
                   1115:                                key_free(id->key);
                   1116:                                id->key = NULL;
1.51      markus   1117:                        }
                   1118:                }
1.117     markus   1119:                if (sent)
                   1120:                        return (sent);
1.28      markus   1121:        }
1.117     markus   1122:        return (0);
1.20      markus   1123: }
                   1124:
1.23      markus   1125: /*
                   1126:  * Send userauth request message specifying keyboard-interactive method.
                   1127:  */
                   1128: int
                   1129: userauth_kbdint(Authctxt *authctxt)
                   1130: {
                   1131:        static int attempt = 0;
                   1132:
                   1133:        if (attempt++ >= options.number_of_password_prompts)
                   1134:                return 0;
1.82      markus   1135:        /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
                   1136:        if (attempt > 1 && !authctxt->info_req_seen) {
                   1137:                debug3("userauth_kbdint: disable: no info_req_seen");
                   1138:                dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
                   1139:                return 0;
                   1140:        }
1.23      markus   1141:
                   1142:        debug2("userauth_kbdint");
                   1143:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
                   1144:        packet_put_cstring(authctxt->server_user);
                   1145:        packet_put_cstring(authctxt->service);
                   1146:        packet_put_cstring(authctxt->method->name);
                   1147:        packet_put_cstring("");                                 /* lang */
                   1148:        packet_put_cstring(options.kbd_interactive_devices ?
                   1149:            options.kbd_interactive_devices : "");
                   1150:        packet_send();
                   1151:
                   1152:        dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
                   1153:        return 1;
                   1154: }
                   1155:
                   1156: /*
1.46      markus   1157:  * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1.23      markus   1158:  */
                   1159: void
1.92      markus   1160: input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1.23      markus   1161: {
                   1162:        Authctxt *authctxt = ctxt;
1.46      markus   1163:        char *name, *inst, *lang, *prompt, *response;
1.32      markus   1164:        u_int num_prompts, i;
1.23      markus   1165:        int echo = 0;
                   1166:
                   1167:        debug2("input_userauth_info_req");
                   1168:
                   1169:        if (authctxt == NULL)
                   1170:                fatal("input_userauth_info_req: no authentication context");
1.82      markus   1171:
                   1172:        authctxt->info_req_seen = 1;
1.23      markus   1173:
                   1174:        name = packet_get_string(NULL);
                   1175:        inst = packet_get_string(NULL);
                   1176:        lang = packet_get_string(NULL);
                   1177:        if (strlen(name) > 0)
1.116     itojun   1178:                logit("%s", name);
1.23      markus   1179:        if (strlen(inst) > 0)
1.116     itojun   1180:                logit("%s", inst);
1.46      markus   1181:        xfree(name);
1.23      markus   1182:        xfree(inst);
1.46      markus   1183:        xfree(lang);
1.23      markus   1184:
                   1185:        num_prompts = packet_get_int();
                   1186:        /*
                   1187:         * Begin to build info response packet based on prompts requested.
                   1188:         * We commit to providing the correct number of responses, so if
                   1189:         * further on we run into a problem that prevents this, we have to
                   1190:         * be sure and clean this up and send a correct error response.
                   1191:         */
                   1192:        packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
                   1193:        packet_put_int(num_prompts);
                   1194:
1.73      markus   1195:        debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1.23      markus   1196:        for (i = 0; i < num_prompts; i++) {
                   1197:                prompt = packet_get_string(NULL);
                   1198:                echo = packet_get_char();
                   1199:
1.77      markus   1200:                response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1.23      markus   1201:
1.49      markus   1202:                packet_put_cstring(response);
1.23      markus   1203:                memset(response, 0, strlen(response));
                   1204:                xfree(response);
                   1205:                xfree(prompt);
                   1206:        }
1.90      markus   1207:        packet_check_eom(); /* done with parsing incoming message. */
1.23      markus   1208:
1.85      markus   1209:        packet_add_padding(64);
1.23      markus   1210:        packet_send();
1.68      markus   1211: }
                   1212:
1.100     markus   1213: static int
1.105     deraadt  1214: ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1.100     markus   1215:     u_char *data, u_int datalen)
                   1216: {
                   1217:        Buffer b;
1.101     markus   1218:        struct stat st;
1.100     markus   1219:        pid_t pid;
1.103     markus   1220:        int to[2], from[2], status, version = 2;
1.100     markus   1221:
1.109     markus   1222:        debug2("ssh_keysign called");
1.100     markus   1223:
1.101     markus   1224:        if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
                   1225:                error("ssh_keysign: no installed: %s", strerror(errno));
                   1226:                return -1;
                   1227:        }
1.100     markus   1228:        if (fflush(stdout) != 0)
                   1229:                error("ssh_keysign: fflush: %s", strerror(errno));
                   1230:        if (pipe(to) < 0) {
                   1231:                error("ssh_keysign: pipe: %s", strerror(errno));
                   1232:                return -1;
                   1233:        }
                   1234:        if (pipe(from) < 0) {
                   1235:                error("ssh_keysign: pipe: %s", strerror(errno));
                   1236:                return -1;
                   1237:        }
                   1238:        if ((pid = fork()) < 0) {
                   1239:                error("ssh_keysign: fork: %s", strerror(errno));
                   1240:                return -1;
                   1241:        }
                   1242:        if (pid == 0) {
                   1243:                seteuid(getuid());
                   1244:                setuid(getuid());
                   1245:                close(from[0]);
                   1246:                if (dup2(from[1], STDOUT_FILENO) < 0)
                   1247:                        fatal("ssh_keysign: dup2: %s", strerror(errno));
                   1248:                close(to[1]);
                   1249:                if (dup2(to[0], STDIN_FILENO) < 0)
                   1250:                        fatal("ssh_keysign: dup2: %s", strerror(errno));
1.103     markus   1251:                close(from[1]);
                   1252:                close(to[0]);
1.102     markus   1253:                execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1.100     markus   1254:                fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
                   1255:                    strerror(errno));
                   1256:        }
                   1257:        close(from[1]);
                   1258:        close(to[0]);
                   1259:
                   1260:        buffer_init(&b);
1.103     markus   1261:        buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1.100     markus   1262:        buffer_put_string(&b, data, datalen);
1.131     djm      1263:        if (ssh_msg_send(to[1], version, &b) == -1)
                   1264:                fatal("ssh_keysign: couldn't send request");
1.100     markus   1265:
1.110     djm      1266:        if (ssh_msg_recv(from[0], &b) < 0) {
1.101     markus   1267:                error("ssh_keysign: no reply");
1.134     markus   1268:                buffer_free(&b);
1.100     markus   1269:                return -1;
                   1270:        }
1.101     markus   1271:        close(from[0]);
                   1272:        close(to[1]);
                   1273:
1.103     markus   1274:        while (waitpid(pid, &status, 0) < 0)
                   1275:                if (errno != EINTR)
                   1276:                        break;
1.101     markus   1277:
1.100     markus   1278:        if (buffer_get_char(&b) != version) {
1.101     markus   1279:                error("ssh_keysign: bad version");
1.134     markus   1280:                buffer_free(&b);
1.100     markus   1281:                return -1;
                   1282:        }
                   1283:        *sigp = buffer_get_string(&b, lenp);
1.134     markus   1284:        buffer_free(&b);
1.100     markus   1285:
                   1286:        return 0;
                   1287: }
                   1288:
1.68      markus   1289: int
                   1290: userauth_hostbased(Authctxt *authctxt)
                   1291: {
                   1292:        Key *private = NULL;
1.100     markus   1293:        Sensitive *sensitive = authctxt->sensitive;
1.68      markus   1294:        Buffer b;
                   1295:        u_char *signature, *blob;
                   1296:        char *chost, *pkalg, *p;
1.72      markus   1297:        const char *service;
1.68      markus   1298:        u_int blen, slen;
1.71      markus   1299:        int ok, i, len, found = 0;
1.68      markus   1300:
                   1301:        /* check for a useful key */
1.100     markus   1302:        for (i = 0; i < sensitive->nkeys; i++) {
                   1303:                private = sensitive->keys[i];
1.68      markus   1304:                if (private && private->type != KEY_RSA1) {
                   1305:                        found = 1;
                   1306:                        /* we take and free the key */
1.100     markus   1307:                        sensitive->keys[i] = NULL;
1.68      markus   1308:                        break;
                   1309:                }
                   1310:        }
                   1311:        if (!found) {
1.109     markus   1312:                debug("No more client hostkeys for hostbased authentication.");
1.68      markus   1313:                return 0;
                   1314:        }
                   1315:        if (key_to_blob(private, &blob, &blen) == 0) {
                   1316:                key_free(private);
                   1317:                return 0;
                   1318:        }
1.84      markus   1319:        /* figure out a name for the client host */
                   1320:        p = get_local_name(packet_get_connection_in());
                   1321:        if (p == NULL) {
                   1322:                error("userauth_hostbased: cannot get local ipaddr/name");
                   1323:                key_free(private);
                   1324:                return 0;
                   1325:        }
                   1326:        len = strlen(p) + 2;
                   1327:        chost = xmalloc(len);
                   1328:        strlcpy(chost, p, len);
                   1329:        strlcat(chost, ".", len);
                   1330:        debug2("userauth_hostbased: chost %s", chost);
1.112     markus   1331:        xfree(p);
1.84      markus   1332:
1.72      markus   1333:        service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
                   1334:            authctxt->service;
1.68      markus   1335:        pkalg = xstrdup(key_ssh_name(private));
                   1336:        buffer_init(&b);
                   1337:        /* construct data */
1.72      markus   1338:        buffer_put_string(&b, session_id2, session_id2_len);
1.68      markus   1339:        buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
                   1340:        buffer_put_cstring(&b, authctxt->server_user);
1.72      markus   1341:        buffer_put_cstring(&b, service);
1.68      markus   1342:        buffer_put_cstring(&b, authctxt->method->name);
                   1343:        buffer_put_cstring(&b, pkalg);
                   1344:        buffer_put_string(&b, blob, blen);
                   1345:        buffer_put_cstring(&b, chost);
                   1346:        buffer_put_cstring(&b, authctxt->local_user);
                   1347: #ifdef DEBUG_PK
                   1348:        buffer_dump(&b);
                   1349: #endif
1.100     markus   1350:        if (sensitive->external_keysign)
                   1351:                ok = ssh_keysign(private, &signature, &slen,
                   1352:                    buffer_ptr(&b), buffer_len(&b));
                   1353:        else
                   1354:                ok = key_sign(private, &signature, &slen,
                   1355:                    buffer_ptr(&b), buffer_len(&b));
1.68      markus   1356:        key_free(private);
                   1357:        buffer_free(&b);
                   1358:        if (ok != 0) {
                   1359:                error("key_sign failed");
                   1360:                xfree(chost);
                   1361:                xfree(pkalg);
                   1362:                return 0;
                   1363:        }
                   1364:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
                   1365:        packet_put_cstring(authctxt->server_user);
                   1366:        packet_put_cstring(authctxt->service);
                   1367:        packet_put_cstring(authctxt->method->name);
                   1368:        packet_put_cstring(pkalg);
                   1369:        packet_put_string(blob, blen);
                   1370:        packet_put_cstring(chost);
                   1371:        packet_put_cstring(authctxt->local_user);
                   1372:        packet_put_string(signature, slen);
                   1373:        memset(signature, 's', slen);
                   1374:        xfree(signature);
                   1375:        xfree(chost);
                   1376:        xfree(pkalg);
                   1377:
                   1378:        packet_send();
                   1379:        return 1;
1.23      markus   1380: }
1.20      markus   1381:
                   1382: /* find auth method */
                   1383:
                   1384: /*
                   1385:  * given auth method name, if configurable options permit this method fill
                   1386:  * in auth_ident field and return true, otherwise return false.
                   1387:  */
1.76      itojun   1388: static int
1.20      markus   1389: authmethod_is_enabled(Authmethod *method)
                   1390: {
                   1391:        if (method == NULL)
                   1392:                return 0;
                   1393:        /* return false if options indicate this method is disabled */
                   1394:        if  (method->enabled == NULL || *method->enabled == 0)
                   1395:                return 0;
                   1396:        /* return false if batch mode is enabled but method needs interactive mode */
                   1397:        if  (method->batch_flag != NULL && *method->batch_flag != 0)
                   1398:                return 0;
                   1399:        return 1;
                   1400: }
                   1401:
1.76      itojun   1402: static Authmethod *
1.20      markus   1403: authmethod_lookup(const char *name)
1.1       markus   1404: {
1.20      markus   1405:        Authmethod *method = NULL;
                   1406:        if (name != NULL)
                   1407:                for (method = authmethods; method->name != NULL; method++)
                   1408:                        if (strcmp(name, method->name) == 0)
                   1409:                                return method;
                   1410:        debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
                   1411:        return NULL;
                   1412: }
1.1       markus   1413:
1.53      markus   1414: /* XXX internal state */
                   1415: static Authmethod *current = NULL;
                   1416: static char *supported = NULL;
                   1417: static char *preferred = NULL;
1.105     deraadt  1418:
1.20      markus   1419: /*
                   1420:  * Given the authentication method list sent by the server, return the
                   1421:  * next method we should try.  If the server initially sends a nil list,
1.67      markus   1422:  * use a built-in default list.
1.41      stevesk  1423:  */
1.76      itojun   1424: static Authmethod *
1.20      markus   1425: authmethod_get(char *authlist)
                   1426: {
1.53      markus   1427:        char *name = NULL;
1.97      markus   1428:        u_int next;
1.41      stevesk  1429:
1.20      markus   1430:        /* Use a suitable default if we're passed a nil list.  */
                   1431:        if (authlist == NULL || strlen(authlist) == 0)
1.53      markus   1432:                authlist = options.preferred_authentications;
1.20      markus   1433:
1.53      markus   1434:        if (supported == NULL || strcmp(authlist, supported) != 0) {
                   1435:                debug3("start over, passed a different list %s", authlist);
                   1436:                if (supported != NULL)
                   1437:                        xfree(supported);
                   1438:                supported = xstrdup(authlist);
                   1439:                preferred = options.preferred_authentications;
                   1440:                debug3("preferred %s", preferred);
                   1441:                current = NULL;
                   1442:        } else if (current != NULL && authmethod_is_enabled(current))
                   1443:                return current;
1.20      markus   1444:
1.53      markus   1445:        for (;;) {
                   1446:                if ((name = match_list(preferred, supported, &next)) == NULL) {
1.109     markus   1447:                        debug("No more authentication methods to try.");
1.53      markus   1448:                        current = NULL;
                   1449:                        return NULL;
                   1450:                }
                   1451:                preferred += next;
1.23      markus   1452:                debug3("authmethod_lookup %s", name);
1.53      markus   1453:                debug3("remaining preferred: %s", preferred);
                   1454:                if ((current = authmethod_lookup(name)) != NULL &&
                   1455:                    authmethod_is_enabled(current)) {
1.23      markus   1456:                        debug3("authmethod_is_enabled %s", name);
1.109     markus   1457:                        debug("Next authentication method: %s", name);
1.53      markus   1458:                        return current;
1.23      markus   1459:                }
1.1       markus   1460:        }
1.53      markus   1461: }
1.1       markus   1462:
1.79      stevesk  1463: static char *
1.53      markus   1464: authmethods_get(void)
                   1465: {
                   1466:        Authmethod *method = NULL;
1.93      markus   1467:        Buffer b;
                   1468:        char *list;
1.27      provos   1469:
1.93      markus   1470:        buffer_init(&b);
1.53      markus   1471:        for (method = authmethods; method->name != NULL; method++) {
                   1472:                if (authmethod_is_enabled(method)) {
1.93      markus   1473:                        if (buffer_len(&b) > 0)
                   1474:                                buffer_append(&b, ",", 1);
                   1475:                        buffer_append(&b, method->name, strlen(method->name));
1.53      markus   1476:                }
                   1477:        }
1.93      markus   1478:        buffer_append(&b, "\0", 1);
                   1479:        list = xstrdup(buffer_ptr(&b));
                   1480:        buffer_free(&b);
                   1481:        return list;
1.1       markus   1482: }