Annotation of src/usr.bin/ssh/clientloop.c, Revision 1.386
1.386 ! djm 1: /* $OpenBSD: clientloop.c,v 1.385 2022/11/29 22:41:14 dtucker Exp $ */
1.1 deraadt 2: /*
1.12 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
1.33 deraadt 6: * The main loop for the interactive session (client side).
1.20 markus 7: *
1.33 deraadt 8: * As far as I am concerned, the code I have written for this software
9: * can be used freely for any purpose. Any derived versions of this
10: * software must be clearly marked as such, and if the derived work is
11: * incompatible with the protocol description in the RFC file, it must be
12: * called by a name other than "ssh" or "Secure Shell".
13: *
14: *
15: * Copyright (c) 1999 Theo de Raadt. All rights reserved.
16: *
17: * Redistribution and use in source and binary forms, with or without
18: * modification, are permitted provided that the following conditions
19: * are met:
20: * 1. Redistributions of source code must retain the above copyright
21: * notice, this list of conditions and the following disclaimer.
22: * 2. Redistributions in binary form must reproduce the above copyright
23: * notice, this list of conditions and the following disclaimer in the
24: * documentation and/or other materials provided with the distribution.
25: *
26: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.20 markus 36: *
37: *
1.33 deraadt 38: * SSH2 support added by Markus Friedl.
1.78 markus 39: * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
1.20 markus 40: *
1.33 deraadt 41: * Redistribution and use in source and binary forms, with or without
42: * modification, are permitted provided that the following conditions
43: * are met:
44: * 1. Redistributions of source code must retain the above copyright
45: * notice, this list of conditions and the following disclaimer.
46: * 2. Redistributions in binary form must reproduce the above copyright
47: * notice, this list of conditions and the following disclaimer in the
48: * documentation and/or other materials provided with the distribution.
49: *
50: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
51: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
52: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
54: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
56: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
57: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
59: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.12 deraadt 60: */
1.1 deraadt 61:
1.152 stevesk 62:
1.175 deraadt 63: #include <sys/types.h>
1.152 stevesk 64: #include <sys/ioctl.h>
1.154 stevesk 65: #include <sys/stat.h>
1.166 stevesk 66: #include <sys/socket.h>
1.171 stevesk 67: #include <sys/time.h>
1.189 djm 68: #include <sys/queue.h>
1.150 stevesk 69:
1.155 stevesk 70: #include <ctype.h>
1.168 stevesk 71: #include <errno.h>
1.151 stevesk 72: #include <paths.h>
1.374 djm 73: #include <poll.h>
1.153 stevesk 74: #include <signal.h>
1.174 stevesk 75: #include <stdio.h>
1.173 stevesk 76: #include <stdlib.h>
1.170 stevesk 77: #include <string.h>
1.328 deraadt 78: #include <stdarg.h>
1.150 stevesk 79: #include <termios.h>
1.175 deraadt 80: #include <pwd.h>
1.169 stevesk 81: #include <unistd.h>
1.266 deraadt 82: #include <limits.h>
1.1 deraadt 83:
1.175 deraadt 84: #include "xmalloc.h"
1.45 markus 85: #include "ssh.h"
86: #include "ssh2.h"
1.1 deraadt 87: #include "packet.h"
1.315 markus 88: #include "sshbuf.h"
1.15 markus 89: #include "compat.h"
1.74 markus 90: #include "channels.h"
1.15 markus 91: #include "dispatch.h"
1.317 markus 92: #include "sshkey.h"
1.175 deraadt 93: #include "cipher.h"
1.54 markus 94: #include "kex.h"
1.275 markus 95: #include "myproposal.h"
1.45 markus 96: #include "log.h"
1.261 millert 97: #include "misc.h"
1.45 markus 98: #include "readconf.h"
99: #include "clientloop.h"
1.147 djm 100: #include "sshconnect.h"
1.40 markus 101: #include "authfd.h"
1.45 markus 102: #include "atomicio.h"
1.118 deraadt 103: #include "sshpty.h"
1.123 djm 104: #include "match.h"
105: #include "msg.h"
1.262 djm 106: #include "ssherr.h"
1.267 djm 107: #include "hostfile.h"
1.319 djm 108:
1.375 djm 109: /* Permitted RSA signature algorithms for UpdateHostkeys proofs */
110: #define HOSTKEY_PROOF_RSA_ALGS "rsa-sha2-512,rsa-sha2-256"
111:
1.39 markus 112: /* import options */
113: extern Options options;
1.1 deraadt 114:
1.123 djm 115: /* Control socket */
1.217 djm 116: extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
1.123 djm 117:
1.13 markus 118: /*
119: * Name of the host we are connecting to. This is the name given on the
1.324 jmc 120: * command line, or the Hostname specified for the user-supplied name in a
1.13 markus 121: * configuration file.
122: */
1.1 deraadt 123: extern char *host;
124:
1.13 markus 125: /*
1.330 djm 126: * If this field is not NULL, the ForwardAgent socket is this path and different
127: * instead of SSH_AUTH_SOCK.
128: */
129: extern char *forward_agent_sock_path;
130:
131: /*
1.13 markus 132: * Flag to indicate that we have received a window change signal which has
133: * not yet been processed. This will cause a message indicating the new
134: * window size to be sent to the server a little later. This is volatile
135: * because this is updated in a signal handler.
136: */
1.88 markus 137: static volatile sig_atomic_t received_window_change_signal = 0;
138: static volatile sig_atomic_t received_signal = 0;
1.1 deraadt 139:
1.222 djm 140: /* Time when backgrounded control master using ControlPersist should exit */
141: static time_t control_persist_exit_time = 0;
142:
1.1 deraadt 143: /* Common data for the client loop code. */
1.217 djm 144: volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
1.11 markus 145: static int last_was_cr; /* Last character was a newline. */
1.197 djm 146: static int exit_status; /* Used to store the command exit status. */
1.315 markus 147: static struct sshbuf *stderr_buffer; /* Used for final exit message. */
1.11 markus 148: static int connection_in; /* Connection to server (input). */
149: static int connection_out; /* Connection to server (output). */
1.56 markus 150: static int need_rekeying; /* Set to non-zero if rekeying is requested. */
1.221 djm 151: static int session_closed; /* In SSH2: login session closed. */
1.274 djm 152: static u_int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
1.345 dtucker 153: static time_t server_alive_time; /* Time to do server_alive_check */
1.384 djm 154: static int hostkeys_update_complete;
155: static int session_setup_complete;
1.1 deraadt 156:
1.321 djm 157: static void client_init_dispatch(struct ssh *ssh);
1.16 markus 158: int session_ident = -1;
159:
1.195 djm 160: /* Track escape per proto2 channel */
161: struct escape_filter_ctx {
162: int escape_pending;
163: int escape_char;
164: };
165:
166: /* Context for channel confirmation replies */
1.190 djm 167: struct channel_reply_ctx {
168: const char *request_type;
1.234 djm 169: int id;
170: enum confirm_action action;
1.190 djm 171: };
172:
1.196 djm 173: /* Global request success/failure callbacks */
1.303 djm 174: /* XXX move to struct ssh? */
1.196 djm 175: struct global_confirm {
176: TAILQ_ENTRY(global_confirm) entry;
177: global_confirm_cb *cb;
178: void *ctx;
179: int ref_count;
180: };
181: TAILQ_HEAD(global_confirms, global_confirm);
182: static struct global_confirms global_confirms =
183: TAILQ_HEAD_INITIALIZER(global_confirms);
184:
1.315 markus 185: void ssh_process_session2_setup(int, int, int, struct sshbuf *);
1.377 djm 186: static void quit_message(const char *fmt, ...)
187: __attribute__((__format__ (printf, 1, 2)));
188:
189: static void
190: quit_message(const char *fmt, ...)
191: {
192: char *msg;
193: va_list args;
194: int r;
195:
196: va_start(args, fmt);
197: xvasprintf(&msg, fmt, args);
198: va_end(args);
199:
200: if ((r = sshbuf_putf(stderr_buffer, "%s\r\n", msg)) != 0)
201: fatal_fr(r, "sshbuf_putf");
202: quit_pending = 1;
203: }
1.123 djm 204:
1.13 markus 205: /*
206: * Signal handler for the window change signal (SIGWINCH). This just sets a
207: * flag indicating that the window has changed.
208: */
1.157 deraadt 209: /*ARGSUSED */
1.77 itojun 210: static void
1.11 markus 211: window_change_handler(int sig)
1.1 deraadt 212: {
1.11 markus 213: received_window_change_signal = 1;
1.1 deraadt 214: }
215:
1.13 markus 216: /*
217: * Signal handler for signals that cause the program to terminate. These
218: * signals must be trapped to restore terminal modes.
219: */
1.157 deraadt 220: /*ARGSUSED */
1.77 itojun 221: static void
1.11 markus 222: signal_handler(int sig)
1.1 deraadt 223: {
1.75 markus 224: received_signal = sig;
225: quit_pending = 1;
1.1 deraadt 226: }
227:
1.13 markus 228: /*
1.222 djm 229: * Sets control_persist_exit_time to the absolute time when the
230: * backgrounded control master should exit due to expiry of the
231: * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
232: * control master process, or if there is no ControlPersist timeout.
233: */
234: static void
1.303 djm 235: set_control_persist_exit_time(struct ssh *ssh)
1.222 djm 236: {
237: if (muxserver_sock == -1 || !options.control_persist
1.232 djm 238: || options.control_persist_timeout == 0) {
1.222 djm 239: /* not using a ControlPersist timeout */
240: control_persist_exit_time = 0;
1.303 djm 241: } else if (channel_still_open(ssh)) {
1.222 djm 242: /* some client connections are still open */
243: if (control_persist_exit_time > 0)
1.354 djm 244: debug2_f("cancel scheduled exit");
1.222 djm 245: control_persist_exit_time = 0;
246: } else if (control_persist_exit_time <= 0) {
247: /* a client connection has recently closed */
1.251 dtucker 248: control_persist_exit_time = monotime() +
1.222 djm 249: (time_t)options.control_persist_timeout;
1.354 djm 250: debug2_f("schedule exit in %d seconds",
1.222 djm 251: options.control_persist_timeout);
252: }
253: /* else we are already counting down to the timeout */
254: }
255:
1.238 dtucker 256: #define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
257: static int
258: client_x11_display_valid(const char *display)
259: {
260: size_t i, dlen;
261:
1.279 djm 262: if (display == NULL)
263: return 0;
264:
1.238 dtucker 265: dlen = strlen(display);
266: for (i = 0; i < dlen; i++) {
1.256 deraadt 267: if (!isalnum((u_char)display[i]) &&
1.238 dtucker 268: strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
269: debug("Invalid character '%c' in DISPLAY", display[i]);
270: return 0;
271: }
272: }
273: return 1;
274: }
275:
1.274 djm 276: #define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
277: #define X11_TIMEOUT_SLACK 60
1.279 djm 278: int
1.303 djm 279: client_x11_get_proto(struct ssh *ssh, const char *display,
280: const char *xauth_path, u_int trusted, u_int timeout,
281: char **_proto, char **_data)
1.138 djm 282: {
1.318 djm 283: char *cmd, line[512], xdisplay[512];
1.279 djm 284: char xauthfile[PATH_MAX], xauthdir[PATH_MAX];
1.138 djm 285: static char proto[512], data[512];
286: FILE *f;
1.288 tedu 287: int got_data = 0, generated = 0, do_unlink = 0, r;
1.138 djm 288: struct stat st;
1.274 djm 289: u_int now, x11_timeout_real;
1.138 djm 290:
291: *_proto = proto;
292: *_data = data;
1.279 djm 293: proto[0] = data[0] = xauthfile[0] = xauthdir[0] = '\0';
1.138 djm 294:
1.279 djm 295: if (!client_x11_display_valid(display)) {
1.283 millert 296: if (display != NULL)
297: logit("DISPLAY \"%s\" invalid; disabling X11 forwarding",
298: display);
1.279 djm 299: return -1;
300: }
301: if (xauth_path != NULL && stat(xauth_path, &st) == -1) {
1.138 djm 302: debug("No xauth program.");
1.279 djm 303: xauth_path = NULL;
304: }
305:
306: if (xauth_path != NULL) {
1.138 djm 307: /*
308: * Handle FamilyLocal case where $DISPLAY does
309: * not match an authorization entry. For this we
310: * just try "xauth list unix:displaynum.screennum".
311: * XXX: "localhost" match to determine FamilyLocal
312: * is not perfect.
313: */
314: if (strncmp(display, "localhost:", 10) == 0) {
1.279 djm 315: if ((r = snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
316: display + 10)) < 0 ||
317: (size_t)r >= sizeof(xdisplay)) {
1.354 djm 318: error_f("display name too long");
1.279 djm 319: return -1;
320: }
1.138 djm 321: display = xdisplay;
322: }
323: if (trusted == 0) {
1.274 djm 324: /*
1.279 djm 325: * Generate an untrusted X11 auth cookie.
326: *
1.274 djm 327: * The authentication cookie should briefly outlive
328: * ssh's willingness to forward X11 connections to
329: * avoid nasty fail-open behaviour in the X server.
330: */
1.279 djm 331: mktemp_proto(xauthdir, sizeof(xauthdir));
332: if (mkdtemp(xauthdir) == NULL) {
1.354 djm 333: error_f("mkdtemp: %s", strerror(errno));
1.279 djm 334: return -1;
335: }
336: do_unlink = 1;
337: if ((r = snprintf(xauthfile, sizeof(xauthfile),
338: "%s/xauthfile", xauthdir)) < 0 ||
339: (size_t)r >= sizeof(xauthfile)) {
1.354 djm 340: error_f("xauthfile path too long");
1.279 djm 341: rmdir(xauthdir);
342: return -1;
343: }
344:
1.318 djm 345: if (timeout == 0) {
346: /* auth doesn't time out */
347: xasprintf(&cmd, "%s -f %s generate %s %s "
348: "untrusted 2>%s",
349: xauth_path, xauthfile, display,
350: SSH_X11_PROTO, _PATH_DEVNULL);
351: } else {
352: /* Add some slack to requested expiry */
353: if (timeout < UINT_MAX - X11_TIMEOUT_SLACK)
354: x11_timeout_real = timeout +
355: X11_TIMEOUT_SLACK;
356: else {
357: /* Don't overflow on long timeouts */
358: x11_timeout_real = UINT_MAX;
359: }
360: xasprintf(&cmd, "%s -f %s generate %s %s "
361: "untrusted timeout %u 2>%s",
362: xauth_path, xauthfile, display,
363: SSH_X11_PROTO, x11_timeout_real,
364: _PATH_DEVNULL);
365: }
1.354 djm 366: debug2_f("xauth command: %s", cmd);
1.318 djm 367:
368: if (timeout != 0 && x11_refuse_time == 0) {
1.279 djm 369: now = monotime() + 1;
370: if (UINT_MAX - timeout < now)
371: x11_refuse_time = UINT_MAX;
372: else
373: x11_refuse_time = now + timeout;
1.303 djm 374: channel_set_x11_refuse_time(ssh,
375: x11_refuse_time);
1.138 djm 376: }
1.279 djm 377: if (system(cmd) == 0)
378: generated = 1;
1.318 djm 379: free(cmd);
1.138 djm 380: }
1.181 markus 381:
382: /*
383: * When in untrusted mode, we read the cookie only if it was
384: * successfully generated as an untrusted one in the step
385: * above.
386: */
387: if (trusted || generated) {
1.318 djm 388: xasprintf(&cmd,
1.181 markus 389: "%s %s%s list %s 2>" _PATH_DEVNULL,
390: xauth_path,
391: generated ? "-f " : "" ,
392: generated ? xauthfile : "",
393: display);
394: debug2("x11_get_proto: %s", cmd);
395: f = popen(cmd, "r");
396: if (f && fgets(line, sizeof(line), f) &&
397: sscanf(line, "%*s %511s %511s", proto, data) == 2)
398: got_data = 1;
399: if (f)
400: pclose(f);
1.318 djm 401: free(cmd);
1.279 djm 402: }
1.138 djm 403: }
404:
405: if (do_unlink) {
406: unlink(xauthfile);
407: rmdir(xauthdir);
408: }
409:
1.279 djm 410: /* Don't fall back to fake X11 data for untrusted forwarding */
411: if (!trusted && !got_data) {
412: error("Warning: untrusted X11 forwarding setup failed: "
413: "xauth key data not generated");
414: return -1;
415: }
416:
1.138 djm 417: /*
418: * If we didn't get authentication data, just make up some
419: * data. The forwarding code will check the validity of the
420: * response anyway, and substitute this data. The X11
421: * server, however, will ignore this fake data and use
422: * whatever authentication mechanisms it was using otherwise
423: * for the local connection.
424: */
425: if (!got_data) {
1.288 tedu 426: u_int8_t rnd[16];
427: u_int i;
1.138 djm 428:
429: logit("Warning: No xauth data; "
430: "using fake authentication data for X11 forwarding.");
431: strlcpy(proto, SSH_X11_PROTO, sizeof proto);
1.288 tedu 432: arc4random_buf(rnd, sizeof(rnd));
433: for (i = 0; i < sizeof(rnd); i++) {
1.138 djm 434: snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
1.288 tedu 435: rnd[i]);
1.138 djm 436: }
437: }
1.279 djm 438:
439: return 0;
1.1 deraadt 440: }
441:
1.13 markus 442: /*
443: * Checks if the client window has changed, and sends a packet about it to
444: * the server if so. The actual change is detected elsewhere (by a software
445: * interrupt on Unix); this just checks the flag and sends a message if
446: * appropriate.
447: */
1.1 deraadt 448:
1.77 itojun 449: static void
1.303 djm 450: client_check_window_change(struct ssh *ssh)
1.1 deraadt 451: {
1.292 djm 452: if (!received_window_change_signal)
1.16 markus 453: return;
454: received_window_change_signal = 0;
1.354 djm 455: debug2_f("changed");
1.303 djm 456: channel_send_window_changes(ssh);
1.1 deraadt 457: }
458:
1.264 markus 459: static int
1.297 markus 460: client_global_request_reply(int type, u_int32_t seq, struct ssh *ssh)
1.117 markus 461: {
1.196 djm 462: struct global_confirm *gc;
463:
464: if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
1.264 markus 465: return 0;
1.196 djm 466: if (gc->cb != NULL)
1.303 djm 467: gc->cb(ssh, type, seq, gc->ctx);
1.196 djm 468: if (--gc->ref_count <= 0) {
469: TAILQ_REMOVE(&global_confirms, gc, entry);
1.342 jsg 470: freezero(gc, sizeof(*gc));
1.196 djm 471: }
472:
1.320 djm 473: ssh_packet_set_alive_timeouts(ssh, 0);
1.264 markus 474: return 0;
1.117 markus 475: }
476:
477: static void
1.345 dtucker 478: schedule_server_alive_check(void)
479: {
480: if (options.server_alive_interval > 0)
481: server_alive_time = monotime() + options.server_alive_interval;
482: }
483:
484: static void
1.320 djm 485: server_alive_check(struct ssh *ssh)
1.117 markus 486: {
1.320 djm 487: int r;
488:
489: if (ssh_packet_inc_alive_timeouts(ssh) > options.server_alive_count_max) {
1.228 djm 490: logit("Timeout, server %s not responding.", host);
1.176 markus 491: cleanup_exit(255);
492: }
1.320 djm 493: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
494: (r = sshpkt_put_cstring(ssh, "keepalive@openssh.com")) != 0 ||
495: (r = sshpkt_put_u8(ssh, 1)) != 0 || /* boolean: want reply */
496: (r = sshpkt_send(ssh)) != 0)
1.354 djm 497: fatal_fr(r, "send packet");
1.196 djm 498: /* Insert an empty placeholder to maintain ordering */
499: client_register_global_confirm(NULL, NULL);
1.345 dtucker 500: schedule_server_alive_check();
1.117 markus 501: }
502:
1.13 markus 503: /*
504: * Waits until the client can do something (some data becomes available on
505: * one of the file descriptors).
506: */
1.77 itojun 507: static void
1.374 djm 508: client_wait_until_can_do_something(struct ssh *ssh, struct pollfd **pfdp,
509: u_int *npfd_allocp, u_int *npfd_activep, int rekeying,
510: int *conn_in_readyp, int *conn_out_readyp)
1.11 markus 511: {
1.386 ! djm 512: struct timespec timeout;
1.377 djm 513: int ret;
1.374 djm 514: u_int p;
1.117 markus 515:
1.374 djm 516: *conn_in_readyp = *conn_out_readyp = 0;
1.11 markus 517:
1.374 djm 518: /* Prepare channel poll. First two pollfd entries are reserved */
1.386 ! djm 519: ptimeout_init(&timeout);
! 520: channel_prepare_poll(ssh, pfdp, npfd_allocp, npfd_activep, 2, &timeout);
1.374 djm 521: if (*npfd_activep < 2)
522: fatal_f("bad npfd %u", *npfd_activep); /* shouldn't happen */
523:
524: /* channel_prepare_poll could have closed the last channel */
1.303 djm 525: if (session_closed && !channel_still_open(ssh) &&
1.320 djm 526: !ssh_packet_have_data_to_write(ssh)) {
1.374 djm 527: /* clear events since we did not call poll() */
528: for (p = 0; p < *npfd_activep; p++)
529: (*pfdp)[p].revents = 0;
1.292 djm 530: return;
1.16 markus 531: }
1.11 markus 532:
1.374 djm 533: /* Monitor server connection on reserved pollfd entries */
534: (*pfdp)[0].fd = connection_in;
535: (*pfdp)[0].events = POLLIN;
536: (*pfdp)[1].fd = connection_out;
537: (*pfdp)[1].events = ssh_packet_have_data_to_write(ssh) ? POLLOUT : 0;
1.11 markus 538:
1.13 markus 539: /*
540: * Wait for something to happen. This will suspend the process until
1.374 djm 541: * some polled descriptor can be read, written, or has some other
1.222 djm 542: * event pending, or a timeout expires.
1.13 markus 543: */
1.386 ! djm 544: set_control_persist_exit_time(ssh);
! 545: if (control_persist_exit_time > 0)
! 546: ptimeout_deadline_monotime(&timeout, control_persist_exit_time);
1.345 dtucker 547: if (options.server_alive_interval > 0)
1.386 ! djm 548: ptimeout_deadline_monotime(&timeout, server_alive_time);
! 549: if (options.rekey_interval > 0 && !rekeying) {
! 550: ptimeout_deadline_sec(&timeout,
1.320 djm 551: ssh_packet_get_rekey_timeout(ssh));
1.386 ! djm 552: }
1.374 djm 553:
1.386 ! djm 554: ret = poll(*pfdp, *npfd_activep, ptimeout_get_ms(&timeout));
1.222 djm 555:
1.326 deraadt 556: if (ret == -1) {
1.51 markus 557: /*
1.374 djm 558: * We have to clear the events because we return.
1.51 markus 559: * We have to return, because the mainloop checks for the flags
560: * set by the signal handlers.
561: */
1.374 djm 562: for (p = 0; p < *npfd_activep; p++)
563: (*pfdp)[p].revents = 0;
1.11 markus 564: if (errno == EINTR)
565: return;
566: /* Note: we might still have data in the buffers. */
1.377 djm 567: quit_message("poll: %s", strerror(errno));
1.374 djm 568: return;
569: }
570:
571: *conn_in_readyp = (*pfdp)[0].revents != 0;
572: *conn_out_readyp = (*pfdp)[1].revents != 0;
573:
574: if (options.server_alive_interval > 0 && !*conn_in_readyp &&
575: monotime() >= server_alive_time) {
1.249 dtucker 576: /*
1.374 djm 577: * ServerAlive check is needed. We can't rely on the poll
1.345 dtucker 578: * timing out since traffic on the client side such as port
579: * forwards can keep waking it up.
1.249 dtucker 580: */
1.345 dtucker 581: server_alive_check(ssh);
1.374 djm 582: }
1.11 markus 583: }
584:
1.77 itojun 585: static void
1.315 markus 586: client_suspend_self(struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr)
1.1 deraadt 587: {
1.11 markus 588: /* Flush stdout and stderr buffers. */
1.315 markus 589: if (sshbuf_len(bout) > 0)
590: atomicio(vwrite, fileno(stdout), sshbuf_mutable_ptr(bout),
591: sshbuf_len(bout));
592: if (sshbuf_len(berr) > 0)
593: atomicio(vwrite, fileno(stderr), sshbuf_mutable_ptr(berr),
594: sshbuf_len(berr));
1.11 markus 595:
1.233 djm 596: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.11 markus 597:
1.305 djm 598: sshbuf_reset(bin);
599: sshbuf_reset(bout);
600: sshbuf_reset(berr);
1.11 markus 601:
602: /* Send the suspend signal to the program itself. */
603: kill(getpid(), SIGTSTP);
604:
1.132 djm 605: /* Reset window sizes in case they have changed */
606: received_window_change_signal = 1;
1.11 markus 607:
1.233 djm 608: enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.11 markus 609: }
610:
1.77 itojun 611: static void
1.374 djm 612: client_process_net_input(struct ssh *ssh)
1.11 markus 613: {
1.378 djm 614: int r;
1.11 markus 615:
1.13 markus 616: /*
617: * Read input from the server, and add any such data to the buffer of
618: * the packet subsystem.
619: */
1.374 djm 620: schedule_server_alive_check();
1.378 djm 621: if ((r = ssh_packet_process_read(ssh, connection_in)) == 0)
622: return; /* success */
623: if (r == SSH_ERR_SYSTEM_ERROR) {
624: if (errno == EAGAIN || errno == EINTR)
625: return;
626: if (errno == EPIPE) {
627: quit_message("Connection to %s closed by remote host.",
628: host);
629: return;
630: }
1.11 markus 631: }
1.378 djm 632: quit_message("Read from remote host %s: %s", host, ssh_err(r));
1.17 markus 633: }
1.16 markus 634:
1.97 jakob 635: static void
1.303 djm 636: client_status_confirm(struct ssh *ssh, int type, Channel *c, void *ctx)
1.123 djm 637: {
1.190 djm 638: struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
639: char errmsg[256];
1.315 markus 640: int r, tochan;
1.190 djm 641:
1.234 djm 642: /*
643: * If a TTY was explicitly requested, then a failure to allocate
644: * one is fatal.
645: */
646: if (cr->action == CONFIRM_TTY &&
647: (options.request_tty == REQUEST_TTY_FORCE ||
648: options.request_tty == REQUEST_TTY_YES))
649: cr->action = CONFIRM_CLOSE;
650:
1.312 djm 651: /* XXX suppress on mux _client_ quietmode */
1.190 djm 652: tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
1.217 djm 653: c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
1.190 djm 654:
655: if (type == SSH2_MSG_CHANNEL_SUCCESS) {
656: debug2("%s request accepted on channel %d",
657: cr->request_type, c->self);
658: } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
659: if (tochan) {
660: snprintf(errmsg, sizeof(errmsg),
661: "%s request failed\r\n", cr->request_type);
662: } else {
663: snprintf(errmsg, sizeof(errmsg),
664: "%s request failed on channel %d",
665: cr->request_type, c->self);
666: }
667: /* If error occurred on primary session channel, then exit */
1.234 djm 668: if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
1.190 djm 669: fatal("%s", errmsg);
1.234 djm 670: /*
671: * If error occurred on mux client, append to
672: * their stderr.
673: */
674: if (tochan) {
1.360 djm 675: debug3_f("channel %d: mux request: %s", c->self,
676: cr->request_type);
1.315 markus 677: if ((r = sshbuf_put(c->extended, errmsg,
678: strlen(errmsg))) != 0)
1.354 djm 679: fatal_fr(r, "sshbuf_put");
1.234 djm 680: } else
1.190 djm 681: error("%s", errmsg);
1.234 djm 682: if (cr->action == CONFIRM_TTY) {
683: /*
684: * If a TTY allocation error occurred, then arrange
685: * for the correct TTY to leave raw mode.
686: */
687: if (c->self == session_ident)
688: leave_raw_mode(0);
689: else
1.303 djm 690: mux_tty_alloc_failed(ssh, c);
1.234 djm 691: } else if (cr->action == CONFIRM_CLOSE) {
1.303 djm 692: chan_read_failed(ssh, c);
693: chan_write_failed(ssh, c);
1.190 djm 694: }
695: }
1.250 djm 696: free(cr);
1.190 djm 697: }
698:
699: static void
1.303 djm 700: client_abandon_status_confirm(struct ssh *ssh, Channel *c, void *ctx)
1.190 djm 701: {
1.250 djm 702: free(ctx);
1.190 djm 703: }
1.129 deraadt 704:
1.236 djm 705: void
1.303 djm 706: client_expect_confirm(struct ssh *ssh, int id, const char *request,
1.234 djm 707: enum confirm_action action)
1.190 djm 708: {
1.255 djm 709: struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
1.123 djm 710:
1.190 djm 711: cr->request_type = request;
1.234 djm 712: cr->action = action;
1.123 djm 713:
1.303 djm 714: channel_register_status_confirm(ssh, id, client_status_confirm,
1.190 djm 715: client_abandon_status_confirm, cr);
1.196 djm 716: }
717:
718: void
719: client_register_global_confirm(global_confirm_cb *cb, void *ctx)
720: {
1.201 djm 721: struct global_confirm *gc, *last_gc;
1.196 djm 722:
723: /* Coalesce identical callbacks */
1.201 djm 724: last_gc = TAILQ_LAST(&global_confirms, global_confirms);
725: if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
726: if (++last_gc->ref_count >= INT_MAX)
1.354 djm 727: fatal_f("last_gc->ref_count = %d",
728: last_gc->ref_count);
1.196 djm 729: return;
730: }
731:
1.255 djm 732: gc = xcalloc(1, sizeof(*gc));
1.196 djm 733: gc->cb = cb;
734: gc->ctx = ctx;
735: gc->ref_count = 1;
736: TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
1.123 djm 737: }
738:
1.384 djm 739: /*
740: * Returns non-zero if the client is able to handle a hostkeys-00@openssh.com
741: * hostkey update request.
742: */
743: static int
744: can_update_hostkeys(void)
745: {
746: if (hostkeys_update_complete)
747: return 0;
748: if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK &&
749: options.batch_mode)
750: return 0; /* won't ask in batchmode, so don't even try */
751: if (!options.update_hostkeys || options.num_user_hostfiles <= 0)
752: return 0;
753: return 1;
754: }
755:
1.385 dtucker 756: static void
757: client_repledge(void)
1.384 djm 758: {
759: debug3_f("enter");
760:
761: /* Might be able to tighten pledge now that session is established */
762: if (options.control_master || options.control_path != NULL ||
763: options.forward_x11 || options.fork_after_authentication ||
764: can_update_hostkeys() ||
765: (session_ident != -1 && !session_setup_complete)) {
766: /* Can't tighten */
767: return;
768: }
769: /*
770: * LocalCommand and UpdateHostkeys have finished, so can get rid of
771: * filesystem.
772: *
773: * XXX protocol allows a server can to change hostkeys during the
774: * connection at rekey time that could trigger a hostkeys update
775: * but AFAIK no implementations support this. Could improve by
776: * forcing known_hosts to be read-only or via unveil(2).
777: */
778: if (options.num_local_forwards != 0 ||
779: options.num_remote_forwards != 0 ||
780: options.num_permitted_remote_opens != 0 ||
781: options.enable_escape_commandline != 0) {
782: /* rfwd needs inet */
783: debug("pledge: network");
784: if (pledge("stdio unix inet dns proc tty", NULL) == -1)
785: fatal_f("pledge(): %s", strerror(errno));
786: } else if (options.forward_agent != 0) {
787: /* agent forwarding needs to open $SSH_AUTH_SOCK at will */
788: debug("pledge: agent");
789: if (pledge("stdio unix proc tty", NULL) == -1)
790: fatal_f("pledge(): %s", strerror(errno));
791: } else {
792: debug("pledge: fork");
793: if (pledge("stdio proc tty", NULL) == -1)
794: fatal_f("pledge(): %s", strerror(errno));
795: }
796: /* XXX further things to do:
797: *
798: * - might be able to get rid of proc if we kill ~^Z
799: * - ssh -N (no session)
800: * - stdio forwarding
801: * - sessions without tty
802: */
803: }
804:
1.123 djm 805: static void
1.303 djm 806: process_cmdline(struct ssh *ssh)
1.97 jakob 807: {
808: void (*handler)(int);
1.261 millert 809: char *s, *cmd;
810: int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
811: struct Forward fwd;
1.97 jakob 812:
1.257 tedu 813: memset(&fwd, 0, sizeof(fwd));
1.183 djm 814:
1.233 djm 815: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.332 dtucker 816: handler = ssh_signal(SIGINT, SIG_IGN);
1.99 markus 817: cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
1.97 jakob 818: if (s == NULL)
819: goto out;
1.256 deraadt 820: while (isspace((u_char)*s))
1.97 jakob 821: s++;
1.121 djm 822: if (*s == '-')
823: s++; /* Skip cmdline '-', if any */
1.120 dtucker 824: if (*s == '\0')
1.97 jakob 825: goto out;
1.121 djm 826:
1.122 djm 827: if (*s == 'h' || *s == 'H' || *s == '?') {
1.121 djm 828: logit("Commands:");
1.164 djm 829: logit(" -L[bind_address:]port:host:hostport "
830: "Request local forward");
831: logit(" -R[bind_address:]port:host:hostport "
832: "Request remote forward");
1.204 stevesk 833: logit(" -D[bind_address:]port "
834: "Request dynamic forward");
1.237 markus 835: logit(" -KL[bind_address:]port "
836: "Cancel local forward");
1.165 stevesk 837: logit(" -KR[bind_address:]port "
1.164 djm 838: "Cancel remote forward");
1.237 markus 839: logit(" -KD[bind_address:]port "
840: "Cancel dynamic forward");
1.146 reyk 841: if (!options.permit_local_command)
842: goto out;
1.164 djm 843: logit(" !args "
844: "Execute local command");
1.146 reyk 845: goto out;
846: }
847:
848: if (*s == '!' && options.permit_local_command) {
849: s++;
850: ssh_local_cmd(s);
1.121 djm 851: goto out;
852: }
853:
854: if (*s == 'K') {
855: delete = 1;
856: s++;
857: }
1.204 stevesk 858: if (*s == 'L')
859: local = 1;
860: else if (*s == 'R')
861: remote = 1;
862: else if (*s == 'D')
863: dynamic = 1;
864: else {
1.109 itojun 865: logit("Invalid command.");
1.97 jakob 866: goto out;
867: }
1.204 stevesk 868:
1.256 deraadt 869: while (isspace((u_char)*++s))
1.179 tedu 870: ;
1.97 jakob 871:
1.217 djm 872: /* XXX update list of forwards in options */
1.121 djm 873: if (delete) {
1.261 millert 874: /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
875: if (!parse_forward(&fwd, s, 1, 0)) {
876: logit("Bad forwarding close specification.");
1.121 djm 877: goto out;
878: }
1.237 markus 879: if (remote)
1.303 djm 880: ok = channel_request_rforward_cancel(ssh, &fwd) == 0;
1.237 markus 881: else if (dynamic)
1.303 djm 882: ok = channel_cancel_lport_listener(ssh, &fwd,
1.261 millert 883: 0, &options.fwd_opts) > 0;
1.237 markus 884: else
1.303 djm 885: ok = channel_cancel_lport_listener(ssh, &fwd,
1.261 millert 886: CHANNEL_CANCEL_PORT_STATIC,
887: &options.fwd_opts) > 0;
1.237 markus 888: if (!ok) {
1.290 dtucker 889: logit("Unknown port forwarding.");
1.237 markus 890: goto out;
891: }
892: logit("Canceled forwarding.");
1.121 djm 893: } else {
1.382 dtucker 894: /* -R specs can be both dynamic or not, so check both. */
895: if (remote) {
896: if (!parse_forward(&fwd, s, 0, remote) &&
897: !parse_forward(&fwd, s, 1, remote)) {
898: logit("Bad remote forwarding specification.");
899: goto out;
900: }
901: } else if (!parse_forward(&fwd, s, dynamic, remote)) {
902: logit("Bad local forwarding specification.");
1.121 djm 903: goto out;
904: }
1.204 stevesk 905: if (local || dynamic) {
1.303 djm 906: if (!channel_setup_local_fwd_listener(ssh, &fwd,
1.261 millert 907: &options.fwd_opts)) {
1.121 djm 908: logit("Port forwarding failed.");
909: goto out;
910: }
1.135 djm 911: } else {
1.303 djm 912: if (channel_request_remote_forwarding(ssh, &fwd) < 0) {
1.167 markus 913: logit("Port forwarding failed.");
914: goto out;
915: }
1.135 djm 916: }
1.121 djm 917: logit("Forwarding port.");
918: }
919:
1.97 jakob 920: out:
1.332 dtucker 921: ssh_signal(SIGINT, handler);
1.233 djm 922: enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.250 djm 923: free(cmd);
924: free(fwd.listen_host);
1.261 millert 925: free(fwd.listen_path);
1.250 djm 926: free(fwd.connect_host);
1.261 millert 927: free(fwd.connect_path);
1.97 jakob 928: }
929:
1.244 dtucker 930: /* reasons to suppress output of an escape command in help output */
931: #define SUPPRESS_NEVER 0 /* never suppress, always show */
1.292 djm 932: #define SUPPRESS_MUXCLIENT 1 /* don't show in mux client sessions */
933: #define SUPPRESS_MUXMASTER 2 /* don't show in mux master sessions */
934: #define SUPPRESS_SYSLOG 4 /* don't show when logging to syslog */
1.383 djm 935: #define SUPPRESS_NOCMDLINE 8 /* don't show when cmdline disabled*/
1.244 dtucker 936: struct escape_help_text {
937: const char *cmd;
938: const char *text;
939: unsigned int flags;
940: };
941: static struct escape_help_text esc_txt[] = {
942: {".", "terminate session", SUPPRESS_MUXMASTER},
943: {".", "terminate connection (and any multiplexed sessions)",
944: SUPPRESS_MUXCLIENT},
1.292 djm 945: {"B", "send a BREAK to the remote system", SUPPRESS_NEVER},
1.383 djm 946: {"C", "open a command line", SUPPRESS_MUXCLIENT|SUPPRESS_NOCMDLINE},
1.292 djm 947: {"R", "request rekey", SUPPRESS_NEVER},
1.246 dtucker 948: {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
1.244 dtucker 949: {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
950: {"#", "list forwarded connections", SUPPRESS_NEVER},
951: {"&", "background ssh (when waiting for connections to terminate)",
952: SUPPRESS_MUXCLIENT},
953: {"?", "this message", SUPPRESS_NEVER},
954: };
955:
956: static void
1.315 markus 957: print_escape_help(struct sshbuf *b, int escape_char, int mux_client,
958: int using_stderr)
1.244 dtucker 959: {
960: unsigned int i, suppress_flags;
1.315 markus 961: int r;
1.244 dtucker 962:
1.315 markus 963: if ((r = sshbuf_putf(b,
964: "%c?\r\nSupported escape sequences:\r\n", escape_char)) != 0)
1.354 djm 965: fatal_fr(r, "sshbuf_putf");
1.244 dtucker 966:
1.292 djm 967: suppress_flags =
1.244 dtucker 968: (mux_client ? SUPPRESS_MUXCLIENT : 0) |
969: (mux_client ? 0 : SUPPRESS_MUXMASTER) |
1.383 djm 970: (using_stderr ? 0 : SUPPRESS_SYSLOG) |
971: (options.enable_escape_commandline == 0 ? SUPPRESS_NOCMDLINE : 0);
1.244 dtucker 972:
973: for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
974: if (esc_txt[i].flags & suppress_flags)
975: continue;
1.315 markus 976: if ((r = sshbuf_putf(b, " %c%-3s - %s\r\n",
977: escape_char, esc_txt[i].cmd, esc_txt[i].text)) != 0)
1.354 djm 978: fatal_fr(r, "sshbuf_putf");
1.244 dtucker 979: }
980:
1.315 markus 981: if ((r = sshbuf_putf(b,
1.246 dtucker 982: " %c%c - send the escape character by typing it twice\r\n"
1.244 dtucker 983: "(Note that escapes are only recognized immediately after "
1.315 markus 984: "newline.)\r\n", escape_char, escape_char)) != 0)
1.354 djm 985: fatal_fr(r, "sshbuf_putf");
1.244 dtucker 986: }
987:
1.314 djm 988: /*
1.296 naddy 989: * Process the characters one by one.
1.195 djm 990: */
1.77 itojun 991: static int
1.303 djm 992: process_escapes(struct ssh *ssh, Channel *c,
1.315 markus 993: struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr,
1.195 djm 994: char *buf, int len)
1.31 markus 995: {
996: pid_t pid;
1.315 markus 997: int r, bytes = 0;
1.42 markus 998: u_int i;
999: u_char ch;
1.31 markus 1000: char *s;
1.294 djm 1001: struct escape_filter_ctx *efc = c->filter_ctx == NULL ?
1002: NULL : (struct escape_filter_ctx *)c->filter_ctx;
1.31 markus 1003:
1.294 djm 1004: if (c->filter_ctx == NULL)
1005: return 0;
1.314 djm 1006:
1.139 djm 1007: if (len <= 0)
1008: return (0);
1009:
1010: for (i = 0; i < (u_int)len; i++) {
1.31 markus 1011: /* Get one character at a time. */
1012: ch = buf[i];
1013:
1.294 djm 1014: if (efc->escape_pending) {
1.31 markus 1015: /* We have previously seen an escape character. */
1016: /* Clear the flag now. */
1.294 djm 1017: efc->escape_pending = 0;
1.31 markus 1018:
1019: /* Process the escaped character. */
1020: switch (ch) {
1021: case '.':
1022: /* Terminate the connection. */
1.315 markus 1023: if ((r = sshbuf_putf(berr, "%c.\r\n",
1024: efc->escape_char)) != 0)
1.354 djm 1025: fatal_fr(r, "sshbuf_putf");
1.217 djm 1026: if (c && c->ctl_chan != -1) {
1.303 djm 1027: chan_read_failed(ssh, c);
1028: chan_write_failed(ssh, c);
1029: if (c->detach_user) {
1030: c->detach_user(ssh,
1031: c->self, NULL);
1032: }
1.253 dtucker 1033: c->type = SSH_CHANNEL_ABANDONED;
1.315 markus 1034: sshbuf_reset(c->input);
1.303 djm 1035: chan_ibuf_empty(ssh, c);
1.195 djm 1036: return 0;
1037: } else
1038: quit_pending = 1;
1.31 markus 1039: return -1;
1040:
1041: case 'Z' - 64:
1.195 djm 1042: /* XXX support this for mux clients */
1.217 djm 1043: if (c && c->ctl_chan != -1) {
1.245 dtucker 1044: char b[16];
1.195 djm 1045: noescape:
1.245 dtucker 1046: if (ch == 'Z' - 64)
1047: snprintf(b, sizeof b, "^Z");
1048: else
1049: snprintf(b, sizeof b, "%c", ch);
1.315 markus 1050: if ((r = sshbuf_putf(berr,
1.245 dtucker 1051: "%c%s escape not available to "
1.195 djm 1052: "multiplexed sessions\r\n",
1.315 markus 1053: efc->escape_char, b)) != 0)
1.354 djm 1054: fatal_fr(r, "sshbuf_putf");
1.195 djm 1055: continue;
1056: }
1.197 djm 1057: /* Suspend the program. Inform the user */
1.315 markus 1058: if ((r = sshbuf_putf(berr,
1059: "%c^Z [suspend ssh]\r\n",
1060: efc->escape_char)) != 0)
1.354 djm 1061: fatal_fr(r, "sshbuf_putf");
1.31 markus 1062:
1063: /* Restore terminal modes and suspend. */
1064: client_suspend_self(bin, bout, berr);
1065:
1066: /* We have been continued. */
1067: continue;
1068:
1.111 markus 1069: case 'B':
1.315 markus 1070: if ((r = sshbuf_putf(berr,
1071: "%cB\r\n", efc->escape_char)) != 0)
1.354 djm 1072: fatal_fr(r, "sshbuf_putf");
1.303 djm 1073: channel_request_start(ssh, c->self, "break", 0);
1.315 markus 1074: if ((r = sshpkt_put_u32(ssh, 1000)) != 0 ||
1075: (r = sshpkt_send(ssh)) != 0)
1.354 djm 1076: fatal_fr(r, "send packet");
1.111 markus 1077: continue;
1078:
1.54 markus 1079: case 'R':
1.357 djm 1080: if (ssh->compat & SSH_BUG_NOREKEY)
1.292 djm 1081: logit("Server does not "
1082: "support re-keying");
1083: else
1084: need_rekeying = 1;
1.54 markus 1085: continue;
1086:
1.242 dtucker 1087: case 'V':
1088: /* FALLTHROUGH */
1089: case 'v':
1090: if (c && c->ctl_chan != -1)
1091: goto noescape;
1092: if (!log_is_on_stderr()) {
1.315 markus 1093: if ((r = sshbuf_putf(berr,
1.242 dtucker 1094: "%c%c [Logging to syslog]\r\n",
1.315 markus 1095: efc->escape_char, ch)) != 0)
1.354 djm 1096: fatal_fr(r, "sshbuf_putf");
1.242 dtucker 1097: continue;
1098: }
1099: if (ch == 'V' && options.log_level >
1100: SYSLOG_LEVEL_QUIET)
1101: log_change_level(--options.log_level);
1102: if (ch == 'v' && options.log_level <
1103: SYSLOG_LEVEL_DEBUG3)
1104: log_change_level(++options.log_level);
1.315 markus 1105: if ((r = sshbuf_putf(berr,
1.294 djm 1106: "%c%c [LogLevel %s]\r\n",
1107: efc->escape_char, ch,
1.315 markus 1108: log_level_name(options.log_level))) != 0)
1.354 djm 1109: fatal_fr(r, "sshbuf_putf");
1.242 dtucker 1110: continue;
1111:
1.31 markus 1112: case '&':
1.217 djm 1113: if (c && c->ctl_chan != -1)
1.195 djm 1114: goto noescape;
1.31 markus 1115: /*
1.197 djm 1116: * Detach the program (continue to serve
1117: * connections, but put in background and no
1118: * more new connections).
1.31 markus 1119: */
1120: /* Restore tty modes. */
1.233 djm 1121: leave_raw_mode(
1122: options.request_tty == REQUEST_TTY_FORCE);
1.31 markus 1123:
1124: /* Stop listening for new connections. */
1.303 djm 1125: channel_stop_listening(ssh);
1.31 markus 1126:
1.354 djm 1127: if ((r = sshbuf_putf(berr, "%c& "
1128: "[backgrounded]\n", efc->escape_char)) != 0)
1129: fatal_fr(r, "sshbuf_putf");
1.31 markus 1130:
1131: /* Fork into background. */
1132: pid = fork();
1.326 deraadt 1133: if (pid == -1) {
1.31 markus 1134: error("fork: %.100s", strerror(errno));
1135: continue;
1136: }
1137: if (pid != 0) { /* This is the parent. */
1138: /* The parent just exits. */
1139: exit(0);
1140: }
1141: /* The child continues serving connections. */
1.292 djm 1142: /* fake EOF on stdin */
1.315 markus 1143: if ((r = sshbuf_put_u8(bin, 4)) != 0)
1.354 djm 1144: fatal_fr(r, "sshbuf_put_u8");
1.292 djm 1145: return -1;
1.31 markus 1146: case '?':
1.294 djm 1147: print_escape_help(berr, efc->escape_char,
1.244 dtucker 1148: (c && c->ctl_chan != -1),
1149: log_is_on_stderr());
1.31 markus 1150: continue;
1151:
1152: case '#':
1.315 markus 1153: if ((r = sshbuf_putf(berr, "%c#\r\n",
1154: efc->escape_char)) != 0)
1.354 djm 1155: fatal_fr(r, "sshbuf_putf");
1.303 djm 1156: s = channel_open_message(ssh);
1.315 markus 1157: if ((r = sshbuf_put(berr, s, strlen(s))) != 0)
1.354 djm 1158: fatal_fr(r, "sshbuf_put");
1.250 djm 1159: free(s);
1.97 jakob 1160: continue;
1161:
1162: case 'C':
1.217 djm 1163: if (c && c->ctl_chan != -1)
1.206 djm 1164: goto noescape;
1.383 djm 1165: if (options.enable_escape_commandline == 0) {
1166: if ((r = sshbuf_putf(berr,
1167: "commandline disabled\r\n")) != 0)
1168: fatal_fr(r, "sshbuf_putf");
1169: continue;
1170: }
1.303 djm 1171: process_cmdline(ssh);
1.31 markus 1172: continue;
1173:
1174: default:
1.294 djm 1175: if (ch != efc->escape_char) {
1.315 markus 1176: if ((r = sshbuf_put_u8(bin,
1177: efc->escape_char)) != 0)
1.354 djm 1178: fatal_fr(r, "sshbuf_put_u8");
1.31 markus 1179: bytes++;
1180: }
1181: /* Escaped characters fall through here */
1182: break;
1183: }
1184: } else {
1185: /*
1.197 djm 1186: * The previous character was not an escape char.
1187: * Check if this is an escape.
1.31 markus 1188: */
1.294 djm 1189: if (last_was_cr && ch == efc->escape_char) {
1.197 djm 1190: /*
1191: * It is. Set the flag and continue to
1192: * next character.
1193: */
1.294 djm 1194: efc->escape_pending = 1;
1.31 markus 1195: continue;
1196: }
1197: }
1198:
1199: /*
1200: * Normal character. Record whether it was a newline,
1201: * and append it to the buffer.
1202: */
1203: last_was_cr = (ch == '\r' || ch == '\n');
1.315 markus 1204: if ((r = sshbuf_put_u8(bin, ch)) != 0)
1.354 djm 1205: fatal_fr(r, "sshbuf_put_u8");
1.31 markus 1206: bytes++;
1207: }
1208: return bytes;
1209: }
1210:
1.13 markus 1211: /*
1.15 markus 1212: * Get packets from the connection input buffer, and process them as long as
1213: * there are packets available.
1214: *
1215: * Any unknown packets received during the actual
1216: * session cause the session to terminate. This is
1217: * intended to make debugging easier since no
1218: * confirmations are sent. Any compatible protocol
1219: * extensions must be negotiated during the
1220: * preparatory phase.
1221: */
1222:
1.77 itojun 1223: static void
1.321 djm 1224: client_process_buffered_input_packets(struct ssh *ssh)
1.15 markus 1225: {
1.321 djm 1226: ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, &quit_pending);
1.15 markus 1227: }
1228:
1.31 markus 1229: /* scan buf[] for '~' before sending data to the peer */
1.30 markus 1230:
1.195 djm 1231: /* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1232: void *
1233: client_new_escape_filter_ctx(int escape_char)
1234: {
1235: struct escape_filter_ctx *ret;
1236:
1.255 djm 1237: ret = xcalloc(1, sizeof(*ret));
1.195 djm 1238: ret->escape_pending = 0;
1239: ret->escape_char = escape_char;
1240: return (void *)ret;
1241: }
1242:
1.198 djm 1243: /* Free the escape filter context on channel free */
1244: void
1.303 djm 1245: client_filter_cleanup(struct ssh *ssh, int cid, void *ctx)
1.198 djm 1246: {
1.250 djm 1247: free(ctx);
1.198 djm 1248: }
1249:
1.195 djm 1250: int
1.303 djm 1251: client_simple_escape_filter(struct ssh *ssh, Channel *c, char *buf, int len)
1.30 markus 1252: {
1.190 djm 1253: if (c->extended_usage != CHAN_EXTENDED_WRITE)
1254: return 0;
1255:
1.303 djm 1256: return process_escapes(ssh, c, c->input, c->output, c->extended,
1.195 djm 1257: buf, len);
1.30 markus 1258: }
1259:
1.77 itojun 1260: static void
1.303 djm 1261: client_channel_closed(struct ssh *ssh, int id, void *arg)
1.60 markus 1262: {
1.303 djm 1263: channel_cancel_cleanup(ssh, id);
1.60 markus 1264: session_closed = 1;
1.233 djm 1265: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.60 markus 1266: }
1267:
1.15 markus 1268: /*
1.13 markus 1269: * Implements the interactive session with the server. This is called after
1270: * the user has been authenticated, and a command has been started on the
1.72 stevesk 1271: * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1272: * used as an escape character for terminating or suspending the session.
1.13 markus 1273: */
1.20 markus 1274: int
1.303 djm 1275: client_loop(struct ssh *ssh, int have_pty, int escape_char_arg,
1276: int ssh2_chan_id)
1.1 deraadt 1277: {
1.374 djm 1278: struct pollfd *pfd = NULL;
1279: u_int npfd_alloc = 0, npfd_active = 0;
1.11 markus 1280: double start_time, total_time;
1.374 djm 1281: int r, len;
1.200 markus 1282: u_int64_t ibytes, obytes;
1.374 djm 1283: int conn_in_ready, conn_out_ready;
1.11 markus 1284:
1285: debug("Entering interactive session.");
1.384 djm 1286: session_ident = ssh2_chan_id;
1.277 semarie 1287:
1.278 semarie 1288: if (options.control_master &&
1.286 djm 1289: !option_clear_or_none(options.control_path)) {
1.278 semarie 1290: debug("pledge: id");
1.327 mestre 1291: if (pledge("stdio rpath wpath cpath unix inet dns recvfd sendfd proc exec id tty",
1.278 semarie 1292: NULL) == -1)
1.354 djm 1293: fatal_f("pledge(): %s", strerror(errno));
1.278 semarie 1294:
1295: } else if (options.forward_x11 || options.permit_local_command) {
1.277 semarie 1296: debug("pledge: exec");
1297: if (pledge("stdio rpath wpath cpath unix inet dns proc exec tty",
1298: NULL) == -1)
1.354 djm 1299: fatal_f("pledge(): %s", strerror(errno));
1.277 semarie 1300:
1301: } else if (options.update_hostkeys) {
1.373 jsg 1302: debug("pledge: filesystem");
1.277 semarie 1303: if (pledge("stdio rpath wpath cpath unix inet dns proc tty",
1304: NULL) == -1)
1.354 djm 1305: fatal_f("pledge(): %s", strerror(errno));
1.277 semarie 1306:
1.286 djm 1307: } else if (!option_clear_or_none(options.proxy_command) ||
1.369 djm 1308: options.fork_after_authentication) {
1.277 semarie 1309: debug("pledge: proc");
1310: if (pledge("stdio cpath unix inet dns proc tty", NULL) == -1)
1.354 djm 1311: fatal_f("pledge(): %s", strerror(errno));
1.277 semarie 1312:
1313: } else {
1314: debug("pledge: network");
1.300 mestre 1315: if (pledge("stdio unix inet dns proc tty", NULL) == -1)
1.354 djm 1316: fatal_f("pledge(): %s", strerror(errno));
1.277 semarie 1317: }
1.11 markus 1318:
1.384 djm 1319: /* might be able to tighten now */
1320: client_repledge();
1321:
1.307 dtucker 1322: start_time = monotime_double();
1.11 markus 1323:
1324: /* Initialize variables. */
1325: last_was_cr = 1;
1326: exit_status = -1;
1.320 djm 1327: connection_in = ssh_packet_get_connection_in(ssh);
1328: connection_out = ssh_packet_get_connection_out(ssh);
1.46 markus 1329:
1.11 markus 1330: quit_pending = 0;
1331:
1.315 markus 1332: /* Initialize buffer. */
1333: if ((stderr_buffer = sshbuf_new()) == NULL)
1.354 djm 1334: fatal_f("sshbuf_new failed");
1.11 markus 1335:
1.321 djm 1336: client_init_dispatch(ssh);
1.15 markus 1337:
1.105 markus 1338: /*
1339: * Set signal handlers, (e.g. to restore non-blocking mode)
1340: * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1341: */
1.332 dtucker 1342: if (ssh_signal(SIGHUP, SIG_IGN) != SIG_IGN)
1343: ssh_signal(SIGHUP, signal_handler);
1344: if (ssh_signal(SIGINT, SIG_IGN) != SIG_IGN)
1345: ssh_signal(SIGINT, signal_handler);
1346: if (ssh_signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1347: ssh_signal(SIGQUIT, signal_handler);
1348: if (ssh_signal(SIGTERM, SIG_IGN) != SIG_IGN)
1349: ssh_signal(SIGTERM, signal_handler);
1350: ssh_signal(SIGWINCH, window_change_handler);
1.11 markus 1351:
1352: if (have_pty)
1.233 djm 1353: enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.11 markus 1354:
1.292 djm 1355: if (session_ident != -1) {
1356: if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
1.303 djm 1357: channel_register_filter(ssh, session_ident,
1.292 djm 1358: client_simple_escape_filter, NULL,
1359: client_filter_cleanup,
1360: client_new_escape_filter_ctx(
1361: escape_char_arg));
1.232 djm 1362: }
1.303 djm 1363: channel_register_cleanup(ssh, session_ident,
1.292 djm 1364: client_channel_closed, 0);
1.48 markus 1365: }
1.345 dtucker 1366:
1367: schedule_server_alive_check();
1.30 markus 1368:
1.11 markus 1369: /* Main loop of the client for the interactive session mode. */
1370: while (!quit_pending) {
1371:
1.13 markus 1372: /* Process buffered packets sent by the server. */
1.321 djm 1373: client_process_buffered_input_packets(ssh);
1.11 markus 1374:
1.303 djm 1375: if (session_closed && !channel_still_open(ssh))
1.60 markus 1376: break;
1377:
1.303 djm 1378: if (ssh_packet_is_rekeying(ssh)) {
1.56 markus 1379: debug("rekeying in progress");
1.284 djm 1380: } else if (need_rekeying) {
1381: /* manual rekey request */
1382: debug("need rekeying");
1.303 djm 1383: if ((r = kex_start_rekex(ssh)) != 0)
1.354 djm 1384: fatal_fr(r, "kex_start_rekex");
1.284 djm 1385: need_rekeying = 0;
1.56 markus 1386: } else {
1387: /*
1388: * Make packets from buffered channel data, and
1389: * enqueue them for sending to the server.
1390: */
1.320 djm 1391: if (ssh_packet_not_very_much_data_to_write(ssh))
1.303 djm 1392: channel_output_poll(ssh);
1.11 markus 1393:
1.56 markus 1394: /*
1395: * Check if the window size has changed, and buffer a
1396: * message about it to the server if so.
1397: */
1.303 djm 1398: client_check_window_change(ssh);
1.11 markus 1399:
1.56 markus 1400: if (quit_pending)
1401: break;
1402: }
1.13 markus 1403: /*
1404: * Wait until we have something to do (something becomes
1405: * available on one of the descriptors).
1406: */
1.374 djm 1407: client_wait_until_can_do_something(ssh, &pfd, &npfd_alloc,
1408: &npfd_active, ssh_packet_is_rekeying(ssh),
1409: &conn_in_ready, &conn_out_ready);
1.11 markus 1410:
1411: if (quit_pending)
1412: break;
1413:
1.379 djm 1414: /* Do channel operations. */
1415: channel_after_poll(ssh, pfd, npfd_active);
1.11 markus 1416:
1.17 markus 1417: /* Buffer input from the connection. */
1.374 djm 1418: if (conn_in_ready)
1419: client_process_net_input(ssh);
1.17 markus 1420:
1421: if (quit_pending)
1422: break;
1.367 djm 1423:
1424: /* A timeout may have triggered rekeying */
1425: if ((r = ssh_packet_check_rekey(ssh)) != 0)
1426: fatal_fr(r, "cannot start rekeying");
1.11 markus 1427:
1.197 djm 1428: /*
1429: * Send as much buffered packet data as possible to the
1430: * sender.
1431: */
1.374 djm 1432: if (conn_out_ready) {
1.338 djm 1433: if ((r = ssh_packet_write_poll(ssh)) != 0) {
1434: sshpkt_fatal(ssh, r,
1435: "%s: ssh_packet_write_poll", __func__);
1436: }
1437: }
1.222 djm 1438:
1439: /*
1440: * If we are a backgrounded control master, and the
1441: * timeout has expired without any active client
1442: * connections, then quit.
1443: */
1444: if (control_persist_exit_time > 0) {
1.251 dtucker 1445: if (monotime() >= control_persist_exit_time) {
1.222 djm 1446: debug("ControlPersist timeout expired");
1447: break;
1448: }
1449: }
1.11 markus 1450: }
1.374 djm 1451: free(pfd);
1.11 markus 1452:
1453: /* Terminate the session. */
1454:
1455: /* Stop watching for window change. */
1.332 dtucker 1456: ssh_signal(SIGWINCH, SIG_DFL);
1.211 andreas 1457:
1.320 djm 1458: if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 ||
1459: (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_BY_APPLICATION)) != 0 ||
1460: (r = sshpkt_put_cstring(ssh, "disconnected by user")) != 0 ||
1461: (r = sshpkt_put_cstring(ssh, "")) != 0 || /* language tag */
1462: (r = sshpkt_send(ssh)) != 0 ||
1463: (r = ssh_packet_write_wait(ssh)) != 0)
1.354 djm 1464: fatal_fr(r, "send disconnect");
1.11 markus 1465:
1.303 djm 1466: channel_free_all(ssh);
1.11 markus 1467:
1.75 markus 1468: if (have_pty)
1.233 djm 1469: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.116 dtucker 1470:
1471: /*
1472: * If there was no shell or command requested, there will be no remote
1473: * exit status to be returned. In that case, clear error code if the
1474: * connection was deliberately terminated at this end.
1475: */
1.370 djm 1476: if (options.session_type == SESSION_TYPE_NONE &&
1477: received_signal == SIGTERM) {
1.116 dtucker 1478: received_signal = 0;
1479: exit_status = 0;
1480: }
1.75 markus 1481:
1.301 dtucker 1482: if (received_signal) {
1483: verbose("Killed by signal %d.", (int) received_signal);
1.359 djm 1484: cleanup_exit(255);
1.301 dtucker 1485: }
1.75 markus 1486:
1.13 markus 1487: /*
1488: * In interactive mode (with pseudo tty) display a message indicating
1489: * that the connection has been closed.
1490: */
1.377 djm 1491: if (have_pty && options.log_level >= SYSLOG_LEVEL_INFO)
1492: quit_message("Connection to %s closed.", host);
1.70 markus 1493:
1.11 markus 1494: /* Output any buffered data for stderr. */
1.315 markus 1495: if (sshbuf_len(stderr_buffer) > 0) {
1.230 djm 1496: len = atomicio(vwrite, fileno(stderr),
1.315 markus 1497: (u_char *)sshbuf_ptr(stderr_buffer),
1498: sshbuf_len(stderr_buffer));
1499: if (len < 0 || (u_int)len != sshbuf_len(stderr_buffer))
1.11 markus 1500: error("Write failed flushing stderr buffer.");
1.315 markus 1501: else if ((r = sshbuf_consume(stderr_buffer, len)) != 0)
1.354 djm 1502: fatal_fr(r, "sshbuf_consume");
1.11 markus 1503: }
1504:
1505: /* Clear and free any buffers. */
1.315 markus 1506: sshbuf_free(stderr_buffer);
1.11 markus 1507:
1508: /* Report bytes transferred, and transfer rates. */
1.307 dtucker 1509: total_time = monotime_double() - start_time;
1.320 djm 1510: ssh_packet_get_bytes(ssh, &ibytes, &obytes);
1.200 markus 1511: verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
1.229 djm 1512: (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
1.11 markus 1513: if (total_time > 0)
1.200 markus 1514: verbose("Bytes per second: sent %.1f, received %.1f",
1515: obytes / total_time, ibytes / total_time);
1.11 markus 1516: /* Return the exit status of the program. */
1517: debug("Exit status %d", exit_status);
1518: return exit_status;
1.15 markus 1519: }
1520:
1521: /*********/
1522:
1.77 itojun 1523: static Channel *
1.303 djm 1524: client_request_forwarded_tcpip(struct ssh *ssh, const char *request_type,
1525: int rchan, u_int rwindow, u_int rmaxpack)
1.40 markus 1526: {
1.103 deraadt 1527: Channel *c = NULL;
1.289 markus 1528: struct sshbuf *b = NULL;
1.40 markus 1529: char *listen_address, *originator_address;
1.320 djm 1530: u_int listen_port, originator_port;
1.289 markus 1531: int r;
1.40 markus 1532:
1533: /* Get rest of the packet */
1.320 djm 1534: if ((r = sshpkt_get_cstring(ssh, &listen_address, NULL)) != 0 ||
1535: (r = sshpkt_get_u32(ssh, &listen_port)) != 0 ||
1536: (r = sshpkt_get_cstring(ssh, &originator_address, NULL)) != 0 ||
1537: (r = sshpkt_get_u32(ssh, &originator_port)) != 0 ||
1538: (r = sshpkt_get_end(ssh)) != 0)
1.354 djm 1539: fatal_fr(r, "parse packet");
1.40 markus 1540:
1.354 djm 1541: debug_f("listen %s port %d, originator %s port %d",
1.261 millert 1542: listen_address, listen_port, originator_address, originator_port);
1.191 djm 1543:
1.320 djm 1544: if (listen_port > 0xffff)
1.354 djm 1545: error_f("invalid listen port");
1.320 djm 1546: else if (originator_port > 0xffff)
1.354 djm 1547: error_f("invalid originator port");
1.320 djm 1548: else {
1549: c = channel_connect_by_listen_address(ssh,
1550: listen_address, listen_port, "forwarded-tcpip",
1551: originator_address);
1552: }
1.40 markus 1553:
1.289 markus 1554: if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1555: if ((b = sshbuf_new()) == NULL) {
1.354 djm 1556: error_f("alloc reply");
1.289 markus 1557: goto out;
1558: }
1559: /* reconstruct and send to muxclient */
1560: if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
1561: (r = sshbuf_put_u8(b, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1562: (r = sshbuf_put_cstring(b, request_type)) != 0 ||
1563: (r = sshbuf_put_u32(b, rchan)) != 0 ||
1564: (r = sshbuf_put_u32(b, rwindow)) != 0 ||
1565: (r = sshbuf_put_u32(b, rmaxpack)) != 0 ||
1566: (r = sshbuf_put_cstring(b, listen_address)) != 0 ||
1567: (r = sshbuf_put_u32(b, listen_port)) != 0 ||
1568: (r = sshbuf_put_cstring(b, originator_address)) != 0 ||
1569: (r = sshbuf_put_u32(b, originator_port)) != 0 ||
1.303 djm 1570: (r = sshbuf_put_stringb(c->output, b)) != 0) {
1.354 djm 1571: error_fr(r, "compose for muxclient");
1.289 markus 1572: goto out;
1573: }
1574: }
1575:
1576: out:
1577: sshbuf_free(b);
1.250 djm 1578: free(originator_address);
1579: free(listen_address);
1.40 markus 1580: return c;
1581: }
1582:
1.103 deraadt 1583: static Channel *
1.303 djm 1584: client_request_forwarded_streamlocal(struct ssh *ssh,
1585: const char *request_type, int rchan)
1.261 millert 1586: {
1587: Channel *c = NULL;
1588: char *listen_path;
1.320 djm 1589: int r;
1.261 millert 1590:
1591: /* Get the remote path. */
1.320 djm 1592: if ((r = sshpkt_get_cstring(ssh, &listen_path, NULL)) != 0 ||
1593: (r = sshpkt_get_string(ssh, NULL, NULL)) != 0 || /* reserved */
1594: (r = sshpkt_get_end(ssh)) != 0)
1.354 djm 1595: fatal_fr(r, "parse packet");
1.261 millert 1596:
1.354 djm 1597: debug_f("request: %s", listen_path);
1.261 millert 1598:
1.303 djm 1599: c = channel_connect_by_listen_path(ssh, listen_path,
1.261 millert 1600: "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1601: free(listen_path);
1602: return c;
1603: }
1604:
1605: static Channel *
1.303 djm 1606: client_request_x11(struct ssh *ssh, const char *request_type, int rchan)
1.40 markus 1607: {
1608: Channel *c = NULL;
1609: char *originator;
1.321 djm 1610: u_int originator_port;
1.320 djm 1611: int r, sock;
1.40 markus 1612:
1613: if (!options.forward_x11) {
1614: error("Warning: ssh server tried X11 forwarding.");
1.197 djm 1615: error("Warning: this is probably a break-in attempt by a "
1616: "malicious server.");
1.221 djm 1617: return NULL;
1618: }
1.274 djm 1619: if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
1.221 djm 1620: verbose("Rejected X11 connection after ForwardX11Timeout "
1621: "expired");
1.40 markus 1622: return NULL;
1623: }
1.320 djm 1624: if ((r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 ||
1.321 djm 1625: (r = sshpkt_get_u32(ssh, &originator_port)) != 0 ||
1.320 djm 1626: (r = sshpkt_get_end(ssh)) != 0)
1.354 djm 1627: fatal_fr(r, "parse packet");
1.40 markus 1628: /* XXX check permission */
1.321 djm 1629: /* XXX range check originator port? */
1630: debug("client_request_x11: request from %s %u", originator,
1.47 markus 1631: originator_port);
1.250 djm 1632: free(originator);
1.303 djm 1633: sock = x11_connect_display(ssh);
1.67 markus 1634: if (sock < 0)
1635: return NULL;
1.303 djm 1636: c = channel_new(ssh, "x11",
1.67 markus 1637: SSH_CHANNEL_X11_OPEN, sock, sock, -1,
1.110 markus 1638: CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
1.82 markus 1639: c->force_drain = 1;
1.40 markus 1640: return c;
1641: }
1642:
1.103 deraadt 1643: static Channel *
1.303 djm 1644: client_request_agent(struct ssh *ssh, const char *request_type, int rchan)
1.40 markus 1645: {
1646: Channel *c = NULL;
1.262 djm 1647: int r, sock;
1.40 markus 1648:
1649: if (!options.forward_agent) {
1650: error("Warning: ssh server tried agent forwarding.");
1.197 djm 1651: error("Warning: this is probably a break-in attempt by a "
1652: "malicious server.");
1.40 markus 1653: return NULL;
1654: }
1.330 djm 1655: if (forward_agent_sock_path == NULL) {
1656: r = ssh_get_authentication_socket(&sock);
1657: } else {
1658: r = ssh_get_authentication_socket_path(forward_agent_sock_path, &sock);
1659: }
1660: if (r != 0) {
1.262 djm 1661: if (r != SSH_ERR_AGENT_NOT_PRESENT)
1.354 djm 1662: debug_fr(r, "ssh_get_authentication_socket");
1.67 markus 1663: return NULL;
1.262 djm 1664: }
1.372 djm 1665: if ((r = ssh_agent_bind_hostkey(sock, ssh->kex->initial_hostkey,
1666: ssh->kex->session_id, ssh->kex->initial_sig, 1)) == 0)
1667: debug_f("bound agent to hostkey");
1668: else
1669: debug2_fr(r, "ssh_agent_bind_hostkey");
1670:
1.303 djm 1671: c = channel_new(ssh, "authentication agent connection",
1.67 markus 1672: SSH_CHANNEL_OPEN, sock, sock, -1,
1.185 dtucker 1673: CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
1.110 markus 1674: "authentication agent connection", 1);
1.82 markus 1675: c->force_drain = 1;
1.40 markus 1676: return c;
1.180 djm 1677: }
1678:
1.306 djm 1679: char *
1.303 djm 1680: client_request_tun_fwd(struct ssh *ssh, int tun_mode,
1.343 djm 1681: int local_tun, int remote_tun, channel_open_fn *cb, void *cbctx)
1.180 djm 1682: {
1683: Channel *c;
1.320 djm 1684: int r, fd;
1.306 djm 1685: char *ifname = NULL;
1.180 djm 1686:
1687: if (tun_mode == SSH_TUNMODE_NO)
1688: return 0;
1689:
1690: debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1691:
1692: /* Open local tunnel device */
1.306 djm 1693: if ((fd = tun_open(local_tun, tun_mode, &ifname)) == -1) {
1.180 djm 1694: error("Tunnel device open failed.");
1.306 djm 1695: return NULL;
1.180 djm 1696: }
1.306 djm 1697: debug("Tunnel forwarding using interface %s", ifname);
1.180 djm 1698:
1.303 djm 1699: c = channel_new(ssh, "tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1.180 djm 1700: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1701: c->datagram = 1;
1.343 djm 1702:
1703: if (cb != NULL)
1704: channel_register_open_confirm(ssh, c->self, cb, cbctx);
1.180 djm 1705:
1.320 djm 1706: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1707: (r = sshpkt_put_cstring(ssh, "tun@openssh.com")) != 0 ||
1708: (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1709: (r = sshpkt_put_u32(ssh, c->local_window_max)) != 0 ||
1710: (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 ||
1711: (r = sshpkt_put_u32(ssh, tun_mode)) != 0 ||
1712: (r = sshpkt_put_u32(ssh, remote_tun)) != 0 ||
1713: (r = sshpkt_send(ssh)) != 0)
1.321 djm 1714: sshpkt_fatal(ssh, r, "%s: send reply", __func__);
1.180 djm 1715:
1.306 djm 1716: return ifname;
1.40 markus 1717: }
1718:
1.22 markus 1719: /* XXXX move to generic input handler */
1.264 markus 1720: static int
1.297 markus 1721: client_input_channel_open(int type, u_int32_t seq, struct ssh *ssh)
1.22 markus 1722: {
1723: Channel *c = NULL;
1.320 djm 1724: char *ctype = NULL;
1725: int r;
1726: u_int rchan;
1727: size_t len;
1728: u_int rmaxpack, rwindow;
1729:
1730: if ((r = sshpkt_get_cstring(ssh, &ctype, &len)) != 0 ||
1731: (r = sshpkt_get_u32(ssh, &rchan)) != 0 ||
1732: (r = sshpkt_get_u32(ssh, &rwindow)) != 0 ||
1733: (r = sshpkt_get_u32(ssh, &rmaxpack)) != 0)
1734: goto out;
1.22 markus 1735:
1.24 markus 1736: debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
1.22 markus 1737: ctype, rchan, rwindow, rmaxpack);
1738:
1.40 markus 1739: if (strcmp(ctype, "forwarded-tcpip") == 0) {
1.303 djm 1740: c = client_request_forwarded_tcpip(ssh, ctype, rchan, rwindow,
1.289 markus 1741: rmaxpack);
1.261 millert 1742: } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
1.303 djm 1743: c = client_request_forwarded_streamlocal(ssh, ctype, rchan);
1.40 markus 1744: } else if (strcmp(ctype, "x11") == 0) {
1.303 djm 1745: c = client_request_x11(ssh, ctype, rchan);
1.40 markus 1746: } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1.303 djm 1747: c = client_request_agent(ssh, ctype, rchan);
1.22 markus 1748: }
1.289 markus 1749: if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1750: debug3("proxied to downstream: %s", ctype);
1751: } else if (c != NULL) {
1.22 markus 1752: debug("confirm %s", ctype);
1753: c->remote_id = rchan;
1.304 djm 1754: c->have_remote_id = 1;
1.22 markus 1755: c->remote_window = rwindow;
1756: c->remote_maxpacket = rmaxpack;
1.69 markus 1757: if (c->type != SSH_CHANNEL_CONNECTING) {
1.320 djm 1758: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 ||
1759: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1760: (r = sshpkt_put_u32(ssh, c->self)) != 0 ||
1761: (r = sshpkt_put_u32(ssh, c->local_window)) != 0 ||
1762: (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 ||
1763: (r = sshpkt_send(ssh)) != 0)
1764: sshpkt_fatal(ssh, r, "%s: send reply", __func__);
1.69 markus 1765: }
1.22 markus 1766: } else {
1767: debug("failure %s", ctype);
1.320 djm 1768: if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 ||
1769: (r = sshpkt_put_u32(ssh, rchan)) != 0 ||
1770: (r = sshpkt_put_u32(ssh, SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED)) != 0 ||
1771: (r = sshpkt_put_cstring(ssh, "open failed")) != 0 ||
1772: (r = sshpkt_put_cstring(ssh, "")) != 0 ||
1773: (r = sshpkt_send(ssh)) != 0)
1774: sshpkt_fatal(ssh, r, "%s: send failure", __func__);
1.22 markus 1775: }
1.320 djm 1776: r = 0;
1777: out:
1.250 djm 1778: free(ctype);
1.320 djm 1779: return r;
1.22 markus 1780: }
1.267 djm 1781:
1.264 markus 1782: static int
1.297 markus 1783: client_input_channel_req(int type, u_int32_t seq, struct ssh *ssh)
1.48 markus 1784: {
1785: Channel *c = NULL;
1.320 djm 1786: char *rtype = NULL;
1787: u_char reply;
1788: u_int id, exitval;
1789: int r, success = 0;
1790:
1791: if ((r = sshpkt_get_u32(ssh, &id)) != 0)
1792: return r;
1793: if (id <= INT_MAX)
1794: c = channel_lookup(ssh, id);
1.297 markus 1795: if (channel_proxy_upstream(c, type, seq, ssh))
1.289 markus 1796: return 0;
1.320 djm 1797: if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 ||
1798: (r = sshpkt_get_u8(ssh, &reply)) != 0)
1799: goto out;
1.48 markus 1800:
1.320 djm 1801: debug("client_input_channel_req: channel %u rtype %s reply %d",
1.48 markus 1802: id, rtype, reply);
1803:
1.320 djm 1804: if (c == NULL) {
1.197 djm 1805: error("client_input_channel_req: channel %d: "
1806: "unknown channel", id);
1.193 markus 1807: } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1.320 djm 1808: if ((r = sshpkt_get_end(ssh)) != 0)
1809: goto out;
1.303 djm 1810: chan_rcvd_eow(ssh, c);
1.48 markus 1811: } else if (strcmp(rtype, "exit-status") == 0) {
1.320 djm 1812: if ((r = sshpkt_get_u32(ssh, &exitval)) != 0)
1813: goto out;
1.217 djm 1814: if (c->ctl_chan != -1) {
1.303 djm 1815: mux_exit_message(ssh, c, exitval);
1.215 djm 1816: success = 1;
1.320 djm 1817: } else if ((int)id == session_ident) {
1.215 djm 1818: /* Record exit value of local session */
1.123 djm 1819: success = 1;
1820: exit_status = exitval;
1.215 djm 1821: } else {
1.218 djm 1822: /* Probably for a mux channel that has already closed */
1.354 djm 1823: debug_f("no sink for exit-status on channel %d",
1824: id);
1.123 djm 1825: }
1.320 djm 1826: if ((r = sshpkt_get_end(ssh)) != 0)
1827: goto out;
1.48 markus 1828: }
1.259 djm 1829: if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
1.304 djm 1830: if (!c->have_remote_id)
1.354 djm 1831: fatal_f("channel %d: no remote_id", c->self);
1.320 djm 1832: if ((r = sshpkt_start(ssh, success ?
1833: SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE)) != 0 ||
1834: (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 ||
1835: (r = sshpkt_send(ssh)) != 0)
1836: sshpkt_fatal(ssh, r, "%s: send failure", __func__);
1.48 markus 1837: }
1.320 djm 1838: r = 0;
1839: out:
1.250 djm 1840: free(rtype);
1.320 djm 1841: return r;
1.48 markus 1842: }
1.267 djm 1843:
1.269 djm 1844: struct hostkeys_update_ctx {
1845: /* The hostname and (optionally) IP address string for the server */
1846: char *host_str, *ip_str;
1847:
1848: /*
1849: * Keys received from the server and a flag for each indicating
1850: * whether they already exist in known_hosts.
1.351 djm 1851: * keys_match is filled in by hostkeys_find() and later (for new
1.381 djm 1852: * keys) by client_global_hostkeys_prove_confirm().
1.269 djm 1853: */
1854: struct sshkey **keys;
1.351 djm 1855: u_int *keys_match; /* mask of HKF_MATCH_* from hostfile.h */
1856: int *keys_verified; /* flag for new keys verified by server */
1857: size_t nkeys, nnew, nincomplete; /* total, new keys, incomplete match */
1.269 djm 1858:
1859: /*
1860: * Keys that are in known_hosts, but were not present in the update
1861: * from the server (i.e. scheduled to be deleted).
1862: * Filled in by hostkeys_find().
1863: */
1864: struct sshkey **old_keys;
1865: size_t nold;
1.347 djm 1866:
1867: /* Various special cases. */
1.350 djm 1868: int complex_hostspec; /* wildcard or manual pattern-list host name */
1.347 djm 1869: int ca_available; /* saw CA key for this host */
1.352 djm 1870: int old_key_seen; /* saw old key with other name/addr */
1.353 djm 1871: int other_name_seen; /* saw key with other name/addr */
1.269 djm 1872: };
1873:
1874: static void
1875: hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx)
1876: {
1877: size_t i;
1878:
1879: if (ctx == NULL)
1880: return;
1881: for (i = 0; i < ctx->nkeys; i++)
1882: sshkey_free(ctx->keys[i]);
1883: free(ctx->keys);
1.351 djm 1884: free(ctx->keys_match);
1885: free(ctx->keys_verified);
1.269 djm 1886: for (i = 0; i < ctx->nold; i++)
1887: sshkey_free(ctx->old_keys[i]);
1888: free(ctx->old_keys);
1889: free(ctx->host_str);
1890: free(ctx->ip_str);
1891: free(ctx);
1892: }
1893:
1.350 djm 1894: /*
1895: * Returns non-zero if a known_hosts hostname list is not of a form that
1896: * can be handled by UpdateHostkeys. These include wildcard hostnames and
1897: * hostnames lists that do not follow the form host[,ip].
1898: */
1899: static int
1900: hostspec_is_complex(const char *hosts)
1901: {
1902: char *cp;
1903:
1904: /* wildcard */
1905: if (strchr(hosts, '*') != NULL || strchr(hosts, '?') != NULL)
1906: return 1;
1907: /* single host/ip = ok */
1908: if ((cp = strchr(hosts, ',')) == NULL)
1909: return 0;
1910: /* more than two entries on the line */
1911: if (strchr(cp + 1, ',') != NULL)
1912: return 1;
1913: /* XXX maybe parse cp+1 and ensure it is an IP? */
1914: return 0;
1915: }
1916:
1.352 djm 1917: /* callback to search for ctx->keys in known_hosts */
1.269 djm 1918: static int
1919: hostkeys_find(struct hostkey_foreach_line *l, void *_ctx)
1920: {
1921: struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1922: size_t i;
1923: struct sshkey **tmp;
1924:
1.353 djm 1925: if (l->key == NULL)
1.269 djm 1926: return 0;
1.353 djm 1927: if (l->status != HKF_STATUS_MATCHED) {
1928: /* Record if one of the keys appears on a non-matching line */
1929: for (i = 0; i < ctx->nkeys; i++) {
1930: if (sshkey_equal(l->key, ctx->keys[i])) {
1931: ctx->other_name_seen = 1;
1.354 djm 1932: debug3_f("found %s key under different "
1933: "name/addr at %s:%ld",
1.353 djm 1934: sshkey_ssh_name(ctx->keys[i]),
1935: l->path, l->linenum);
1936: return 0;
1937: }
1938: }
1939: return 0;
1940: }
1941: /* Don't proceed if revocation or CA markers are present */
1942: /* XXX relax this */
1943: if (l->marker != MRK_NONE) {
1.354 djm 1944: debug3_f("hostkeys file %s:%ld has CA/revocation marker",
1945: l->path, l->linenum);
1.353 djm 1946: ctx->complex_hostspec = 1;
1947: return 0;
1948: }
1949:
1.355 djm 1950: /* If CheckHostIP is enabled, then check for mismatched hostname/addr */
1951: if (ctx->ip_str != NULL && strchr(l->hosts, ',') != NULL) {
1952: if ((l->match & HKF_MATCH_HOST) == 0) {
1953: /* Record if address matched a different hostname. */
1954: ctx->other_name_seen = 1;
1955: debug3_f("found address %s against different hostname "
1956: "at %s:%ld", ctx->ip_str, l->path, l->linenum);
1957: return 0;
1958: } else if ((l->match & HKF_MATCH_IP) == 0) {
1959: /* Record if hostname matched a different address. */
1960: ctx->other_name_seen = 1;
1961: debug3_f("found hostname %s against different address "
1962: "at %s:%ld", ctx->host_str, l->path, l->linenum);
1963: }
1.353 djm 1964: }
1.269 djm 1965:
1.350 djm 1966: /*
1967: * UpdateHostkeys is skipped for wildcard host names and hostnames
1968: * that contain more than two entries (ssh never writes these).
1969: */
1970: if (hostspec_is_complex(l->hosts)) {
1.354 djm 1971: debug3_f("hostkeys file %s:%ld complex host specification",
1972: l->path, l->linenum);
1.350 djm 1973: ctx->complex_hostspec = 1;
1.347 djm 1974: return 0;
1975: }
1976:
1.269 djm 1977: /* Mark off keys we've already seen for this host */
1978: for (i = 0; i < ctx->nkeys; i++) {
1.351 djm 1979: if (!sshkey_equal(l->key, ctx->keys[i]))
1980: continue;
1.354 djm 1981: debug3_f("found %s key at %s:%ld",
1.351 djm 1982: sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum);
1983: ctx->keys_match[i] |= l->match;
1984: return 0;
1.269 djm 1985: }
1986: /* This line contained a key that not offered by the server */
1.354 djm 1987: debug3_f("deprecated %s key at %s:%ld", sshkey_ssh_name(l->key),
1988: l->path, l->linenum);
1.299 deraadt 1989: if ((tmp = recallocarray(ctx->old_keys, ctx->nold, ctx->nold + 1,
1.269 djm 1990: sizeof(*ctx->old_keys))) == NULL)
1.354 djm 1991: fatal_f("recallocarray failed nold = %zu", ctx->nold);
1.269 djm 1992: ctx->old_keys = tmp;
1993: ctx->old_keys[ctx->nold++] = l->key;
1994: l->key = NULL;
1995:
1996: return 0;
1997: }
1998:
1.352 djm 1999: /* callback to search for ctx->old_keys in known_hosts under other names */
2000: static int
2001: hostkeys_check_old(struct hostkey_foreach_line *l, void *_ctx)
2002: {
2003: struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
2004: size_t i;
2005: int hashed;
2006:
2007: /* only care about lines that *don't* match the active host spec */
2008: if (l->status == HKF_STATUS_MATCHED || l->key == NULL)
2009: return 0;
2010:
2011: hashed = l->match & (HKF_MATCH_HOST_HASHED|HKF_MATCH_IP_HASHED);
2012: for (i = 0; i < ctx->nold; i++) {
2013: if (!sshkey_equal(l->key, ctx->old_keys[i]))
2014: continue;
1.354 djm 2015: debug3_f("found deprecated %s key at %s:%ld as %s",
1.364 djm 2016: sshkey_ssh_name(ctx->old_keys[i]), l->path, l->linenum,
1.352 djm 2017: hashed ? "[HASHED]" : l->hosts);
2018: ctx->old_key_seen = 1;
2019: break;
2020: }
2021: return 0;
2022: }
2023:
2024: /*
2025: * Check known_hosts files for deprecated keys under other names. Returns 0
2026: * on success or -1 on failure. Updates ctx->old_key_seen if deprecated keys
2027: * exist under names other than the active hostname/IP.
2028: */
2029: static int
2030: check_old_keys_othernames(struct hostkeys_update_ctx *ctx)
2031: {
2032: size_t i;
2033: int r;
2034:
1.354 djm 2035: debug2_f("checking for %zu deprecated keys", ctx->nold);
1.352 djm 2036: for (i = 0; i < options.num_user_hostfiles; i++) {
1.354 djm 2037: debug3_f("searching %s for %s / %s",
1.352 djm 2038: options.user_hostfiles[i], ctx->host_str,
2039: ctx->ip_str ? ctx->ip_str : "(none)");
2040: if ((r = hostkeys_foreach(options.user_hostfiles[i],
2041: hostkeys_check_old, ctx, ctx->host_str, ctx->ip_str,
1.356 djm 2042: HKF_WANT_PARSE_KEY, 0)) != 0) {
1.352 djm 2043: if (r == SSH_ERR_SYSTEM_ERROR && errno == ENOENT) {
1.354 djm 2044: debug_f("hostkeys file %s does not exist",
2045: options.user_hostfiles[i]);
1.352 djm 2046: continue;
2047: }
1.354 djm 2048: error_fr(r, "hostkeys_foreach failed for %s",
2049: options.user_hostfiles[i]);
1.352 djm 2050: return -1;
2051: }
2052: }
2053: return 0;
2054: }
2055:
1.269 djm 2056: static void
1.337 djm 2057: hostkey_change_preamble(LogLevel loglevel)
1.269 djm 2058: {
1.336 djm 2059: do_log2(loglevel, "The server has updated its host keys.");
2060: do_log2(loglevel, "These changes were verified by the server's "
2061: "existing trusted key.");
2062: }
2063:
2064: static void
2065: update_known_hosts(struct hostkeys_update_ctx *ctx)
2066: {
2067: int r, was_raw = 0, first = 1;
2068: int asking = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK;
2069: LogLevel loglevel = asking ? SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE;
1.269 djm 2070: char *fp, *response;
2071: size_t i;
1.333 djm 2072: struct stat sb;
1.269 djm 2073:
2074: for (i = 0; i < ctx->nkeys; i++) {
1.351 djm 2075: if (!ctx->keys_verified[i])
1.269 djm 2076: continue;
2077: if ((fp = sshkey_fingerprint(ctx->keys[i],
2078: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1.354 djm 2079: fatal_f("sshkey_fingerprint failed");
1.336 djm 2080: if (first && asking)
1.337 djm 2081: hostkey_change_preamble(loglevel);
1.269 djm 2082: do_log2(loglevel, "Learned new hostkey: %s %s",
2083: sshkey_type(ctx->keys[i]), fp);
1.336 djm 2084: first = 0;
1.269 djm 2085: free(fp);
2086: }
2087: for (i = 0; i < ctx->nold; i++) {
2088: if ((fp = sshkey_fingerprint(ctx->old_keys[i],
2089: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1.354 djm 2090: fatal_f("sshkey_fingerprint failed");
1.336 djm 2091: if (first && asking)
1.337 djm 2092: hostkey_change_preamble(loglevel);
1.269 djm 2093: do_log2(loglevel, "Deprecating obsolete hostkey: %s %s",
2094: sshkey_type(ctx->old_keys[i]), fp);
1.336 djm 2095: first = 0;
1.269 djm 2096: free(fp);
2097: }
2098: if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
1.271 djm 2099: if (get_saved_tio() != NULL) {
2100: leave_raw_mode(1);
2101: was_raw = 1;
2102: }
1.269 djm 2103: response = NULL;
2104: for (i = 0; !quit_pending && i < 3; i++) {
2105: free(response);
2106: response = read_passphrase("Accept updated hostkeys? "
2107: "(yes/no): ", RP_ECHO);
2108: if (strcasecmp(response, "yes") == 0)
2109: break;
2110: else if (quit_pending || response == NULL ||
2111: strcasecmp(response, "no") == 0) {
2112: options.update_hostkeys = 0;
2113: break;
2114: } else {
2115: do_log2(loglevel, "Please enter "
2116: "\"yes\" or \"no\"");
2117: }
2118: }
2119: if (quit_pending || i >= 3 || response == NULL)
2120: options.update_hostkeys = 0;
2121: free(response);
1.271 djm 2122: if (was_raw)
2123: enter_raw_mode(1);
1.269 djm 2124: }
1.333 djm 2125: if (options.update_hostkeys == 0)
2126: return;
1.269 djm 2127: /*
2128: * Now that all the keys are verified, we can go ahead and replace
2129: * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't
2130: * cancel the operation).
2131: */
1.333 djm 2132: for (i = 0; i < options.num_user_hostfiles; i++) {
2133: /*
2134: * NB. keys are only added to hostfiles[0], for the rest we
2135: * just delete the hostname entries.
2136: */
2137: if (stat(options.user_hostfiles[i], &sb) != 0) {
2138: if (errno == ENOENT) {
1.354 djm 2139: debug_f("known hosts file %s does not "
2140: "exist", options.user_hostfiles[i]);
1.333 djm 2141: } else {
1.354 djm 2142: error_f("known hosts file %s "
2143: "inaccessible: %s",
2144: options.user_hostfiles[i], strerror(errno));
1.333 djm 2145: }
2146: continue;
2147: }
2148: if ((r = hostfile_replace_entries(options.user_hostfiles[i],
2149: ctx->host_str, ctx->ip_str,
2150: i == 0 ? ctx->keys : NULL, i == 0 ? ctx->nkeys : 0,
2151: options.hash_known_hosts, 0,
2152: options.fingerprint_hash)) != 0) {
1.354 djm 2153: error_fr(r, "hostfile_replace_entries failed for %s",
2154: options.user_hostfiles[i]);
1.333 djm 2155: }
2156: }
1.269 djm 2157: }
2158:
2159: static void
1.381 djm 2160: client_global_hostkeys_prove_confirm(struct ssh *ssh, int type,
1.303 djm 2161: u_int32_t seq, void *_ctx)
1.269 djm 2162: {
2163: struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
2164: size_t i, ndone;
2165: struct sshbuf *signdata;
1.375 djm 2166: int r, plaintype;
1.269 djm 2167: const u_char *sig;
1.375 djm 2168: const char *rsa_kexalg = NULL;
2169: char *alg = NULL;
1.269 djm 2170: size_t siglen;
2171:
2172: if (ctx->nnew == 0)
1.354 djm 2173: fatal_f("ctx->nnew == 0"); /* sanity */
1.269 djm 2174: if (type != SSH2_MSG_REQUEST_SUCCESS) {
2175: error("Server failed to confirm ownership of "
2176: "private host keys");
2177: hostkeys_update_ctx_free(ctx);
2178: return;
2179: }
1.375 djm 2180: if (sshkey_type_plain(sshkey_type_from_name(
2181: ssh->kex->hostkey_alg)) == KEY_RSA)
2182: rsa_kexalg = ssh->kex->hostkey_alg;
1.269 djm 2183: if ((signdata = sshbuf_new()) == NULL)
1.354 djm 2184: fatal_f("sshbuf_new failed");
1.269 djm 2185: /*
2186: * Expect a signature for each of the ctx->nnew private keys we
2187: * haven't seen before. They will be in the same order as the
1.351 djm 2188: * ctx->keys where the corresponding ctx->keys_match[i] == 0.
1.269 djm 2189: */
2190: for (ndone = i = 0; i < ctx->nkeys; i++) {
1.351 djm 2191: if (ctx->keys_match[i])
1.269 djm 2192: continue;
1.375 djm 2193: plaintype = sshkey_type_plain(ctx->keys[i]->type);
1.269 djm 2194: /* Prepare data to be signed: session ID, unique string, key */
2195: sshbuf_reset(signdata);
1.270 djm 2196: if ( (r = sshbuf_put_cstring(signdata,
2197: "hostkeys-prove-00@openssh.com")) != 0 ||
1.358 djm 2198: (r = sshbuf_put_stringb(signdata,
2199: ssh->kex->session_id)) != 0 ||
1.269 djm 2200: (r = sshkey_puts(ctx->keys[i], signdata)) != 0)
1.354 djm 2201: fatal_fr(r, "compose signdata");
1.269 djm 2202: /* Extract and verify signature */
2203: if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) {
1.354 djm 2204: error_fr(r, "parse sig");
1.269 djm 2205: goto out;
2206: }
1.375 djm 2207: if ((r = sshkey_get_sigtype(sig, siglen, &alg)) != 0) {
2208: error_fr(r, "server gave unintelligible signature "
2209: "for %s key %zu", sshkey_type(ctx->keys[i]), i);
2210: goto out;
2211: }
1.309 djm 2212: /*
1.375 djm 2213: * Special case for RSA keys: if a RSA hostkey was negotiated,
2214: * then use its signature type for verification of RSA hostkey
2215: * proofs. Otherwise, accept only RSA-SHA256/512 signatures.
1.309 djm 2216: */
1.375 djm 2217: if (plaintype == KEY_RSA && rsa_kexalg == NULL &&
2218: match_pattern_list(alg, HOSTKEY_PROOF_RSA_ALGS, 0) != 1) {
2219: debug_f("server used untrusted RSA signature algorithm "
2220: "%s for key %zu, disregarding", alg, i);
2221: free(alg);
2222: /* zap the key from the list */
2223: sshkey_free(ctx->keys[i]);
2224: ctx->keys[i] = NULL;
2225: ndone++;
2226: continue;
2227: }
2228: debug3_f("verify %s key %zu using sigalg %s",
2229: sshkey_type(ctx->keys[i]), i, alg);
2230: free(alg);
1.269 djm 2231: if ((r = sshkey_verify(ctx->keys[i], sig, siglen,
1.308 djm 2232: sshbuf_ptr(signdata), sshbuf_len(signdata),
1.375 djm 2233: plaintype == KEY_RSA ? rsa_kexalg : NULL, 0, NULL)) != 0) {
1.361 djm 2234: error_fr(r, "server gave bad signature for %s key %zu",
1.354 djm 2235: sshkey_type(ctx->keys[i]), i);
1.269 djm 2236: goto out;
2237: }
2238: /* Key is good. Mark it as 'seen' */
1.351 djm 2239: ctx->keys_verified[i] = 1;
1.269 djm 2240: ndone++;
2241: }
1.354 djm 2242: /* Shouldn't happen */
1.269 djm 2243: if (ndone != ctx->nnew)
1.354 djm 2244: fatal_f("ndone != ctx->nnew (%zu / %zu)", ndone, ctx->nnew);
1.320 djm 2245: if ((r = sshpkt_get_end(ssh)) != 0) {
1.354 djm 2246: error_f("protocol error");
1.320 djm 2247: goto out;
2248: }
1.269 djm 2249:
2250: /* Make the edits to known_hosts */
2251: update_known_hosts(ctx);
2252: out:
2253: hostkeys_update_ctx_free(ctx);
1.384 djm 2254: hostkeys_update_complete = 1;
2255: client_repledge();
1.269 djm 2256: }
2257:
1.267 djm 2258: /*
1.291 djm 2259: * Returns non-zero if the key is accepted by HostkeyAlgorithms.
2260: * Made slightly less trivial by the multiple RSA signature algorithm names.
2261: */
2262: static int
2263: key_accepted_by_hostkeyalgs(const struct sshkey *key)
2264: {
2265: const char *ktype = sshkey_ssh_name(key);
1.331 dtucker 2266: const char *hostkeyalgs = options.hostkeyalgorithms;
1.291 djm 2267:
2268: if (key == NULL || key->type == KEY_UNSPEC)
2269: return 0;
2270: if (key->type == KEY_RSA &&
2271: (match_pattern_list("rsa-sha2-256", hostkeyalgs, 0) == 1 ||
2272: match_pattern_list("rsa-sha2-512", hostkeyalgs, 0) == 1))
2273: return 1;
2274: return match_pattern_list(ktype, hostkeyalgs, 0) == 1;
2275: }
2276:
2277: /*
1.270 djm 2278: * Handle hostkeys-00@openssh.com global request to inform the client of all
1.267 djm 2279: * the server's hostkeys. The keys are checked against the user's
2280: * HostkeyAlgorithms preference before they are accepted.
2281: */
2282: static int
1.321 djm 2283: client_input_hostkeys(struct ssh *ssh)
1.267 djm 2284: {
2285: const u_char *blob = NULL;
1.269 djm 2286: size_t i, len = 0;
1.267 djm 2287: struct sshbuf *buf = NULL;
1.269 djm 2288: struct sshkey *key = NULL, **tmp;
1.384 djm 2289: int r, prove_sent = 0;
1.269 djm 2290: char *fp;
1.267 djm 2291: static int hostkeys_seen = 0; /* XXX use struct ssh */
1.268 djm 2292: extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */
1.272 djm 2293: struct hostkeys_update_ctx *ctx = NULL;
1.351 djm 2294: u_int want;
1.267 djm 2295:
2296: if (hostkeys_seen)
1.354 djm 2297: fatal_f("server already sent hostkeys");
1.384 djm 2298: if (!can_update_hostkeys())
1.267 djm 2299: return 1;
1.384 djm 2300: hostkeys_seen = 1;
1.272 djm 2301:
2302: ctx = xcalloc(1, sizeof(*ctx));
1.269 djm 2303: while (ssh_packet_remaining(ssh) > 0) {
1.267 djm 2304: sshkey_free(key);
2305: key = NULL;
1.269 djm 2306: if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) {
1.354 djm 2307: error_fr(r, "parse key");
1.269 djm 2308: goto out;
2309: }
1.270 djm 2310: if ((r = sshkey_from_blob(blob, len, &key)) != 0) {
1.354 djm 2311: do_log2_fr(r, r == SSH_ERR_KEY_TYPE_UNKNOWN ?
1.335 djm 2312: SYSLOG_LEVEL_DEBUG1 : SYSLOG_LEVEL_ERROR,
1.354 djm 2313: "convert key");
1.335 djm 2314: continue;
1.270 djm 2315: }
1.267 djm 2316: fp = sshkey_fingerprint(key, options.fingerprint_hash,
2317: SSH_FP_DEFAULT);
1.354 djm 2318: debug3_f("received %s key %s", sshkey_type(key), fp);
1.267 djm 2319: free(fp);
1.275 markus 2320:
1.291 djm 2321: if (!key_accepted_by_hostkeyalgs(key)) {
1.354 djm 2322: debug3_f("%s key not permitted by "
2323: "HostkeyAlgorithms", sshkey_ssh_name(key));
1.267 djm 2324: continue;
2325: }
1.269 djm 2326: /* Skip certs */
2327: if (sshkey_is_cert(key)) {
1.354 djm 2328: debug3_f("%s key is a certificate; skipping",
2329: sshkey_ssh_name(key));
1.269 djm 2330: continue;
2331: }
2332: /* Ensure keys are unique */
2333: for (i = 0; i < ctx->nkeys; i++) {
2334: if (sshkey_equal(key, ctx->keys[i])) {
1.354 djm 2335: error_f("received duplicated %s host key",
2336: sshkey_ssh_name(key));
1.269 djm 2337: goto out;
2338: }
2339: }
2340: /* Key is good, record it */
1.299 deraadt 2341: if ((tmp = recallocarray(ctx->keys, ctx->nkeys, ctx->nkeys + 1,
1.269 djm 2342: sizeof(*ctx->keys))) == NULL)
1.354 djm 2343: fatal_f("recallocarray failed nkeys = %zu",
2344: ctx->nkeys);
1.269 djm 2345: ctx->keys = tmp;
2346: ctx->keys[ctx->nkeys++] = key;
1.267 djm 2347: key = NULL;
2348: }
2349:
1.269 djm 2350: if (ctx->nkeys == 0) {
1.354 djm 2351: debug_f("server sent no hostkeys");
1.267 djm 2352: goto out;
2353: }
1.270 djm 2354:
1.351 djm 2355: if ((ctx->keys_match = calloc(ctx->nkeys,
2356: sizeof(*ctx->keys_match))) == NULL ||
2357: (ctx->keys_verified = calloc(ctx->nkeys,
2358: sizeof(*ctx->keys_verified))) == NULL)
1.354 djm 2359: fatal_f("calloc failed");
1.267 djm 2360:
1.268 djm 2361: get_hostfile_hostname_ipaddr(host,
2362: options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL,
1.269 djm 2363: options.port, &ctx->host_str,
2364: options.check_host_ip ? &ctx->ip_str : NULL);
1.268 djm 2365:
1.269 djm 2366: /* Find which keys we already know about. */
1.333 djm 2367: for (i = 0; i < options.num_user_hostfiles; i++) {
1.354 djm 2368: debug_f("searching %s for %s / %s",
1.339 dtucker 2369: options.user_hostfiles[i], ctx->host_str,
1.340 dtucker 2370: ctx->ip_str ? ctx->ip_str : "(none)");
1.333 djm 2371: if ((r = hostkeys_foreach(options.user_hostfiles[i],
2372: hostkeys_find, ctx, ctx->host_str, ctx->ip_str,
1.356 djm 2373: HKF_WANT_PARSE_KEY, 0)) != 0) {
1.333 djm 2374: if (r == SSH_ERR_SYSTEM_ERROR && errno == ENOENT) {
1.354 djm 2375: debug_f("hostkeys file %s does not exist",
2376: options.user_hostfiles[i]);
1.333 djm 2377: continue;
2378: }
1.354 djm 2379: error_fr(r, "hostkeys_foreach failed for %s",
2380: options.user_hostfiles[i]);
1.333 djm 2381: goto out;
2382: }
1.267 djm 2383: }
2384:
1.269 djm 2385: /* Figure out if we have any new keys to add */
1.351 djm 2386: ctx->nnew = ctx->nincomplete = 0;
2387: want = HKF_MATCH_HOST | ( options.check_host_ip ? HKF_MATCH_IP : 0);
1.269 djm 2388: for (i = 0; i < ctx->nkeys; i++) {
1.351 djm 2389: if (ctx->keys_match[i] == 0)
1.269 djm 2390: ctx->nnew++;
1.351 djm 2391: if ((ctx->keys_match[i] & want) != want)
2392: ctx->nincomplete++;
1.269 djm 2393: }
2394:
1.354 djm 2395: debug3_f("%zu server keys: %zu new, %zu retained, "
2396: "%zu incomplete match. %zu to remove", ctx->nkeys, ctx->nnew,
2397: ctx->nkeys - ctx->nnew - ctx->nincomplete,
1.351 djm 2398: ctx->nincomplete, ctx->nold);
1.269 djm 2399:
1.353 djm 2400: if (ctx->nnew == 0 && ctx->nold == 0) {
1.354 djm 2401: debug_f("no new or deprecated keys from server");
1.353 djm 2402: goto out;
2403: }
2404:
2405: /* Various reasons why we cannot proceed with the update */
2406: if (ctx->complex_hostspec) {
1.354 djm 2407: debug_f("CA/revocation marker, manual host list or wildcard "
2408: "host pattern found, skipping UserKnownHostsFile update");
1.353 djm 2409: goto out;
2410: }
2411: if (ctx->other_name_seen) {
1.354 djm 2412: debug_f("host key found matching a different name/address, "
2413: "skipping UserKnownHostsFile update");
1.347 djm 2414: goto out;
1.352 djm 2415: }
2416: /*
2417: * If removing keys, check whether they appear under different
2418: * names/addresses and refuse to proceed if they do. This avoids
2419: * cases such as hosts with multiple names becoming inconsistent
2420: * with regards to CheckHostIP entries.
2421: * XXX UpdateHostkeys=force to override this (and other) checks?
2422: */
2423: if (ctx->nold != 0) {
2424: if (check_old_keys_othernames(ctx) != 0)
2425: goto out; /* error already logged */
2426: if (ctx->old_key_seen) {
1.354 djm 2427: debug_f("key(s) for %s%s%s exist under other names; "
2428: "skipping UserKnownHostsFile update",
1.352 djm 2429: ctx->host_str, ctx->ip_str == NULL ? "" : ",",
2430: ctx->ip_str == NULL ? "" : ctx->ip_str);
2431: goto out;
2432: }
2433: }
2434:
1.353 djm 2435: if (ctx->nnew == 0) {
1.351 djm 2436: /*
2437: * We have some keys to remove or fix matching for.
2438: * We can proceed to do this without requiring a fresh proof
2439: * from the server.
2440: */
1.269 djm 2441: update_known_hosts(ctx);
1.353 djm 2442: goto out;
2443: }
2444: /*
2445: * We have received previously-unseen keys from the server.
2446: * Ask the server to confirm ownership of the private halves.
2447: */
1.354 djm 2448: debug3_f("asking server to prove ownership for %zu keys", ctx->nnew);
1.353 djm 2449: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2450: (r = sshpkt_put_cstring(ssh,
2451: "hostkeys-prove-00@openssh.com")) != 0 ||
2452: (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */
1.354 djm 2453: fatal_fr(r, "prepare hostkeys-prove");
1.353 djm 2454: if ((buf = sshbuf_new()) == NULL)
1.354 djm 2455: fatal_f("sshbuf_new");
1.353 djm 2456: for (i = 0; i < ctx->nkeys; i++) {
2457: if (ctx->keys_match[i])
2458: continue;
2459: sshbuf_reset(buf);
2460: if ((r = sshkey_putb(ctx->keys[i], buf)) != 0 ||
1.354 djm 2461: (r = sshpkt_put_stringb(ssh, buf)) != 0)
2462: fatal_fr(r, "assemble hostkeys-prove");
1.269 djm 2463: }
1.353 djm 2464: if ((r = sshpkt_send(ssh)) != 0)
1.354 djm 2465: fatal_fr(r, "send hostkeys-prove");
1.353 djm 2466: client_register_global_confirm(
1.381 djm 2467: client_global_hostkeys_prove_confirm, ctx);
1.353 djm 2468: ctx = NULL; /* will be freed in callback */
1.384 djm 2469: prove_sent = 1;
1.269 djm 2470:
1.267 djm 2471: /* Success */
2472: out:
1.269 djm 2473: hostkeys_update_ctx_free(ctx);
1.267 djm 2474: sshkey_free(key);
2475: sshbuf_free(buf);
1.384 djm 2476: if (!prove_sent) {
2477: /* UpdateHostkeys handling completed */
2478: hostkeys_update_complete = 1;
2479: client_repledge();
2480: }
1.269 djm 2481: /*
2482: * NB. Return success for all cases. The server doesn't need to know
2483: * what the client does with its hosts file.
2484: */
2485: return 1;
1.267 djm 2486: }
2487:
1.264 markus 2488: static int
1.297 markus 2489: client_input_global_request(int type, u_int32_t seq, struct ssh *ssh)
1.95 markus 2490: {
2491: char *rtype;
1.320 djm 2492: u_char want_reply;
2493: int r, success = 0;
1.95 markus 2494:
1.320 djm 2495: if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 ||
2496: (r = sshpkt_get_u8(ssh, &want_reply)) != 0)
2497: goto out;
1.117 markus 2498: debug("client_input_global_request: rtype %s want_reply %d",
2499: rtype, want_reply);
1.270 djm 2500: if (strcmp(rtype, "hostkeys-00@openssh.com") == 0)
1.321 djm 2501: success = client_input_hostkeys(ssh);
1.95 markus 2502: if (want_reply) {
1.320 djm 2503: if ((r = sshpkt_start(ssh, success ? SSH2_MSG_REQUEST_SUCCESS :
2504: SSH2_MSG_REQUEST_FAILURE)) != 0 ||
2505: (r = sshpkt_send(ssh)) != 0 ||
2506: (r = ssh_packet_write_wait(ssh)) != 0)
2507: goto out;
1.95 markus 2508: }
1.320 djm 2509: r = 0;
2510: out:
1.250 djm 2511: free(rtype);
1.320 djm 2512: return r;
1.95 markus 2513: }
1.22 markus 2514:
1.354 djm 2515: static void
2516: client_send_env(struct ssh *ssh, int id, const char *name, const char *val)
2517: {
2518: int r;
2519:
2520: debug("channel %d: setting env %s = \"%s\"", id, name, val);
2521: channel_request_start(ssh, id, "env", 0);
2522: if ((r = sshpkt_put_cstring(ssh, name)) != 0 ||
2523: (r = sshpkt_put_cstring(ssh, val)) != 0 ||
2524: (r = sshpkt_send(ssh)) != 0)
2525: fatal_fr(r, "send setenv");
2526: }
2527:
1.123 djm 2528: void
1.303 djm 2529: client_session2_setup(struct ssh *ssh, int id, int want_tty, int want_subsystem,
1.315 markus 2530: const char *term, struct termios *tiop, int in_fd, struct sshbuf *cmd,
2531: char **env)
1.123 djm 2532: {
1.380 djm 2533: size_t i, j, len;
2534: int matched, r;
1.313 djm 2535: char *name, *val;
1.132 djm 2536: Channel *c = NULL;
1.123 djm 2537:
1.354 djm 2538: debug2_f("id %d", id);
1.123 djm 2539:
1.303 djm 2540: if ((c = channel_lookup(ssh, id)) == NULL)
1.354 djm 2541: fatal_f("channel %d: unknown channel", id);
1.224 djm 2542:
1.320 djm 2543: ssh_packet_set_interactive(ssh, want_tty,
1.224 djm 2544: options.ip_qos_interactive, options.ip_qos_bulk);
1.132 djm 2545:
1.123 djm 2546: if (want_tty) {
2547: struct winsize ws;
2548:
2549: /* Store window size in the packet. */
1.326 deraadt 2550: if (ioctl(in_fd, TIOCGWINSZ, &ws) == -1)
1.123 djm 2551: memset(&ws, 0, sizeof(ws));
2552:
1.303 djm 2553: channel_request_start(ssh, id, "pty-req", 1);
2554: client_expect_confirm(ssh, id, "PTY allocation", CONFIRM_TTY);
1.320 djm 2555: if ((r = sshpkt_put_cstring(ssh, term != NULL ? term : ""))
2556: != 0 ||
2557: (r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 ||
2558: (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 ||
2559: (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 ||
2560: (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0)
1.354 djm 2561: fatal_fr(r, "build pty-req");
1.194 djm 2562: if (tiop == NULL)
2563: tiop = get_saved_tio();
1.316 markus 2564: ssh_tty_make_modes(ssh, -1, tiop);
1.320 djm 2565: if ((r = sshpkt_send(ssh)) != 0)
1.354 djm 2566: fatal_fr(r, "send pty-req");
1.123 djm 2567: /* XXX wait for reply */
1.132 djm 2568: c->client_tty = 1;
1.123 djm 2569: }
2570:
2571: /* Transfer any environment variables from client to server */
1.126 djm 2572: if (options.num_send_env != 0 && env != NULL) {
1.123 djm 2573: debug("Sending environment.");
1.126 djm 2574: for (i = 0; env[i] != NULL; i++) {
1.123 djm 2575: /* Split */
1.126 djm 2576: name = xstrdup(env[i]);
1.123 djm 2577: if ((val = strchr(name, '=')) == NULL) {
1.250 djm 2578: free(name);
1.123 djm 2579: continue;
2580: }
2581: *val++ = '\0';
2582:
2583: matched = 0;
2584: for (j = 0; j < options.num_send_env; j++) {
2585: if (match_pattern(name, options.send_env[j])) {
2586: matched = 1;
2587: break;
2588: }
2589: }
2590: if (!matched) {
2591: debug3("Ignored env %s", name);
1.250 djm 2592: free(name);
1.123 djm 2593: continue;
2594: }
1.354 djm 2595: client_send_env(ssh, id, name, val);
1.250 djm 2596: free(name);
1.123 djm 2597: }
1.313 djm 2598: }
2599: for (i = 0; i < options.num_setenv; i++) {
2600: /* Split */
2601: name = xstrdup(options.setenv[i]);
2602: if ((val = strchr(name, '=')) == NULL) {
2603: free(name);
2604: continue;
2605: }
2606: *val++ = '\0';
1.354 djm 2607: client_send_env(ssh, id, name, val);
1.313 djm 2608: free(name);
1.123 djm 2609: }
2610:
1.315 markus 2611: len = sshbuf_len(cmd);
1.123 djm 2612: if (len > 0) {
2613: if (len > 900)
2614: len = 900;
2615: if (want_subsystem) {
1.190 djm 2616: debug("Sending subsystem: %.*s",
1.380 djm 2617: (int)len, (const u_char*)sshbuf_ptr(cmd));
1.303 djm 2618: channel_request_start(ssh, id, "subsystem", 1);
2619: client_expect_confirm(ssh, id, "subsystem",
2620: CONFIRM_CLOSE);
1.123 djm 2621: } else {
1.190 djm 2622: debug("Sending command: %.*s",
1.380 djm 2623: (int)len, (const u_char*)sshbuf_ptr(cmd));
1.303 djm 2624: channel_request_start(ssh, id, "exec", 1);
2625: client_expect_confirm(ssh, id, "exec", CONFIRM_CLOSE);
1.123 djm 2626: }
1.320 djm 2627: if ((r = sshpkt_put_stringb(ssh, cmd)) != 0 ||
2628: (r = sshpkt_send(ssh)) != 0)
1.354 djm 2629: fatal_fr(r, "send command");
1.123 djm 2630: } else {
1.303 djm 2631: channel_request_start(ssh, id, "shell", 1);
2632: client_expect_confirm(ssh, id, "shell", CONFIRM_CLOSE);
1.354 djm 2633: if ((r = sshpkt_send(ssh)) != 0)
2634: fatal_fr(r, "send shell");
1.123 djm 2635: }
1.384 djm 2636:
2637: session_setup_complete = 1;
2638: client_repledge();
1.123 djm 2639: }
2640:
1.77 itojun 2641: static void
1.321 djm 2642: client_init_dispatch(struct ssh *ssh)
1.16 markus 2643: {
1.321 djm 2644: ssh_dispatch_init(ssh, &dispatch_protocol_error);
1.100 markus 2645:
1.321 djm 2646: ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2647: ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2648: ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2649: ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
2650: ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
2651: ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2652: ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
2653: ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
2654: ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
2655: ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2656: ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
2657: ssh_dispatch_set(ssh, SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
1.55 markus 2658:
2659: /* rekeying */
1.321 djm 2660: ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit);
1.100 markus 2661:
2662: /* global request reply messages */
1.321 djm 2663: ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2664: ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
1.232 djm 2665: }
2666:
2667: void
2668: client_stop_mux(void)
2669: {
2670: if (options.control_path != NULL && muxserver_sock != -1)
2671: unlink(options.control_path);
2672: /*
1.247 dtucker 2673: * If we are in persist mode, or don't have a shell, signal that we
2674: * should close when all active channels are closed.
1.232 djm 2675: */
1.366 djm 2676: if (options.control_persist || options.session_type == SESSION_TYPE_NONE) {
1.232 djm 2677: session_closed = 1;
1.235 djm 2678: setproctitle("[stopped mux]");
2679: }
1.113 markus 2680: }
2681:
2682: /* client specific fatal cleanup */
2683: void
1.114 markus 2684: cleanup_exit(int i)
1.113 markus 2685: {
1.233 djm 2686: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.192 djm 2687: if (options.control_path != NULL && muxserver_sock != -1)
1.123 djm 2688: unlink(options.control_path);
1.223 djm 2689: ssh_kill_proxy_command();
1.114 markus 2690: _exit(i);
1.1 deraadt 2691: }