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

1.1       markus      1: /*
                      2:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
                      3:  *
                      4:  * Redistribution and use in source and binary forms, with or without
                      5:  * modification, are permitted provided that the following conditions
                      6:  * are met:
                      7:  * 1. Redistributions of source code must retain the above copyright
                      8:  *    notice, this list of conditions and the following disclaimer.
                      9:  * 2. Redistributions in binary form must reproduce the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer in the
                     11:  *    documentation and/or other materials provided with the distribution.
                     12:  *
                     13:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     14:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     15:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     16:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     17:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     18:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     19:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     20:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     21:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     22:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     23:  */
                     24:
                     25: #include "includes.h"
1.115   ! markus     26: RCSID("$OpenBSD: sshconnect2.c,v 1.114 2003/04/01 10:22:21 markus Exp $");
1.1       markus     27:
                     28: #include "ssh.h"
1.37      markus     29: #include "ssh2.h"
1.1       markus     30: #include "xmalloc.h"
                     31: #include "buffer.h"
                     32: #include "packet.h"
                     33: #include "compat.h"
                     34: #include "bufaux.h"
1.37      markus     35: #include "cipher.h"
1.1       markus     36: #include "kex.h"
                     37: #include "myproposal.h"
                     38: #include "sshconnect.h"
                     39: #include "authfile.h"
1.57      provos     40: #include "dh.h"
1.17      markus     41: #include "authfd.h"
1.37      markus     42: #include "log.h"
                     43: #include "readconf.h"
                     44: #include "readpass.h"
1.53      markus     45: #include "match.h"
1.61      markus     46: #include "dispatch.h"
1.68      markus     47: #include "canohost.h"
1.100     markus     48: #include "msg.h"
                     49: #include "pathnames.h"
1.22      provos     50:
1.1       markus     51: /* import */
                     52: extern char *client_version_string;
                     53: extern char *server_version_string;
                     54: extern Options options;
                     55:
                     56: /*
                     57:  * SSH2 key exchange
                     58:  */
                     59:
1.32      markus     60: u_char *session_id2 = NULL;
1.1       markus     61: int session_id2_len = 0;
                     62:
1.61      markus     63: char *xxx_host;
                     64: struct sockaddr *xxx_hostaddr;
                     65:
1.63      markus     66: Kex *xxx_kex = NULL;
                     67:
1.76      itojun     68: static int
1.75      markus     69: verify_host_key_callback(Key *hostkey)
1.61      markus     70: {
1.75      markus     71:        if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
1.83      markus     72:                fatal("Host key verification failed.");
1.61      markus     73:        return 0;
                     74: }
                     75:
1.1       markus     76: void
1.22      provos     77: ssh_kex2(char *host, struct sockaddr *hostaddr)
                     78: {
                     79:        Kex *kex;
1.61      markus     80:
                     81:        xxx_host = host;
                     82:        xxx_hostaddr = hostaddr;
1.22      provos     83:
1.29      markus     84:        if (options.ciphers == (char *)-1) {
                     85:                log("No valid ciphers for protocol version 2 given, using defaults.");
                     86:                options.ciphers = NULL;
1.24      markus     87:        }
1.22      provos     88:        if (options.ciphers != NULL) {
                     89:                myproposal[PROPOSAL_ENC_ALGS_CTOS] =
                     90:                myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
                     91:        }
1.60      stevesk    92:        myproposal[PROPOSAL_ENC_ALGS_CTOS] =
                     93:            compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
                     94:        myproposal[PROPOSAL_ENC_ALGS_STOC] =
                     95:            compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
1.22      provos     96:        if (options.compression) {
1.47      markus     97:                myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1.107     markus     98:                myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
1.22      provos     99:        } else {
1.47      markus    100:                myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1.107     markus    101:                myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
1.47      markus    102:        }
                    103:        if (options.macs != NULL) {
                    104:                myproposal[PROPOSAL_MAC_ALGS_CTOS] =
                    105:                myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1.22      provos    106:        }
1.70      markus    107:        if (options.hostkeyalgorithms != NULL)
1.88      deraadt   108:                myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
1.70      markus    109:                    options.hostkeyalgorithms;
1.115   ! markus    110:
        !           111:        if (options.rekey_limit)
        !           112:                packet_set_rekey_limit(options.rekey_limit);
1.22      provos    113:
1.65      markus    114:        /* start key exchange */
1.64      markus    115:        kex = kex_setup(myproposal);
1.111     markus    116:        kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
                    117:        kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
1.61      markus    118:        kex->client_version_string=client_version_string;
                    119:        kex->server_version_string=server_version_string;
1.75      markus    120:        kex->verify_host_key=&verify_host_key_callback;
1.63      markus    121:
                    122:        xxx_kex = kex;
1.22      provos    123:
1.66      markus    124:        dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
1.62      markus    125:
                    126:        session_id2 = kex->session_id;
                    127:        session_id2_len = kex->session_id_len;
1.22      provos    128:
                    129: #ifdef DEBUG_KEXDH
                    130:        /* send 1st encrypted/maced/compressed message */
                    131:        packet_start(SSH2_MSG_IGNORE);
                    132:        packet_put_cstring("markus");
                    133:        packet_send();
                    134:        packet_write_wait();
                    135: #endif
1.1       markus    136: }
1.11      markus    137:
1.1       markus    138: /*
                    139:  * Authenticate user
                    140:  */
