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

1.306   ! deraadt     1: /* $OpenBSD: sshconnect2.c,v 1.305 2019/05/31 03:20:07 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.278     markus     46: #include "sshbuf.h"
1.1       markus     47: #include "packet.h"
                     48: #include "compat.h"
1.37      markus     49: #include "cipher.h"
1.278     markus     50: #include "sshkey.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.246     djm        68: #include "utf8.h"
1.22      provos     69:
1.121     markus     70: #ifdef GSSAPI
                     71: #include "ssh-gss.h"
                     72: #endif
1.293     djm        73:
1.1       markus     74: /* import */
                     75: extern char *client_version_string;
                     76: extern char *server_version_string;
                     77: extern Options options;
                     78:
                     79: /*
                     80:  * SSH2 key exchange
                     81:  */
                     82:
1.32      markus     83: u_char *session_id2 = NULL;
1.120     markus     84: u_int session_id2_len = 0;
1.1       markus     85:
1.61      markus     86: char *xxx_host;
                     87: struct sockaddr *xxx_hostaddr;
                     88:
1.76      itojun     89: static int
1.259     markus     90: verify_host_key_callback(struct sshkey *hostkey, struct ssh *ssh)
1.61      markus     91: {
1.75      markus     92:        if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
1.83      markus     93:                fatal("Host key verification failed.");
1.61      markus     94:        return 0;
                     95: }
                     96:
1.186     djm        97: static char *
                     98: order_hostkeyalgs(char *host, struct sockaddr *hostaddr, u_short port)
                     99: {
                    100:        char *oavail, *avail, *first, *last, *alg, *hostname, *ret;
                    101:        size_t maxlen;
                    102:        struct hostkeys *hostkeys;
                    103:        int ktype;
1.188     djm       104:        u_int i;
1.186     djm       105:
                    106:        /* Find all hostkeys for this hostname */
                    107:        get_hostfile_hostname_ipaddr(host, hostaddr, port, &hostname, NULL);
                    108:        hostkeys = init_hostkeys();
1.188     djm       109:        for (i = 0; i < options.num_user_hostfiles; i++)
                    110:                load_hostkeys(hostkeys, hostname, options.user_hostfiles[i]);
                    111:        for (i = 0; i < options.num_system_hostfiles; i++)
                    112:                load_hostkeys(hostkeys, hostname, options.system_hostfiles[i]);
1.186     djm       113:
                    114:        oavail = avail = xstrdup(KEX_DEFAULT_PK_ALG);
                    115:        maxlen = strlen(avail) + 1;
                    116:        first = xmalloc(maxlen);
                    117:        last = xmalloc(maxlen);
                    118:        *first = *last = '\0';
                    119:
                    120: #define ALG_APPEND(to, from) \
                    121:        do { \
                    122:                if (*to != '\0') \
                    123:                        strlcat(to, ",", maxlen); \
                    124:                strlcat(to, from, maxlen); \
                    125:        } while (0)
                    126:
                    127:        while ((alg = strsep(&avail, ",")) && *alg != '\0') {
1.214     djm       128:                if ((ktype = sshkey_type_from_name(alg)) == KEY_UNSPEC)
1.186     djm       129:                        fatal("%s: unknown alg %s", __func__, alg);
                    130:                if (lookup_key_in_hostkeys_by_type(hostkeys,
1.214     djm       131:                    sshkey_type_plain(ktype), NULL))
1.186     djm       132:                        ALG_APPEND(first, alg);
                    133:                else
                    134:                        ALG_APPEND(last, alg);
                    135:        }
                    136: #undef ALG_APPEND
1.216     djm       137:        xasprintf(&ret, "%s%s%s", first,
                    138:            (*first == '\0' || *last == '\0') ? "" : ",", last);
1.186     djm       139:        if (*first != '\0')
                    140:                debug3("%s: prefer hostkeyalgs: %s", __func__, first);
                    141:
1.197     djm       142:        free(first);
                    143:        free(last);
                    144:        free(hostname);
                    145:        free(oavail);
1.186     djm       146:        free_hostkeys(hostkeys);
                    147:
                    148:        return ret;
                    149: }
                    150:
1.1       markus    151: void
1.291     djm       152: ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port)
1.22      provos    153: {
1.205     markus    154:        char *myproposal[PROPOSAL_MAX] = { KEX_CLIENT };
1.275     djm       155:        char *s, *all_key;
1.221     markus    156:        int r;
1.61      markus    157:
                    158:        xxx_host = host;
                    159:        xxx_hostaddr = hostaddr;
1.22      provos    160:
1.231     markus    161:        if ((s = kex_names_cat(options.kex_algorithms, "ext-info-c")) == NULL)
                    162:                fatal("%s: kex_names_cat", __func__);
1.284     djm       163:        myproposal[PROPOSAL_KEX_ALGS] = compat_kex_proposal(s);
1.60      stevesk   164:        myproposal[PROPOSAL_ENC_ALGS_CTOS] =
1.284     djm       165:            compat_cipher_proposal(options.ciphers);
1.60      stevesk   166:        myproposal[PROPOSAL_ENC_ALGS_STOC] =
1.284     djm       167:            compat_cipher_proposal(options.ciphers);
1.245     dtucker   168:        myproposal[PROPOSAL_COMP_ALGS_CTOS] =
                    169:            myproposal[PROPOSAL_COMP_ALGS_STOC] = options.compression ?
1.277     sf        170:            "zlib@openssh.com,zlib,none" : "none,zlib@openssh.com,zlib";
1.226     djm       171:        myproposal[PROPOSAL_MAC_ALGS_CTOS] =
                    172:            myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
                    173:        if (options.hostkeyalgorithms != NULL) {
1.275     djm       174:                all_key = sshkey_alg_list(0, 0, 1, ',');
                    175:                if (kex_assemble_names(&options.hostkeyalgorithms,
                    176:                    KEX_DEFAULT_PK_ALG, all_key) != 0)
1.226     djm       177:                        fatal("%s: kex_assemble_namelist", __func__);
1.275     djm       178:                free(all_key);
1.88      deraadt   179:                myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
1.284     djm       180:                    compat_pkalg_proposal(options.hostkeyalgorithms);
1.226     djm       181:        } else {
1.225     markus    182:                /* Enforce default */
                    183:                options.hostkeyalgorithms = xstrdup(KEX_DEFAULT_PK_ALG);
1.186     djm       184:                /* Prefer algorithms that we already have keys for */
                    185:                myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
1.200     djm       186:                    compat_pkalg_proposal(
1.284     djm       187:                    order_hostkeyalgs(host, hostaddr, port));
1.186     djm       188:        }
1.115     markus    189:
1.196     dtucker   190:        if (options.rekey_limit || options.rekey_interval)
1.294     djm       191:                ssh_packet_set_rekey_limits(ssh, options.rekey_limit,
1.254     dtucker   192:                    options.rekey_interval);
1.22      provos    193:
1.65      markus    194:        /* start key exchange */
1.291     djm       195:        if ((r = kex_setup(ssh, myproposal)) != 0)
1.221     markus    196:                fatal("kex_setup: %s", ssh_err(r));
1.207     markus    197: #ifdef WITH_OPENSSL
1.301     djm       198:        ssh->kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_client;
                    199:        ssh->kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_client;
                    200:        ssh->kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_client;
                    201:        ssh->kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_client;
                    202:        ssh->kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_client;
1.291     djm       203:        ssh->kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
                    204:        ssh->kex->kex[KEX_DH_GEX_SHA256] = kexgex_client;
1.301     djm       205:        ssh->kex->kex[KEX_ECDH_SHA2] = kex_gen_client;
1.207     markus    206: #endif
1.301     djm       207:        ssh->kex->kex[KEX_C25519_SHA256] = kex_gen_client;
                    208:        ssh->kex->kex[KEX_KEM_SNTRUP4591761X25519_SHA512] = kex_gen_client;
1.291     djm       209:        ssh->kex->verify_host_key=&verify_host_key_callback;
1.22      provos    210:
1.291     djm       211:        ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &ssh->kex->done);
1.173     andreas   212:
1.231     markus    213:        /* remove ext-info from the KEX proposals for rekeying */
                    214:        myproposal[PROPOSAL_KEX_ALGS] =
1.284     djm       215:            compat_kex_proposal(options.kex_algorithms);
1.291     djm       216:        if ((r = kex_prop2buf(ssh->kex->my, myproposal)) != 0)
1.231     markus    217:                fatal("kex_prop2buf: %s", ssh_err(r));
1.62      markus    218:
1.291     djm       219:        session_id2 = ssh->kex->session_id;
                    220:        session_id2_len = ssh->kex->session_id_len;
1.22      provos    221:
                    222: #ifdef DEBUG_KEXDH
                    223:        /* send 1st encrypted/maced/compressed message */
1.278     markus    224:        if ((r = sshpkt_start(ssh, SSH2_MSG_IGNORE)) != 0 ||
                    225:            (r = sshpkt_put_cstring(ssh, "markus")) != 0 ||
1.280     markus    226:            (r = sshpkt_send(ssh)) != 0 ||
                    227:            (r = ssh_packet_write_wait(ssh)) != 0)
1.278     markus    228:                fatal("%s: %s", __func__, ssh_err(r));
1.22      provos    229: #endif
1.1       markus    230: }
1.11      markus    231:
1.1       markus    232: /*
                    233:  * Authenticate user
                    234:  */
1.20      markus    235:
1.214     djm       236: typedef struct cauthctxt Authctxt;
                    237: typedef struct cauthmethod Authmethod;
1.117     markus    238: typedef struct identity Identity;
                    239: typedef struct idlist Idlist;
1.20      markus    240:
1.117     markus    241: struct identity {
                    242:        TAILQ_ENTRY(identity) next;
1.214     djm       243:        int     agent_fd;               /* >=0 if agent supports key */
                    244:        struct sshkey   *key;           /* public/private key */
1.117     markus    245:        char    *filename;              /* comment for agent-only keys */
                    246:        int     tried;
                    247:        int     isprivate;              /* key points to the private key */
1.191     dtucker   248:        int     userprovided;
1.117     markus    249: };
                    250: TAILQ_HEAD(idlist, identity);
1.20      markus    251:
1.214     djm       252: struct cauthctxt {
1.20      markus    253:        const char *server_user;
1.68      markus    254:        const char *local_user;
1.20      markus    255:        const char *host;
                    256:        const char *service;
1.214     djm       257:        struct cauthmethod *method;
1.183     djm       258:        sig_atomic_t success;
1.51      markus    259:        char *authlist;
1.302     djm       260: #ifdef GSSAPI
                    261:        /* gssapi */
                    262:        gss_OID_set gss_supported_mechs;
                    263:        u_int mech_tried;
                    264: #endif
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.223     djm       270:        char *oktypes, *ktypes;
                    271:        const char *active_ktype;
1.82      markus    272:        /* kbd-interactive */
                    273:        int info_req_seen;
1.292     djm       274:        int attempt_kbdint;
                    275:        /* password */
                    276:        int attempt_passwd;
1.121     markus    277:        /* generic */
                    278:        void *methoddata;
1.20      markus    279: };
1.214     djm       280:
                    281: struct cauthmethod {
1.20      markus    282:        char    *name;          /* string to compare against server's list */
1.295     djm       283:        int     (*userauth)(struct ssh *ssh);
                    284:        void    (*cleanup)(struct ssh *ssh);
1.20      markus    285:        int     *enabled;       /* flag in option struct that enables method */
                    286:        int     *batch_flag;    /* flag in option struct that disables method */
                    287: };
                    288:
1.302     djm       289: static int input_userauth_service_accept(int, u_int32_t, struct ssh *);
                    290: static int input_userauth_ext_info(int, u_int32_t, struct ssh *);
                    291: static int input_userauth_success(int, u_int32_t, struct ssh *);
                    292: static int input_userauth_failure(int, u_int32_t, struct ssh *);
                    293: static int input_userauth_banner(int, u_int32_t, struct ssh *);
                    294: static int input_userauth_error(int, u_int32_t, struct ssh *);
                    295: static int input_userauth_info_req(int, u_int32_t, struct ssh *);
                    296: static int input_userauth_pk_ok(int, u_int32_t, struct ssh *);
                    297: static int input_userauth_passwd_changereq(int, u_int32_t, struct ssh *);
                    298:
                    299: static int userauth_none(struct ssh *);
                    300: static int userauth_pubkey(struct ssh *);
                    301: static int userauth_passwd(struct ssh *);
                    302: static int userauth_kbdint(struct ssh *);
                    303: static int userauth_hostbased(struct ssh *);
1.20      markus    304:
1.121     markus    305: #ifdef GSSAPI
1.302     djm       306: static int userauth_gssapi(struct ssh *);
                    307: static void userauth_gssapi_cleanup(struct ssh *);
                    308: static int input_gssapi_response(int type, u_int32_t, struct ssh *);
                    309: static int input_gssapi_token(int type, u_int32_t, struct ssh *);
                    310: static int input_gssapi_error(int, u_int32_t, struct ssh *);
                    311: static int input_gssapi_errtok(int, u_int32_t, struct ssh *);
1.121     markus    312: #endif
                    313:
1.295     djm       314: void   userauth(struct ssh *, char *);
1.51      markus    315:
1.303     djm       316: static void pubkey_cleanup(struct ssh *);
1.295     djm       317: static int sign_and_send_pubkey(struct ssh *ssh, Identity *);
1.117     markus    318: static void pubkey_prepare(Authctxt *);
1.251     djm       319: static void pubkey_reset(Authctxt *);
1.259     markus    320: static struct sshkey *load_identity_file(Identity *);
1.76      itojun    321:
                    322: static Authmethod *authmethod_get(char *authlist);
                    323: static Authmethod *authmethod_lookup(const char *name);
                    324: static char *authmethods_get(void);
1.20      markus    325:
                    326: Authmethod authmethods[] = {
1.121     markus    327: #ifdef GSSAPI
1.132     markus    328:        {"gssapi-with-mic",
1.121     markus    329:                userauth_gssapi,
1.302     djm       330:                userauth_gssapi_cleanup,
1.121     markus    331:                &options.gss_authentication,
                    332:                NULL},
                    333: #endif
1.81      markus    334:        {"hostbased",
                    335:                userauth_hostbased,
1.170     djm       336:                NULL,
1.81      markus    337:                &options.hostbased_authentication,
                    338:                NULL},
1.20      markus    339:        {"publickey",
                    340:                userauth_pubkey,
1.303     djm       341:                NULL,
1.28      markus    342:                &options.pubkey_authentication,
1.20      markus    343:                NULL},
1.81      markus    344:        {"keyboard-interactive",
                    345:                userauth_kbdint,
1.170     djm       346:                NULL,
1.81      markus    347:                &options.kbd_interactive_authentication,
                    348:                &options.batch_mode},
1.20      markus    349:        {"password",
                    350:                userauth_passwd,
1.170     djm       351:                NULL,
1.20      markus    352:                &options.password_authentication,
1.23      markus    353:                &options.batch_mode},
                    354:        {"none",
                    355:                userauth_none,
                    356:                NULL,
1.170     djm       357:                NULL,
1.23      markus    358:                NULL},
1.170     djm       359:        {NULL, NULL, NULL, NULL, NULL}
1.20      markus    360: };
                    361:
                    362: void
1.291     djm       363: ssh_userauth2(struct ssh *ssh, const char *local_user,
                    364:     const char *server_user, char *host, Sensitive *sensitive)
1.20      markus    365: {
                    366:        Authctxt authctxt;
1.231     markus    367:        int r;
1.39      markus    368:
1.73      markus    369:        if (options.challenge_response_authentication)
1.39      markus    370:                options.kbd_interactive_authentication = 1;
1.53      markus    371:        if (options.preferred_authentications == NULL)
                    372:                options.preferred_authentications = authmethods_get();
                    373:
1.20      markus    374:        /* setup authentication context */
1.82      markus    375:        memset(&authctxt, 0, sizeof(authctxt));
1.20      markus    376:        authctxt.server_user = server_user;
1.68      markus    377:        authctxt.local_user = local_user;
1.20      markus    378:        authctxt.host = host;
                    379:        authctxt.service = "ssh-connection";            /* service name */
                    380:        authctxt.success = 0;
1.23      markus    381:        authctxt.method = authmethod_lookup("none");
1.51      markus    382:        authctxt.authlist = NULL;
1.121     markus    383:        authctxt.methoddata = NULL;
1.100     markus    384:        authctxt.sensitive = sensitive;
1.223     djm       385:        authctxt.active_ktype = authctxt.oktypes = authctxt.ktypes = NULL;
1.82      markus    386:        authctxt.info_req_seen = 0;
1.292     djm       387:        authctxt.attempt_kbdint = 0;
                    388:        authctxt.attempt_passwd = 0;
1.302     djm       389: #if GSSAPI
                    390:        authctxt.gss_supported_mechs = NULL;
                    391:        authctxt.mech_tried = 0;
                    392: #endif
1.215     djm       393:        authctxt.agent_fd = -1;
1.289     djm       394:        pubkey_prepare(&authctxt);
1.291     djm       395:        if (authctxt.method == NULL) {
                    396:                fatal("%s: internal error: cannot send userauth none request",
                    397:                    __func__);
                    398:        }
1.20      markus    399:
1.231     markus    400:        if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_REQUEST)) != 0 ||
                    401:            (r = sshpkt_put_cstring(ssh, "ssh-userauth")) != 0 ||
                    402:            (r = sshpkt_send(ssh)) != 0)
                    403:                fatal("%s: %s", __func__, ssh_err(r));
                    404:
1.260     markus    405:        ssh->authctxt = &authctxt;
1.231     markus    406:        ssh_dispatch_init(ssh, &input_userauth_error);
                    407:        ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_ext_info);
                    408:        ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_ACCEPT, &input_userauth_service_accept);
1.263     markus    409:        ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt.success); /* loop until success */
1.303     djm       410:        pubkey_cleanup(ssh);
1.260     markus    411:        ssh->authctxt = NULL;
1.20      markus    412:
1.231     markus    413:        ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_MIN, SSH2_MSG_USERAUTH_MAX, NULL);
1.119     markus    414:
1.248     dtucker   415:        if (!authctxt.success)
                    416:                fatal("Authentication failed.");
1.109     markus    417:        debug("Authentication succeeded (%s).", authctxt.method->name);
1.20      markus    418: }
1.119     markus    419:
1.231     markus    420: /* ARGSUSED */
1.302     djm       421: static int
1.261     markus    422: input_userauth_service_accept(int type, u_int32_t seq, struct ssh *ssh)
1.231     markus    423: {
                    424:        int r;
                    425:
                    426:        if (ssh_packet_remaining(ssh) > 0) {
                    427:                char *reply;
                    428:
                    429:                if ((r = sshpkt_get_cstring(ssh, &reply, NULL)) != 0)
                    430:                        goto out;
                    431:                debug2("service_accept: %s", reply);
                    432:                free(reply);
                    433:        } else {
                    434:                debug2("buggy server: service_accept w/o service");
                    435:        }
                    436:        if ((r = sshpkt_get_end(ssh)) != 0)
                    437:                goto out;
                    438:        debug("SSH2_MSG_SERVICE_ACCEPT received");
                    439:
                    440:        /* initial userauth request */
1.295     djm       441:        userauth_none(ssh);
1.231     markus    442:
                    443:        ssh_dispatch_set(ssh, SSH2_MSG_EXT_INFO, &input_userauth_error);
                    444:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
                    445:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
                    446:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
                    447:        r = 0;
                    448:  out:
                    449:        return r;
                    450: }
                    451:
                    452: /* ARGSUSED */
1.302     djm       453: static int
1.261     markus    454: input_userauth_ext_info(int type, u_int32_t seqnr, struct ssh *ssh)
1.231     markus    455: {
1.261     markus    456:        return kex_input_ext_info(type, seqnr, ssh);
1.231     markus    457: }
                    458:
1.20      markus    459: void
1.295     djm       460: userauth(struct ssh *ssh, char *authlist)
1.51      markus    461: {
1.295     djm       462:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.278     markus    463:
1.170     djm       464:        if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
1.295     djm       465:                authctxt->method->cleanup(ssh);
1.170     djm       466:
1.197     djm       467:        free(authctxt->methoddata);
                    468:        authctxt->methoddata = NULL;
1.51      markus    469:        if (authlist == NULL) {
                    470:                authlist = authctxt->authlist;
                    471:        } else {
1.197     djm       472:                free(authctxt->authlist);
1.51      markus    473:                authctxt->authlist = authlist;
                    474:        }
                    475:        for (;;) {
                    476:                Authmethod *method = authmethod_get(authlist);
                    477:                if (method == NULL)
1.264     dtucker   478:                        fatal("%s@%s: Permission denied (%s).",
                    479:                            authctxt->server_user, authctxt->host, authlist);
1.51      markus    480:                authctxt->method = method;
1.119     markus    481:
                    482:                /* reset the per method handler */
1.278     markus    483:                ssh_dispatch_range(ssh, SSH2_MSG_USERAUTH_PER_METHOD_MIN,
1.119     markus    484:                    SSH2_MSG_USERAUTH_PER_METHOD_MAX, NULL);
                    485:
                    486:                /* and try new method */
1.295     djm       487:                if (method->userauth(ssh) != 0) {
1.51      markus    488:                        debug2("we sent a %s packet, wait for reply", method->name);
                    489:                        break;
                    490:                } else {
                    491:                        debug2("we did not send a packet, disable method");
                    492:                        method->enabled = NULL;
                    493:                }
                    494:        }
                    495: }
1.105     deraadt   496:
1.169     djm       497: /* ARGSUSED */
1.302     djm       498: static int
1.261     markus    499: input_userauth_error(int type, u_int32_t seq, struct ssh *ssh)
1.20      markus    500: {
1.295     djm       501:        fatal("%s: bad message during authentication: type %d", __func__, type);
1.218     markus    502:        return 0;
1.35      markus    503: }
1.105     deraadt   504:
1.169     djm       505: /* ARGSUSED */
1.302     djm       506: static int
1.261     markus    507: input_userauth_banner(int type, u_int32_t seq, struct ssh *ssh)
1.35      markus    508: {
1.295     djm       509:        char *msg = NULL;
1.294     djm       510:        size_t len;
                    511:        int r;
1.126     deraadt   512:
1.246     djm       513:        debug3("%s", __func__);
1.294     djm       514:        if ((r = sshpkt_get_cstring(ssh, &msg, &len)) != 0 ||
1.295     djm       515:            (r = sshpkt_get_cstring(ssh, NULL, NULL)) != 0)
1.294     djm       516:                goto out;
1.246     djm       517:        if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO)
                    518:                fmprintf(stderr, "%s", msg);
1.294     djm       519:        r = 0;
                    520:  out:
1.246     djm       521:        free(msg);
1.294     djm       522:        return r;
1.20      markus    523: }
1.105     deraadt   524:
1.169     djm       525: /* ARGSUSED */
1.302     djm       526: static int
1.261     markus    527: input_userauth_success(int type, u_int32_t seq, struct ssh *ssh)
1.20      markus    528: {
1.260     markus    529:        Authctxt *authctxt = ssh->authctxt;
1.172     djm       530:
1.20      markus    531:        if (authctxt == NULL)
1.295     djm       532:                fatal("%s: no authentication context", __func__);
1.197     djm       533:        free(authctxt->authlist);
                    534:        authctxt->authlist = NULL;
1.172     djm       535:        if (authctxt->method != NULL && authctxt->method->cleanup != NULL)
1.295     djm       536:                authctxt->method->cleanup(ssh);
1.197     djm       537:        free(authctxt->methoddata);
                    538:        authctxt->methoddata = NULL;
1.20      markus    539:        authctxt->success = 1;                  /* break out */
1.218     markus    540:        return 0;
1.20      markus    541: }
1.105     deraadt   542:
1.302     djm       543: #if 0
                    544: static int
1.261     markus    545: input_userauth_success_unexpected(int type, u_int32_t seq, struct ssh *ssh)
1.172     djm       546: {
1.260     markus    547:        Authctxt *authctxt = ssh->authctxt;
1.172     djm       548:
                    549:        if (authctxt == NULL)
                    550:                fatal("%s: no authentication context", __func__);
                    551:
                    552:        fatal("Unexpected authentication success during %s.",
                    553:            authctxt->method->name);
1.218     markus    554:        return 0;
1.172     djm       555: }
1.302     djm       556: #endif
1.172     djm       557:
1.169     djm       558: /* ARGSUSED */
1.302     djm       559: static int
1.261     markus    560: input_userauth_failure(int type, u_int32_t seq, struct ssh *ssh)
1.20      markus    561: {
1.260     markus    562:        Authctxt *authctxt = ssh->authctxt;
1.20      markus    563:        char *authlist = NULL;
1.278     markus    564:        u_char partial;
                    565:        int r;
1.20      markus    566:
                    567:        if (authctxt == NULL)
                    568:                fatal("input_userauth_failure: no authentication context");
                    569:
1.278     markus    570:        if ((r = sshpkt_get_cstring(ssh, &authlist, NULL)) != 0 ||
                    571:            (r = sshpkt_get_u8(ssh, &partial)) != 0 ||
                    572:            (r = sshpkt_get_end(ssh)) != 0)
                    573:                goto out;
1.20      markus    574:
1.193     markus    575:        if (partial != 0) {
1.247     dtucker   576:                verbose("Authenticated with partial success.");
1.193     markus    577:                /* reset state */
1.251     djm       578:                pubkey_reset(authctxt);
1.193     markus    579:        }
1.109     markus    580:        debug("Authentications that can continue: %s", authlist);
1.20      markus    581:
1.295     djm       582:        userauth(ssh, authlist);
1.278     markus    583:        authlist = NULL;
                    584:  out:
                    585:        free(authlist);
1.218     markus    586:        return 0;
1.51      markus    587: }
1.169     djm       588:
1.287     djm       589: /*
                    590:  * Format an identity for logging including filename, key type, fingerprint
                    591:  * and location (agent, etc.). Caller must free.
                    592:  */
                    593: static char *
                    594: format_identity(Identity *id)
                    595: {
                    596:        char *fp = NULL, *ret = NULL;
                    597:
                    598:        if (id->key != NULL) {
                    599:             fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
                    600:                    SSH_FP_DEFAULT);
                    601:        }
                    602:        xasprintf(&ret, "%s %s%s%s%s%s%s",
                    603:            id->filename,
                    604:            id->key ? sshkey_type(id->key) : "", id->key ? " " : "",
                    605:            fp ? fp : "",
                    606:            id->userprovided ? " explicit" : "",
                    607:            (id->key && (id->key->flags & SSHKEY_FLAG_EXT)) ? " token" : "",
                    608:            id->agent_fd != -1 ? " agent" : "");
                    609:        free(fp);
                    610:        return ret;
                    611: }
                    612:
