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