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