10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * Copyright (c) 2000 Markus Friedl. All rights reserved.
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
50Sstevel@tonic-gate * modification, are permitted provided that the following conditions
60Sstevel@tonic-gate * are met:
70Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
80Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
90Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
100Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
110Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
140Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
150Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
160Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
170Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
180Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
190Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
200Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
210Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
220Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate /*
258658SJan.Pechanec@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
260Sstevel@tonic-gate * Use is subject to license terms.
270Sstevel@tonic-gate */
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include "includes.h"
300Sstevel@tonic-gate RCSID("$OpenBSD: sshconnect2.c,v 1.107 2002/07/01 19:48:46 markus Exp $");
310Sstevel@tonic-gate
320Sstevel@tonic-gate #include "ssh.h"
330Sstevel@tonic-gate #include "ssh2.h"
340Sstevel@tonic-gate #include "xmalloc.h"
350Sstevel@tonic-gate #include "buffer.h"
360Sstevel@tonic-gate #include "packet.h"
370Sstevel@tonic-gate #include "compat.h"
380Sstevel@tonic-gate #include "bufaux.h"
390Sstevel@tonic-gate #include "cipher.h"
400Sstevel@tonic-gate #include "kex.h"
410Sstevel@tonic-gate #include "myproposal.h"
420Sstevel@tonic-gate #include "sshconnect.h"
430Sstevel@tonic-gate #include "authfile.h"
440Sstevel@tonic-gate #include "dh.h"
450Sstevel@tonic-gate #include "authfd.h"
460Sstevel@tonic-gate #include "log.h"
470Sstevel@tonic-gate #include "readconf.h"
480Sstevel@tonic-gate #include "readpass.h"
490Sstevel@tonic-gate #include "match.h"
500Sstevel@tonic-gate #include "dispatch.h"
510Sstevel@tonic-gate #include "canohost.h"
520Sstevel@tonic-gate #include "msg.h"
530Sstevel@tonic-gate #include "pathnames.h"
540Sstevel@tonic-gate #include "g11n.h"
550Sstevel@tonic-gate
560Sstevel@tonic-gate #ifdef GSSAPI
570Sstevel@tonic-gate #include "ssh-gss.h"
580Sstevel@tonic-gate extern Gssctxt *xxx_gssctxt;
590Sstevel@tonic-gate #endif /* GSSAPI */
600Sstevel@tonic-gate
610Sstevel@tonic-gate /* import */
620Sstevel@tonic-gate extern char *client_version_string;
630Sstevel@tonic-gate extern char *server_version_string;
640Sstevel@tonic-gate extern Options options;
654958Sjp161948 extern Buffer command;
660Sstevel@tonic-gate
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate * SSH2 key exchange
690Sstevel@tonic-gate */
700Sstevel@tonic-gate
710Sstevel@tonic-gate u_char *session_id2 = NULL;
720Sstevel@tonic-gate int session_id2_len = 0;
730Sstevel@tonic-gate
740Sstevel@tonic-gate char *xxx_host;
750Sstevel@tonic-gate struct sockaddr *xxx_hostaddr;
760Sstevel@tonic-gate
770Sstevel@tonic-gate Kex *xxx_kex = NULL;
780Sstevel@tonic-gate
790Sstevel@tonic-gate static int
verify_host_key_callback(Key * hostkey)800Sstevel@tonic-gate verify_host_key_callback(Key *hostkey)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate if (verify_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
830Sstevel@tonic-gate fatal("Host key verification failed.");
840Sstevel@tonic-gate return 0;
850Sstevel@tonic-gate }
860Sstevel@tonic-gate
870Sstevel@tonic-gate static int
accept_host_key_callback(Key * hostkey)880Sstevel@tonic-gate accept_host_key_callback(Key *hostkey)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate if (accept_host_key(xxx_host, xxx_hostaddr, hostkey) == -1)
910Sstevel@tonic-gate log("GSS-API authenticated host key addition to "
920Sstevel@tonic-gate "known_hosts file failed");
930Sstevel@tonic-gate return 0;
940Sstevel@tonic-gate }
950Sstevel@tonic-gate
960Sstevel@tonic-gate void
ssh_kex2(char * host,struct sockaddr * hostaddr)970Sstevel@tonic-gate ssh_kex2(char *host, struct sockaddr *hostaddr)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate Kex *kex;
1000Sstevel@tonic-gate Kex_hook_func kex_hook = NULL;
1018658SJan.Pechanec@Sun.COM static char **myproposal;
1028658SJan.Pechanec@Sun.COM
1038658SJan.Pechanec@Sun.COM myproposal = my_clnt_proposal;
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate xxx_host = host;
1060Sstevel@tonic-gate xxx_hostaddr = hostaddr;
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate #ifdef GSSAPI
1090Sstevel@tonic-gate /* Add the GSSAPI mechanisms currently supported on this client to
1100Sstevel@tonic-gate * the key exchange algorithm proposal */
1110Sstevel@tonic-gate if (options.gss_keyex)
1120Sstevel@tonic-gate kex_hook = ssh_gssapi_client_kex_hook;
1130Sstevel@tonic-gate #endif /* GSSAPI */
1140Sstevel@tonic-gate if (options.ciphers == (char *)-1) {
1150Sstevel@tonic-gate log("No valid ciphers for protocol version 2 given, using defaults.");
1160Sstevel@tonic-gate options.ciphers = NULL;
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate if (options.ciphers != NULL) {
1190Sstevel@tonic-gate myproposal[PROPOSAL_ENC_ALGS_CTOS] =
1200Sstevel@tonic-gate myproposal[PROPOSAL_ENC_ALGS_STOC] = options.ciphers;
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate myproposal[PROPOSAL_ENC_ALGS_CTOS] =
1230Sstevel@tonic-gate compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_CTOS]);
1240Sstevel@tonic-gate myproposal[PROPOSAL_ENC_ALGS_STOC] =
1250Sstevel@tonic-gate compat_cipher_proposal(myproposal[PROPOSAL_ENC_ALGS_STOC]);
1260Sstevel@tonic-gate if (options.compression) {
1270Sstevel@tonic-gate myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1280Sstevel@tonic-gate myproposal[PROPOSAL_COMP_ALGS_STOC] = "zlib,none";
1290Sstevel@tonic-gate } else {
1300Sstevel@tonic-gate myproposal[PROPOSAL_COMP_ALGS_CTOS] =
1310Sstevel@tonic-gate myproposal[PROPOSAL_COMP_ALGS_STOC] = "none,zlib";
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate if (options.macs != NULL) {
1340Sstevel@tonic-gate myproposal[PROPOSAL_MAC_ALGS_CTOS] =
1350Sstevel@tonic-gate myproposal[PROPOSAL_MAC_ALGS_STOC] = options.macs;
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate if (options.hostkeyalgorithms != NULL)
1380Sstevel@tonic-gate myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] =
1390Sstevel@tonic-gate options.hostkeyalgorithms;
1400Sstevel@tonic-gate
1415562Sjp161948 if (options.rekey_limit)
1425562Sjp161948 packet_set_rekey_limit((u_int32_t)options.rekey_limit);
1435562Sjp161948
1440Sstevel@tonic-gate if (datafellows & SSH_BUG_LOCALES_NOT_LANGTAGS) {
1450Sstevel@tonic-gate char *locale = setlocale(LC_ALL, "");
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /* Solaris 9 SSHD expects a locale, not a langtag list */
1480Sstevel@tonic-gate myproposal[PROPOSAL_LANG_CTOS] = "";
1490Sstevel@tonic-gate if (locale != NULL && *locale != '\0' &&
1500Sstevel@tonic-gate strcmp(locale, "C") != 0)
1510Sstevel@tonic-gate myproposal[PROPOSAL_LANG_CTOS] = locale;
1520Sstevel@tonic-gate } else {
1530Sstevel@tonic-gate myproposal[PROPOSAL_LANG_CTOS] = g11n_getlangs();
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate /* Same languages proposal for both directions */
1570Sstevel@tonic-gate if (myproposal[PROPOSAL_LANG_CTOS] == NULL) {
1580Sstevel@tonic-gate myproposal[PROPOSAL_LANG_CTOS] = "";
1590Sstevel@tonic-gate myproposal[PROPOSAL_LANG_STOC] = "";
1600Sstevel@tonic-gate } else {
1610Sstevel@tonic-gate myproposal[PROPOSAL_LANG_STOC] =
1620Sstevel@tonic-gate myproposal[PROPOSAL_LANG_CTOS];
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate /* start key exchange */
1660Sstevel@tonic-gate kex = kex_setup(host, myproposal, kex_hook);
1677574SJan.Pechanec@Sun.COM kex_start(kex);
1680Sstevel@tonic-gate kex->kex[KEX_DH_GRP1_SHA1] = kexdh_client;
1690Sstevel@tonic-gate kex->kex[KEX_DH_GEX_SHA1] = kexgex_client;
1700Sstevel@tonic-gate #ifdef GSSAPI
1710Sstevel@tonic-gate kex->kex[KEX_GSS_GRP1_SHA1] = kexgss_client;
1720Sstevel@tonic-gate kex->options.gss_deleg_creds = options.gss_deleg_creds;
1730Sstevel@tonic-gate #endif /* GSSAPI */
1740Sstevel@tonic-gate kex->client_version_string=client_version_string;
1750Sstevel@tonic-gate kex->server_version_string=server_version_string;
1760Sstevel@tonic-gate kex->verify_host_key=&verify_host_key_callback;
1770Sstevel@tonic-gate kex->accept_host_key=&accept_host_key_callback;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate xxx_kex = kex;
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate dispatch_run(DISPATCH_BLOCK, &kex->done, kex);
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate session_id2 = kex->session_id;
1840Sstevel@tonic-gate session_id2_len = kex->session_id_len;
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate #ifdef DEBUG_KEXDH
1870Sstevel@tonic-gate /* send 1st encrypted/maced/compressed message */
1880Sstevel@tonic-gate packet_start(SSH2_MSG_IGNORE);
1890Sstevel@tonic-gate packet_put_cstring("markus");
1900Sstevel@tonic-gate packet_send();
1910Sstevel@tonic-gate packet_write_wait();
1920Sstevel@tonic-gate #endif
1930Sstevel@tonic-gate debug("done: ssh_kex2.");
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate * Authenticate user
1980Sstevel@tonic-gate */
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate typedef struct Authctxt Authctxt;
2010Sstevel@tonic-gate typedef struct Authmethod Authmethod;
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate typedef int sign_cb_fn(
2040Sstevel@tonic-gate Authctxt *authctxt, Key *key,
2050Sstevel@tonic-gate u_char **sigp, u_int *lenp, u_char *data, u_int datalen);
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate struct Authctxt {
2080Sstevel@tonic-gate const char *server_user;
2090Sstevel@tonic-gate const char *local_user;
2100Sstevel@tonic-gate const char *host;
2110Sstevel@tonic-gate const char *service;
2120Sstevel@tonic-gate Authmethod *method;
2130Sstevel@tonic-gate int success;
2140Sstevel@tonic-gate char *authlist;
2150Sstevel@tonic-gate /* pubkey */
2160Sstevel@tonic-gate Key *last_key;
2170Sstevel@tonic-gate sign_cb_fn *last_key_sign;
2180Sstevel@tonic-gate int last_key_hint;
2190Sstevel@tonic-gate AuthenticationConnection *agent;
2200Sstevel@tonic-gate /* hostbased */
2210Sstevel@tonic-gate Sensitive *sensitive;
2220Sstevel@tonic-gate /* kbd-interactive */
2230Sstevel@tonic-gate int info_req_seen;
2240Sstevel@tonic-gate /* generic */
2250Sstevel@tonic-gate void *methoddata;
2260Sstevel@tonic-gate };
2270Sstevel@tonic-gate struct Authmethod {
2280Sstevel@tonic-gate char *name; /* string to compare against server's list */
2290Sstevel@tonic-gate int (*userauth)(Authctxt *authctxt);
2300Sstevel@tonic-gate void (*cleanup)(Authctxt *authctxt);
2310Sstevel@tonic-gate int *enabled; /* flag in option struct that enables method */
2320Sstevel@tonic-gate int *batch_flag; /* flag in option struct that disables method */
2330Sstevel@tonic-gate };
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate void input_userauth_success(int, u_int32_t, void *);
2360Sstevel@tonic-gate void input_userauth_failure(int, u_int32_t, void *);
2370Sstevel@tonic-gate void input_userauth_banner(int, u_int32_t, void *);
2380Sstevel@tonic-gate void input_userauth_error(int, u_int32_t, void *);
2390Sstevel@tonic-gate void input_userauth_info_req(int, u_int32_t, void *);
2400Sstevel@tonic-gate void input_userauth_pk_ok(int, u_int32_t, void *);
2410Sstevel@tonic-gate void input_userauth_passwd_changereq(int, u_int32_t, void *);
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate int userauth_none(Authctxt *);
2440Sstevel@tonic-gate int userauth_pubkey(Authctxt *);
2450Sstevel@tonic-gate int userauth_passwd(Authctxt *);
2460Sstevel@tonic-gate int userauth_kbdint(Authctxt *);
2470Sstevel@tonic-gate int userauth_hostbased(Authctxt *);
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate #ifdef GSSAPI
2500Sstevel@tonic-gate static int userauth_gssapi_keyex(Authctxt *authctxt);
2510Sstevel@tonic-gate static int userauth_gssapi(Authctxt *authctxt);
2520Sstevel@tonic-gate static void userauth_gssapi_cleanup(Authctxt *authctxt);
2530Sstevel@tonic-gate static void input_gssapi_response(int type, u_int32_t, void *);
2540Sstevel@tonic-gate static void input_gssapi_token(int type, u_int32_t, void *);
2550Sstevel@tonic-gate static void input_gssapi_hash(int type, u_int32_t, void *);
2560Sstevel@tonic-gate static void input_gssapi_error(int, u_int32_t, void *);
2570Sstevel@tonic-gate static void input_gssapi_errtok(int, u_int32_t, void *);
2580Sstevel@tonic-gate #endif /* GSSAPI */
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate void userauth(Authctxt *, char *);
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate static int sign_and_send_pubkey(Authctxt *, Key *, sign_cb_fn *);
2630Sstevel@tonic-gate static void clear_auth_state(Authctxt *);
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate static Authmethod *authmethod_get(char *authlist);
2660Sstevel@tonic-gate static Authmethod *authmethod_lookup(const char *name);
2670Sstevel@tonic-gate static char *authmethods_get(void);
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate Authmethod authmethods[] = {
2700Sstevel@tonic-gate #ifdef GSSAPI
2710Sstevel@tonic-gate {"gssapi-keyex",
2720Sstevel@tonic-gate userauth_gssapi_keyex,
2730Sstevel@tonic-gate userauth_gssapi_cleanup,
2740Sstevel@tonic-gate &options.gss_keyex,
2750Sstevel@tonic-gate NULL},
2760Sstevel@tonic-gate {"gssapi-with-mic",
2770Sstevel@tonic-gate userauth_gssapi,
2780Sstevel@tonic-gate userauth_gssapi_cleanup,
2790Sstevel@tonic-gate &options.gss_authentication,
2800Sstevel@tonic-gate NULL},
2810Sstevel@tonic-gate #endif /* GSSAPI */
2820Sstevel@tonic-gate {"hostbased",
2830Sstevel@tonic-gate userauth_hostbased,
2840Sstevel@tonic-gate NULL,
2850Sstevel@tonic-gate &options.hostbased_authentication,
2860Sstevel@tonic-gate NULL},
2870Sstevel@tonic-gate {"publickey",
2880Sstevel@tonic-gate userauth_pubkey,
2890Sstevel@tonic-gate NULL,
2900Sstevel@tonic-gate &options.pubkey_authentication,
2910Sstevel@tonic-gate NULL},
2920Sstevel@tonic-gate {"keyboard-interactive",
2930Sstevel@tonic-gate userauth_kbdint,
2940Sstevel@tonic-gate NULL,
2950Sstevel@tonic-gate &options.kbd_interactive_authentication,
2960Sstevel@tonic-gate &options.batch_mode},
2970Sstevel@tonic-gate {"password",
2980Sstevel@tonic-gate userauth_passwd,
2990Sstevel@tonic-gate NULL,
3000Sstevel@tonic-gate &options.password_authentication,
3010Sstevel@tonic-gate &options.batch_mode},
3020Sstevel@tonic-gate {"none",
3030Sstevel@tonic-gate userauth_none,
3040Sstevel@tonic-gate NULL,
3050Sstevel@tonic-gate NULL,
3060Sstevel@tonic-gate NULL},
3070Sstevel@tonic-gate {NULL, NULL, NULL, NULL, NULL}
3080Sstevel@tonic-gate };
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate void
ssh_userauth2(const char * local_user,const char * server_user,char * host,Sensitive * sensitive)3110Sstevel@tonic-gate ssh_userauth2(const char *local_user, const char *server_user, char *host,
3120Sstevel@tonic-gate Sensitive *sensitive)
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate Authctxt authctxt;
3150Sstevel@tonic-gate int type;
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate if (options.challenge_response_authentication)
3180Sstevel@tonic-gate options.kbd_interactive_authentication = 1;
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate packet_start(SSH2_MSG_SERVICE_REQUEST);
3210Sstevel@tonic-gate packet_put_cstring("ssh-userauth");
3220Sstevel@tonic-gate packet_send();
3230Sstevel@tonic-gate debug("send SSH2_MSG_SERVICE_REQUEST");
3240Sstevel@tonic-gate packet_write_wait();
3250Sstevel@tonic-gate type = packet_read();
3260Sstevel@tonic-gate if (type != SSH2_MSG_SERVICE_ACCEPT)
3270Sstevel@tonic-gate fatal("Server denied authentication request: %d", type);
3280Sstevel@tonic-gate if (packet_remaining() > 0) {
3290Sstevel@tonic-gate char *reply = packet_get_string(NULL);
3300Sstevel@tonic-gate debug2("service_accept: %s", reply);
3310Sstevel@tonic-gate xfree(reply);
3320Sstevel@tonic-gate } else {
3330Sstevel@tonic-gate debug2("buggy server: service_accept w/o service");
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate packet_check_eom();
3360Sstevel@tonic-gate debug("got SSH2_MSG_SERVICE_ACCEPT");
3370Sstevel@tonic-gate
3380Sstevel@tonic-gate if (options.preferred_authentications == NULL)
3390Sstevel@tonic-gate options.preferred_authentications = authmethods_get();
3400Sstevel@tonic-gate
3410Sstevel@tonic-gate /* setup authentication context */
3420Sstevel@tonic-gate memset(&authctxt, 0, sizeof(authctxt));
3430Sstevel@tonic-gate authctxt.agent = ssh_get_authentication_connection();
3440Sstevel@tonic-gate authctxt.server_user = server_user;
3450Sstevel@tonic-gate authctxt.local_user = local_user;
3460Sstevel@tonic-gate authctxt.host = host;
3470Sstevel@tonic-gate authctxt.service = "ssh-connection"; /* service name */
3480Sstevel@tonic-gate authctxt.success = 0;
3490Sstevel@tonic-gate authctxt.method = authmethod_lookup("none");
3500Sstevel@tonic-gate authctxt.authlist = NULL;
3510Sstevel@tonic-gate authctxt.methoddata = NULL;
3520Sstevel@tonic-gate authctxt.sensitive = sensitive;
3530Sstevel@tonic-gate authctxt.info_req_seen = 0;
3540Sstevel@tonic-gate if (authctxt.method == NULL)
3550Sstevel@tonic-gate fatal("ssh_userauth2: internal error: cannot send userauth none request");
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate /* initial userauth request */
3580Sstevel@tonic-gate userauth_none(&authctxt);
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate dispatch_init(&input_userauth_error);
3610Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_SUCCESS, &input_userauth_success);
3620Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_FAILURE, &input_userauth_failure);
3630Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_BANNER, &input_userauth_banner);
3640Sstevel@tonic-gate dispatch_run(DISPATCH_BLOCK, &authctxt.success, &authctxt); /* loop until success */
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate if (authctxt.agent != NULL)
3670Sstevel@tonic-gate ssh_close_authentication_connection(authctxt.agent);
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate debug("Authentication succeeded (%s)", authctxt.method->name);
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate void
userauth(Authctxt * authctxt,char * authlist)3720Sstevel@tonic-gate userauth(Authctxt *authctxt, char *authlist)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate if (authctxt->method != NULL &&
3750Sstevel@tonic-gate authctxt->method->cleanup != NULL)
3760Sstevel@tonic-gate authctxt->method->cleanup(authctxt);
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate if (authlist == NULL) {
3790Sstevel@tonic-gate authlist = authctxt->authlist;
3800Sstevel@tonic-gate } else {
3810Sstevel@tonic-gate if (authctxt->authlist)
3820Sstevel@tonic-gate xfree(authctxt->authlist);
3830Sstevel@tonic-gate authctxt->authlist = authlist;
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate for (;;) {
3860Sstevel@tonic-gate Authmethod *method = authmethod_get(authlist);
3870Sstevel@tonic-gate if (method == NULL)
3880Sstevel@tonic-gate fatal("Permission denied (%s).", authlist);
3890Sstevel@tonic-gate authctxt->method = method;
3900Sstevel@tonic-gate if (method->userauth(authctxt) != 0) {
3910Sstevel@tonic-gate debug2("we sent a %s packet, wait for reply", method->name);
3920Sstevel@tonic-gate break;
3930Sstevel@tonic-gate } else {
3940Sstevel@tonic-gate debug2("we did not send a packet, disable method");
3950Sstevel@tonic-gate method->enabled = NULL;
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate void
input_userauth_error(int type,u_int32_t seq,void * ctxt)4010Sstevel@tonic-gate input_userauth_error(int type, u_int32_t seq, void *ctxt)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate fatal("input_userauth_error: bad message during authentication: "
4040Sstevel@tonic-gate "type %d", type);
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate void
input_userauth_banner(int type,u_int32_t seq,void * ctxt)4080Sstevel@tonic-gate input_userauth_banner(int type, u_int32_t seq, void *ctxt)
4090Sstevel@tonic-gate {
4100Sstevel@tonic-gate char *msg, *lang;
4112563Sjp161948
4120Sstevel@tonic-gate debug3("input_userauth_banner");
4139600SNobutomo.Nakano@Sun.COM msg = packet_get_utf8_string(NULL);
4140Sstevel@tonic-gate lang = packet_get_string(NULL);
4154958Sjp161948 /*
4164958Sjp161948 * Banner is a warning message according to RFC 4252. So, never print
4174958Sjp161948 * a banner in error log level or lower. If the log level is higher,
4184958Sjp161948 * use DisableBanner option to decide whether to display it or not.
4194958Sjp161948 */
4209600SNobutomo.Nakano@Sun.COM if (options.log_level > SYSLOG_LEVEL_ERROR) {
4214958Sjp161948 if (options.disable_banner == 0 ||
4224958Sjp161948 (options.disable_banner == SSH_NO_BANNER_IN_EXEC_MODE &&
4239600SNobutomo.Nakano@Sun.COM buffer_len(&command) == 0)) {
4249600SNobutomo.Nakano@Sun.COM msg = g11n_filter_string(msg);
4259600SNobutomo.Nakano@Sun.COM (void) fprintf(stderr, "%s", msg);
4269600SNobutomo.Nakano@Sun.COM }
4279600SNobutomo.Nakano@Sun.COM }
4280Sstevel@tonic-gate xfree(msg);
4290Sstevel@tonic-gate xfree(lang);
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate void
input_userauth_success(int type,u_int32_t seq,void * ctxt)4330Sstevel@tonic-gate input_userauth_success(int type, u_int32_t seq, void *ctxt)
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate Authctxt *authctxt = ctxt;
4360Sstevel@tonic-gate if (authctxt == NULL)
4370Sstevel@tonic-gate fatal("input_userauth_success: no authentication context");
4380Sstevel@tonic-gate if (authctxt->authlist)
4390Sstevel@tonic-gate xfree(authctxt->authlist);
4400Sstevel@tonic-gate if (authctxt->method != NULL &&
4410Sstevel@tonic-gate authctxt->method->cleanup != NULL)
4420Sstevel@tonic-gate authctxt->method->cleanup(authctxt);
4430Sstevel@tonic-gate clear_auth_state(authctxt);
4440Sstevel@tonic-gate authctxt->success = 1; /* break out */
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate void
input_userauth_failure(int type,u_int32_t seq,void * ctxt)4480Sstevel@tonic-gate input_userauth_failure(int type, u_int32_t seq, void *ctxt)
4490Sstevel@tonic-gate {
4500Sstevel@tonic-gate Authctxt *authctxt = ctxt;
4510Sstevel@tonic-gate char *authlist = NULL;
4520Sstevel@tonic-gate int partial;
4530Sstevel@tonic-gate
4540Sstevel@tonic-gate if (authctxt == NULL)
4550Sstevel@tonic-gate fatal("input_userauth_failure: no authentication context");
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate authlist = packet_get_string(NULL);
4580Sstevel@tonic-gate partial = packet_get_char();
4590Sstevel@tonic-gate packet_check_eom();
4600Sstevel@tonic-gate
4610Sstevel@tonic-gate if (partial != 0)
4620Sstevel@tonic-gate log("Authenticated with partial success.");
4630Sstevel@tonic-gate debug("Authentications that can continue: %s", authlist);
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate clear_auth_state(authctxt);
4660Sstevel@tonic-gate userauth(authctxt, authlist);
4670Sstevel@tonic-gate }
4680Sstevel@tonic-gate void
input_userauth_pk_ok(int type,u_int32_t seq,void * ctxt)4690Sstevel@tonic-gate input_userauth_pk_ok(int type, u_int32_t seq, void *ctxt)
4700Sstevel@tonic-gate {
4710Sstevel@tonic-gate Authctxt *authctxt = ctxt;
4720Sstevel@tonic-gate Key *key = NULL;
4730Sstevel@tonic-gate Buffer b;
4740Sstevel@tonic-gate int pktype, sent = 0;
4750Sstevel@tonic-gate u_int alen, blen;
4760Sstevel@tonic-gate char *pkalg, *fp;
4770Sstevel@tonic-gate u_char *pkblob;
4780Sstevel@tonic-gate
4790Sstevel@tonic-gate if (authctxt == NULL)
4800Sstevel@tonic-gate fatal("input_userauth_pk_ok: no authentication context");
4810Sstevel@tonic-gate if (datafellows & SSH_BUG_PKOK) {
4820Sstevel@tonic-gate /* this is similar to SSH_BUG_PKAUTH */
4830Sstevel@tonic-gate debug2("input_userauth_pk_ok: SSH_BUG_PKOK");
4840Sstevel@tonic-gate pkblob = packet_get_string(&blen);
4850Sstevel@tonic-gate buffer_init(&b);
4860Sstevel@tonic-gate buffer_append(&b, pkblob, blen);
4870Sstevel@tonic-gate pkalg = buffer_get_string(&b, &alen);
4880Sstevel@tonic-gate buffer_free(&b);
4890Sstevel@tonic-gate } else {
4900Sstevel@tonic-gate pkalg = packet_get_string(&alen);
4910Sstevel@tonic-gate pkblob = packet_get_string(&blen);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate packet_check_eom();
4940Sstevel@tonic-gate
4950Sstevel@tonic-gate debug("Server accepts key: pkalg %s blen %u lastkey %p hint %d",
4960Sstevel@tonic-gate pkalg, blen, authctxt->last_key, authctxt->last_key_hint);
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate do {
4990Sstevel@tonic-gate if (authctxt->last_key == NULL ||
5000Sstevel@tonic-gate authctxt->last_key_sign == NULL) {
5010Sstevel@tonic-gate debug("no last key or no sign cb");
5020Sstevel@tonic-gate break;
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate if ((pktype = key_type_from_name(pkalg)) == KEY_UNSPEC) {
5050Sstevel@tonic-gate debug("unknown pkalg %s", pkalg);
5060Sstevel@tonic-gate break;
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate if ((key = key_from_blob(pkblob, blen)) == NULL) {
5090Sstevel@tonic-gate debug("no key from blob. pkalg %s", pkalg);
5100Sstevel@tonic-gate break;
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate if (key->type != pktype) {
5130Sstevel@tonic-gate error("input_userauth_pk_ok: type mismatch "
5140Sstevel@tonic-gate "for decoded key (received %d, expected %d)",
5150Sstevel@tonic-gate key->type, pktype);
5160Sstevel@tonic-gate break;
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
5190Sstevel@tonic-gate debug2("input_userauth_pk_ok: fp %s", fp);
5200Sstevel@tonic-gate xfree(fp);
5210Sstevel@tonic-gate if (!key_equal(key, authctxt->last_key)) {
5220Sstevel@tonic-gate debug("key != last_key");
5230Sstevel@tonic-gate break;
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate sent = sign_and_send_pubkey(authctxt, key,
5260Sstevel@tonic-gate authctxt->last_key_sign);
5270Sstevel@tonic-gate } while (0);
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate if (key != NULL)
5300Sstevel@tonic-gate key_free(key);
5310Sstevel@tonic-gate xfree(pkalg);
5320Sstevel@tonic-gate xfree(pkblob);
5330Sstevel@tonic-gate
5340Sstevel@tonic-gate /* unregister */
5350Sstevel@tonic-gate clear_auth_state(authctxt);
5360Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_PK_OK, NULL);
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate /* try another method if we did not send a packet */
5390Sstevel@tonic-gate if (sent == 0)
5400Sstevel@tonic-gate userauth(authctxt, NULL);
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate #ifdef GSSAPI
5450Sstevel@tonic-gate int
userauth_gssapi(Authctxt * authctxt)5460Sstevel@tonic-gate userauth_gssapi(Authctxt *authctxt)
5470Sstevel@tonic-gate {
5480Sstevel@tonic-gate Gssctxt *gssctxt = NULL;
5490Sstevel@tonic-gate static int initialized = 0;
5500Sstevel@tonic-gate static int mech_idx = 0;
5510Sstevel@tonic-gate static gss_OID_set supported = GSS_C_NULL_OID_SET;
5520Sstevel@tonic-gate gss_OID mech = GSS_C_NULL_OID;
5530Sstevel@tonic-gate
5540Sstevel@tonic-gate /* Things work better if we send one mechanism at a time, rather
5550Sstevel@tonic-gate * than them all at once. This means that if we fail at some point
5560Sstevel@tonic-gate * in the middle of a negotiation, we can come back and try something
5570Sstevel@tonic-gate * different. */
5580Sstevel@tonic-gate
5590Sstevel@tonic-gate if (datafellows & SSH_OLD_GSSAPI) return 0;
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate /* Before we offer a mechanism, check that we can support it. Don't
5620Sstevel@tonic-gate * bother trying to get credentials - as the standard fallback will
5630Sstevel@tonic-gate * deal with that kind of failure.
5640Sstevel@tonic-gate */
5650Sstevel@tonic-gate
5660Sstevel@tonic-gate if (!initialized) {
5670Sstevel@tonic-gate initialized = 1;
5680Sstevel@tonic-gate ssh_gssapi_client_mechs(authctxt->host, &supported);
5690Sstevel@tonic-gate if (supported == GSS_C_NULL_OID_SET || supported->count == 0)
5700Sstevel@tonic-gate return (0);
5710Sstevel@tonic-gate } else if (supported != GSS_C_NULL_OID_SET) {
5720Sstevel@tonic-gate /* Try next mech, if any */
5730Sstevel@tonic-gate mech_idx++;
5740Sstevel@tonic-gate
5750Sstevel@tonic-gate if (mech_idx >= supported->count)
5760Sstevel@tonic-gate return (0);
5770Sstevel@tonic-gate } else {
5780Sstevel@tonic-gate return (0);
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate mech = &supported->elements[mech_idx];
5820Sstevel@tonic-gate
5830Sstevel@tonic-gate ssh_gssapi_build_ctx(&gssctxt, 1, mech);
5840Sstevel@tonic-gate authctxt->methoddata=(void *)gssctxt;
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_REQUEST);
5870Sstevel@tonic-gate packet_put_cstring(authctxt->server_user);
5880Sstevel@tonic-gate packet_put_cstring(authctxt->service);
5890Sstevel@tonic-gate packet_put_cstring(authctxt->method->name);
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate packet_put_int(1);
5920Sstevel@tonic-gate
5930Sstevel@tonic-gate /* The newest gsskeyex draft stipulates that OIDs should
5940Sstevel@tonic-gate * be DER encoded, so we need to add the object type and
5950Sstevel@tonic-gate * length information back on */
5960Sstevel@tonic-gate if (datafellows & SSH_BUG_GSSAPI_BER) {
5970Sstevel@tonic-gate packet_put_string(mech->elements, mech->length);
5980Sstevel@tonic-gate } else {
5990Sstevel@tonic-gate packet_put_int((mech->length)+2);
6000Sstevel@tonic-gate packet_put_char(0x06);
6010Sstevel@tonic-gate packet_put_char(mech->length);
6020Sstevel@tonic-gate packet_put_raw(mech->elements, mech->length);
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate
6050Sstevel@tonic-gate packet_send();
6060Sstevel@tonic-gate packet_write_wait();
6070Sstevel@tonic-gate
6080Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,&input_gssapi_response);
6090Sstevel@tonic-gate
6100Sstevel@tonic-gate return 1;
6110Sstevel@tonic-gate }
6120Sstevel@tonic-gate
6130Sstevel@tonic-gate void
input_gssapi_response(int type,u_int32_t plen,void * ctxt)6140Sstevel@tonic-gate input_gssapi_response(int type, u_int32_t plen, void *ctxt)
6150Sstevel@tonic-gate {
6160Sstevel@tonic-gate Authctxt *authctxt = ctxt;
6170Sstevel@tonic-gate Gssctxt *gssctxt;
6180Sstevel@tonic-gate OM_uint32 status,ms;
6190Sstevel@tonic-gate u_int oidlen;
6200Sstevel@tonic-gate char *oidv;
6210Sstevel@tonic-gate gss_buffer_desc send_tok;
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate if (authctxt == NULL)
6240Sstevel@tonic-gate fatal("input_gssapi_response: no authentication context");
6250Sstevel@tonic-gate gssctxt = authctxt->methoddata;
6260Sstevel@tonic-gate
6270Sstevel@tonic-gate /* Setup our OID */
6280Sstevel@tonic-gate oidv=packet_get_string(&oidlen);
6290Sstevel@tonic-gate
6300Sstevel@tonic-gate if (datafellows & SSH_BUG_GSSAPI_BER) {
6310Sstevel@tonic-gate if (!ssh_gssapi_check_mech_oid(gssctxt,oidv,oidlen)) {
6320Sstevel@tonic-gate gss_OID oid;
6330Sstevel@tonic-gate
6340Sstevel@tonic-gate oid = ssh_gssapi_make_oid(oidlen, oidv);
6350Sstevel@tonic-gate debug("Server returned different OID (%s) than expected (%s)",
6360Sstevel@tonic-gate ssh_gssapi_oid_to_str(oid),
6370Sstevel@tonic-gate ssh_gssapi_oid_to_str(gssctxt->desired_mech));
6380Sstevel@tonic-gate ssh_gssapi_release_oid(&oid);
6390Sstevel@tonic-gate clear_auth_state(authctxt);
6400Sstevel@tonic-gate userauth(authctxt,NULL);
6410Sstevel@tonic-gate return;
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate } else {
6440Sstevel@tonic-gate if(oidv[0]!=0x06 || oidv[1]!=oidlen-2) {
6450Sstevel@tonic-gate debug("Badly encoded mechanism OID received");
6460Sstevel@tonic-gate clear_auth_state(authctxt);
6470Sstevel@tonic-gate userauth(authctxt,NULL);
6480Sstevel@tonic-gate return;
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate if (!ssh_gssapi_check_mech_oid(gssctxt,oidv+2,oidlen-2)) {
6510Sstevel@tonic-gate gss_OID oid;
6520Sstevel@tonic-gate
6530Sstevel@tonic-gate oid = ssh_gssapi_make_oid(oidlen-2, oidv+2);
6540Sstevel@tonic-gate debug("Server returned different OID (%s) than expected (%s)",
6550Sstevel@tonic-gate ssh_gssapi_oid_to_str(oid),
6560Sstevel@tonic-gate ssh_gssapi_oid_to_str(gssctxt->desired_mech));
6570Sstevel@tonic-gate clear_auth_state(authctxt);
6580Sstevel@tonic-gate userauth(authctxt,NULL);
6590Sstevel@tonic-gate return;
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate
6630Sstevel@tonic-gate packet_check_eom();
6640Sstevel@tonic-gate
6650Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,&input_gssapi_token);
6660Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR,&input_gssapi_error);
6670Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK,&input_gssapi_errtok);
6680Sstevel@tonic-gate
6690Sstevel@tonic-gate status = ssh_gssapi_init_ctx(gssctxt, authctxt->host,
6700Sstevel@tonic-gate options.gss_deleg_creds,
6710Sstevel@tonic-gate GSS_C_NO_BUFFER, &send_tok);
6720Sstevel@tonic-gate if (GSS_ERROR(status)) {
6730Sstevel@tonic-gate if (send_tok.length>0) {
6740Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
6750Sstevel@tonic-gate packet_put_string(send_tok.value,send_tok.length);
6760Sstevel@tonic-gate packet_send();
6770Sstevel@tonic-gate packet_write_wait();
6780Sstevel@tonic-gate }
6790Sstevel@tonic-gate /* Start again with next method on list */
6800Sstevel@tonic-gate debug("Trying to start again");
6810Sstevel@tonic-gate clear_auth_state(authctxt);
6820Sstevel@tonic-gate userauth(authctxt,NULL);
6830Sstevel@tonic-gate return;
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate
6860Sstevel@tonic-gate /* We must have data to send */
6870Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
6880Sstevel@tonic-gate packet_put_string(send_tok.value,send_tok.length);
6890Sstevel@tonic-gate packet_send();
6900Sstevel@tonic-gate packet_write_wait();
6910Sstevel@tonic-gate gss_release_buffer(&ms, &send_tok);
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate
6940Sstevel@tonic-gate void
input_gssapi_token(int type,u_int32_t plen,void * ctxt)6950Sstevel@tonic-gate input_gssapi_token(int type, u_int32_t plen, void *ctxt)
6960Sstevel@tonic-gate {
6970Sstevel@tonic-gate Authctxt *authctxt = ctxt;
6980Sstevel@tonic-gate Gssctxt *gssctxt;
6990Sstevel@tonic-gate gss_buffer_desc send_tok, recv_tok, g_mic_data;
7000Sstevel@tonic-gate Buffer mic_data;
7010Sstevel@tonic-gate OM_uint32 status;
7020Sstevel@tonic-gate u_int slen;
7030Sstevel@tonic-gate
7040Sstevel@tonic-gate if (authctxt == NULL || authctxt->method == NULL)
7050Sstevel@tonic-gate fatal("input_gssapi_response: no authentication context");
7060Sstevel@tonic-gate gssctxt = authctxt->methoddata;
7070Sstevel@tonic-gate
7080Sstevel@tonic-gate recv_tok.value=packet_get_string(&slen);
7090Sstevel@tonic-gate recv_tok.length=slen; /* safe typecast */
7100Sstevel@tonic-gate
7110Sstevel@tonic-gate status=ssh_gssapi_init_ctx(gssctxt, authctxt->host,
7120Sstevel@tonic-gate options.gss_deleg_creds,
7130Sstevel@tonic-gate &recv_tok, &send_tok);
7140Sstevel@tonic-gate
7150Sstevel@tonic-gate packet_check_eom();
7160Sstevel@tonic-gate
7170Sstevel@tonic-gate if (GSS_ERROR(status)) {
7180Sstevel@tonic-gate if (send_tok.length>0) {
7190Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK);
7200Sstevel@tonic-gate packet_put_string(send_tok.value,send_tok.length);
7210Sstevel@tonic-gate packet_send();
7220Sstevel@tonic-gate packet_write_wait();
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate /* Start again with the next method in the list */
7250Sstevel@tonic-gate clear_auth_state(authctxt);
7260Sstevel@tonic-gate userauth(authctxt,NULL);
7270Sstevel@tonic-gate return;
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate
7300Sstevel@tonic-gate if (send_tok.length>0) {
7310Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
7320Sstevel@tonic-gate packet_put_string(send_tok.value,send_tok.length);
7330Sstevel@tonic-gate packet_send();
7340Sstevel@tonic-gate packet_write_wait();
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate
7370Sstevel@tonic-gate if (status != GSS_S_COMPLETE)
7380Sstevel@tonic-gate return;
7390Sstevel@tonic-gate
7400Sstevel@tonic-gate /* Make data buffer to MIC */
7410Sstevel@tonic-gate buffer_init(&mic_data);
7420Sstevel@tonic-gate buffer_put_string(&mic_data, session_id2, session_id2_len);
7430Sstevel@tonic-gate buffer_put_char(&mic_data, SSH2_MSG_USERAUTH_REQUEST);
7440Sstevel@tonic-gate buffer_put_cstring(&mic_data, authctxt->server_user);
7450Sstevel@tonic-gate buffer_put_cstring(&mic_data, authctxt->service);
7460Sstevel@tonic-gate buffer_put_cstring(&mic_data, authctxt->method->name);
7470Sstevel@tonic-gate
7480Sstevel@tonic-gate /* Make MIC */
7490Sstevel@tonic-gate g_mic_data.value = buffer_ptr(&mic_data);
7500Sstevel@tonic-gate g_mic_data.length = buffer_len(&mic_data);
7510Sstevel@tonic-gate
7520Sstevel@tonic-gate status = ssh_gssapi_get_mic(gssctxt, &g_mic_data, &send_tok);
7530Sstevel@tonic-gate buffer_clear(&mic_data);
7540Sstevel@tonic-gate
7550Sstevel@tonic-gate if (GSS_ERROR(status) || send_tok.length == 0) {
7560Sstevel@tonic-gate /*
7570Sstevel@tonic-gate * Oops, now what? There's no error token...
7580Sstevel@tonic-gate * Next userauth
7590Sstevel@tonic-gate */
7600Sstevel@tonic-gate debug("GSS_GetMIC() failed! - "
7610Sstevel@tonic-gate "Abandoning GSSAPI userauth");
7620Sstevel@tonic-gate clear_auth_state(authctxt);
7630Sstevel@tonic-gate userauth(authctxt,NULL);
7640Sstevel@tonic-gate return;
7650Sstevel@tonic-gate }
7660Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_GSSAPI_MIC);
7670Sstevel@tonic-gate packet_put_string(send_tok.value,send_tok.length);
7680Sstevel@tonic-gate packet_send();
7690Sstevel@tonic-gate packet_write_wait();
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate
7720Sstevel@tonic-gate void
input_gssapi_errtok(int type,u_int32_t plen,void * ctxt)7730Sstevel@tonic-gate input_gssapi_errtok(int type, u_int32_t plen, void *ctxt)
7740Sstevel@tonic-gate {
7750Sstevel@tonic-gate OM_uint32 min_status;
7760Sstevel@tonic-gate Authctxt *authctxt = ctxt;
7770Sstevel@tonic-gate Gssctxt *gssctxt;
7780Sstevel@tonic-gate gss_buffer_desc send_tok, recv_tok;
7790Sstevel@tonic-gate
7800Sstevel@tonic-gate if (authctxt == NULL)
7810Sstevel@tonic-gate fatal("input_gssapi_response: no authentication context");
7820Sstevel@tonic-gate gssctxt = authctxt->methoddata;
7830Sstevel@tonic-gate
7840Sstevel@tonic-gate recv_tok.value=packet_get_string(&recv_tok.length);
7850Sstevel@tonic-gate
7860Sstevel@tonic-gate /* Stick it into GSSAPI and see what it says */
7870Sstevel@tonic-gate (void) ssh_gssapi_init_ctx(gssctxt, authctxt->host,
7880Sstevel@tonic-gate options.gss_deleg_creds,
7890Sstevel@tonic-gate &recv_tok, &send_tok);
7900Sstevel@tonic-gate
7910Sstevel@tonic-gate xfree(recv_tok.value);
7920Sstevel@tonic-gate (void) gss_release_buffer(&min_status, &send_tok);
7930Sstevel@tonic-gate
7940Sstevel@tonic-gate debug("Server sent a GSS-API error token during GSS userauth -- %s",
7950Sstevel@tonic-gate ssh_gssapi_last_error(gssctxt, NULL, NULL));
7960Sstevel@tonic-gate
7970Sstevel@tonic-gate packet_check_eom();
7980Sstevel@tonic-gate
7990Sstevel@tonic-gate /* We can't send a packet to the server */
8000Sstevel@tonic-gate
8010Sstevel@tonic-gate /* The draft says that we should wait for the server to fail
8020Sstevel@tonic-gate * before starting the next authentication. So, we clear the
8030Sstevel@tonic-gate * state, but don't do anything else
8040Sstevel@tonic-gate */
8050Sstevel@tonic-gate clear_auth_state(authctxt);
8060Sstevel@tonic-gate return;
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate
8090Sstevel@tonic-gate void
input_gssapi_error(int type,u_int32_t plen,void * ctxt)8100Sstevel@tonic-gate input_gssapi_error(int type, u_int32_t plen, void *ctxt)
8110Sstevel@tonic-gate {
8120Sstevel@tonic-gate OM_uint32 maj,min;
8130Sstevel@tonic-gate char *msg;
8140Sstevel@tonic-gate char *lang;
8150Sstevel@tonic-gate
8160Sstevel@tonic-gate maj = packet_get_int();
8170Sstevel@tonic-gate min = packet_get_int();
8180Sstevel@tonic-gate msg = packet_get_string(NULL);
8190Sstevel@tonic-gate lang = packet_get_string(NULL);
8200Sstevel@tonic-gate
8210Sstevel@tonic-gate packet_check_eom();
8220Sstevel@tonic-gate
8230Sstevel@tonic-gate fprintf(stderr, "Server GSSAPI Error:\n%s (%d, %d)\n", msg, maj, min);
8240Sstevel@tonic-gate xfree(msg);
8250Sstevel@tonic-gate xfree(lang);
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate
8280Sstevel@tonic-gate int
userauth_gssapi_keyex(Authctxt * authctxt)8290Sstevel@tonic-gate userauth_gssapi_keyex(Authctxt *authctxt)
8300Sstevel@tonic-gate {
8310Sstevel@tonic-gate Gssctxt *gssctxt;
8320Sstevel@tonic-gate gss_buffer_desc send_tok;
8330Sstevel@tonic-gate OM_uint32 status;
8340Sstevel@tonic-gate static int attempt = 0;
8350Sstevel@tonic-gate
8360Sstevel@tonic-gate if (authctxt == NULL || authctxt->method == NULL)
8370Sstevel@tonic-gate fatal("input_gssapi_response: no authentication context");
8380Sstevel@tonic-gate
8390Sstevel@tonic-gate if (xxx_gssctxt == NULL || xxx_gssctxt->context == GSS_C_NO_CONTEXT)
8400Sstevel@tonic-gate return 0;
8410Sstevel@tonic-gate
8420Sstevel@tonic-gate if (strcmp(authctxt->method->name, "gssapi-keyex") == 0)
8430Sstevel@tonic-gate authctxt->methoddata = gssctxt = xxx_gssctxt;
8440Sstevel@tonic-gate
8450Sstevel@tonic-gate if (attempt++ >= 1)
8460Sstevel@tonic-gate return 0;
8470Sstevel@tonic-gate
8480Sstevel@tonic-gate if (strcmp(authctxt->method->name, "gssapi-keyex") == 0) {
8490Sstevel@tonic-gate gss_buffer_desc g_mic_data;
8500Sstevel@tonic-gate Buffer mic_data;
8510Sstevel@tonic-gate
8520Sstevel@tonic-gate debug2("Authenticating with GSS-API context from key exchange (w/ MIC)");
8530Sstevel@tonic-gate
8540Sstevel@tonic-gate /* Make data buffer to MIC */
8550Sstevel@tonic-gate buffer_init(&mic_data);
8560Sstevel@tonic-gate buffer_put_string(&mic_data, session_id2, session_id2_len);
8570Sstevel@tonic-gate buffer_put_char(&mic_data, SSH2_MSG_USERAUTH_REQUEST);
8580Sstevel@tonic-gate buffer_put_cstring(&mic_data, authctxt->server_user);
8590Sstevel@tonic-gate buffer_put_cstring(&mic_data, authctxt->service);
8600Sstevel@tonic-gate buffer_put_cstring(&mic_data, authctxt->method->name);
8610Sstevel@tonic-gate
8620Sstevel@tonic-gate /* Make MIC */
8630Sstevel@tonic-gate g_mic_data.value = buffer_ptr(&mic_data);
8640Sstevel@tonic-gate g_mic_data.length = buffer_len(&mic_data);
8650Sstevel@tonic-gate status = ssh_gssapi_get_mic(gssctxt, &g_mic_data, &send_tok);
8660Sstevel@tonic-gate buffer_clear(&mic_data);
8670Sstevel@tonic-gate
8680Sstevel@tonic-gate if (GSS_ERROR(status) || send_tok.length == 0) {
8690Sstevel@tonic-gate /*
8700Sstevel@tonic-gate * Oops, now what? There's no error token...
8710Sstevel@tonic-gate * Next userauth
8720Sstevel@tonic-gate */
8730Sstevel@tonic-gate debug("GSS_GetMIC() failed! - "
8740Sstevel@tonic-gate "Abandoning GSSAPI userauth");
8750Sstevel@tonic-gate clear_auth_state(authctxt);
8760Sstevel@tonic-gate userauth(authctxt,NULL);
8770Sstevel@tonic-gate return 0;
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_REQUEST);
8800Sstevel@tonic-gate packet_put_cstring(authctxt->server_user);
8810Sstevel@tonic-gate packet_put_cstring(authctxt->service);
8820Sstevel@tonic-gate packet_put_cstring(authctxt->method->name);
8830Sstevel@tonic-gate packet_put_string(send_tok.value,send_tok.length); /* MIC */
8840Sstevel@tonic-gate packet_send();
8850Sstevel@tonic-gate packet_write_wait();
8860Sstevel@tonic-gate (void) gss_release_buffer(&status, &send_tok);
8870Sstevel@tonic-gate } else if (strcmp(authctxt->method->name, "external-keyx") == 0) {
8880Sstevel@tonic-gate debug2("Authentication with deprecated \"external-keyx\""
8890Sstevel@tonic-gate " method not supported");
8900Sstevel@tonic-gate return 0;
8910Sstevel@tonic-gate }
8920Sstevel@tonic-gate return 1;
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate
8950Sstevel@tonic-gate static
8960Sstevel@tonic-gate void
userauth_gssapi_cleanup(Authctxt * authctxt)8970Sstevel@tonic-gate userauth_gssapi_cleanup(Authctxt *authctxt)
8980Sstevel@tonic-gate {
8990Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,NULL);
9000Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,NULL);
9010Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR,NULL);
9020Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERRTOK,NULL);
9030Sstevel@tonic-gate
9040Sstevel@tonic-gate if (authctxt == NULL ||
9050Sstevel@tonic-gate authctxt->method == NULL ||
9060Sstevel@tonic-gate authctxt->methoddata == NULL)
9070Sstevel@tonic-gate return;
9080Sstevel@tonic-gate
9090Sstevel@tonic-gate if (strncmp(authctxt->method->name, "gssapi", strlen("gssapi")) == 0) {
9100Sstevel@tonic-gate ssh_gssapi_delete_ctx((Gssctxt **)&authctxt->methoddata);
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate }
9130Sstevel@tonic-gate #endif /* GSSAPI */
9140Sstevel@tonic-gate
9150Sstevel@tonic-gate int
userauth_none(Authctxt * authctxt)9160Sstevel@tonic-gate userauth_none(Authctxt *authctxt)
9170Sstevel@tonic-gate {
9180Sstevel@tonic-gate /* initial userauth request */
9190Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_REQUEST);
9200Sstevel@tonic-gate packet_put_cstring(authctxt->server_user);
9210Sstevel@tonic-gate packet_put_cstring(authctxt->service);
9220Sstevel@tonic-gate packet_put_cstring(authctxt->method->name);
9230Sstevel@tonic-gate packet_send();
9240Sstevel@tonic-gate return 1;
9250Sstevel@tonic-gate
9260Sstevel@tonic-gate }
9270Sstevel@tonic-gate
9280Sstevel@tonic-gate int
userauth_passwd(Authctxt * authctxt)9290Sstevel@tonic-gate userauth_passwd(Authctxt *authctxt)
9300Sstevel@tonic-gate {
9310Sstevel@tonic-gate static int attempt = 0;
9320Sstevel@tonic-gate char prompt[150];
9330Sstevel@tonic-gate char *password;
9340Sstevel@tonic-gate
9350Sstevel@tonic-gate if (attempt++ >= options.number_of_password_prompts)
9360Sstevel@tonic-gate return 0;
9370Sstevel@tonic-gate
9380Sstevel@tonic-gate if (attempt != 1)
9390Sstevel@tonic-gate error("Permission denied, please try again.");
9400Sstevel@tonic-gate
9410Sstevel@tonic-gate snprintf(prompt, sizeof(prompt), gettext("%.30s@%.128s's password: "),
9420Sstevel@tonic-gate authctxt->server_user, authctxt->host);
9430Sstevel@tonic-gate password = read_passphrase(prompt, 0);
9440Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_REQUEST);
9450Sstevel@tonic-gate packet_put_cstring(authctxt->server_user);
9460Sstevel@tonic-gate packet_put_cstring(authctxt->service);
9470Sstevel@tonic-gate packet_put_cstring(authctxt->method->name);
9480Sstevel@tonic-gate packet_put_char(0);
9490Sstevel@tonic-gate packet_put_cstring(password);
9500Sstevel@tonic-gate memset(password, 0, strlen(password));
9510Sstevel@tonic-gate xfree(password);
9520Sstevel@tonic-gate packet_add_padding(64);
9530Sstevel@tonic-gate packet_send();
9540Sstevel@tonic-gate
9550Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
9560Sstevel@tonic-gate &input_userauth_passwd_changereq);
9570Sstevel@tonic-gate
9580Sstevel@tonic-gate return 1;
9590Sstevel@tonic-gate }
9600Sstevel@tonic-gate /*
9610Sstevel@tonic-gate * parse PASSWD_CHANGEREQ, prompt user and send SSH2_MSG_USERAUTH_REQUEST
9620Sstevel@tonic-gate */
9630Sstevel@tonic-gate void
input_userauth_passwd_changereq(int type,u_int32_t seqnr,void * ctxt)9640Sstevel@tonic-gate input_userauth_passwd_changereq(int type, u_int32_t seqnr, void *ctxt)
9650Sstevel@tonic-gate {
9660Sstevel@tonic-gate Authctxt *authctxt = ctxt;
9670Sstevel@tonic-gate char *info, *lang, *password = NULL, *retype = NULL;
9680Sstevel@tonic-gate char prompt[150];
9690Sstevel@tonic-gate
9700Sstevel@tonic-gate debug2("input_userauth_passwd_changereq");
9710Sstevel@tonic-gate
9720Sstevel@tonic-gate if (authctxt == NULL)
9730Sstevel@tonic-gate fatal("input_userauth_passwd_changereq: "
9740Sstevel@tonic-gate "no authentication context");
9750Sstevel@tonic-gate
9769600SNobutomo.Nakano@Sun.COM info = packet_get_utf8_string(NULL);
9779600SNobutomo.Nakano@Sun.COM if (strlen(info) != 0) {
9789600SNobutomo.Nakano@Sun.COM info = g11n_filter_string(info);
9799600SNobutomo.Nakano@Sun.COM log("%s", info);
9809600SNobutomo.Nakano@Sun.COM }
9819600SNobutomo.Nakano@Sun.COM xfree(info);
9820Sstevel@tonic-gate lang = packet_get_string(NULL);
9830Sstevel@tonic-gate xfree(lang);
9849600SNobutomo.Nakano@Sun.COM
9850Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_REQUEST);
9860Sstevel@tonic-gate packet_put_cstring(authctxt->server_user);
9870Sstevel@tonic-gate packet_put_cstring(authctxt->service);
9880Sstevel@tonic-gate packet_put_cstring(authctxt->method->name);
9890Sstevel@tonic-gate packet_put_char(1); /* additional info */
9900Sstevel@tonic-gate snprintf(prompt, sizeof(prompt),
9910Sstevel@tonic-gate gettext("Enter %.30s@%.128s's old password: "),
9920Sstevel@tonic-gate authctxt->server_user, authctxt->host);
9930Sstevel@tonic-gate password = read_passphrase(prompt, 0);
9940Sstevel@tonic-gate packet_put_cstring(password);
9950Sstevel@tonic-gate memset(password, 0, strlen(password));
9960Sstevel@tonic-gate xfree(password);
9970Sstevel@tonic-gate password = NULL;
9980Sstevel@tonic-gate while (password == NULL) {
9990Sstevel@tonic-gate snprintf(prompt, sizeof(prompt),
10000Sstevel@tonic-gate gettext("Enter %.30s@%.128s's new password: "),
10010Sstevel@tonic-gate authctxt->server_user, authctxt->host);
10020Sstevel@tonic-gate password = read_passphrase(prompt, RP_ALLOW_EOF);
10030Sstevel@tonic-gate if (password == NULL) {
10040Sstevel@tonic-gate /* bail out */
10050Sstevel@tonic-gate return;
10060Sstevel@tonic-gate }
10070Sstevel@tonic-gate snprintf(prompt, sizeof(prompt),
10080Sstevel@tonic-gate gettext("Retype %.30s@%.128s's new password: "),
10090Sstevel@tonic-gate authctxt->server_user, authctxt->host);
10100Sstevel@tonic-gate retype = read_passphrase(prompt, 0);
10110Sstevel@tonic-gate if (strcmp(password, retype) != 0) {
10120Sstevel@tonic-gate memset(password, 0, strlen(password));
10130Sstevel@tonic-gate xfree(password);
10140Sstevel@tonic-gate log("Mismatch; try again, EOF to quit.");
10150Sstevel@tonic-gate password = NULL;
10160Sstevel@tonic-gate }
10170Sstevel@tonic-gate memset(retype, 0, strlen(retype));
10180Sstevel@tonic-gate xfree(retype);
10190Sstevel@tonic-gate }
10200Sstevel@tonic-gate packet_put_cstring(password);
10210Sstevel@tonic-gate memset(password, 0, strlen(password));
10220Sstevel@tonic-gate xfree(password);
10230Sstevel@tonic-gate packet_add_padding(64);
10240Sstevel@tonic-gate packet_send();
10250Sstevel@tonic-gate
10260Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ,
10270Sstevel@tonic-gate &input_userauth_passwd_changereq);
10280Sstevel@tonic-gate }
10290Sstevel@tonic-gate
10300Sstevel@tonic-gate static void
clear_auth_state(Authctxt * authctxt)10310Sstevel@tonic-gate clear_auth_state(Authctxt *authctxt)
10320Sstevel@tonic-gate {
10330Sstevel@tonic-gate /* XXX clear authentication state */
10340Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ, NULL);
10350Sstevel@tonic-gate #ifdef GSSAPI
10360Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE,NULL);
10370Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,NULL);
10380Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_ERROR,NULL);
10390Sstevel@tonic-gate #endif /* GSSAPI */
10400Sstevel@tonic-gate
10410Sstevel@tonic-gate if (authctxt->last_key != NULL && authctxt->last_key_hint == -1) {
10420Sstevel@tonic-gate debug3("clear_auth_state: key_free %p", authctxt->last_key);
10430Sstevel@tonic-gate key_free(authctxt->last_key);
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate authctxt->last_key = NULL;
10460Sstevel@tonic-gate authctxt->last_key_hint = -2;
10470Sstevel@tonic-gate authctxt->last_key_sign = NULL;
10480Sstevel@tonic-gate }
10490Sstevel@tonic-gate
10500Sstevel@tonic-gate static int
sign_and_send_pubkey(Authctxt * authctxt,Key * k,sign_cb_fn * sign_callback)10510Sstevel@tonic-gate sign_and_send_pubkey(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback)
10520Sstevel@tonic-gate {
10530Sstevel@tonic-gate Buffer b;
10540Sstevel@tonic-gate u_char *blob, *signature;
10550Sstevel@tonic-gate u_int bloblen, slen;
10560Sstevel@tonic-gate int skip = 0;
10570Sstevel@tonic-gate int ret = -1;
10580Sstevel@tonic-gate int have_sig = 1;
10590Sstevel@tonic-gate
10600Sstevel@tonic-gate debug3("sign_and_send_pubkey");
10610Sstevel@tonic-gate
10620Sstevel@tonic-gate if (key_to_blob(k, &blob, &bloblen) == 0) {
10630Sstevel@tonic-gate /* we cannot handle this key */
10640Sstevel@tonic-gate debug3("sign_and_send_pubkey: cannot handle key");
10650Sstevel@tonic-gate return 0;
10660Sstevel@tonic-gate }
10670Sstevel@tonic-gate /* data to be signed */
10680Sstevel@tonic-gate buffer_init(&b);
10690Sstevel@tonic-gate if (datafellows & SSH_OLD_SESSIONID) {
10700Sstevel@tonic-gate buffer_append(&b, session_id2, session_id2_len);
10710Sstevel@tonic-gate skip = session_id2_len;
10720Sstevel@tonic-gate } else {
10730Sstevel@tonic-gate buffer_put_string(&b, session_id2, session_id2_len);
10740Sstevel@tonic-gate skip = buffer_len(&b);
10750Sstevel@tonic-gate }
10760Sstevel@tonic-gate buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
10770Sstevel@tonic-gate buffer_put_cstring(&b, authctxt->server_user);
10780Sstevel@tonic-gate buffer_put_cstring(&b,
10790Sstevel@tonic-gate datafellows & SSH_BUG_PKSERVICE ?
10800Sstevel@tonic-gate "ssh-userauth" :
10810Sstevel@tonic-gate authctxt->service);
10820Sstevel@tonic-gate if (datafellows & SSH_BUG_PKAUTH) {
10830Sstevel@tonic-gate buffer_put_char(&b, have_sig);
10840Sstevel@tonic-gate } else {
10850Sstevel@tonic-gate buffer_put_cstring(&b, authctxt->method->name);
10860Sstevel@tonic-gate buffer_put_char(&b, have_sig);
10870Sstevel@tonic-gate buffer_put_cstring(&b, key_ssh_name(k));
10880Sstevel@tonic-gate }
10890Sstevel@tonic-gate buffer_put_string(&b, blob, bloblen);
10900Sstevel@tonic-gate
10910Sstevel@tonic-gate /* generate signature */
10920Sstevel@tonic-gate ret = (*sign_callback)(authctxt, k, &signature, &slen,
10930Sstevel@tonic-gate buffer_ptr(&b), buffer_len(&b));
10940Sstevel@tonic-gate if (ret == -1) {
10950Sstevel@tonic-gate xfree(blob);
10960Sstevel@tonic-gate buffer_free(&b);
10970Sstevel@tonic-gate return 0;
10980Sstevel@tonic-gate }
10990Sstevel@tonic-gate #ifdef DEBUG_PK
11000Sstevel@tonic-gate buffer_dump(&b);
11010Sstevel@tonic-gate #endif
11020Sstevel@tonic-gate if (datafellows & SSH_BUG_PKSERVICE) {
11030Sstevel@tonic-gate buffer_clear(&b);
11040Sstevel@tonic-gate buffer_append(&b, session_id2, session_id2_len);
11050Sstevel@tonic-gate skip = session_id2_len;
11060Sstevel@tonic-gate buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
11070Sstevel@tonic-gate buffer_put_cstring(&b, authctxt->server_user);
11080Sstevel@tonic-gate buffer_put_cstring(&b, authctxt->service);
11090Sstevel@tonic-gate buffer_put_cstring(&b, authctxt->method->name);
11100Sstevel@tonic-gate buffer_put_char(&b, have_sig);
11110Sstevel@tonic-gate if (!(datafellows & SSH_BUG_PKAUTH))
11120Sstevel@tonic-gate buffer_put_cstring(&b, key_ssh_name(k));
11130Sstevel@tonic-gate buffer_put_string(&b, blob, bloblen);
11140Sstevel@tonic-gate }
11150Sstevel@tonic-gate xfree(blob);
11160Sstevel@tonic-gate
11170Sstevel@tonic-gate /* append signature */
11180Sstevel@tonic-gate buffer_put_string(&b, signature, slen);
11190Sstevel@tonic-gate xfree(signature);
11200Sstevel@tonic-gate
11210Sstevel@tonic-gate /* skip session id and packet type */
11220Sstevel@tonic-gate if (buffer_len(&b) < skip + 1)
11230Sstevel@tonic-gate fatal("userauth_pubkey: internal error");
11240Sstevel@tonic-gate buffer_consume(&b, skip + 1);
11250Sstevel@tonic-gate
11260Sstevel@tonic-gate /* put remaining data from buffer into packet */
11270Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_REQUEST);
11280Sstevel@tonic-gate packet_put_raw(buffer_ptr(&b), buffer_len(&b));
11290Sstevel@tonic-gate buffer_free(&b);
11300Sstevel@tonic-gate packet_send();
11310Sstevel@tonic-gate
11320Sstevel@tonic-gate return 1;
11330Sstevel@tonic-gate }
11340Sstevel@tonic-gate
11350Sstevel@tonic-gate static int
send_pubkey_test(Authctxt * authctxt,Key * k,sign_cb_fn * sign_callback,int hint)11360Sstevel@tonic-gate send_pubkey_test(Authctxt *authctxt, Key *k, sign_cb_fn *sign_callback,
11370Sstevel@tonic-gate int hint)
11380Sstevel@tonic-gate {
11390Sstevel@tonic-gate u_char *blob;
11400Sstevel@tonic-gate u_int bloblen, have_sig = 0;
11410Sstevel@tonic-gate
11420Sstevel@tonic-gate debug3("send_pubkey_test");
11430Sstevel@tonic-gate
11440Sstevel@tonic-gate if (key_to_blob(k, &blob, &bloblen) == 0) {
11450Sstevel@tonic-gate /* we cannot handle this key */
11460Sstevel@tonic-gate debug3("send_pubkey_test: cannot handle key");
11470Sstevel@tonic-gate return 0;
11480Sstevel@tonic-gate }
11490Sstevel@tonic-gate /* register callback for USERAUTH_PK_OK message */
11500Sstevel@tonic-gate authctxt->last_key_sign = sign_callback;
11510Sstevel@tonic-gate authctxt->last_key_hint = hint;
11520Sstevel@tonic-gate authctxt->last_key = k;
11530Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_PK_OK, &input_userauth_pk_ok);
11540Sstevel@tonic-gate
11550Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_REQUEST);
11560Sstevel@tonic-gate packet_put_cstring(authctxt->server_user);
11570Sstevel@tonic-gate packet_put_cstring(authctxt->service);
11580Sstevel@tonic-gate packet_put_cstring(authctxt->method->name);
11590Sstevel@tonic-gate packet_put_char(have_sig);
11600Sstevel@tonic-gate if (!(datafellows & SSH_BUG_PKAUTH))
11610Sstevel@tonic-gate packet_put_cstring(key_ssh_name(k));
11620Sstevel@tonic-gate packet_put_string(blob, bloblen);
11630Sstevel@tonic-gate xfree(blob);
11640Sstevel@tonic-gate packet_send();
11650Sstevel@tonic-gate return 1;
11660Sstevel@tonic-gate }
11670Sstevel@tonic-gate
11680Sstevel@tonic-gate static Key *
load_identity_file(char * filename)11690Sstevel@tonic-gate load_identity_file(char *filename)
11700Sstevel@tonic-gate {
11710Sstevel@tonic-gate Key *private;
11720Sstevel@tonic-gate char prompt[300], *passphrase;
11730Sstevel@tonic-gate int quit, i;
11740Sstevel@tonic-gate struct stat st;
11750Sstevel@tonic-gate
11760Sstevel@tonic-gate if (stat(filename, &st) < 0) {
11770Sstevel@tonic-gate debug3("no such identity: %s", filename);
11780Sstevel@tonic-gate return NULL;
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate private = key_load_private_type(KEY_UNSPEC, filename, "", NULL);
11810Sstevel@tonic-gate if (private == NULL) {
11820Sstevel@tonic-gate if (options.batch_mode)
11830Sstevel@tonic-gate return NULL;
11840Sstevel@tonic-gate snprintf(prompt, sizeof prompt,
11850Sstevel@tonic-gate gettext("Enter passphrase for key '%.100s': "), filename);
11860Sstevel@tonic-gate for (i = 0; i < options.number_of_password_prompts; i++) {
11870Sstevel@tonic-gate passphrase = read_passphrase(prompt, 0);
11880Sstevel@tonic-gate if (strcmp(passphrase, "") != 0) {
11890Sstevel@tonic-gate private = key_load_private_type(KEY_UNSPEC, filename,
11900Sstevel@tonic-gate passphrase, NULL);
11910Sstevel@tonic-gate quit = 0;
11920Sstevel@tonic-gate } else {
11930Sstevel@tonic-gate debug2("no passphrase given, try next key");
11940Sstevel@tonic-gate quit = 1;
11950Sstevel@tonic-gate }
11960Sstevel@tonic-gate memset(passphrase, 0, strlen(passphrase));
11970Sstevel@tonic-gate xfree(passphrase);
11980Sstevel@tonic-gate if (private != NULL || quit)
11990Sstevel@tonic-gate break;
12000Sstevel@tonic-gate debug2("bad passphrase given, try again...");
12010Sstevel@tonic-gate }
12020Sstevel@tonic-gate }
12030Sstevel@tonic-gate return private;
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate
12060Sstevel@tonic-gate static int
identity_sign_cb(Authctxt * authctxt,Key * key,u_char ** sigp,u_int * lenp,u_char * data,u_int datalen)12070Sstevel@tonic-gate identity_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
12080Sstevel@tonic-gate u_char *data, u_int datalen)
12090Sstevel@tonic-gate {
12100Sstevel@tonic-gate Key *private;
12110Sstevel@tonic-gate int idx, ret;
12120Sstevel@tonic-gate
12130Sstevel@tonic-gate idx = authctxt->last_key_hint;
12140Sstevel@tonic-gate if (idx < 0)
12150Sstevel@tonic-gate return -1;
12160Sstevel@tonic-gate
12170Sstevel@tonic-gate /* private key is stored in external hardware */
12180Sstevel@tonic-gate if (options.identity_keys[idx]->flags & KEY_FLAG_EXT)
12190Sstevel@tonic-gate return key_sign(options.identity_keys[idx], sigp, lenp, data, datalen);
12200Sstevel@tonic-gate
12210Sstevel@tonic-gate private = load_identity_file(options.identity_files[idx]);
12220Sstevel@tonic-gate if (private == NULL)
12230Sstevel@tonic-gate return -1;
12240Sstevel@tonic-gate ret = key_sign(private, sigp, lenp, data, datalen);
12250Sstevel@tonic-gate key_free(private);
12260Sstevel@tonic-gate return ret;
12270Sstevel@tonic-gate }
12280Sstevel@tonic-gate
12290Sstevel@tonic-gate static int
agent_sign_cb(Authctxt * authctxt,Key * key,u_char ** sigp,u_int * lenp,u_char * data,u_int datalen)12300Sstevel@tonic-gate agent_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
12310Sstevel@tonic-gate u_char *data, u_int datalen)
12320Sstevel@tonic-gate {
12330Sstevel@tonic-gate return ssh_agent_sign(authctxt->agent, key, sigp, lenp, data, datalen);
12340Sstevel@tonic-gate }
12350Sstevel@tonic-gate
12360Sstevel@tonic-gate static int
key_sign_cb(Authctxt * authctxt,Key * key,u_char ** sigp,u_int * lenp,u_char * data,u_int datalen)12370Sstevel@tonic-gate key_sign_cb(Authctxt *authctxt, Key *key, u_char **sigp, u_int *lenp,
12380Sstevel@tonic-gate u_char *data, u_int datalen)
12390Sstevel@tonic-gate {
12400Sstevel@tonic-gate return key_sign(key, sigp, lenp, data, datalen);
12410Sstevel@tonic-gate }
12420Sstevel@tonic-gate
12430Sstevel@tonic-gate static int
userauth_pubkey_agent(Authctxt * authctxt)12440Sstevel@tonic-gate userauth_pubkey_agent(Authctxt *authctxt)
12450Sstevel@tonic-gate {
12460Sstevel@tonic-gate static int called = 0;
12470Sstevel@tonic-gate int ret = 0;
12480Sstevel@tonic-gate char *comment;
12490Sstevel@tonic-gate Key *k;
12500Sstevel@tonic-gate
12510Sstevel@tonic-gate if (called == 0) {
12520Sstevel@tonic-gate if (ssh_get_num_identities(authctxt->agent, 2) == 0)
12530Sstevel@tonic-gate debug2("userauth_pubkey_agent: no keys at all");
12540Sstevel@tonic-gate called = 1;
12550Sstevel@tonic-gate }
12560Sstevel@tonic-gate k = ssh_get_next_identity(authctxt->agent, &comment, 2);
12570Sstevel@tonic-gate if (k == NULL) {
12580Sstevel@tonic-gate debug2("userauth_pubkey_agent: no more keys");
12590Sstevel@tonic-gate } else {
12600Sstevel@tonic-gate debug("Offering agent key: %s", comment);
12610Sstevel@tonic-gate xfree(comment);
12620Sstevel@tonic-gate ret = send_pubkey_test(authctxt, k, agent_sign_cb, -1);
12630Sstevel@tonic-gate if (ret == 0)
12640Sstevel@tonic-gate key_free(k);
12650Sstevel@tonic-gate }
12660Sstevel@tonic-gate if (ret == 0)
12670Sstevel@tonic-gate debug2("userauth_pubkey_agent: no message sent");
12680Sstevel@tonic-gate return ret;
12690Sstevel@tonic-gate }
12700Sstevel@tonic-gate
12710Sstevel@tonic-gate int
userauth_pubkey(Authctxt * authctxt)12720Sstevel@tonic-gate userauth_pubkey(Authctxt *authctxt)
12730Sstevel@tonic-gate {
12740Sstevel@tonic-gate static int idx = 0;
12750Sstevel@tonic-gate int sent = 0;
12760Sstevel@tonic-gate Key *key;
12770Sstevel@tonic-gate char *filename;
12780Sstevel@tonic-gate
12790Sstevel@tonic-gate if (authctxt->agent != NULL) {
12800Sstevel@tonic-gate do {
12810Sstevel@tonic-gate sent = userauth_pubkey_agent(authctxt);
12820Sstevel@tonic-gate } while (!sent && authctxt->agent->howmany > 0);
12830Sstevel@tonic-gate }
12840Sstevel@tonic-gate while (!sent && idx < options.num_identity_files) {
12850Sstevel@tonic-gate key = options.identity_keys[idx];
12860Sstevel@tonic-gate filename = options.identity_files[idx];
12870Sstevel@tonic-gate if (key == NULL) {
12880Sstevel@tonic-gate debug("Trying private key: %s", filename);
12890Sstevel@tonic-gate key = load_identity_file(filename);
12900Sstevel@tonic-gate if (key != NULL) {
12910Sstevel@tonic-gate sent = sign_and_send_pubkey(authctxt, key,
12920Sstevel@tonic-gate key_sign_cb);
12930Sstevel@tonic-gate key_free(key);
12940Sstevel@tonic-gate }
12950Sstevel@tonic-gate } else if (key->type != KEY_RSA1) {
12960Sstevel@tonic-gate debug("Trying public key: %s", filename);
12970Sstevel@tonic-gate sent = send_pubkey_test(authctxt, key,
12980Sstevel@tonic-gate identity_sign_cb, idx);
12990Sstevel@tonic-gate }
13000Sstevel@tonic-gate idx++;
13010Sstevel@tonic-gate }
13020Sstevel@tonic-gate return sent;
13030Sstevel@tonic-gate }
13040Sstevel@tonic-gate
13050Sstevel@tonic-gate /*
13060Sstevel@tonic-gate * Send userauth request message specifying keyboard-interactive method.
13070Sstevel@tonic-gate */
13080Sstevel@tonic-gate int
userauth_kbdint(Authctxt * authctxt)13090Sstevel@tonic-gate userauth_kbdint(Authctxt *authctxt)
13100Sstevel@tonic-gate {
13110Sstevel@tonic-gate static int attempt = 0;
13120Sstevel@tonic-gate
13130Sstevel@tonic-gate if (attempt++ >= options.number_of_password_prompts)
13140Sstevel@tonic-gate return 0;
13150Sstevel@tonic-gate /* disable if no SSH2_MSG_USERAUTH_INFO_REQUEST has been seen */
13160Sstevel@tonic-gate if (attempt > 1 && !authctxt->info_req_seen) {
13170Sstevel@tonic-gate debug3("userauth_kbdint: disable: no info_req_seen");
13180Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, NULL);
13190Sstevel@tonic-gate return 0;
13200Sstevel@tonic-gate }
13210Sstevel@tonic-gate
13220Sstevel@tonic-gate debug2("userauth_kbdint");
13230Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_REQUEST);
13240Sstevel@tonic-gate packet_put_cstring(authctxt->server_user);
13250Sstevel@tonic-gate packet_put_cstring(authctxt->service);
13260Sstevel@tonic-gate packet_put_cstring(authctxt->method->name);
13270Sstevel@tonic-gate packet_put_cstring(""); /* lang */
13280Sstevel@tonic-gate packet_put_cstring(options.kbd_interactive_devices ?
13290Sstevel@tonic-gate options.kbd_interactive_devices : "");
13300Sstevel@tonic-gate packet_send();
13310Sstevel@tonic-gate
13320Sstevel@tonic-gate dispatch_set(SSH2_MSG_USERAUTH_INFO_REQUEST, &input_userauth_info_req);
13330Sstevel@tonic-gate return 1;
13340Sstevel@tonic-gate }
13350Sstevel@tonic-gate
13360Sstevel@tonic-gate /*
13370Sstevel@tonic-gate * parse INFO_REQUEST, prompt user and send INFO_RESPONSE
13380Sstevel@tonic-gate */
13390Sstevel@tonic-gate void
input_userauth_info_req(int type,u_int32_t seq,void * ctxt)13400Sstevel@tonic-gate input_userauth_info_req(int type, u_int32_t seq, void *ctxt)
13410Sstevel@tonic-gate {
13420Sstevel@tonic-gate Authctxt *authctxt = ctxt;
13430Sstevel@tonic-gate char *name, *inst, *lang, *prompt, *response;
13440Sstevel@tonic-gate u_int num_prompts, i;
13450Sstevel@tonic-gate int echo = 0;
13460Sstevel@tonic-gate
13470Sstevel@tonic-gate debug2("input_userauth_info_req");
13480Sstevel@tonic-gate
13490Sstevel@tonic-gate if (authctxt == NULL)
13500Sstevel@tonic-gate fatal("input_userauth_info_req: no authentication context");
13510Sstevel@tonic-gate
13520Sstevel@tonic-gate authctxt->info_req_seen = 1;
13530Sstevel@tonic-gate
13549600SNobutomo.Nakano@Sun.COM /*
13559600SNobutomo.Nakano@Sun.COM * We assume that ASCII is used for user name although it is defined
13569600SNobutomo.Nakano@Sun.COM * by the protocol to use UTF-8 encoding. Therefore, we don't perform
13579600SNobutomo.Nakano@Sun.COM * code conversion from UTF-8 to native codeset for the user name
13589600SNobutomo.Nakano@Sun.COM * string.
13599600SNobutomo.Nakano@Sun.COM */
13600Sstevel@tonic-gate name = packet_get_string(NULL);
13619600SNobutomo.Nakano@Sun.COM inst = packet_get_utf8_string(NULL);
13620Sstevel@tonic-gate lang = packet_get_string(NULL);
13639600SNobutomo.Nakano@Sun.COM if (strlen(name) != 0) {
13649600SNobutomo.Nakano@Sun.COM name = g11n_filter_string(name);
13650Sstevel@tonic-gate log("%s", name);
13669600SNobutomo.Nakano@Sun.COM }
13679600SNobutomo.Nakano@Sun.COM if (strlen(inst) != 0) {
13689600SNobutomo.Nakano@Sun.COM inst = g11n_filter_string(inst);
13690Sstevel@tonic-gate log("%s", inst);
13709600SNobutomo.Nakano@Sun.COM }
13710Sstevel@tonic-gate xfree(name);
13720Sstevel@tonic-gate xfree(inst);
13730Sstevel@tonic-gate xfree(lang);
13740Sstevel@tonic-gate
13750Sstevel@tonic-gate num_prompts = packet_get_int();
13760Sstevel@tonic-gate /*
13770Sstevel@tonic-gate * Begin to build info response packet based on prompts requested.
13780Sstevel@tonic-gate * We commit to providing the correct number of responses, so if
13790Sstevel@tonic-gate * further on we run into a problem that prevents this, we have to
13800Sstevel@tonic-gate * be sure and clean this up and send a correct error response.
13810Sstevel@tonic-gate */
13820Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_INFO_RESPONSE);
13830Sstevel@tonic-gate packet_put_int(num_prompts);
13840Sstevel@tonic-gate
13850Sstevel@tonic-gate debug2("input_userauth_info_req: num_prompts %d", num_prompts);
13860Sstevel@tonic-gate for (i = 0; i < num_prompts; i++) {
13879600SNobutomo.Nakano@Sun.COM prompt = packet_get_utf8_string(NULL);
13880Sstevel@tonic-gate echo = packet_get_char();
13890Sstevel@tonic-gate
13909600SNobutomo.Nakano@Sun.COM prompt = g11n_filter_string(prompt);
13910Sstevel@tonic-gate response = read_passphrase(prompt, echo ? RP_ECHO : 0);
13920Sstevel@tonic-gate
13939600SNobutomo.Nakano@Sun.COM /*
13949600SNobutomo.Nakano@Sun.COM * We assume that ASCII is used for password as well. We
13959600SNobutomo.Nakano@Sun.COM * don't perform code conversion from native codeset to
13969600SNobutomo.Nakano@Sun.COM * UTF-8 for the password string.
13979600SNobutomo.Nakano@Sun.COM */
13980Sstevel@tonic-gate packet_put_cstring(response);
13990Sstevel@tonic-gate memset(response, 0, strlen(response));
14000Sstevel@tonic-gate xfree(response);
14010Sstevel@tonic-gate xfree(prompt);
14020Sstevel@tonic-gate }
14030Sstevel@tonic-gate packet_check_eom(); /* done with parsing incoming message. */
14040Sstevel@tonic-gate
14050Sstevel@tonic-gate packet_add_padding(64);
14060Sstevel@tonic-gate packet_send();
14070Sstevel@tonic-gate }
14080Sstevel@tonic-gate
14090Sstevel@tonic-gate static int
ssh_keysign(Key * key,u_char ** sigp,u_int * lenp,u_char * data,u_int datalen)14100Sstevel@tonic-gate ssh_keysign(Key *key, u_char **sigp, u_int *lenp,
14110Sstevel@tonic-gate u_char *data, u_int datalen)
14120Sstevel@tonic-gate {
14130Sstevel@tonic-gate Buffer b;
14140Sstevel@tonic-gate struct stat st;
14150Sstevel@tonic-gate pid_t pid;
14160Sstevel@tonic-gate int to[2], from[2], status, version = 2;
14170Sstevel@tonic-gate
14180Sstevel@tonic-gate debug2("ssh_keysign called");
14190Sstevel@tonic-gate
14200Sstevel@tonic-gate if (stat(_PATH_SSH_KEY_SIGN, &st) < 0) {
14210Sstevel@tonic-gate error("ssh_keysign: no installed: %s", strerror(errno));
14220Sstevel@tonic-gate return -1;
14230Sstevel@tonic-gate }
14240Sstevel@tonic-gate if (fflush(stdout) != 0)
14250Sstevel@tonic-gate error("ssh_keysign: fflush: %s", strerror(errno));
14260Sstevel@tonic-gate if (pipe(to) < 0) {
14270Sstevel@tonic-gate error("ssh_keysign: pipe: %s", strerror(errno));
14280Sstevel@tonic-gate return -1;
14290Sstevel@tonic-gate }
14300Sstevel@tonic-gate if (pipe(from) < 0) {
14310Sstevel@tonic-gate error("ssh_keysign: pipe: %s", strerror(errno));
14320Sstevel@tonic-gate return -1;
14330Sstevel@tonic-gate }
14340Sstevel@tonic-gate if ((pid = fork()) < 0) {
14350Sstevel@tonic-gate error("ssh_keysign: fork: %s", strerror(errno));
14360Sstevel@tonic-gate return -1;
14370Sstevel@tonic-gate }
14380Sstevel@tonic-gate if (pid == 0) {
14390Sstevel@tonic-gate seteuid(getuid());
14400Sstevel@tonic-gate setuid(getuid());
14410Sstevel@tonic-gate close(from[0]);
14420Sstevel@tonic-gate if (dup2(from[1], STDOUT_FILENO) < 0)
14430Sstevel@tonic-gate fatal("ssh_keysign: dup2: %s", strerror(errno));
14440Sstevel@tonic-gate close(to[1]);
14450Sstevel@tonic-gate if (dup2(to[0], STDIN_FILENO) < 0)
14460Sstevel@tonic-gate fatal("ssh_keysign: dup2: %s", strerror(errno));
14470Sstevel@tonic-gate close(from[1]);
14480Sstevel@tonic-gate close(to[0]);
14490Sstevel@tonic-gate execl(_PATH_SSH_KEY_SIGN, _PATH_SSH_KEY_SIGN, (char *) 0);
14500Sstevel@tonic-gate fatal("ssh_keysign: exec(%s): %s", _PATH_SSH_KEY_SIGN,
14510Sstevel@tonic-gate strerror(errno));
14520Sstevel@tonic-gate }
14530Sstevel@tonic-gate close(from[1]);
14540Sstevel@tonic-gate close(to[0]);
14550Sstevel@tonic-gate
14560Sstevel@tonic-gate buffer_init(&b);
14570Sstevel@tonic-gate buffer_put_int(&b, packet_get_connection_in()); /* send # of socket */
14580Sstevel@tonic-gate buffer_put_string(&b, data, datalen);
14590Sstevel@tonic-gate ssh_msg_send(to[1], version, &b);
14600Sstevel@tonic-gate
14610Sstevel@tonic-gate if (ssh_msg_recv(from[0], &b) < 0) {
14620Sstevel@tonic-gate error("ssh_keysign: no reply");
14630Sstevel@tonic-gate buffer_clear(&b);
14640Sstevel@tonic-gate return -1;
14650Sstevel@tonic-gate }
14660Sstevel@tonic-gate close(from[0]);
14670Sstevel@tonic-gate close(to[1]);
14680Sstevel@tonic-gate
14690Sstevel@tonic-gate while (waitpid(pid, &status, 0) < 0)
14700Sstevel@tonic-gate if (errno != EINTR)
14710Sstevel@tonic-gate break;
14720Sstevel@tonic-gate
14730Sstevel@tonic-gate if (buffer_get_char(&b) != version) {
14740Sstevel@tonic-gate error("ssh_keysign: bad version");
14750Sstevel@tonic-gate buffer_clear(&b);
14760Sstevel@tonic-gate return -1;
14770Sstevel@tonic-gate }
14780Sstevel@tonic-gate *sigp = buffer_get_string(&b, lenp);
14790Sstevel@tonic-gate buffer_clear(&b);
14800Sstevel@tonic-gate
14810Sstevel@tonic-gate return 0;
14820Sstevel@tonic-gate }
14830Sstevel@tonic-gate
14840Sstevel@tonic-gate int
userauth_hostbased(Authctxt * authctxt)14850Sstevel@tonic-gate userauth_hostbased(Authctxt *authctxt)
14860Sstevel@tonic-gate {
14870Sstevel@tonic-gate Key *private = NULL;
14880Sstevel@tonic-gate Sensitive *sensitive = authctxt->sensitive;
14890Sstevel@tonic-gate Buffer b;
14900Sstevel@tonic-gate u_char *signature, *blob;
14910Sstevel@tonic-gate char *chost, *pkalg, *p;
14920Sstevel@tonic-gate const char *service;
14930Sstevel@tonic-gate u_int blen, slen;
14940Sstevel@tonic-gate int ok, i, len, found = 0;
14950Sstevel@tonic-gate static int last_hostkey = -1;
14960Sstevel@tonic-gate
14970Sstevel@tonic-gate /* check for a useful key */
14980Sstevel@tonic-gate for (i = 0; i < sensitive->nkeys; i++) {
14990Sstevel@tonic-gate private = sensitive->keys[i];
15000Sstevel@tonic-gate if (private && private->type != KEY_RSA1 && i > last_hostkey) {
15010Sstevel@tonic-gate found = 1;
15020Sstevel@tonic-gate last_hostkey = i;
15030Sstevel@tonic-gate /* we take and free the key */
15040Sstevel@tonic-gate sensitive->keys[i] = NULL;
15050Sstevel@tonic-gate break;
15060Sstevel@tonic-gate }
15070Sstevel@tonic-gate }
15080Sstevel@tonic-gate if (!found) {
15090Sstevel@tonic-gate debug("No more client hostkeys for hostbased authentication");
15100Sstevel@tonic-gate return 0;
15110Sstevel@tonic-gate }
15120Sstevel@tonic-gate if (key_to_blob(private, &blob, &blen) == 0) {
15130Sstevel@tonic-gate key_free(private);
15140Sstevel@tonic-gate return 0;
15150Sstevel@tonic-gate }
15160Sstevel@tonic-gate /* figure out a name for the client host */
15170Sstevel@tonic-gate p = get_local_name(packet_get_connection_in());
15180Sstevel@tonic-gate if (p == NULL) {
15190Sstevel@tonic-gate error("userauth_hostbased: cannot get local ipaddr/name");
15200Sstevel@tonic-gate key_free(private);
15210Sstevel@tonic-gate return 0;
15220Sstevel@tonic-gate }
15230Sstevel@tonic-gate
15240Sstevel@tonic-gate service = datafellows & SSH_BUG_HBSERVICE ? "ssh-userauth" :
15250Sstevel@tonic-gate authctxt->service;
15260Sstevel@tonic-gate pkalg = xstrdup(key_ssh_name(private));
15270Sstevel@tonic-gate
15280Sstevel@tonic-gate len = strlen(p) + 2;
15290Sstevel@tonic-gate chost = xmalloc(len);
15300Sstevel@tonic-gate strlcpy(chost, p, len);
15310Sstevel@tonic-gate strlcat(chost, ".", len);
15320Sstevel@tonic-gate xfree(p);
15330Sstevel@tonic-gate debug2("userauth_hostbased: chost %s, pkalg %s", chost, pkalg);
15340Sstevel@tonic-gate
15350Sstevel@tonic-gate buffer_init(&b);
15360Sstevel@tonic-gate /* construct data */
15370Sstevel@tonic-gate buffer_put_string(&b, session_id2, session_id2_len);
15380Sstevel@tonic-gate buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
15390Sstevel@tonic-gate buffer_put_cstring(&b, authctxt->server_user);
15400Sstevel@tonic-gate buffer_put_cstring(&b, service);
15410Sstevel@tonic-gate buffer_put_cstring(&b, authctxt->method->name);
15420Sstevel@tonic-gate buffer_put_cstring(&b, pkalg);
15430Sstevel@tonic-gate buffer_put_string(&b, blob, blen);
15440Sstevel@tonic-gate buffer_put_cstring(&b, chost);
15450Sstevel@tonic-gate buffer_put_cstring(&b, authctxt->local_user);
15460Sstevel@tonic-gate #ifdef DEBUG_PK
15470Sstevel@tonic-gate buffer_dump(&b);
15480Sstevel@tonic-gate #endif
15490Sstevel@tonic-gate if (sensitive->external_keysign)
15500Sstevel@tonic-gate ok = ssh_keysign(private, &signature, &slen,
15510Sstevel@tonic-gate buffer_ptr(&b), buffer_len(&b));
15520Sstevel@tonic-gate else
15530Sstevel@tonic-gate ok = key_sign(private, &signature, &slen,
15540Sstevel@tonic-gate buffer_ptr(&b), buffer_len(&b));
15550Sstevel@tonic-gate key_free(private);
15560Sstevel@tonic-gate buffer_free(&b);
15570Sstevel@tonic-gate if (ok != 0) {
15580Sstevel@tonic-gate error("key_sign failed");
15590Sstevel@tonic-gate xfree(chost);
15600Sstevel@tonic-gate xfree(pkalg);
15610Sstevel@tonic-gate return 0;
15620Sstevel@tonic-gate }
15630Sstevel@tonic-gate packet_start(SSH2_MSG_USERAUTH_REQUEST);
15640Sstevel@tonic-gate packet_put_cstring(authctxt->server_user);
15650Sstevel@tonic-gate packet_put_cstring(authctxt->service);
15660Sstevel@tonic-gate packet_put_cstring(authctxt->method->name);
15670Sstevel@tonic-gate packet_put_cstring(pkalg);
15680Sstevel@tonic-gate packet_put_string(blob, blen);
15690Sstevel@tonic-gate packet_put_cstring(chost);
15700Sstevel@tonic-gate packet_put_cstring(authctxt->local_user);
15710Sstevel@tonic-gate packet_put_string(signature, slen);
15720Sstevel@tonic-gate memset(signature, 's', slen);
15730Sstevel@tonic-gate xfree(signature);
15740Sstevel@tonic-gate xfree(chost);
15750Sstevel@tonic-gate xfree(pkalg);
15760Sstevel@tonic-gate
15770Sstevel@tonic-gate packet_send();
15780Sstevel@tonic-gate return 1;
15790Sstevel@tonic-gate }
15800Sstevel@tonic-gate
15810Sstevel@tonic-gate /* find auth method */
15820Sstevel@tonic-gate
15830Sstevel@tonic-gate /*
15840Sstevel@tonic-gate * given auth method name, if configurable options permit this method fill
15850Sstevel@tonic-gate * in auth_ident field and return true, otherwise return false.
15860Sstevel@tonic-gate */
15870Sstevel@tonic-gate static int
authmethod_is_enabled(Authmethod * method)15880Sstevel@tonic-gate authmethod_is_enabled(Authmethod *method)
15890Sstevel@tonic-gate {
15900Sstevel@tonic-gate if (method == NULL)
15910Sstevel@tonic-gate return 0;
15920Sstevel@tonic-gate /* return false if options indicate this method is disabled */
15930Sstevel@tonic-gate if (method->enabled == NULL || *method->enabled == 0)
15940Sstevel@tonic-gate return 0;
15950Sstevel@tonic-gate /* return false if batch mode is enabled but method needs interactive mode */
15960Sstevel@tonic-gate if (method->batch_flag != NULL && *method->batch_flag != 0)
15970Sstevel@tonic-gate return 0;
15980Sstevel@tonic-gate return 1;
15990Sstevel@tonic-gate }
16000Sstevel@tonic-gate
16010Sstevel@tonic-gate static Authmethod *
authmethod_lookup(const char * name)16020Sstevel@tonic-gate authmethod_lookup(const char *name)
16030Sstevel@tonic-gate {
16040Sstevel@tonic-gate Authmethod *method = NULL;
16050Sstevel@tonic-gate if (name != NULL) {
16060Sstevel@tonic-gate for (method = authmethods; method->name != NULL; method++) {
16070Sstevel@tonic-gate if (strcmp(name, method->name) == 0)
16080Sstevel@tonic-gate return method;
16090Sstevel@tonic-gate }
16100Sstevel@tonic-gate }
16110Sstevel@tonic-gate debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
16120Sstevel@tonic-gate return NULL;
16130Sstevel@tonic-gate }
16140Sstevel@tonic-gate
16150Sstevel@tonic-gate /* XXX internal state */
16160Sstevel@tonic-gate static Authmethod *current = NULL;
16170Sstevel@tonic-gate static char *supported = NULL;
16180Sstevel@tonic-gate static char *preferred = NULL;
16190Sstevel@tonic-gate
16200Sstevel@tonic-gate /*
16210Sstevel@tonic-gate * Given the authentication method list sent by the server, return the
16220Sstevel@tonic-gate * next method we should try. If the server initially sends a nil list,
16230Sstevel@tonic-gate * use a built-in default list.
16240Sstevel@tonic-gate */
16250Sstevel@tonic-gate static Authmethod *
authmethod_get(char * authlist)16260Sstevel@tonic-gate authmethod_get(char *authlist)
16270Sstevel@tonic-gate {
16280Sstevel@tonic-gate char *name = NULL;
16290Sstevel@tonic-gate u_int next;
16300Sstevel@tonic-gate
16310Sstevel@tonic-gate /* Use a suitable default if we're passed a nil list. */
16320Sstevel@tonic-gate if (authlist == NULL || strlen(authlist) == 0)
16330Sstevel@tonic-gate authlist = options.preferred_authentications;
16340Sstevel@tonic-gate
16350Sstevel@tonic-gate if (supported == NULL || strcmp(authlist, supported) != 0) {
16360Sstevel@tonic-gate debug3("start over, passed a different list %s", authlist);
16370Sstevel@tonic-gate if (supported != NULL)
16380Sstevel@tonic-gate xfree(supported);
16390Sstevel@tonic-gate supported = xstrdup(authlist);
16400Sstevel@tonic-gate preferred = options.preferred_authentications;
16410Sstevel@tonic-gate debug3("preferred %s", preferred);
16420Sstevel@tonic-gate current = NULL;
16430Sstevel@tonic-gate } else if (current != NULL && authmethod_is_enabled(current))
16440Sstevel@tonic-gate return current;
16450Sstevel@tonic-gate
16460Sstevel@tonic-gate for (;;) {
16470Sstevel@tonic-gate if ((name = match_list(preferred, supported, &next)) == NULL) {
16480Sstevel@tonic-gate debug("No more authentication methods to try.");
16490Sstevel@tonic-gate current = NULL;
16500Sstevel@tonic-gate return NULL;
16510Sstevel@tonic-gate }
16520Sstevel@tonic-gate preferred += next;
16530Sstevel@tonic-gate debug3("authmethod_lookup %s", name);
16540Sstevel@tonic-gate debug3("remaining preferred: %s", preferred);
16550Sstevel@tonic-gate if ((current = authmethod_lookup(name)) != NULL &&
16560Sstevel@tonic-gate authmethod_is_enabled(current)) {
16570Sstevel@tonic-gate debug3("authmethod_is_enabled %s", name);
16580Sstevel@tonic-gate debug("Next authentication method: %s", name);
1659*9845SJan.Pechanec@Sun.COM xfree(name);
16600Sstevel@tonic-gate return current;
16610Sstevel@tonic-gate }
1662*9845SJan.Pechanec@Sun.COM xfree(name);
16630Sstevel@tonic-gate }
16640Sstevel@tonic-gate }
16650Sstevel@tonic-gate
16660Sstevel@tonic-gate static char *
authmethods_get(void)16670Sstevel@tonic-gate authmethods_get(void)
16680Sstevel@tonic-gate {
16690Sstevel@tonic-gate Authmethod *method = NULL;
16700Sstevel@tonic-gate Buffer b;
16710Sstevel@tonic-gate char *list;
16720Sstevel@tonic-gate
16730Sstevel@tonic-gate buffer_init(&b);
16740Sstevel@tonic-gate for (method = authmethods; method->name != NULL; method++) {
16750Sstevel@tonic-gate if (authmethod_is_enabled(method)) {
16760Sstevel@tonic-gate if (buffer_len(&b) > 0)
16770Sstevel@tonic-gate buffer_append(&b, ",", 1);
16780Sstevel@tonic-gate buffer_append(&b, method->name, strlen(method->name));
16790Sstevel@tonic-gate }
16800Sstevel@tonic-gate }
16810Sstevel@tonic-gate buffer_append(&b, "\0", 1);
16820Sstevel@tonic-gate list = xstrdup(buffer_ptr(&b));
16830Sstevel@tonic-gate buffer_free(&b);
16840Sstevel@tonic-gate return list;
16850Sstevel@tonic-gate }
1686