1.20      markus    141:
                    142: typedef struct Authctxt Authctxt;
                    143: typedef struct Authmethod Authmethod;
                    144:
                    145: typedef int sign_cb_fn(
                    146:     Authctxt *authctxt, Key *key,
1.96      markus    147:     u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
1.20      markus    148:
                    149: struct Authctxt {
                    150:        const char *server_user;
1.68      markus    151:        const char *local_user;
1.20      markus    152:        const char *host;
                    153:        const char *service;
1.23      markus    154:        Authmethod *method;
1.20      markus    155:        int success;
1.51      markus    156:        char *authlist;
1.68      markus    157:        /* pubkey */
1.51      markus    158:        Key *last_key;
                    159:        sign_cb_fn *last_key_sign;
                    160:        int last_key_hint;
1.68      markus    161:        AuthenticationConnection *agent;
                    162:        /* hostbased */
1.100     markus    163:        Sensitive *sensitive;
1.82      markus    164:        /* kbd-interactive */
                    165:        int info_req_seen;
1.20      markus    166: };
                    167: struct Authmethod {
                    168:        char    *name;          /* string to compare against server's list */
                    169:        int     (*userauth)(Authctxt *authctxt);
                    170:        int     *enabled;       /* flag in option struct that enables method */
                    171:        int     *batch_flag;    /* flag in option struct that disables method */
                    172: };
                    173:
1.92      markus    174: void   input_userauth_success(int, u_int32_t, void *);
                    175: void   input_userauth_failure(int, u_int32_t, void *);
                    176: void   input_userauth_banner(int, u_int32_t, void *);
                    177: void   input_userauth_error(int, u_int32_t, void *);
                    178: void   input_userauth_info_req(int, u_int32_t, void *);
                    179: void   input_userauth_pk_ok(int, u_int32_t, void *);
1.99      markus    180: void   input_userauth_passwd_changereq(int, u_int32_t, void *);
1.86      itojun    181:
                    182: int    userauth_none(Authctxt *);
                    183: int    userauth_pubkey(Authctxt *);
                    184: int    userauth_passwd(Authctxt *);
                    185: int    userauth_kbdint(Authctxt *);
                    186: int    userauth_hostbased(Authctxt *);
1.20      markus    187:
1.86      itojun    188: void   userauth(Authctxt *, char *);
1.51      markus    189:
1.76      itojun    190: static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
                    191: static void clear_auth_state(Authctxt *);
                    192:
                    193: static Authmethod *authmethod_get(char *authlist);
                    194: static Authmethod *authmethod_lookup(const char *name);
                    195: static char *authmethods_get(void);
1.20      markus    196:
                    197: Authmethod authmethods[] = {
1.81      markus    198:        {"hostbased",
                    199:                userauth_hostbased,
                    200:                &options.hostbased_authentication,
                    201:                NULL},
1.20      markus    202:        {"publickey",
                    203:                userauth_pubkey,
1.28      markus    204:                &options.pubkey_authentication,
1.20      markus    205:                NULL},
1.81      markus    206:        {"keyboard-interactive",
                    207:                userauth_kbdint,
                    208:                &options.kbd_interactive_authentication,
                    209:                &options.batch_mode},
1.20      markus    210:        {"password",
                    211:                userauth_passwd,
                    212:                &options.password_authentication,
1.23      markus    213:                &options.batch_mode},
                    214:        {"none",
                    215:                userauth_none,
                    216:                NULL,
                    217:                NULL},
1.20      markus    218:        {NULL, NULL, NULL, NULL}
                    219: };
                    220:
                    221: void
1.68      markus    222: ssh_userauth2(const char *local_user, const char *server_user, char *host,
1.100     markus    223:     Sensitive *sensitive)
1.20      markus    224: {
                    225:        Authctxt authctxt;
                    226:        int type;
1.39      markus    227:
1.73      markus    228:        if (options.challenge_response_authentication)
1.39      markus    229:                options.kbd_interactive_authentication = 1;
1.20      markus    230:
                    231:        packet_start(SSH2_MSG_SERVICE_REQUEST);
                    232:        packet_put_cstring("ssh-userauth");
                    233:        packet_send();
1.108     markus    234:        debug("SSH2_MSG_SERVICE_REQUEST sent");
1.20      markus    235:        packet_write_wait();
1.91      markus    236:        type = packet_read();
1.108     markus    237:        if (type != SSH2_MSG_SERVICE_ACCEPT)
                    238:                fatal("Server denied authentication request: %d", type);
1.20      markus    239:        if (packet_remaining() > 0) {
1.91      markus    240:                char *reply = packet_get_string(NULL);
1.108     markus    241:                debug2("service_accept: %s", reply);
1.20      markus    242:                xfree(reply);
                    243:        } else {
1.109     markus    244:                debug2("buggy server: service_accept w/o service");
1.20      markus    245:        }
1.90      markus    246:        packet_check_eom();
1.108     markus    247:        debug("SSH2_MSG_SERVICE_ACCEPT received");
1.20      markus    248:
1.53      markus    249:        if (options.preferred_authentications == NULL)
                    250:                options.preferred_authentications = authmethods_get();
                    251:
1.20      markus    252:        /* setup authentication context */
1.82      markus    253:        memset(&authctxt, 0, sizeof(authctxt));
1.20      markus    254:        authctxt.agent = ssh_get_authentication_connection();
                    255:        authctxt.server_user = server_user;
1.68      markus    256:        authctxt.local_user = local_user;
1.20      markus    257:        authctxt.host = host;
                    258:        authctxt.service = "ssh-connection";            /* service name */
                    259:        authctxt.success = 0;
1.23      markus    260:        authctxt.method = authmethod_lookup("none");
1.51      markus    261:        authctxt.authlist = NULL;
1.100     markus    262:        authctxt.sensitive = sensitive;
1.82      markus    263:        authctxt.info_req_seen = 0;
1.23      markus    264:        if (authctxt.method == NULL)
                    265:                fatal("ssh_userauth2: internal error: cannot send userauth none request");
1.20      markus    266:
                    267:        /* initial userauth request */
1.23      markus    268:        userauth_none(&authctxt);
1.20      markus    269:
1.65      markus    270:        dispatch_init(&input_userauth_error);
1.20      markus    271:        dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
                    272:        dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
1.35      markus    273:        dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
1.20      markus    274:        dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt);     /* loop until success */
                    275:
                    276:        if (authctxt.agent != NULL)
                    277:                ssh_close_authentication_connection(authctxt.agent);
                    278:
1.109     markus    279:        debug("Authentication succeeded (%s).", authctxt.method->name);
1.20      markus    280: }
                    281: void
1.51      markus    282: userauth(Authctxt *authctxt, char *authlist)
                    283: {
                    284:        if (authlist == NULL) {
                    285:                authlist = authctxt->authlist;
                    286:        } else {
                    287:                if (authctxt->authlist)
                    288:                        xfree(authctxt->authlist);
                    289:                authctxt->authlist = authlist;
                    290:        }
                    291:        for (;;) {
                    292:                Authmethod *method = authmethod_get(authlist);
                    293:                if (method == NULL)
                    294:                        fatal("Permission denied (%s).", authlist);
                    295:                authctxt->method = method;
                    296:                if (method->userauth(authctxt) != 0) {
                    297:                        debug2("we sent a %s packet, wait for reply", method->name);
                    298:                        break;
                    299:                } else {
                    300:                        debug2("we did not send a packet, disable method");
                    301:                        method->enabled = NULL;
                    302:                }
                    303:        }
                    304: }
