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