1.169     djm       613: /* ARGSUSED */
1.302     djm       614: static int
1.261     markus    615: input_userauth_pk_ok(int type, u_int32_t seq, struct ssh *ssh)
1.51      markus    616: {
1.260     markus    617:        Authctxt *authctxt = ssh->authctxt;
1.259     markus    618:        struct sshkey *key = NULL;
1.117     markus    619:        Identity *id = NULL;
1.287     djm       620:        int pktype, found = 0, sent = 0;
1.278     markus    621:        size_t blen;
1.287     djm       622:        char *pkalg = NULL, *fp = NULL, *ident = NULL;
1.278     markus    623:        u_char *pkblob = NULL;
                    624:        int r;
1.51      markus    625:
                    626:        if (authctxt == NULL)
                    627:                fatal("input_userauth_pk_ok: no authentication context");
1.267     djm       628:
1.278     markus    629:        if ((r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
                    630:            (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0 ||
                    631:            (r = sshpkt_get_end(ssh)) != 0)
                    632:                goto done;
1.51      markus    633:
1.278     markus    634:        if ((pktype = sshkey_type_from_name(pkalg)) == KEY_UNSPEC) {
1.287     djm       635:                debug("%s: server sent unknown pkalg %s", __func__, pkalg);
1.117     markus    636:                goto done;
                    637:        }
1.278     markus    638:        if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
                    639:                debug("no key from blob. pkalg %s: %s", pkalg, ssh_err(r));
1.117     markus    640:                goto done;
                    641:        }
                    642:        if (key->type != pktype) {
                    643:                error("input_userauth_pk_ok: type mismatch "
                    644:                    "for decoded key (received %d, expected %d)",
                    645:                    key->type, pktype);
                    646:                goto done;
                    647:        }
                    648:
1.127     markus    649:        /*
                    650:         * search keys in the reverse order, because last candidate has been
                    651:         * moved to the end of the queue.  this also avoids confusion by
                    652:         * duplicate keys
                    653:         */
1.136     henning   654:        TAILQ_FOREACH_REVERSE(id, &authctxt->keys, idlist, next) {
1.278     markus    655:                if (sshkey_equal(key, id->key)) {
1.287     djm       656:                        found = 1;
1.51      markus    657:                        break;
                    658:                }
1.117     markus    659:        }
1.287     djm       660:        if (!found || id == NULL) {
                    661:                fp = sshkey_fingerprint(key, options.fingerprint_hash,
                    662:                    SSH_FP_DEFAULT);
                    663:                error("%s: server replied with unknown key: %s %s", __func__,
                    664:                    sshkey_type(key), fp == NULL ? "<ERROR>" : fp);
                    665:                goto done;
                    666:        }
                    667:        ident = format_identity(id);
                    668:        debug("Server accepts key: %s", ident);
1.295     djm       669:        sent = sign_and_send_pubkey(ssh, id);
1.278     markus    670:        r = 0;
                    671:  done:
                    672:        sshkey_free(key);
1.287     djm       673:        free(ident);
                    674:        free(fp);
1.197     djm       675:        free(pkalg);
                    676:        free(pkblob);
1.51      markus    677:
1.106     deraadt   678:        /* try another method if we did not send a packet */
1.278     markus    679:        if (r == 0 && sent == 0)
1.295     djm       680:                userauth(ssh, NULL);
1.278     markus    681:        return r;
1.20      markus    682: }
1.121     markus    683:
                    684: #ifdef GSSAPI
1.302     djm       685: static int
1.295     djm       686: userauth_gssapi(struct ssh *ssh)
1.121     markus    687: {
1.295     djm       688:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.121     markus    689:        Gssctxt *gssctxt = NULL;
                    690:        OM_uint32 min;
1.278     markus    691:        int r, ok = 0;
1.302     djm       692:        gss_OID mech = NULL;
1.121     markus    693:
                    694:        /* Try one GSSAPI method at a time, rather than sending them all at
                    695:         * once. */
                    696:
1.302     djm       697:        if (authctxt->gss_supported_mechs == NULL)
                    698:                gss_indicate_mechs(&min, &authctxt->gss_supported_mechs);
1.121     markus    699:
1.302     djm       700:        /* Check to see whether the mechanism is usable before we offer it */
                    701:        while (authctxt->mech_tried < authctxt->gss_supported_mechs->count &&
                    702:            !ok) {
                    703:                mech = &authctxt->gss_supported_mechs->
                    704:                    elements[authctxt->mech_tried];
1.121     markus    705:                /* My DER encoding requires length<128 */
1.302     djm       706:                if (mech->length < 128 && ssh_gssapi_check_mechanism(&gssctxt,
                    707:                    mech, authctxt->host)) {
1.121     markus    708:                        ok = 1; /* Mechanism works */
                    709:                } else {
1.302     djm       710:                        authctxt->mech_tried++;
1.121     markus    711:                }
                    712:        }
                    713:
1.302     djm       714:        if (!ok || mech == NULL)
1.139     djm       715:                return 0;
1.121     markus    716:
                    717:        authctxt->methoddata=(void *)gssctxt;
                    718:
1.278     markus    719:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                    720:            (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
                    721:            (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
                    722:            (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
                    723:            (r = sshpkt_put_u32(ssh, 1)) != 0 ||
1.302     djm       724:            (r = sshpkt_put_u32(ssh, (mech->length) + 2)) != 0 ||
1.278     markus    725:            (r = sshpkt_put_u8(ssh, SSH_GSS_OIDTYPE)) != 0 ||
1.302     djm       726:            (r = sshpkt_put_u8(ssh, mech->length)) != 0 ||
                    727:            (r = sshpkt_put(ssh, mech->elements, mech->length)) != 0 ||
1.278     markus    728:            (r = sshpkt_send(ssh)) != 0)
                    729:                fatal("%s: %s", __func__, ssh_err(r));
                    730:
                    731:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_RESPONSE, &input_gssapi_response);
                    732:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, &input_gssapi_token);
                    733:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERROR, &input_gssapi_error);
                    734:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_ERRTOK, &input_gssapi_errtok);
1.121     markus    735:
1.302     djm       736:        authctxt->mech_tried++; /* Move along to next candidate */
1.121     markus    737:
                    738:        return 1;
                    739: }
                    740:
1.302     djm       741: static void
                    742: userauth_gssapi_cleanup(struct ssh *ssh)
                    743: {
                    744:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
                    745:        Gssctxt *gssctxt = (Gssctxt *)authctxt->methoddata;
                    746:
                    747:        ssh_gssapi_delete_ctx(&gssctxt);
                    748:        authctxt->methoddata = NULL;
                    749:
                    750:        free(authctxt->gss_supported_mechs);
                    751:        authctxt->gss_supported_mechs = NULL;
                    752: }
                    753:
1.130     markus    754: static OM_uint32
1.262     djm       755: process_gssapi_token(struct ssh *ssh, gss_buffer_t recv_tok)
1.130     markus    756: {
1.260     markus    757:        Authctxt *authctxt = ssh->authctxt;
1.130     markus    758:        Gssctxt *gssctxt = authctxt->methoddata;
                    759:        gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
1.142     djm       760:        gss_buffer_desc mic = GSS_C_EMPTY_BUFFER;
                    761:        gss_buffer_desc gssbuf;
1.132     markus    762:        OM_uint32 status, ms, flags;
1.278     markus    763:        int r;
1.133     djm       764:
1.130     markus    765:        status = ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
1.132     markus    766:            recv_tok, &send_tok, &flags);
1.130     markus    767:
                    768:        if (send_tok.length > 0) {
1.278     markus    769:                u_char type = GSS_ERROR(status) ?
                    770:                    SSH2_MSG_USERAUTH_GSSAPI_ERRTOK :
                    771:                    SSH2_MSG_USERAUTH_GSSAPI_TOKEN;
                    772:
                    773:                if ((r = sshpkt_start(ssh, type)) != 0 ||
                    774:                    (r = sshpkt_put_string(ssh, send_tok.value,
                    775:                    send_tok.length)) != 0 ||
                    776:                    (r = sshpkt_send(ssh)) != 0)
                    777:                        fatal("%s: %s", __func__, ssh_err(r));
1.133     djm       778:
1.130     markus    779:                gss_release_buffer(&ms, &send_tok);
                    780:        }
1.133     djm       781:
1.130     markus    782:        if (status == GSS_S_COMPLETE) {
1.132     markus    783:                /* send either complete or MIC, depending on mechanism */
                    784:                if (!(flags & GSS_C_INTEG_FLAG)) {
1.278     markus    785:                        if ((r = sshpkt_start(ssh,
                    786:                            SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE)) != 0 ||
                    787:                            (r = sshpkt_send(ssh)) != 0)
                    788:                                fatal("%s: %s", __func__, ssh_err(r));
1.132     markus    789:                } else {
1.278     markus    790:                        struct sshbuf *b;
                    791:
                    792:                        if ((b = sshbuf_new()) == NULL)
                    793:                                fatal("%s: sshbuf_new failed", __func__);
                    794:                        ssh_gssapi_buildmic(b, authctxt->server_user,
1.132     markus    795:                            authctxt->service, "gssapi-with-mic");
                    796:
1.278     markus    797:                        if ((gssbuf.value = sshbuf_mutable_ptr(b)) == NULL)
                    798:                                fatal("%s: sshbuf_mutable_ptr failed", __func__);
                    799:                        gssbuf.length = sshbuf_len(b);
1.133     djm       800:
1.132     markus    801:                        status = ssh_gssapi_sign(gssctxt, &gssbuf, &mic);
1.133     djm       802:
1.132     markus    803:                        if (!GSS_ERROR(status)) {
1.278     markus    804:                                if ((r = sshpkt_start(ssh,
                    805:                                    SSH2_MSG_USERAUTH_GSSAPI_MIC)) != 0 ||
                    806:                                    (r = sshpkt_put_string(ssh, mic.value,
                    807:                                    mic.length)) != 0 ||
                    808:                                    (r = sshpkt_send(ssh)) != 0)
                    809:                                        fatal("%s: %s", __func__, ssh_err(r));
1.132     markus    810:                        }
1.133     djm       811:
1.278     markus    812:                        sshbuf_free(b);
1.132     markus    813:                        gss_release_buffer(&ms, &mic);
1.133     djm       814:                }
1.130     markus    815:        }
1.133     djm       816:
1.130     markus    817:        return status;
                    818: }
                    819:
1.169     djm       820: /* ARGSUSED */
1.302     djm       821: static int
1.261     markus    822: input_gssapi_response(int type, u_int32_t plen, struct ssh *ssh)
1.121     markus    823: {
1.260     markus    824:        Authctxt *authctxt = ssh->authctxt;
1.121     markus    825:        Gssctxt *gssctxt;
1.278     markus    826:        size_t oidlen;
                    827:        u_char *oidv = NULL;
                    828:        int r;
1.121     markus    829:
                    830:        if (authctxt == NULL)
                    831:                fatal("input_gssapi_response: no authentication context");
                    832:        gssctxt = authctxt->methoddata;
                    833:
                    834:        /* Setup our OID */
1.278     markus    835:        if ((r = sshpkt_get_string(ssh, &oidv, &oidlen)) != 0)
                    836:                goto done;
1.121     markus    837:
1.129     markus    838:        if (oidlen <= 2 ||
                    839:            oidv[0] != SSH_GSS_OIDTYPE ||
                    840:            oidv[1] != oidlen - 2) {
                    841:                debug("Badly encoded mechanism OID received");
1.296     djm       842:                userauth(ssh, NULL);
1.278     markus    843:                goto ok;
1.121     markus    844:        }
1.129     markus    845:
                    846:        if (!ssh_gssapi_check_oid(gssctxt, oidv + 2, oidlen - 2))
                    847:                fatal("Server returned different OID than expected");
1.121     markus    848:
1.278     markus    849:        if ((r = sshpkt_get_end(ssh)) != 0)
                    850:                goto done;
1.121     markus    851:
1.262     djm       852:        if (GSS_ERROR(process_gssapi_token(ssh, GSS_C_NO_BUFFER))) {
1.121     markus    853:                /* Start again with next method on list */
                    854:                debug("Trying to start again");
1.296     djm       855:                userauth(ssh, NULL);
1.278     markus    856:                goto ok;
1.121     markus    857:        }
1.278     markus    858:  ok:
                    859:        r = 0;
                    860:  done:
                    861:        free(oidv);
                    862:        return r;
1.121     markus    863: }
                    864:
