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

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