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

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