1.169     djm       865: /* ARGSUSED */
1.302     djm       866: static int
1.261     markus    867: input_gssapi_token(int type, u_int32_t plen, struct ssh *ssh)
1.121     markus    868: {
1.260     markus    869:        Authctxt *authctxt = ssh->authctxt;
1.121     markus    870:        gss_buffer_desc recv_tok;
1.278     markus    871:        u_char *p = NULL;
                    872:        size_t len;
1.130     markus    873:        OM_uint32 status;
1.278     markus    874:        int r;
1.121     markus    875:
                    876:        if (authctxt == NULL)
                    877:                fatal("input_gssapi_response: no authentication context");
                    878:
1.278     markus    879:        if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
                    880:            (r = sshpkt_get_end(ssh)) != 0)
                    881:                goto out;
1.121     markus    882:
1.278     markus    883:        recv_tok.value = p;
                    884:        recv_tok.length = len;
1.262     djm       885:        status = process_gssapi_token(ssh, &recv_tok);
1.121     markus    886:
1.278     markus    887:        /* Start again with the next method in the list */
1.121     markus    888:        if (GSS_ERROR(status)) {
1.296     djm       889:                userauth(ssh, NULL);
1.278     markus    890:                /* ok */
1.121     markus    891:        }
1.278     markus    892:        r = 0;
                    893:  out:
                    894:        free(p);
                    895:        return r;
1.121     markus    896: }
                    897:
1.169     djm       898: /* ARGSUSED */
1.302     djm       899: static int
1.261     markus    900: input_gssapi_errtok(int type, u_int32_t plen, struct ssh *ssh)
1.121     markus    901: {
1.260     markus    902:        Authctxt *authctxt = ssh->authctxt;
1.121     markus    903:        Gssctxt *gssctxt;
                    904:        gss_buffer_desc send_tok = GSS_C_EMPTY_BUFFER;
                    905:        gss_buffer_desc recv_tok;
1.194     djm       906:        OM_uint32 ms;
1.278     markus    907:        u_char *p = NULL;
                    908:        size_t len;
                    909:        int r;
1.121     markus    910:
                    911:        if (authctxt == NULL)
                    912:                fatal("input_gssapi_response: no authentication context");
                    913:        gssctxt = authctxt->methoddata;
                    914:
1.278     markus    915:        if ((r = sshpkt_get_string(ssh, &p, &len)) != 0 ||
                    916:            (r = sshpkt_get_end(ssh)) != 0) {
                    917:                free(p);
                    918:                return r;
                    919:        }
1.121     markus    920:
                    921:        /* Stick it into GSSAPI and see what it says */
1.278     markus    922:        recv_tok.value = p;
                    923:        recv_tok.length = len;
1.194     djm       924:        (void)ssh_gssapi_init_ctx(gssctxt, options.gss_deleg_creds,
1.140     djm       925:            &recv_tok, &send_tok, NULL);
1.278     markus    926:        free(p);
1.121     markus    927:        gss_release_buffer(&ms, &send_tok);
                    928:
                    929:        /* Server will be returning a failed packet after this one */
1.220     djm       930:        return 0;
1.121     markus    931: }
                    932:
