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