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

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