1.169     djm       933: /* ARGSUSED */
1.302     djm       934: static int
1.261     markus    935: input_gssapi_error(int type, u_int32_t plen, struct ssh *ssh)
1.121     markus    936: {
1.278     markus    937:        char *msg = NULL;
                    938:        char *lang = NULL;
                    939:        int r;
1.121     markus    940:
1.278     markus    941:        if ((r = sshpkt_get_u32(ssh, NULL)) != 0 ||     /* maj */
                    942:            (r = sshpkt_get_u32(ssh, NULL)) != 0 ||     /* min */
                    943:            (r = sshpkt_get_cstring(ssh, &msg, NULL)) != 0 ||
                    944:            (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
                    945:                goto out;
                    946:        r = sshpkt_get_end(ssh);
1.143     stevesk   947:        debug("Server GSSAPI Error:\n%s", msg);
1.278     markus    948:  out:
1.197     djm       949:        free(msg);
                    950:        free(lang);
1.278     markus    951:        return r;
1.121     markus    952: }
                    953: #endif /* GSSAPI */
1.20      markus    954:
1.302     djm       955: static int
1.295     djm       956: userauth_none(struct ssh *ssh)
1.23      markus    957: {
1.295     djm       958:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.278     markus    959:        int r;
                    960:
1.23      markus    961:        /* initial userauth request */
1.278     markus    962:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                    963:            (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
                    964:            (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
                    965:            (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
                    966:            (r = sshpkt_send(ssh)) != 0)
                    967:                fatal("%s: %s", __func__, ssh_err(r));
1.23      markus    968:        return 1;
                    969: }
                    970:
1.302     djm       971: static int
1.295     djm       972: userauth_passwd(struct ssh *ssh)
1.1       markus    973: {
1.295     djm       974:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.290     djm       975:        char *password, *prompt = NULL;
1.175     dtucker   976:        const char *host = options.host_key_alias ?  options.host_key_alias :
                    977:            authctxt->host;
1.278     markus    978:        int r;
1.6       markus    979:
1.292     djm       980:        if (authctxt->attempt_passwd++ >= options.number_of_password_prompts)
1.6       markus    981:                return 0;
1.13      todd      982:
1.292     djm       983:        if (authctxt->attempt_passwd != 1)
1.13      todd      984:                error("Permission denied, please try again.");
1.1       markus    985:
1.290     djm       986:        xasprintf(&prompt, "%s@%s's password: ", authctxt->server_user, host);
1.1       markus    987:        password = read_passphrase(prompt, 0);
1.278     markus    988:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                    989:            (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
                    990:            (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
                    991:            (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
                    992:            (r = sshpkt_put_u8(ssh, 0)) != 0 ||
                    993:            (r = sshpkt_put_cstring(ssh, password)) != 0 ||
                    994:            (r = sshpkt_add_padding(ssh, 64)) != 0 ||
                    995:            (r = sshpkt_send(ssh)) != 0)
                    996:                fatal("%s: %s", __func__, ssh_err(r));
1.99      markus    997:
1.290     djm       998:        free(prompt);
                    999:        if (password != NULL)
1.278     markus   1000:                freezero(password, strlen(password));
                   1001:
                   1002:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99      markus   1003:            &input_userauth_passwd_changereq);
                   1004:
1.1       markus   1005:        return 1;
                   1006: }
1.169     djm      1007:
1.99      markus   1008: /*
                   1009:  * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
                   1010:  */
1.169     djm      1011: /* ARGSUSED */
1.302     djm      1012: static int
1.261     markus   1013: input_userauth_passwd_changereq(int type, u_int32_t seqnr, struct ssh *ssh)
1.99      markus   1014: {
1.260     markus   1015:        Authctxt *authctxt = ssh->authctxt;
1.278     markus   1016:        char *info = NULL, *lang = NULL, *password = NULL, *retype = NULL;
1.266     dtucker  1017:        char prompt[256];
1.252     djm      1018:        const char *host;
1.278     markus   1019:        int r;
1.99      markus   1020:
                   1021:        debug2("input_userauth_passwd_changereq");
                   1022:
                   1023:        if (authctxt == NULL)
                   1024:                fatal("input_userauth_passwd_changereq: "
                   1025:                    "no authentication context");
1.252     djm      1026:        host = options.host_key_alias ? options.host_key_alias : authctxt->host;
1.99      markus   1027:
1.278     markus   1028:        if ((r = sshpkt_get_cstring(ssh, &info, NULL)) != 0 ||
                   1029:            (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
                   1030:                goto out;
1.99      markus   1031:        if (strlen(info) > 0)
1.116     itojun   1032:                logit("%s", info);
1.278     markus   1033:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                   1034:            (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
                   1035:            (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
                   1036:            (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
                   1037:            (r = sshpkt_put_u8(ssh, 1)) != 0)   /* additional info */
                   1038:                goto out;
                   1039:
1.104     deraadt  1040:        snprintf(prompt, sizeof(prompt),
1.99      markus   1041:            "Enter %.30s@%.128s's old password: ",
1.175     dtucker  1042:            authctxt->server_user, host);
1.99      markus   1043:        password = read_passphrase(prompt, 0);
1.278     markus   1044:        if ((r = sshpkt_put_cstring(ssh, password)) != 0)
                   1045:                goto out;
                   1046:
                   1047:        freezero(password, strlen(password));
1.99      markus   1048:        password = NULL;
                   1049:        while (password == NULL) {
1.104     deraadt  1050:                snprintf(prompt, sizeof(prompt),
1.99      markus   1051:                    "Enter %.30s@%.128s's new password: ",
1.175     dtucker  1052:                    authctxt->server_user, host);
1.99      markus   1053:                password = read_passphrase(prompt, RP_ALLOW_EOF);
                   1054:                if (password == NULL) {
                   1055:                        /* bail out */
1.278     markus   1056:                        r = 0;
                   1057:                        goto out;
1.99      markus   1058:                }
1.104     deraadt  1059:                snprintf(prompt, sizeof(prompt),
1.99      markus   1060:                    "Retype %.30s@%.128s's new password: ",
1.175     dtucker  1061:                    authctxt->server_user, host);
1.99      markus   1062:                retype = read_passphrase(prompt, 0);
                   1063:                if (strcmp(password, retype) != 0) {
1.278     markus   1064:                        freezero(password, strlen(password));
1.116     itojun   1065:                        logit("Mismatch; try again, EOF to quit.");
1.99      markus   1066:                        password = NULL;
                   1067:                }
1.278     markus   1068:                freezero(retype, strlen(retype));
1.99      markus   1069:        }
1.278     markus   1070:        if ((r = sshpkt_put_cstring(ssh, password)) != 0 ||
                   1071:            (r = sshpkt_add_padding(ssh, 64)) != 0 ||
                   1072:            (r = sshpkt_send(ssh)) != 0)
                   1073:                goto out;
1.104     deraadt  1074:
1.278     markus   1075:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99      markus   1076:            &input_userauth_passwd_changereq);
1.278     markus   1077:        r = 0;
                   1078:  out:
                   1079:        if (password)
                   1080:                freezero(password, strlen(password));
                   1081:        free(info);
                   1082:        free(lang);
                   1083:        return r;
1.117     markus   1084: }
                   1085:
1.272     djm      1086: /*
                   1087:  * Select an algorithm for publickey signatures.
                   1088:  * Returns algorithm (caller must free) or NULL if no mutual algorithm found.
                   1089:  *
                   1090:  * Call with ssh==NULL to ignore server-sig-algs extension list and
                   1091:  * only attempt with the key's base signature type.
                   1092:  */
                   1093: static char *
                   1094: key_sig_algorithm(struct ssh *ssh, const struct sshkey *key)
1.231     markus   1095: {
1.274     djm      1096:        char *allowed, *oallowed, *cp, *tmp, *alg = NULL;
1.231     markus   1097:
1.272     djm      1098:        /*
                   1099:         * The signature algorithm will only differ from the key algorithm
                   1100:         * for RSA keys/certs and when the server advertises support for
                   1101:         * newer (SHA2) algorithms.
                   1102:         */
                   1103:        if (ssh == NULL || ssh->kex->server_sig_algs == NULL ||
1.288     djm      1104:            (key->type != KEY_RSA && key->type != KEY_RSA_CERT) ||
                   1105:            (key->type == KEY_RSA_CERT && (datafellows & SSH_BUG_SIGTYPE))) {
1.272     djm      1106:                /* Filter base key signature alg against our configuration */
1.279     markus   1107:                return match_list(sshkey_ssh_name(key),
1.272     djm      1108:                    options.pubkey_key_types, NULL);
1.231     markus   1109:        }
                   1110:
1.272     djm      1111:        /*
                   1112:         * For RSA keys/certs, since these might have a different sig type:
                   1113:         * find the first entry in PubkeyAcceptedKeyTypes of the right type
                   1114:         * that also appears in the supported signature algorithms list from
                   1115:         * the server.
                   1116:         */
                   1117:        oallowed = allowed = xstrdup(options.pubkey_key_types);
                   1118:        while ((cp = strsep(&allowed, ",")) != NULL) {
                   1119:                if (sshkey_type_from_name(cp) != key->type)
                   1120:                        continue;
1.274     djm      1121:                tmp = match_list(sshkey_sigalg_by_name(cp), ssh->kex->server_sig_algs, NULL);
                   1122:                if (tmp != NULL)
                   1123:                        alg = xstrdup(cp);
                   1124:                free(tmp);
1.272     djm      1125:                if (alg != NULL)
                   1126:                        break;
1.269     djm      1127:        }
1.272     djm      1128:        free(oallowed);
                   1129:        return alg;
1.269     djm      1130: }
                   1131:
1.117     markus   1132: static int
1.214     djm      1133: identity_sign(struct identity *id, u_char **sigp, size_t *lenp,
1.272     djm      1134:     const u_char *data, size_t datalen, u_int compat, const char *alg)
1.117     markus   1135: {
1.259     markus   1136:        struct sshkey *prv;
1.269     djm      1137:        int r;
1.99      markus   1138:
1.272     djm      1139:        /* The agent supports this key. */
1.269     djm      1140:        if (id->key != NULL && id->agent_fd != -1) {
1.272     djm      1141:                return ssh_agent_sign(id->agent_fd, id->key, sigp, lenp,
                   1142:                    data, datalen, alg, compat);
1.269     djm      1143:        }
1.214     djm      1144:
1.117     markus   1145:        /*
1.272     djm      1146:         * We have already loaded the private key or the private key is
                   1147:         * stored in external hardware.
1.117     markus   1148:         */
1.255     djm      1149:        if (id->key != NULL &&
1.272     djm      1150:            (id->isprivate || (id->key->flags & SSHKEY_FLAG_EXT))) {
                   1151:                if ((r = sshkey_sign(id->key, sigp, lenp, data, datalen,
                   1152:                    alg, compat)) != 0)
                   1153:                        return r;
                   1154:                /*
                   1155:                 * PKCS#11 tokens may not support all signature algorithms,
                   1156:                 * so check what we get back.
                   1157:                 */
                   1158:                if ((r = sshkey_check_sigtype(*sigp, *lenp, alg)) != 0)
                   1159:                        return r;
                   1160:                return 0;
                   1161:        }
1.255     djm      1162:
1.272     djm      1163:        /* Load the private key from the file. */
1.229     jcs      1164:        if ((prv = load_identity_file(id)) == NULL)
1.239     djm      1165:                return SSH_ERR_KEY_NOT_FOUND;
1.265     djm      1166:        if (id->key != NULL && !sshkey_equal_public(prv, id->key)) {
                   1167:                error("%s: private key %s contents do not match public",
                   1168:                   __func__, id->filename);
                   1169:                return SSH_ERR_KEY_NOT_FOUND;
                   1170:        }
1.272     djm      1171:        r = sshkey_sign(prv, sigp, lenp, data, datalen, alg, compat);
1.214     djm      1172:        sshkey_free(prv);
1.269     djm      1173:        return r;
1.51      markus   1174: }
                   1175:
1.76      itojun   1176: static int
1.255     djm      1177: id_filename_matches(Identity *id, Identity *private_id)
                   1178: {
                   1179:        const char *suffixes[] = { ".pub", "-cert.pub", NULL };
                   1180:        size_t len = strlen(id->filename), plen = strlen(private_id->filename);
                   1181:        size_t i, slen;
                   1182:
                   1183:        if (strcmp(id->filename, private_id->filename) == 0)
                   1184:                return 1;
                   1185:        for (i = 0; suffixes[i]; i++) {
                   1186:                slen = strlen(suffixes[i]);
                   1187:                if (len > slen && plen == len - slen &&
                   1188:                    strcmp(id->filename + (len - slen), suffixes[i]) == 0 &&
                   1189:                    memcmp(id->filename, private_id->filename, plen) == 0)
                   1190:                        return 1;
                   1191:        }
                   1192:        return 0;
                   1193: }
                   1194:
                   1195: static int
1.295     djm      1196: sign_and_send_pubkey(struct ssh *ssh, Identity *id)
1.1       markus   1197: {
1.295     djm      1198:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.272     djm      1199:        struct sshbuf *b = NULL;
                   1200:        Identity *private_id, *sign_id = NULL;
                   1201:        u_char *signature = NULL;
                   1202:        size_t slen = 0, skip = 0;
                   1203:        int r, fallback_sigtype, sent = 0;
                   1204:        char *alg = NULL, *fp = NULL;
                   1205:        const char *loc = "";
1.1       markus   1206:
1.222     djm      1207:        if ((fp = sshkey_fingerprint(id->key, options.fingerprint_hash,
                   1208:            SSH_FP_DEFAULT)) == NULL)
                   1209:                return 0;
1.51      markus   1210:
1.272     djm      1211:        debug3("%s: %s %s", __func__, sshkey_type(id->key), fp);
1.1       markus   1212:
1.227     djm      1213:        /*
                   1214:         * If the key is an certificate, try to find a matching private key
                   1215:         * and use it to complete the signature.
1.241     djm      1216:         * If no such private key exists, fall back to trying the certificate
                   1217:         * key itself in case it has a private half already loaded.
1.272     djm      1218:         * This will try to set sign_id to the private key that will perform
                   1219:         * the signature.
1.227     djm      1220:         */
1.272     djm      1221:        if (sshkey_is_cert(id->key)) {
1.227     djm      1222:                TAILQ_FOREACH(private_id, &authctxt->keys, next) {
                   1223:                        if (sshkey_equal_public(id->key, private_id->key) &&
                   1224:                            id->key->type != private_id->key->type) {
1.272     djm      1225:                                sign_id = private_id;
1.227     djm      1226:                                break;
                   1227:                        }
                   1228:                }
1.255     djm      1229:                /*
                   1230:                 * Exact key matches are preferred, but also allow
                   1231:                 * filename matches for non-PKCS#11/agent keys that
                   1232:                 * didn't load public keys. This supports the case
                   1233:                 * of keeping just a private key file and public
                   1234:                 * certificate on disk.
                   1235:                 */
1.272     djm      1236:                if (sign_id == NULL &&
                   1237:                    !id->isprivate && id->agent_fd == -1 &&
1.255     djm      1238:                    (id->key->flags & SSHKEY_FLAG_EXT) == 0) {
                   1239:                        TAILQ_FOREACH(private_id, &authctxt->keys, next) {
                   1240:                                if (private_id->key == NULL &&
                   1241:                                    id_filename_matches(id, private_id)) {
1.272     djm      1242:                                        sign_id = private_id;
1.255     djm      1243:                                        break;
                   1244:                                }
                   1245:                        }
                   1246:                }
1.272     djm      1247:                if (sign_id != NULL) {
1.227     djm      1248:                        debug2("%s: using private key \"%s\"%s for "
                   1249:                            "certificate", __func__, id->filename,
                   1250:                            id->agent_fd != -1 ? " from agent" : "");
                   1251:                } else {
1.240     djm      1252:                        debug("%s: no separate private key for certificate "
1.227     djm      1253:                            "\"%s\"", __func__, id->filename);
                   1254:                }
                   1255:        }
                   1256:
1.272     djm      1257:        /*
                   1258:         * If the above didn't select another identity to do the signing
                   1259:         * then default to the one we started with.
                   1260:         */
                   1261:        if (sign_id == NULL)
                   1262:                sign_id = id;
                   1263:
                   1264:        /* assemble and sign data */
                   1265:        for (fallback_sigtype = 0; fallback_sigtype <= 1; fallback_sigtype++) {
                   1266:                free(alg);
                   1267:                slen = 0;
                   1268:                signature = NULL;
                   1269:                if ((alg = key_sig_algorithm(fallback_sigtype ? NULL : ssh,
                   1270:                    id->key)) == NULL) {
                   1271:                        error("%s: no mutual signature supported", __func__);
                   1272:                        goto out;
                   1273:                }
                   1274:                debug3("%s: signing using %s", __func__, alg);
                   1275:
                   1276:                sshbuf_free(b);
                   1277:                if ((b = sshbuf_new()) == NULL)
                   1278:                        fatal("%s: sshbuf_new failed", __func__);
                   1279:                if (datafellows & SSH_OLD_SESSIONID) {
                   1280:                        if ((r = sshbuf_put(b, session_id2,
                   1281:                            session_id2_len)) != 0) {
                   1282:                                fatal("%s: sshbuf_put: %s",
                   1283:                                    __func__, ssh_err(r));
                   1284:                        }
                   1285:                } else {
                   1286:                        if ((r = sshbuf_put_string(b, session_id2,
                   1287:                            session_id2_len)) != 0) {
                   1288:                                fatal("%s: sshbuf_put_string: %s",
                   1289:                                    __func__, ssh_err(r));
                   1290:                        }
                   1291:                }
1.278     markus   1292:                skip = sshbuf_len(b);
1.272     djm      1293:                if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                   1294:                    (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
                   1295:                    (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
                   1296:                    (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
                   1297:                    (r = sshbuf_put_u8(b, 1)) != 0 ||
                   1298:                    (r = sshbuf_put_cstring(b, alg)) != 0 ||
                   1299:                    (r = sshkey_puts(id->key, b)) != 0) {
                   1300:                        fatal("%s: assemble signed data: %s",
                   1301:                            __func__, ssh_err(r));
                   1302:                }
                   1303:
                   1304:                /* generate signature */
                   1305:                r = identity_sign(sign_id, &signature, &slen,
                   1306:                    sshbuf_ptr(b), sshbuf_len(b), datafellows, alg);
                   1307:                if (r == 0)
                   1308:                        break;
                   1309:                else if (r == SSH_ERR_KEY_NOT_FOUND)
                   1310:                        goto out; /* soft failure */
                   1311:                else if (r == SSH_ERR_SIGN_ALG_UNSUPPORTED &&
                   1312:                    !fallback_sigtype) {
                   1313:                        if (sign_id->agent_fd != -1)
                   1314:                                loc = "agent ";
                   1315:                        else if ((sign_id->key->flags & SSHKEY_FLAG_EXT) != 0)
                   1316:                                loc = "token ";
                   1317:                        logit("%skey %s %s returned incorrect signature type",
                   1318:                            loc, sshkey_type(id->key), fp);
                   1319:                        continue;
                   1320:                }
                   1321:                error("%s: signing failed: %s", __func__, ssh_err(r));
                   1322:                goto out;
1.17      markus   1323:        }
1.272     djm      1324:        if (slen == 0 || signature == NULL) /* shouldn't happen */
                   1325:                fatal("%s: no signature", __func__);
1.51      markus   1326:
1.1       markus   1327:        /* append signature */
1.272     djm      1328:        if ((r = sshbuf_put_string(b, signature, slen)) != 0)
                   1329:                fatal("%s: append signature: %s", __func__, ssh_err(r));
1.1       markus   1330:
1.272     djm      1331: #ifdef DEBUG_PK
                   1332:        sshbuf_dump(b, stderr);
                   1333: #endif
1.1       markus   1334:        /* skip session id and packet type */
1.272     djm      1335:        if ((r = sshbuf_consume(b, skip + 1)) != 0)
                   1336:                fatal("%s: consume: %s", __func__, ssh_err(r));
1.1       markus   1337:
                   1338:        /* put remaining data from buffer into packet */
1.272     djm      1339:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                   1340:            (r = sshpkt_putb(ssh, b)) != 0 ||
                   1341:            (r = sshpkt_send(ssh)) != 0)
                   1342:                fatal("%s: enqueue request: %s", __func__, ssh_err(r));
                   1343:
                   1344:        /* success */
                   1345:        sent = 1;
1.17      markus   1346:
1.272     djm      1347:  out:
                   1348:        free(fp);
                   1349:        free(alg);
                   1350:        sshbuf_free(b);
                   1351:        freezero(signature, slen);
                   1352:        return sent;
1.16      markus   1353: }
                   1354:
1.76      itojun   1355: static int
1.295     djm      1356: send_pubkey_test(struct ssh *ssh, Identity *id)
1.20      markus   1357: {
1.295     djm      1358:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.272     djm      1359:        u_char *blob = NULL;
                   1360:        char *alg = NULL;
1.278     markus   1361:        size_t bloblen;
                   1362:        u_int have_sig = 0;
                   1363:        int sent = 0, r;
1.20      markus   1364:
1.272     djm      1365:        if ((alg = key_sig_algorithm(ssh, id->key)) == NULL) {
                   1366:                debug("%s: no mutual signature algorithm", __func__);
                   1367:                goto out;
                   1368:        }
1.16      markus   1369:
1.278     markus   1370:        if ((r = sshkey_to_blob(id->key, &blob, &bloblen)) != 0) {
1.51      markus   1371:                /* we cannot handle this key */
1.272     djm      1372:                debug3("%s: cannot handle key", __func__);
                   1373:                goto out;
1.16      markus   1374:        }
1.51      markus   1375:        /* register callback for USERAUTH_PK_OK message */
1.278     markus   1376:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
1.51      markus   1377:
1.278     markus   1378:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                   1379:            (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
                   1380:            (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
                   1381:            (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
                   1382:            (r = sshpkt_put_u8(ssh, have_sig)) != 0 ||
                   1383:            (r = sshpkt_put_cstring(ssh, alg)) != 0 ||
                   1384:            (r = sshpkt_put_string(ssh, blob, bloblen)) != 0 ||
                   1385:            (r = sshpkt_send(ssh)) != 0)
                   1386:                fatal("%s: %s", __func__, ssh_err(r));
1.272     djm      1387:        sent = 1;
1.278     markus   1388:
                   1389:  out:
1.272     djm      1390:        free(alg);
1.197     djm      1391:        free(blob);
1.272     djm      1392:        return sent;
1.51      markus   1393: }
                   1394:
1.259     markus   1395: static struct sshkey *
1.229     jcs      1396: load_identity_file(Identity *id)
1.51      markus   1397: {
1.259     markus   1398:        struct sshkey *private = NULL;
1.229     jcs      1399:        char prompt[300], *passphrase, *comment;
1.215     djm      1400:        int r, perm_ok = 0, quit = 0, i;
1.52      markus   1401:        struct stat st;
1.16      markus   1402:
1.306   ! deraadt  1403:        if (stat(id->filename, &st) == -1) {
1.229     jcs      1404:                (id->userprovided ? logit : debug3)("no such identity: %s: %s",
                   1405:                    id->filename, strerror(errno));
1.52      markus   1406:                return NULL;
                   1407:        }
1.214     djm      1408:        snprintf(prompt, sizeof prompt,
1.229     jcs      1409:            "Enter passphrase for key '%.100s': ", id->filename);
1.214     djm      1410:        for (i = 0; i <= options.number_of_password_prompts; i++) {
                   1411:                if (i == 0)
                   1412:                        passphrase = "";
                   1413:                else {
1.20      markus   1414:                        passphrase = read_passphrase(prompt, 0);
1.214     djm      1415:                        if (*passphrase == '\0') {
1.20      markus   1416:                                debug2("no passphrase given, try next key");
1.214     djm      1417:                                free(passphrase);
                   1418:                                break;
                   1419:                        }
                   1420:                }
1.229     jcs      1421:                switch ((r = sshkey_load_private_type(KEY_UNSPEC, id->filename,
                   1422:                    passphrase, &private, &comment, &perm_ok))) {
1.214     djm      1423:                case 0:
                   1424:                        break;
                   1425:                case SSH_ERR_KEY_WRONG_PASSPHRASE:
                   1426:                        if (options.batch_mode) {
                   1427:                                quit = 1;
                   1428:                                break;
                   1429:                        }
1.215     djm      1430:                        if (i != 0)
                   1431:                                debug2("bad passphrase given, try again...");
1.214     djm      1432:                        break;
                   1433:                case SSH_ERR_SYSTEM_ERROR:
                   1434:                        if (errno == ENOENT) {
                   1435:                                debug2("Load key \"%s\": %s",
1.229     jcs      1436:                                    id->filename, ssh_err(r));
1.51      markus   1437:                                quit = 1;
1.214     djm      1438:                                break;
1.20      markus   1439:                        }
1.214     djm      1440:                        /* FALLTHROUGH */
                   1441:                default:
1.229     jcs      1442:                        error("Load key \"%s\": %s", id->filename, ssh_err(r));
1.214     djm      1443:                        quit = 1;
                   1444:                        break;
                   1445:                }
1.230     djm      1446:                if (!quit && private != NULL && id->agent_fd == -1 &&
1.229     jcs      1447:                    !(id->key && id->isprivate))
                   1448:                        maybe_add_key_to_agent(id->filename, private, comment,
                   1449:                            passphrase);
1.278     markus   1450:                if (i > 0)
                   1451:                        freezero(passphrase, strlen(passphrase));
1.232     mmcc     1452:                free(comment);
1.214     djm      1453:                if (private != NULL || quit)
                   1454:                        break;
1.16      markus   1455:        }
1.51      markus   1456:        return private;
                   1457: }
                   1458:
1.272     djm      1459: static int
                   1460: key_type_allowed_by_config(struct sshkey *key)
                   1461: {
                   1462:        if (match_pattern_list(sshkey_ssh_name(key),
                   1463:            options.pubkey_key_types, 0) == 1)
                   1464:                return 1;
                   1465:
                   1466:        /* RSA keys/certs might be allowed by alternate signature types */
                   1467:        switch (key->type) {
                   1468:        case KEY_RSA:
                   1469:                if (match_pattern_list("rsa-sha2-512",
                   1470:                    options.pubkey_key_types, 0) == 1)
                   1471:                        return 1;
                   1472:                if (match_pattern_list("rsa-sha2-256",
                   1473:                    options.pubkey_key_types, 0) == 1)
                   1474:                        return 1;
                   1475:                break;
                   1476:        case KEY_RSA_CERT:
                   1477:                if (match_pattern_list("rsa-sha2-512-cert-v01@openssh.com",
                   1478:                    options.pubkey_key_types, 0) == 1)
                   1479:                        return 1;
                   1480:                if (match_pattern_list("rsa-sha2-256-cert-v01@openssh.com",
                   1481:                    options.pubkey_key_types, 0) == 1)
                   1482:                        return 1;
                   1483:                break;
                   1484:        }
                   1485:        return 0;
                   1486: }
                   1487:
                   1488:
1.117     markus   1489: /*
                   1490:  * try keys in the following order:
1.227     djm      1491:  *     1. certificates listed in the config file
                   1492:  *     2. other input certificates
                   1493:  *     3. agent keys that are found in the config file
                   1494:  *     4. other agent keys
                   1495:  *     5. keys that are only listed in the config file
1.117     markus   1496:  */
                   1497: static void
                   1498: pubkey_prepare(Authctxt *authctxt)
1.51      markus   1499: {
1.214     djm      1500:        struct identity *id, *id2, *tmp;
                   1501:        struct idlist agent, files, *preferred;
                   1502:        struct sshkey *key;
1.230     djm      1503:        int agent_fd = -1, i, r, found;
1.214     djm      1504:        size_t j;
                   1505:        struct ssh_identitylist *idlist;
1.287     djm      1506:        char *ident;
1.51      markus   1507:
1.117     markus   1508:        TAILQ_INIT(&agent);     /* keys from the agent */
                   1509:        TAILQ_INIT(&files);     /* keys from the config file */
                   1510:        preferred = &authctxt->keys;
                   1511:        TAILQ_INIT(preferred);  /* preferred order of keys */
                   1512:
1.190     djm      1513:        /* list of keys stored in the filesystem and PKCS#11 */
1.117     markus   1514:        for (i = 0; i < options.num_identity_files; i++) {
                   1515:                key = options.identity_keys[i];
1.180     djm      1516:                if (key && key->cert && key->cert->type != SSH2_CERT_TYPE_USER)
1.117     markus   1517:                        continue;
                   1518:                options.identity_keys[i] = NULL;
1.150     djm      1519:                id = xcalloc(1, sizeof(*id));
1.230     djm      1520:                id->agent_fd = -1;
1.117     markus   1521:                id->key = key;
                   1522:                id->filename = xstrdup(options.identity_files[i]);
1.192     dtucker  1523:                id->userprovided = options.identity_file_userprovided[i];
1.117     markus   1524:                TAILQ_INSERT_TAIL(&files, id, next);
1.190     djm      1525:        }
1.227     djm      1526:        /* list of certificates specified by user */
                   1527:        for (i = 0; i < options.num_certificate_files; i++) {
                   1528:                key = options.certificates[i];
1.278     markus   1529:                if (!sshkey_is_cert(key) || key->cert == NULL ||
1.227     djm      1530:                    key->cert->type != SSH2_CERT_TYPE_USER)
                   1531:                        continue;
                   1532:                id = xcalloc(1, sizeof(*id));
1.230     djm      1533:                id->agent_fd = -1;
1.227     djm      1534:                id->key = key;
                   1535:                id->filename = xstrdup(options.certificate_files[i]);
                   1536:                id->userprovided = options.certificate_file_userprovided[i];
                   1537:                TAILQ_INSERT_TAIL(preferred, id, next);
1.117     markus   1538:        }
                   1539:        /* list of keys supported by the agent */
1.214     djm      1540:        if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
                   1541:                if (r != SSH_ERR_AGENT_NOT_PRESENT)
                   1542:                        debug("%s: ssh_get_authentication_socket: %s",
                   1543:                            __func__, ssh_err(r));
1.258     naddy    1544:        } else if ((r = ssh_fetch_identitylist(agent_fd, &idlist)) != 0) {
1.214     djm      1545:                if (r != SSH_ERR_AGENT_NO_IDENTITIES)
                   1546:                        debug("%s: ssh_fetch_identitylist: %s",
                   1547:                            __func__, ssh_err(r));
1.237     markus   1548:                close(agent_fd);
1.214     djm      1549:        } else {
                   1550:                for (j = 0; j < idlist->nkeys; j++) {
1.117     markus   1551:                        found = 0;
                   1552:                        TAILQ_FOREACH(id, &files, next) {
1.214     djm      1553:                                /*
                   1554:                                 * agent keys from the config file are
                   1555:                                 * preferred
                   1556:                                 */
                   1557:                                if (sshkey_equal(idlist->keys[j], id->key)) {
1.117     markus   1558:                                        TAILQ_REMOVE(&files, id, next);
                   1559:                                        TAILQ_INSERT_TAIL(preferred, id, next);
1.214     djm      1560:                                        id->agent_fd = agent_fd;
1.117     markus   1561:                                        found = 1;
                   1562:                                        break;
                   1563:                                }
                   1564:                        }
1.135     markus   1565:                        if (!found && !options.identities_only) {
1.150     djm      1566:                                id = xcalloc(1, sizeof(*id));
1.214     djm      1567:                                /* XXX "steals" key/comment from idlist */
                   1568:                                id->key = idlist->keys[j];
                   1569:                                id->filename = idlist->comments[j];
                   1570:                                idlist->keys[j] = NULL;
                   1571:                                idlist->comments[j] = NULL;
                   1572:                                id->agent_fd = agent_fd;
1.117     markus   1573:                                TAILQ_INSERT_TAIL(&agent, id, next);
                   1574:                        }
                   1575:                }
1.214     djm      1576:                ssh_free_identitylist(idlist);
1.117     markus   1577:                /* append remaining agent keys */
                   1578:                for (id = TAILQ_FIRST(&agent); id; id = TAILQ_FIRST(&agent)) {
                   1579:                        TAILQ_REMOVE(&agent, id, next);
                   1580:                        TAILQ_INSERT_TAIL(preferred, id, next);
                   1581:                }
1.214     djm      1582:                authctxt->agent_fd = agent_fd;
1.244     djm      1583:        }
                   1584:        /* Prefer PKCS11 keys that are explicitly listed */
                   1585:        TAILQ_FOREACH_SAFE(id, &files, next, tmp) {
                   1586:                if (id->key == NULL || (id->key->flags & SSHKEY_FLAG_EXT) == 0)
                   1587:                        continue;
                   1588:                found = 0;
                   1589:                TAILQ_FOREACH(id2, &files, next) {
                   1590:                        if (id2->key == NULL ||
                   1591:                            (id2->key->flags & SSHKEY_FLAG_EXT) == 0)
                   1592:                                continue;
                   1593:                        if (sshkey_equal(id->key, id2->key)) {
                   1594:                                TAILQ_REMOVE(&files, id, next);
                   1595:                                TAILQ_INSERT_TAIL(preferred, id, next);
                   1596:                                found = 1;
                   1597:                                break;
                   1598:                        }
                   1599:                }
                   1600:                /* If IdentitiesOnly set and key not found then don't use it */
                   1601:                if (!found && options.identities_only) {
                   1602:                        TAILQ_REMOVE(&files, id, next);
1.278     markus   1603:                        freezero(id, sizeof(*id));
1.244     djm      1604:                }
1.117     markus   1605:        }
                   1606:        /* append remaining keys from the config file */
                   1607:        for (id = TAILQ_FIRST(&files); id; id = TAILQ_FIRST(&files)) {
                   1608:                TAILQ_REMOVE(&files, id, next);
                   1609:                TAILQ_INSERT_TAIL(preferred, id, next);
                   1610:        }
1.228     djm      1611:        /* finally, filter by PubkeyAcceptedKeyTypes */
                   1612:        TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
1.273     djm      1613:                if (id->key != NULL && !key_type_allowed_by_config(id->key)) {
1.228     djm      1614:                        debug("Skipping %s key %s - "
                   1615:                            "not in PubkeyAcceptedKeyTypes",
                   1616:                            sshkey_ssh_name(id->key), id->filename);
                   1617:                        TAILQ_REMOVE(preferred, id, next);
                   1618:                        sshkey_free(id->key);
                   1619:                        free(id->filename);
                   1620:                        memset(id, 0, sizeof(*id));
                   1621:                        continue;
                   1622:                }
1.117     markus   1623:        }
1.287     djm      1624:        /* List the keys we plan on using */
                   1625:        TAILQ_FOREACH_SAFE(id, preferred, next, id2) {
                   1626:                ident = format_identity(id);
                   1627:                debug("Will attempt key: %s", ident);
                   1628:                free(ident);
                   1629:        }
                   1630:        debug2("%s: done", __func__);
1.17      markus   1631: }
                   1632:
1.117     markus   1633: static void
1.303     djm      1634: pubkey_cleanup(struct ssh *ssh)
1.51      markus   1635: {
1.302     djm      1636:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.117     markus   1637:        Identity *id;
1.51      markus   1638:
1.289     djm      1639:        if (authctxt->agent_fd != -1) {
1.214     djm      1640:                ssh_close_authentication_socket(authctxt->agent_fd);
1.289     djm      1641:                authctxt->agent_fd = -1;
                   1642:        }
1.117     markus   1643:        for (id = TAILQ_FIRST(&authctxt->keys); id;
                   1644:            id = TAILQ_FIRST(&authctxt->keys)) {
                   1645:                TAILQ_REMOVE(&authctxt->keys, id, next);
1.235     mmcc     1646:                sshkey_free(id->key);
1.197     djm      1647:                free(id->filename);
                   1648:                free(id);
1.117     markus   1649:        }
1.1       markus   1650: }
                   1651:
1.251     djm      1652: static void
                   1653: pubkey_reset(Authctxt *authctxt)
                   1654: {
                   1655:        Identity *id;
                   1656:
                   1657:        TAILQ_FOREACH(id, &authctxt->keys, next)
                   1658:                id->tried = 0;
                   1659: }
                   1660:
1.225     markus   1661: static int
                   1662: try_identity(Identity *id)
                   1663: {
                   1664:        if (!id->key)
                   1665:                return (0);
1.279     markus   1666:        if (sshkey_type_plain(id->key->type) == KEY_RSA &&
1.225     markus   1667:            (datafellows & SSH_BUG_RSASIGMD5) != 0) {
                   1668:                debug("Skipped %s key %s for RSA/MD5 server",
1.279     markus   1669:                    sshkey_type(id->key), id->filename);
1.225     markus   1670:                return (0);
                   1671:        }
1.257     djm      1672:        return 1;
1.225     markus   1673: }
                   1674:
1.302     djm      1675: static int
1.295     djm      1676: userauth_pubkey(struct ssh *ssh)
1.20      markus   1677: {
1.295     djm      1678:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.117     markus   1679:        Identity *id;
1.20      markus   1680:        int sent = 0;
1.287     djm      1681:        char *ident;
1.20      markus   1682:
1.117     markus   1683:        while ((id = TAILQ_FIRST(&authctxt->keys))) {
                   1684:                if (id->tried++)
                   1685:                        return (0);
1.127     markus   1686:                /* move key to the end of the queue */
1.117     markus   1687:                TAILQ_REMOVE(&authctxt->keys, id, next);
                   1688:                TAILQ_INSERT_TAIL(&authctxt->keys, id, next);
                   1689:                /*
                   1690:                 * send a test message if we have the public key. for
                   1691:                 * encrypted keys we cannot do this and have to load the
                   1692:                 * private key instead
                   1693:                 */
1.200     djm      1694:                if (id->key != NULL) {
1.225     markus   1695:                        if (try_identity(id)) {
1.287     djm      1696:                                ident = format_identity(id);
                   1697:                                debug("Offering public key: %s", ident);
                   1698:                                free(ident);
1.295     djm      1699:                                sent = send_pubkey_test(ssh, id);
1.200     djm      1700:                        }
                   1701:                } else {
1.117     markus   1702:                        debug("Trying private key: %s", id->filename);
1.229     jcs      1703:                        id->key = load_identity_file(id);
1.117     markus   1704:                        if (id->key != NULL) {
1.225     markus   1705:                                if (try_identity(id)) {
                   1706:                                        id->isprivate = 1;
1.295     djm      1707:                                        sent = sign_and_send_pubkey(ssh, id);
1.200     djm      1708:                                }
1.278     markus   1709:                                sshkey_free(id->key);
1.117     markus   1710:                                id->key = NULL;
1.251     djm      1711:                                id->isprivate = 0;
1.51      markus   1712:                        }
                   1713:                }
1.117     markus   1714:                if (sent)
                   1715:                        return (sent);
1.28      markus   1716:        }
1.117     markus   1717:        return (0);
1.20      markus   1718: }
                   1719:
1.23      markus   1720: /*
                   1721:  * Send userauth request message specifying keyboard-interactive method.
                   1722:  */
1.302     djm      1723: static int
1.295     djm      1724: userauth_kbdint(struct ssh *ssh)
1.23      markus   1725: {
1.295     djm      1726:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.278     markus   1727:        int r;
1.23      markus   1728:
1.292     djm      1729:        if (authctxt->attempt_kbdint++ >= options.number_of_password_prompts)
1.23      markus   1730:                return 0;
1.82      markus   1731:        /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
1.292     djm      1732:        if (authctxt->attempt_kbdint > 1 && !authctxt->info_req_seen) {
1.82      markus   1733:                debug3("userauth_kbdint: disable: no info_req_seen");
1.278     markus   1734:                ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
1.82      markus   1735:                return 0;
                   1736:        }
1.23      markus   1737:
                   1738:        debug2("userauth_kbdint");
1.278     markus   1739:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                   1740:            (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
                   1741:            (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
                   1742:            (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
                   1743:            (r = sshpkt_put_cstring(ssh, "")) != 0 ||           /* lang */
                   1744:            (r = sshpkt_put_cstring(ssh, options.kbd_interactive_devices ?
                   1745:            options.kbd_interactive_devices : "")) != 0 ||
                   1746:            (r = sshpkt_send(ssh)) != 0)
                   1747:                fatal("%s: %s", __func__, ssh_err(r));
1.23      markus   1748:
1.278     markus   1749:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
1.23      markus   1750:        return 1;
                   1751: }
                   1752:
                   1753: /*
1.46      markus   1754:  * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1.23      markus   1755:  */
1.302     djm      1756: static int
1.261     markus   1757: input_userauth_info_req(int type, u_int32_t seq, struct ssh *ssh)
1.23      markus   1758: {
1.260     markus   1759:        Authctxt *authctxt = ssh->authctxt;
1.278     markus   1760:        char *name = NULL, *inst = NULL, *lang = NULL, *prompt = NULL;
                   1761:        char *response = NULL;
                   1762:        u_char echo = 0;
1.32      markus   1763:        u_int num_prompts, i;
1.278     markus   1764:        int r;
1.23      markus   1765:
                   1766:        debug2("input_userauth_info_req");
                   1767:
                   1768:        if (authctxt == NULL)
                   1769:                fatal("input_userauth_info_req: no authentication context");
1.82      markus   1770:
                   1771:        authctxt->info_req_seen = 1;
1.23      markus   1772:
1.278     markus   1773:        if ((r = sshpkt_get_cstring(ssh, &name, NULL)) != 0 ||
                   1774:            (r = sshpkt_get_cstring(ssh, &inst, NULL)) != 0 ||
                   1775:            (r = sshpkt_get_cstring(ssh, &lang, NULL)) != 0)
                   1776:                goto out;
1.23      markus   1777:        if (strlen(name) > 0)
1.116     itojun   1778:                logit("%s", name);
1.23      markus   1779:        if (strlen(inst) > 0)
1.116     itojun   1780:                logit("%s", inst);
1.23      markus   1781:
1.278     markus   1782:        if ((r = sshpkt_get_u32(ssh, &num_prompts)) != 0)
                   1783:                goto out;
1.23      markus   1784:        /*
                   1785:         * Begin to build info response packet based on prompts requested.
                   1786:         * We commit to providing the correct number of responses, so if
                   1787:         * further on we run into a problem that prevents this, we have to
                   1788:         * be sure and clean this up and send a correct error response.
                   1789:         */
1.278     markus   1790:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_INFO_RESPONSE)) != 0 ||
                   1791:            (r = sshpkt_put_u32(ssh, num_prompts)) != 0)
                   1792:                goto out;
1.23      markus   1793:
1.73      markus   1794:        debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1.23      markus   1795:        for (i = 0; i < num_prompts; i++) {
1.278     markus   1796:                if ((r = sshpkt_get_cstring(ssh, &prompt, NULL)) != 0 ||
                   1797:                    (r = sshpkt_get_u8(ssh, &echo)) != 0)
                   1798:                        goto out;
1.77      markus   1799:                response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1.278     markus   1800:                if ((r = sshpkt_put_cstring(ssh, response)) != 0)
                   1801:                        goto out;
                   1802:                freezero(response, strlen(response));
1.197     djm      1803:                free(prompt);
1.278     markus   1804:                response = prompt = NULL;
1.23      markus   1805:        }
1.278     markus   1806:        /* done with parsing incoming message. */
                   1807:        if ((r = sshpkt_get_end(ssh)) != 0 ||
                   1808:            (r = sshpkt_add_padding(ssh, 64)) != 0)
                   1809:                goto out;
                   1810:        r = sshpkt_send(ssh);
                   1811:  out:
                   1812:        if (response)
                   1813:                freezero(response, strlen(response));
                   1814:        free(prompt);
                   1815:        free(name);
                   1816:        free(inst);
                   1817:        free(lang);
                   1818:        return r;
1.68      markus   1819: }
                   1820:
1.100     markus   1821: static int
1.294     djm      1822: ssh_keysign(struct ssh *ssh, struct sshkey *key, u_char **sigp, size_t *lenp,
1.223     djm      1823:     const u_char *data, size_t datalen)
1.100     markus   1824: {
1.223     djm      1825:        struct sshbuf *b;
1.101     markus   1826:        struct stat st;
1.100     markus   1827:        pid_t pid;
1.304     deraadt  1828:        int r, to[2], from[2], status;
1.295     djm      1829:        int sock = ssh_packet_get_connection_in(ssh);
1.223     djm      1830:        u_char rversion = 0, version = 2;
                   1831:        void (*osigchld)(int);
1.100     markus   1832:
1.223     djm      1833:        *sigp = NULL;
                   1834:        *lenp = 0;
1.100     markus   1835:
1.306   ! deraadt  1836:        if (stat(_PATH_SSH_KEY_SIGN, &st) == -1) {
1.223     djm      1837:                error("%s: not installed: %s", __func__, strerror(errno));
                   1838:                return -1;
                   1839:        }
                   1840:        if (fflush(stdout) != 0) {
                   1841:                error("%s: fflush: %s", __func__, strerror(errno));
1.101     markus   1842:                return -1;
                   1843:        }
1.306   ! deraadt  1844:        if (pipe(to) == -1) {
1.223     djm      1845:                error("%s: pipe: %s", __func__, strerror(errno));
1.100     markus   1846:                return -1;
                   1847:        }
1.306   ! deraadt  1848:        if (pipe(from) == -1) {
1.223     djm      1849:                error("%s: pipe: %s", __func__, strerror(errno));
1.100     markus   1850:                return -1;
                   1851:        }
1.306   ! deraadt  1852:        if ((pid = fork()) == -1) {
1.223     djm      1853:                error("%s: fork: %s", __func__, strerror(errno));
1.100     markus   1854:                return -1;
                   1855:        }
1.223     djm      1856:        osigchld = signal(SIGCHLD, SIG_DFL);
1.100     markus   1857:        if (pid == 0) {
                   1858:                close(from[0]);
1.306   ! deraadt  1859:                if (dup2(from[1], STDOUT_FILENO) == -1)
1.223     djm      1860:                        fatal("%s: dup2: %s", __func__, strerror(errno));
1.100     markus   1861:                close(to[1]);
1.306   ! deraadt  1862:                if (dup2(to[0], STDIN_FILENO) == -1)
1.223     djm      1863:                        fatal("%s: dup2: %s", __func__, strerror(errno));
1.103     markus   1864:                close(from[1]);
                   1865:                close(to[0]);
1.304     deraadt  1866:
1.306   ! deraadt  1867:                if (dup2(sock, STDERR_FILENO + 1) == -1)
1.304     deraadt  1868:                        fatal("%s: dup2: %s", __func__, strerror(errno));
                   1869:                sock = STDERR_FILENO + 1;
                   1870:                fcntl(sock, F_SETFD, 0);        /* keep the socket on exec */
1.223     djm      1871:                closefrom(sock + 1);
1.304     deraadt  1872:
1.223     djm      1873:                debug3("%s: [child] pid=%ld, exec %s",
                   1874:                    __func__, (long)getpid(), _PATH_SSH_KEY_SIGN);
1.233     mmcc     1875:                execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *)NULL);
1.223     djm      1876:                fatal("%s: exec(%s): %s", __func__, _PATH_SSH_KEY_SIGN,
1.100     markus   1877:                    strerror(errno));
                   1878:        }
                   1879:        close(from[1]);
                   1880:        close(to[0]);
1.305     djm      1881:        sock = STDERR_FILENO + 1;
1.100     markus   1882:
1.223     djm      1883:        if ((b = sshbuf_new()) == NULL)
                   1884:                fatal("%s: sshbuf_new failed", __func__);
                   1885:        /* send # of sock, data to be signed */
1.253     djm      1886:        if ((r = sshbuf_put_u32(b, sock)) != 0 ||
1.223     djm      1887:            (r = sshbuf_put_string(b, data, datalen)) != 0)
                   1888:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                   1889:        if (ssh_msg_send(to[1], version, b) == -1)
                   1890:                fatal("%s: couldn't send request", __func__);
                   1891:        sshbuf_reset(b);
                   1892:        r = ssh_msg_recv(from[0], b);
1.101     markus   1893:        close(from[0]);
                   1894:        close(to[1]);
1.223     djm      1895:        if (r < 0) {
                   1896:                error("%s: no reply", __func__);
                   1897:                goto fail;
                   1898:        }
1.101     markus   1899:
1.223     djm      1900:        errno = 0;
1.306   ! deraadt  1901:        while (waitpid(pid, &status, 0) == -1) {
1.223     djm      1902:                if (errno != EINTR) {
                   1903:                        error("%s: waitpid %ld: %s",
                   1904:                            __func__, (long)pid, strerror(errno));
                   1905:                        goto fail;
                   1906:                }
                   1907:        }
                   1908:        if (!WIFEXITED(status)) {
                   1909:                error("%s: exited abnormally", __func__);
                   1910:                goto fail;
                   1911:        }
                   1912:        if (WEXITSTATUS(status) != 0) {
                   1913:                error("%s: exited with status %d",
                   1914:                    __func__, WEXITSTATUS(status));
                   1915:                goto fail;
                   1916:        }
                   1917:        if ((r = sshbuf_get_u8(b, &rversion)) != 0) {
                   1918:                error("%s: buffer error: %s", __func__, ssh_err(r));
                   1919:                goto fail;
                   1920:        }
                   1921:        if (rversion != version) {
                   1922:                error("%s: bad version", __func__);
                   1923:                goto fail;
                   1924:        }
                   1925:        if ((r = sshbuf_get_string(b, sigp, lenp)) != 0) {
                   1926:                error("%s: buffer error: %s", __func__, ssh_err(r));
                   1927:  fail:
                   1928:                signal(SIGCHLD, osigchld);
                   1929:                sshbuf_free(b);
1.100     markus   1930:                return -1;
                   1931:        }
1.223     djm      1932:        signal(SIGCHLD, osigchld);
                   1933:        sshbuf_free(b);
1.100     markus   1934:
                   1935:        return 0;
                   1936: }
                   1937:
1.302     djm      1938: static int
1.295     djm      1939: userauth_hostbased(struct ssh *ssh)
1.68      markus   1940: {
1.295     djm      1941:        Authctxt *authctxt = (Authctxt *)ssh->authctxt;
1.223     djm      1942:        struct sshkey *private = NULL;
                   1943:        struct sshbuf *b = NULL;
                   1944:        u_char *sig = NULL, *keyblob = NULL;
                   1945:        char *fp = NULL, *chost = NULL, *lname = NULL;
                   1946:        size_t siglen = 0, keylen = 0;
                   1947:        int i, r, success = 0;
                   1948:
                   1949:        if (authctxt->ktypes == NULL) {
                   1950:                authctxt->oktypes = xstrdup(options.hostbased_key_types);
                   1951:                authctxt->ktypes = authctxt->oktypes;
                   1952:        }
1.213     djm      1953:
1.223     djm      1954:        /*
                   1955:         * Work through each listed type pattern in HostbasedKeyTypes,
                   1956:         * trying each hostkey that matches the type in turn.
                   1957:         */
                   1958:        for (;;) {
                   1959:                if (authctxt->active_ktype == NULL)
                   1960:                        authctxt->active_ktype = strsep(&authctxt->ktypes, ",");
                   1961:                if (authctxt->active_ktype == NULL ||
                   1962:                    *authctxt->active_ktype == '\0')
                   1963:                        break;
                   1964:                debug3("%s: trying key type %s", __func__,
                   1965:                    authctxt->active_ktype);
1.68      markus   1966:
1.223     djm      1967:                /* check for a useful key */
                   1968:                private = NULL;
                   1969:                for (i = 0; i < authctxt->sensitive->nkeys; i++) {
                   1970:                        if (authctxt->sensitive->keys[i] == NULL ||
                   1971:                            authctxt->sensitive->keys[i]->type == KEY_UNSPEC)
                   1972:                                continue;
                   1973:                        if (match_pattern_list(
                   1974:                            sshkey_ssh_name(authctxt->sensitive->keys[i]),
1.224     djm      1975:                            authctxt->active_ktype, 0) != 1)
1.223     djm      1976:                                continue;
1.68      markus   1977:                        /* we take and free the key */
1.223     djm      1978:                        private = authctxt->sensitive->keys[i];
                   1979:                        authctxt->sensitive->keys[i] = NULL;
1.68      markus   1980:                        break;
                   1981:                }
1.223     djm      1982:                /* Found one */
                   1983:                if (private != NULL)
                   1984:                        break;
                   1985:                /* No more keys of this type; advance */
                   1986:                authctxt->active_ktype = NULL;
1.68      markus   1987:        }
1.223     djm      1988:        if (private == NULL) {
                   1989:                free(authctxt->oktypes);
                   1990:                authctxt->oktypes = authctxt->ktypes = NULL;
                   1991:                authctxt->active_ktype = NULL;
1.109     markus   1992:                debug("No more client hostkeys for hostbased authentication.");
1.223     djm      1993:                goto out;
1.68      markus   1994:        }
1.211     djm      1995:
1.223     djm      1996:        if ((fp = sshkey_fingerprint(private, options.fingerprint_hash,
                   1997:            SSH_FP_DEFAULT)) == NULL) {
                   1998:                error("%s: sshkey_fingerprint failed", __func__);
                   1999:                goto out;
1.68      markus   2000:        }
1.223     djm      2001:        debug("%s: trying hostkey %s %s",
                   2002:            __func__, sshkey_ssh_name(private), fp);
1.211     djm      2003:
1.84      markus   2004:        /* figure out a name for the client host */
1.295     djm      2005:        lname = get_local_name(ssh_packet_get_connection_in(ssh));
                   2006:        if (lname == NULL) {
1.223     djm      2007:                error("%s: cannot get local ipaddr/name", __func__);
                   2008:                goto out;
1.84      markus   2009:        }
1.223     djm      2010:
                   2011:        /* XXX sshbuf_put_stringf? */
                   2012:        xasprintf(&chost, "%s.", lname);
                   2013:        debug2("%s: chost %s", __func__, chost);
1.84      markus   2014:
1.68      markus   2015:        /* construct data */
1.223     djm      2016:        if ((b = sshbuf_new()) == NULL) {
                   2017:                error("%s: sshbuf_new failed", __func__);
                   2018:                goto out;
                   2019:        }
                   2020:        if ((r = sshkey_to_blob(private, &keyblob, &keylen)) != 0) {
                   2021:                error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
                   2022:                goto out;
                   2023:        }
                   2024:        if ((r = sshbuf_put_string(b, session_id2, session_id2_len)) != 0 ||
                   2025:            (r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                   2026:            (r = sshbuf_put_cstring(b, authctxt->server_user)) != 0 ||
1.267     djm      2027:            (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
1.223     djm      2028:            (r = sshbuf_put_cstring(b, authctxt->method->name)) != 0 ||
1.279     markus   2029:            (r = sshbuf_put_cstring(b, sshkey_ssh_name(private))) != 0 ||
1.223     djm      2030:            (r = sshbuf_put_string(b, keyblob, keylen)) != 0 ||
                   2031:            (r = sshbuf_put_cstring(b, chost)) != 0 ||
                   2032:            (r = sshbuf_put_cstring(b, authctxt->local_user)) != 0) {
                   2033:                error("%s: buffer error: %s", __func__, ssh_err(r));
                   2034:                goto out;
                   2035:        }
                   2036:
1.68      markus   2037: #ifdef DEBUG_PK
1.223     djm      2038:        sshbuf_dump(b, stderr);
1.68      markus   2039: #endif
1.294     djm      2040:        if ((r = ssh_keysign(ssh, private, &sig, &siglen,
                   2041:            sshbuf_ptr(b), sshbuf_len(b))) != 0) {
1.223     djm      2042:                error("sign using hostkey %s %s failed",
                   2043:                    sshkey_ssh_name(private), fp);
                   2044:                goto out;
                   2045:        }
                   2046:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
                   2047:            (r = sshpkt_put_cstring(ssh, authctxt->server_user)) != 0 ||
                   2048:            (r = sshpkt_put_cstring(ssh, authctxt->service)) != 0 ||
                   2049:            (r = sshpkt_put_cstring(ssh, authctxt->method->name)) != 0 ||
1.279     markus   2050:            (r = sshpkt_put_cstring(ssh, sshkey_ssh_name(private))) != 0 ||
1.223     djm      2051:            (r = sshpkt_put_string(ssh, keyblob, keylen)) != 0 ||
                   2052:            (r = sshpkt_put_cstring(ssh, chost)) != 0 ||
                   2053:            (r = sshpkt_put_cstring(ssh, authctxt->local_user)) != 0 ||
                   2054:            (r = sshpkt_put_string(ssh, sig, siglen)) != 0 ||
                   2055:            (r = sshpkt_send(ssh)) != 0) {
                   2056:                error("%s: packet error: %s", __func__, ssh_err(r));
                   2057:                goto out;
                   2058:        }
                   2059:        success = 1;
                   2060:
                   2061:  out:
1.278     markus   2062:        if (sig != NULL)
                   2063:                freezero(sig, siglen);
1.223     djm      2064:        free(keyblob);
                   2065:        free(lname);
                   2066:        free(fp);
1.197     djm      2067:        free(chost);
1.223     djm      2068:        sshkey_free(private);
                   2069:        sshbuf_free(b);
1.68      markus   2070:
1.223     djm      2071:        return success;
1.23      markus   2072: }
1.170     djm      2073:
1.20      markus   2074: /* find auth method */
                   2075:
                   2076: /*
                   2077:  * given auth method name, if configurable options permit this method fill
                   2078:  * in auth_ident field and return true, otherwise return false.
                   2079:  */
1.76      itojun   2080: static int
1.20      markus   2081: authmethod_is_enabled(Authmethod *method)
                   2082: {
                   2083:        if (method == NULL)
                   2084:                return 0;
                   2085:        /* return false if options indicate this method is disabled */
                   2086:        if  (method->enabled == NULL || *method->enabled == 0)
                   2087:                return 0;
                   2088:        /* return false if batch mode is enabled but method needs interactive mode */
                   2089:        if  (method->batch_flag != NULL && *method->batch_flag != 0)
                   2090:                return 0;
                   2091:        return 1;
                   2092: }
                   2093:
1.76      itojun   2094: static Authmethod *
1.20      markus   2095: authmethod_lookup(const char *name)
1.1       markus   2096: {
1.20      markus   2097:        Authmethod *method = NULL;
                   2098:        if (name != NULL)
                   2099:                for (method = authmethods; method->name != NULL; method++)
                   2100:                        if (strcmp(name, method->name) == 0)
                   2101:                                return method;
                   2102:        debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
                   2103:        return NULL;
                   2104: }
1.1       markus   2105:
1.53      markus   2106: /* XXX internal state */
                   2107: static Authmethod *current = NULL;
                   2108: static char *supported = NULL;
                   2109: static char *preferred = NULL;
1.105     deraadt  2110:
1.20      markus   2111: /*
                   2112:  * Given the authentication method list sent by the server, return the
                   2113:  * next method we should try.  If the server initially sends a nil list,
1.67      markus   2114:  * use a built-in default list.
1.41      stevesk  2115:  */
1.76      itojun   2116: static Authmethod *
1.20      markus   2117: authmethod_get(char *authlist)
                   2118: {
1.53      markus   2119:        char *name = NULL;
1.97      markus   2120:        u_int next;
1.41      stevesk  2121:
1.20      markus   2122:        /* Use a suitable default if we're passed a nil list.  */
                   2123:        if (authlist == NULL || strlen(authlist) == 0)
1.53      markus   2124:                authlist = options.preferred_authentications;
1.20      markus   2125:
1.53      markus   2126:        if (supported == NULL || strcmp(authlist, supported) != 0) {
                   2127:                debug3("start over, passed a different list %s", authlist);
1.197     djm      2128:                free(supported);
1.53      markus   2129:                supported = xstrdup(authlist);
                   2130:                preferred = options.preferred_authentications;
                   2131:                debug3("preferred %s", preferred);
                   2132:                current = NULL;
                   2133:        } else if (current != NULL && authmethod_is_enabled(current))
                   2134:                return current;
1.20      markus   2135:
1.53      markus   2136:        for (;;) {
                   2137:                if ((name = match_list(preferred, supported, &next)) == NULL) {
1.109     markus   2138:                        debug("No more authentication methods to try.");
1.53      markus   2139:                        current = NULL;
                   2140:                        return NULL;
                   2141:                }
                   2142:                preferred += next;
1.23      markus   2143:                debug3("authmethod_lookup %s", name);
1.53      markus   2144:                debug3("remaining preferred: %s", preferred);
                   2145:                if ((current = authmethod_lookup(name)) != NULL &&
                   2146:                    authmethod_is_enabled(current)) {
1.23      markus   2147:                        debug3("authmethod_is_enabled %s", name);
1.109     markus   2148:                        debug("Next authentication method: %s", name);
1.197     djm      2149:                        free(name);
1.53      markus   2150:                        return current;
1.23      markus   2151:                }
1.198     dtucker  2152:                free(name);
1.1       markus   2153:        }
1.53      markus   2154: }
1.1       markus   2155:
1.79      stevesk  2156: static char *
1.53      markus   2157: authmethods_get(void)
                   2158: {
                   2159:        Authmethod *method = NULL;
1.278     markus   2160:        struct sshbuf *b;
1.93      markus   2161:        char *list;
1.278     markus   2162:        int r;
1.27      provos   2163:
1.278     markus   2164:        if ((b = sshbuf_new()) == NULL)
                   2165:                fatal("%s: sshbuf_new failed", __func__);
1.53      markus   2166:        for (method = authmethods; method->name != NULL; method++) {
                   2167:                if (authmethod_is_enabled(method)) {
1.278     markus   2168:                        if ((r = sshbuf_putf(b, "%s%s",
                   2169:                            sshbuf_len(b) ? "," : "", method->name)) != 0)
                   2170:                                fatal("%s: buffer error: %s",
                   2171:                                    __func__, ssh_err(r));
1.53      markus   2172:                }
                   2173:        }
1.278     markus   2174:        if ((list = sshbuf_dup_string(b)) == NULL)
1.242     djm      2175:                fatal("%s: sshbuf_dup_string failed", __func__);
1.278     markus   2176:        sshbuf_free(b);
1.93      markus   2177:        return list;
1.1       markus   2178: }