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

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