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

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