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

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