1.105     deraadt   305:
1.51      markus    306: void
1.92      markus    307: input_userauth_error(int type, u_int32_t seq, void *ctxt)
1.20      markus    308: {
1.35      markus    309:        fatal("input_userauth_error: bad message during authentication: "
                    310:           "type %d", type);
                    311: }
1.105     deraadt   312:
1.35      markus    313: void
1.92      markus    314: input_userauth_banner(int type, u_int32_t seq, void *ctxt)
1.35      markus    315: {
                    316:        char *msg, *lang;
                    317:        debug3("input_userauth_banner");
                    318:        msg = packet_get_string(NULL);
                    319:        lang = packet_get_string(NULL);
                    320:        fprintf(stderr, "%s", msg);
                    321:        xfree(msg);
                    322:        xfree(lang);
1.20      markus    323: }
1.105     deraadt   324:
1.20      markus    325: void
1.92      markus    326: input_userauth_success(int type, u_int32_t seq, void *ctxt)
1.20      markus    327: {
                    328:        Authctxt *authctxt = ctxt;
                    329:        if (authctxt == NULL)
                    330:                fatal("input_userauth_success: no authentication context");
1.51      markus    331:        if (authctxt->authlist)
                    332:                xfree(authctxt->authlist);
                    333:        clear_auth_state(authctxt);
1.20      markus    334:        authctxt->success = 1;                  /* break out */
                    335: }
1.105     deraadt   336:
1.20      markus    337: void
1.92      markus    338: input_userauth_failure(int type, u_int32_t seq, void *ctxt)
1.20      markus    339: {
                    340:        Authctxt *authctxt = ctxt;
                    341:        char *authlist = NULL;
                    342:        int partial;
                    343:
                    344:        if (authctxt == NULL)
                    345:                fatal("input_userauth_failure: no authentication context");
                    346:
1.23      markus    347:        authlist = packet_get_string(NULL);
1.20      markus    348:        partial = packet_get_char();
1.90      markus    349:        packet_check_eom();
1.20      markus    350:
                    351:        if (partial != 0)
1.45      markus    352:                log("Authenticated with partial success.");
1.109     markus    353:        debug("Authentications that can continue: %s", authlist);
1.20      markus    354:
1.51      markus    355:        clear_auth_state(authctxt);
                    356:        userauth(authctxt, authlist);
                    357: }
                    358: void
1.92      markus    359: input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
1.51      markus    360: {
                    361:        Authctxt *authctxt = ctxt;
                    362:        Key *key = NULL;
                    363:        Buffer b;
1.96      markus    364:        int pktype, sent = 0;
                    365:        u_int alen, blen;
                    366:        char *pkalg, *fp;
                    367:        u_char *pkblob;
1.51      markus    368:
                    369:        if (authctxt == NULL)
                    370:                fatal("input_userauth_pk_ok: no authentication context");
                    371:        if (datafellows & SSH_BUG_PKOK) {
                    372:                /* this is similar to SSH_BUG_PKAUTH */
                    373:                debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
                    374:                pkblob = packet_get_string(&blen);
                    375:                buffer_init(&b);
                    376:                buffer_append(&b, pkblob, blen);
                    377:                pkalg = buffer_get_string(&b, &alen);
                    378:                buffer_free(&b);
                    379:        } else {
                    380:                pkalg = packet_get_string(&alen);
                    381:                pkblob = packet_get_string(&blen);
                    382:        }
1.90      markus    383:        packet_check_eom();
1.51      markus    384:
1.109     markus    385:        debug("Server accepts key: pkalg %s blen %u lastkey %p hint %d",
1.51      markus    386:            pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
                    387:
                    388:        do {
                    389:                if (authctxt->last_key == NULL ||
                    390:                    authctxt->last_key_sign == NULL) {
                    391:                        debug("no last key or no sign cb");
                    392:                        break;
                    393:                }
1.95      markus    394:                if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
1.51      markus    395:                        debug("unknown pkalg %s", pkalg);
                    396:                        break;
                    397:                }
                    398:                if ((key = key_from_blob(pkblob, blen)) == NULL) {
                    399:                        debug("no key from blob. pkalg %s", pkalg);
1.95      markus    400:                        break;
                    401:                }
1.98      markus    402:                if (key->type != pktype) {
1.95      markus    403:                        error("input_userauth_pk_ok: type mismatch "
                    404:                            "for decoded key (received %d, expected %d)",
1.104     deraadt   405:                            key->type, pktype);
1.51      markus    406:                        break;
                    407:                }
1.54      markus    408:                fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
                    409:                debug2("input_userauth_pk_ok: fp %s", fp);
                    410:                xfree(fp);
1.51      markus    411:                if (!key_equal(key, authctxt->last_key)) {
                    412:                        debug("key != last_key");
1.20      markus    413:                        break;
                    414:                }
1.51      markus    415:                sent = sign_and_send_pubkey(authctxt, key,
                    416:                   authctxt->last_key_sign);
1.87      deraadt   417:        } while (0);
1.51      markus    418:
                    419:        if (key != NULL)
                    420:                key_free(key);
                    421:        xfree(pkalg);
                    422:        xfree(pkblob);
                    423:
                    424:        /* unregister */
                    425:        clear_auth_state(authctxt);
                    426:        dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
                    427:
1.106     deraadt   428:        /* try another method if we did not send a packet */
1.51      markus    429:        if (sent == 0)
                    430:                userauth(authctxt, NULL);
                    431:
1.20      markus    432: }
                    433:
1.1       markus    434: int
1.23      markus    435: userauth_none(Authctxt *authctxt)
                    436: {
                    437:        /* initial userauth request */
                    438:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
                    439:        packet_put_cstring(authctxt->server_user);
                    440:        packet_put_cstring(authctxt->service);
                    441:        packet_put_cstring(authctxt->method->name);
                    442:        packet_send();
                    443:        return 1;
                    444: }
                    445:
                    446: int
