Annotation of src/usr.bin/ssh/clientloop.c, Revision 1.292
1.292 ! djm 1: /* $OpenBSD: clientloop.c,v 1.291 2017/03/10 05:01:13 djm 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.153 stevesk 73: #include <signal.h>
1.174 stevesk 74: #include <stdio.h>
1.173 stevesk 75: #include <stdlib.h>
1.170 stevesk 76: #include <string.h>
1.150 stevesk 77: #include <termios.h>
1.175 deraadt 78: #include <pwd.h>
1.169 stevesk 79: #include <unistd.h>
1.266 deraadt 80: #include <limits.h>
1.1 deraadt 81:
1.175 deraadt 82: #include "xmalloc.h"
1.45 markus 83: #include "ssh.h"
84: #include "ssh1.h"
85: #include "ssh2.h"
1.1 deraadt 86: #include "packet.h"
87: #include "buffer.h"
1.15 markus 88: #include "compat.h"
1.74 markus 89: #include "channels.h"
1.15 markus 90: #include "dispatch.h"
1.40 markus 91: #include "key.h"
1.175 deraadt 92: #include "cipher.h"
1.54 markus 93: #include "kex.h"
1.275 markus 94: #include "myproposal.h"
1.45 markus 95: #include "log.h"
1.261 millert 96: #include "misc.h"
1.45 markus 97: #include "readconf.h"
98: #include "clientloop.h"
1.147 djm 99: #include "sshconnect.h"
1.40 markus 100: #include "authfd.h"
1.45 markus 101: #include "atomicio.h"
1.118 deraadt 102: #include "sshpty.h"
1.123 djm 103: #include "match.h"
104: #include "msg.h"
1.262 djm 105: #include "ssherr.h"
1.267 djm 106: #include "hostfile.h"
1.39 markus 107:
108: /* import options */
109: extern Options options;
110:
1.1 deraadt 111: /* Flag indicating that stdin should be redirected from /dev/null. */
112: extern int stdin_null_flag;
113:
1.116 dtucker 114: /* Flag indicating that no shell has been requested */
115: extern int no_shell_flag;
116:
1.286 djm 117: /* Flag indicating that ssh should daemonise after authentication is complete */
118: extern int fork_after_authentication_flag;
119:
1.123 djm 120: /* Control socket */
1.217 djm 121: extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */
1.123 djm 122:
1.13 markus 123: /*
124: * Name of the host we are connecting to. This is the name given on the
125: * command line, or the HostName specified for the user-supplied name in a
126: * configuration file.
127: */
1.1 deraadt 128: extern char *host;
129:
1.13 markus 130: /*
131: * Flag to indicate that we have received a window change signal which has
132: * not yet been processed. This will cause a message indicating the new
133: * window size to be sent to the server a little later. This is volatile
134: * because this is updated in a signal handler.
135: */
1.88 markus 136: static volatile sig_atomic_t received_window_change_signal = 0;
137: static volatile sig_atomic_t received_signal = 0;
1.1 deraadt 138:
1.145 djm 139: /* Flag indicating whether the user's terminal is in non-blocking mode. */
1.1 deraadt 140: static int in_non_blocking_mode = 0;
141:
1.222 djm 142: /* Time when backgrounded control master using ControlPersist should exit */
143: static time_t control_persist_exit_time = 0;
144:
1.1 deraadt 145: /* Common data for the client loop code. */
1.217 djm 146: volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
1.195 djm 147: static int escape_char1; /* Escape character. (proto1 only) */
148: static int escape_pending1; /* Last character was an escape (proto1 only) */
1.11 markus 149: static int last_was_cr; /* Last character was a newline. */
1.197 djm 150: static int exit_status; /* Used to store the command exit status. */
151: static int stdin_eof; /* EOF has been encountered on stderr. */
1.11 markus 152: static Buffer stdin_buffer; /* Buffer for stdin data. */
153: static Buffer stdout_buffer; /* Buffer for stdout data. */
154: static Buffer stderr_buffer; /* Buffer for stderr data. */
1.221 djm 155: static u_int buffer_high; /* Soft max buffer size. */
1.11 markus 156: static int connection_in; /* Connection to server (input). */
157: static int connection_out; /* Connection to server (output). */
1.56 markus 158: static int need_rekeying; /* Set to non-zero if rekeying is requested. */
1.221 djm 159: static int session_closed; /* In SSH2: login session closed. */
1.274 djm 160: static u_int x11_refuse_time; /* If >0, refuse x11 opens after this time. */
1.1 deraadt 161:
1.77 itojun 162: static void client_init_dispatch(void);
1.16 markus 163: int session_ident = -1;
164:
1.195 djm 165: /* Track escape per proto2 channel */
166: struct escape_filter_ctx {
167: int escape_pending;
168: int escape_char;
169: };
170:
171: /* Context for channel confirmation replies */
1.190 djm 172: struct channel_reply_ctx {
173: const char *request_type;
1.234 djm 174: int id;
175: enum confirm_action action;
1.190 djm 176: };
177:
1.196 djm 178: /* Global request success/failure callbacks */
179: struct global_confirm {
180: TAILQ_ENTRY(global_confirm) entry;
181: global_confirm_cb *cb;
182: void *ctx;
183: int ref_count;
184: };
185: TAILQ_HEAD(global_confirms, global_confirm);
186: static struct global_confirms global_confirms =
187: TAILQ_HEAD_INITIALIZER(global_confirms);
188:
1.123 djm 189: void ssh_process_session2_setup(int, int, int, Buffer *);
190:
1.1 deraadt 191: /* Restores stdin to blocking mode. */
192:
1.77 itojun 193: static void
1.49 itojun 194: leave_non_blocking(void)
1.1 deraadt 195: {
1.11 markus 196: if (in_non_blocking_mode) {
1.125 djm 197: unset_nonblock(fileno(stdin));
1.11 markus 198: in_non_blocking_mode = 0;
199: }
1.1 deraadt 200: }
201:
1.13 markus 202: /*
203: * Signal handler for the window change signal (SIGWINCH). This just sets a
204: * flag indicating that the window has changed.
205: */
1.157 deraadt 206: /*ARGSUSED */
1.77 itojun 207: static void
1.11 markus 208: window_change_handler(int sig)
1.1 deraadt 209: {
1.11 markus 210: received_window_change_signal = 1;
211: signal(SIGWINCH, window_change_handler);
1.1 deraadt 212: }
213:
1.13 markus 214: /*
215: * Signal handler for signals that cause the program to terminate. These
216: * signals must be trapped to restore terminal modes.
217: */
1.157 deraadt 218: /*ARGSUSED */
1.77 itojun 219: static void
1.11 markus 220: signal_handler(int sig)
1.1 deraadt 221: {
1.75 markus 222: received_signal = sig;
223: quit_pending = 1;
1.1 deraadt 224: }
225:
1.13 markus 226: /*
227: * Returns current time in seconds from Jan 1, 1970 with the maximum
228: * available resolution.
229: */
1.1 deraadt 230:
1.77 itojun 231: static double
1.49 itojun 232: get_current_time(void)
1.1 deraadt 233: {
1.11 markus 234: struct timeval tv;
235: gettimeofday(&tv, NULL);
236: return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
1.138 djm 237: }
238:
1.222 djm 239: /*
240: * Sets control_persist_exit_time to the absolute time when the
241: * backgrounded control master should exit due to expiry of the
242: * ControlPersist timeout. Sets it to 0 if we are not a backgrounded
243: * control master process, or if there is no ControlPersist timeout.
244: */
245: static void
246: set_control_persist_exit_time(void)
247: {
248: if (muxserver_sock == -1 || !options.control_persist
1.232 djm 249: || options.control_persist_timeout == 0) {
1.222 djm 250: /* not using a ControlPersist timeout */
251: control_persist_exit_time = 0;
1.232 djm 252: } else if (channel_still_open()) {
1.222 djm 253: /* some client connections are still open */
254: if (control_persist_exit_time > 0)
255: debug2("%s: cancel scheduled exit", __func__);
256: control_persist_exit_time = 0;
257: } else if (control_persist_exit_time <= 0) {
258: /* a client connection has recently closed */
1.251 dtucker 259: control_persist_exit_time = monotime() +
1.222 djm 260: (time_t)options.control_persist_timeout;
261: debug2("%s: schedule exit in %d seconds", __func__,
262: options.control_persist_timeout);
263: }
264: /* else we are already counting down to the timeout */
265: }
266:
1.238 dtucker 267: #define SSH_X11_VALID_DISPLAY_CHARS ":/.-_"
268: static int
269: client_x11_display_valid(const char *display)
270: {
271: size_t i, dlen;
272:
1.279 djm 273: if (display == NULL)
274: return 0;
275:
1.238 dtucker 276: dlen = strlen(display);
277: for (i = 0; i < dlen; i++) {
1.256 deraadt 278: if (!isalnum((u_char)display[i]) &&
1.238 dtucker 279: strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) {
280: debug("Invalid character '%c' in DISPLAY", display[i]);
281: return 0;
282: }
283: }
284: return 1;
285: }
286:
1.274 djm 287: #define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1"
288: #define X11_TIMEOUT_SLACK 60
1.279 djm 289: int
1.138 djm 290: client_x11_get_proto(const char *display, const char *xauth_path,
1.221 djm 291: u_int trusted, u_int timeout, char **_proto, char **_data)
1.138 djm 292: {
1.279 djm 293: char cmd[1024], line[512], xdisplay[512];
294: char xauthfile[PATH_MAX], xauthdir[PATH_MAX];
1.138 djm 295: static char proto[512], data[512];
296: FILE *f;
1.288 tedu 297: int got_data = 0, generated = 0, do_unlink = 0, r;
1.138 djm 298: struct stat st;
1.274 djm 299: u_int now, x11_timeout_real;
1.138 djm 300:
301: *_proto = proto;
302: *_data = data;
1.279 djm 303: proto[0] = data[0] = xauthfile[0] = xauthdir[0] = '\0';
1.138 djm 304:
1.279 djm 305: if (!client_x11_display_valid(display)) {
1.283 millert 306: if (display != NULL)
307: logit("DISPLAY \"%s\" invalid; disabling X11 forwarding",
308: display);
1.279 djm 309: return -1;
310: }
311: if (xauth_path != NULL && stat(xauth_path, &st) == -1) {
1.138 djm 312: debug("No xauth program.");
1.279 djm 313: xauth_path = NULL;
314: }
315:
316: if (xauth_path != NULL) {
1.138 djm 317: /*
318: * Handle FamilyLocal case where $DISPLAY does
319: * not match an authorization entry. For this we
320: * just try "xauth list unix:displaynum.screennum".
321: * XXX: "localhost" match to determine FamilyLocal
322: * is not perfect.
323: */
324: if (strncmp(display, "localhost:", 10) == 0) {
1.279 djm 325: if ((r = snprintf(xdisplay, sizeof(xdisplay), "unix:%s",
326: display + 10)) < 0 ||
327: (size_t)r >= sizeof(xdisplay)) {
328: error("%s: display name too long", __func__);
329: return -1;
330: }
1.138 djm 331: display = xdisplay;
332: }
333: if (trusted == 0) {
1.274 djm 334: /*
1.279 djm 335: * Generate an untrusted X11 auth cookie.
336: *
1.274 djm 337: * The authentication cookie should briefly outlive
338: * ssh's willingness to forward X11 connections to
339: * avoid nasty fail-open behaviour in the X server.
340: */
1.279 djm 341: mktemp_proto(xauthdir, sizeof(xauthdir));
342: if (mkdtemp(xauthdir) == NULL) {
343: error("%s: mkdtemp: %s",
344: __func__, strerror(errno));
345: return -1;
346: }
347: do_unlink = 1;
348: if ((r = snprintf(xauthfile, sizeof(xauthfile),
349: "%s/xauthfile", xauthdir)) < 0 ||
350: (size_t)r >= sizeof(xauthfile)) {
351: error("%s: xauthfile path too long", __func__);
352: unlink(xauthfile);
353: rmdir(xauthdir);
354: return -1;
355: }
356:
1.274 djm 357: if (timeout >= UINT_MAX - X11_TIMEOUT_SLACK)
358: x11_timeout_real = UINT_MAX;
359: else
360: x11_timeout_real = timeout + X11_TIMEOUT_SLACK;
1.279 djm 361: if ((r = snprintf(cmd, sizeof(cmd),
362: "%s -f %s generate %s " SSH_X11_PROTO
363: " untrusted timeout %u 2>" _PATH_DEVNULL,
364: xauth_path, xauthfile, display,
365: x11_timeout_real)) < 0 ||
366: (size_t)r >= sizeof(cmd))
367: fatal("%s: cmd too long", __func__);
368: debug2("%s: %s", __func__, cmd);
369: if (x11_refuse_time == 0) {
370: now = monotime() + 1;
371: if (UINT_MAX - timeout < now)
372: x11_refuse_time = UINT_MAX;
373: else
374: x11_refuse_time = now + timeout;
375: channel_set_x11_refuse_time(x11_refuse_time);
1.138 djm 376: }
1.279 djm 377: if (system(cmd) == 0)
378: generated = 1;
1.138 djm 379: }
1.181 markus 380:
381: /*
382: * When in untrusted mode, we read the cookie only if it was
383: * successfully generated as an untrusted one in the step
384: * above.
385: */
386: if (trusted || generated) {
387: snprintf(cmd, sizeof(cmd),
388: "%s %s%s list %s 2>" _PATH_DEVNULL,
389: xauth_path,
390: generated ? "-f " : "" ,
391: generated ? xauthfile : "",
392: display);
393: debug2("x11_get_proto: %s", cmd);
394: f = popen(cmd, "r");
395: if (f && fgets(line, sizeof(line), f) &&
396: sscanf(line, "%*s %511s %511s", proto, data) == 2)
397: got_data = 1;
398: if (f)
399: pclose(f);
1.279 djm 400: }
1.138 djm 401: }
402:
403: if (do_unlink) {
404: unlink(xauthfile);
405: rmdir(xauthdir);
406: }
407:
1.279 djm 408: /* Don't fall back to fake X11 data for untrusted forwarding */
409: if (!trusted && !got_data) {
410: error("Warning: untrusted X11 forwarding setup failed: "
411: "xauth key data not generated");
412: return -1;
413: }
414:
1.138 djm 415: /*
416: * If we didn't get authentication data, just make up some
417: * data. The forwarding code will check the validity of the
418: * response anyway, and substitute this data. The X11
419: * server, however, will ignore this fake data and use
420: * whatever authentication mechanisms it was using otherwise
421: * for the local connection.
422: */
423: if (!got_data) {
1.288 tedu 424: u_int8_t rnd[16];
425: u_int i;
1.138 djm 426:
427: logit("Warning: No xauth data; "
428: "using fake authentication data for X11 forwarding.");
429: strlcpy(proto, SSH_X11_PROTO, sizeof proto);
1.288 tedu 430: arc4random_buf(rnd, sizeof(rnd));
431: for (i = 0; i < sizeof(rnd); i++) {
1.138 djm 432: snprintf(data + 2 * i, sizeof data - 2 * i, "%02x",
1.288 tedu 433: rnd[i]);
1.138 djm 434: }
435: }
1.279 djm 436:
437: return 0;
1.1 deraadt 438: }
439:
1.13 markus 440: /*
441: * Checks if the client window has changed, and sends a packet about it to
442: * the server if so. The actual change is detected elsewhere (by a software
443: * interrupt on Unix); this just checks the flag and sends a message if
444: * appropriate.
445: */
1.1 deraadt 446:
1.77 itojun 447: static void
1.49 itojun 448: client_check_window_change(void)
1.1 deraadt 449: {
1.292 ! djm 450: if (!received_window_change_signal)
1.16 markus 451: return;
452: /** XXX race */
453: received_window_change_signal = 0;
454:
1.292 ! djm 455: debug2("%s: changed", __func__);
1.16 markus 456:
1.292 ! djm 457: channel_send_window_changes();
1.1 deraadt 458: }
459:
1.264 markus 460: static int
1.117 markus 461: client_global_request_reply(int type, u_int32_t seq, void *ctxt)
462: {
1.196 djm 463: struct global_confirm *gc;
464:
465: if ((gc = TAILQ_FIRST(&global_confirms)) == NULL)
1.264 markus 466: return 0;
1.196 djm 467: if (gc->cb != NULL)
468: gc->cb(type, seq, gc->ctx);
469: if (--gc->ref_count <= 0) {
470: TAILQ_REMOVE(&global_confirms, gc, entry);
1.257 tedu 471: explicit_bzero(gc, sizeof(*gc));
1.250 djm 472: free(gc);
1.196 djm 473: }
474:
1.210 andreas 475: packet_set_alive_timeouts(0);
1.264 markus 476: return 0;
1.117 markus 477: }
478:
479: static void
480: server_alive_check(void)
481: {
1.210 andreas 482: if (packet_inc_alive_timeouts() > options.server_alive_count_max) {
1.228 djm 483: logit("Timeout, server %s not responding.", host);
1.176 markus 484: cleanup_exit(255);
485: }
1.117 markus 486: packet_start(SSH2_MSG_GLOBAL_REQUEST);
487: packet_put_cstring("keepalive@openssh.com");
488: packet_put_char(1); /* boolean: want reply */
489: packet_send();
1.196 djm 490: /* Insert an empty placeholder to maintain ordering */
491: client_register_global_confirm(NULL, NULL);
1.117 markus 492: }
493:
1.13 markus 494: /*
495: * Waits until the client can do something (some data becomes available on
496: * one of the file descriptors).
497: */
1.77 itojun 498: static void
1.46 markus 499: client_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp,
1.130 avsm 500: int *maxfdp, u_int *nallocp, int rekeying)
1.11 markus 501: {
1.117 markus 502: struct timeval tv, *tvp;
1.222 djm 503: int timeout_secs;
1.251 dtucker 504: time_t minwait_secs = 0, server_alive_time = 0, now = monotime();
1.117 markus 505: int ret;
506:
1.46 markus 507: /* Add any selections by the channel mechanism. */
1.239 djm 508: channel_prepare_select(readsetp, writesetp, maxfdp, nallocp,
509: &minwait_secs, rekeying);
1.11 markus 510:
1.292 ! djm 511: /* channel_prepare_select could have closed the last channel */
! 512: if (session_closed && !channel_still_open() &&
! 513: !packet_have_data_to_write()) {
! 514: /* clear mask since we did not call select() */
! 515: memset(*readsetp, 0, *nallocp);
! 516: memset(*writesetp, 0, *nallocp);
! 517: return;
1.16 markus 518: } else {
1.292 ! djm 519: FD_SET(connection_in, *readsetp);
1.16 markus 520: }
1.11 markus 521:
522: /* Select server connection if have data to write to the server. */
523: if (packet_have_data_to_write())
1.46 markus 524: FD_SET(connection_out, *writesetp);
1.11 markus 525:
1.13 markus 526: /*
527: * Wait for something to happen. This will suspend the process until
528: * some selected descriptor can be read, written, or has some other
1.222 djm 529: * event pending, or a timeout expires.
1.13 markus 530: */
1.11 markus 531:
1.222 djm 532: timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */
1.292 ! djm 533: if (options.server_alive_interval > 0) {
1.222 djm 534: timeout_secs = options.server_alive_interval;
1.249 dtucker 535: server_alive_time = now + options.server_alive_interval;
536: }
1.292 ! djm 537: if (options.rekey_interval > 0 && !rekeying)
1.287 deraadt 538: timeout_secs = MINIMUM(timeout_secs, packet_get_rekey_timeout());
1.222 djm 539: set_control_persist_exit_time();
540: if (control_persist_exit_time > 0) {
1.287 deraadt 541: timeout_secs = MINIMUM(timeout_secs,
1.249 dtucker 542: control_persist_exit_time - now);
1.222 djm 543: if (timeout_secs < 0)
544: timeout_secs = 0;
545: }
1.239 djm 546: if (minwait_secs != 0)
1.287 deraadt 547: timeout_secs = MINIMUM(timeout_secs, (int)minwait_secs);
1.222 djm 548: if (timeout_secs == INT_MAX)
1.117 markus 549: tvp = NULL;
1.129 deraadt 550: else {
1.222 djm 551: tv.tv_sec = timeout_secs;
1.117 markus 552: tv.tv_usec = 0;
553: tvp = &tv;
554: }
1.222 djm 555:
1.117 markus 556: ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp);
557: if (ret < 0) {
1.11 markus 558: char buf[100];
1.51 markus 559:
560: /*
561: * We have to clear the select masks, because we return.
562: * We have to return, because the mainloop checks for the flags
563: * set by the signal handlers.
564: */
1.87 markus 565: memset(*readsetp, 0, *nallocp);
566: memset(*writesetp, 0, *nallocp);
1.50 deraadt 567:
1.11 markus 568: if (errno == EINTR)
569: return;
570: /* Note: we might still have data in the buffers. */
571: snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno));
572: buffer_append(&stderr_buffer, buf, strlen(buf));
573: quit_pending = 1;
1.249 dtucker 574: } else if (ret == 0) {
575: /*
576: * Timeout. Could have been either keepalive or rekeying.
577: * Keepalive we check here, rekeying is checked in clientloop.
578: */
1.251 dtucker 579: if (server_alive_time != 0 && server_alive_time <= monotime())
1.249 dtucker 580: server_alive_check();
581: }
582:
1.11 markus 583: }
584:
1.77 itojun 585: static void
1.31 markus 586: client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
1.1 deraadt 587: {
1.11 markus 588: /* Flush stdout and stderr buffers. */
1.31 markus 589: if (buffer_len(bout) > 0)
1.197 djm 590: atomicio(vwrite, fileno(stdout), buffer_ptr(bout),
591: buffer_len(bout));
1.31 markus 592: if (buffer_len(berr) > 0)
1.197 djm 593: atomicio(vwrite, fileno(stderr), buffer_ptr(berr),
594: buffer_len(berr));
1.11 markus 595:
1.233 djm 596: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.11 markus 597:
1.13 markus 598: /*
599: * Free (and clear) the buffer to reduce the amount of data that gets
600: * written to swap.
601: */
1.31 markus 602: buffer_free(bin);
603: buffer_free(bout);
604: buffer_free(berr);
1.11 markus 605:
606: /* Send the suspend signal to the program itself. */
607: kill(getpid(), SIGTSTP);
608:
1.132 djm 609: /* Reset window sizes in case they have changed */
610: received_window_change_signal = 1;
1.11 markus 611:
612: /* OK, we have been continued by the user. Reinitialize buffers. */
1.31 markus 613: buffer_init(bin);
614: buffer_init(bout);
615: buffer_init(berr);
1.11 markus 616:
1.233 djm 617: enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.11 markus 618: }
619:
1.77 itojun 620: static void
1.161 deraadt 621: client_process_net_input(fd_set *readset)
1.11 markus 622: {
1.280 markus 623: int len;
1.17 markus 624: char buf[8192];
1.11 markus 625:
1.13 markus 626: /*
627: * Read input from the server, and add any such data to the buffer of
628: * the packet subsystem.
629: */
1.11 markus 630: if (FD_ISSET(connection_in, readset)) {
631: /* Read as much as possible. */
1.280 markus 632: len = read(connection_in, buf, sizeof(buf));
633: if (len == 0) {
1.197 djm 634: /*
635: * Received EOF. The remote host has closed the
636: * connection.
637: */
638: snprintf(buf, sizeof buf,
639: "Connection to %.300s closed by remote host.\r\n",
640: host);
1.1 deraadt 641: buffer_append(&stderr_buffer, buf, strlen(buf));
642: quit_pending = 1;
643: return;
1.11 markus 644: }
1.13 markus 645: /*
646: * There is a kernel bug on Solaris that causes select to
647: * sometimes wake up even though there is no data available.
648: */
1.53 millert 649: if (len < 0 && (errno == EAGAIN || errno == EINTR))
1.11 markus 650: len = 0;
651:
652: if (len < 0) {
1.197 djm 653: /*
654: * An error has encountered. Perhaps there is a
655: * network problem.
656: */
657: snprintf(buf, sizeof buf,
658: "Read from remote host %.300s: %.100s\r\n",
659: host, strerror(errno));
1.11 markus 660: buffer_append(&stderr_buffer, buf, strlen(buf));
661: quit_pending = 1;
662: return;
663: }
664: packet_process_incoming(buf, len);
665: }
1.17 markus 666: }
1.16 markus 667:
1.97 jakob 668: static void
1.190 djm 669: client_status_confirm(int type, Channel *c, void *ctx)
1.123 djm 670: {
1.190 djm 671: struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx;
672: char errmsg[256];
673: int tochan;
674:
1.234 djm 675: /*
676: * If a TTY was explicitly requested, then a failure to allocate
677: * one is fatal.
678: */
679: if (cr->action == CONFIRM_TTY &&
680: (options.request_tty == REQUEST_TTY_FORCE ||
681: options.request_tty == REQUEST_TTY_YES))
682: cr->action = CONFIRM_CLOSE;
683:
1.190 djm 684: /* XXX supress on mux _client_ quietmode */
685: tochan = options.log_level >= SYSLOG_LEVEL_ERROR &&
1.217 djm 686: c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE;
1.190 djm 687:
688: if (type == SSH2_MSG_CHANNEL_SUCCESS) {
689: debug2("%s request accepted on channel %d",
690: cr->request_type, c->self);
691: } else if (type == SSH2_MSG_CHANNEL_FAILURE) {
692: if (tochan) {
693: snprintf(errmsg, sizeof(errmsg),
694: "%s request failed\r\n", cr->request_type);
695: } else {
696: snprintf(errmsg, sizeof(errmsg),
697: "%s request failed on channel %d",
698: cr->request_type, c->self);
699: }
700: /* If error occurred on primary session channel, then exit */
1.234 djm 701: if (cr->action == CONFIRM_CLOSE && c->self == session_ident)
1.190 djm 702: fatal("%s", errmsg);
1.234 djm 703: /*
704: * If error occurred on mux client, append to
705: * their stderr.
706: */
707: if (tochan) {
708: buffer_append(&c->extended, errmsg,
709: strlen(errmsg));
710: } else
1.190 djm 711: error("%s", errmsg);
1.234 djm 712: if (cr->action == CONFIRM_TTY) {
713: /*
714: * If a TTY allocation error occurred, then arrange
715: * for the correct TTY to leave raw mode.
716: */
717: if (c->self == session_ident)
718: leave_raw_mode(0);
719: else
720: mux_tty_alloc_failed(c);
721: } else if (cr->action == CONFIRM_CLOSE) {
1.190 djm 722: chan_read_failed(c);
723: chan_write_failed(c);
724: }
725: }
1.250 djm 726: free(cr);
1.190 djm 727: }
728:
729: static void
730: client_abandon_status_confirm(Channel *c, void *ctx)
731: {
1.250 djm 732: free(ctx);
1.190 djm 733: }
1.129 deraadt 734:
1.236 djm 735: void
1.234 djm 736: client_expect_confirm(int id, const char *request,
737: enum confirm_action action)
1.190 djm 738: {
1.255 djm 739: struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr));
1.123 djm 740:
1.190 djm 741: cr->request_type = request;
1.234 djm 742: cr->action = action;
1.123 djm 743:
1.190 djm 744: channel_register_status_confirm(id, client_status_confirm,
745: client_abandon_status_confirm, cr);
1.196 djm 746: }
747:
748: void
749: client_register_global_confirm(global_confirm_cb *cb, void *ctx)
750: {
1.201 djm 751: struct global_confirm *gc, *last_gc;
1.196 djm 752:
753: /* Coalesce identical callbacks */
1.201 djm 754: last_gc = TAILQ_LAST(&global_confirms, global_confirms);
755: if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) {
756: if (++last_gc->ref_count >= INT_MAX)
757: fatal("%s: last_gc->ref_count = %d",
758: __func__, last_gc->ref_count);
1.196 djm 759: return;
760: }
761:
1.255 djm 762: gc = xcalloc(1, sizeof(*gc));
1.196 djm 763: gc->cb = cb;
764: gc->ctx = ctx;
765: gc->ref_count = 1;
766: TAILQ_INSERT_TAIL(&global_confirms, gc, entry);
1.123 djm 767: }
768:
769: static void
1.99 markus 770: process_cmdline(void)
1.97 jakob 771: {
772: void (*handler)(int);
1.261 millert 773: char *s, *cmd;
774: int ok, delete = 0, local = 0, remote = 0, dynamic = 0;
775: struct Forward fwd;
1.97 jakob 776:
1.257 tedu 777: memset(&fwd, 0, sizeof(fwd));
1.183 djm 778:
1.233 djm 779: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.101 markus 780: handler = signal(SIGINT, SIG_IGN);
1.99 markus 781: cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
1.97 jakob 782: if (s == NULL)
783: goto out;
1.256 deraadt 784: while (isspace((u_char)*s))
1.97 jakob 785: s++;
1.121 djm 786: if (*s == '-')
787: s++; /* Skip cmdline '-', if any */
1.120 dtucker 788: if (*s == '\0')
1.97 jakob 789: goto out;
1.121 djm 790:
1.122 djm 791: if (*s == 'h' || *s == 'H' || *s == '?') {
1.121 djm 792: logit("Commands:");
1.164 djm 793: logit(" -L[bind_address:]port:host:hostport "
794: "Request local forward");
795: logit(" -R[bind_address:]port:host:hostport "
796: "Request remote forward");
1.204 stevesk 797: logit(" -D[bind_address:]port "
798: "Request dynamic forward");
1.237 markus 799: logit(" -KL[bind_address:]port "
800: "Cancel local forward");
1.165 stevesk 801: logit(" -KR[bind_address:]port "
1.164 djm 802: "Cancel remote forward");
1.237 markus 803: logit(" -KD[bind_address:]port "
804: "Cancel dynamic forward");
1.146 reyk 805: if (!options.permit_local_command)
806: goto out;
1.164 djm 807: logit(" !args "
808: "Execute local command");
1.146 reyk 809: goto out;
810: }
811:
812: if (*s == '!' && options.permit_local_command) {
813: s++;
814: ssh_local_cmd(s);
1.121 djm 815: goto out;
816: }
817:
818: if (*s == 'K') {
819: delete = 1;
820: s++;
821: }
1.204 stevesk 822: if (*s == 'L')
823: local = 1;
824: else if (*s == 'R')
825: remote = 1;
826: else if (*s == 'D')
827: dynamic = 1;
828: else {
1.109 itojun 829: logit("Invalid command.");
1.97 jakob 830: goto out;
831: }
1.204 stevesk 832:
1.256 deraadt 833: while (isspace((u_char)*++s))
1.179 tedu 834: ;
1.97 jakob 835:
1.217 djm 836: /* XXX update list of forwards in options */
1.121 djm 837: if (delete) {
1.261 millert 838: /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */
839: if (!parse_forward(&fwd, s, 1, 0)) {
840: logit("Bad forwarding close specification.");
1.121 djm 841: goto out;
842: }
1.237 markus 843: if (remote)
1.261 millert 844: ok = channel_request_rforward_cancel(&fwd) == 0;
1.237 markus 845: else if (dynamic)
1.261 millert 846: ok = channel_cancel_lport_listener(&fwd,
847: 0, &options.fwd_opts) > 0;
1.237 markus 848: else
1.261 millert 849: ok = channel_cancel_lport_listener(&fwd,
850: CHANNEL_CANCEL_PORT_STATIC,
851: &options.fwd_opts) > 0;
1.237 markus 852: if (!ok) {
1.290 dtucker 853: logit("Unknown port forwarding.");
1.237 markus 854: goto out;
855: }
856: logit("Canceled forwarding.");
1.121 djm 857: } else {
1.209 djm 858: if (!parse_forward(&fwd, s, dynamic, remote)) {
1.121 djm 859: logit("Bad forwarding specification.");
860: goto out;
861: }
1.204 stevesk 862: if (local || dynamic) {
1.261 millert 863: if (!channel_setup_local_fwd_listener(&fwd,
864: &options.fwd_opts)) {
1.121 djm 865: logit("Port forwarding failed.");
866: goto out;
867: }
1.135 djm 868: } else {
1.261 millert 869: if (channel_request_remote_forwarding(&fwd) < 0) {
1.167 markus 870: logit("Port forwarding failed.");
871: goto out;
872: }
1.135 djm 873: }
1.121 djm 874: logit("Forwarding port.");
875: }
876:
1.97 jakob 877: out:
878: signal(SIGINT, handler);
1.233 djm 879: enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.250 djm 880: free(cmd);
881: free(fwd.listen_host);
1.261 millert 882: free(fwd.listen_path);
1.250 djm 883: free(fwd.connect_host);
1.261 millert 884: free(fwd.connect_path);
1.97 jakob 885: }
886:
1.244 dtucker 887: /* reasons to suppress output of an escape command in help output */
888: #define SUPPRESS_NEVER 0 /* never suppress, always show */
1.292 ! djm 889: #define SUPPRESS_MUXCLIENT 1 /* don't show in mux client sessions */
! 890: #define SUPPRESS_MUXMASTER 2 /* don't show in mux master sessions */
! 891: #define SUPPRESS_SYSLOG 4 /* don't show when logging to syslog */
1.244 dtucker 892: struct escape_help_text {
893: const char *cmd;
894: const char *text;
895: unsigned int flags;
896: };
897: static struct escape_help_text esc_txt[] = {
898: {".", "terminate session", SUPPRESS_MUXMASTER},
899: {".", "terminate connection (and any multiplexed sessions)",
900: SUPPRESS_MUXCLIENT},
1.292 ! djm 901: {"B", "send a BREAK to the remote system", SUPPRESS_NEVER},
1.244 dtucker 902: {"C", "open a command line", SUPPRESS_MUXCLIENT},
1.292 ! djm 903: {"R", "request rekey", SUPPRESS_NEVER},
1.246 dtucker 904: {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT},
1.244 dtucker 905: {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT},
906: {"#", "list forwarded connections", SUPPRESS_NEVER},
907: {"&", "background ssh (when waiting for connections to terminate)",
908: SUPPRESS_MUXCLIENT},
909: {"?", "this message", SUPPRESS_NEVER},
910: };
911:
912: static void
1.292 ! djm 913: print_escape_help(Buffer *b, int escape_char, int mux_client, int using_stderr)
1.244 dtucker 914: {
915: unsigned int i, suppress_flags;
916: char string[1024];
917:
918: snprintf(string, sizeof string, "%c?\r\n"
919: "Supported escape sequences:\r\n", escape_char);
920: buffer_append(b, string, strlen(string));
921:
1.292 ! djm 922: suppress_flags =
1.244 dtucker 923: (mux_client ? SUPPRESS_MUXCLIENT : 0) |
924: (mux_client ? 0 : SUPPRESS_MUXMASTER) |
925: (using_stderr ? 0 : SUPPRESS_SYSLOG);
926:
927: for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) {
928: if (esc_txt[i].flags & suppress_flags)
929: continue;
1.246 dtucker 930: snprintf(string, sizeof string, " %c%-3s - %s\r\n",
1.244 dtucker 931: escape_char, esc_txt[i].cmd, esc_txt[i].text);
932: buffer_append(b, string, strlen(string));
933: }
934:
935: snprintf(string, sizeof string,
1.246 dtucker 936: " %c%c - send the escape character by typing it twice\r\n"
1.244 dtucker 937: "(Note that escapes are only recognized immediately after "
938: "newline.)\r\n", escape_char, escape_char);
939: buffer_append(b, string, strlen(string));
940: }
941:
1.195 djm 942: /*
943: * Process the characters one by one, call with c==NULL for proto1 case.
944: */
1.77 itojun 945: static int
1.195 djm 946: process_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr,
947: char *buf, int len)
1.31 markus 948: {
1.32 markus 949: char string[1024];
1.31 markus 950: pid_t pid;
951: int bytes = 0;
1.42 markus 952: u_int i;
953: u_char ch;
1.31 markus 954: char *s;
1.195 djm 955: int *escape_pendingp, escape_char;
956: struct escape_filter_ctx *efc;
1.31 markus 957:
1.195 djm 958: if (c == NULL) {
959: escape_pendingp = &escape_pending1;
960: escape_char = escape_char1;
961: } else {
962: if (c->filter_ctx == NULL)
963: return 0;
964: efc = (struct escape_filter_ctx *)c->filter_ctx;
965: escape_pendingp = &efc->escape_pending;
966: escape_char = efc->escape_char;
967: }
968:
1.139 djm 969: if (len <= 0)
970: return (0);
971:
972: for (i = 0; i < (u_int)len; i++) {
1.31 markus 973: /* Get one character at a time. */
974: ch = buf[i];
975:
1.195 djm 976: if (*escape_pendingp) {
1.31 markus 977: /* We have previously seen an escape character. */
978: /* Clear the flag now. */
1.195 djm 979: *escape_pendingp = 0;
1.31 markus 980:
981: /* Process the escaped character. */
982: switch (ch) {
983: case '.':
984: /* Terminate the connection. */
1.195 djm 985: snprintf(string, sizeof string, "%c.\r\n",
986: escape_char);
1.32 markus 987: buffer_append(berr, string, strlen(string));
1.31 markus 988:
1.217 djm 989: if (c && c->ctl_chan != -1) {
1.195 djm 990: chan_read_failed(c);
991: chan_write_failed(c);
1.252 dtucker 992: if (c->detach_user)
993: c->detach_user(c->self, NULL);
1.253 dtucker 994: c->type = SSH_CHANNEL_ABANDONED;
995: buffer_clear(&c->input);
996: chan_ibuf_empty(c);
1.195 djm 997: return 0;
998: } else
999: quit_pending = 1;
1.31 markus 1000: return -1;
1001:
1002: case 'Z' - 64:
1.195 djm 1003: /* XXX support this for mux clients */
1.217 djm 1004: if (c && c->ctl_chan != -1) {
1.245 dtucker 1005: char b[16];
1.195 djm 1006: noescape:
1.245 dtucker 1007: if (ch == 'Z' - 64)
1008: snprintf(b, sizeof b, "^Z");
1009: else
1010: snprintf(b, sizeof b, "%c", ch);
1.195 djm 1011: snprintf(string, sizeof string,
1.245 dtucker 1012: "%c%s escape not available to "
1.195 djm 1013: "multiplexed sessions\r\n",
1.245 dtucker 1014: escape_char, b);
1.195 djm 1015: buffer_append(berr, string,
1016: strlen(string));
1017: continue;
1018: }
1.197 djm 1019: /* Suspend the program. Inform the user */
1.195 djm 1020: snprintf(string, sizeof string,
1021: "%c^Z [suspend ssh]\r\n", escape_char);
1.32 markus 1022: buffer_append(berr, string, strlen(string));
1.31 markus 1023:
1024: /* Restore terminal modes and suspend. */
1025: client_suspend_self(bin, bout, berr);
1026:
1027: /* We have been continued. */
1028: continue;
1029:
1.111 markus 1030: case 'B':
1.292 ! djm 1031: snprintf(string, sizeof string,
! 1032: "%cB\r\n", escape_char);
! 1033: buffer_append(berr, string, strlen(string));
! 1034: channel_request_start(c->self, "break", 0);
! 1035: packet_put_int(1000);
! 1036: packet_send();
1.111 markus 1037: continue;
1038:
1.54 markus 1039: case 'R':
1.292 ! djm 1040: if (datafellows & SSH_BUG_NOREKEY)
! 1041: logit("Server does not "
! 1042: "support re-keying");
! 1043: else
! 1044: need_rekeying = 1;
1.54 markus 1045: continue;
1046:
1.242 dtucker 1047: case 'V':
1048: /* FALLTHROUGH */
1049: case 'v':
1050: if (c && c->ctl_chan != -1)
1051: goto noescape;
1052: if (!log_is_on_stderr()) {
1053: snprintf(string, sizeof string,
1054: "%c%c [Logging to syslog]\r\n",
1055: escape_char, ch);
1056: buffer_append(berr, string,
1057: strlen(string));
1058: continue;
1059: }
1060: if (ch == 'V' && options.log_level >
1061: SYSLOG_LEVEL_QUIET)
1062: log_change_level(--options.log_level);
1063: if (ch == 'v' && options.log_level <
1064: SYSLOG_LEVEL_DEBUG3)
1065: log_change_level(++options.log_level);
1066: snprintf(string, sizeof string,
1067: "%c%c [LogLevel %s]\r\n", escape_char, ch,
1068: log_level_name(options.log_level));
1069: buffer_append(berr, string, strlen(string));
1070: continue;
1071:
1.31 markus 1072: case '&':
1.217 djm 1073: if (c && c->ctl_chan != -1)
1.195 djm 1074: goto noescape;
1.31 markus 1075: /*
1.197 djm 1076: * Detach the program (continue to serve
1077: * connections, but put in background and no
1078: * more new connections).
1.31 markus 1079: */
1080: /* Restore tty modes. */
1.233 djm 1081: leave_raw_mode(
1082: options.request_tty == REQUEST_TTY_FORCE);
1.31 markus 1083:
1084: /* Stop listening for new connections. */
1.86 markus 1085: channel_stop_listening();
1.31 markus 1086:
1.86 markus 1087: snprintf(string, sizeof string,
1088: "%c& [backgrounded]\n", escape_char);
1089: buffer_append(berr, string, strlen(string));
1.31 markus 1090:
1091: /* Fork into background. */
1092: pid = fork();
1093: if (pid < 0) {
1094: error("fork: %.100s", strerror(errno));
1095: continue;
1096: }
1097: if (pid != 0) { /* This is the parent. */
1098: /* The parent just exits. */
1099: exit(0);
1100: }
1101: /* The child continues serving connections. */
1.292 ! djm 1102: buffer_append(bin, "\004", 1);
! 1103: /* fake EOF on stdin */
! 1104: return -1;
1.31 markus 1105: case '?':
1.292 ! djm 1106: print_escape_help(berr, escape_char,
1.244 dtucker 1107: (c && c->ctl_chan != -1),
1108: log_is_on_stderr());
1.31 markus 1109: continue;
1110:
1111: case '#':
1.195 djm 1112: snprintf(string, sizeof string, "%c#\r\n",
1113: escape_char);
1.32 markus 1114: buffer_append(berr, string, strlen(string));
1.31 markus 1115: s = channel_open_message();
1116: buffer_append(berr, s, strlen(s));
1.250 djm 1117: free(s);
1.97 jakob 1118: continue;
1119:
1120: case 'C':
1.217 djm 1121: if (c && c->ctl_chan != -1)
1.206 djm 1122: goto noescape;
1.99 markus 1123: process_cmdline();
1.31 markus 1124: continue;
1125:
1126: default:
1127: if (ch != escape_char) {
1128: buffer_put_char(bin, escape_char);
1129: bytes++;
1130: }
1131: /* Escaped characters fall through here */
1132: break;
1133: }
1134: } else {
1135: /*
1.197 djm 1136: * The previous character was not an escape char.
1137: * Check if this is an escape.
1.31 markus 1138: */
1139: if (last_was_cr && ch == escape_char) {
1.197 djm 1140: /*
1141: * It is. Set the flag and continue to
1142: * next character.
1143: */
1.195 djm 1144: *escape_pendingp = 1;
1.31 markus 1145: continue;
1146: }
1147: }
1148:
1149: /*
1150: * Normal character. Record whether it was a newline,
1151: * and append it to the buffer.
1152: */
1153: last_was_cr = (ch == '\r' || ch == '\n');
1154: buffer_put_char(bin, ch);
1155: bytes++;
1156: }
1157: return bytes;
1158: }
1159:
1.13 markus 1160: /*
1.15 markus 1161: * Get packets from the connection input buffer, and process them as long as
1162: * there are packets available.
1163: *
1164: * Any unknown packets received during the actual
1165: * session cause the session to terminate. This is
1166: * intended to make debugging easier since no
1167: * confirmations are sent. Any compatible protocol
1168: * extensions must be negotiated during the
1169: * preparatory phase.
1170: */
1171:
1.77 itojun 1172: static void
1.49 itojun 1173: client_process_buffered_input_packets(void)
1.15 markus 1174: {
1.265 markus 1175: dispatch_run(DISPATCH_NONBLOCK, &quit_pending, active_state);
1.15 markus 1176: }
1177:
1.31 markus 1178: /* scan buf[] for '~' before sending data to the peer */
1.30 markus 1179:
1.195 djm 1180: /* Helper: allocate a new escape_filter_ctx and fill in its escape char */
1181: void *
1182: client_new_escape_filter_ctx(int escape_char)
1183: {
1184: struct escape_filter_ctx *ret;
1185:
1.255 djm 1186: ret = xcalloc(1, sizeof(*ret));
1.195 djm 1187: ret->escape_pending = 0;
1188: ret->escape_char = escape_char;
1189: return (void *)ret;
1190: }
1191:
1.198 djm 1192: /* Free the escape filter context on channel free */
1193: void
1194: client_filter_cleanup(int cid, void *ctx)
1195: {
1.250 djm 1196: free(ctx);
1.198 djm 1197: }
1198:
1.195 djm 1199: int
1200: client_simple_escape_filter(Channel *c, char *buf, int len)
1.30 markus 1201: {
1.190 djm 1202: if (c->extended_usage != CHAN_EXTENDED_WRITE)
1203: return 0;
1204:
1.195 djm 1205: return process_escapes(c, &c->input, &c->output, &c->extended,
1206: buf, len);
1.30 markus 1207: }
1208:
1.77 itojun 1209: static void
1.60 markus 1210: client_channel_closed(int id, void *arg)
1211: {
1.83 markus 1212: channel_cancel_cleanup(id);
1.60 markus 1213: session_closed = 1;
1.233 djm 1214: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.60 markus 1215: }
1216:
1.15 markus 1217: /*
1.13 markus 1218: * Implements the interactive session with the server. This is called after
1219: * the user has been authenticated, and a command has been started on the
1.72 stevesk 1220: * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character
1221: * used as an escape character for terminating or suspending the session.
1.13 markus 1222: */
1.1 deraadt 1223:
1.20 markus 1224: int
1.30 markus 1225: client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
1.1 deraadt 1226: {
1.46 markus 1227: fd_set *readset = NULL, *writeset = NULL;
1.11 markus 1228: double start_time, total_time;
1.284 djm 1229: int r, max_fd = 0, max_fd2 = 0, len;
1.200 markus 1230: u_int64_t ibytes, obytes;
1.130 avsm 1231: u_int nalloc = 0;
1.11 markus 1232: char buf[100];
1233:
1234: debug("Entering interactive session.");
1.277 semarie 1235:
1.278 semarie 1236: if (options.control_master &&
1.286 djm 1237: !option_clear_or_none(options.control_path)) {
1.278 semarie 1238: debug("pledge: id");
1.285 tb 1239: if (pledge("stdio rpath wpath cpath unix inet dns recvfd proc exec id tty",
1.278 semarie 1240: NULL) == -1)
1241: fatal("%s pledge(): %s", __func__, strerror(errno));
1242:
1243: } else if (options.forward_x11 || options.permit_local_command) {
1.277 semarie 1244: debug("pledge: exec");
1245: if (pledge("stdio rpath wpath cpath unix inet dns proc exec tty",
1246: NULL) == -1)
1247: fatal("%s pledge(): %s", __func__, strerror(errno));
1248:
1249: } else if (options.update_hostkeys) {
1250: debug("pledge: filesystem full");
1251: if (pledge("stdio rpath wpath cpath unix inet dns proc tty",
1252: NULL) == -1)
1253: fatal("%s pledge(): %s", __func__, strerror(errno));
1254:
1.286 djm 1255: } else if (!option_clear_or_none(options.proxy_command) ||
1256: fork_after_authentication_flag) {
1.277 semarie 1257: debug("pledge: proc");
1258: if (pledge("stdio cpath unix inet dns proc tty", NULL) == -1)
1259: fatal("%s pledge(): %s", __func__, strerror(errno));
1260:
1261: } else {
1262: debug("pledge: network");
1263: if (pledge("stdio unix inet dns tty", NULL) == -1)
1264: fatal("%s pledge(): %s", __func__, strerror(errno));
1265: }
1.11 markus 1266:
1267: start_time = get_current_time();
1268:
1269: /* Initialize variables. */
1.195 djm 1270: escape_pending1 = 0;
1.11 markus 1271: last_was_cr = 1;
1272: exit_status = -1;
1273: stdin_eof = 0;
1274: buffer_high = 64 * 1024;
1275: connection_in = packet_get_connection_in();
1276: connection_out = packet_get_connection_out();
1.287 deraadt 1277: max_fd = MAXIMUM(connection_in, connection_out);
1.46 markus 1278:
1.11 markus 1279: quit_pending = 0;
1.195 djm 1280: escape_char1 = escape_char_arg;
1.11 markus 1281:
1282: /* Initialize buffers. */
1283: buffer_init(&stdin_buffer);
1284: buffer_init(&stdout_buffer);
1285: buffer_init(&stderr_buffer);
1286:
1.15 markus 1287: client_init_dispatch();
1288:
1.105 markus 1289: /*
1290: * Set signal handlers, (e.g. to restore non-blocking mode)
1291: * but don't overwrite SIG_IGN, matches behaviour from rsh(1)
1292: */
1.131 djm 1293: if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1294: signal(SIGHUP, signal_handler);
1.105 markus 1295: if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1296: signal(SIGINT, signal_handler);
1297: if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)
1298: signal(SIGQUIT, signal_handler);
1299: if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
1300: signal(SIGTERM, signal_handler);
1.132 djm 1301: signal(SIGWINCH, window_change_handler);
1.11 markus 1302:
1303: if (have_pty)
1.233 djm 1304: enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.11 markus 1305:
1.292 ! djm 1306: session_ident = ssh2_chan_id;
! 1307: if (session_ident != -1) {
! 1308: if (escape_char_arg != SSH_ESCAPECHAR_NONE) {
! 1309: channel_register_filter(session_ident,
! 1310: client_simple_escape_filter, NULL,
! 1311: client_filter_cleanup,
! 1312: client_new_escape_filter_ctx(
! 1313: escape_char_arg));
1.232 djm 1314: }
1.292 ! djm 1315: channel_register_cleanup(session_ident,
! 1316: client_channel_closed, 0);
1.48 markus 1317: }
1.30 markus 1318:
1.11 markus 1319: /* Main loop of the client for the interactive session mode. */
1320: while (!quit_pending) {
1321:
1.13 markus 1322: /* Process buffered packets sent by the server. */
1.11 markus 1323: client_process_buffered_input_packets();
1324:
1.292 ! djm 1325: if (session_closed && !channel_still_open())
1.60 markus 1326: break;
1327:
1.284 djm 1328: if (ssh_packet_is_rekeying(active_state)) {
1.56 markus 1329: debug("rekeying in progress");
1.284 djm 1330: } else if (need_rekeying) {
1331: /* manual rekey request */
1332: debug("need rekeying");
1333: if ((r = kex_start_rekex(active_state)) != 0)
1334: fatal("%s: kex_start_rekex: %s", __func__,
1335: ssh_err(r));
1336: need_rekeying = 0;
1.56 markus 1337: } else {
1338: /*
1339: * Make packets from buffered channel data, and
1340: * enqueue them for sending to the server.
1341: */
1342: if (packet_not_very_much_data_to_write())
1343: channel_output_poll();
1.11 markus 1344:
1.56 markus 1345: /*
1346: * Check if the window size has changed, and buffer a
1347: * message about it to the server if so.
1348: */
1349: client_check_window_change();
1.11 markus 1350:
1.56 markus 1351: if (quit_pending)
1352: break;
1353: }
1.13 markus 1354: /*
1355: * Wait until we have something to do (something becomes
1356: * available on one of the descriptors).
1357: */
1.81 markus 1358: max_fd2 = max_fd;
1.56 markus 1359: client_wait_until_can_do_something(&readset, &writeset,
1.284 djm 1360: &max_fd2, &nalloc, ssh_packet_is_rekeying(active_state));
1.11 markus 1361:
1362: if (quit_pending)
1363: break;
1364:
1.56 markus 1365: /* Do channel operations unless rekeying in progress. */
1.284 djm 1366: if (!ssh_packet_is_rekeying(active_state))
1.56 markus 1367: channel_after_select(readset, writeset);
1.11 markus 1368:
1.17 markus 1369: /* Buffer input from the connection. */
1.46 markus 1370: client_process_net_input(readset);
1.17 markus 1371:
1372: if (quit_pending)
1373: break;
1.11 markus 1374:
1.197 djm 1375: /*
1376: * Send as much buffered packet data as possible to the
1377: * sender.
1378: */
1.46 markus 1379: if (FD_ISSET(connection_out, writeset))
1.11 markus 1380: packet_write_poll();
1.222 djm 1381:
1382: /*
1383: * If we are a backgrounded control master, and the
1384: * timeout has expired without any active client
1385: * connections, then quit.
1386: */
1387: if (control_persist_exit_time > 0) {
1.251 dtucker 1388: if (monotime() >= control_persist_exit_time) {
1.222 djm 1389: debug("ControlPersist timeout expired");
1390: break;
1391: }
1392: }
1.11 markus 1393: }
1.250 djm 1394: free(readset);
1395: free(writeset);
1.11 markus 1396:
1397: /* Terminate the session. */
1398:
1399: /* Stop watching for window change. */
1.132 djm 1400: signal(SIGWINCH, SIG_DFL);
1.211 andreas 1401:
1.292 ! djm 1402: packet_start(SSH2_MSG_DISCONNECT);
! 1403: packet_put_int(SSH2_DISCONNECT_BY_APPLICATION);
! 1404: packet_put_cstring("disconnected by user");
! 1405: packet_put_cstring(""); /* language tag */
! 1406: packet_send();
! 1407: packet_write_wait();
1.11 markus 1408:
1.76 markus 1409: channel_free_all();
1.11 markus 1410:
1.75 markus 1411: if (have_pty)
1.233 djm 1412: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.75 markus 1413:
1414: /* restore blocking io */
1415: if (!isatty(fileno(stdin)))
1416: unset_nonblock(fileno(stdin));
1417: if (!isatty(fileno(stdout)))
1418: unset_nonblock(fileno(stdout));
1419: if (!isatty(fileno(stderr)))
1420: unset_nonblock(fileno(stderr));
1.116 dtucker 1421:
1422: /*
1423: * If there was no shell or command requested, there will be no remote
1424: * exit status to be returned. In that case, clear error code if the
1425: * connection was deliberately terminated at this end.
1426: */
1427: if (no_shell_flag && received_signal == SIGTERM) {
1428: received_signal = 0;
1429: exit_status = 0;
1430: }
1.75 markus 1431:
1.113 markus 1432: if (received_signal)
1.89 itojun 1433: fatal("Killed by signal %d.", (int) received_signal);
1.75 markus 1434:
1.13 markus 1435: /*
1436: * In interactive mode (with pseudo tty) display a message indicating
1437: * that the connection has been closed.
1438: */
1.11 markus 1439: if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) {
1.197 djm 1440: snprintf(buf, sizeof buf,
1441: "Connection to %.64s closed.\r\n", host);
1.11 markus 1442: buffer_append(&stderr_buffer, buf, strlen(buf));
1443: }
1.70 markus 1444:
1.11 markus 1445: /* Output any buffered data for stdout. */
1.231 djm 1446: if (buffer_len(&stdout_buffer) > 0) {
1.230 djm 1447: len = atomicio(vwrite, fileno(stdout),
1448: buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer));
1.231 djm 1449: if (len < 0 || (u_int)len != buffer_len(&stdout_buffer))
1.11 markus 1450: error("Write failed flushing stdout buffer.");
1.231 djm 1451: else
1452: buffer_consume(&stdout_buffer, len);
1.11 markus 1453: }
1454:
1455: /* Output any buffered data for stderr. */
1.231 djm 1456: if (buffer_len(&stderr_buffer) > 0) {
1.230 djm 1457: len = atomicio(vwrite, fileno(stderr),
1458: buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer));
1.231 djm 1459: if (len < 0 || (u_int)len != buffer_len(&stderr_buffer))
1.11 markus 1460: error("Write failed flushing stderr buffer.");
1.231 djm 1461: else
1462: buffer_consume(&stderr_buffer, len);
1.11 markus 1463: }
1464:
1465: /* Clear and free any buffers. */
1.281 jsg 1466: explicit_bzero(buf, sizeof(buf));
1.11 markus 1467: buffer_free(&stdin_buffer);
1468: buffer_free(&stdout_buffer);
1469: buffer_free(&stderr_buffer);
1470:
1471: /* Report bytes transferred, and transfer rates. */
1472: total_time = get_current_time() - start_time;
1.263 markus 1473: packet_get_bytes(&ibytes, &obytes);
1.200 markus 1474: verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds",
1.229 djm 1475: (unsigned long long)obytes, (unsigned long long)ibytes, total_time);
1.11 markus 1476: if (total_time > 0)
1.200 markus 1477: verbose("Bytes per second: sent %.1f, received %.1f",
1478: obytes / total_time, ibytes / total_time);
1.11 markus 1479: /* Return the exit status of the program. */
1480: debug("Exit status %d", exit_status);
1481: return exit_status;
1.15 markus 1482: }
1483:
1484: /*********/
1485:
1.77 itojun 1486: static Channel *
1.289 markus 1487: client_request_forwarded_tcpip(const char *request_type, int rchan,
1488: u_int rwindow, u_int rmaxpack)
1.40 markus 1489: {
1.103 deraadt 1490: Channel *c = NULL;
1.289 markus 1491: struct sshbuf *b = NULL;
1.40 markus 1492: char *listen_address, *originator_address;
1.208 djm 1493: u_short listen_port, originator_port;
1.289 markus 1494: int r;
1.40 markus 1495:
1496: /* Get rest of the packet */
1497: listen_address = packet_get_string(NULL);
1498: listen_port = packet_get_int();
1499: originator_address = packet_get_string(NULL);
1500: originator_port = packet_get_int();
1.93 markus 1501: packet_check_eom();
1.40 markus 1502:
1.261 millert 1503: debug("%s: listen %s port %d, originator %s port %d", __func__,
1504: listen_address, listen_port, originator_address, originator_port);
1.191 djm 1505:
1.260 markus 1506: c = channel_connect_by_listen_address(listen_address, listen_port,
1.191 djm 1507: "forwarded-tcpip", originator_address);
1.40 markus 1508:
1.289 markus 1509: if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1510: if ((b = sshbuf_new()) == NULL) {
1511: error("%s: alloc reply", __func__);
1512: goto out;
1513: }
1514: /* reconstruct and send to muxclient */
1515: if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
1516: (r = sshbuf_put_u8(b, SSH2_MSG_CHANNEL_OPEN)) != 0 ||
1517: (r = sshbuf_put_cstring(b, request_type)) != 0 ||
1518: (r = sshbuf_put_u32(b, rchan)) != 0 ||
1519: (r = sshbuf_put_u32(b, rwindow)) != 0 ||
1520: (r = sshbuf_put_u32(b, rmaxpack)) != 0 ||
1521: (r = sshbuf_put_cstring(b, listen_address)) != 0 ||
1522: (r = sshbuf_put_u32(b, listen_port)) != 0 ||
1523: (r = sshbuf_put_cstring(b, originator_address)) != 0 ||
1524: (r = sshbuf_put_u32(b, originator_port)) != 0 ||
1525: (r = sshbuf_put_stringb(&c->output, b)) != 0) {
1526: error("%s: compose for muxclient %s", __func__,
1527: ssh_err(r));
1528: goto out;
1529: }
1530: }
1531:
1532: out:
1533: sshbuf_free(b);
1.250 djm 1534: free(originator_address);
1535: free(listen_address);
1.40 markus 1536: return c;
1537: }
1538:
1.103 deraadt 1539: static Channel *
1.261 millert 1540: client_request_forwarded_streamlocal(const char *request_type, int rchan)
1541: {
1542: Channel *c = NULL;
1543: char *listen_path;
1544:
1545: /* Get the remote path. */
1546: listen_path = packet_get_string(NULL);
1547: /* XXX: Skip reserved field for now. */
1548: if (packet_get_string_ptr(NULL) == NULL)
1549: fatal("%s: packet_get_string_ptr failed", __func__);
1550: packet_check_eom();
1551:
1552: debug("%s: %s", __func__, listen_path);
1553:
1554: c = channel_connect_by_listen_path(listen_path,
1555: "forwarded-streamlocal@openssh.com", "forwarded-streamlocal");
1556: free(listen_path);
1557: return c;
1558: }
1559:
1560: static Channel *
1.40 markus 1561: client_request_x11(const char *request_type, int rchan)
1562: {
1563: Channel *c = NULL;
1564: char *originator;
1.208 djm 1565: u_short originator_port;
1.67 markus 1566: int sock;
1.40 markus 1567:
1568: if (!options.forward_x11) {
1569: error("Warning: ssh server tried X11 forwarding.");
1.197 djm 1570: error("Warning: this is probably a break-in attempt by a "
1571: "malicious server.");
1.221 djm 1572: return NULL;
1573: }
1.274 djm 1574: if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
1.221 djm 1575: verbose("Rejected X11 connection after ForwardX11Timeout "
1576: "expired");
1.40 markus 1577: return NULL;
1578: }
1579: originator = packet_get_string(NULL);
1580: if (datafellows & SSH_BUG_X11FWD) {
1581: debug2("buggy server: x11 request w/o originator_port");
1582: originator_port = 0;
1583: } else {
1584: originator_port = packet_get_int();
1585: }
1.93 markus 1586: packet_check_eom();
1.40 markus 1587: /* XXX check permission */
1.47 markus 1588: debug("client_request_x11: request from %s %d", originator,
1589: originator_port);
1.250 djm 1590: free(originator);
1.40 markus 1591: sock = x11_connect_display();
1.67 markus 1592: if (sock < 0)
1593: return NULL;
1594: c = channel_new("x11",
1595: SSH_CHANNEL_X11_OPEN, sock, sock, -1,
1.110 markus 1596: CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
1.82 markus 1597: c->force_drain = 1;
1.40 markus 1598: return c;
1599: }
1600:
1.103 deraadt 1601: static Channel *
1.40 markus 1602: client_request_agent(const char *request_type, int rchan)
1603: {
1604: Channel *c = NULL;
1.262 djm 1605: int r, sock;
1.40 markus 1606:
1607: if (!options.forward_agent) {
1608: error("Warning: ssh server tried agent forwarding.");
1.197 djm 1609: error("Warning: this is probably a break-in attempt by a "
1610: "malicious server.");
1.40 markus 1611: return NULL;
1612: }
1.262 djm 1613: if ((r = ssh_get_authentication_socket(&sock)) != 0) {
1614: if (r != SSH_ERR_AGENT_NOT_PRESENT)
1615: debug("%s: ssh_get_authentication_socket: %s",
1616: __func__, ssh_err(r));
1.67 markus 1617: return NULL;
1.262 djm 1618: }
1.67 markus 1619: c = channel_new("authentication agent connection",
1620: SSH_CHANNEL_OPEN, sock, sock, -1,
1.185 dtucker 1621: CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
1.110 markus 1622: "authentication agent connection", 1);
1.82 markus 1623: c->force_drain = 1;
1.40 markus 1624: return c;
1.180 djm 1625: }
1626:
1627: int
1628: client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
1629: {
1630: Channel *c;
1631: int fd;
1632:
1633: if (tun_mode == SSH_TUNMODE_NO)
1634: return 0;
1635:
1636: debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
1637:
1638: /* Open local tunnel device */
1639: if ((fd = tun_open(local_tun, tun_mode)) == -1) {
1640: error("Tunnel device open failed.");
1641: return -1;
1642: }
1643:
1644: c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
1645: CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1);
1646: c->datagram = 1;
1647:
1648: packet_start(SSH2_MSG_CHANNEL_OPEN);
1649: packet_put_cstring("tun@openssh.com");
1650: packet_put_int(c->self);
1651: packet_put_int(c->local_window_max);
1652: packet_put_int(c->local_maxpacket);
1653: packet_put_int(tun_mode);
1654: packet_put_int(remote_tun);
1655: packet_send();
1656:
1657: return 0;
1.40 markus 1658: }
1659:
1.22 markus 1660: /* XXXX move to generic input handler */
1.264 markus 1661: static int
1.94 markus 1662: client_input_channel_open(int type, u_int32_t seq, void *ctxt)
1.22 markus 1663: {
1664: Channel *c = NULL;
1665: char *ctype;
1666: int rchan;
1.102 markus 1667: u_int rmaxpack, rwindow, len;
1.22 markus 1668:
1669: ctype = packet_get_string(&len);
1670: rchan = packet_get_int();
1671: rwindow = packet_get_int();
1672: rmaxpack = packet_get_int();
1673:
1.24 markus 1674: debug("client_input_channel_open: ctype %s rchan %d win %d max %d",
1.22 markus 1675: ctype, rchan, rwindow, rmaxpack);
1676:
1.40 markus 1677: if (strcmp(ctype, "forwarded-tcpip") == 0) {
1.289 markus 1678: c = client_request_forwarded_tcpip(ctype, rchan, rwindow,
1679: rmaxpack);
1.261 millert 1680: } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) {
1681: c = client_request_forwarded_streamlocal(ctype, rchan);
1.40 markus 1682: } else if (strcmp(ctype, "x11") == 0) {
1683: c = client_request_x11(ctype, rchan);
1684: } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) {
1685: c = client_request_agent(ctype, rchan);
1.22 markus 1686: }
1.289 markus 1687: if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) {
1688: debug3("proxied to downstream: %s", ctype);
1689: } else if (c != NULL) {
1.22 markus 1690: debug("confirm %s", ctype);
1691: c->remote_id = rchan;
1692: c->remote_window = rwindow;
1693: c->remote_maxpacket = rmaxpack;
1.69 markus 1694: if (c->type != SSH_CHANNEL_CONNECTING) {
1695: packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1696: packet_put_int(c->remote_id);
1697: packet_put_int(c->self);
1698: packet_put_int(c->local_window);
1699: packet_put_int(c->local_maxpacket);
1700: packet_send();
1701: }
1.22 markus 1702: } else {
1703: debug("failure %s", ctype);
1704: packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1705: packet_put_int(rchan);
1706: packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED);
1.66 markus 1707: if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1.69 markus 1708: packet_put_cstring("open failed");
1.66 markus 1709: packet_put_cstring("");
1710: }
1.22 markus 1711: packet_send();
1712: }
1.250 djm 1713: free(ctype);
1.264 markus 1714: return 0;
1.22 markus 1715: }
1.267 djm 1716:
1.264 markus 1717: static int
1.94 markus 1718: client_input_channel_req(int type, u_int32_t seq, void *ctxt)
1.48 markus 1719: {
1720: Channel *c = NULL;
1.123 djm 1721: int exitval, id, reply, success = 0;
1.48 markus 1722: char *rtype;
1723:
1724: id = packet_get_int();
1.289 markus 1725: c = channel_lookup(id);
1726: if (channel_proxy_upstream(c, type, seq, ctxt))
1727: return 0;
1.48 markus 1728: rtype = packet_get_string(NULL);
1729: reply = packet_get_char();
1730:
1731: debug("client_input_channel_req: channel %d rtype %s reply %d",
1732: id, rtype, reply);
1733:
1.128 djm 1734: if (id == -1) {
1735: error("client_input_channel_req: request for channel -1");
1.289 markus 1736: } else if (c == NULL) {
1.197 djm 1737: error("client_input_channel_req: channel %d: "
1738: "unknown channel", id);
1.193 markus 1739: } else if (strcmp(rtype, "eow@openssh.com") == 0) {
1740: packet_check_eom();
1741: chan_rcvd_eow(c);
1.48 markus 1742: } else if (strcmp(rtype, "exit-status") == 0) {
1.123 djm 1743: exitval = packet_get_int();
1.217 djm 1744: if (c->ctl_chan != -1) {
1745: mux_exit_message(c, exitval);
1.215 djm 1746: success = 1;
1747: } else if (id == session_ident) {
1748: /* Record exit value of local session */
1.123 djm 1749: success = 1;
1750: exit_status = exitval;
1.215 djm 1751: } else {
1.218 djm 1752: /* Probably for a mux channel that has already closed */
1753: debug("%s: no sink for exit-status on channel %d",
1754: __func__, id);
1.123 djm 1755: }
1.93 markus 1756: packet_check_eom();
1.48 markus 1757: }
1.259 djm 1758: if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) {
1.48 markus 1759: packet_start(success ?
1760: SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE);
1.205 markus 1761: packet_put_int(c->remote_id);
1.48 markus 1762: packet_send();
1763: }
1.250 djm 1764: free(rtype);
1.264 markus 1765: return 0;
1.48 markus 1766: }
1.267 djm 1767:
1.269 djm 1768: struct hostkeys_update_ctx {
1769: /* The hostname and (optionally) IP address string for the server */
1770: char *host_str, *ip_str;
1771:
1772: /*
1773: * Keys received from the server and a flag for each indicating
1774: * whether they already exist in known_hosts.
1775: * keys_seen is filled in by hostkeys_find() and later (for new
1776: * keys) by client_global_hostkeys_private_confirm().
1777: */
1778: struct sshkey **keys;
1779: int *keys_seen;
1780: size_t nkeys;
1781:
1782: size_t nnew;
1783:
1784: /*
1785: * Keys that are in known_hosts, but were not present in the update
1786: * from the server (i.e. scheduled to be deleted).
1787: * Filled in by hostkeys_find().
1788: */
1789: struct sshkey **old_keys;
1790: size_t nold;
1791: };
1792:
1793: static void
1794: hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx)
1795: {
1796: size_t i;
1797:
1798: if (ctx == NULL)
1799: return;
1800: for (i = 0; i < ctx->nkeys; i++)
1801: sshkey_free(ctx->keys[i]);
1802: free(ctx->keys);
1803: free(ctx->keys_seen);
1804: for (i = 0; i < ctx->nold; i++)
1805: sshkey_free(ctx->old_keys[i]);
1806: free(ctx->old_keys);
1807: free(ctx->host_str);
1808: free(ctx->ip_str);
1809: free(ctx);
1810: }
1811:
1812: static int
1813: hostkeys_find(struct hostkey_foreach_line *l, void *_ctx)
1814: {
1815: struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1816: size_t i;
1817: struct sshkey **tmp;
1818:
1819: if (l->status != HKF_STATUS_MATCHED || l->key == NULL ||
1820: l->key->type == KEY_RSA1)
1821: return 0;
1822:
1823: /* Mark off keys we've already seen for this host */
1824: for (i = 0; i < ctx->nkeys; i++) {
1825: if (sshkey_equal(l->key, ctx->keys[i])) {
1826: debug3("%s: found %s key at %s:%ld", __func__,
1827: sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum);
1828: ctx->keys_seen[i] = 1;
1829: return 0;
1830: }
1831: }
1832: /* This line contained a key that not offered by the server */
1833: debug3("%s: deprecated %s key at %s:%ld", __func__,
1834: sshkey_ssh_name(l->key), l->path, l->linenum);
1835: if ((tmp = reallocarray(ctx->old_keys, ctx->nold + 1,
1836: sizeof(*ctx->old_keys))) == NULL)
1837: fatal("%s: reallocarray failed nold = %zu",
1838: __func__, ctx->nold);
1839: ctx->old_keys = tmp;
1840: ctx->old_keys[ctx->nold++] = l->key;
1841: l->key = NULL;
1842:
1843: return 0;
1844: }
1845:
1846: static void
1847: update_known_hosts(struct hostkeys_update_ctx *ctx)
1848: {
1.271 djm 1849: int r, was_raw = 0;
1850: int loglevel = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK ?
1.269 djm 1851: SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE;
1852: char *fp, *response;
1853: size_t i;
1854:
1855: for (i = 0; i < ctx->nkeys; i++) {
1856: if (ctx->keys_seen[i] != 2)
1857: continue;
1858: if ((fp = sshkey_fingerprint(ctx->keys[i],
1859: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1860: fatal("%s: sshkey_fingerprint failed", __func__);
1861: do_log2(loglevel, "Learned new hostkey: %s %s",
1862: sshkey_type(ctx->keys[i]), fp);
1863: free(fp);
1864: }
1865: for (i = 0; i < ctx->nold; i++) {
1866: if ((fp = sshkey_fingerprint(ctx->old_keys[i],
1867: options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
1868: fatal("%s: sshkey_fingerprint failed", __func__);
1869: do_log2(loglevel, "Deprecating obsolete hostkey: %s %s",
1870: sshkey_type(ctx->old_keys[i]), fp);
1871: free(fp);
1872: }
1873: if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) {
1.271 djm 1874: if (get_saved_tio() != NULL) {
1875: leave_raw_mode(1);
1876: was_raw = 1;
1877: }
1.269 djm 1878: response = NULL;
1879: for (i = 0; !quit_pending && i < 3; i++) {
1880: free(response);
1881: response = read_passphrase("Accept updated hostkeys? "
1882: "(yes/no): ", RP_ECHO);
1883: if (strcasecmp(response, "yes") == 0)
1884: break;
1885: else if (quit_pending || response == NULL ||
1886: strcasecmp(response, "no") == 0) {
1887: options.update_hostkeys = 0;
1888: break;
1889: } else {
1890: do_log2(loglevel, "Please enter "
1891: "\"yes\" or \"no\"");
1892: }
1893: }
1894: if (quit_pending || i >= 3 || response == NULL)
1895: options.update_hostkeys = 0;
1896: free(response);
1.271 djm 1897: if (was_raw)
1898: enter_raw_mode(1);
1.269 djm 1899: }
1900:
1901: /*
1902: * Now that all the keys are verified, we can go ahead and replace
1903: * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't
1904: * cancel the operation).
1905: */
1906: if (options.update_hostkeys != 0 &&
1907: (r = hostfile_replace_entries(options.user_hostfiles[0],
1908: ctx->host_str, ctx->ip_str, ctx->keys, ctx->nkeys,
1909: options.hash_known_hosts, 0,
1910: options.fingerprint_hash)) != 0)
1911: error("%s: hostfile_replace_entries failed: %s",
1912: __func__, ssh_err(r));
1913: }
1914:
1915: static void
1916: client_global_hostkeys_private_confirm(int type, u_int32_t seq, void *_ctx)
1917: {
1918: struct ssh *ssh = active_state; /* XXX */
1919: struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx;
1920: size_t i, ndone;
1921: struct sshbuf *signdata;
1922: int r;
1923: const u_char *sig;
1924: size_t siglen;
1925:
1926: if (ctx->nnew == 0)
1927: fatal("%s: ctx->nnew == 0", __func__); /* sanity */
1928: if (type != SSH2_MSG_REQUEST_SUCCESS) {
1929: error("Server failed to confirm ownership of "
1930: "private host keys");
1931: hostkeys_update_ctx_free(ctx);
1932: return;
1933: }
1934: if ((signdata = sshbuf_new()) == NULL)
1935: fatal("%s: sshbuf_new failed", __func__);
1936: /* Don't want to accidentally accept an unbound signature */
1937: if (ssh->kex->session_id_len == 0)
1938: fatal("%s: ssh->kex->session_id_len == 0", __func__);
1939: /*
1940: * Expect a signature for each of the ctx->nnew private keys we
1941: * haven't seen before. They will be in the same order as the
1942: * ctx->keys where the corresponding ctx->keys_seen[i] == 0.
1943: */
1944: for (ndone = i = 0; i < ctx->nkeys; i++) {
1945: if (ctx->keys_seen[i])
1946: continue;
1947: /* Prepare data to be signed: session ID, unique string, key */
1948: sshbuf_reset(signdata);
1.270 djm 1949: if ( (r = sshbuf_put_cstring(signdata,
1950: "hostkeys-prove-00@openssh.com")) != 0 ||
1951: (r = sshbuf_put_string(signdata, ssh->kex->session_id,
1.269 djm 1952: ssh->kex->session_id_len)) != 0 ||
1953: (r = sshkey_puts(ctx->keys[i], signdata)) != 0)
1954: fatal("%s: failed to prepare signature: %s",
1955: __func__, ssh_err(r));
1956: /* Extract and verify signature */
1957: if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) {
1958: error("%s: couldn't parse message: %s",
1959: __func__, ssh_err(r));
1960: goto out;
1961: }
1962: if ((r = sshkey_verify(ctx->keys[i], sig, siglen,
1963: sshbuf_ptr(signdata), sshbuf_len(signdata), 0)) != 0) {
1964: error("%s: server gave bad signature for %s key %zu",
1965: __func__, sshkey_type(ctx->keys[i]), i);
1966: goto out;
1967: }
1968: /* Key is good. Mark it as 'seen' */
1969: ctx->keys_seen[i] = 2;
1970: ndone++;
1971: }
1972: if (ndone != ctx->nnew)
1973: fatal("%s: ndone != ctx->nnew (%zu / %zu)", __func__,
1974: ndone, ctx->nnew); /* Shouldn't happen */
1975: ssh_packet_check_eom(ssh);
1976:
1977: /* Make the edits to known_hosts */
1978: update_known_hosts(ctx);
1979: out:
1980: hostkeys_update_ctx_free(ctx);
1981: }
1982:
1.267 djm 1983: /*
1.291 djm 1984: * Returns non-zero if the key is accepted by HostkeyAlgorithms.
1985: * Made slightly less trivial by the multiple RSA signature algorithm names.
1986: */
1987: static int
1988: key_accepted_by_hostkeyalgs(const struct sshkey *key)
1989: {
1990: const char *ktype = sshkey_ssh_name(key);
1991: const char *hostkeyalgs = options.hostkeyalgorithms != NULL ?
1992: options.hostkeyalgorithms : KEX_DEFAULT_PK_ALG;
1993:
1994: if (key == NULL || key->type == KEY_UNSPEC)
1995: return 0;
1996: if (key->type == KEY_RSA &&
1997: (match_pattern_list("rsa-sha2-256", hostkeyalgs, 0) == 1 ||
1998: match_pattern_list("rsa-sha2-512", hostkeyalgs, 0) == 1))
1999: return 1;
2000: return match_pattern_list(ktype, hostkeyalgs, 0) == 1;
2001: }
2002:
2003: /*
1.270 djm 2004: * Handle hostkeys-00@openssh.com global request to inform the client of all
1.267 djm 2005: * the server's hostkeys. The keys are checked against the user's
2006: * HostkeyAlgorithms preference before they are accepted.
2007: */
2008: static int
2009: client_input_hostkeys(void)
2010: {
1.269 djm 2011: struct ssh *ssh = active_state; /* XXX */
1.267 djm 2012: const u_char *blob = NULL;
1.269 djm 2013: size_t i, len = 0;
1.267 djm 2014: struct sshbuf *buf = NULL;
1.269 djm 2015: struct sshkey *key = NULL, **tmp;
2016: int r;
2017: char *fp;
1.267 djm 2018: static int hostkeys_seen = 0; /* XXX use struct ssh */
1.268 djm 2019: extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */
1.272 djm 2020: struct hostkeys_update_ctx *ctx = NULL;
1.267 djm 2021:
2022: if (hostkeys_seen)
2023: fatal("%s: server already sent hostkeys", __func__);
1.269 djm 2024: if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK &&
2025: options.batch_mode)
2026: return 1; /* won't ask in batchmode, so don't even try */
1.267 djm 2027: if (!options.update_hostkeys || options.num_user_hostfiles <= 0)
2028: return 1;
1.272 djm 2029:
2030: ctx = xcalloc(1, sizeof(*ctx));
1.269 djm 2031: while (ssh_packet_remaining(ssh) > 0) {
1.267 djm 2032: sshkey_free(key);
2033: key = NULL;
1.269 djm 2034: if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) {
2035: error("%s: couldn't parse message: %s",
2036: __func__, ssh_err(r));
2037: goto out;
2038: }
1.270 djm 2039: if ((r = sshkey_from_blob(blob, len, &key)) != 0) {
2040: error("%s: parse key: %s", __func__, ssh_err(r));
2041: goto out;
2042: }
1.267 djm 2043: fp = sshkey_fingerprint(key, options.fingerprint_hash,
2044: SSH_FP_DEFAULT);
2045: debug3("%s: received %s key %s", __func__,
2046: sshkey_type(key), fp);
2047: free(fp);
1.275 markus 2048:
1.291 djm 2049: if (!key_accepted_by_hostkeyalgs(key)) {
1.267 djm 2050: debug3("%s: %s key not permitted by HostkeyAlgorithms",
2051: __func__, sshkey_ssh_name(key));
2052: continue;
2053: }
1.269 djm 2054: /* Skip certs */
2055: if (sshkey_is_cert(key)) {
2056: debug3("%s: %s key is a certificate; skipping",
2057: __func__, sshkey_ssh_name(key));
2058: continue;
2059: }
2060: /* Ensure keys are unique */
2061: for (i = 0; i < ctx->nkeys; i++) {
2062: if (sshkey_equal(key, ctx->keys[i])) {
2063: error("%s: received duplicated %s host key",
2064: __func__, sshkey_ssh_name(key));
2065: goto out;
2066: }
2067: }
2068: /* Key is good, record it */
2069: if ((tmp = reallocarray(ctx->keys, ctx->nkeys + 1,
2070: sizeof(*ctx->keys))) == NULL)
2071: fatal("%s: reallocarray failed nkeys = %zu",
2072: __func__, ctx->nkeys);
2073: ctx->keys = tmp;
2074: ctx->keys[ctx->nkeys++] = key;
1.267 djm 2075: key = NULL;
2076: }
2077:
1.269 djm 2078: if (ctx->nkeys == 0) {
1.270 djm 2079: debug("%s: server sent no hostkeys", __func__);
1.267 djm 2080: goto out;
2081: }
1.270 djm 2082:
1.269 djm 2083: if ((ctx->keys_seen = calloc(ctx->nkeys,
2084: sizeof(*ctx->keys_seen))) == NULL)
2085: fatal("%s: calloc failed", __func__);
1.267 djm 2086:
1.268 djm 2087: get_hostfile_hostname_ipaddr(host,
2088: options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL,
1.269 djm 2089: options.port, &ctx->host_str,
2090: options.check_host_ip ? &ctx->ip_str : NULL);
1.268 djm 2091:
1.269 djm 2092: /* Find which keys we already know about. */
2093: if ((r = hostkeys_foreach(options.user_hostfiles[0], hostkeys_find,
2094: ctx, ctx->host_str, ctx->ip_str,
2095: HKF_WANT_PARSE_KEY|HKF_WANT_MATCH)) != 0) {
2096: error("%s: hostkeys_foreach failed: %s", __func__, ssh_err(r));
1.267 djm 2097: goto out;
2098: }
2099:
1.269 djm 2100: /* Figure out if we have any new keys to add */
2101: ctx->nnew = 0;
2102: for (i = 0; i < ctx->nkeys; i++) {
2103: if (!ctx->keys_seen[i])
2104: ctx->nnew++;
2105: }
2106:
2107: debug3("%s: %zu keys from server: %zu new, %zu retained. %zu to remove",
2108: __func__, ctx->nkeys, ctx->nnew, ctx->nkeys - ctx->nnew, ctx->nold);
2109:
2110: if (ctx->nnew == 0 && ctx->nold != 0) {
2111: /* We have some keys to remove. Just do it. */
2112: update_known_hosts(ctx);
2113: } else if (ctx->nnew != 0) {
2114: /*
2115: * We have received hitherto-unseen keys from the server.
2116: * Ask the server to confirm ownership of the private halves.
2117: */
2118: debug3("%s: asking server to prove ownership for %zu keys",
2119: __func__, ctx->nnew);
2120: if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
2121: (r = sshpkt_put_cstring(ssh,
1.270 djm 2122: "hostkeys-prove-00@openssh.com")) != 0 ||
1.269 djm 2123: (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */
2124: fatal("%s: cannot prepare packet: %s",
2125: __func__, ssh_err(r));
2126: if ((buf = sshbuf_new()) == NULL)
2127: fatal("%s: sshbuf_new", __func__);
2128: for (i = 0; i < ctx->nkeys; i++) {
2129: if (ctx->keys_seen[i])
2130: continue;
2131: sshbuf_reset(buf);
2132: if ((r = sshkey_putb(ctx->keys[i], buf)) != 0)
2133: fatal("%s: sshkey_putb: %s",
2134: __func__, ssh_err(r));
2135: if ((r = sshpkt_put_stringb(ssh, buf)) != 0)
2136: fatal("%s: sshpkt_put_string: %s",
2137: __func__, ssh_err(r));
2138: }
2139: if ((r = sshpkt_send(ssh)) != 0)
2140: fatal("%s: sshpkt_send: %s", __func__, ssh_err(r));
2141: client_register_global_confirm(
2142: client_global_hostkeys_private_confirm, ctx);
2143: ctx = NULL; /* will be freed in callback */
2144: }
2145:
1.267 djm 2146: /* Success */
2147: out:
1.269 djm 2148: hostkeys_update_ctx_free(ctx);
1.267 djm 2149: sshkey_free(key);
2150: sshbuf_free(buf);
1.269 djm 2151: /*
2152: * NB. Return success for all cases. The server doesn't need to know
2153: * what the client does with its hosts file.
2154: */
2155: return 1;
1.267 djm 2156: }
2157:
1.264 markus 2158: static int
1.95 markus 2159: client_input_global_request(int type, u_int32_t seq, void *ctxt)
2160: {
2161: char *rtype;
2162: int want_reply;
2163: int success = 0;
2164:
1.267 djm 2165: rtype = packet_get_cstring(NULL);
1.95 markus 2166: want_reply = packet_get_char();
1.117 markus 2167: debug("client_input_global_request: rtype %s want_reply %d",
2168: rtype, want_reply);
1.270 djm 2169: if (strcmp(rtype, "hostkeys-00@openssh.com") == 0)
1.267 djm 2170: success = client_input_hostkeys();
1.95 markus 2171: if (want_reply) {
2172: packet_start(success ?
2173: SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE);
2174: packet_send();
2175: packet_write_wait();
2176: }
1.250 djm 2177: free(rtype);
1.264 markus 2178: return 0;
1.95 markus 2179: }
1.22 markus 2180:
1.123 djm 2181: void
1.129 deraadt 2182: client_session2_setup(int id, int want_tty, int want_subsystem,
1.190 djm 2183: const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env)
1.123 djm 2184: {
2185: int len;
1.132 djm 2186: Channel *c = NULL;
1.123 djm 2187:
2188: debug2("%s: id %d", __func__, id);
2189:
1.132 djm 2190: if ((c = channel_lookup(id)) == NULL)
2191: fatal("client_session2_setup: channel %d: unknown channel", id);
1.224 djm 2192:
2193: packet_set_interactive(want_tty,
2194: options.ip_qos_interactive, options.ip_qos_bulk);
1.132 djm 2195:
1.123 djm 2196: if (want_tty) {
2197: struct winsize ws;
2198:
2199: /* Store window size in the packet. */
2200: if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0)
2201: memset(&ws, 0, sizeof(ws));
2202:
1.190 djm 2203: channel_request_start(id, "pty-req", 1);
1.234 djm 2204: client_expect_confirm(id, "PTY allocation", CONFIRM_TTY);
1.123 djm 2205: packet_put_cstring(term != NULL ? term : "");
1.158 deraadt 2206: packet_put_int((u_int)ws.ws_col);
2207: packet_put_int((u_int)ws.ws_row);
2208: packet_put_int((u_int)ws.ws_xpixel);
2209: packet_put_int((u_int)ws.ws_ypixel);
1.194 djm 2210: if (tiop == NULL)
2211: tiop = get_saved_tio();
2212: tty_make_modes(-1, tiop);
1.123 djm 2213: packet_send();
2214: /* XXX wait for reply */
1.132 djm 2215: c->client_tty = 1;
1.123 djm 2216: }
2217:
2218: /* Transfer any environment variables from client to server */
1.126 djm 2219: if (options.num_send_env != 0 && env != NULL) {
1.123 djm 2220: int i, j, matched;
2221: char *name, *val;
2222:
2223: debug("Sending environment.");
1.126 djm 2224: for (i = 0; env[i] != NULL; i++) {
1.123 djm 2225: /* Split */
1.126 djm 2226: name = xstrdup(env[i]);
1.123 djm 2227: if ((val = strchr(name, '=')) == NULL) {
1.250 djm 2228: free(name);
1.123 djm 2229: continue;
2230: }
2231: *val++ = '\0';
2232:
2233: matched = 0;
2234: for (j = 0; j < options.num_send_env; j++) {
2235: if (match_pattern(name, options.send_env[j])) {
2236: matched = 1;
2237: break;
2238: }
2239: }
2240: if (!matched) {
2241: debug3("Ignored env %s", name);
1.250 djm 2242: free(name);
1.123 djm 2243: continue;
2244: }
2245:
2246: debug("Sending env %s = %s", name, val);
2247: channel_request_start(id, "env", 0);
2248: packet_put_cstring(name);
2249: packet_put_cstring(val);
2250: packet_send();
1.250 djm 2251: free(name);
1.123 djm 2252: }
2253: }
2254:
2255: len = buffer_len(cmd);
2256: if (len > 0) {
2257: if (len > 900)
2258: len = 900;
2259: if (want_subsystem) {
1.190 djm 2260: debug("Sending subsystem: %.*s",
2261: len, (u_char*)buffer_ptr(cmd));
2262: channel_request_start(id, "subsystem", 1);
1.234 djm 2263: client_expect_confirm(id, "subsystem", CONFIRM_CLOSE);
1.123 djm 2264: } else {
1.190 djm 2265: debug("Sending command: %.*s",
2266: len, (u_char*)buffer_ptr(cmd));
2267: channel_request_start(id, "exec", 1);
1.234 djm 2268: client_expect_confirm(id, "exec", CONFIRM_CLOSE);
1.123 djm 2269: }
2270: packet_put_string(buffer_ptr(cmd), buffer_len(cmd));
2271: packet_send();
2272: } else {
1.190 djm 2273: channel_request_start(id, "shell", 1);
1.234 djm 2274: client_expect_confirm(id, "shell", CONFIRM_CLOSE);
1.123 djm 2275: packet_send();
2276: }
2277: }
2278:
1.77 itojun 2279: static void
1.292 ! djm 2280: client_init_dispatch(void)
1.16 markus 2281: {
1.55 markus 2282: dispatch_init(&dispatch_protocol_error);
1.100 markus 2283:
1.16 markus 2284: dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose);
2285: dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data);
2286: dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof);
2287: dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data);
1.22 markus 2288: dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open);
1.16 markus 2289: dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation);
2290: dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure);
1.48 markus 2291: dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req);
1.16 markus 2292: dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust);
1.189 djm 2293: dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm);
2294: dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm);
1.95 markus 2295: dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request);
1.55 markus 2296:
2297: /* rekeying */
2298: dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
1.100 markus 2299:
2300: /* global request reply messages */
2301: dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply);
2302: dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply);
1.232 djm 2303: }
2304:
2305: void
2306: client_stop_mux(void)
2307: {
2308: if (options.control_path != NULL && muxserver_sock != -1)
2309: unlink(options.control_path);
2310: /*
1.247 dtucker 2311: * If we are in persist mode, or don't have a shell, signal that we
2312: * should close when all active channels are closed.
1.232 djm 2313: */
1.247 dtucker 2314: if (options.control_persist || no_shell_flag) {
1.232 djm 2315: session_closed = 1;
1.235 djm 2316: setproctitle("[stopped mux]");
2317: }
1.113 markus 2318: }
2319:
2320: /* client specific fatal cleanup */
2321: void
1.114 markus 2322: cleanup_exit(int i)
1.113 markus 2323: {
1.233 djm 2324: leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE);
1.113 markus 2325: leave_non_blocking();
1.192 djm 2326: if (options.control_path != NULL && muxserver_sock != -1)
1.123 djm 2327: unlink(options.control_path);
1.223 djm 2328: ssh_kill_proxy_command();
1.114 markus 2329: _exit(i);
1.1 deraadt 2330: }