clientloop.c revision 264377
1264377Sdes/* $OpenBSD: clientloop.c,v 1.258 2014/02/02 03:44:31 djm Exp $ */ 2224638Sbrooks/* $FreeBSD: stable/10/crypto/openssh/clientloop.c 264377 2014-04-12 20:22:59Z des $ */ 357429Smarkm/* 457429Smarkm * Author: Tatu Ylonen <ylo@cs.hut.fi> 557429Smarkm * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 657429Smarkm * All rights reserved 765668Skris * The main loop for the interactive session (client side). 860573Skris * 965668Skris * As far as I am concerned, the code I have written for this software 1065668Skris * can be used freely for any purpose. Any derived versions of this 1165668Skris * software must be clearly marked as such, and if the derived work is 1265668Skris * incompatible with the protocol description in the RFC file, it must be 1365668Skris * called by a name other than "ssh" or "Secure Shell". 1460573Skris * 1560573Skris * 1665668Skris * Copyright (c) 1999 Theo de Raadt. All rights reserved. 1760573Skris * 1865668Skris * Redistribution and use in source and binary forms, with or without 1965668Skris * modification, are permitted provided that the following conditions 2065668Skris * are met: 2165668Skris * 1. Redistributions of source code must retain the above copyright 2265668Skris * notice, this list of conditions and the following disclaimer. 2365668Skris * 2. Redistributions in binary form must reproduce the above copyright 2465668Skris * notice, this list of conditions and the following disclaimer in the 2565668Skris * documentation and/or other materials provided with the distribution. 2665668Skris * 2765668Skris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2865668Skris * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2965668Skris * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 3065668Skris * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 3165668Skris * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 3265668Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3365668Skris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3465668Skris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3565668Skris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3665668Skris * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3765668Skris * 3865668Skris * 3960573Skris * SSH2 support added by Markus Friedl. 4092555Sdes * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved. 4165668Skris * 4265668Skris * Redistribution and use in source and binary forms, with or without 4365668Skris * modification, are permitted provided that the following conditions 4465668Skris * are met: 4565668Skris * 1. Redistributions of source code must retain the above copyright 4665668Skris * notice, this list of conditions and the following disclaimer. 4765668Skris * 2. Redistributions in binary form must reproduce the above copyright 4865668Skris * notice, this list of conditions and the following disclaimer in the 4965668Skris * documentation and/or other materials provided with the distribution. 5065668Skris * 5165668Skris * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 5265668Skris * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 5365668Skris * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 5465668Skris * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 5565668Skris * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 5665668Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 5765668Skris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 5865668Skris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 5965668Skris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 6065668Skris * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 6157429Smarkm */ 6257429Smarkm 6357429Smarkm#include "includes.h" 6457429Smarkm 65162852Sdes#include <sys/types.h> 66162852Sdes#include <sys/ioctl.h> 67162852Sdes#include <sys/param.h> 68162852Sdes#ifdef HAVE_SYS_STAT_H 69162852Sdes# include <sys/stat.h> 70162852Sdes#endif 71162852Sdes#ifdef HAVE_SYS_TIME_H 72162852Sdes# include <sys/time.h> 73162852Sdes#endif 74162852Sdes#include <sys/socket.h> 75162852Sdes 76162852Sdes#include <ctype.h> 77162852Sdes#include <errno.h> 78162852Sdes#ifdef HAVE_PATHS_H 79162852Sdes#include <paths.h> 80162852Sdes#endif 81162852Sdes#include <signal.h> 82162852Sdes#include <stdarg.h> 83162852Sdes#include <stdio.h> 84162852Sdes#include <stdlib.h> 85162852Sdes#include <string.h> 86162852Sdes#include <termios.h> 87162852Sdes#include <pwd.h> 88162852Sdes#include <unistd.h> 89162852Sdes 90181111Sdes#include "openbsd-compat/sys-queue.h" 91162852Sdes#include "xmalloc.h" 9276259Sgreen#include "ssh.h" 9376259Sgreen#include "ssh1.h" 9476259Sgreen#include "ssh2.h" 9557429Smarkm#include "packet.h" 9657429Smarkm#include "buffer.h" 9760573Skris#include "compat.h" 9860573Skris#include "channels.h" 9960573Skris#include "dispatch.h" 10076259Sgreen#include "key.h" 101162852Sdes#include "cipher.h" 10276259Sgreen#include "kex.h" 10376259Sgreen#include "log.h" 10476259Sgreen#include "readconf.h" 10576259Sgreen#include "clientloop.h" 106157016Sdes#include "sshconnect.h" 10776259Sgreen#include "authfd.h" 10876259Sgreen#include "atomicio.h" 109137015Sdes#include "sshpty.h" 11076259Sgreen#include "misc.h" 111137015Sdes#include "match.h" 112137015Sdes#include "msg.h" 113197679Sdes#include "roaming.h" 11460573Skris 11569587Sgreen/* import options */ 11668700Sgreenextern Options options; 11768700Sgreen 11857429Smarkm/* Flag indicating that stdin should be redirected from /dev/null. */ 11957429Smarkmextern int stdin_null_flag; 12057429Smarkm 121126274Sdes/* Flag indicating that no shell has been requested */ 122126274Sdesextern int no_shell_flag; 123126274Sdes 124137015Sdes/* Control socket */ 125204917Sdesextern int muxserver_sock; /* XXX use mux_client_cleanup() instead */ 126137015Sdes 12757429Smarkm/* 12857429Smarkm * Name of the host we are connecting to. This is the name given on the 12957429Smarkm * command line, or the HostName specified for the user-supplied name in a 13057429Smarkm * configuration file. 13157429Smarkm */ 13257429Smarkmextern char *host; 13357429Smarkm 13457429Smarkm/* 13557429Smarkm * Flag to indicate that we have received a window change signal which has 13657429Smarkm * not yet been processed. This will cause a message indicating the new 13757429Smarkm * window size to be sent to the server a little later. This is volatile 13857429Smarkm * because this is updated in a signal handler. 13957429Smarkm */ 14092555Sdesstatic volatile sig_atomic_t received_window_change_signal = 0; 14192555Sdesstatic volatile sig_atomic_t received_signal = 0; 14257429Smarkm 143157016Sdes/* Flag indicating whether the user's terminal is in non-blocking mode. */ 14457429Smarkmstatic int in_non_blocking_mode = 0; 14557429Smarkm 146215116Sdes/* Time when backgrounded control master using ControlPersist should exit */ 147215116Sdesstatic time_t control_persist_exit_time = 0; 148215116Sdes 14957429Smarkm/* Common data for the client loop code. */ 150204917Sdesvolatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */ 151181111Sdesstatic int escape_char1; /* Escape character. (proto1 only) */ 152181111Sdesstatic int escape_pending1; /* Last character was an escape (proto1 only) */ 15357429Smarkmstatic int last_was_cr; /* Last character was a newline. */ 154181111Sdesstatic int exit_status; /* Used to store the command exit status. */ 155181111Sdesstatic int stdin_eof; /* EOF has been encountered on stderr. */ 15657429Smarkmstatic Buffer stdin_buffer; /* Buffer for stdin data. */ 15757429Smarkmstatic Buffer stdout_buffer; /* Buffer for stdout data. */ 15857429Smarkmstatic Buffer stderr_buffer; /* Buffer for stderr data. */ 159215116Sdesstatic u_int buffer_high; /* Soft max buffer size. */ 16057429Smarkmstatic int connection_in; /* Connection to server (input). */ 16157429Smarkmstatic int connection_out; /* Connection to server (output). */ 16276259Sgreenstatic int need_rekeying; /* Set to non-zero if rekeying is requested. */ 163215116Sdesstatic int session_closed; /* In SSH2: login session closed. */ 164215116Sdesstatic int x11_refuse_time; /* If >0, refuse x11 opens after this time. */ 16557429Smarkm 16692555Sdesstatic void client_init_dispatch(void); 16760573Skrisint session_ident = -1; 16860573Skris 169204917Sdesint session_resumed = 0; 170204917Sdes 171181111Sdes/* Track escape per proto2 channel */ 172181111Sdesstruct escape_filter_ctx { 173181111Sdes int escape_pending; 174181111Sdes int escape_char; 175137015Sdes}; 176137015Sdes 177181111Sdes/* Context for channel confirmation replies */ 178181111Sdesstruct channel_reply_ctx { 179181111Sdes const char *request_type; 180226046Sdes int id; 181226046Sdes enum confirm_action action; 182181111Sdes}; 183181111Sdes 184181111Sdes/* Global request success/failure callbacks */ 185181111Sdesstruct global_confirm { 186181111Sdes TAILQ_ENTRY(global_confirm) entry; 187181111Sdes global_confirm_cb *cb; 188181111Sdes void *ctx; 189181111Sdes int ref_count; 190181111Sdes}; 191181111SdesTAILQ_HEAD(global_confirms, global_confirm); 192181111Sdesstatic struct global_confirms global_confirms = 193181111Sdes TAILQ_HEAD_INITIALIZER(global_confirms); 194181111Sdes 19576259Sgreen/*XXX*/ 19676259Sgreenextern Kex *xxx_kex; 19757429Smarkm 198137015Sdesvoid ssh_process_session2_setup(int, int, int, Buffer *); 199137015Sdes 20057429Smarkm/* Restores stdin to blocking mode. */ 20157429Smarkm 20292555Sdesstatic void 20376259Sgreenleave_non_blocking(void) 20457429Smarkm{ 20557429Smarkm if (in_non_blocking_mode) { 206137015Sdes unset_nonblock(fileno(stdin)); 20757429Smarkm in_non_blocking_mode = 0; 20857429Smarkm } 20957429Smarkm} 21057429Smarkm 21157429Smarkm/* Puts stdin terminal in non-blocking mode. */ 21257429Smarkm 21392555Sdesstatic void 21476259Sgreenenter_non_blocking(void) 21557429Smarkm{ 21657429Smarkm in_non_blocking_mode = 1; 217137015Sdes set_nonblock(fileno(stdin)); 21857429Smarkm} 21957429Smarkm 22057429Smarkm/* 22157429Smarkm * Signal handler for the window change signal (SIGWINCH). This just sets a 22257429Smarkm * flag indicating that the window has changed. 22357429Smarkm */ 224162852Sdes/*ARGSUSED */ 22592555Sdesstatic void 22657429Smarkmwindow_change_handler(int sig) 22757429Smarkm{ 22857429Smarkm received_window_change_signal = 1; 22957429Smarkm signal(SIGWINCH, window_change_handler); 23057429Smarkm} 23157429Smarkm 23257429Smarkm/* 23357429Smarkm * Signal handler for signals that cause the program to terminate. These 23457429Smarkm * signals must be trapped to restore terminal modes. 23557429Smarkm */ 236162852Sdes/*ARGSUSED */ 23792555Sdesstatic void 23857429Smarkmsignal_handler(int sig) 23957429Smarkm{ 24092555Sdes received_signal = sig; 24192555Sdes quit_pending = 1; 24257429Smarkm} 24357429Smarkm 24457429Smarkm/* 24557429Smarkm * Returns current time in seconds from Jan 1, 1970 with the maximum 24657429Smarkm * available resolution. 24757429Smarkm */ 24857429Smarkm 24992555Sdesstatic double 25076259Sgreenget_current_time(void) 25157429Smarkm{ 25257429Smarkm struct timeval tv; 25357429Smarkm gettimeofday(&tv, NULL); 25457429Smarkm return (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0; 25557429Smarkm} 25657429Smarkm 257215116Sdes/* 258215116Sdes * Sets control_persist_exit_time to the absolute time when the 259215116Sdes * backgrounded control master should exit due to expiry of the 260215116Sdes * ControlPersist timeout. Sets it to 0 if we are not a backgrounded 261215116Sdes * control master process, or if there is no ControlPersist timeout. 262215116Sdes */ 263215116Sdesstatic void 264215116Sdesset_control_persist_exit_time(void) 265215116Sdes{ 266215116Sdes if (muxserver_sock == -1 || !options.control_persist 267226046Sdes || options.control_persist_timeout == 0) { 268215116Sdes /* not using a ControlPersist timeout */ 269215116Sdes control_persist_exit_time = 0; 270226046Sdes } else if (channel_still_open()) { 271215116Sdes /* some client connections are still open */ 272215116Sdes if (control_persist_exit_time > 0) 273215116Sdes debug2("%s: cancel scheduled exit", __func__); 274215116Sdes control_persist_exit_time = 0; 275215116Sdes } else if (control_persist_exit_time <= 0) { 276215116Sdes /* a client connection has recently closed */ 277255767Sdes control_persist_exit_time = monotime() + 278215116Sdes (time_t)options.control_persist_timeout; 279215116Sdes debug2("%s: schedule exit in %d seconds", __func__, 280215116Sdes options.control_persist_timeout); 281215116Sdes } 282215116Sdes /* else we are already counting down to the timeout */ 283215116Sdes} 284215116Sdes 285240075Sdes#define SSH_X11_VALID_DISPLAY_CHARS ":/.-_" 286240075Sdesstatic int 287240075Sdesclient_x11_display_valid(const char *display) 288240075Sdes{ 289240075Sdes size_t i, dlen; 290240075Sdes 291240075Sdes dlen = strlen(display); 292240075Sdes for (i = 0; i < dlen; i++) { 293262566Sdes if (!isalnum((u_char)display[i]) && 294240075Sdes strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) { 295240075Sdes debug("Invalid character '%c' in DISPLAY", display[i]); 296240075Sdes return 0; 297240075Sdes } 298240075Sdes } 299240075Sdes return 1; 300240075Sdes} 301240075Sdes 302149749Sdes#define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1" 303149749Sdesvoid 304149749Sdesclient_x11_get_proto(const char *display, const char *xauth_path, 305215116Sdes u_int trusted, u_int timeout, char **_proto, char **_data) 306149749Sdes{ 307149749Sdes char cmd[1024]; 308149749Sdes char line[512]; 309149749Sdes char xdisplay[512]; 310149749Sdes static char proto[512], data[512]; 311149749Sdes FILE *f; 312149749Sdes int got_data = 0, generated = 0, do_unlink = 0, i; 313149749Sdes char *xauthdir, *xauthfile; 314149749Sdes struct stat st; 315215116Sdes u_int now; 316149749Sdes 317149749Sdes xauthdir = xauthfile = NULL; 318149749Sdes *_proto = proto; 319149749Sdes *_data = data; 320149749Sdes proto[0] = data[0] = '\0'; 321149749Sdes 322149749Sdes if (xauth_path == NULL ||(stat(xauth_path, &st) == -1)) { 323149749Sdes debug("No xauth program."); 324240075Sdes } else if (!client_x11_display_valid(display)) { 325240075Sdes logit("DISPLAY '%s' invalid, falling back to fake xauth data", 326240075Sdes display); 327149749Sdes } else { 328149749Sdes if (display == NULL) { 329149749Sdes debug("x11_get_proto: DISPLAY not set"); 330149749Sdes return; 331149749Sdes } 332149749Sdes /* 333149749Sdes * Handle FamilyLocal case where $DISPLAY does 334149749Sdes * not match an authorization entry. For this we 335149749Sdes * just try "xauth list unix:displaynum.screennum". 336149749Sdes * XXX: "localhost" match to determine FamilyLocal 337149749Sdes * is not perfect. 338149749Sdes */ 339149749Sdes if (strncmp(display, "localhost:", 10) == 0) { 340149749Sdes snprintf(xdisplay, sizeof(xdisplay), "unix:%s", 341149749Sdes display + 10); 342149749Sdes display = xdisplay; 343149749Sdes } 344149749Sdes if (trusted == 0) { 345149749Sdes xauthdir = xmalloc(MAXPATHLEN); 346149749Sdes xauthfile = xmalloc(MAXPATHLEN); 347221420Sdes mktemp_proto(xauthdir, MAXPATHLEN); 348149749Sdes if (mkdtemp(xauthdir) != NULL) { 349149749Sdes do_unlink = 1; 350149749Sdes snprintf(xauthfile, MAXPATHLEN, "%s/xauthfile", 351149749Sdes xauthdir); 352149749Sdes snprintf(cmd, sizeof(cmd), 353149749Sdes "%s -f %s generate %s " SSH_X11_PROTO 354215116Sdes " untrusted timeout %u 2>" _PATH_DEVNULL, 355215116Sdes xauth_path, xauthfile, display, timeout); 356149749Sdes debug2("x11_get_proto: %s", cmd); 357149749Sdes if (system(cmd) == 0) 358149749Sdes generated = 1; 359215116Sdes if (x11_refuse_time == 0) { 360255767Sdes now = monotime() + 1; 361215116Sdes if (UINT_MAX - timeout < now) 362215116Sdes x11_refuse_time = UINT_MAX; 363215116Sdes else 364215116Sdes x11_refuse_time = now + timeout; 365215116Sdes } 366149749Sdes } 367149749Sdes } 368181111Sdes 369181111Sdes /* 370181111Sdes * When in untrusted mode, we read the cookie only if it was 371181111Sdes * successfully generated as an untrusted one in the step 372181111Sdes * above. 373181111Sdes */ 374181111Sdes if (trusted || generated) { 375181111Sdes snprintf(cmd, sizeof(cmd), 376181111Sdes "%s %s%s list %s 2>" _PATH_DEVNULL, 377181111Sdes xauth_path, 378181111Sdes generated ? "-f " : "" , 379181111Sdes generated ? xauthfile : "", 380181111Sdes display); 381181111Sdes debug2("x11_get_proto: %s", cmd); 382181111Sdes f = popen(cmd, "r"); 383181111Sdes if (f && fgets(line, sizeof(line), f) && 384181111Sdes sscanf(line, "%*s %511s %511s", proto, data) == 2) 385181111Sdes got_data = 1; 386181111Sdes if (f) 387181111Sdes pclose(f); 388181111Sdes } else 389181111Sdes error("Warning: untrusted X11 forwarding setup failed: " 390181111Sdes "xauth key data not generated"); 391149749Sdes } 392149749Sdes 393149749Sdes if (do_unlink) { 394149749Sdes unlink(xauthfile); 395149749Sdes rmdir(xauthdir); 396149749Sdes } 397255767Sdes free(xauthdir); 398255767Sdes free(xauthfile); 399149749Sdes 400149749Sdes /* 401149749Sdes * If we didn't get authentication data, just make up some 402149749Sdes * data. The forwarding code will check the validity of the 403149749Sdes * response anyway, and substitute this data. The X11 404149749Sdes * server, however, will ignore this fake data and use 405149749Sdes * whatever authentication mechanisms it was using otherwise 406149749Sdes * for the local connection. 407149749Sdes */ 408149749Sdes if (!got_data) { 409149749Sdes u_int32_t rnd = 0; 410149749Sdes 411149749Sdes logit("Warning: No xauth data; " 412149749Sdes "using fake authentication data for X11 forwarding."); 413149749Sdes strlcpy(proto, SSH_X11_PROTO, sizeof proto); 414149749Sdes for (i = 0; i < 16; i++) { 415149749Sdes if (i % 4 == 0) 416149749Sdes rnd = arc4random(); 417149749Sdes snprintf(data + 2 * i, sizeof data - 2 * i, "%02x", 418149749Sdes rnd & 0xff); 419149749Sdes rnd >>= 8; 420149749Sdes } 421149749Sdes } 422149749Sdes} 423149749Sdes 42457429Smarkm/* 42557429Smarkm * This is called when the interactive is entered. This checks if there is 42657429Smarkm * an EOF coming on stdin. We must check this explicitly, as select() does 42757429Smarkm * not appear to wake up when redirecting from /dev/null. 42857429Smarkm */ 42957429Smarkm 43092555Sdesstatic void 43176259Sgreenclient_check_initial_eof_on_stdin(void) 43257429Smarkm{ 43357429Smarkm int len; 43457429Smarkm char buf[1]; 43557429Smarkm 43657429Smarkm /* 43757429Smarkm * If standard input is to be "redirected from /dev/null", we simply 43857429Smarkm * mark that we have seen an EOF and send an EOF message to the 43957429Smarkm * server. Otherwise, we try to read a single character; it appears 44057429Smarkm * that for some files, such /dev/null, select() never wakes up for 44157429Smarkm * read for this descriptor, which means that we never get EOF. This 44257429Smarkm * way we will get the EOF if stdin comes from /dev/null or similar. 44357429Smarkm */ 44457429Smarkm if (stdin_null_flag) { 44557429Smarkm /* Fake EOF on stdin. */ 44657429Smarkm debug("Sending eof."); 44757429Smarkm stdin_eof = 1; 44857429Smarkm packet_start(SSH_CMSG_EOF); 44957429Smarkm packet_send(); 45057429Smarkm } else { 45157429Smarkm enter_non_blocking(); 45257429Smarkm 45357429Smarkm /* Check for immediate EOF on stdin. */ 45457429Smarkm len = read(fileno(stdin), buf, 1); 45557429Smarkm if (len == 0) { 456181111Sdes /* 457181111Sdes * EOF. Record that we have seen it and send 458181111Sdes * EOF to server. 459181111Sdes */ 46057429Smarkm debug("Sending eof."); 46157429Smarkm stdin_eof = 1; 46257429Smarkm packet_start(SSH_CMSG_EOF); 46357429Smarkm packet_send(); 46457429Smarkm } else if (len > 0) { 46557429Smarkm /* 46657429Smarkm * Got data. We must store the data in the buffer, 46757429Smarkm * and also process it as an escape character if 46857429Smarkm * appropriate. 46957429Smarkm */ 470181111Sdes if ((u_char) buf[0] == escape_char1) 471181111Sdes escape_pending1 = 1; 47276259Sgreen else 47357429Smarkm buffer_append(&stdin_buffer, buf, 1); 47457429Smarkm } 47557429Smarkm leave_non_blocking(); 47657429Smarkm } 47757429Smarkm} 47857429Smarkm 47957429Smarkm 48057429Smarkm/* 48157429Smarkm * Make packets from buffered stdin data, and buffer them for sending to the 48257429Smarkm * connection. 48357429Smarkm */ 48457429Smarkm 48592555Sdesstatic void 48676259Sgreenclient_make_packets_from_stdin_data(void) 48757429Smarkm{ 48876259Sgreen u_int len; 48957429Smarkm 49057429Smarkm /* Send buffered stdin data to the server. */ 49157429Smarkm while (buffer_len(&stdin_buffer) > 0 && 49292555Sdes packet_not_very_much_data_to_write()) { 49357429Smarkm len = buffer_len(&stdin_buffer); 49457429Smarkm /* Keep the packets at reasonable size. */ 49557429Smarkm if (len > packet_get_maxsize()) 49657429Smarkm len = packet_get_maxsize(); 49757429Smarkm packet_start(SSH_CMSG_STDIN_DATA); 49857429Smarkm packet_put_string(buffer_ptr(&stdin_buffer), len); 49957429Smarkm packet_send(); 50057429Smarkm buffer_consume(&stdin_buffer, len); 50157429Smarkm /* If we have a pending EOF, send it now. */ 50257429Smarkm if (stdin_eof && buffer_len(&stdin_buffer) == 0) { 50357429Smarkm packet_start(SSH_CMSG_EOF); 50457429Smarkm packet_send(); 50557429Smarkm } 50657429Smarkm } 50757429Smarkm} 50857429Smarkm 50957429Smarkm/* 51057429Smarkm * Checks if the client window has changed, and sends a packet about it to 51157429Smarkm * the server if so. The actual change is detected elsewhere (by a software 51257429Smarkm * interrupt on Unix); this just checks the flag and sends a message if 51357429Smarkm * appropriate. 51457429Smarkm */ 51557429Smarkm 51692555Sdesstatic void 51776259Sgreenclient_check_window_change(void) 51857429Smarkm{ 51960573Skris struct winsize ws; 52057429Smarkm 52160573Skris if (! received_window_change_signal) 52260573Skris return; 52360573Skris /** XXX race */ 52460573Skris received_window_change_signal = 0; 52557429Smarkm 52669587Sgreen debug2("client_check_window_change: changed"); 52760573Skris 52860573Skris if (compat20) { 529137015Sdes channel_send_window_changes(); 53060573Skris } else { 531137015Sdes if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0) 532137015Sdes return; 53360573Skris packet_start(SSH_CMSG_WINDOW_SIZE); 534162852Sdes packet_put_int((u_int)ws.ws_row); 535162852Sdes packet_put_int((u_int)ws.ws_col); 536162852Sdes packet_put_int((u_int)ws.ws_xpixel); 537162852Sdes packet_put_int((u_int)ws.ws_ypixel); 53860573Skris packet_send(); 53957429Smarkm } 54057429Smarkm} 54157429Smarkm 542126274Sdesstatic void 543126274Sdesclient_global_request_reply(int type, u_int32_t seq, void *ctxt) 544126274Sdes{ 545181111Sdes struct global_confirm *gc; 546181111Sdes 547181111Sdes if ((gc = TAILQ_FIRST(&global_confirms)) == NULL) 548181111Sdes return; 549181111Sdes if (gc->cb != NULL) 550181111Sdes gc->cb(type, seq, gc->ctx); 551181111Sdes if (--gc->ref_count <= 0) { 552181111Sdes TAILQ_REMOVE(&global_confirms, gc, entry); 553264377Sdes explicit_bzero(gc, sizeof(*gc)); 554255767Sdes free(gc); 555181111Sdes } 556181111Sdes 557197679Sdes packet_set_alive_timeouts(0); 558126274Sdes} 559126274Sdes 560126274Sdesstatic void 561126274Sdesserver_alive_check(void) 562126274Sdes{ 563197679Sdes if (packet_inc_alive_timeouts() > options.server_alive_count_max) { 564221420Sdes logit("Timeout, server %s not responding.", host); 565164146Sdes cleanup_exit(255); 566164146Sdes } 567126274Sdes packet_start(SSH2_MSG_GLOBAL_REQUEST); 568126274Sdes packet_put_cstring("keepalive@openssh.com"); 569126274Sdes packet_put_char(1); /* boolean: want reply */ 570126274Sdes packet_send(); 571181111Sdes /* Insert an empty placeholder to maintain ordering */ 572181111Sdes client_register_global_confirm(NULL, NULL); 573126274Sdes} 574126274Sdes 57557429Smarkm/* 57657429Smarkm * Waits until the client can do something (some data becomes available on 57757429Smarkm * one of the file descriptors). 57857429Smarkm */ 57992555Sdesstatic void 58076259Sgreenclient_wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, 581137015Sdes int *maxfdp, u_int *nallocp, int rekeying) 58257429Smarkm{ 583126274Sdes struct timeval tv, *tvp; 584215116Sdes int timeout_secs; 585255767Sdes time_t minwait_secs = 0, server_alive_time = 0, now = monotime(); 586126274Sdes int ret; 587126274Sdes 58876259Sgreen /* Add any selections by the channel mechanism. */ 589240075Sdes channel_prepare_select(readsetp, writesetp, maxfdp, nallocp, 590240075Sdes &minwait_secs, rekeying); 59157429Smarkm 59260573Skris if (!compat20) { 59360573Skris /* Read from the connection, unless our buffers are full. */ 59460573Skris if (buffer_len(&stdout_buffer) < buffer_high && 59560573Skris buffer_len(&stderr_buffer) < buffer_high && 59660573Skris channel_not_very_much_buffered_data()) 59776259Sgreen FD_SET(connection_in, *readsetp); 59860573Skris /* 59960573Skris * Read from stdin, unless we have seen EOF or have very much 60060573Skris * buffered data to send to the server. 60160573Skris */ 60260573Skris if (!stdin_eof && packet_not_very_much_data_to_write()) 60376259Sgreen FD_SET(fileno(stdin), *readsetp); 60460573Skris 60560573Skris /* Select stdout/stderr if have data in buffer. */ 60660573Skris if (buffer_len(&stdout_buffer) > 0) 60776259Sgreen FD_SET(fileno(stdout), *writesetp); 60860573Skris if (buffer_len(&stderr_buffer) > 0) 60976259Sgreen FD_SET(fileno(stderr), *writesetp); 61060573Skris } else { 61192555Sdes /* channel_prepare_select could have closed the last channel */ 61292555Sdes if (session_closed && !channel_still_open() && 61392555Sdes !packet_have_data_to_write()) { 61492555Sdes /* clear mask since we did not call select() */ 61592555Sdes memset(*readsetp, 0, *nallocp); 61692555Sdes memset(*writesetp, 0, *nallocp); 61792555Sdes return; 61892555Sdes } else { 61992555Sdes FD_SET(connection_in, *readsetp); 62092555Sdes } 62160573Skris } 62257429Smarkm 62357429Smarkm /* Select server connection if have data to write to the server. */ 62457429Smarkm if (packet_have_data_to_write()) 62576259Sgreen FD_SET(connection_out, *writesetp); 62657429Smarkm 62757429Smarkm /* 62857429Smarkm * Wait for something to happen. This will suspend the process until 62957429Smarkm * some selected descriptor can be read, written, or has some other 630215116Sdes * event pending, or a timeout expires. 63157429Smarkm */ 63257429Smarkm 633215116Sdes timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */ 634255767Sdes if (options.server_alive_interval > 0 && compat20) { 635215116Sdes timeout_secs = options.server_alive_interval; 636255767Sdes server_alive_time = now + options.server_alive_interval; 637255767Sdes } 638255767Sdes if (options.rekey_interval > 0 && compat20 && !rekeying) 639255767Sdes timeout_secs = MIN(timeout_secs, packet_get_rekey_timeout()); 640215116Sdes set_control_persist_exit_time(); 641215116Sdes if (control_persist_exit_time > 0) { 642215116Sdes timeout_secs = MIN(timeout_secs, 643255767Sdes control_persist_exit_time - now); 644215116Sdes if (timeout_secs < 0) 645215116Sdes timeout_secs = 0; 646215116Sdes } 647240075Sdes if (minwait_secs != 0) 648240075Sdes timeout_secs = MIN(timeout_secs, (int)minwait_secs); 649215116Sdes if (timeout_secs == INT_MAX) 650126274Sdes tvp = NULL; 651137015Sdes else { 652215116Sdes tv.tv_sec = timeout_secs; 653126274Sdes tv.tv_usec = 0; 654126274Sdes tvp = &tv; 655126274Sdes } 656215116Sdes 657126274Sdes ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp); 658126274Sdes if (ret < 0) { 65957429Smarkm char buf[100]; 66076259Sgreen 66176259Sgreen /* 66276259Sgreen * We have to clear the select masks, because we return. 66376259Sgreen * We have to return, because the mainloop checks for the flags 66476259Sgreen * set by the signal handlers. 66576259Sgreen */ 66692555Sdes memset(*readsetp, 0, *nallocp); 66792555Sdes memset(*writesetp, 0, *nallocp); 66876259Sgreen 66957429Smarkm if (errno == EINTR) 67057429Smarkm return; 67157429Smarkm /* Note: we might still have data in the buffers. */ 67257429Smarkm snprintf(buf, sizeof buf, "select: %s\r\n", strerror(errno)); 67357429Smarkm buffer_append(&stderr_buffer, buf, strlen(buf)); 67457429Smarkm quit_pending = 1; 675255767Sdes } else if (ret == 0) { 676255767Sdes /* 677255767Sdes * Timeout. Could have been either keepalive or rekeying. 678255767Sdes * Keepalive we check here, rekeying is checked in clientloop. 679255767Sdes */ 680255767Sdes if (server_alive_time != 0 && server_alive_time <= monotime()) 681255767Sdes server_alive_check(); 682255767Sdes } 683255767Sdes 68457429Smarkm} 68557429Smarkm 68692555Sdesstatic void 68765668Skrisclient_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr) 68857429Smarkm{ 68957429Smarkm /* Flush stdout and stderr buffers. */ 69065668Skris if (buffer_len(bout) > 0) 691181111Sdes atomicio(vwrite, fileno(stdout), buffer_ptr(bout), 692181111Sdes buffer_len(bout)); 69365668Skris if (buffer_len(berr) > 0) 694181111Sdes atomicio(vwrite, fileno(stderr), buffer_ptr(berr), 695181111Sdes buffer_len(berr)); 69657429Smarkm 697226046Sdes leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 69857429Smarkm 69957429Smarkm /* 70057429Smarkm * Free (and clear) the buffer to reduce the amount of data that gets 70157429Smarkm * written to swap. 70257429Smarkm */ 70365668Skris buffer_free(bin); 70465668Skris buffer_free(bout); 70565668Skris buffer_free(berr); 70657429Smarkm 70757429Smarkm /* Send the suspend signal to the program itself. */ 70857429Smarkm kill(getpid(), SIGTSTP); 70957429Smarkm 710146998Sdes /* Reset window sizes in case they have changed */ 711146998Sdes received_window_change_signal = 1; 71257429Smarkm 71357429Smarkm /* OK, we have been continued by the user. Reinitialize buffers. */ 71465668Skris buffer_init(bin); 71565668Skris buffer_init(bout); 71665668Skris buffer_init(berr); 71757429Smarkm 718226046Sdes enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 71957429Smarkm} 72057429Smarkm 72192555Sdesstatic void 722162852Sdesclient_process_net_input(fd_set *readset) 72357429Smarkm{ 724197679Sdes int len, cont = 0; 725197679Sdes char buf[SSH_IOBUFSZ]; 72657429Smarkm 72757429Smarkm /* 72857429Smarkm * Read input from the server, and add any such data to the buffer of 72957429Smarkm * the packet subsystem. 73057429Smarkm */ 73157429Smarkm if (FD_ISSET(connection_in, readset)) { 73257429Smarkm /* Read as much as possible. */ 733197679Sdes len = roaming_read(connection_in, buf, sizeof(buf), &cont); 734197679Sdes if (len == 0 && cont == 0) { 735181111Sdes /* 736181111Sdes * Received EOF. The remote host has closed the 737181111Sdes * connection. 738181111Sdes */ 739181111Sdes snprintf(buf, sizeof buf, 740181111Sdes "Connection to %.300s closed by remote host.\r\n", 741181111Sdes host); 74257429Smarkm buffer_append(&stderr_buffer, buf, strlen(buf)); 74357429Smarkm quit_pending = 1; 74457429Smarkm return; 74557429Smarkm } 74657429Smarkm /* 74757429Smarkm * There is a kernel bug on Solaris that causes select to 74857429Smarkm * sometimes wake up even though there is no data available. 74957429Smarkm */ 750181111Sdes if (len < 0 && 751181111Sdes (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)) 75257429Smarkm len = 0; 75357429Smarkm 75457429Smarkm if (len < 0) { 755181111Sdes /* 756181111Sdes * An error has encountered. Perhaps there is a 757181111Sdes * network problem. 758181111Sdes */ 759181111Sdes snprintf(buf, sizeof buf, 760181111Sdes "Read from remote host %.300s: %.100s\r\n", 761181111Sdes host, strerror(errno)); 76257429Smarkm buffer_append(&stderr_buffer, buf, strlen(buf)); 76357429Smarkm quit_pending = 1; 76457429Smarkm return; 76557429Smarkm } 76657429Smarkm packet_process_incoming(buf, len); 76757429Smarkm } 76860573Skris} 76960573Skris 77098675Sdesstatic void 771181111Sdesclient_status_confirm(int type, Channel *c, void *ctx) 772137015Sdes{ 773181111Sdes struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx; 774181111Sdes char errmsg[256]; 775181111Sdes int tochan; 776137015Sdes 777226046Sdes /* 778226046Sdes * If a TTY was explicitly requested, then a failure to allocate 779226046Sdes * one is fatal. 780226046Sdes */ 781226046Sdes if (cr->action == CONFIRM_TTY && 782226046Sdes (options.request_tty == REQUEST_TTY_FORCE || 783226046Sdes options.request_tty == REQUEST_TTY_YES)) 784226046Sdes cr->action = CONFIRM_CLOSE; 785226046Sdes 786181111Sdes /* XXX supress on mux _client_ quietmode */ 787181111Sdes tochan = options.log_level >= SYSLOG_LEVEL_ERROR && 788204917Sdes c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE; 789137015Sdes 790181111Sdes if (type == SSH2_MSG_CHANNEL_SUCCESS) { 791181111Sdes debug2("%s request accepted on channel %d", 792181111Sdes cr->request_type, c->self); 793181111Sdes } else if (type == SSH2_MSG_CHANNEL_FAILURE) { 794181111Sdes if (tochan) { 795181111Sdes snprintf(errmsg, sizeof(errmsg), 796181111Sdes "%s request failed\r\n", cr->request_type); 797181111Sdes } else { 798181111Sdes snprintf(errmsg, sizeof(errmsg), 799181111Sdes "%s request failed on channel %d", 800181111Sdes cr->request_type, c->self); 801181111Sdes } 802181111Sdes /* If error occurred on primary session channel, then exit */ 803226046Sdes if (cr->action == CONFIRM_CLOSE && c->self == session_ident) 804181111Sdes fatal("%s", errmsg); 805226046Sdes /* 806226046Sdes * If error occurred on mux client, append to 807226046Sdes * their stderr. 808226046Sdes */ 809226046Sdes if (tochan) { 810226046Sdes buffer_append(&c->extended, errmsg, 811226046Sdes strlen(errmsg)); 812226046Sdes } else 813181111Sdes error("%s", errmsg); 814226046Sdes if (cr->action == CONFIRM_TTY) { 815226046Sdes /* 816226046Sdes * If a TTY allocation error occurred, then arrange 817226046Sdes * for the correct TTY to leave raw mode. 818226046Sdes */ 819226046Sdes if (c->self == session_ident) 820226046Sdes leave_raw_mode(0); 821226046Sdes else 822226046Sdes mux_tty_alloc_failed(c); 823226046Sdes } else if (cr->action == CONFIRM_CLOSE) { 824181111Sdes chan_read_failed(c); 825181111Sdes chan_write_failed(c); 826181111Sdes } 827137015Sdes } 828255767Sdes free(cr); 829137015Sdes} 830137015Sdes 831137015Sdesstatic void 832181111Sdesclient_abandon_status_confirm(Channel *c, void *ctx) 833137015Sdes{ 834255767Sdes free(ctx); 835137015Sdes} 836137015Sdes 837226046Sdesvoid 838226046Sdesclient_expect_confirm(int id, const char *request, 839226046Sdes enum confirm_action action) 840137015Sdes{ 841258343Sdes struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr)); 842137015Sdes 843181111Sdes cr->request_type = request; 844226046Sdes cr->action = action; 845137015Sdes 846181111Sdes channel_register_status_confirm(id, client_status_confirm, 847181111Sdes client_abandon_status_confirm, cr); 848181111Sdes} 849137015Sdes 850181111Sdesvoid 851181111Sdesclient_register_global_confirm(global_confirm_cb *cb, void *ctx) 852181111Sdes{ 853181111Sdes struct global_confirm *gc, *last_gc; 854137015Sdes 855181111Sdes /* Coalesce identical callbacks */ 856181111Sdes last_gc = TAILQ_LAST(&global_confirms, global_confirms); 857181111Sdes if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) { 858181111Sdes if (++last_gc->ref_count >= INT_MAX) 859181111Sdes fatal("%s: last_gc->ref_count = %d", 860181111Sdes __func__, last_gc->ref_count); 861146998Sdes return; 862146998Sdes } 863146998Sdes 864258343Sdes gc = xcalloc(1, sizeof(*gc)); 865181111Sdes gc->cb = cb; 866181111Sdes gc->ctx = ctx; 867181111Sdes gc->ref_count = 1; 868181111Sdes TAILQ_INSERT_TAIL(&global_confirms, gc, entry); 869137015Sdes} 870137015Sdes 871137015Sdesstatic void 87298675Sdesprocess_cmdline(void) 87398675Sdes{ 87498675Sdes void (*handler)(int); 875146998Sdes char *s, *cmd, *cancel_host; 876240075Sdes int delete = 0, local = 0, remote = 0, dynamic = 0; 877240075Sdes int cancel_port, ok; 878146998Sdes Forward fwd; 87998675Sdes 880264377Sdes memset(&fwd, 0, sizeof(fwd)); 881181111Sdes fwd.listen_host = fwd.connect_host = NULL; 882181111Sdes 883226046Sdes leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 88498675Sdes handler = signal(SIGINT, SIG_IGN); 88598675Sdes cmd = s = read_passphrase("\r\nssh> ", RP_ECHO); 88698675Sdes if (s == NULL) 88798675Sdes goto out; 888262566Sdes while (isspace((u_char)*s)) 88998675Sdes s++; 890137015Sdes if (*s == '-') 891137015Sdes s++; /* Skip cmdline '-', if any */ 892137015Sdes if (*s == '\0') 89398675Sdes goto out; 894137015Sdes 895137015Sdes if (*s == 'h' || *s == 'H' || *s == '?') { 896137015Sdes logit("Commands:"); 897162852Sdes logit(" -L[bind_address:]port:host:hostport " 898162852Sdes "Request local forward"); 899162852Sdes logit(" -R[bind_address:]port:host:hostport " 900162852Sdes "Request remote forward"); 901192595Sdes logit(" -D[bind_address:]port " 902192595Sdes "Request dynamic forward"); 903240075Sdes logit(" -KL[bind_address:]port " 904240075Sdes "Cancel local forward"); 905162852Sdes logit(" -KR[bind_address:]port " 906162852Sdes "Cancel remote forward"); 907240075Sdes logit(" -KD[bind_address:]port " 908240075Sdes "Cancel dynamic forward"); 909157016Sdes if (!options.permit_local_command) 910157016Sdes goto out; 911162852Sdes logit(" !args " 912162852Sdes "Execute local command"); 913137015Sdes goto out; 914137015Sdes } 915137015Sdes 916157016Sdes if (*s == '!' && options.permit_local_command) { 917157016Sdes s++; 918157016Sdes ssh_local_cmd(s); 919157016Sdes goto out; 920157016Sdes } 921157016Sdes 922137015Sdes if (*s == 'K') { 923137015Sdes delete = 1; 924137015Sdes s++; 925137015Sdes } 926192595Sdes if (*s == 'L') 927192595Sdes local = 1; 928192595Sdes else if (*s == 'R') 929192595Sdes remote = 1; 930192595Sdes else if (*s == 'D') 931192595Sdes dynamic = 1; 932192595Sdes else { 933124208Sdes logit("Invalid command."); 93498675Sdes goto out; 93598675Sdes } 936192595Sdes 937240075Sdes if (delete && !compat20) { 938124208Sdes logit("Not supported for SSH protocol version 1."); 93998675Sdes goto out; 94098675Sdes } 941137015Sdes 942262566Sdes while (isspace((u_char)*++s)) 943181111Sdes ; 94498675Sdes 945204917Sdes /* XXX update list of forwards in options */ 946137015Sdes if (delete) { 947146998Sdes cancel_port = 0; 948146998Sdes cancel_host = hpdelim(&s); /* may be NULL */ 949146998Sdes if (s != NULL) { 950146998Sdes cancel_port = a2port(s); 951146998Sdes cancel_host = cleanhostname(cancel_host); 952146998Sdes } else { 953146998Sdes cancel_port = a2port(cancel_host); 954146998Sdes cancel_host = NULL; 95598675Sdes } 956192595Sdes if (cancel_port <= 0) { 957146998Sdes logit("Bad forwarding close port"); 958137015Sdes goto out; 959137015Sdes } 960240075Sdes if (remote) 961240075Sdes ok = channel_request_rforward_cancel(cancel_host, 962240075Sdes cancel_port) == 0; 963240075Sdes else if (dynamic) 964240075Sdes ok = channel_cancel_lport_listener(cancel_host, 965240075Sdes cancel_port, 0, options.gateway_ports) > 0; 966240075Sdes else 967240075Sdes ok = channel_cancel_lport_listener(cancel_host, 968240075Sdes cancel_port, CHANNEL_CANCEL_PORT_STATIC, 969240075Sdes options.gateway_ports) > 0; 970240075Sdes if (!ok) { 971240075Sdes logit("Unkown port forwarding."); 972240075Sdes goto out; 973240075Sdes } 974240075Sdes logit("Canceled forwarding."); 975137015Sdes } else { 976192595Sdes if (!parse_forward(&fwd, s, dynamic, remote)) { 977137015Sdes logit("Bad forwarding specification."); 978137015Sdes goto out; 979137015Sdes } 980192595Sdes if (local || dynamic) { 981248619Sdes if (!channel_setup_local_fwd_listener(fwd.listen_host, 982146998Sdes fwd.listen_port, fwd.connect_host, 983248619Sdes fwd.connect_port, options.gateway_ports)) { 984137015Sdes logit("Port forwarding failed."); 985137015Sdes goto out; 986137015Sdes } 987146998Sdes } else { 988162852Sdes if (channel_request_remote_forwarding(fwd.listen_host, 989146998Sdes fwd.listen_port, fwd.connect_host, 990162852Sdes fwd.connect_port) < 0) { 991162852Sdes logit("Port forwarding failed."); 992162852Sdes goto out; 993162852Sdes } 994146998Sdes } 995137015Sdes logit("Forwarding port."); 996137015Sdes } 997137015Sdes 99898675Sdesout: 99998675Sdes signal(SIGINT, handler); 1000226046Sdes enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 1001255767Sdes free(cmd); 1002255767Sdes free(fwd.listen_host); 1003255767Sdes free(fwd.connect_host); 100498675Sdes} 100598675Sdes 1006248619Sdes/* reasons to suppress output of an escape command in help output */ 1007248619Sdes#define SUPPRESS_NEVER 0 /* never suppress, always show */ 1008248619Sdes#define SUPPRESS_PROTO1 1 /* don't show in protocol 1 sessions */ 1009248619Sdes#define SUPPRESS_MUXCLIENT 2 /* don't show in mux client sessions */ 1010248619Sdes#define SUPPRESS_MUXMASTER 4 /* don't show in mux master sessions */ 1011248619Sdes#define SUPPRESS_SYSLOG 8 /* don't show when logging to syslog */ 1012248619Sdesstruct escape_help_text { 1013248619Sdes const char *cmd; 1014248619Sdes const char *text; 1015248619Sdes unsigned int flags; 1016248619Sdes}; 1017248619Sdesstatic struct escape_help_text esc_txt[] = { 1018248619Sdes {".", "terminate session", SUPPRESS_MUXMASTER}, 1019248619Sdes {".", "terminate connection (and any multiplexed sessions)", 1020248619Sdes SUPPRESS_MUXCLIENT}, 1021248619Sdes {"B", "send a BREAK to the remote system", SUPPRESS_PROTO1}, 1022248619Sdes {"C", "open a command line", SUPPRESS_MUXCLIENT}, 1023248619Sdes {"R", "request rekey", SUPPRESS_PROTO1}, 1024248619Sdes {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT}, 1025248619Sdes {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT}, 1026248619Sdes {"#", "list forwarded connections", SUPPRESS_NEVER}, 1027248619Sdes {"&", "background ssh (when waiting for connections to terminate)", 1028248619Sdes SUPPRESS_MUXCLIENT}, 1029248619Sdes {"?", "this message", SUPPRESS_NEVER}, 1030248619Sdes}; 1031248619Sdes 1032248619Sdesstatic void 1033248619Sdesprint_escape_help(Buffer *b, int escape_char, int protocol2, int mux_client, 1034248619Sdes int using_stderr) 1035248619Sdes{ 1036248619Sdes unsigned int i, suppress_flags; 1037248619Sdes char string[1024]; 1038248619Sdes 1039248619Sdes snprintf(string, sizeof string, "%c?\r\n" 1040248619Sdes "Supported escape sequences:\r\n", escape_char); 1041248619Sdes buffer_append(b, string, strlen(string)); 1042248619Sdes 1043248619Sdes suppress_flags = (protocol2 ? 0 : SUPPRESS_PROTO1) | 1044248619Sdes (mux_client ? SUPPRESS_MUXCLIENT : 0) | 1045248619Sdes (mux_client ? 0 : SUPPRESS_MUXMASTER) | 1046248619Sdes (using_stderr ? 0 : SUPPRESS_SYSLOG); 1047248619Sdes 1048248619Sdes for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) { 1049248619Sdes if (esc_txt[i].flags & suppress_flags) 1050248619Sdes continue; 1051248619Sdes snprintf(string, sizeof string, " %c%-3s - %s\r\n", 1052248619Sdes escape_char, esc_txt[i].cmd, esc_txt[i].text); 1053248619Sdes buffer_append(b, string, strlen(string)); 1054248619Sdes } 1055248619Sdes 1056248619Sdes snprintf(string, sizeof string, 1057248619Sdes " %c%c - send the escape character by typing it twice\r\n" 1058248619Sdes "(Note that escapes are only recognized immediately after " 1059248619Sdes "newline.)\r\n", escape_char, escape_char); 1060248619Sdes buffer_append(b, string, strlen(string)); 1061248619Sdes} 1062248619Sdes 1063181111Sdes/* 1064181111Sdes * Process the characters one by one, call with c==NULL for proto1 case. 1065181111Sdes */ 106692555Sdesstatic int 1067181111Sdesprocess_escapes(Channel *c, Buffer *bin, Buffer *bout, Buffer *berr, 1068181111Sdes char *buf, int len) 106965668Skris{ 107065668Skris char string[1024]; 107165668Skris pid_t pid; 107265668Skris int bytes = 0; 107376259Sgreen u_int i; 107476259Sgreen u_char ch; 107565668Skris char *s; 1076181111Sdes int *escape_pendingp, escape_char; 1077181111Sdes struct escape_filter_ctx *efc; 107865668Skris 1079181111Sdes if (c == NULL) { 1080181111Sdes escape_pendingp = &escape_pending1; 1081181111Sdes escape_char = escape_char1; 1082181111Sdes } else { 1083181111Sdes if (c->filter_ctx == NULL) 1084181111Sdes return 0; 1085181111Sdes efc = (struct escape_filter_ctx *)c->filter_ctx; 1086181111Sdes escape_pendingp = &efc->escape_pending; 1087181111Sdes escape_char = efc->escape_char; 1088181111Sdes } 1089181111Sdes 1090149749Sdes if (len <= 0) 1091149749Sdes return (0); 1092149749Sdes 1093149749Sdes for (i = 0; i < (u_int)len; i++) { 109465668Skris /* Get one character at a time. */ 109565668Skris ch = buf[i]; 109665668Skris 1097181111Sdes if (*escape_pendingp) { 109865668Skris /* We have previously seen an escape character. */ 109965668Skris /* Clear the flag now. */ 1100181111Sdes *escape_pendingp = 0; 110165668Skris 110265668Skris /* Process the escaped character. */ 110365668Skris switch (ch) { 110465668Skris case '.': 110565668Skris /* Terminate the connection. */ 1106181111Sdes snprintf(string, sizeof string, "%c.\r\n", 1107181111Sdes escape_char); 110865668Skris buffer_append(berr, string, strlen(string)); 110965668Skris 1110204917Sdes if (c && c->ctl_chan != -1) { 1111181111Sdes chan_read_failed(c); 1112181111Sdes chan_write_failed(c); 1113255767Sdes if (c->detach_user) 1114255767Sdes c->detach_user(c->self, NULL); 1115255767Sdes c->type = SSH_CHANNEL_ABANDONED; 1116255767Sdes buffer_clear(&c->input); 1117255767Sdes chan_ibuf_empty(c); 1118181111Sdes return 0; 1119181111Sdes } else 1120181111Sdes quit_pending = 1; 112165668Skris return -1; 112265668Skris 112365668Skris case 'Z' - 64: 1124181111Sdes /* XXX support this for mux clients */ 1125204917Sdes if (c && c->ctl_chan != -1) { 1126248619Sdes char b[16]; 1127181111Sdes noescape: 1128248619Sdes if (ch == 'Z' - 64) 1129248619Sdes snprintf(b, sizeof b, "^Z"); 1130248619Sdes else 1131248619Sdes snprintf(b, sizeof b, "%c", ch); 1132181111Sdes snprintf(string, sizeof string, 1133248619Sdes "%c%s escape not available to " 1134181111Sdes "multiplexed sessions\r\n", 1135248619Sdes escape_char, b); 1136181111Sdes buffer_append(berr, string, 1137181111Sdes strlen(string)); 1138181111Sdes continue; 1139181111Sdes } 1140181111Sdes /* Suspend the program. Inform the user */ 1141181111Sdes snprintf(string, sizeof string, 1142181111Sdes "%c^Z [suspend ssh]\r\n", escape_char); 114365668Skris buffer_append(berr, string, strlen(string)); 114465668Skris 114565668Skris /* Restore terminal modes and suspend. */ 114665668Skris client_suspend_self(bin, bout, berr); 114765668Skris 114865668Skris /* We have been continued. */ 114965668Skris continue; 115065668Skris 1151124208Sdes case 'B': 1152124208Sdes if (compat20) { 1153124208Sdes snprintf(string, sizeof string, 1154124208Sdes "%cB\r\n", escape_char); 1155124208Sdes buffer_append(berr, string, 1156124208Sdes strlen(string)); 1157262566Sdes channel_request_start(c->self, 1158124208Sdes "break", 0); 1159124208Sdes packet_put_int(1000); 1160124208Sdes packet_send(); 1161124208Sdes } 1162124208Sdes continue; 1163124208Sdes 116476259Sgreen case 'R': 116576259Sgreen if (compat20) { 116676259Sgreen if (datafellows & SSH_BUG_NOREKEY) 1167181111Sdes logit("Server does not " 1168181111Sdes "support re-keying"); 116976259Sgreen else 117076259Sgreen need_rekeying = 1; 117176259Sgreen } 117276259Sgreen continue; 117376259Sgreen 1174248619Sdes case 'V': 1175248619Sdes /* FALLTHROUGH */ 1176248619Sdes case 'v': 1177248619Sdes if (c && c->ctl_chan != -1) 1178248619Sdes goto noescape; 1179248619Sdes if (!log_is_on_stderr()) { 1180248619Sdes snprintf(string, sizeof string, 1181248619Sdes "%c%c [Logging to syslog]\r\n", 1182248619Sdes escape_char, ch); 1183248619Sdes buffer_append(berr, string, 1184248619Sdes strlen(string)); 1185248619Sdes continue; 1186248619Sdes } 1187248619Sdes if (ch == 'V' && options.log_level > 1188248619Sdes SYSLOG_LEVEL_QUIET) 1189248619Sdes log_change_level(--options.log_level); 1190248619Sdes if (ch == 'v' && options.log_level < 1191248619Sdes SYSLOG_LEVEL_DEBUG3) 1192248619Sdes log_change_level(++options.log_level); 1193248619Sdes snprintf(string, sizeof string, 1194248619Sdes "%c%c [LogLevel %s]\r\n", escape_char, ch, 1195248619Sdes log_level_name(options.log_level)); 1196248619Sdes buffer_append(berr, string, strlen(string)); 1197248619Sdes continue; 1198248619Sdes 119965668Skris case '&': 1200204917Sdes if (c && c->ctl_chan != -1) 1201181111Sdes goto noescape; 120265668Skris /* 1203181111Sdes * Detach the program (continue to serve 1204181111Sdes * connections, but put in background and no 1205181111Sdes * more new connections). 120665668Skris */ 120765668Skris /* Restore tty modes. */ 1208226046Sdes leave_raw_mode( 1209226046Sdes options.request_tty == REQUEST_TTY_FORCE); 121065668Skris 121165668Skris /* Stop listening for new connections. */ 121265668Skris channel_stop_listening(); 121365668Skris 121492555Sdes snprintf(string, sizeof string, 121592555Sdes "%c& [backgrounded]\n", escape_char); 121692555Sdes buffer_append(berr, string, strlen(string)); 121765668Skris 121865668Skris /* Fork into background. */ 121965668Skris pid = fork(); 122065668Skris if (pid < 0) { 122165668Skris error("fork: %.100s", strerror(errno)); 122265668Skris continue; 122365668Skris } 122465668Skris if (pid != 0) { /* This is the parent. */ 122565668Skris /* The parent just exits. */ 122665668Skris exit(0); 122765668Skris } 122865668Skris /* The child continues serving connections. */ 122992555Sdes if (compat20) { 123092555Sdes buffer_append(bin, "\004", 1); 123192555Sdes /* fake EOF on stdin */ 123292555Sdes return -1; 123392555Sdes } else if (!stdin_eof) { 123492555Sdes /* 1235181111Sdes * Sending SSH_CMSG_EOF alone does not 1236181111Sdes * always appear to be enough. So we 1237181111Sdes * try to send an EOF character first. 123892555Sdes */ 123992555Sdes packet_start(SSH_CMSG_STDIN_DATA); 124092555Sdes packet_put_string("\004", 1); 124192555Sdes packet_send(); 124292555Sdes /* Close stdin. */ 124392555Sdes stdin_eof = 1; 124492555Sdes if (buffer_len(bin) == 0) { 124592555Sdes packet_start(SSH_CMSG_EOF); 124692555Sdes packet_send(); 124792555Sdes } 124892555Sdes } 124992555Sdes continue; 125065668Skris 125165668Skris case '?': 1252248619Sdes print_escape_help(berr, escape_char, compat20, 1253248619Sdes (c && c->ctl_chan != -1), 1254248619Sdes log_is_on_stderr()); 125565668Skris continue; 125665668Skris 125765668Skris case '#': 1258181111Sdes snprintf(string, sizeof string, "%c#\r\n", 1259181111Sdes escape_char); 126065668Skris buffer_append(berr, string, strlen(string)); 126165668Skris s = channel_open_message(); 126265668Skris buffer_append(berr, s, strlen(s)); 1263255767Sdes free(s); 126465668Skris continue; 126565668Skris 126698675Sdes case 'C': 1267204917Sdes if (c && c->ctl_chan != -1) 1268192595Sdes goto noescape; 126998675Sdes process_cmdline(); 127098675Sdes continue; 127198675Sdes 127265668Skris default: 127365668Skris if (ch != escape_char) { 127465668Skris buffer_put_char(bin, escape_char); 127565668Skris bytes++; 127665668Skris } 127765668Skris /* Escaped characters fall through here */ 127865668Skris break; 127965668Skris } 128065668Skris } else { 128165668Skris /* 1282181111Sdes * The previous character was not an escape char. 1283181111Sdes * Check if this is an escape. 128465668Skris */ 128565668Skris if (last_was_cr && ch == escape_char) { 1286181111Sdes /* 1287181111Sdes * It is. Set the flag and continue to 1288181111Sdes * next character. 1289181111Sdes */ 1290181111Sdes *escape_pendingp = 1; 129165668Skris continue; 129265668Skris } 129365668Skris } 129465668Skris 129565668Skris /* 129665668Skris * Normal character. Record whether it was a newline, 129765668Skris * and append it to the buffer. 129865668Skris */ 129965668Skris last_was_cr = (ch == '\r' || ch == '\n'); 130065668Skris buffer_put_char(bin, ch); 130165668Skris bytes++; 130265668Skris } 130365668Skris return bytes; 130465668Skris} 130565668Skris 130692555Sdesstatic void 1307162852Sdesclient_process_input(fd_set *readset) 130860573Skris{ 130960573Skris int len; 1310197679Sdes char buf[SSH_IOBUFSZ]; 131160573Skris 131257429Smarkm /* Read input from stdin. */ 131357429Smarkm if (FD_ISSET(fileno(stdin), readset)) { 131457429Smarkm /* Read as much as possible. */ 131557429Smarkm len = read(fileno(stdin), buf, sizeof(buf)); 1316181111Sdes if (len < 0 && 1317181111Sdes (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)) 131876259Sgreen return; /* we'll try again later */ 131957429Smarkm if (len <= 0) { 132057429Smarkm /* 132157429Smarkm * Received EOF or error. They are treated 132257429Smarkm * similarly, except that an error message is printed 132357429Smarkm * if it was an error condition. 132457429Smarkm */ 132557429Smarkm if (len < 0) { 1326181111Sdes snprintf(buf, sizeof buf, "read: %.100s\r\n", 1327181111Sdes strerror(errno)); 132857429Smarkm buffer_append(&stderr_buffer, buf, strlen(buf)); 132957429Smarkm } 133057429Smarkm /* Mark that we have seen EOF. */ 133157429Smarkm stdin_eof = 1; 133257429Smarkm /* 133357429Smarkm * Send an EOF message to the server unless there is 133457429Smarkm * data in the buffer. If there is data in the 133557429Smarkm * buffer, no message will be sent now. Code 133657429Smarkm * elsewhere will send the EOF when the buffer 133757429Smarkm * becomes empty if stdin_eof is set. 133857429Smarkm */ 133957429Smarkm if (buffer_len(&stdin_buffer) == 0) { 134057429Smarkm packet_start(SSH_CMSG_EOF); 134157429Smarkm packet_send(); 134257429Smarkm } 1343181111Sdes } else if (escape_char1 == SSH_ESCAPECHAR_NONE) { 134457429Smarkm /* 134557429Smarkm * Normal successful read, and no escape character. 134657429Smarkm * Just append the data to buffer. 134757429Smarkm */ 134857429Smarkm buffer_append(&stdin_buffer, buf, len); 134957429Smarkm } else { 135057429Smarkm /* 1351181111Sdes * Normal, successful read. But we have an escape 1352181111Sdes * character and have to process the characters one 1353181111Sdes * by one. 135457429Smarkm */ 1355181111Sdes if (process_escapes(NULL, &stdin_buffer, 1356181111Sdes &stdout_buffer, &stderr_buffer, buf, len) == -1) 135765668Skris return; 135857429Smarkm } 135957429Smarkm } 136057429Smarkm} 136157429Smarkm 136292555Sdesstatic void 1363162852Sdesclient_process_output(fd_set *writeset) 136457429Smarkm{ 136557429Smarkm int len; 136657429Smarkm char buf[100]; 136757429Smarkm 136857429Smarkm /* Write buffered output to stdout. */ 136957429Smarkm if (FD_ISSET(fileno(stdout), writeset)) { 137057429Smarkm /* Write as much data as possible. */ 137157429Smarkm len = write(fileno(stdout), buffer_ptr(&stdout_buffer), 137257429Smarkm buffer_len(&stdout_buffer)); 137357429Smarkm if (len <= 0) { 1374181111Sdes if (errno == EINTR || errno == EAGAIN || 1375181111Sdes errno == EWOULDBLOCK) 137657429Smarkm len = 0; 137757429Smarkm else { 137857429Smarkm /* 137957429Smarkm * An error or EOF was encountered. Put an 138057429Smarkm * error message to stderr buffer. 138157429Smarkm */ 1382181111Sdes snprintf(buf, sizeof buf, 1383181111Sdes "write stdout: %.50s\r\n", strerror(errno)); 138457429Smarkm buffer_append(&stderr_buffer, buf, strlen(buf)); 138557429Smarkm quit_pending = 1; 138657429Smarkm return; 138757429Smarkm } 138857429Smarkm } 138957429Smarkm /* Consume printed data from the buffer. */ 139057429Smarkm buffer_consume(&stdout_buffer, len); 139157429Smarkm } 139257429Smarkm /* Write buffered output to stderr. */ 139357429Smarkm if (FD_ISSET(fileno(stderr), writeset)) { 139457429Smarkm /* Write as much data as possible. */ 139557429Smarkm len = write(fileno(stderr), buffer_ptr(&stderr_buffer), 139657429Smarkm buffer_len(&stderr_buffer)); 139757429Smarkm if (len <= 0) { 1398181111Sdes if (errno == EINTR || errno == EAGAIN || 1399181111Sdes errno == EWOULDBLOCK) 140057429Smarkm len = 0; 140157429Smarkm else { 1402181111Sdes /* 1403181111Sdes * EOF or error, but can't even print 1404181111Sdes * error message. 1405181111Sdes */ 140657429Smarkm quit_pending = 1; 140757429Smarkm return; 140857429Smarkm } 140957429Smarkm } 141057429Smarkm /* Consume printed characters from the buffer. */ 141157429Smarkm buffer_consume(&stderr_buffer, len); 141257429Smarkm } 141357429Smarkm} 141457429Smarkm 141557429Smarkm/* 141660573Skris * Get packets from the connection input buffer, and process them as long as 141760573Skris * there are packets available. 141860573Skris * 141960573Skris * Any unknown packets received during the actual 142060573Skris * session cause the session to terminate. This is 142160573Skris * intended to make debugging easier since no 142260573Skris * confirmations are sent. Any compatible protocol 142360573Skris * extensions must be negotiated during the 142460573Skris * preparatory phase. 142560573Skris */ 142660573Skris 142792555Sdesstatic void 142876259Sgreenclient_process_buffered_input_packets(void) 142960573Skris{ 1430181111Sdes dispatch_run(DISPATCH_NONBLOCK, &quit_pending, 1431181111Sdes compat20 ? xxx_kex : NULL); 143260573Skris} 143360573Skris 143465668Skris/* scan buf[] for '~' before sending data to the peer */ 143565668Skris 1436181111Sdes/* Helper: allocate a new escape_filter_ctx and fill in its escape char */ 1437181111Sdesvoid * 1438181111Sdesclient_new_escape_filter_ctx(int escape_char) 143965668Skris{ 1440181111Sdes struct escape_filter_ctx *ret; 1441181111Sdes 1442258343Sdes ret = xcalloc(1, sizeof(*ret)); 1443181111Sdes ret->escape_pending = 0; 1444181111Sdes ret->escape_char = escape_char; 1445181111Sdes return (void *)ret; 144665668Skris} 144765668Skris 1448181111Sdes/* Free the escape filter context on channel free */ 1449181111Sdesvoid 1450181111Sdesclient_filter_cleanup(int cid, void *ctx) 1451181111Sdes{ 1452255767Sdes free(ctx); 1453181111Sdes} 1454181111Sdes 1455181111Sdesint 1456181111Sdesclient_simple_escape_filter(Channel *c, char *buf, int len) 1457181111Sdes{ 1458181111Sdes if (c->extended_usage != CHAN_EXTENDED_WRITE) 1459181111Sdes return 0; 1460181111Sdes 1461181111Sdes return process_escapes(c, &c->input, &c->output, &c->extended, 1462181111Sdes buf, len); 1463181111Sdes} 1464181111Sdes 146592555Sdesstatic void 146676259Sgreenclient_channel_closed(int id, void *arg) 146776259Sgreen{ 146892555Sdes channel_cancel_cleanup(id); 146976259Sgreen session_closed = 1; 1470226046Sdes leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 147176259Sgreen} 147276259Sgreen 147360573Skris/* 147457429Smarkm * Implements the interactive session with the server. This is called after 147557429Smarkm * the user has been authenticated, and a command has been started on the 147692555Sdes * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character 147792555Sdes * used as an escape character for terminating or suspending the session. 147857429Smarkm */ 147957429Smarkm 148060573Skrisint 148165668Skrisclient_loop(int have_pty, int escape_char_arg, int ssh2_chan_id) 148257429Smarkm{ 148376259Sgreen fd_set *readset = NULL, *writeset = NULL; 148457429Smarkm double start_time, total_time; 1485137015Sdes int max_fd = 0, max_fd2 = 0, len, rekeying = 0; 1486181111Sdes u_int64_t ibytes, obytes; 1487137015Sdes u_int nalloc = 0; 148857429Smarkm char buf[100]; 148957429Smarkm 149057429Smarkm debug("Entering interactive session."); 149157429Smarkm 149257429Smarkm start_time = get_current_time(); 149357429Smarkm 149457429Smarkm /* Initialize variables. */ 1495181111Sdes escape_pending1 = 0; 149657429Smarkm last_was_cr = 1; 149757429Smarkm exit_status = -1; 149857429Smarkm stdin_eof = 0; 149957429Smarkm buffer_high = 64 * 1024; 150057429Smarkm connection_in = packet_get_connection_in(); 150157429Smarkm connection_out = packet_get_connection_out(); 150276259Sgreen max_fd = MAX(connection_in, connection_out); 150376259Sgreen 150476259Sgreen if (!compat20) { 150576259Sgreen /* enable nonblocking unless tty */ 150676259Sgreen if (!isatty(fileno(stdin))) 150776259Sgreen set_nonblock(fileno(stdin)); 150876259Sgreen if (!isatty(fileno(stdout))) 150976259Sgreen set_nonblock(fileno(stdout)); 151076259Sgreen if (!isatty(fileno(stderr))) 151176259Sgreen set_nonblock(fileno(stderr)); 151276259Sgreen max_fd = MAX(max_fd, fileno(stdin)); 151376259Sgreen max_fd = MAX(max_fd, fileno(stdout)); 151476259Sgreen max_fd = MAX(max_fd, fileno(stderr)); 151576259Sgreen } 151657429Smarkm quit_pending = 0; 1517181111Sdes escape_char1 = escape_char_arg; 151857429Smarkm 151957429Smarkm /* Initialize buffers. */ 152057429Smarkm buffer_init(&stdin_buffer); 152157429Smarkm buffer_init(&stdout_buffer); 152257429Smarkm buffer_init(&stderr_buffer); 152357429Smarkm 152460573Skris client_init_dispatch(); 152560573Skris 1526113908Sdes /* 1527113908Sdes * Set signal handlers, (e.g. to restore non-blocking mode) 1528113908Sdes * but don't overwrite SIG_IGN, matches behaviour from rsh(1) 1529113908Sdes */ 1530146998Sdes if (signal(SIGHUP, SIG_IGN) != SIG_IGN) 1531146998Sdes signal(SIGHUP, signal_handler); 1532113908Sdes if (signal(SIGINT, SIG_IGN) != SIG_IGN) 1533113908Sdes signal(SIGINT, signal_handler); 1534113908Sdes if (signal(SIGQUIT, SIG_IGN) != SIG_IGN) 1535113908Sdes signal(SIGQUIT, signal_handler); 1536113908Sdes if (signal(SIGTERM, SIG_IGN) != SIG_IGN) 1537113908Sdes signal(SIGTERM, signal_handler); 1538146998Sdes signal(SIGWINCH, window_change_handler); 153957429Smarkm 154057429Smarkm if (have_pty) 1541226046Sdes enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 154257429Smarkm 154376259Sgreen if (compat20) { 154476259Sgreen session_ident = ssh2_chan_id; 1545226046Sdes if (session_ident != -1) { 1546226046Sdes if (escape_char_arg != SSH_ESCAPECHAR_NONE) { 1547226046Sdes channel_register_filter(session_ident, 1548226046Sdes client_simple_escape_filter, NULL, 1549226046Sdes client_filter_cleanup, 1550226046Sdes client_new_escape_filter_ctx( 1551226046Sdes escape_char_arg)); 1552226046Sdes } 155376259Sgreen channel_register_cleanup(session_ident, 1554157016Sdes client_channel_closed, 0); 1555226046Sdes } 155676259Sgreen } else { 155776259Sgreen /* Check if we should immediately send eof on stdin. */ 155860573Skris client_check_initial_eof_on_stdin(); 155976259Sgreen } 156057429Smarkm 156157429Smarkm /* Main loop of the client for the interactive session mode. */ 156257429Smarkm while (!quit_pending) { 156357429Smarkm 156457429Smarkm /* Process buffered packets sent by the server. */ 156557429Smarkm client_process_buffered_input_packets(); 156657429Smarkm 156776259Sgreen if (compat20 && session_closed && !channel_still_open()) 156860573Skris break; 156960573Skris 157076259Sgreen rekeying = (xxx_kex != NULL && !xxx_kex->done); 157157429Smarkm 157276259Sgreen if (rekeying) { 157376259Sgreen debug("rekeying in progress"); 157476259Sgreen } else { 157576259Sgreen /* 157676259Sgreen * Make packets of buffered stdin data, and buffer 157776259Sgreen * them for sending to the server. 157876259Sgreen */ 157976259Sgreen if (!compat20) 158076259Sgreen client_make_packets_from_stdin_data(); 158157429Smarkm 158276259Sgreen /* 158376259Sgreen * Make packets from buffered channel data, and 158476259Sgreen * enqueue them for sending to the server. 158576259Sgreen */ 158676259Sgreen if (packet_not_very_much_data_to_write()) 158776259Sgreen channel_output_poll(); 158857429Smarkm 158976259Sgreen /* 159076259Sgreen * Check if the window size has changed, and buffer a 159176259Sgreen * message about it to the server if so. 159276259Sgreen */ 159376259Sgreen client_check_window_change(); 159457429Smarkm 159576259Sgreen if (quit_pending) 159676259Sgreen break; 159776259Sgreen } 159857429Smarkm /* 159957429Smarkm * Wait until we have something to do (something becomes 160057429Smarkm * available on one of the descriptors). 160157429Smarkm */ 160292555Sdes max_fd2 = max_fd; 160376259Sgreen client_wait_until_can_do_something(&readset, &writeset, 160492555Sdes &max_fd2, &nalloc, rekeying); 160557429Smarkm 160657429Smarkm if (quit_pending) 160757429Smarkm break; 160857429Smarkm 160976259Sgreen /* Do channel operations unless rekeying in progress. */ 161076259Sgreen if (!rekeying) { 161176259Sgreen channel_after_select(readset, writeset); 1612124208Sdes if (need_rekeying || packet_need_rekeying()) { 1613124208Sdes debug("need rekeying"); 161476259Sgreen xxx_kex->done = 0; 161576259Sgreen kex_send_kexinit(xxx_kex); 161676259Sgreen need_rekeying = 0; 161776259Sgreen } 161876259Sgreen } 161976259Sgreen 162060573Skris /* Buffer input from the connection. */ 162176259Sgreen client_process_net_input(readset); 162257429Smarkm 162360573Skris if (quit_pending) 162460573Skris break; 162557429Smarkm 162660573Skris if (!compat20) { 162760573Skris /* Buffer data from stdin */ 162876259Sgreen client_process_input(readset); 162960573Skris /* 163060573Skris * Process output to stdout and stderr. Output to 163160573Skris * the connection is processed elsewhere (above). 163260573Skris */ 163376259Sgreen client_process_output(writeset); 163460573Skris } 163560573Skris 1636204917Sdes if (session_resumed) { 1637204917Sdes connection_in = packet_get_connection_in(); 1638204917Sdes connection_out = packet_get_connection_out(); 1639204917Sdes max_fd = MAX(max_fd, connection_out); 1640204917Sdes max_fd = MAX(max_fd, connection_in); 1641204917Sdes session_resumed = 0; 1642204917Sdes } 1643204917Sdes 1644181111Sdes /* 1645181111Sdes * Send as much buffered packet data as possible to the 1646181111Sdes * sender. 1647181111Sdes */ 164876259Sgreen if (FD_ISSET(connection_out, writeset)) 164957429Smarkm packet_write_poll(); 1650215116Sdes 1651215116Sdes /* 1652215116Sdes * If we are a backgrounded control master, and the 1653215116Sdes * timeout has expired without any active client 1654215116Sdes * connections, then quit. 1655215116Sdes */ 1656215116Sdes if (control_persist_exit_time > 0) { 1657255767Sdes if (monotime() >= control_persist_exit_time) { 1658215116Sdes debug("ControlPersist timeout expired"); 1659215116Sdes break; 1660215116Sdes } 1661215116Sdes } 166257429Smarkm } 1663255767Sdes free(readset); 1664255767Sdes free(writeset); 166557429Smarkm 166657429Smarkm /* Terminate the session. */ 166757429Smarkm 166857429Smarkm /* Stop watching for window change. */ 1669146998Sdes signal(SIGWINCH, SIG_DFL); 167057429Smarkm 1671197679Sdes if (compat20) { 1672197679Sdes packet_start(SSH2_MSG_DISCONNECT); 1673197679Sdes packet_put_int(SSH2_DISCONNECT_BY_APPLICATION); 1674197679Sdes packet_put_cstring("disconnected by user"); 1675207319Sdes packet_put_cstring(""); /* language tag */ 1676197679Sdes packet_send(); 1677197679Sdes packet_write_wait(); 1678197679Sdes } 1679197679Sdes 168092555Sdes channel_free_all(); 168157429Smarkm 168292555Sdes if (have_pty) 1683226046Sdes leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 168492555Sdes 168592555Sdes /* restore blocking io */ 168692555Sdes if (!isatty(fileno(stdin))) 168792555Sdes unset_nonblock(fileno(stdin)); 168892555Sdes if (!isatty(fileno(stdout))) 168992555Sdes unset_nonblock(fileno(stdout)); 169092555Sdes if (!isatty(fileno(stderr))) 169192555Sdes unset_nonblock(fileno(stderr)); 169292555Sdes 1693126274Sdes /* 1694126274Sdes * If there was no shell or command requested, there will be no remote 1695126274Sdes * exit status to be returned. In that case, clear error code if the 1696126274Sdes * connection was deliberately terminated at this end. 1697126274Sdes */ 1698126274Sdes if (no_shell_flag && received_signal == SIGTERM) { 1699126274Sdes received_signal = 0; 1700126274Sdes exit_status = 0; 170192555Sdes } 170292555Sdes 1703126274Sdes if (received_signal) 1704126274Sdes fatal("Killed by signal %d.", (int) received_signal); 1705126274Sdes 170657429Smarkm /* 170757429Smarkm * In interactive mode (with pseudo tty) display a message indicating 170857429Smarkm * that the connection has been closed. 170957429Smarkm */ 171057429Smarkm if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) { 1711181111Sdes snprintf(buf, sizeof buf, 1712181111Sdes "Connection to %.64s closed.\r\n", host); 171357429Smarkm buffer_append(&stderr_buffer, buf, strlen(buf)); 171457429Smarkm } 171592555Sdes 171657429Smarkm /* Output any buffered data for stdout. */ 1717221420Sdes if (buffer_len(&stdout_buffer) > 0) { 1718221420Sdes len = atomicio(vwrite, fileno(stdout), 1719221420Sdes buffer_ptr(&stdout_buffer), buffer_len(&stdout_buffer)); 1720221420Sdes if (len < 0 || (u_int)len != buffer_len(&stdout_buffer)) 172157429Smarkm error("Write failed flushing stdout buffer."); 1722221420Sdes else 1723221420Sdes buffer_consume(&stdout_buffer, len); 172457429Smarkm } 172557429Smarkm 172657429Smarkm /* Output any buffered data for stderr. */ 1727221420Sdes if (buffer_len(&stderr_buffer) > 0) { 1728221420Sdes len = atomicio(vwrite, fileno(stderr), 1729221420Sdes buffer_ptr(&stderr_buffer), buffer_len(&stderr_buffer)); 1730221420Sdes if (len < 0 || (u_int)len != buffer_len(&stderr_buffer)) 173157429Smarkm error("Write failed flushing stderr buffer."); 1732221420Sdes else 1733221420Sdes buffer_consume(&stderr_buffer, len); 173457429Smarkm } 173557429Smarkm 173657429Smarkm /* Clear and free any buffers. */ 173757429Smarkm memset(buf, 0, sizeof(buf)); 173857429Smarkm buffer_free(&stdin_buffer); 173957429Smarkm buffer_free(&stdout_buffer); 174057429Smarkm buffer_free(&stderr_buffer); 174157429Smarkm 174257429Smarkm /* Report bytes transferred, and transfer rates. */ 174357429Smarkm total_time = get_current_time() - start_time; 1744181111Sdes packet_get_state(MODE_IN, NULL, NULL, NULL, &ibytes); 1745181111Sdes packet_get_state(MODE_OUT, NULL, NULL, NULL, &obytes); 1746181111Sdes verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds", 1747221420Sdes (unsigned long long)obytes, (unsigned long long)ibytes, total_time); 174857429Smarkm if (total_time > 0) 1749181111Sdes verbose("Bytes per second: sent %.1f, received %.1f", 1750181111Sdes obytes / total_time, ibytes / total_time); 175157429Smarkm /* Return the exit status of the program. */ 175257429Smarkm debug("Exit status %d", exit_status); 175357429Smarkm return exit_status; 175457429Smarkm} 175560573Skris 175660573Skris/*********/ 175760573Skris 175892555Sdesstatic void 175992555Sdesclient_input_stdout_data(int type, u_int32_t seq, void *ctxt) 176060573Skris{ 176176259Sgreen u_int data_len; 176260573Skris char *data = packet_get_string(&data_len); 176392555Sdes packet_check_eom(); 176460573Skris buffer_append(&stdout_buffer, data, data_len); 1765264377Sdes explicit_bzero(data, data_len); 1766255767Sdes free(data); 176760573Skris} 176892555Sdesstatic void 176992555Sdesclient_input_stderr_data(int type, u_int32_t seq, void *ctxt) 177060573Skris{ 177176259Sgreen u_int data_len; 177260573Skris char *data = packet_get_string(&data_len); 177392555Sdes packet_check_eom(); 177460573Skris buffer_append(&stderr_buffer, data, data_len); 1775264377Sdes explicit_bzero(data, data_len); 1776255767Sdes free(data); 177760573Skris} 177892555Sdesstatic void 177992555Sdesclient_input_exit_status(int type, u_int32_t seq, void *ctxt) 178060573Skris{ 178160573Skris exit_status = packet_get_int(); 178292555Sdes packet_check_eom(); 178360573Skris /* Acknowledge the exit. */ 178460573Skris packet_start(SSH_CMSG_EXIT_CONFIRMATION); 178560573Skris packet_send(); 178660573Skris /* 178760573Skris * Must wait for packet to be sent since we are 178860573Skris * exiting the loop. 178960573Skris */ 179060573Skris packet_write_wait(); 179160573Skris /* Flag that we want to exit. */ 179260573Skris quit_pending = 1; 179360573Skris} 1794126274Sdesstatic void 1795126274Sdesclient_input_agent_open(int type, u_int32_t seq, void *ctxt) 1796126274Sdes{ 1797126274Sdes Channel *c = NULL; 1798126274Sdes int remote_id, sock; 179960573Skris 1800126274Sdes /* Read the remote channel number from the message. */ 1801126274Sdes remote_id = packet_get_int(); 1802126274Sdes packet_check_eom(); 1803126274Sdes 1804126274Sdes /* 1805126274Sdes * Get a connection to the local authentication agent (this may again 1806126274Sdes * get forwarded). 1807126274Sdes */ 1808126274Sdes sock = ssh_get_authentication_socket(); 1809126274Sdes 1810126274Sdes /* 1811126274Sdes * If we could not connect the agent, send an error message back to 1812126274Sdes * the server. This should never happen unless the agent dies, 1813126274Sdes * because authentication forwarding is only enabled if we have an 1814126274Sdes * agent. 1815126274Sdes */ 1816126274Sdes if (sock >= 0) { 1817126274Sdes c = channel_new("", SSH_CHANNEL_OPEN, sock, sock, 1818126274Sdes -1, 0, 0, 0, "authentication agent connection", 1); 1819126274Sdes c->remote_id = remote_id; 1820126274Sdes c->force_drain = 1; 1821126274Sdes } 1822126274Sdes if (c == NULL) { 1823126274Sdes packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE); 1824126274Sdes packet_put_int(remote_id); 1825126274Sdes } else { 1826126274Sdes /* Send a confirmation to the remote host. */ 1827126274Sdes debug("Forwarding authentication connection."); 1828126274Sdes packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION); 1829126274Sdes packet_put_int(remote_id); 1830126274Sdes packet_put_int(c->self); 1831126274Sdes } 1832126274Sdes packet_send(); 1833126274Sdes} 1834126274Sdes 183592555Sdesstatic Channel * 183676259Sgreenclient_request_forwarded_tcpip(const char *request_type, int rchan) 183776259Sgreen{ 1838106121Sdes Channel *c = NULL; 183976259Sgreen char *listen_address, *originator_address; 1840192595Sdes u_short listen_port, originator_port; 184176259Sgreen 184276259Sgreen /* Get rest of the packet */ 184376259Sgreen listen_address = packet_get_string(NULL); 184476259Sgreen listen_port = packet_get_int(); 184576259Sgreen originator_address = packet_get_string(NULL); 184676259Sgreen originator_port = packet_get_int(); 184792555Sdes packet_check_eom(); 184876259Sgreen 1849181111Sdes debug("client_request_forwarded_tcpip: listen %s port %d, " 1850181111Sdes "originator %s port %d", listen_address, listen_port, 1851181111Sdes originator_address, originator_port); 185276259Sgreen 1853181111Sdes c = channel_connect_by_listen_address(listen_port, 1854181111Sdes "forwarded-tcpip", originator_address); 1855181111Sdes 1856255767Sdes free(originator_address); 1857255767Sdes free(listen_address); 185876259Sgreen return c; 185976259Sgreen} 186076259Sgreen 1861106121Sdesstatic Channel * 186276259Sgreenclient_request_x11(const char *request_type, int rchan) 186376259Sgreen{ 186476259Sgreen Channel *c = NULL; 186576259Sgreen char *originator; 1866192595Sdes u_short originator_port; 186792555Sdes int sock; 186876259Sgreen 186976259Sgreen if (!options.forward_x11) { 187076259Sgreen error("Warning: ssh server tried X11 forwarding."); 1871181111Sdes error("Warning: this is probably a break-in attempt by a " 1872181111Sdes "malicious server."); 187376259Sgreen return NULL; 187476259Sgreen } 1875255767Sdes if (x11_refuse_time != 0 && monotime() >= x11_refuse_time) { 1876215116Sdes verbose("Rejected X11 connection after ForwardX11Timeout " 1877215116Sdes "expired"); 1878215116Sdes return NULL; 1879215116Sdes } 188076259Sgreen originator = packet_get_string(NULL); 188176259Sgreen if (datafellows & SSH_BUG_X11FWD) { 188276259Sgreen debug2("buggy server: x11 request w/o originator_port"); 188376259Sgreen originator_port = 0; 188476259Sgreen } else { 188576259Sgreen originator_port = packet_get_int(); 188676259Sgreen } 188792555Sdes packet_check_eom(); 188876259Sgreen /* XXX check permission */ 188976259Sgreen debug("client_request_x11: request from %s %d", originator, 189076259Sgreen originator_port); 1891255767Sdes free(originator); 189276259Sgreen sock = x11_connect_display(); 189392555Sdes if (sock < 0) 189492555Sdes return NULL; 1895224638Sbrooks if (options.hpn_disabled) 1896224638Sbrooks c = channel_new("x11", SSH_CHANNEL_X11_OPEN, sock, sock, -1, 1897224638Sbrooks CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 1898224638Sbrooks 0, "x11", 1); 1899224638Sbrooks else 1900224638Sbrooks c = channel_new("x11", SSH_CHANNEL_X11_OPEN, sock, sock, -1, 1901224638Sbrooks options.hpn_buffer_size, CHAN_X11_PACKET_DEFAULT, 1902224638Sbrooks 0, "x11", 1); 190392555Sdes c->force_drain = 1; 190476259Sgreen return c; 190576259Sgreen} 190676259Sgreen 1907106121Sdesstatic Channel * 190876259Sgreenclient_request_agent(const char *request_type, int rchan) 190976259Sgreen{ 191076259Sgreen Channel *c = NULL; 191192555Sdes int sock; 191276259Sgreen 191376259Sgreen if (!options.forward_agent) { 191476259Sgreen error("Warning: ssh server tried agent forwarding."); 1915181111Sdes error("Warning: this is probably a break-in attempt by a " 1916181111Sdes "malicious server."); 191776259Sgreen return NULL; 191876259Sgreen } 1919181111Sdes sock = ssh_get_authentication_socket(); 192092555Sdes if (sock < 0) 192192555Sdes return NULL; 1922224638Sbrooks if (options.hpn_disabled) 1923224638Sbrooks c = channel_new("authentication agent connection", 1924224638Sbrooks SSH_CHANNEL_OPEN, sock, sock, -1, 1925224638Sbrooks CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0, 1926224638Sbrooks "authentication agent connection", 1); 1927224638Sbrooks else 1928224638Sbrooks c = channel_new("authentication agent connection", 1929224638Sbrooks SSH_CHANNEL_OPEN, sock, sock, -1, 1930224638Sbrooks options.hpn_buffer_size, options.hpn_buffer_size, 0, 1931224638Sbrooks "authentication agent connection", 1); 193292555Sdes c->force_drain = 1; 193376259Sgreen return c; 193476259Sgreen} 193576259Sgreen 1936181111Sdesint 1937181111Sdesclient_request_tun_fwd(int tun_mode, int local_tun, int remote_tun) 1938181111Sdes{ 1939181111Sdes Channel *c; 1940181111Sdes int fd; 1941181111Sdes 1942181111Sdes if (tun_mode == SSH_TUNMODE_NO) 1943181111Sdes return 0; 1944181111Sdes 1945181111Sdes if (!compat20) { 1946192595Sdes error("Tunnel forwarding is not supported for protocol 1"); 1947181111Sdes return -1; 1948181111Sdes } 1949181111Sdes 1950181111Sdes debug("Requesting tun unit %d in mode %d", local_tun, tun_mode); 1951181111Sdes 1952181111Sdes /* Open local tunnel device */ 1953181111Sdes if ((fd = tun_open(local_tun, tun_mode)) == -1) { 1954181111Sdes error("Tunnel device open failed."); 1955181111Sdes return -1; 1956181111Sdes } 1957181111Sdes 1958224638Sbrooks if (options.hpn_disabled) 1959224638Sbrooks c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1, 1960224638Sbrooks CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 1961224638Sbrooks 0, "tun", 1); 1962224638Sbrooks else 1963224638Sbrooks c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1, 1964224638Sbrooks options.hpn_buffer_size, CHAN_TCP_PACKET_DEFAULT, 1965224638Sbrooks 0, "tun", 1); 1966181111Sdes c->datagram = 1; 1967181111Sdes 1968181111Sdes#if defined(SSH_TUN_FILTER) 1969181111Sdes if (options.tun_open == SSH_TUNMODE_POINTOPOINT) 1970181111Sdes channel_register_filter(c->self, sys_tun_infilter, 1971181111Sdes sys_tun_outfilter, NULL, NULL); 1972181111Sdes#endif 1973181111Sdes 1974181111Sdes packet_start(SSH2_MSG_CHANNEL_OPEN); 1975181111Sdes packet_put_cstring("tun@openssh.com"); 1976181111Sdes packet_put_int(c->self); 1977181111Sdes packet_put_int(c->local_window_max); 1978181111Sdes packet_put_int(c->local_maxpacket); 1979181111Sdes packet_put_int(tun_mode); 1980181111Sdes packet_put_int(remote_tun); 1981181111Sdes packet_send(); 1982181111Sdes 1983181111Sdes return 0; 1984181111Sdes} 1985181111Sdes 198660573Skris/* XXXX move to generic input handler */ 198792555Sdesstatic void 198892555Sdesclient_input_channel_open(int type, u_int32_t seq, void *ctxt) 198960573Skris{ 199060573Skris Channel *c = NULL; 199160573Skris char *ctype; 199260573Skris int rchan; 199399060Sdes u_int rmaxpack, rwindow, len; 199460573Skris 199560573Skris ctype = packet_get_string(&len); 199660573Skris rchan = packet_get_int(); 199760573Skris rwindow = packet_get_int(); 199860573Skris rmaxpack = packet_get_int(); 199960573Skris 200060573Skris debug("client_input_channel_open: ctype %s rchan %d win %d max %d", 200160573Skris ctype, rchan, rwindow, rmaxpack); 200260573Skris 200376259Sgreen if (strcmp(ctype, "forwarded-tcpip") == 0) { 200476259Sgreen c = client_request_forwarded_tcpip(ctype, rchan); 200576259Sgreen } else if (strcmp(ctype, "x11") == 0) { 200676259Sgreen c = client_request_x11(ctype, rchan); 200776259Sgreen } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) { 200876259Sgreen c = client_request_agent(ctype, rchan); 200960573Skris } 201060573Skris/* XXX duplicate : */ 201160573Skris if (c != NULL) { 201260573Skris debug("confirm %s", ctype); 201360573Skris c->remote_id = rchan; 201460573Skris c->remote_window = rwindow; 201560573Skris c->remote_maxpacket = rmaxpack; 201692555Sdes if (c->type != SSH_CHANNEL_CONNECTING) { 201792555Sdes packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); 201892555Sdes packet_put_int(c->remote_id); 201992555Sdes packet_put_int(c->self); 202092555Sdes packet_put_int(c->local_window); 202192555Sdes packet_put_int(c->local_maxpacket); 202292555Sdes packet_send(); 202392555Sdes } 202460573Skris } else { 202560573Skris debug("failure %s", ctype); 202660573Skris packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); 202760573Skris packet_put_int(rchan); 202860573Skris packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED); 202992555Sdes if (!(datafellows & SSH_BUG_OPENFAILURE)) { 203092555Sdes packet_put_cstring("open failed"); 203192555Sdes packet_put_cstring(""); 203292555Sdes } 203360573Skris packet_send(); 203460573Skris } 2035255767Sdes free(ctype); 203660573Skris} 203792555Sdesstatic void 203892555Sdesclient_input_channel_req(int type, u_int32_t seq, void *ctxt) 203976259Sgreen{ 204076259Sgreen Channel *c = NULL; 2041137015Sdes int exitval, id, reply, success = 0; 204276259Sgreen char *rtype; 204360573Skris 204476259Sgreen id = packet_get_int(); 204576259Sgreen rtype = packet_get_string(NULL); 204676259Sgreen reply = packet_get_char(); 204776259Sgreen 204876259Sgreen debug("client_input_channel_req: channel %d rtype %s reply %d", 204976259Sgreen id, rtype, reply); 205076259Sgreen 2051137015Sdes if (id == -1) { 2052137015Sdes error("client_input_channel_req: request for channel -1"); 2053137015Sdes } else if ((c = channel_lookup(id)) == NULL) { 2054181111Sdes error("client_input_channel_req: channel %d: " 2055181111Sdes "unknown channel", id); 2056181111Sdes } else if (strcmp(rtype, "eow@openssh.com") == 0) { 2057181111Sdes packet_check_eom(); 2058181111Sdes chan_rcvd_eow(c); 205976259Sgreen } else if (strcmp(rtype, "exit-status") == 0) { 2060137015Sdes exitval = packet_get_int(); 2061204917Sdes if (c->ctl_chan != -1) { 2062204917Sdes mux_exit_message(c, exitval); 2063137015Sdes success = 1; 2064204917Sdes } else if (id == session_ident) { 2065204917Sdes /* Record exit value of local session */ 2066204917Sdes success = 1; 2067137015Sdes exit_status = exitval; 2068137015Sdes } else { 2069204917Sdes /* Probably for a mux channel that has already closed */ 2070204917Sdes debug("%s: no sink for exit-status on channel %d", 2071204917Sdes __func__, id); 2072137015Sdes } 207392555Sdes packet_check_eom(); 207476259Sgreen } 2075221420Sdes if (reply && c != NULL) { 207676259Sgreen packet_start(success ? 207776259Sgreen SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); 2078192595Sdes packet_put_int(c->remote_id); 207976259Sgreen packet_send(); 208076259Sgreen } 2081255767Sdes free(rtype); 208276259Sgreen} 208392555Sdesstatic void 208492555Sdesclient_input_global_request(int type, u_int32_t seq, void *ctxt) 208592555Sdes{ 208692555Sdes char *rtype; 208792555Sdes int want_reply; 208892555Sdes int success = 0; 208976259Sgreen 209092555Sdes rtype = packet_get_string(NULL); 209192555Sdes want_reply = packet_get_char(); 2092126274Sdes debug("client_input_global_request: rtype %s want_reply %d", 2093126274Sdes rtype, want_reply); 209492555Sdes if (want_reply) { 209592555Sdes packet_start(success ? 209692555Sdes SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); 209792555Sdes packet_send(); 209892555Sdes packet_write_wait(); 209992555Sdes } 2100255767Sdes free(rtype); 210192555Sdes} 210292555Sdes 2103137015Sdesvoid 2104137015Sdesclient_session2_setup(int id, int want_tty, int want_subsystem, 2105181111Sdes const char *term, struct termios *tiop, int in_fd, Buffer *cmd, char **env) 2106137015Sdes{ 2107137015Sdes int len; 2108146998Sdes Channel *c = NULL; 2109137015Sdes 2110137015Sdes debug2("%s: id %d", __func__, id); 2111137015Sdes 2112146998Sdes if ((c = channel_lookup(id)) == NULL) 2113146998Sdes fatal("client_session2_setup: channel %d: unknown channel", id); 2114146998Sdes 2115221420Sdes packet_set_interactive(want_tty, 2116221420Sdes options.ip_qos_interactive, options.ip_qos_bulk); 2117221420Sdes 2118137015Sdes if (want_tty) { 2119137015Sdes struct winsize ws; 2120137015Sdes 2121137015Sdes /* Store window size in the packet. */ 2122137015Sdes if (ioctl(in_fd, TIOCGWINSZ, &ws) < 0) 2123137015Sdes memset(&ws, 0, sizeof(ws)); 2124137015Sdes 2125181111Sdes channel_request_start(id, "pty-req", 1); 2126226046Sdes client_expect_confirm(id, "PTY allocation", CONFIRM_TTY); 2127137015Sdes packet_put_cstring(term != NULL ? term : ""); 2128162852Sdes packet_put_int((u_int)ws.ws_col); 2129162852Sdes packet_put_int((u_int)ws.ws_row); 2130162852Sdes packet_put_int((u_int)ws.ws_xpixel); 2131162852Sdes packet_put_int((u_int)ws.ws_ypixel); 2132181111Sdes if (tiop == NULL) 2133181111Sdes tiop = get_saved_tio(); 2134181111Sdes tty_make_modes(-1, tiop); 2135137015Sdes packet_send(); 2136137015Sdes /* XXX wait for reply */ 2137146998Sdes c->client_tty = 1; 2138137015Sdes } 2139137015Sdes 2140137015Sdes /* Transfer any environment variables from client to server */ 2141137015Sdes if (options.num_send_env != 0 && env != NULL) { 2142137015Sdes int i, j, matched; 2143137015Sdes char *name, *val; 2144137015Sdes 2145137015Sdes debug("Sending environment."); 2146137015Sdes for (i = 0; env[i] != NULL; i++) { 2147137015Sdes /* Split */ 2148137015Sdes name = xstrdup(env[i]); 2149137015Sdes if ((val = strchr(name, '=')) == NULL) { 2150255767Sdes free(name); 2151137015Sdes continue; 2152137015Sdes } 2153137015Sdes *val++ = '\0'; 2154137015Sdes 2155137015Sdes matched = 0; 2156137015Sdes for (j = 0; j < options.num_send_env; j++) { 2157137015Sdes if (match_pattern(name, options.send_env[j])) { 2158137015Sdes matched = 1; 2159137015Sdes break; 2160137015Sdes } 2161137015Sdes } 2162137015Sdes if (!matched) { 2163137015Sdes debug3("Ignored env %s", name); 2164255767Sdes free(name); 2165137015Sdes continue; 2166137015Sdes } 2167137015Sdes 2168137015Sdes debug("Sending env %s = %s", name, val); 2169137015Sdes channel_request_start(id, "env", 0); 2170137015Sdes packet_put_cstring(name); 2171137015Sdes packet_put_cstring(val); 2172137015Sdes packet_send(); 2173255767Sdes free(name); 2174137015Sdes } 2175137015Sdes } 2176137015Sdes 2177137015Sdes len = buffer_len(cmd); 2178137015Sdes if (len > 0) { 2179137015Sdes if (len > 900) 2180137015Sdes len = 900; 2181137015Sdes if (want_subsystem) { 2182181111Sdes debug("Sending subsystem: %.*s", 2183181111Sdes len, (u_char*)buffer_ptr(cmd)); 2184181111Sdes channel_request_start(id, "subsystem", 1); 2185226046Sdes client_expect_confirm(id, "subsystem", CONFIRM_CLOSE); 2186137015Sdes } else { 2187181111Sdes debug("Sending command: %.*s", 2188181111Sdes len, (u_char*)buffer_ptr(cmd)); 2189181111Sdes channel_request_start(id, "exec", 1); 2190226046Sdes client_expect_confirm(id, "exec", CONFIRM_CLOSE); 2191137015Sdes } 2192137015Sdes packet_put_string(buffer_ptr(cmd), buffer_len(cmd)); 2193137015Sdes packet_send(); 2194137015Sdes } else { 2195181111Sdes channel_request_start(id, "shell", 1); 2196226046Sdes client_expect_confirm(id, "shell", CONFIRM_CLOSE); 2197137015Sdes packet_send(); 2198137015Sdes } 2199137015Sdes} 2200137015Sdes 220192555Sdesstatic void 220276259Sgreenclient_init_dispatch_20(void) 220360573Skris{ 220460573Skris dispatch_init(&dispatch_protocol_error); 220598675Sdes 220660573Skris dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); 220760573Skris dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); 220860573Skris dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); 220960573Skris dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); 221060573Skris dispatch_set(SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open); 221160573Skris dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); 221260573Skris dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); 221376259Sgreen dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req); 221460573Skris dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); 2215181111Sdes dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm); 2216181111Sdes dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm); 221792555Sdes dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request); 221876259Sgreen 221976259Sgreen /* rekeying */ 222076259Sgreen dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); 222198675Sdes 222298675Sdes /* global request reply messages */ 222398675Sdes dispatch_set(SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply); 222498675Sdes dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply); 222560573Skris} 2226181111Sdes 222792555Sdesstatic void 222876259Sgreenclient_init_dispatch_13(void) 222960573Skris{ 223060573Skris dispatch_init(NULL); 223160573Skris dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close); 223260573Skris dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation); 223360573Skris dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data); 223460573Skris dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); 223560573Skris dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); 223660573Skris dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open); 223760573Skris dispatch_set(SSH_SMSG_EXITSTATUS, &client_input_exit_status); 223860573Skris dispatch_set(SSH_SMSG_STDERR_DATA, &client_input_stderr_data); 223960573Skris dispatch_set(SSH_SMSG_STDOUT_DATA, &client_input_stdout_data); 224068700Sgreen 224168700Sgreen dispatch_set(SSH_SMSG_AGENT_OPEN, options.forward_agent ? 2242126274Sdes &client_input_agent_open : &deny_input_open); 224368700Sgreen dispatch_set(SSH_SMSG_X11_OPEN, options.forward_x11 ? 224469587Sgreen &x11_input_open : &deny_input_open); 224560573Skris} 2246181111Sdes 224792555Sdesstatic void 224876259Sgreenclient_init_dispatch_15(void) 224960573Skris{ 225060573Skris client_init_dispatch_13(); 225160573Skris dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof); 225260573Skris dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, & channel_input_oclose); 225360573Skris} 2254181111Sdes 225592555Sdesstatic void 225676259Sgreenclient_init_dispatch(void) 225760573Skris{ 225860573Skris if (compat20) 225960573Skris client_init_dispatch_20(); 226060573Skris else if (compat13) 226160573Skris client_init_dispatch_13(); 226260573Skris else 226360573Skris client_init_dispatch_15(); 226460573Skris} 2265126274Sdes 2266226046Sdesvoid 2267226046Sdesclient_stop_mux(void) 2268226046Sdes{ 2269226046Sdes if (options.control_path != NULL && muxserver_sock != -1) 2270226046Sdes unlink(options.control_path); 2271226046Sdes /* 2272248619Sdes * If we are in persist mode, or don't have a shell, signal that we 2273248619Sdes * should close when all active channels are closed. 2274226046Sdes */ 2275248619Sdes if (options.control_persist || no_shell_flag) { 2276226046Sdes session_closed = 1; 2277226046Sdes setproctitle("[stopped mux]"); 2278226046Sdes } 2279226046Sdes} 2280226046Sdes 2281126274Sdes/* client specific fatal cleanup */ 2282126274Sdesvoid 2283126274Sdescleanup_exit(int i) 2284126274Sdes{ 2285226046Sdes leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 2286126274Sdes leave_non_blocking(); 2287181111Sdes if (options.control_path != NULL && muxserver_sock != -1) 2288137015Sdes unlink(options.control_path); 2289221420Sdes ssh_kill_proxy_command(); 2290126274Sdes _exit(i); 2291126274Sdes} 2292