1.20      markus    447: userauth_passwd(Authctxt *authctxt)
1.1       markus    448: {
1.6       markus    449:        static int attempt = 0;
1.99      markus    450:        char prompt[150];
1.1       markus    451:        char *password;
1.6       markus    452:
1.13      todd      453:        if (attempt++ >= options.number_of_password_prompts)
1.6       markus    454:                return 0;
1.13      todd      455:
1.87      deraadt   456:        if (attempt != 1)
1.13      todd      457:                error("Permission denied, please try again.");
1.1       markus    458:
1.43      itojun    459:        snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ",
1.20      markus    460:            authctxt->server_user, authctxt->host);
1.1       markus    461:        password = read_passphrase(prompt, 0);
                    462:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
1.20      markus    463:        packet_put_cstring(authctxt->server_user);
                    464:        packet_put_cstring(authctxt->service);
1.23      markus    465:        packet_put_cstring(authctxt->method->name);
1.1       markus    466:        packet_put_char(0);
1.49      markus    467:        packet_put_cstring(password);
1.1       markus    468:        memset(password, 0, strlen(password));
                    469:        xfree(password);
1.85      markus    470:        packet_add_padding(64);
1.1       markus    471:        packet_send();
1.99      markus    472:
1.104     deraadt   473:        dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99      markus    474:            &input_userauth_passwd_changereq);
                    475:
1.1       markus    476:        return 1;
                    477: }
1.99      markus    478: /*
                    479:  * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
                    480:  */
                    481: void
                    482: input_userauth_passwd_changereq(int type, uint32_t seqnr, void *ctxt)
                    483: {
                    484:        Authctxt *authctxt = ctxt;
                    485:        char *info, *lang, *password = NULL, *retype = NULL;
                    486:        char prompt[150];
                    487:
                    488:        debug2("input_userauth_passwd_changereq");
                    489:
                    490:        if (authctxt == NULL)
                    491:                fatal("input_userauth_passwd_changereq: "
                    492:                    "no authentication context");
                    493:
                    494:        info = packet_get_string(NULL);
                    495:        lang = packet_get_string(NULL);
                    496:        if (strlen(info) > 0)
                    497:                log("%s", info);
                    498:        xfree(info);
                    499:        xfree(lang);
                    500:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
                    501:        packet_put_cstring(authctxt->server_user);
                    502:        packet_put_cstring(authctxt->service);
                    503:        packet_put_cstring(authctxt->method->name);
                    504:        packet_put_char(1);                     /* additional info */
1.104     deraadt   505:        snprintf(prompt, sizeof(prompt),
1.99      markus    506:            "Enter %.30s@%.128s's old password: ",
                    507:            authctxt->server_user, authctxt->host);
                    508:        password = read_passphrase(prompt, 0);
                    509:        packet_put_cstring(password);
                    510:        memset(password, 0, strlen(password));
                    511:        xfree(password);
                    512:        password = NULL;
                    513:        while (password == NULL) {
1.104     deraadt   514:                snprintf(prompt, sizeof(prompt),
1.99      markus    515:                    "Enter %.30s@%.128s's new password: ",
                    516:                    authctxt->server_user, authctxt->host);
                    517:                password = read_passphrase(prompt, RP_ALLOW_EOF);
                    518:                if (password == NULL) {
                    519:                        /* bail out */
                    520:                        return;
                    521:                }
1.104     deraadt   522:                snprintf(prompt, sizeof(prompt),
1.99      markus    523:                    "Retype %.30s@%.128s's new password: ",
                    524:                    authctxt->server_user, authctxt->host);
                    525:                retype = read_passphrase(prompt, 0);
                    526:                if (strcmp(password, retype) != 0) {
                    527:                        memset(password, 0, strlen(password));
                    528:                        xfree(password);
                    529:                        log("Mismatch; try again, EOF to quit.");
                    530:                        password = NULL;
                    531:                }
                    532:                memset(retype, 0, strlen(retype));
                    533:                xfree(retype);
                    534:        }
                    535:        packet_put_cstring(password);
                    536:        memset(password, 0, strlen(password));
                    537:        xfree(password);
                    538:        packet_add_padding(64);
                    539:        packet_send();
1.104     deraadt   540:
                    541:        dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
1.99      markus    542:            &input_userauth_passwd_changereq);
                    543: }
1.1       markus    544:
1.79      stevesk   545: static void
1.51      markus    546: clear_auth_state(Authctxt *authctxt)
                    547: {
                    548:        /* XXX clear authentication state */
1.99      markus    549:        dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
                    550:
1.51      markus    551:        if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
                    552:                debug3("clear_auth_state: key_free %p", authctxt->last_key);
                    553:                key_free(authctxt->last_key);
                    554:        }
                    555:        authctxt->last_key = NULL;
                    556:        authctxt->last_key_hint = -2;
                    557:        authctxt->last_key_sign = NULL;
                    558: }
                    559:
1.76      itojun    560: static int
1.20      markus    561: sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
1.1       markus    562: {
                    563:        Buffer b;
1.32      markus    564:        u_char *blob, *signature;
1.96      markus    565:        u_int bloblen, slen;
1.14      markus    566:        int skip = 0;
1.17      markus    567:        int ret = -1;
1.23      markus    568:        int have_sig = 1;
1.1       markus    569:
1.30      markus    570:        debug3("sign_and_send_pubkey");
1.51      markus    571:
1.28      markus    572:        if (key_to_blob(k, &blob, &bloblen) == 0) {
                    573:                /* we cannot handle this key */
1.30      markus    574:                debug3("sign_and_send_pubkey: cannot handle key");
1.28      markus    575:                return 0;
                    576:        }
1.1       markus    577:        /* data to be signed */
                    578:        buffer_init(&b);
1.26      markus    579:        if (datafellows & SSH_OLD_SESSIONID) {
                    580:                buffer_append(&b, session_id2, session_id2_len);
1.41      stevesk   581:                skip = session_id2_len;
1.26      markus    582:        } else {
1.14      markus    583:                buffer_put_string(&b, session_id2, session_id2_len);
                    584:                skip = buffer_len(&b);
                    585:        }
1.1       markus    586:        buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.20      markus    587:        buffer_put_cstring(&b, authctxt->server_user);
1.10      markus    588:        buffer_put_cstring(&b,
1.30      markus    589:            datafellows & SSH_BUG_PKSERVICE ?
1.10      markus    590:            "ssh-userauth" :
1.20      markus    591:            authctxt->service);
1.30      markus    592:        if (datafellows & SSH_BUG_PKAUTH) {
                    593:                buffer_put_char(&b, have_sig);
                    594:        } else {
                    595:                buffer_put_cstring(&b, authctxt->method->name);
                    596:                buffer_put_char(&b, have_sig);
1.41      stevesk   597:                buffer_put_cstring(&b, key_ssh_name(k));
1.30      markus    598:        }
1.1       markus    599:        buffer_put_string(&b, blob, bloblen);
                    600:
                    601:        /* generate signature */
1.51      markus    602:        ret = (*sign_callback)(authctxt, k, &signature, &slen,
                    603:            buffer_ptr(&b), buffer_len(&b));
1.17      markus    604:        if (ret == -1) {
                    605:                xfree(blob);
                    606:                buffer_free(&b);
                    607:                return 0;
                    608:        }
1.28      markus    609: #ifdef DEBUG_PK
1.1       markus    610:        buffer_dump(&b);
                    611: #endif
1.30      markus    612:        if (datafellows & SSH_BUG_PKSERVICE) {
1.10      markus    613:                buffer_clear(&b);
                    614:                buffer_append(&b, session_id2, session_id2_len);
1.51      markus    615:                skip = session_id2_len;
1.10      markus    616:                buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
1.20      markus    617:                buffer_put_cstring(&b, authctxt->server_user);
                    618:                buffer_put_cstring(&b, authctxt->service);
1.23      markus    619:                buffer_put_cstring(&b, authctxt->method->name);
                    620:                buffer_put_char(&b, have_sig);
1.30      markus    621:                if (!(datafellows & SSH_BUG_PKAUTH))
1.41      stevesk   622:                        buffer_put_cstring(&b, key_ssh_name(k));
1.10      markus    623:                buffer_put_string(&b, blob, bloblen);
                    624:        }
                    625:        xfree(blob);
1.51      markus    626:
1.1       markus    627:        /* append signature */
                    628:        buffer_put_string(&b, signature, slen);
                    629:        xfree(signature);
                    630:
                    631:        /* skip session id and packet type */
1.14      markus    632:        if (buffer_len(&b) < skip + 1)
1.20      markus    633:                fatal("userauth_pubkey: internal error");
1.14      markus    634:        buffer_consume(&b, skip + 1);
1.1       markus    635:
                    636:        /* put remaining data from buffer into packet */
                    637:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
                    638:        packet_put_raw(buffer_ptr(&b), buffer_len(&b));
                    639:        buffer_free(&b);
                    640:        packet_send();
1.17      markus    641:
                    642:        return 1;
1.16      markus    643: }
                    644:
1.76      itojun    645: static int
1.51      markus    646: send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
                    647:     int hint)
1.20      markus    648: {
1.51      markus    649:        u_char *blob;
1.97      markus    650:        u_int bloblen, have_sig = 0;
1.20      markus    651:
1.51      markus    652:        debug3("send_pubkey_test");
1.16      markus    653:
1.51      markus    654:        if (key_to_blob(k, &blob, &bloblen) == 0) {
                    655:                /* we cannot handle this key */
                    656:                debug3("send_pubkey_test: cannot handle key");
1.16      markus    657:                return 0;
                    658:        }
1.51      markus    659:        /* register callback for USERAUTH_PK_OK message */
                    660:        authctxt->last_key_sign = sign_callback;
                    661:        authctxt->last_key_hint = hint;
                    662:        authctxt->last_key = k;
                    663:        dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
                    664:
                    665:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
                    666:        packet_put_cstring(authctxt->server_user);
                    667:        packet_put_cstring(authctxt->service);
                    668:        packet_put_cstring(authctxt->method->name);
                    669:        packet_put_char(have_sig);
                    670:        if (!(datafellows & SSH_BUG_PKAUTH))
                    671:                packet_put_cstring(key_ssh_name(k));
                    672:        packet_put_string(blob, bloblen);
                    673:        xfree(blob);
                    674:        packet_send();
                    675:        return 1;
                    676: }
                    677:
1.76      itojun    678: static Key *
1.51      markus    679: load_identity_file(char *filename)
                    680: {
                    681:        Key *private;
                    682:        char prompt[300], *passphrase;
1.56      markus    683:        int quit, i;
1.52      markus    684:        struct stat st;
1.16      markus    685:
1.52      markus    686:        if (stat(filename, &st) < 0) {
                    687:                debug3("no such identity: %s", filename);
                    688:                return NULL;
                    689:        }
1.56      markus    690:        private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
                    691:        if (private == NULL) {
                    692:                if (options.batch_mode)
1.51      markus    693:                        return NULL;
1.16      markus    694:                snprintf(prompt, sizeof prompt,
1.88      deraadt   695:                    "Enter passphrase for key '%.100s': ", filename);
1.20      markus    696:                for (i = 0; i < options.number_of_password_prompts; i++) {
                    697:                        passphrase = read_passphrase(prompt, 0);
                    698:                        if (strcmp(passphrase, "") != 0) {
1.56      markus    699:                                private = key_load_private_type(KEY_UNSPEC, filename,
                    700:                                    passphrase, NULL);
1.51      markus    701:                                quit = 0;
1.20      markus    702:                        } else {
                    703:                                debug2("no passphrase given, try next key");
1.51      markus    704:                                quit = 1;
1.20      markus    705:                        }
                    706:                        memset(passphrase, 0, strlen(passphrase));
                    707:                        xfree(passphrase);
1.56      markus    708:                        if (private != NULL || quit)
1.20      markus    709:                                break;
                    710:                        debug2("bad passphrase given, try again...");
1.16      markus    711:                }
                    712:        }
1.51      markus    713:        return private;
                    714: }
                    715:
1.76      itojun    716: static int
1.96      markus    717: identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
                    718:     u_char *data, u_int datalen)
1.51      markus    719: {
                    720:        Key *private;
                    721:        int idx, ret;
                    722:
                    723:        idx = authctxt->last_key_hint;
                    724:        if (idx < 0)
                    725:                return -1;
1.80      markus    726:
                    727:        /* private key is stored in external hardware */
1.88      deraadt   728:        if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
1.80      markus    729:                return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
                    730:
1.51      markus    731:        private = load_identity_file(options.identity_files[idx]);
                    732:        if (private == NULL)
                    733:                return -1;
                    734:        ret = key_sign(private, sigp, lenp, data, datalen);
                    735:        key_free(private);
1.17      markus    736:        return ret;
                    737: }
                    738:
