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

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