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

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