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

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