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