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

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