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

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