1.76      itojun    739: static int
1.96      markus    740: agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
                    741:     u_char *data, u_int datalen)
1.17      markus    742: {
1.20      markus    743:        return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
1.17      markus    744: }
                    745:
1.76      itojun    746: static int
1.96      markus    747: key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
                    748:     u_char *data, u_int datalen)
1.51      markus    749: {
1.67      markus    750:        return key_sign(key, sigp, lenp, data, datalen);
1.51      markus    751: }
                    752:
1.76      itojun    753: static int
1.20      markus    754: userauth_pubkey_agent(Authctxt *authctxt)
1.17      markus    755: {
                    756:        static int called = 0;
1.28      markus    757:        int ret = 0;
1.17      markus    758:        char *comment;
                    759:        Key *k;
                    760:
                    761:        if (called == 0) {
1.28      markus    762:                if (ssh_get_num_identities(authctxt->agent, 2) == 0)
                    763:                        debug2("userauth_pubkey_agent: no keys at all");
1.20      markus    764:                called = 1;
1.17      markus    765:        }
1.28      markus    766:        k = ssh_get_next_identity(authctxt->agent, &comment, 2);
1.20      markus    767:        if (k == NULL) {
1.28      markus    768:                debug2("userauth_pubkey_agent: no more keys");
                    769:        } else {
1.109     markus    770:                debug("Offering agent key: %s", comment);
1.28      markus    771:                xfree(comment);
1.51      markus    772:                ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
                    773:                if (ret == 0)
                    774:                        key_free(k);
1.20      markus    775:        }
1.28      markus    776:        if (ret == 0)
                    777:                debug2("userauth_pubkey_agent: no message sent");
1.17      markus    778:        return ret;
1.1       markus    779: }
                    780:
1.20      markus    781: int
                    782: userauth_pubkey(Authctxt *authctxt)
                    783: {
                    784:        static int idx = 0;
                    785:        int sent = 0;
1.51      markus    786:        Key *key;
                    787:        char *filename;
1.20      markus    788:
1.28      markus    789:        if (authctxt->agent != NULL) {
                    790:                do {
                    791:                        sent = userauth_pubkey_agent(authctxt);
1.87      deraadt   792:                } while (!sent && authctxt->agent->howmany > 0);
1.28      markus    793:        }
                    794:        while (!sent && idx < options.num_identity_files) {
1.51      markus    795:                key = options.identity_keys[idx];
                    796:                filename = options.identity_files[idx];
                    797:                if (key == NULL) {
1.109     markus    798:                        debug("Trying private key: %s", filename);
1.51      markus    799:                        key = load_identity_file(filename);
                    800:                        if (key != NULL) {
                    801:                                sent = sign_and_send_pubkey(authctxt, key,
                    802:                                    key_sign_cb);
                    803:                                key_free(key);
                    804:                        }
                    805:                } else if (key->type != KEY_RSA1) {
1.109     markus    806:                        debug("Offering public key: %s", filename);
1.51      markus    807:                        sent = send_pubkey_test(authctxt, key,
                    808:                            identity_sign_cb, idx);
                    809:                }
1.28      markus    810:                idx++;
                    811:        }
1.20      markus    812:        return sent;
                    813: }
                    814:
1.23      markus    815: /*
                    816:  * Send userauth request message specifying keyboard-interactive method.
                    817:  */
                    818: int
                    819: userauth_kbdint(Authctxt *authctxt)
                    820: {
                    821:        static int attempt = 0;
                    822:
                    823:        if (attempt++ >= options.number_of_password_prompts)
                    824:                return 0;
1.82      markus    825:        /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
                    826:        if (attempt > 1 && !authctxt->info_req_seen) {
                    827:                debug3("userauth_kbdint: disable: no info_req_seen");
                    828:                dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
                    829:                return 0;
                    830:        }
1.23      markus    831:
                    832:        debug2("userauth_kbdint");
                    833:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
                    834:        packet_put_cstring(authctxt->server_user);
                    835:        packet_put_cstring(authctxt->service);
                    836:        packet_put_cstring(authctxt->method->name);
                    837:        packet_put_cstring("");                                 /* lang */
                    838:        packet_put_cstring(options.kbd_interactive_devices ?
                    839:            options.kbd_interactive_devices : "");
                    840:        packet_send();
                    841:
                    842:        dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
                    843:        return 1;
                    844: }
                    845:
                    846: /*
1.46      markus    847:  * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
1.23      markus    848:  */
                    849: void
1.92      markus    850: input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
1.23      markus    851: {
                    852:        Authctxt *authctxt = ctxt;
1.46      markus    853:        char *name, *inst, *lang, *prompt, *response;
1.32      markus    854:        u_int num_prompts, i;
1.23      markus    855:        int echo = 0;
                    856:
                    857:        debug2("input_userauth_info_req");
                    858:
                    859:        if (authctxt == NULL)
                    860:                fatal("input_userauth_info_req: no authentication context");
1.82      markus    861:
                    862:        authctxt->info_req_seen = 1;
1.23      markus    863:
                    864:        name = packet_get_string(NULL);
                    865:        inst = packet_get_string(NULL);
                    866:        lang = packet_get_string(NULL);
                    867:        if (strlen(name) > 0)
1.78      markus    868:                log("%s", name);
1.23      markus    869:        if (strlen(inst) > 0)
1.78      markus    870:                log("%s", inst);
1.46      markus    871:        xfree(name);
1.23      markus    872:        xfree(inst);
1.46      markus    873:        xfree(lang);
1.23      markus    874:
                    875:        num_prompts = packet_get_int();
                    876:        /*
                    877:         * Begin to build info response packet based on prompts requested.
                    878:         * We commit to providing the correct number of responses, so if
                    879:         * further on we run into a problem that prevents this, we have to
                    880:         * be sure and clean this up and send a correct error response.
                    881:         */
                    882:        packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
                    883:        packet_put_int(num_prompts);
                    884:
1.73      markus    885:        debug2("input_userauth_info_req: num_prompts %d", num_prompts);
1.23      markus    886:        for (i = 0; i < num_prompts; i++) {
                    887:                prompt = packet_get_string(NULL);
                    888:                echo = packet_get_char();
                    889:
1.77      markus    890:                response = read_passphrase(prompt, echo ? RP_ECHO : 0);
1.23      markus    891:
1.49      markus    892:                packet_put_cstring(response);
1.23      markus    893:                memset(response, 0, strlen(response));
                    894:                xfree(response);
                    895:                xfree(prompt);
                    896:        }
1.90      markus    897:        packet_check_eom(); /* done with parsing incoming message. */
1.23      markus    898:
1.85      markus    899:        packet_add_padding(64);
1.23      markus    900:        packet_send();
1.68      markus    901: }
                    902:
1.100     markus    903: static int
1.105     deraadt   904: ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
1.100     markus    905:     u_char *data, u_int datalen)
                    906: {
                    907:        Buffer b;
1.101     markus    908:        struct stat st;
1.100     markus    909:        pid_t pid;
1.103     markus    910:        int to[2], from[2], status, version = 2;
1.100     markus    911:
1.109     markus    912:        debug2("ssh_keysign called");
1.100     markus    913:
1.101     markus    914:        if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
                    915:                error("ssh_keysign: no installed: %s", strerror(errno));
                    916:                return -1;
                    917:        }
1.100     markus    918:        if (fflush(stdout) != 0)
                    919:                error("ssh_keysign: fflush: %s", strerror(errno));
                    920:        if (pipe(to) < 0) {
                    921:                error("ssh_keysign: pipe: %s", strerror(errno));
                    922:                return -1;
                    923:        }
                    924:        if (pipe(from) < 0) {
                    925:                error("ssh_keysign: pipe: %s", strerror(errno));
                    926:                return -1;
                    927:        }
                    928:        if ((pid = fork()) < 0) {
                    929:                error("ssh_keysign: fork: %s", strerror(errno));
                    930:                return -1;
                    931:        }
                    932:        if (pid == 0) {
                    933:                seteuid(getuid());
                    934:                setuid(getuid());
                    935:                close(from[0]);
                    936:                if (dup2(from[1], STDOUT_FILENO) < 0)
                    937:                        fatal("ssh_keysign: dup2: %s", strerror(errno));
                    938:                close(to[1]);
                    939:                if (dup2(to[0], STDIN_FILENO) < 0)
                    940:                        fatal("ssh_keysign: dup2: %s", strerror(errno));
1.103     markus    941:                close(from[1]);
                    942:                close(to[0]);
1.102     markus    943:                execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
1.100     markus    944:                fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
                    945:                    strerror(errno));
                    946:        }
                    947:        close(from[1]);
                    948:        close(to[0]);
                    949:
                    950:        buffer_init(&b);
1.103     markus    951:        buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
1.100     markus    952:        buffer_put_string(&b, data, datalen);
1.110     djm       953:        ssh_msg_send(to[1], version, &b);
1.100     markus    954:
1.110     djm       955:        if (ssh_msg_recv(from[0], &b) < 0) {
1.101     markus    956:                error("ssh_keysign: no reply");
1.100     markus    957:                buffer_clear(&b);
                    958:                return -1;
                    959:        }
1.101     markus    960:        close(from[0]);
                    961:        close(to[1]);
                    962:
1.103     markus    963:        while (waitpid(pid, &status, 0) < 0)
                    964:                if (errno != EINTR)
                    965:                        break;
1.101     markus    966:
1.100     markus    967:        if (buffer_get_char(&b) != version) {
1.101     markus    968:                error("ssh_keysign: bad version");
1.100     markus    969:                buffer_clear(&b);
                    970:                return -1;
                    971:        }
                    972:        *sigp = buffer_get_string(&b, lenp);
                    973:        buffer_clear(&b);
                    974:
                    975:        return 0;
                    976: }
                    977:
1.68      markus    978: int
                    979: userauth_hostbased(Authctxt *authctxt)
                    980: {
                    981:        Key *private = NULL;
1.100     markus    982:        Sensitive *sensitive = authctxt->sensitive;
1.68      markus    983:        Buffer b;
                    984:        u_char *signature, *blob;
                    985:        char *chost, *pkalg, *p;
1.72      markus    986:        const char *service;
1.68      markus    987:        u_int blen, slen;
1.71      markus    988:        int ok, i, len, found = 0;
1.68      markus    989:
                    990:        /* check for a useful key */
1.100     markus    991:        for (i = 0; i < sensitive->nkeys; i++) {
                    992:                private = sensitive->keys[i];
1.68      markus    993:                if (private && private->type != KEY_RSA1) {
                    994:                        found = 1;
                    995:                        /* we take and free the key */
1.100     markus    996:                        sensitive->keys[i] = NULL;
1.68      markus    997:                        break;
                    998:                }
                    999:        }
                   1000:        if (!found) {
1.109     markus   1001:                debug("No more client hostkeys for hostbased authentication.");
1.68      markus   1002:                return 0;
                   1003:        }
                   1004:        if (key_to_blob(private, &blob, &blen) == 0) {
                   1005:                key_free(private);
                   1006:                return 0;
                   1007:        }
1.84      markus   1008:        /* figure out a name for the client host */
                   1009:        p = get_local_name(packet_get_connection_in());
                   1010:        if (p == NULL) {
                   1011:                error("userauth_hostbased: cannot get local ipaddr/name");
                   1012:                key_free(private);
                   1013:                return 0;
                   1014:        }
                   1015:        len = strlen(p) + 2;
                   1016:        chost = xmalloc(len);
                   1017:        strlcpy(chost, p, len);
                   1018:        strlcat(chost, ".", len);
                   1019:        debug2("userauth_hostbased: chost %s", chost);
1.112     markus   1020:        xfree(p);
1.84      markus   1021:
1.72      markus   1022:        service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
                   1023:            authctxt->service;
1.68      markus   1024:        pkalg = xstrdup(key_ssh_name(private));
                   1025:        buffer_init(&b);
                   1026:        /* construct data */
1.72      markus   1027:        buffer_put_string(&b, session_id2, session_id2_len);
1.68      markus   1028:        buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
                   1029:        buffer_put_cstring(&b, authctxt->server_user);
1.72      markus   1030:        buffer_put_cstring(&b, service);
1.68      markus   1031:        buffer_put_cstring(&b, authctxt->method->name);
                   1032:        buffer_put_cstring(&b, pkalg);
                   1033:        buffer_put_string(&b, blob, blen);
                   1034:        buffer_put_cstring(&b, chost);
                   1035:        buffer_put_cstring(&b, authctxt->local_user);
                   1036: #ifdef DEBUG_PK
                   1037:        buffer_dump(&b);
                   1038: #endif
1.100     markus   1039:        if (sensitive->external_keysign)
                   1040:                ok = ssh_keysign(private, &signature, &slen,
                   1041:                    buffer_ptr(&b), buffer_len(&b));
                   1042:        else
                   1043:                ok = key_sign(private, &signature, &slen,
                   1044:                    buffer_ptr(&b), buffer_len(&b));
1.68      markus   1045:        key_free(private);
                   1046:        buffer_free(&b);
                   1047:        if (ok != 0) {
                   1048:                error("key_sign failed");
                   1049:                xfree(chost);
                   1050:                xfree(pkalg);
                   1051:                return 0;
                   1052:        }
                   1053:        packet_start(SSH2_MSG_USERAUTH_REQUEST);
                   1054:        packet_put_cstring(authctxt->server_user);
                   1055:        packet_put_cstring(authctxt->service);
                   1056:        packet_put_cstring(authctxt->method->name);
                   1057:        packet_put_cstring(pkalg);
                   1058:        packet_put_string(blob, blen);
                   1059:        packet_put_cstring(chost);
                   1060:        packet_put_cstring(authctxt->local_user);
                   1061:        packet_put_string(signature, slen);
                   1062:        memset(signature, 's', slen);
                   1063:        xfree(signature);
                   1064:        xfree(chost);
                   1065:        xfree(pkalg);
                   1066:
                   1067:        packet_send();
                   1068:        return 1;
1.23      markus   1069: }
1.20      markus   1070:
                   1071: /* find auth method */
                   1072:
                   1073: /*
                   1074:  * given auth method name, if configurable options permit this method fill
                   1075:  * in auth_ident field and return true, otherwise return false.
                   1076:  */
1.76      itojun   1077: static int
1.20      markus   1078: authmethod_is_enabled(Authmethod *method)
                   1079: {
                   1080:        if (method == NULL)
                   1081:                return 0;
                   1082:        /* return false if options indicate this method is disabled */
                   1083:        if  (method->enabled == NULL || *method->enabled == 0)
                   1084:                return 0;
                   1085:        /* return false if batch mode is enabled but method needs interactive mode */
                   1086:        if  (method->batch_flag != NULL && *method->batch_flag != 0)
                   1087:                return 0;
                   1088:        return 1;
                   1089: }
                   1090:
1.76      itojun   1091: static Authmethod *
1.20      markus   1092: authmethod_lookup(const char *name)
1.1       markus   1093: {
1.20      markus   1094:        Authmethod *method = NULL;
                   1095:        if (name != NULL)
                   1096:                for (method = authmethods; method->name != NULL; method++)
                   1097:                        if (strcmp(name, method->name) == 0)
                   1098:                                return method;
                   1099:        debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
                   1100:        return NULL;
                   1101: }
1.1       markus   1102:
1.53      markus   1103: /* XXX internal state */
                   1104: static Authmethod *current = NULL;
                   1105: static char *supported = NULL;
                   1106: static char *preferred = NULL;
1.105     deraadt  1107:
1.20      markus   1108: /*
                   1109:  * Given the authentication method list sent by the server, return the
                   1110:  * next method we should try.  If the server initially sends a nil list,
1.67      markus   1111:  * use a built-in default list.
1.41      stevesk  1112:  */
1.76      itojun   1113: static Authmethod *
1.20      markus   1114: authmethod_get(char *authlist)
                   1115: {
1.53      markus   1116:        char *name = NULL;
1.97      markus   1117:        u_int next;
1.41      stevesk  1118:
1.20      markus   1119:        /* Use a suitable default if we're passed a nil list.  */
                   1120:        if (authlist == NULL || strlen(authlist) == 0)
1.53      markus   1121:                authlist = options.preferred_authentications;
1.20      markus   1122:
1.53      markus   1123:        if (supported == NULL || strcmp(authlist, supported) != 0) {
                   1124:                debug3("start over, passed a different list %s", authlist);
                   1125:                if (supported != NULL)
                   1126:                        xfree(supported);
                   1127:                supported = xstrdup(authlist);
                   1128:                preferred = options.preferred_authentications;
                   1129:                debug3("preferred %s", preferred);
                   1130:                current = NULL;
                   1131:        } else if (current != NULL && authmethod_is_enabled(current))
                   1132:                return current;
1.20      markus   1133:
1.53      markus   1134:        for (;;) {
                   1135:                if ((name = match_list(preferred, supported, &next)) == NULL) {
1.109     markus   1136:                        debug("No more authentication methods to try.");
1.53      markus   1137:                        current = NULL;
                   1138:                        return NULL;
                   1139:                }
                   1140:                preferred += next;
1.23      markus   1141:                debug3("authmethod_lookup %s", name);
1.53      markus   1142:                debug3("remaining preferred: %s", preferred);
                   1143:                if ((current = authmethod_lookup(name)) != NULL &&
                   1144:                    authmethod_is_enabled(current)) {
1.23      markus   1145:                        debug3("authmethod_is_enabled %s", name);
1.109     markus   1146:                        debug("Next authentication method: %s", name);
1.53      markus   1147:                        return current;
1.23      markus   1148:                }
1.1       markus   1149:        }
1.53      markus   1150: }
1.1       markus   1151:
1.79      stevesk  1152: static char *
1.53      markus   1153: authmethods_get(void)
                   1154: {
                   1155:        Authmethod *method = NULL;
1.93      markus   1156:        Buffer b;
                   1157:        char *list;
1.27      provos   1158:
1.93      markus   1159:        buffer_init(&b);
1.53      markus   1160:        for (method = authmethods; method->name != NULL; method++) {
                   1161:                if (authmethod_is_enabled(method)) {
1.93      markus   1162:                        if (buffer_len(&b) > 0)
                   1163:                                buffer_append(&b, ",", 1);
                   1164:                        buffer_append(&b, method->name, strlen(method->name));
1.53      markus   1165:                }
                   1166:        }
1.93      markus   1167:        buffer_append(&b, "\0", 1);
                   1168:        list = xstrdup(buffer_ptr(&b));
                   1169:        buffer_free(&b);
                   1170:        return list;
1.1       markus   1171: }