1*19261079SEd Maste /* $OpenBSD: clientloop.c,v 1.369 2021/07/23 04:04:52 djm Exp $ */ 2511b41d2SMark Murray /* 3511b41d2SMark Murray * Author: Tatu Ylonen <ylo@cs.hut.fi> 4511b41d2SMark Murray * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 5511b41d2SMark Murray * All rights reserved 6511b41d2SMark Murray * The main loop for the interactive session (client side). 7511b41d2SMark Murray * 8b66f2d16SKris Kennaway * As far as I am concerned, the code I have written for this software 9b66f2d16SKris Kennaway * can be used freely for any purpose. Any derived versions of this 10b66f2d16SKris Kennaway * software must be clearly marked as such, and if the derived work is 11b66f2d16SKris Kennaway * incompatible with the protocol description in the RFC file, it must be 12b66f2d16SKris Kennaway * called by a name other than "ssh" or "Secure Shell". 13b66f2d16SKris Kennaway * 14b66f2d16SKris Kennaway * 15b66f2d16SKris Kennaway * Copyright (c) 1999 Theo de Raadt. All rights reserved. 16b66f2d16SKris Kennaway * 17b66f2d16SKris Kennaway * Redistribution and use in source and binary forms, with or without 18b66f2d16SKris Kennaway * modification, are permitted provided that the following conditions 19b66f2d16SKris Kennaway * are met: 20b66f2d16SKris Kennaway * 1. Redistributions of source code must retain the above copyright 21b66f2d16SKris Kennaway * notice, this list of conditions and the following disclaimer. 22b66f2d16SKris Kennaway * 2. Redistributions in binary form must reproduce the above copyright 23b66f2d16SKris Kennaway * notice, this list of conditions and the following disclaimer in the 24b66f2d16SKris Kennaway * documentation and/or other materials provided with the distribution. 25b66f2d16SKris Kennaway * 26b66f2d16SKris Kennaway * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 27b66f2d16SKris Kennaway * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 28b66f2d16SKris Kennaway * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29b66f2d16SKris Kennaway * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 30b66f2d16SKris Kennaway * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31b66f2d16SKris Kennaway * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 32b66f2d16SKris Kennaway * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 33b66f2d16SKris Kennaway * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 34b66f2d16SKris Kennaway * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 35b66f2d16SKris Kennaway * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36b66f2d16SKris Kennaway * 37b66f2d16SKris Kennaway * 38a04a10f8SKris Kennaway * SSH2 support added by Markus Friedl. 39ae1f160dSDag-Erling Smørgrav * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved. 40b66f2d16SKris Kennaway * 41b66f2d16SKris Kennaway * Redistribution and use in source and binary forms, with or without 42b66f2d16SKris Kennaway * modification, are permitted provided that the following conditions 43b66f2d16SKris Kennaway * are met: 44b66f2d16SKris Kennaway * 1. Redistributions of source code must retain the above copyright 45b66f2d16SKris Kennaway * notice, this list of conditions and the following disclaimer. 46b66f2d16SKris Kennaway * 2. Redistributions in binary form must reproduce the above copyright 47b66f2d16SKris Kennaway * notice, this list of conditions and the following disclaimer in the 48b66f2d16SKris Kennaway * documentation and/or other materials provided with the distribution. 49b66f2d16SKris Kennaway * 50b66f2d16SKris Kennaway * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 51b66f2d16SKris Kennaway * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 52b66f2d16SKris Kennaway * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 53b66f2d16SKris Kennaway * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 54b66f2d16SKris Kennaway * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 55b66f2d16SKris Kennaway * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 56b66f2d16SKris Kennaway * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 57b66f2d16SKris Kennaway * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 58b66f2d16SKris Kennaway * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 59b66f2d16SKris Kennaway * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 60511b41d2SMark Murray */ 61511b41d2SMark Murray 62511b41d2SMark Murray #include "includes.h" 63511b41d2SMark Murray 64761efaa7SDag-Erling Smørgrav #include <sys/types.h> 65761efaa7SDag-Erling Smørgrav #include <sys/ioctl.h> 66761efaa7SDag-Erling Smørgrav #ifdef HAVE_SYS_STAT_H 67761efaa7SDag-Erling Smørgrav # include <sys/stat.h> 68761efaa7SDag-Erling Smørgrav #endif 69761efaa7SDag-Erling Smørgrav #ifdef HAVE_SYS_TIME_H 70761efaa7SDag-Erling Smørgrav # include <sys/time.h> 71761efaa7SDag-Erling Smørgrav #endif 72761efaa7SDag-Erling Smørgrav #include <sys/socket.h> 73761efaa7SDag-Erling Smørgrav 74761efaa7SDag-Erling Smørgrav #include <ctype.h> 75761efaa7SDag-Erling Smørgrav #include <errno.h> 76761efaa7SDag-Erling Smørgrav #ifdef HAVE_PATHS_H 77761efaa7SDag-Erling Smørgrav #include <paths.h> 78761efaa7SDag-Erling Smørgrav #endif 79761efaa7SDag-Erling Smørgrav #include <signal.h> 80761efaa7SDag-Erling Smørgrav #include <stdio.h> 81761efaa7SDag-Erling Smørgrav #include <stdlib.h> 82761efaa7SDag-Erling Smørgrav #include <string.h> 83*19261079SEd Maste #include <stdarg.h> 84761efaa7SDag-Erling Smørgrav #include <termios.h> 85761efaa7SDag-Erling Smørgrav #include <pwd.h> 86761efaa7SDag-Erling Smørgrav #include <unistd.h> 87bc5531deSDag-Erling Smørgrav #include <limits.h> 88761efaa7SDag-Erling Smørgrav 89d4af9e69SDag-Erling Smørgrav #include "openbsd-compat/sys-queue.h" 90761efaa7SDag-Erling Smørgrav #include "xmalloc.h" 91511b41d2SMark Murray #include "ssh.h" 921e8db6e2SBrian Feldman #include "ssh2.h" 93511b41d2SMark Murray #include "packet.h" 94190cef3dSDag-Erling Smørgrav #include "sshbuf.h" 95a04a10f8SKris Kennaway #include "compat.h" 96a04a10f8SKris Kennaway #include "channels.h" 97a04a10f8SKris Kennaway #include "dispatch.h" 98190cef3dSDag-Erling Smørgrav #include "sshkey.h" 99761efaa7SDag-Erling Smørgrav #include "cipher.h" 1001e8db6e2SBrian Feldman #include "kex.h" 101eccfee6eSDag-Erling Smørgrav #include "myproposal.h" 1021e8db6e2SBrian Feldman #include "log.h" 103a0ee8cc6SDag-Erling Smørgrav #include "misc.h" 1041e8db6e2SBrian Feldman #include "readconf.h" 1051e8db6e2SBrian Feldman #include "clientloop.h" 106021d409fSDag-Erling Smørgrav #include "sshconnect.h" 1071e8db6e2SBrian Feldman #include "authfd.h" 1081e8db6e2SBrian Feldman #include "atomicio.h" 109d74d50a8SDag-Erling Smørgrav #include "sshpty.h" 110d74d50a8SDag-Erling Smørgrav #include "match.h" 111d74d50a8SDag-Erling Smørgrav #include "msg.h" 112bc5531deSDag-Erling Smørgrav #include "ssherr.h" 113bc5531deSDag-Erling Smørgrav #include "hostfile.h" 1145b9b2fafSBrian Feldman 1155b9b2fafSBrian Feldman /* import options */ 1164899dde7SBrian Feldman extern Options options; 1174899dde7SBrian Feldman 118d74d50a8SDag-Erling Smørgrav /* Control socket */ 119b15c8340SDag-Erling Smørgrav extern int muxserver_sock; /* XXX use mux_client_cleanup() instead */ 120d74d50a8SDag-Erling Smørgrav 121511b41d2SMark Murray /* 122511b41d2SMark Murray * Name of the host we are connecting to. This is the name given on the 123*19261079SEd Maste * command line, or the Hostname specified for the user-supplied name in a 124511b41d2SMark Murray * configuration file. 125511b41d2SMark Murray */ 126511b41d2SMark Murray extern char *host; 127511b41d2SMark Murray 128511b41d2SMark Murray /* 129*19261079SEd Maste * If this field is not NULL, the ForwardAgent socket is this path and different 130*19261079SEd Maste * instead of SSH_AUTH_SOCK. 131*19261079SEd Maste */ 132*19261079SEd Maste extern char *forward_agent_sock_path; 133*19261079SEd Maste 134*19261079SEd Maste /* 135511b41d2SMark Murray * Flag to indicate that we have received a window change signal which has 136511b41d2SMark Murray * not yet been processed. This will cause a message indicating the new 137511b41d2SMark Murray * window size to be sent to the server a little later. This is volatile 138511b41d2SMark Murray * because this is updated in a signal handler. 139511b41d2SMark Murray */ 140ae1f160dSDag-Erling Smørgrav static volatile sig_atomic_t received_window_change_signal = 0; 141ae1f160dSDag-Erling Smørgrav static volatile sig_atomic_t received_signal = 0; 142511b41d2SMark Murray 143e2f6069cSDag-Erling Smørgrav /* Time when backgrounded control master using ControlPersist should exit */ 144e2f6069cSDag-Erling Smørgrav static time_t control_persist_exit_time = 0; 145e2f6069cSDag-Erling Smørgrav 146511b41d2SMark Murray /* Common data for the client loop code. */ 147b15c8340SDag-Erling Smørgrav volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */ 148511b41d2SMark Murray static int last_was_cr; /* Last character was a newline. */ 149d4af9e69SDag-Erling Smørgrav static int exit_status; /* Used to store the command exit status. */ 150190cef3dSDag-Erling Smørgrav static struct sshbuf *stderr_buffer; /* Used for final exit message. */ 151511b41d2SMark Murray static int connection_in; /* Connection to server (input). */ 152511b41d2SMark Murray static int connection_out; /* Connection to server (output). */ 1531e8db6e2SBrian Feldman static int need_rekeying; /* Set to non-zero if rekeying is requested. */ 154e2f6069cSDag-Erling Smørgrav static int session_closed; /* In SSH2: login session closed. */ 155557f75e5SDag-Erling Smørgrav static u_int x11_refuse_time; /* If >0, refuse x11 opens after this time. */ 156*19261079SEd Maste static time_t server_alive_time; /* Time to do server_alive_check */ 157a04a10f8SKris Kennaway 158*19261079SEd Maste static void client_init_dispatch(struct ssh *ssh); 159a04a10f8SKris Kennaway int session_ident = -1; 160a04a10f8SKris Kennaway 161d4af9e69SDag-Erling Smørgrav /* Track escape per proto2 channel */ 162d4af9e69SDag-Erling Smørgrav struct escape_filter_ctx { 163d4af9e69SDag-Erling Smørgrav int escape_pending; 164d4af9e69SDag-Erling Smørgrav int escape_char; 165d74d50a8SDag-Erling Smørgrav }; 166d74d50a8SDag-Erling Smørgrav 167d4af9e69SDag-Erling Smørgrav /* Context for channel confirmation replies */ 168d4af9e69SDag-Erling Smørgrav struct channel_reply_ctx { 169d4af9e69SDag-Erling Smørgrav const char *request_type; 170e146993eSDag-Erling Smørgrav int id; 171e146993eSDag-Erling Smørgrav enum confirm_action action; 172d4af9e69SDag-Erling Smørgrav }; 173d4af9e69SDag-Erling Smørgrav 174d4af9e69SDag-Erling Smørgrav /* Global request success/failure callbacks */ 1754f52dfbbSDag-Erling Smørgrav /* XXX move to struct ssh? */ 176d4af9e69SDag-Erling Smørgrav struct global_confirm { 177d4af9e69SDag-Erling Smørgrav TAILQ_ENTRY(global_confirm) entry; 178d4af9e69SDag-Erling Smørgrav global_confirm_cb *cb; 179d4af9e69SDag-Erling Smørgrav void *ctx; 180d4af9e69SDag-Erling Smørgrav int ref_count; 181d4af9e69SDag-Erling Smørgrav }; 182d4af9e69SDag-Erling Smørgrav TAILQ_HEAD(global_confirms, global_confirm); 183d4af9e69SDag-Erling Smørgrav static struct global_confirms global_confirms = 184d4af9e69SDag-Erling Smørgrav TAILQ_HEAD_INITIALIZER(global_confirms); 185d4af9e69SDag-Erling Smørgrav 186190cef3dSDag-Erling Smørgrav void ssh_process_session2_setup(int, int, int, struct sshbuf *); 187d74d50a8SDag-Erling Smørgrav 188511b41d2SMark Murray /* 189511b41d2SMark Murray * Signal handler for the window change signal (SIGWINCH). This just sets a 190511b41d2SMark Murray * flag indicating that the window has changed. 191511b41d2SMark Murray */ 192761efaa7SDag-Erling Smørgrav /*ARGSUSED */ 193ae1f160dSDag-Erling Smørgrav static void 194511b41d2SMark Murray window_change_handler(int sig) 195511b41d2SMark Murray { 196511b41d2SMark Murray received_window_change_signal = 1; 197511b41d2SMark Murray } 198511b41d2SMark Murray 199511b41d2SMark Murray /* 200511b41d2SMark Murray * Signal handler for signals that cause the program to terminate. These 201511b41d2SMark Murray * signals must be trapped to restore terminal modes. 202511b41d2SMark Murray */ 203761efaa7SDag-Erling Smørgrav /*ARGSUSED */ 204ae1f160dSDag-Erling Smørgrav static void 205511b41d2SMark Murray signal_handler(int sig) 206511b41d2SMark Murray { 207ae1f160dSDag-Erling Smørgrav received_signal = sig; 208ae1f160dSDag-Erling Smørgrav quit_pending = 1; 209511b41d2SMark Murray } 210511b41d2SMark Murray 211511b41d2SMark Murray /* 212e2f6069cSDag-Erling Smørgrav * Sets control_persist_exit_time to the absolute time when the 213e2f6069cSDag-Erling Smørgrav * backgrounded control master should exit due to expiry of the 214e2f6069cSDag-Erling Smørgrav * ControlPersist timeout. Sets it to 0 if we are not a backgrounded 215e2f6069cSDag-Erling Smørgrav * control master process, or if there is no ControlPersist timeout. 216e2f6069cSDag-Erling Smørgrav */ 217e2f6069cSDag-Erling Smørgrav static void 2184f52dfbbSDag-Erling Smørgrav set_control_persist_exit_time(struct ssh *ssh) 219e2f6069cSDag-Erling Smørgrav { 220e2f6069cSDag-Erling Smørgrav if (muxserver_sock == -1 || !options.control_persist 221e146993eSDag-Erling Smørgrav || options.control_persist_timeout == 0) { 222e2f6069cSDag-Erling Smørgrav /* not using a ControlPersist timeout */ 223e2f6069cSDag-Erling Smørgrav control_persist_exit_time = 0; 2244f52dfbbSDag-Erling Smørgrav } else if (channel_still_open(ssh)) { 225e2f6069cSDag-Erling Smørgrav /* some client connections are still open */ 226e2f6069cSDag-Erling Smørgrav if (control_persist_exit_time > 0) 227*19261079SEd Maste debug2_f("cancel scheduled exit"); 228e2f6069cSDag-Erling Smørgrav control_persist_exit_time = 0; 229e2f6069cSDag-Erling Smørgrav } else if (control_persist_exit_time <= 0) { 230e2f6069cSDag-Erling Smørgrav /* a client connection has recently closed */ 231e4a9863fSDag-Erling Smørgrav control_persist_exit_time = monotime() + 232e2f6069cSDag-Erling Smørgrav (time_t)options.control_persist_timeout; 233*19261079SEd Maste debug2_f("schedule exit in %d seconds", 234e2f6069cSDag-Erling Smørgrav options.control_persist_timeout); 235e2f6069cSDag-Erling Smørgrav } 236e2f6069cSDag-Erling Smørgrav /* else we are already counting down to the timeout */ 237e2f6069cSDag-Erling Smørgrav } 238e2f6069cSDag-Erling Smørgrav 239462c32cbSDag-Erling Smørgrav #define SSH_X11_VALID_DISPLAY_CHARS ":/.-_" 240462c32cbSDag-Erling Smørgrav static int 241462c32cbSDag-Erling Smørgrav client_x11_display_valid(const char *display) 242462c32cbSDag-Erling Smørgrav { 243462c32cbSDag-Erling Smørgrav size_t i, dlen; 244462c32cbSDag-Erling Smørgrav 245acc1a9efSDag-Erling Smørgrav if (display == NULL) 246acc1a9efSDag-Erling Smørgrav return 0; 247acc1a9efSDag-Erling Smørgrav 248462c32cbSDag-Erling Smørgrav dlen = strlen(display); 249462c32cbSDag-Erling Smørgrav for (i = 0; i < dlen; i++) { 250f7167e0eSDag-Erling Smørgrav if (!isalnum((u_char)display[i]) && 251462c32cbSDag-Erling Smørgrav strchr(SSH_X11_VALID_DISPLAY_CHARS, display[i]) == NULL) { 252462c32cbSDag-Erling Smørgrav debug("Invalid character '%c' in DISPLAY", display[i]); 253462c32cbSDag-Erling Smørgrav return 0; 254462c32cbSDag-Erling Smørgrav } 255462c32cbSDag-Erling Smørgrav } 256462c32cbSDag-Erling Smørgrav return 1; 257462c32cbSDag-Erling Smørgrav } 258462c32cbSDag-Erling Smørgrav 259043840dfSDag-Erling Smørgrav #define SSH_X11_PROTO "MIT-MAGIC-COOKIE-1" 260557f75e5SDag-Erling Smørgrav #define X11_TIMEOUT_SLACK 60 261acc1a9efSDag-Erling Smørgrav int 2624f52dfbbSDag-Erling Smørgrav client_x11_get_proto(struct ssh *ssh, const char *display, 2634f52dfbbSDag-Erling Smørgrav const char *xauth_path, u_int trusted, u_int timeout, 2644f52dfbbSDag-Erling Smørgrav char **_proto, char **_data) 265043840dfSDag-Erling Smørgrav { 2662f513db7SEd Maste char *cmd, line[512], xdisplay[512]; 267acc1a9efSDag-Erling Smørgrav char xauthfile[PATH_MAX], xauthdir[PATH_MAX]; 268043840dfSDag-Erling Smørgrav static char proto[512], data[512]; 269043840dfSDag-Erling Smørgrav FILE *f; 270ca86bcf2SDag-Erling Smørgrav int got_data = 0, generated = 0, do_unlink = 0, r; 271043840dfSDag-Erling Smørgrav struct stat st; 272557f75e5SDag-Erling Smørgrav u_int now, x11_timeout_real; 273043840dfSDag-Erling Smørgrav 274043840dfSDag-Erling Smørgrav *_proto = proto; 275043840dfSDag-Erling Smørgrav *_data = data; 276acc1a9efSDag-Erling Smørgrav proto[0] = data[0] = xauthfile[0] = xauthdir[0] = '\0'; 277043840dfSDag-Erling Smørgrav 278acc1a9efSDag-Erling Smørgrav if (!client_x11_display_valid(display)) { 279acc1a9efSDag-Erling Smørgrav if (display != NULL) 280acc1a9efSDag-Erling Smørgrav logit("DISPLAY \"%s\" invalid; disabling X11 forwarding", 281462c32cbSDag-Erling Smørgrav display); 282acc1a9efSDag-Erling Smørgrav return -1; 283043840dfSDag-Erling Smørgrav } 284acc1a9efSDag-Erling Smørgrav if (xauth_path != NULL && stat(xauth_path, &st) == -1) { 285acc1a9efSDag-Erling Smørgrav debug("No xauth program."); 286acc1a9efSDag-Erling Smørgrav xauth_path = NULL; 287acc1a9efSDag-Erling Smørgrav } 288acc1a9efSDag-Erling Smørgrav 289acc1a9efSDag-Erling Smørgrav if (xauth_path != NULL) { 290043840dfSDag-Erling Smørgrav /* 291043840dfSDag-Erling Smørgrav * Handle FamilyLocal case where $DISPLAY does 292043840dfSDag-Erling Smørgrav * not match an authorization entry. For this we 293043840dfSDag-Erling Smørgrav * just try "xauth list unix:displaynum.screennum". 294043840dfSDag-Erling Smørgrav * XXX: "localhost" match to determine FamilyLocal 295043840dfSDag-Erling Smørgrav * is not perfect. 296043840dfSDag-Erling Smørgrav */ 297043840dfSDag-Erling Smørgrav if (strncmp(display, "localhost:", 10) == 0) { 298acc1a9efSDag-Erling Smørgrav if ((r = snprintf(xdisplay, sizeof(xdisplay), "unix:%s", 299acc1a9efSDag-Erling Smørgrav display + 10)) < 0 || 300acc1a9efSDag-Erling Smørgrav (size_t)r >= sizeof(xdisplay)) { 301*19261079SEd Maste error_f("display name too long"); 302acc1a9efSDag-Erling Smørgrav return -1; 303acc1a9efSDag-Erling Smørgrav } 304043840dfSDag-Erling Smørgrav display = xdisplay; 305043840dfSDag-Erling Smørgrav } 306043840dfSDag-Erling Smørgrav if (trusted == 0) { 307557f75e5SDag-Erling Smørgrav /* 308acc1a9efSDag-Erling Smørgrav * Generate an untrusted X11 auth cookie. 309acc1a9efSDag-Erling Smørgrav * 310557f75e5SDag-Erling Smørgrav * The authentication cookie should briefly outlive 311557f75e5SDag-Erling Smørgrav * ssh's willingness to forward X11 connections to 312557f75e5SDag-Erling Smørgrav * avoid nasty fail-open behaviour in the X server. 313557f75e5SDag-Erling Smørgrav */ 314acc1a9efSDag-Erling Smørgrav mktemp_proto(xauthdir, sizeof(xauthdir)); 315acc1a9efSDag-Erling Smørgrav if (mkdtemp(xauthdir) == NULL) { 316*19261079SEd Maste error_f("mkdtemp: %s", strerror(errno)); 317acc1a9efSDag-Erling Smørgrav return -1; 318acc1a9efSDag-Erling Smørgrav } 319acc1a9efSDag-Erling Smørgrav do_unlink = 1; 320acc1a9efSDag-Erling Smørgrav if ((r = snprintf(xauthfile, sizeof(xauthfile), 321acc1a9efSDag-Erling Smørgrav "%s/xauthfile", xauthdir)) < 0 || 322acc1a9efSDag-Erling Smørgrav (size_t)r >= sizeof(xauthfile)) { 323*19261079SEd Maste error_f("xauthfile path too long"); 324acc1a9efSDag-Erling Smørgrav rmdir(xauthdir); 325acc1a9efSDag-Erling Smørgrav return -1; 326acc1a9efSDag-Erling Smørgrav } 327acc1a9efSDag-Erling Smørgrav 3282f513db7SEd Maste if (timeout == 0) { 3292f513db7SEd Maste /* auth doesn't time out */ 3302f513db7SEd Maste xasprintf(&cmd, "%s -f %s generate %s %s " 3312f513db7SEd Maste "untrusted 2>%s", 332557f75e5SDag-Erling Smørgrav xauth_path, xauthfile, display, 3332f513db7SEd Maste SSH_X11_PROTO, _PATH_DEVNULL); 3342f513db7SEd Maste } else { 3352f513db7SEd Maste /* Add some slack to requested expiry */ 3362f513db7SEd Maste if (timeout < UINT_MAX - X11_TIMEOUT_SLACK) 3372f513db7SEd Maste x11_timeout_real = timeout + 3382f513db7SEd Maste X11_TIMEOUT_SLACK; 3392f513db7SEd Maste else { 3402f513db7SEd Maste /* Don't overflow on long timeouts */ 3412f513db7SEd Maste x11_timeout_real = UINT_MAX; 3422f513db7SEd Maste } 3432f513db7SEd Maste xasprintf(&cmd, "%s -f %s generate %s %s " 3442f513db7SEd Maste "untrusted timeout %u 2>%s", 3452f513db7SEd Maste xauth_path, xauthfile, display, 3462f513db7SEd Maste SSH_X11_PROTO, x11_timeout_real, 3472f513db7SEd Maste _PATH_DEVNULL); 3482f513db7SEd Maste } 349*19261079SEd Maste debug2_f("xauth command: %s", cmd); 3502f513db7SEd Maste 3512f513db7SEd Maste if (timeout != 0 && x11_refuse_time == 0) { 352e4a9863fSDag-Erling Smørgrav now = monotime() + 1; 353e2f6069cSDag-Erling Smørgrav if (UINT_MAX - timeout < now) 354e2f6069cSDag-Erling Smørgrav x11_refuse_time = UINT_MAX; 355e2f6069cSDag-Erling Smørgrav else 356e2f6069cSDag-Erling Smørgrav x11_refuse_time = now + timeout; 3574f52dfbbSDag-Erling Smørgrav channel_set_x11_refuse_time(ssh, 3584f52dfbbSDag-Erling Smørgrav x11_refuse_time); 359e2f6069cSDag-Erling Smørgrav } 360557f75e5SDag-Erling Smørgrav if (system(cmd) == 0) 361557f75e5SDag-Erling Smørgrav generated = 1; 3622f513db7SEd Maste free(cmd); 363043840dfSDag-Erling Smørgrav } 364d4af9e69SDag-Erling Smørgrav 365d4af9e69SDag-Erling Smørgrav /* 366d4af9e69SDag-Erling Smørgrav * When in untrusted mode, we read the cookie only if it was 367d4af9e69SDag-Erling Smørgrav * successfully generated as an untrusted one in the step 368d4af9e69SDag-Erling Smørgrav * above. 369d4af9e69SDag-Erling Smørgrav */ 370d4af9e69SDag-Erling Smørgrav if (trusted || generated) { 3712f513db7SEd Maste xasprintf(&cmd, 372021d409fSDag-Erling Smørgrav "%s %s%s list %s 2>" _PATH_DEVNULL, 373043840dfSDag-Erling Smørgrav xauth_path, 374043840dfSDag-Erling Smørgrav generated ? "-f " : "" , 375043840dfSDag-Erling Smørgrav generated ? xauthfile : "", 376043840dfSDag-Erling Smørgrav display); 377043840dfSDag-Erling Smørgrav debug2("x11_get_proto: %s", cmd); 378043840dfSDag-Erling Smørgrav f = popen(cmd, "r"); 379043840dfSDag-Erling Smørgrav if (f && fgets(line, sizeof(line), f) && 380043840dfSDag-Erling Smørgrav sscanf(line, "%*s %511s %511s", proto, data) == 2) 381043840dfSDag-Erling Smørgrav got_data = 1; 382043840dfSDag-Erling Smørgrav if (f) 383043840dfSDag-Erling Smørgrav pclose(f); 3842f513db7SEd Maste free(cmd); 385acc1a9efSDag-Erling Smørgrav } 386043840dfSDag-Erling Smørgrav } 387043840dfSDag-Erling Smørgrav 388043840dfSDag-Erling Smørgrav if (do_unlink) { 389043840dfSDag-Erling Smørgrav unlink(xauthfile); 390043840dfSDag-Erling Smørgrav rmdir(xauthdir); 391043840dfSDag-Erling Smørgrav } 392acc1a9efSDag-Erling Smørgrav 393acc1a9efSDag-Erling Smørgrav /* Don't fall back to fake X11 data for untrusted forwarding */ 394acc1a9efSDag-Erling Smørgrav if (!trusted && !got_data) { 395acc1a9efSDag-Erling Smørgrav error("Warning: untrusted X11 forwarding setup failed: " 396acc1a9efSDag-Erling Smørgrav "xauth key data not generated"); 397acc1a9efSDag-Erling Smørgrav return -1; 398acc1a9efSDag-Erling Smørgrav } 399043840dfSDag-Erling Smørgrav 400043840dfSDag-Erling Smørgrav /* 401043840dfSDag-Erling Smørgrav * If we didn't get authentication data, just make up some 402043840dfSDag-Erling Smørgrav * data. The forwarding code will check the validity of the 403043840dfSDag-Erling Smørgrav * response anyway, and substitute this data. The X11 404043840dfSDag-Erling Smørgrav * server, however, will ignore this fake data and use 405043840dfSDag-Erling Smørgrav * whatever authentication mechanisms it was using otherwise 406043840dfSDag-Erling Smørgrav * for the local connection. 407043840dfSDag-Erling Smørgrav */ 408043840dfSDag-Erling Smørgrav if (!got_data) { 409ca86bcf2SDag-Erling Smørgrav u_int8_t rnd[16]; 410ca86bcf2SDag-Erling Smørgrav u_int i; 411043840dfSDag-Erling Smørgrav 412043840dfSDag-Erling Smørgrav logit("Warning: No xauth data; " 413043840dfSDag-Erling Smørgrav "using fake authentication data for X11 forwarding."); 414043840dfSDag-Erling Smørgrav strlcpy(proto, SSH_X11_PROTO, sizeof proto); 415ca86bcf2SDag-Erling Smørgrav arc4random_buf(rnd, sizeof(rnd)); 416ca86bcf2SDag-Erling Smørgrav for (i = 0; i < sizeof(rnd); i++) { 417043840dfSDag-Erling Smørgrav snprintf(data + 2 * i, sizeof data - 2 * i, "%02x", 418ca86bcf2SDag-Erling Smørgrav rnd[i]); 419043840dfSDag-Erling Smørgrav } 420043840dfSDag-Erling Smørgrav } 421acc1a9efSDag-Erling Smørgrav 422acc1a9efSDag-Erling Smørgrav return 0; 423043840dfSDag-Erling Smørgrav } 424043840dfSDag-Erling Smørgrav 425511b41d2SMark Murray /* 426511b41d2SMark Murray * Checks if the client window has changed, and sends a packet about it to 427511b41d2SMark Murray * the server if so. The actual change is detected elsewhere (by a software 428511b41d2SMark Murray * interrupt on Unix); this just checks the flag and sends a message if 429511b41d2SMark Murray * appropriate. 430511b41d2SMark Murray */ 431511b41d2SMark Murray 432ae1f160dSDag-Erling Smørgrav static void 4334f52dfbbSDag-Erling Smørgrav client_check_window_change(struct ssh *ssh) 434511b41d2SMark Murray { 435a04a10f8SKris Kennaway if (!received_window_change_signal) 436a04a10f8SKris Kennaway return; 437511b41d2SMark Murray received_window_change_signal = 0; 438*19261079SEd Maste debug2_f("changed"); 4394f52dfbbSDag-Erling Smørgrav channel_send_window_changes(ssh); 440511b41d2SMark Murray } 441511b41d2SMark Murray 442bc5531deSDag-Erling Smørgrav static int 4434f52dfbbSDag-Erling Smørgrav client_global_request_reply(int type, u_int32_t seq, struct ssh *ssh) 444efcad6b7SDag-Erling Smørgrav { 445d4af9e69SDag-Erling Smørgrav struct global_confirm *gc; 446d4af9e69SDag-Erling Smørgrav 447d4af9e69SDag-Erling Smørgrav if ((gc = TAILQ_FIRST(&global_confirms)) == NULL) 448bc5531deSDag-Erling Smørgrav return 0; 449d4af9e69SDag-Erling Smørgrav if (gc->cb != NULL) 4504f52dfbbSDag-Erling Smørgrav gc->cb(ssh, type, seq, gc->ctx); 451d4af9e69SDag-Erling Smørgrav if (--gc->ref_count <= 0) { 452d4af9e69SDag-Erling Smørgrav TAILQ_REMOVE(&global_confirms, gc, entry); 453*19261079SEd Maste freezero(gc, sizeof(*gc)); 454d4af9e69SDag-Erling Smørgrav } 455d4af9e69SDag-Erling Smørgrav 456*19261079SEd Maste ssh_packet_set_alive_timeouts(ssh, 0); 457bc5531deSDag-Erling Smørgrav return 0; 458efcad6b7SDag-Erling Smørgrav } 459efcad6b7SDag-Erling Smørgrav 460efcad6b7SDag-Erling Smørgrav static void 461*19261079SEd Maste schedule_server_alive_check(void) 462efcad6b7SDag-Erling Smørgrav { 463*19261079SEd Maste if (options.server_alive_interval > 0) 464*19261079SEd Maste server_alive_time = monotime() + options.server_alive_interval; 465*19261079SEd Maste } 466*19261079SEd Maste 467*19261079SEd Maste static void 468*19261079SEd Maste server_alive_check(struct ssh *ssh) 469*19261079SEd Maste { 470*19261079SEd Maste int r; 471*19261079SEd Maste 472*19261079SEd Maste if (ssh_packet_inc_alive_timeouts(ssh) > options.server_alive_count_max) { 4734a421b63SDag-Erling Smørgrav logit("Timeout, server %s not responding.", host); 47492eb0aa1SDag-Erling Smørgrav cleanup_exit(255); 47592eb0aa1SDag-Erling Smørgrav } 476*19261079SEd Maste if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || 477*19261079SEd Maste (r = sshpkt_put_cstring(ssh, "keepalive@openssh.com")) != 0 || 478*19261079SEd Maste (r = sshpkt_put_u8(ssh, 1)) != 0 || /* boolean: want reply */ 479*19261079SEd Maste (r = sshpkt_send(ssh)) != 0) 480*19261079SEd Maste fatal_fr(r, "send packet"); 481d4af9e69SDag-Erling Smørgrav /* Insert an empty placeholder to maintain ordering */ 482d4af9e69SDag-Erling Smørgrav client_register_global_confirm(NULL, NULL); 483*19261079SEd Maste schedule_server_alive_check(); 484efcad6b7SDag-Erling Smørgrav } 485efcad6b7SDag-Erling Smørgrav 486511b41d2SMark Murray /* 487511b41d2SMark Murray * Waits until the client can do something (some data becomes available on 488511b41d2SMark Murray * one of the file descriptors). 489511b41d2SMark Murray */ 490ae1f160dSDag-Erling Smørgrav static void 4914f52dfbbSDag-Erling Smørgrav client_wait_until_can_do_something(struct ssh *ssh, 4924f52dfbbSDag-Erling Smørgrav fd_set **readsetp, fd_set **writesetp, 493d74d50a8SDag-Erling Smørgrav int *maxfdp, u_int *nallocp, int rekeying) 494511b41d2SMark Murray { 495efcad6b7SDag-Erling Smørgrav struct timeval tv, *tvp; 496e2f6069cSDag-Erling Smørgrav int timeout_secs; 497*19261079SEd Maste time_t minwait_secs = 0, now = monotime(); 498190cef3dSDag-Erling Smørgrav int r, ret; 499efcad6b7SDag-Erling Smørgrav 5001e8db6e2SBrian Feldman /* Add any selections by the channel mechanism. */ 501*19261079SEd Maste channel_prepare_select(ssh, readsetp, writesetp, maxfdp, 5024f52dfbbSDag-Erling Smørgrav nallocp, &minwait_secs); 503511b41d2SMark Murray 504ae1f160dSDag-Erling Smørgrav /* channel_prepare_select could have closed the last channel */ 5054f52dfbbSDag-Erling Smørgrav if (session_closed && !channel_still_open(ssh) && 506*19261079SEd Maste !ssh_packet_have_data_to_write(ssh)) { 507ae1f160dSDag-Erling Smørgrav /* clear mask since we did not call select() */ 508ae1f160dSDag-Erling Smørgrav memset(*readsetp, 0, *nallocp); 509ae1f160dSDag-Erling Smørgrav memset(*writesetp, 0, *nallocp); 510ae1f160dSDag-Erling Smørgrav return; 5114f52dfbbSDag-Erling Smørgrav } 5124f52dfbbSDag-Erling Smørgrav 5131e8db6e2SBrian Feldman FD_SET(connection_in, *readsetp); 514511b41d2SMark Murray 515511b41d2SMark Murray /* Select server connection if have data to write to the server. */ 516*19261079SEd Maste if (ssh_packet_have_data_to_write(ssh)) 5171e8db6e2SBrian Feldman FD_SET(connection_out, *writesetp); 518511b41d2SMark Murray 519511b41d2SMark Murray /* 520511b41d2SMark Murray * Wait for something to happen. This will suspend the process until 521511b41d2SMark Murray * some selected descriptor can be read, written, or has some other 522e2f6069cSDag-Erling Smørgrav * event pending, or a timeout expires. 523511b41d2SMark Murray */ 524511b41d2SMark Murray 525e2f6069cSDag-Erling Smørgrav timeout_secs = INT_MAX; /* we use INT_MAX to mean no timeout */ 526*19261079SEd Maste if (options.server_alive_interval > 0) 527*19261079SEd Maste timeout_secs = MAXIMUM(server_alive_time - now, 0); 5284f52dfbbSDag-Erling Smørgrav if (options.rekey_interval > 0 && !rekeying) 529*19261079SEd Maste timeout_secs = MINIMUM(timeout_secs, 530*19261079SEd Maste ssh_packet_get_rekey_timeout(ssh)); 5314f52dfbbSDag-Erling Smørgrav set_control_persist_exit_time(ssh); 532e2f6069cSDag-Erling Smørgrav if (control_persist_exit_time > 0) { 533ca86bcf2SDag-Erling Smørgrav timeout_secs = MINIMUM(timeout_secs, 534e4a9863fSDag-Erling Smørgrav control_persist_exit_time - now); 535e2f6069cSDag-Erling Smørgrav if (timeout_secs < 0) 536e2f6069cSDag-Erling Smørgrav timeout_secs = 0; 537e2f6069cSDag-Erling Smørgrav } 538462c32cbSDag-Erling Smørgrav if (minwait_secs != 0) 539ca86bcf2SDag-Erling Smørgrav timeout_secs = MINIMUM(timeout_secs, (int)minwait_secs); 540e2f6069cSDag-Erling Smørgrav if (timeout_secs == INT_MAX) 541efcad6b7SDag-Erling Smørgrav tvp = NULL; 542efcad6b7SDag-Erling Smørgrav else { 543e2f6069cSDag-Erling Smørgrav tv.tv_sec = timeout_secs; 544efcad6b7SDag-Erling Smørgrav tv.tv_usec = 0; 545efcad6b7SDag-Erling Smørgrav tvp = &tv; 546efcad6b7SDag-Erling Smørgrav } 547e2f6069cSDag-Erling Smørgrav 548efcad6b7SDag-Erling Smørgrav ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp); 549*19261079SEd Maste if (ret == -1) { 5501e8db6e2SBrian Feldman /* 5511e8db6e2SBrian Feldman * We have to clear the select masks, because we return. 5521e8db6e2SBrian Feldman * We have to return, because the mainloop checks for the flags 5531e8db6e2SBrian Feldman * set by the signal handlers. 5541e8db6e2SBrian Feldman */ 555ae1f160dSDag-Erling Smørgrav memset(*readsetp, 0, *nallocp); 556ae1f160dSDag-Erling Smørgrav memset(*writesetp, 0, *nallocp); 557511b41d2SMark Murray if (errno == EINTR) 558511b41d2SMark Murray return; 559511b41d2SMark Murray /* Note: we might still have data in the buffers. */ 560190cef3dSDag-Erling Smørgrav if ((r = sshbuf_putf(stderr_buffer, 561190cef3dSDag-Erling Smørgrav "select: %s\r\n", strerror(errno))) != 0) 562*19261079SEd Maste fatal_fr(r, "sshbuf_putf"); 563511b41d2SMark Murray quit_pending = 1; 564*19261079SEd Maste } else if (options.server_alive_interval > 0 && !FD_ISSET(connection_in, 565*19261079SEd Maste *readsetp) && monotime() >= server_alive_time) 566e4a9863fSDag-Erling Smørgrav /* 567*19261079SEd Maste * ServerAlive check is needed. We can't rely on the select 568*19261079SEd Maste * timing out since traffic on the client side such as port 569*19261079SEd Maste * forwards can keep waking it up. 570e4a9863fSDag-Erling Smørgrav */ 571*19261079SEd Maste server_alive_check(ssh); 572e4a9863fSDag-Erling Smørgrav } 573e4a9863fSDag-Erling Smørgrav 574ae1f160dSDag-Erling Smørgrav static void 575190cef3dSDag-Erling Smørgrav client_suspend_self(struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr) 576511b41d2SMark Murray { 577511b41d2SMark Murray /* Flush stdout and stderr buffers. */ 578190cef3dSDag-Erling Smørgrav if (sshbuf_len(bout) > 0) 579190cef3dSDag-Erling Smørgrav atomicio(vwrite, fileno(stdout), sshbuf_mutable_ptr(bout), 580190cef3dSDag-Erling Smørgrav sshbuf_len(bout)); 581190cef3dSDag-Erling Smørgrav if (sshbuf_len(berr) > 0) 582190cef3dSDag-Erling Smørgrav atomicio(vwrite, fileno(stderr), sshbuf_mutable_ptr(berr), 583190cef3dSDag-Erling Smørgrav sshbuf_len(berr)); 584511b41d2SMark Murray 585e146993eSDag-Erling Smørgrav leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 586511b41d2SMark Murray 5874f52dfbbSDag-Erling Smørgrav sshbuf_reset(bin); 5884f52dfbbSDag-Erling Smørgrav sshbuf_reset(bout); 5894f52dfbbSDag-Erling Smørgrav sshbuf_reset(berr); 590511b41d2SMark Murray 591511b41d2SMark Murray /* Send the suspend signal to the program itself. */ 592511b41d2SMark Murray kill(getpid(), SIGTSTP); 593511b41d2SMark Murray 5945e8dbd04SDag-Erling Smørgrav /* Reset window sizes in case they have changed */ 595511b41d2SMark Murray received_window_change_signal = 1; 596511b41d2SMark Murray 597e146993eSDag-Erling Smørgrav enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 598511b41d2SMark Murray } 599511b41d2SMark Murray 600ae1f160dSDag-Erling Smørgrav static void 601*19261079SEd Maste client_process_net_input(struct ssh *ssh, fd_set *readset) 602511b41d2SMark Murray { 6037aee6ffeSDag-Erling Smørgrav char buf[SSH_IOBUFSZ]; 604190cef3dSDag-Erling Smørgrav int r, len; 605511b41d2SMark Murray 606511b41d2SMark Murray /* 607511b41d2SMark Murray * Read input from the server, and add any such data to the buffer of 608511b41d2SMark Murray * the packet subsystem. 609511b41d2SMark Murray */ 610511b41d2SMark Murray if (FD_ISSET(connection_in, readset)) { 611*19261079SEd Maste schedule_server_alive_check(); 612511b41d2SMark Murray /* Read as much as possible. */ 613acc1a9efSDag-Erling Smørgrav len = read(connection_in, buf, sizeof(buf)); 614acc1a9efSDag-Erling Smørgrav if (len == 0) { 615d4af9e69SDag-Erling Smørgrav /* 616d4af9e69SDag-Erling Smørgrav * Received EOF. The remote host has closed the 617d4af9e69SDag-Erling Smørgrav * connection. 618d4af9e69SDag-Erling Smørgrav */ 619190cef3dSDag-Erling Smørgrav if ((r = sshbuf_putf(stderr_buffer, 620d4af9e69SDag-Erling Smørgrav "Connection to %.300s closed by remote host.\r\n", 621190cef3dSDag-Erling Smørgrav host)) != 0) 622*19261079SEd Maste fatal_fr(r, "sshbuf_putf"); 623511b41d2SMark Murray quit_pending = 1; 624511b41d2SMark Murray return; 625511b41d2SMark Murray } 626511b41d2SMark Murray /* 627511b41d2SMark Murray * There is a kernel bug on Solaris that causes select to 628511b41d2SMark Murray * sometimes wake up even though there is no data available. 629511b41d2SMark Murray */ 630*19261079SEd Maste if (len == -1 && 631d4af9e69SDag-Erling Smørgrav (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK)) 632511b41d2SMark Murray len = 0; 633511b41d2SMark Murray 634*19261079SEd Maste if (len == -1) { 635d4af9e69SDag-Erling Smørgrav /* 636d4af9e69SDag-Erling Smørgrav * An error has encountered. Perhaps there is a 637d4af9e69SDag-Erling Smørgrav * network problem. 638d4af9e69SDag-Erling Smørgrav */ 639190cef3dSDag-Erling Smørgrav if ((r = sshbuf_putf(stderr_buffer, 640d4af9e69SDag-Erling Smørgrav "Read from remote host %.300s: %.100s\r\n", 641190cef3dSDag-Erling Smørgrav host, strerror(errno))) != 0) 642*19261079SEd Maste fatal_fr(r, "sshbuf_putf"); 643511b41d2SMark Murray quit_pending = 1; 644511b41d2SMark Murray return; 645511b41d2SMark Murray } 646*19261079SEd Maste ssh_packet_process_incoming(ssh, buf, len); 647511b41d2SMark Murray } 648a04a10f8SKris Kennaway } 649a04a10f8SKris Kennaway 650545d5ecaSDag-Erling Smørgrav static void 6514f52dfbbSDag-Erling Smørgrav client_status_confirm(struct ssh *ssh, int type, Channel *c, void *ctx) 652d74d50a8SDag-Erling Smørgrav { 653d4af9e69SDag-Erling Smørgrav struct channel_reply_ctx *cr = (struct channel_reply_ctx *)ctx; 654d4af9e69SDag-Erling Smørgrav char errmsg[256]; 655190cef3dSDag-Erling Smørgrav int r, tochan; 656d74d50a8SDag-Erling Smørgrav 657e146993eSDag-Erling Smørgrav /* 658e146993eSDag-Erling Smørgrav * If a TTY was explicitly requested, then a failure to allocate 659e146993eSDag-Erling Smørgrav * one is fatal. 660e146993eSDag-Erling Smørgrav */ 661e146993eSDag-Erling Smørgrav if (cr->action == CONFIRM_TTY && 662e146993eSDag-Erling Smørgrav (options.request_tty == REQUEST_TTY_FORCE || 663e146993eSDag-Erling Smørgrav options.request_tty == REQUEST_TTY_YES)) 664e146993eSDag-Erling Smørgrav cr->action = CONFIRM_CLOSE; 665e146993eSDag-Erling Smørgrav 666190cef3dSDag-Erling Smørgrav /* XXX suppress on mux _client_ quietmode */ 667d4af9e69SDag-Erling Smørgrav tochan = options.log_level >= SYSLOG_LEVEL_ERROR && 668b15c8340SDag-Erling Smørgrav c->ctl_chan != -1 && c->extended_usage == CHAN_EXTENDED_WRITE; 669d74d50a8SDag-Erling Smørgrav 670d4af9e69SDag-Erling Smørgrav if (type == SSH2_MSG_CHANNEL_SUCCESS) { 671d4af9e69SDag-Erling Smørgrav debug2("%s request accepted on channel %d", 672d4af9e69SDag-Erling Smørgrav cr->request_type, c->self); 673d4af9e69SDag-Erling Smørgrav } else if (type == SSH2_MSG_CHANNEL_FAILURE) { 674d4af9e69SDag-Erling Smørgrav if (tochan) { 675d4af9e69SDag-Erling Smørgrav snprintf(errmsg, sizeof(errmsg), 676d4af9e69SDag-Erling Smørgrav "%s request failed\r\n", cr->request_type); 677d4af9e69SDag-Erling Smørgrav } else { 678d4af9e69SDag-Erling Smørgrav snprintf(errmsg, sizeof(errmsg), 679d4af9e69SDag-Erling Smørgrav "%s request failed on channel %d", 680d4af9e69SDag-Erling Smørgrav cr->request_type, c->self); 681d74d50a8SDag-Erling Smørgrav } 682d4af9e69SDag-Erling Smørgrav /* If error occurred on primary session channel, then exit */ 683e146993eSDag-Erling Smørgrav if (cr->action == CONFIRM_CLOSE && c->self == session_ident) 684d4af9e69SDag-Erling Smørgrav fatal("%s", errmsg); 685e146993eSDag-Erling Smørgrav /* 686e146993eSDag-Erling Smørgrav * If error occurred on mux client, append to 687e146993eSDag-Erling Smørgrav * their stderr. 688e146993eSDag-Erling Smørgrav */ 689e146993eSDag-Erling Smørgrav if (tochan) { 690*19261079SEd Maste debug3_f("channel %d: mux request: %s", c->self, 691*19261079SEd Maste cr->request_type); 692190cef3dSDag-Erling Smørgrav if ((r = sshbuf_put(c->extended, errmsg, 693190cef3dSDag-Erling Smørgrav strlen(errmsg))) != 0) 694*19261079SEd Maste fatal_fr(r, "sshbuf_put"); 695e146993eSDag-Erling Smørgrav } else 696d4af9e69SDag-Erling Smørgrav error("%s", errmsg); 697e146993eSDag-Erling Smørgrav if (cr->action == CONFIRM_TTY) { 698e146993eSDag-Erling Smørgrav /* 699e146993eSDag-Erling Smørgrav * If a TTY allocation error occurred, then arrange 700e146993eSDag-Erling Smørgrav * for the correct TTY to leave raw mode. 701e146993eSDag-Erling Smørgrav */ 702e146993eSDag-Erling Smørgrav if (c->self == session_ident) 703e146993eSDag-Erling Smørgrav leave_raw_mode(0); 704e146993eSDag-Erling Smørgrav else 7054f52dfbbSDag-Erling Smørgrav mux_tty_alloc_failed(ssh, c); 706e146993eSDag-Erling Smørgrav } else if (cr->action == CONFIRM_CLOSE) { 7074f52dfbbSDag-Erling Smørgrav chan_read_failed(ssh, c); 7084f52dfbbSDag-Erling Smørgrav chan_write_failed(ssh, c); 709d74d50a8SDag-Erling Smørgrav } 710d74d50a8SDag-Erling Smørgrav } 711e4a9863fSDag-Erling Smørgrav free(cr); 712d4af9e69SDag-Erling Smørgrav } 713d74d50a8SDag-Erling Smørgrav 714d74d50a8SDag-Erling Smørgrav static void 7154f52dfbbSDag-Erling Smørgrav client_abandon_status_confirm(struct ssh *ssh, Channel *c, void *ctx) 716d74d50a8SDag-Erling Smørgrav { 717e4a9863fSDag-Erling Smørgrav free(ctx); 718d74d50a8SDag-Erling Smørgrav } 719d74d50a8SDag-Erling Smørgrav 720e146993eSDag-Erling Smørgrav void 7214f52dfbbSDag-Erling Smørgrav client_expect_confirm(struct ssh *ssh, int id, const char *request, 722e146993eSDag-Erling Smørgrav enum confirm_action action) 723d74d50a8SDag-Erling Smørgrav { 7240a37d4a3SXin LI struct channel_reply_ctx *cr = xcalloc(1, sizeof(*cr)); 725d74d50a8SDag-Erling Smørgrav 726d4af9e69SDag-Erling Smørgrav cr->request_type = request; 727e146993eSDag-Erling Smørgrav cr->action = action; 728d74d50a8SDag-Erling Smørgrav 7294f52dfbbSDag-Erling Smørgrav channel_register_status_confirm(ssh, id, client_status_confirm, 730d4af9e69SDag-Erling Smørgrav client_abandon_status_confirm, cr); 731d4af9e69SDag-Erling Smørgrav } 732d4af9e69SDag-Erling Smørgrav 733d4af9e69SDag-Erling Smørgrav void 734d4af9e69SDag-Erling Smørgrav client_register_global_confirm(global_confirm_cb *cb, void *ctx) 735d4af9e69SDag-Erling Smørgrav { 736d4af9e69SDag-Erling Smørgrav struct global_confirm *gc, *last_gc; 737d4af9e69SDag-Erling Smørgrav 738d4af9e69SDag-Erling Smørgrav /* Coalesce identical callbacks */ 739d4af9e69SDag-Erling Smørgrav last_gc = TAILQ_LAST(&global_confirms, global_confirms); 740d4af9e69SDag-Erling Smørgrav if (last_gc && last_gc->cb == cb && last_gc->ctx == ctx) { 741d4af9e69SDag-Erling Smørgrav if (++last_gc->ref_count >= INT_MAX) 742*19261079SEd Maste fatal_f("last_gc->ref_count = %d", 743*19261079SEd Maste last_gc->ref_count); 744d74d50a8SDag-Erling Smørgrav return; 745d74d50a8SDag-Erling Smørgrav } 746d74d50a8SDag-Erling Smørgrav 7470a37d4a3SXin LI gc = xcalloc(1, sizeof(*gc)); 748d4af9e69SDag-Erling Smørgrav gc->cb = cb; 749d4af9e69SDag-Erling Smørgrav gc->ctx = ctx; 750d4af9e69SDag-Erling Smørgrav gc->ref_count = 1; 751d4af9e69SDag-Erling Smørgrav TAILQ_INSERT_TAIL(&global_confirms, gc, entry); 752d74d50a8SDag-Erling Smørgrav } 753d74d50a8SDag-Erling Smørgrav 754d74d50a8SDag-Erling Smørgrav static void 7554f52dfbbSDag-Erling Smørgrav process_cmdline(struct ssh *ssh) 756545d5ecaSDag-Erling Smørgrav { 757545d5ecaSDag-Erling Smørgrav void (*handler)(int); 758a0ee8cc6SDag-Erling Smørgrav char *s, *cmd; 759a0ee8cc6SDag-Erling Smørgrav int ok, delete = 0, local = 0, remote = 0, dynamic = 0; 760a0ee8cc6SDag-Erling Smørgrav struct Forward fwd; 761545d5ecaSDag-Erling Smørgrav 762b83788ffSDag-Erling Smørgrav memset(&fwd, 0, sizeof(fwd)); 763d4af9e69SDag-Erling Smørgrav 764e146993eSDag-Erling Smørgrav leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 765*19261079SEd Maste handler = ssh_signal(SIGINT, SIG_IGN); 766545d5ecaSDag-Erling Smørgrav cmd = s = read_passphrase("\r\nssh> ", RP_ECHO); 767545d5ecaSDag-Erling Smørgrav if (s == NULL) 768545d5ecaSDag-Erling Smørgrav goto out; 769f7167e0eSDag-Erling Smørgrav while (isspace((u_char)*s)) 770545d5ecaSDag-Erling Smørgrav s++; 771d74d50a8SDag-Erling Smørgrav if (*s == '-') 772d74d50a8SDag-Erling Smørgrav s++; /* Skip cmdline '-', if any */ 773d74d50a8SDag-Erling Smørgrav if (*s == '\0') 774545d5ecaSDag-Erling Smørgrav goto out; 775d74d50a8SDag-Erling Smørgrav 776d74d50a8SDag-Erling Smørgrav if (*s == 'h' || *s == 'H' || *s == '?') { 777d74d50a8SDag-Erling Smørgrav logit("Commands:"); 778761efaa7SDag-Erling Smørgrav logit(" -L[bind_address:]port:host:hostport " 779761efaa7SDag-Erling Smørgrav "Request local forward"); 780761efaa7SDag-Erling Smørgrav logit(" -R[bind_address:]port:host:hostport " 781761efaa7SDag-Erling Smørgrav "Request remote forward"); 782cce7d346SDag-Erling Smørgrav logit(" -D[bind_address:]port " 783cce7d346SDag-Erling Smørgrav "Request dynamic forward"); 784462c32cbSDag-Erling Smørgrav logit(" -KL[bind_address:]port " 785462c32cbSDag-Erling Smørgrav "Cancel local forward"); 786761efaa7SDag-Erling Smørgrav logit(" -KR[bind_address:]port " 787761efaa7SDag-Erling Smørgrav "Cancel remote forward"); 788462c32cbSDag-Erling Smørgrav logit(" -KD[bind_address:]port " 789462c32cbSDag-Erling Smørgrav "Cancel dynamic forward"); 790021d409fSDag-Erling Smørgrav if (!options.permit_local_command) 791021d409fSDag-Erling Smørgrav goto out; 792761efaa7SDag-Erling Smørgrav logit(" !args " 793761efaa7SDag-Erling Smørgrav "Execute local command"); 794021d409fSDag-Erling Smørgrav goto out; 795021d409fSDag-Erling Smørgrav } 796021d409fSDag-Erling Smørgrav 797021d409fSDag-Erling Smørgrav if (*s == '!' && options.permit_local_command) { 798021d409fSDag-Erling Smørgrav s++; 799021d409fSDag-Erling Smørgrav ssh_local_cmd(s); 800d74d50a8SDag-Erling Smørgrav goto out; 801d74d50a8SDag-Erling Smørgrav } 802d74d50a8SDag-Erling Smørgrav 803d74d50a8SDag-Erling Smørgrav if (*s == 'K') { 804d74d50a8SDag-Erling Smørgrav delete = 1; 805d74d50a8SDag-Erling Smørgrav s++; 806d74d50a8SDag-Erling Smørgrav } 807cce7d346SDag-Erling Smørgrav if (*s == 'L') 808cce7d346SDag-Erling Smørgrav local = 1; 809cce7d346SDag-Erling Smørgrav else if (*s == 'R') 810cce7d346SDag-Erling Smørgrav remote = 1; 811cce7d346SDag-Erling Smørgrav else if (*s == 'D') 812cce7d346SDag-Erling Smørgrav dynamic = 1; 813cce7d346SDag-Erling Smørgrav else { 814d95e11bfSDag-Erling Smørgrav logit("Invalid command."); 815545d5ecaSDag-Erling Smørgrav goto out; 816545d5ecaSDag-Erling Smørgrav } 817cce7d346SDag-Erling Smørgrav 818f7167e0eSDag-Erling Smørgrav while (isspace((u_char)*++s)) 819d4af9e69SDag-Erling Smørgrav ; 820545d5ecaSDag-Erling Smørgrav 821b15c8340SDag-Erling Smørgrav /* XXX update list of forwards in options */ 822d74d50a8SDag-Erling Smørgrav if (delete) { 823a0ee8cc6SDag-Erling Smørgrav /* We pass 1 for dynamicfwd to restrict to 1 or 2 fields. */ 824a0ee8cc6SDag-Erling Smørgrav if (!parse_forward(&fwd, s, 1, 0)) { 825a0ee8cc6SDag-Erling Smørgrav logit("Bad forwarding close specification."); 826545d5ecaSDag-Erling Smørgrav goto out; 827545d5ecaSDag-Erling Smørgrav } 828462c32cbSDag-Erling Smørgrav if (remote) 8294f52dfbbSDag-Erling Smørgrav ok = channel_request_rforward_cancel(ssh, &fwd) == 0; 830462c32cbSDag-Erling Smørgrav else if (dynamic) 8314f52dfbbSDag-Erling Smørgrav ok = channel_cancel_lport_listener(ssh, &fwd, 832a0ee8cc6SDag-Erling Smørgrav 0, &options.fwd_opts) > 0; 833462c32cbSDag-Erling Smørgrav else 8344f52dfbbSDag-Erling Smørgrav ok = channel_cancel_lport_listener(ssh, &fwd, 835a0ee8cc6SDag-Erling Smørgrav CHANNEL_CANCEL_PORT_STATIC, 836a0ee8cc6SDag-Erling Smørgrav &options.fwd_opts) > 0; 837462c32cbSDag-Erling Smørgrav if (!ok) { 838d93a896eSDag-Erling Smørgrav logit("Unknown port forwarding."); 839462c32cbSDag-Erling Smørgrav goto out; 840462c32cbSDag-Erling Smørgrav } 841462c32cbSDag-Erling Smørgrav logit("Canceled forwarding."); 8425e8dbd04SDag-Erling Smørgrav } else { 843cce7d346SDag-Erling Smørgrav if (!parse_forward(&fwd, s, dynamic, remote)) { 8445e8dbd04SDag-Erling Smørgrav logit("Bad forwarding specification."); 845545d5ecaSDag-Erling Smørgrav goto out; 846545d5ecaSDag-Erling Smørgrav } 847cce7d346SDag-Erling Smørgrav if (local || dynamic) { 8484f52dfbbSDag-Erling Smørgrav if (!channel_setup_local_fwd_listener(ssh, &fwd, 849a0ee8cc6SDag-Erling Smørgrav &options.fwd_opts)) { 850d95e11bfSDag-Erling Smørgrav logit("Port forwarding failed."); 851545d5ecaSDag-Erling Smørgrav goto out; 852545d5ecaSDag-Erling Smørgrav } 8535e8dbd04SDag-Erling Smørgrav } else { 8544f52dfbbSDag-Erling Smørgrav if (channel_request_remote_forwarding(ssh, &fwd) < 0) { 855761efaa7SDag-Erling Smørgrav logit("Port forwarding failed."); 856761efaa7SDag-Erling Smørgrav goto out; 857761efaa7SDag-Erling Smørgrav } 8585e8dbd04SDag-Erling Smørgrav } 859d95e11bfSDag-Erling Smørgrav logit("Forwarding port."); 860d74d50a8SDag-Erling Smørgrav } 861d74d50a8SDag-Erling Smørgrav 862545d5ecaSDag-Erling Smørgrav out: 863*19261079SEd Maste ssh_signal(SIGINT, handler); 864e146993eSDag-Erling Smørgrav enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 865e4a9863fSDag-Erling Smørgrav free(cmd); 866e4a9863fSDag-Erling Smørgrav free(fwd.listen_host); 867a0ee8cc6SDag-Erling Smørgrav free(fwd.listen_path); 868e4a9863fSDag-Erling Smørgrav free(fwd.connect_host); 869a0ee8cc6SDag-Erling Smørgrav free(fwd.connect_path); 870545d5ecaSDag-Erling Smørgrav } 871545d5ecaSDag-Erling Smørgrav 8726888a9beSDag-Erling Smørgrav /* reasons to suppress output of an escape command in help output */ 8736888a9beSDag-Erling Smørgrav #define SUPPRESS_NEVER 0 /* never suppress, always show */ 8744f52dfbbSDag-Erling Smørgrav #define SUPPRESS_MUXCLIENT 1 /* don't show in mux client sessions */ 8754f52dfbbSDag-Erling Smørgrav #define SUPPRESS_MUXMASTER 2 /* don't show in mux master sessions */ 8764f52dfbbSDag-Erling Smørgrav #define SUPPRESS_SYSLOG 4 /* don't show when logging to syslog */ 8776888a9beSDag-Erling Smørgrav struct escape_help_text { 8786888a9beSDag-Erling Smørgrav const char *cmd; 8796888a9beSDag-Erling Smørgrav const char *text; 8806888a9beSDag-Erling Smørgrav unsigned int flags; 8816888a9beSDag-Erling Smørgrav }; 8826888a9beSDag-Erling Smørgrav static struct escape_help_text esc_txt[] = { 8836888a9beSDag-Erling Smørgrav {".", "terminate session", SUPPRESS_MUXMASTER}, 8846888a9beSDag-Erling Smørgrav {".", "terminate connection (and any multiplexed sessions)", 8856888a9beSDag-Erling Smørgrav SUPPRESS_MUXCLIENT}, 8864f52dfbbSDag-Erling Smørgrav {"B", "send a BREAK to the remote system", SUPPRESS_NEVER}, 8876888a9beSDag-Erling Smørgrav {"C", "open a command line", SUPPRESS_MUXCLIENT}, 8884f52dfbbSDag-Erling Smørgrav {"R", "request rekey", SUPPRESS_NEVER}, 8896888a9beSDag-Erling Smørgrav {"V/v", "decrease/increase verbosity (LogLevel)", SUPPRESS_MUXCLIENT}, 8906888a9beSDag-Erling Smørgrav {"^Z", "suspend ssh", SUPPRESS_MUXCLIENT}, 8916888a9beSDag-Erling Smørgrav {"#", "list forwarded connections", SUPPRESS_NEVER}, 8926888a9beSDag-Erling Smørgrav {"&", "background ssh (when waiting for connections to terminate)", 8936888a9beSDag-Erling Smørgrav SUPPRESS_MUXCLIENT}, 8946888a9beSDag-Erling Smørgrav {"?", "this message", SUPPRESS_NEVER}, 8956888a9beSDag-Erling Smørgrav }; 8966888a9beSDag-Erling Smørgrav 8976888a9beSDag-Erling Smørgrav static void 898190cef3dSDag-Erling Smørgrav print_escape_help(struct sshbuf *b, int escape_char, int mux_client, 899190cef3dSDag-Erling Smørgrav int using_stderr) 9006888a9beSDag-Erling Smørgrav { 9016888a9beSDag-Erling Smørgrav unsigned int i, suppress_flags; 902190cef3dSDag-Erling Smørgrav int r; 9036888a9beSDag-Erling Smørgrav 904190cef3dSDag-Erling Smørgrav if ((r = sshbuf_putf(b, 905190cef3dSDag-Erling Smørgrav "%c?\r\nSupported escape sequences:\r\n", escape_char)) != 0) 906*19261079SEd Maste fatal_fr(r, "sshbuf_putf"); 9076888a9beSDag-Erling Smørgrav 9084f52dfbbSDag-Erling Smørgrav suppress_flags = 9096888a9beSDag-Erling Smørgrav (mux_client ? SUPPRESS_MUXCLIENT : 0) | 9106888a9beSDag-Erling Smørgrav (mux_client ? 0 : SUPPRESS_MUXMASTER) | 9116888a9beSDag-Erling Smørgrav (using_stderr ? 0 : SUPPRESS_SYSLOG); 9126888a9beSDag-Erling Smørgrav 9136888a9beSDag-Erling Smørgrav for (i = 0; i < sizeof(esc_txt)/sizeof(esc_txt[0]); i++) { 9146888a9beSDag-Erling Smørgrav if (esc_txt[i].flags & suppress_flags) 9156888a9beSDag-Erling Smørgrav continue; 916190cef3dSDag-Erling Smørgrav if ((r = sshbuf_putf(b, " %c%-3s - %s\r\n", 917190cef3dSDag-Erling Smørgrav escape_char, esc_txt[i].cmd, esc_txt[i].text)) != 0) 918*19261079SEd Maste fatal_fr(r, "sshbuf_putf"); 9196888a9beSDag-Erling Smørgrav } 9206888a9beSDag-Erling Smørgrav 921190cef3dSDag-Erling Smørgrav if ((r = sshbuf_putf(b, 9226888a9beSDag-Erling Smørgrav " %c%c - send the escape character by typing it twice\r\n" 9236888a9beSDag-Erling Smørgrav "(Note that escapes are only recognized immediately after " 924190cef3dSDag-Erling Smørgrav "newline.)\r\n", escape_char, escape_char)) != 0) 925*19261079SEd Maste fatal_fr(r, "sshbuf_putf"); 9266888a9beSDag-Erling Smørgrav } 9276888a9beSDag-Erling Smørgrav 928d4af9e69SDag-Erling Smørgrav /* 9294f52dfbbSDag-Erling Smørgrav * Process the characters one by one. 930d4af9e69SDag-Erling Smørgrav */ 931ae1f160dSDag-Erling Smørgrav static int 9324f52dfbbSDag-Erling Smørgrav process_escapes(struct ssh *ssh, Channel *c, 933190cef3dSDag-Erling Smørgrav struct sshbuf *bin, struct sshbuf *bout, struct sshbuf *berr, 934d4af9e69SDag-Erling Smørgrav char *buf, int len) 935b66f2d16SKris Kennaway { 936b66f2d16SKris Kennaway pid_t pid; 937190cef3dSDag-Erling Smørgrav int r, bytes = 0; 9381e8db6e2SBrian Feldman u_int i; 9391e8db6e2SBrian Feldman u_char ch; 940b66f2d16SKris Kennaway char *s; 9414f52dfbbSDag-Erling Smørgrav struct escape_filter_ctx *efc = c->filter_ctx == NULL ? 9424f52dfbbSDag-Erling Smørgrav NULL : (struct escape_filter_ctx *)c->filter_ctx; 943d4af9e69SDag-Erling Smørgrav 944d4af9e69SDag-Erling Smørgrav if (c->filter_ctx == NULL) 945d4af9e69SDag-Erling Smørgrav return 0; 946b66f2d16SKris Kennaway 947043840dfSDag-Erling Smørgrav if (len <= 0) 948043840dfSDag-Erling Smørgrav return (0); 949043840dfSDag-Erling Smørgrav 950043840dfSDag-Erling Smørgrav for (i = 0; i < (u_int)len; i++) { 951b66f2d16SKris Kennaway /* Get one character at a time. */ 952b66f2d16SKris Kennaway ch = buf[i]; 953b66f2d16SKris Kennaway 9544f52dfbbSDag-Erling Smørgrav if (efc->escape_pending) { 955b66f2d16SKris Kennaway /* We have previously seen an escape character. */ 956b66f2d16SKris Kennaway /* Clear the flag now. */ 9574f52dfbbSDag-Erling Smørgrav efc->escape_pending = 0; 958b66f2d16SKris Kennaway 959b66f2d16SKris Kennaway /* Process the escaped character. */ 960b66f2d16SKris Kennaway switch (ch) { 961b66f2d16SKris Kennaway case '.': 962b66f2d16SKris Kennaway /* Terminate the connection. */ 963190cef3dSDag-Erling Smørgrav if ((r = sshbuf_putf(berr, "%c.\r\n", 964190cef3dSDag-Erling Smørgrav efc->escape_char)) != 0) 965*19261079SEd Maste fatal_fr(r, "sshbuf_putf"); 966b15c8340SDag-Erling Smørgrav if (c && c->ctl_chan != -1) { 9674f52dfbbSDag-Erling Smørgrav chan_read_failed(ssh, c); 9684f52dfbbSDag-Erling Smørgrav chan_write_failed(ssh, c); 9694f52dfbbSDag-Erling Smørgrav if (c->detach_user) { 9704f52dfbbSDag-Erling Smørgrav c->detach_user(ssh, 9714f52dfbbSDag-Erling Smørgrav c->self, NULL); 9724f52dfbbSDag-Erling Smørgrav } 973e4a9863fSDag-Erling Smørgrav c->type = SSH_CHANNEL_ABANDONED; 974190cef3dSDag-Erling Smørgrav sshbuf_reset(c->input); 9754f52dfbbSDag-Erling Smørgrav chan_ibuf_empty(ssh, c); 976d4af9e69SDag-Erling Smørgrav return 0; 977d4af9e69SDag-Erling Smørgrav } else 978b66f2d16SKris Kennaway quit_pending = 1; 979b66f2d16SKris Kennaway return -1; 980b66f2d16SKris Kennaway 981b66f2d16SKris Kennaway case 'Z' - 64: 982d4af9e69SDag-Erling Smørgrav /* XXX support this for mux clients */ 983b15c8340SDag-Erling Smørgrav if (c && c->ctl_chan != -1) { 9846888a9beSDag-Erling Smørgrav char b[16]; 985d4af9e69SDag-Erling Smørgrav noescape: 9866888a9beSDag-Erling Smørgrav if (ch == 'Z' - 64) 9876888a9beSDag-Erling Smørgrav snprintf(b, sizeof b, "^Z"); 9886888a9beSDag-Erling Smørgrav else 9896888a9beSDag-Erling Smørgrav snprintf(b, sizeof b, "%c", ch); 990190cef3dSDag-Erling Smørgrav if ((r = sshbuf_putf(berr, 9916888a9beSDag-Erling Smørgrav "%c%s escape not available to " 992d4af9e69SDag-Erling Smørgrav "multiplexed sessions\r\n", 993190cef3dSDag-Erling Smørgrav efc->escape_char, b)) != 0) 994*19261079SEd Maste fatal_fr(r, "sshbuf_putf"); 995d4af9e69SDag-Erling Smørgrav continue; 996d4af9e69SDag-Erling Smørgrav } 997d4af9e69SDag-Erling Smørgrav /* Suspend the program. Inform the user */ 998190cef3dSDag-Erling Smørgrav if ((r = sshbuf_putf(berr, 999190cef3dSDag-Erling Smørgrav "%c^Z [suspend ssh]\r\n", 1000190cef3dSDag-Erling Smørgrav efc->escape_char)) != 0) 1001*19261079SEd Maste fatal_fr(r, "sshbuf_putf"); 1002b66f2d16SKris Kennaway 1003b66f2d16SKris Kennaway /* Restore terminal modes and suspend. */ 1004b66f2d16SKris Kennaway client_suspend_self(bin, bout, berr); 1005b66f2d16SKris Kennaway 1006b66f2d16SKris Kennaway /* We have been continued. */ 1007b66f2d16SKris Kennaway continue; 1008b66f2d16SKris Kennaway 1009d95e11bfSDag-Erling Smørgrav case 'B': 1010190cef3dSDag-Erling Smørgrav if ((r = sshbuf_putf(berr, 1011190cef3dSDag-Erling Smørgrav "%cB\r\n", efc->escape_char)) != 0) 1012*19261079SEd Maste fatal_fr(r, "sshbuf_putf"); 10134f52dfbbSDag-Erling Smørgrav channel_request_start(ssh, c->self, "break", 0); 1014190cef3dSDag-Erling Smørgrav if ((r = sshpkt_put_u32(ssh, 1000)) != 0 || 1015190cef3dSDag-Erling Smørgrav (r = sshpkt_send(ssh)) != 0) 1016*19261079SEd Maste fatal_fr(r, "send packet"); 1017d95e11bfSDag-Erling Smørgrav continue; 1018d95e11bfSDag-Erling Smørgrav 10191e8db6e2SBrian Feldman case 'R': 1020*19261079SEd Maste if (ssh->compat & SSH_BUG_NOREKEY) 1021d4af9e69SDag-Erling Smørgrav logit("Server does not " 1022d4af9e69SDag-Erling Smørgrav "support re-keying"); 10231e8db6e2SBrian Feldman else 10241e8db6e2SBrian Feldman need_rekeying = 1; 10251e8db6e2SBrian Feldman continue; 10261e8db6e2SBrian Feldman 10276888a9beSDag-Erling Smørgrav case 'V': 10286888a9beSDag-Erling Smørgrav /* FALLTHROUGH */ 10296888a9beSDag-Erling Smørgrav case 'v': 10306888a9beSDag-Erling Smørgrav if (c && c->ctl_chan != -1) 10316888a9beSDag-Erling Smørgrav goto noescape; 10326888a9beSDag-Erling Smørgrav if (!log_is_on_stderr()) { 1033190cef3dSDag-Erling Smørgrav if ((r = sshbuf_putf(berr, 10346888a9beSDag-Erling Smørgrav "%c%c [Logging to syslog]\r\n", 1035190cef3dSDag-Erling Smørgrav efc->escape_char, ch)) != 0) 1036*19261079SEd Maste fatal_fr(r, "sshbuf_putf"); 10376888a9beSDag-Erling Smørgrav continue; 10386888a9beSDag-Erling Smørgrav } 10396888a9beSDag-Erling Smørgrav if (ch == 'V' && options.log_level > 10406888a9beSDag-Erling Smørgrav SYSLOG_LEVEL_QUIET) 10416888a9beSDag-Erling Smørgrav log_change_level(--options.log_level); 10426888a9beSDag-Erling Smørgrav if (ch == 'v' && options.log_level < 10436888a9beSDag-Erling Smørgrav SYSLOG_LEVEL_DEBUG3) 10446888a9beSDag-Erling Smørgrav log_change_level(++options.log_level); 1045190cef3dSDag-Erling Smørgrav if ((r = sshbuf_putf(berr, 10464f52dfbbSDag-Erling Smørgrav "%c%c [LogLevel %s]\r\n", 10474f52dfbbSDag-Erling Smørgrav efc->escape_char, ch, 1048190cef3dSDag-Erling Smørgrav log_level_name(options.log_level))) != 0) 1049*19261079SEd Maste fatal_fr(r, "sshbuf_putf"); 10506888a9beSDag-Erling Smørgrav continue; 10516888a9beSDag-Erling Smørgrav 1052b66f2d16SKris Kennaway case '&': 1053b15c8340SDag-Erling Smørgrav if (c && c->ctl_chan != -1) 1054d4af9e69SDag-Erling Smørgrav goto noescape; 1055b66f2d16SKris Kennaway /* 1056d4af9e69SDag-Erling Smørgrav * Detach the program (continue to serve 1057d4af9e69SDag-Erling Smørgrav * connections, but put in background and no 1058d4af9e69SDag-Erling Smørgrav * more new connections). 1059b66f2d16SKris Kennaway */ 1060ae1f160dSDag-Erling Smørgrav /* Restore tty modes. */ 1061e146993eSDag-Erling Smørgrav leave_raw_mode( 1062e146993eSDag-Erling Smørgrav options.request_tty == REQUEST_TTY_FORCE); 1063ae1f160dSDag-Erling Smørgrav 1064ae1f160dSDag-Erling Smørgrav /* Stop listening for new connections. */ 10654f52dfbbSDag-Erling Smørgrav channel_stop_listening(ssh); 1066ae1f160dSDag-Erling Smørgrav 1067*19261079SEd Maste if ((r = sshbuf_putf(berr, "%c& " 1068*19261079SEd Maste "[backgrounded]\n", efc->escape_char)) != 0) 1069*19261079SEd Maste fatal_fr(r, "sshbuf_putf"); 1070ae1f160dSDag-Erling Smørgrav 1071ae1f160dSDag-Erling Smørgrav /* Fork into background. */ 1072ae1f160dSDag-Erling Smørgrav pid = fork(); 1073*19261079SEd Maste if (pid == -1) { 1074ae1f160dSDag-Erling Smørgrav error("fork: %.100s", strerror(errno)); 1075ae1f160dSDag-Erling Smørgrav continue; 1076ae1f160dSDag-Erling Smørgrav } 1077ae1f160dSDag-Erling Smørgrav if (pid != 0) { /* This is the parent. */ 1078ae1f160dSDag-Erling Smørgrav /* The parent just exits. */ 1079ae1f160dSDag-Erling Smørgrav exit(0); 1080ae1f160dSDag-Erling Smørgrav } 1081ae1f160dSDag-Erling Smørgrav /* The child continues serving connections. */ 1082ae1f160dSDag-Erling Smørgrav /* fake EOF on stdin */ 1083190cef3dSDag-Erling Smørgrav if ((r = sshbuf_put_u8(bin, 4)) != 0) 1084*19261079SEd Maste fatal_fr(r, "sshbuf_put_u8"); 1085ae1f160dSDag-Erling Smørgrav return -1; 1086b66f2d16SKris Kennaway case '?': 10874f52dfbbSDag-Erling Smørgrav print_escape_help(berr, efc->escape_char, 10886888a9beSDag-Erling Smørgrav (c && c->ctl_chan != -1), 10896888a9beSDag-Erling Smørgrav log_is_on_stderr()); 1090b66f2d16SKris Kennaway continue; 1091b66f2d16SKris Kennaway 1092b66f2d16SKris Kennaway case '#': 1093190cef3dSDag-Erling Smørgrav if ((r = sshbuf_putf(berr, "%c#\r\n", 1094190cef3dSDag-Erling Smørgrav efc->escape_char)) != 0) 1095*19261079SEd Maste fatal_fr(r, "sshbuf_putf"); 10964f52dfbbSDag-Erling Smørgrav s = channel_open_message(ssh); 1097190cef3dSDag-Erling Smørgrav if ((r = sshbuf_put(berr, s, strlen(s))) != 0) 1098*19261079SEd Maste fatal_fr(r, "sshbuf_put"); 1099e4a9863fSDag-Erling Smørgrav free(s); 1100b66f2d16SKris Kennaway continue; 1101b66f2d16SKris Kennaway 1102545d5ecaSDag-Erling Smørgrav case 'C': 1103b15c8340SDag-Erling Smørgrav if (c && c->ctl_chan != -1) 1104cce7d346SDag-Erling Smørgrav goto noescape; 11054f52dfbbSDag-Erling Smørgrav process_cmdline(ssh); 1106545d5ecaSDag-Erling Smørgrav continue; 1107545d5ecaSDag-Erling Smørgrav 1108b66f2d16SKris Kennaway default: 11094f52dfbbSDag-Erling Smørgrav if (ch != efc->escape_char) { 1110190cef3dSDag-Erling Smørgrav if ((r = sshbuf_put_u8(bin, 1111190cef3dSDag-Erling Smørgrav efc->escape_char)) != 0) 1112*19261079SEd Maste fatal_fr(r, "sshbuf_put_u8"); 1113b66f2d16SKris Kennaway bytes++; 1114b66f2d16SKris Kennaway } 1115b66f2d16SKris Kennaway /* Escaped characters fall through here */ 1116b66f2d16SKris Kennaway break; 1117b66f2d16SKris Kennaway } 1118b66f2d16SKris Kennaway } else { 1119b66f2d16SKris Kennaway /* 1120d4af9e69SDag-Erling Smørgrav * The previous character was not an escape char. 1121d4af9e69SDag-Erling Smørgrav * Check if this is an escape. 1122b66f2d16SKris Kennaway */ 11234f52dfbbSDag-Erling Smørgrav if (last_was_cr && ch == efc->escape_char) { 1124d4af9e69SDag-Erling Smørgrav /* 1125d4af9e69SDag-Erling Smørgrav * It is. Set the flag and continue to 1126d4af9e69SDag-Erling Smørgrav * next character. 1127d4af9e69SDag-Erling Smørgrav */ 11284f52dfbbSDag-Erling Smørgrav efc->escape_pending = 1; 1129b66f2d16SKris Kennaway continue; 1130b66f2d16SKris Kennaway } 1131b66f2d16SKris Kennaway } 1132b66f2d16SKris Kennaway 1133b66f2d16SKris Kennaway /* 1134b66f2d16SKris Kennaway * Normal character. Record whether it was a newline, 1135b66f2d16SKris Kennaway * and append it to the buffer. 1136b66f2d16SKris Kennaway */ 1137b66f2d16SKris Kennaway last_was_cr = (ch == '\r' || ch == '\n'); 1138190cef3dSDag-Erling Smørgrav if ((r = sshbuf_put_u8(bin, ch)) != 0) 1139*19261079SEd Maste fatal_fr(r, "sshbuf_put_u8"); 1140b66f2d16SKris Kennaway bytes++; 1141b66f2d16SKris Kennaway } 1142b66f2d16SKris Kennaway return bytes; 1143b66f2d16SKris Kennaway } 1144b66f2d16SKris Kennaway 1145511b41d2SMark Murray /* 1146a04a10f8SKris Kennaway * Get packets from the connection input buffer, and process them as long as 1147a04a10f8SKris Kennaway * there are packets available. 1148a04a10f8SKris Kennaway * 1149a04a10f8SKris Kennaway * Any unknown packets received during the actual 1150a04a10f8SKris Kennaway * session cause the session to terminate. This is 1151a04a10f8SKris Kennaway * intended to make debugging easier since no 1152a04a10f8SKris Kennaway * confirmations are sent. Any compatible protocol 1153a04a10f8SKris Kennaway * extensions must be negotiated during the 1154a04a10f8SKris Kennaway * preparatory phase. 1155a04a10f8SKris Kennaway */ 1156a04a10f8SKris Kennaway 1157ae1f160dSDag-Erling Smørgrav static void 1158*19261079SEd Maste client_process_buffered_input_packets(struct ssh *ssh) 1159a04a10f8SKris Kennaway { 1160*19261079SEd Maste ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, &quit_pending); 1161a04a10f8SKris Kennaway } 1162a04a10f8SKris Kennaway 1163b66f2d16SKris Kennaway /* scan buf[] for '~' before sending data to the peer */ 1164b66f2d16SKris Kennaway 1165d4af9e69SDag-Erling Smørgrav /* Helper: allocate a new escape_filter_ctx and fill in its escape char */ 1166d4af9e69SDag-Erling Smørgrav void * 1167d4af9e69SDag-Erling Smørgrav client_new_escape_filter_ctx(int escape_char) 1168b66f2d16SKris Kennaway { 1169d4af9e69SDag-Erling Smørgrav struct escape_filter_ctx *ret; 1170d4af9e69SDag-Erling Smørgrav 11710a37d4a3SXin LI ret = xcalloc(1, sizeof(*ret)); 1172d4af9e69SDag-Erling Smørgrav ret->escape_pending = 0; 1173d4af9e69SDag-Erling Smørgrav ret->escape_char = escape_char; 1174d4af9e69SDag-Erling Smørgrav return (void *)ret; 1175d4af9e69SDag-Erling Smørgrav } 1176d4af9e69SDag-Erling Smørgrav 1177d4af9e69SDag-Erling Smørgrav /* Free the escape filter context on channel free */ 1178d4af9e69SDag-Erling Smørgrav void 11794f52dfbbSDag-Erling Smørgrav client_filter_cleanup(struct ssh *ssh, int cid, void *ctx) 1180d4af9e69SDag-Erling Smørgrav { 1181e4a9863fSDag-Erling Smørgrav free(ctx); 1182d4af9e69SDag-Erling Smørgrav } 1183d4af9e69SDag-Erling Smørgrav 1184d4af9e69SDag-Erling Smørgrav int 11854f52dfbbSDag-Erling Smørgrav client_simple_escape_filter(struct ssh *ssh, Channel *c, char *buf, int len) 1186d4af9e69SDag-Erling Smørgrav { 1187d4af9e69SDag-Erling Smørgrav if (c->extended_usage != CHAN_EXTENDED_WRITE) 1188d4af9e69SDag-Erling Smørgrav return 0; 1189d4af9e69SDag-Erling Smørgrav 11904f52dfbbSDag-Erling Smørgrav return process_escapes(ssh, c, c->input, c->output, c->extended, 1191d4af9e69SDag-Erling Smørgrav buf, len); 1192b66f2d16SKris Kennaway } 1193b66f2d16SKris Kennaway 1194ae1f160dSDag-Erling Smørgrav static void 11954f52dfbbSDag-Erling Smørgrav client_channel_closed(struct ssh *ssh, int id, void *arg) 11961e8db6e2SBrian Feldman { 11974f52dfbbSDag-Erling Smørgrav channel_cancel_cleanup(ssh, id); 11981e8db6e2SBrian Feldman session_closed = 1; 1199e146993eSDag-Erling Smørgrav leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 12001e8db6e2SBrian Feldman } 12011e8db6e2SBrian Feldman 1202a04a10f8SKris Kennaway /* 1203511b41d2SMark Murray * Implements the interactive session with the server. This is called after 1204511b41d2SMark Murray * the user has been authenticated, and a command has been started on the 1205ae1f160dSDag-Erling Smørgrav * remote host. If escape_char != SSH_ESCAPECHAR_NONE, it is the character 1206ae1f160dSDag-Erling Smørgrav * used as an escape character for terminating or suspending the session. 1207511b41d2SMark Murray */ 1208511b41d2SMark Murray int 12094f52dfbbSDag-Erling Smørgrav client_loop(struct ssh *ssh, int have_pty, int escape_char_arg, 12104f52dfbbSDag-Erling Smørgrav int ssh2_chan_id) 1211511b41d2SMark Murray { 12121e8db6e2SBrian Feldman fd_set *readset = NULL, *writeset = NULL; 1213511b41d2SMark Murray double start_time, total_time; 1214acc1a9efSDag-Erling Smørgrav int r, max_fd = 0, max_fd2 = 0, len; 1215d4af9e69SDag-Erling Smørgrav u_int64_t ibytes, obytes; 1216d74d50a8SDag-Erling Smørgrav u_int nalloc = 0; 1217511b41d2SMark Murray 1218511b41d2SMark Murray debug("Entering interactive session."); 1219511b41d2SMark Murray 1220acc1a9efSDag-Erling Smørgrav if (options.control_master && 1221acc1a9efSDag-Erling Smørgrav !option_clear_or_none(options.control_path)) { 1222acc1a9efSDag-Erling Smørgrav debug("pledge: id"); 1223*19261079SEd Maste if (pledge("stdio rpath wpath cpath unix inet dns recvfd sendfd proc exec id tty", 1224acc1a9efSDag-Erling Smørgrav NULL) == -1) 1225*19261079SEd Maste fatal_f("pledge(): %s", strerror(errno)); 1226acc1a9efSDag-Erling Smørgrav 1227acc1a9efSDag-Erling Smørgrav } else if (options.forward_x11 || options.permit_local_command) { 1228acc1a9efSDag-Erling Smørgrav debug("pledge: exec"); 1229acc1a9efSDag-Erling Smørgrav if (pledge("stdio rpath wpath cpath unix inet dns proc exec tty", 1230acc1a9efSDag-Erling Smørgrav NULL) == -1) 1231*19261079SEd Maste fatal_f("pledge(): %s", strerror(errno)); 1232acc1a9efSDag-Erling Smørgrav 1233acc1a9efSDag-Erling Smørgrav } else if (options.update_hostkeys) { 1234acc1a9efSDag-Erling Smørgrav debug("pledge: filesystem full"); 1235acc1a9efSDag-Erling Smørgrav if (pledge("stdio rpath wpath cpath unix inet dns proc tty", 1236acc1a9efSDag-Erling Smørgrav NULL) == -1) 1237*19261079SEd Maste fatal_f("pledge(): %s", strerror(errno)); 1238acc1a9efSDag-Erling Smørgrav 1239076ad2f8SDag-Erling Smørgrav } else if (!option_clear_or_none(options.proxy_command) || 1240*19261079SEd Maste options.fork_after_authentication) { 1241acc1a9efSDag-Erling Smørgrav debug("pledge: proc"); 1242acc1a9efSDag-Erling Smørgrav if (pledge("stdio cpath unix inet dns proc tty", NULL) == -1) 1243*19261079SEd Maste fatal_f("pledge(): %s", strerror(errno)); 1244acc1a9efSDag-Erling Smørgrav 1245acc1a9efSDag-Erling Smørgrav } else { 1246acc1a9efSDag-Erling Smørgrav debug("pledge: network"); 12474f52dfbbSDag-Erling Smørgrav if (pledge("stdio unix inet dns proc tty", NULL) == -1) 1248*19261079SEd Maste fatal_f("pledge(): %s", strerror(errno)); 1249acc1a9efSDag-Erling Smørgrav } 1250acc1a9efSDag-Erling Smørgrav 125147dd1d1bSDag-Erling Smørgrav start_time = monotime_double(); 1252511b41d2SMark Murray 1253511b41d2SMark Murray /* Initialize variables. */ 1254511b41d2SMark Murray last_was_cr = 1; 1255511b41d2SMark Murray exit_status = -1; 1256*19261079SEd Maste connection_in = ssh_packet_get_connection_in(ssh); 1257*19261079SEd Maste connection_out = ssh_packet_get_connection_out(ssh); 1258ca86bcf2SDag-Erling Smørgrav max_fd = MAXIMUM(connection_in, connection_out); 12591e8db6e2SBrian Feldman 1260511b41d2SMark Murray quit_pending = 0; 1261511b41d2SMark Murray 1262190cef3dSDag-Erling Smørgrav /* Initialize buffer. */ 1263190cef3dSDag-Erling Smørgrav if ((stderr_buffer = sshbuf_new()) == NULL) 1264*19261079SEd Maste fatal_f("sshbuf_new failed"); 1265511b41d2SMark Murray 1266*19261079SEd Maste client_init_dispatch(ssh); 1267a04a10f8SKris Kennaway 1268d0c8c0bcSDag-Erling Smørgrav /* 1269d0c8c0bcSDag-Erling Smørgrav * Set signal handlers, (e.g. to restore non-blocking mode) 1270d0c8c0bcSDag-Erling Smørgrav * but don't overwrite SIG_IGN, matches behaviour from rsh(1) 1271d0c8c0bcSDag-Erling Smørgrav */ 1272*19261079SEd Maste if (ssh_signal(SIGHUP, SIG_IGN) != SIG_IGN) 1273*19261079SEd Maste ssh_signal(SIGHUP, signal_handler); 1274*19261079SEd Maste if (ssh_signal(SIGINT, SIG_IGN) != SIG_IGN) 1275*19261079SEd Maste ssh_signal(SIGINT, signal_handler); 1276*19261079SEd Maste if (ssh_signal(SIGQUIT, SIG_IGN) != SIG_IGN) 1277*19261079SEd Maste ssh_signal(SIGQUIT, signal_handler); 1278*19261079SEd Maste if (ssh_signal(SIGTERM, SIG_IGN) != SIG_IGN) 1279*19261079SEd Maste ssh_signal(SIGTERM, signal_handler); 1280*19261079SEd Maste ssh_signal(SIGWINCH, window_change_handler); 1281511b41d2SMark Murray 1282511b41d2SMark Murray if (have_pty) 1283e146993eSDag-Erling Smørgrav enter_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 1284511b41d2SMark Murray 12851e8db6e2SBrian Feldman session_ident = ssh2_chan_id; 1286e146993eSDag-Erling Smørgrav if (session_ident != -1) { 1287e146993eSDag-Erling Smørgrav if (escape_char_arg != SSH_ESCAPECHAR_NONE) { 12884f52dfbbSDag-Erling Smørgrav channel_register_filter(ssh, session_ident, 1289d4af9e69SDag-Erling Smørgrav client_simple_escape_filter, NULL, 1290d4af9e69SDag-Erling Smørgrav client_filter_cleanup, 1291e146993eSDag-Erling Smørgrav client_new_escape_filter_ctx( 1292e146993eSDag-Erling Smørgrav escape_char_arg)); 1293e146993eSDag-Erling Smørgrav } 12944f52dfbbSDag-Erling Smørgrav channel_register_cleanup(ssh, session_ident, 1295021d409fSDag-Erling Smørgrav client_channel_closed, 0); 1296e146993eSDag-Erling Smørgrav } 1297b66f2d16SKris Kennaway 1298*19261079SEd Maste schedule_server_alive_check(); 1299*19261079SEd Maste 1300511b41d2SMark Murray /* Main loop of the client for the interactive session mode. */ 1301511b41d2SMark Murray while (!quit_pending) { 1302511b41d2SMark Murray 1303511b41d2SMark Murray /* Process buffered packets sent by the server. */ 1304*19261079SEd Maste client_process_buffered_input_packets(ssh); 1305511b41d2SMark Murray 13064f52dfbbSDag-Erling Smørgrav if (session_closed && !channel_still_open(ssh)) 1307a04a10f8SKris Kennaway break; 1308a04a10f8SKris Kennaway 13094f52dfbbSDag-Erling Smørgrav if (ssh_packet_is_rekeying(ssh)) { 13101e8db6e2SBrian Feldman debug("rekeying in progress"); 1311acc1a9efSDag-Erling Smørgrav } else if (need_rekeying) { 1312acc1a9efSDag-Erling Smørgrav /* manual rekey request */ 1313acc1a9efSDag-Erling Smørgrav debug("need rekeying"); 13144f52dfbbSDag-Erling Smørgrav if ((r = kex_start_rekex(ssh)) != 0) 1315*19261079SEd Maste fatal_fr(r, "kex_start_rekex"); 1316acc1a9efSDag-Erling Smørgrav need_rekeying = 0; 13171e8db6e2SBrian Feldman } else { 1318511b41d2SMark Murray /* 13191e8db6e2SBrian Feldman * Make packets from buffered channel data, and 13201e8db6e2SBrian Feldman * enqueue them for sending to the server. 1321511b41d2SMark Murray */ 1322*19261079SEd Maste if (ssh_packet_not_very_much_data_to_write(ssh)) 13234f52dfbbSDag-Erling Smørgrav channel_output_poll(ssh); 1324511b41d2SMark Murray 1325511b41d2SMark Murray /* 13261e8db6e2SBrian Feldman * Check if the window size has changed, and buffer a 13271e8db6e2SBrian Feldman * message about it to the server if so. 1328511b41d2SMark Murray */ 13294f52dfbbSDag-Erling Smørgrav client_check_window_change(ssh); 1330511b41d2SMark Murray 1331511b41d2SMark Murray if (quit_pending) 1332511b41d2SMark Murray break; 13331e8db6e2SBrian Feldman } 1334511b41d2SMark Murray /* 1335511b41d2SMark Murray * Wait until we have something to do (something becomes 1336511b41d2SMark Murray * available on one of the descriptors). 1337511b41d2SMark Murray */ 1338ae1f160dSDag-Erling Smørgrav max_fd2 = max_fd; 13394f52dfbbSDag-Erling Smørgrav client_wait_until_can_do_something(ssh, &readset, &writeset, 13404f52dfbbSDag-Erling Smørgrav &max_fd2, &nalloc, ssh_packet_is_rekeying(ssh)); 1341511b41d2SMark Murray 1342511b41d2SMark Murray if (quit_pending) 1343511b41d2SMark Murray break; 1344511b41d2SMark Murray 13451e8db6e2SBrian Feldman /* Do channel operations unless rekeying in progress. */ 13464f52dfbbSDag-Erling Smørgrav if (!ssh_packet_is_rekeying(ssh)) 13474f52dfbbSDag-Erling Smørgrav channel_after_select(ssh, readset, writeset); 1348511b41d2SMark Murray 1349a04a10f8SKris Kennaway /* Buffer input from the connection. */ 1350*19261079SEd Maste client_process_net_input(ssh, readset); 1351511b41d2SMark Murray 1352a04a10f8SKris Kennaway if (quit_pending) 1353a04a10f8SKris Kennaway break; 1354a04a10f8SKris Kennaway 1355*19261079SEd Maste /* A timeout may have triggered rekeying */ 1356*19261079SEd Maste if ((r = ssh_packet_check_rekey(ssh)) != 0) 1357*19261079SEd Maste fatal_fr(r, "cannot start rekeying"); 1358*19261079SEd Maste 1359d4af9e69SDag-Erling Smørgrav /* 1360d4af9e69SDag-Erling Smørgrav * Send as much buffered packet data as possible to the 1361d4af9e69SDag-Erling Smørgrav * sender. 1362d4af9e69SDag-Erling Smørgrav */ 1363*19261079SEd Maste if (FD_ISSET(connection_out, writeset)) { 1364*19261079SEd Maste if ((r = ssh_packet_write_poll(ssh)) != 0) { 1365*19261079SEd Maste sshpkt_fatal(ssh, r, 1366*19261079SEd Maste "%s: ssh_packet_write_poll", __func__); 1367*19261079SEd Maste } 1368*19261079SEd Maste } 1369e2f6069cSDag-Erling Smørgrav 1370e2f6069cSDag-Erling Smørgrav /* 1371e2f6069cSDag-Erling Smørgrav * If we are a backgrounded control master, and the 1372e2f6069cSDag-Erling Smørgrav * timeout has expired without any active client 1373e2f6069cSDag-Erling Smørgrav * connections, then quit. 1374e2f6069cSDag-Erling Smørgrav */ 1375e2f6069cSDag-Erling Smørgrav if (control_persist_exit_time > 0) { 1376e4a9863fSDag-Erling Smørgrav if (monotime() >= control_persist_exit_time) { 1377e2f6069cSDag-Erling Smørgrav debug("ControlPersist timeout expired"); 1378e2f6069cSDag-Erling Smørgrav break; 1379e2f6069cSDag-Erling Smørgrav } 1380e2f6069cSDag-Erling Smørgrav } 1381511b41d2SMark Murray } 1382e4a9863fSDag-Erling Smørgrav free(readset); 1383e4a9863fSDag-Erling Smørgrav free(writeset); 1384511b41d2SMark Murray 1385511b41d2SMark Murray /* Terminate the session. */ 1386511b41d2SMark Murray 1387511b41d2SMark Murray /* Stop watching for window change. */ 1388*19261079SEd Maste ssh_signal(SIGWINCH, SIG_DFL); 1389511b41d2SMark Murray 1390*19261079SEd Maste if ((r = sshpkt_start(ssh, SSH2_MSG_DISCONNECT)) != 0 || 1391*19261079SEd Maste (r = sshpkt_put_u32(ssh, SSH2_DISCONNECT_BY_APPLICATION)) != 0 || 1392*19261079SEd Maste (r = sshpkt_put_cstring(ssh, "disconnected by user")) != 0 || 1393*19261079SEd Maste (r = sshpkt_put_cstring(ssh, "")) != 0 || /* language tag */ 1394*19261079SEd Maste (r = sshpkt_send(ssh)) != 0 || 1395*19261079SEd Maste (r = ssh_packet_write_wait(ssh)) != 0) 1396*19261079SEd Maste fatal_fr(r, "send disconnect"); 13977aee6ffeSDag-Erling Smørgrav 13984f52dfbbSDag-Erling Smørgrav channel_free_all(ssh); 1399ae1f160dSDag-Erling Smørgrav 1400ae1f160dSDag-Erling Smørgrav if (have_pty) 1401e146993eSDag-Erling Smørgrav leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 1402ae1f160dSDag-Erling Smørgrav 1403efcad6b7SDag-Erling Smørgrav /* 1404efcad6b7SDag-Erling Smørgrav * If there was no shell or command requested, there will be no remote 1405efcad6b7SDag-Erling Smørgrav * exit status to be returned. In that case, clear error code if the 1406efcad6b7SDag-Erling Smørgrav * connection was deliberately terminated at this end. 1407efcad6b7SDag-Erling Smørgrav */ 1408*19261079SEd Maste if (options.session_type == SESSION_TYPE_NONE && received_signal == SIGTERM) { 1409efcad6b7SDag-Erling Smørgrav received_signal = 0; 1410efcad6b7SDag-Erling Smørgrav exit_status = 0; 1411ae1f160dSDag-Erling Smørgrav } 1412511b41d2SMark Murray 14134f52dfbbSDag-Erling Smørgrav if (received_signal) { 14144f52dfbbSDag-Erling Smørgrav verbose("Killed by signal %d.", (int) received_signal); 1415*19261079SEd Maste cleanup_exit(255); 14164f52dfbbSDag-Erling Smørgrav } 1417efcad6b7SDag-Erling Smørgrav 1418511b41d2SMark Murray /* 1419511b41d2SMark Murray * In interactive mode (with pseudo tty) display a message indicating 1420511b41d2SMark Murray * that the connection has been closed. 1421511b41d2SMark Murray */ 1422511b41d2SMark Murray if (have_pty && options.log_level != SYSLOG_LEVEL_QUIET) { 1423190cef3dSDag-Erling Smørgrav if ((r = sshbuf_putf(stderr_buffer, 1424190cef3dSDag-Erling Smørgrav "Connection to %.64s closed.\r\n", host)) != 0) 1425*19261079SEd Maste fatal_fr(r, "sshbuf_putf"); 1426511b41d2SMark Murray } 1427ae1f160dSDag-Erling Smørgrav 1428511b41d2SMark Murray /* Output any buffered data for stderr. */ 1429190cef3dSDag-Erling Smørgrav if (sshbuf_len(stderr_buffer) > 0) { 14304a421b63SDag-Erling Smørgrav len = atomicio(vwrite, fileno(stderr), 1431190cef3dSDag-Erling Smørgrav (u_char *)sshbuf_ptr(stderr_buffer), 1432190cef3dSDag-Erling Smørgrav sshbuf_len(stderr_buffer)); 1433190cef3dSDag-Erling Smørgrav if (len < 0 || (u_int)len != sshbuf_len(stderr_buffer)) 1434511b41d2SMark Murray error("Write failed flushing stderr buffer."); 1435190cef3dSDag-Erling Smørgrav else if ((r = sshbuf_consume(stderr_buffer, len)) != 0) 1436*19261079SEd Maste fatal_fr(r, "sshbuf_consume"); 1437511b41d2SMark Murray } 1438511b41d2SMark Murray 1439511b41d2SMark Murray /* Clear and free any buffers. */ 1440190cef3dSDag-Erling Smørgrav sshbuf_free(stderr_buffer); 1441511b41d2SMark Murray 1442511b41d2SMark Murray /* Report bytes transferred, and transfer rates. */ 144347dd1d1bSDag-Erling Smørgrav total_time = monotime_double() - start_time; 1444*19261079SEd Maste ssh_packet_get_bytes(ssh, &ibytes, &obytes); 1445d4af9e69SDag-Erling Smørgrav verbose("Transferred: sent %llu, received %llu bytes, in %.1f seconds", 14464a421b63SDag-Erling Smørgrav (unsigned long long)obytes, (unsigned long long)ibytes, total_time); 1447511b41d2SMark Murray if (total_time > 0) 1448d4af9e69SDag-Erling Smørgrav verbose("Bytes per second: sent %.1f, received %.1f", 1449d4af9e69SDag-Erling Smørgrav obytes / total_time, ibytes / total_time); 1450511b41d2SMark Murray /* Return the exit status of the program. */ 1451511b41d2SMark Murray debug("Exit status %d", exit_status); 1452511b41d2SMark Murray return exit_status; 1453511b41d2SMark Murray } 1454a04a10f8SKris Kennaway 1455a04a10f8SKris Kennaway /*********/ 1456a04a10f8SKris Kennaway 1457ae1f160dSDag-Erling Smørgrav static Channel * 14584f52dfbbSDag-Erling Smørgrav client_request_forwarded_tcpip(struct ssh *ssh, const char *request_type, 14594f52dfbbSDag-Erling Smørgrav int rchan, u_int rwindow, u_int rmaxpack) 14601e8db6e2SBrian Feldman { 14611e8db6e2SBrian Feldman Channel *c = NULL; 1462ca86bcf2SDag-Erling Smørgrav struct sshbuf *b = NULL; 14631e8db6e2SBrian Feldman char *listen_address, *originator_address; 1464*19261079SEd Maste u_int listen_port, originator_port; 1465ca86bcf2SDag-Erling Smørgrav int r; 14661e8db6e2SBrian Feldman 14671e8db6e2SBrian Feldman /* Get rest of the packet */ 1468*19261079SEd Maste if ((r = sshpkt_get_cstring(ssh, &listen_address, NULL)) != 0 || 1469*19261079SEd Maste (r = sshpkt_get_u32(ssh, &listen_port)) != 0 || 1470*19261079SEd Maste (r = sshpkt_get_cstring(ssh, &originator_address, NULL)) != 0 || 1471*19261079SEd Maste (r = sshpkt_get_u32(ssh, &originator_port)) != 0 || 1472*19261079SEd Maste (r = sshpkt_get_end(ssh)) != 0) 1473*19261079SEd Maste fatal_fr(r, "parse packet"); 14741e8db6e2SBrian Feldman 1475*19261079SEd Maste debug_f("listen %s port %d, originator %s port %d", 1476a0ee8cc6SDag-Erling Smørgrav listen_address, listen_port, originator_address, originator_port); 14771e8db6e2SBrian Feldman 1478*19261079SEd Maste if (listen_port > 0xffff) 1479*19261079SEd Maste error_f("invalid listen port"); 1480*19261079SEd Maste else if (originator_port > 0xffff) 1481*19261079SEd Maste error_f("invalid originator port"); 1482*19261079SEd Maste else { 1483*19261079SEd Maste c = channel_connect_by_listen_address(ssh, 1484*19261079SEd Maste listen_address, listen_port, "forwarded-tcpip", 1485*19261079SEd Maste originator_address); 1486*19261079SEd Maste } 1487d4af9e69SDag-Erling Smørgrav 1488ca86bcf2SDag-Erling Smørgrav if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) { 1489ca86bcf2SDag-Erling Smørgrav if ((b = sshbuf_new()) == NULL) { 1490*19261079SEd Maste error_f("alloc reply"); 1491ca86bcf2SDag-Erling Smørgrav goto out; 1492ca86bcf2SDag-Erling Smørgrav } 1493ca86bcf2SDag-Erling Smørgrav /* reconstruct and send to muxclient */ 1494ca86bcf2SDag-Erling Smørgrav if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */ 1495ca86bcf2SDag-Erling Smørgrav (r = sshbuf_put_u8(b, SSH2_MSG_CHANNEL_OPEN)) != 0 || 1496ca86bcf2SDag-Erling Smørgrav (r = sshbuf_put_cstring(b, request_type)) != 0 || 1497ca86bcf2SDag-Erling Smørgrav (r = sshbuf_put_u32(b, rchan)) != 0 || 1498ca86bcf2SDag-Erling Smørgrav (r = sshbuf_put_u32(b, rwindow)) != 0 || 1499ca86bcf2SDag-Erling Smørgrav (r = sshbuf_put_u32(b, rmaxpack)) != 0 || 1500ca86bcf2SDag-Erling Smørgrav (r = sshbuf_put_cstring(b, listen_address)) != 0 || 1501ca86bcf2SDag-Erling Smørgrav (r = sshbuf_put_u32(b, listen_port)) != 0 || 1502ca86bcf2SDag-Erling Smørgrav (r = sshbuf_put_cstring(b, originator_address)) != 0 || 1503ca86bcf2SDag-Erling Smørgrav (r = sshbuf_put_u32(b, originator_port)) != 0 || 15044f52dfbbSDag-Erling Smørgrav (r = sshbuf_put_stringb(c->output, b)) != 0) { 1505*19261079SEd Maste error_fr(r, "compose for muxclient"); 1506ca86bcf2SDag-Erling Smørgrav goto out; 1507ca86bcf2SDag-Erling Smørgrav } 1508ca86bcf2SDag-Erling Smørgrav } 1509ca86bcf2SDag-Erling Smørgrav 1510ca86bcf2SDag-Erling Smørgrav out: 1511ca86bcf2SDag-Erling Smørgrav sshbuf_free(b); 1512e4a9863fSDag-Erling Smørgrav free(originator_address); 1513e4a9863fSDag-Erling Smørgrav free(listen_address); 15141e8db6e2SBrian Feldman return c; 15151e8db6e2SBrian Feldman } 15161e8db6e2SBrian Feldman 1517ae1f160dSDag-Erling Smørgrav static Channel * 15184f52dfbbSDag-Erling Smørgrav client_request_forwarded_streamlocal(struct ssh *ssh, 15194f52dfbbSDag-Erling Smørgrav const char *request_type, int rchan) 1520a0ee8cc6SDag-Erling Smørgrav { 1521a0ee8cc6SDag-Erling Smørgrav Channel *c = NULL; 1522a0ee8cc6SDag-Erling Smørgrav char *listen_path; 1523*19261079SEd Maste int r; 1524a0ee8cc6SDag-Erling Smørgrav 1525a0ee8cc6SDag-Erling Smørgrav /* Get the remote path. */ 1526*19261079SEd Maste if ((r = sshpkt_get_cstring(ssh, &listen_path, NULL)) != 0 || 1527*19261079SEd Maste (r = sshpkt_get_string(ssh, NULL, NULL)) != 0 || /* reserved */ 1528*19261079SEd Maste (r = sshpkt_get_end(ssh)) != 0) 1529*19261079SEd Maste fatal_fr(r, "parse packet"); 1530a0ee8cc6SDag-Erling Smørgrav 1531*19261079SEd Maste debug_f("request: %s", listen_path); 1532a0ee8cc6SDag-Erling Smørgrav 15334f52dfbbSDag-Erling Smørgrav c = channel_connect_by_listen_path(ssh, listen_path, 1534a0ee8cc6SDag-Erling Smørgrav "forwarded-streamlocal@openssh.com", "forwarded-streamlocal"); 1535a0ee8cc6SDag-Erling Smørgrav free(listen_path); 1536a0ee8cc6SDag-Erling Smørgrav return c; 1537a0ee8cc6SDag-Erling Smørgrav } 1538a0ee8cc6SDag-Erling Smørgrav 1539a0ee8cc6SDag-Erling Smørgrav static Channel * 15404f52dfbbSDag-Erling Smørgrav client_request_x11(struct ssh *ssh, const char *request_type, int rchan) 15411e8db6e2SBrian Feldman { 15421e8db6e2SBrian Feldman Channel *c = NULL; 15431e8db6e2SBrian Feldman char *originator; 1544*19261079SEd Maste u_int originator_port; 1545*19261079SEd Maste int r, sock; 15461e8db6e2SBrian Feldman 15471e8db6e2SBrian Feldman if (!options.forward_x11) { 15481e8db6e2SBrian Feldman error("Warning: ssh server tried X11 forwarding."); 1549d4af9e69SDag-Erling Smørgrav error("Warning: this is probably a break-in attempt by a " 1550d4af9e69SDag-Erling Smørgrav "malicious server."); 15511e8db6e2SBrian Feldman return NULL; 15521e8db6e2SBrian Feldman } 1553557f75e5SDag-Erling Smørgrav if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) { 1554e2f6069cSDag-Erling Smørgrav verbose("Rejected X11 connection after ForwardX11Timeout " 1555e2f6069cSDag-Erling Smørgrav "expired"); 1556e2f6069cSDag-Erling Smørgrav return NULL; 1557e2f6069cSDag-Erling Smørgrav } 1558*19261079SEd Maste if ((r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 || 1559*19261079SEd Maste (r = sshpkt_get_u32(ssh, &originator_port)) != 0 || 1560*19261079SEd Maste (r = sshpkt_get_end(ssh)) != 0) 1561*19261079SEd Maste fatal_fr(r, "parse packet"); 15621e8db6e2SBrian Feldman /* XXX check permission */ 1563*19261079SEd Maste /* XXX range check originator port? */ 1564*19261079SEd Maste debug("client_request_x11: request from %s %u", originator, 15651e8db6e2SBrian Feldman originator_port); 1566e4a9863fSDag-Erling Smørgrav free(originator); 15674f52dfbbSDag-Erling Smørgrav sock = x11_connect_display(ssh); 1568ae1f160dSDag-Erling Smørgrav if (sock < 0) 1569ae1f160dSDag-Erling Smørgrav return NULL; 15704f52dfbbSDag-Erling Smørgrav c = channel_new(ssh, "x11", 157160c59fadSDag-Erling Smørgrav SSH_CHANNEL_X11_OPEN, sock, sock, -1, 157260c59fadSDag-Erling Smørgrav CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1); 1573ae1f160dSDag-Erling Smørgrav c->force_drain = 1; 15741e8db6e2SBrian Feldman return c; 15751e8db6e2SBrian Feldman } 15761e8db6e2SBrian Feldman 1577ae1f160dSDag-Erling Smørgrav static Channel * 15784f52dfbbSDag-Erling Smørgrav client_request_agent(struct ssh *ssh, const char *request_type, int rchan) 15791e8db6e2SBrian Feldman { 15801e8db6e2SBrian Feldman Channel *c = NULL; 1581bc5531deSDag-Erling Smørgrav int r, sock; 15821e8db6e2SBrian Feldman 15831e8db6e2SBrian Feldman if (!options.forward_agent) { 15841e8db6e2SBrian Feldman error("Warning: ssh server tried agent forwarding."); 1585d4af9e69SDag-Erling Smørgrav error("Warning: this is probably a break-in attempt by a " 1586d4af9e69SDag-Erling Smørgrav "malicious server."); 15871e8db6e2SBrian Feldman return NULL; 15881e8db6e2SBrian Feldman } 1589*19261079SEd Maste if (forward_agent_sock_path == NULL) { 1590*19261079SEd Maste r = ssh_get_authentication_socket(&sock); 1591*19261079SEd Maste } else { 1592*19261079SEd Maste r = ssh_get_authentication_socket_path(forward_agent_sock_path, &sock); 1593*19261079SEd Maste } 1594*19261079SEd Maste if (r != 0) { 1595bc5531deSDag-Erling Smørgrav if (r != SSH_ERR_AGENT_NOT_PRESENT) 1596*19261079SEd Maste debug_fr(r, "ssh_get_authentication_socket"); 1597ae1f160dSDag-Erling Smørgrav return NULL; 1598bc5531deSDag-Erling Smørgrav } 15994f52dfbbSDag-Erling Smørgrav c = channel_new(ssh, "authentication agent connection", 16001e8db6e2SBrian Feldman SSH_CHANNEL_OPEN, sock, sock, -1, 1601e3bd730fSBryan Drewery CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, 160289986192SBrooks Davis "authentication agent connection", 1); 1603ae1f160dSDag-Erling Smørgrav c->force_drain = 1; 16041e8db6e2SBrian Feldman return c; 16051e8db6e2SBrian Feldman } 16061e8db6e2SBrian Feldman 160747dd1d1bSDag-Erling Smørgrav char * 16084f52dfbbSDag-Erling Smørgrav client_request_tun_fwd(struct ssh *ssh, int tun_mode, 1609*19261079SEd Maste int local_tun, int remote_tun, channel_open_fn *cb, void *cbctx) 1610d4af9e69SDag-Erling Smørgrav { 1611d4af9e69SDag-Erling Smørgrav Channel *c; 1612*19261079SEd Maste int r, fd; 161347dd1d1bSDag-Erling Smørgrav char *ifname = NULL; 1614d4af9e69SDag-Erling Smørgrav 1615d4af9e69SDag-Erling Smørgrav if (tun_mode == SSH_TUNMODE_NO) 1616d4af9e69SDag-Erling Smørgrav return 0; 1617d4af9e69SDag-Erling Smørgrav 1618d4af9e69SDag-Erling Smørgrav debug("Requesting tun unit %d in mode %d", local_tun, tun_mode); 1619d4af9e69SDag-Erling Smørgrav 1620d4af9e69SDag-Erling Smørgrav /* Open local tunnel device */ 162147dd1d1bSDag-Erling Smørgrav if ((fd = tun_open(local_tun, tun_mode, &ifname)) == -1) { 1622d4af9e69SDag-Erling Smørgrav error("Tunnel device open failed."); 162347dd1d1bSDag-Erling Smørgrav return NULL; 1624d4af9e69SDag-Erling Smørgrav } 162547dd1d1bSDag-Erling Smørgrav debug("Tunnel forwarding using interface %s", ifname); 1626d4af9e69SDag-Erling Smørgrav 16274f52dfbbSDag-Erling Smørgrav c = channel_new(ssh, "tun", SSH_CHANNEL_OPENING, fd, fd, -1, 162860c59fadSDag-Erling Smørgrav CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); 1629d4af9e69SDag-Erling Smørgrav c->datagram = 1; 1630d4af9e69SDag-Erling Smørgrav 1631d4af9e69SDag-Erling Smørgrav #if defined(SSH_TUN_FILTER) 1632d4af9e69SDag-Erling Smørgrav if (options.tun_open == SSH_TUNMODE_POINTOPOINT) 16334f52dfbbSDag-Erling Smørgrav channel_register_filter(ssh, c->self, sys_tun_infilter, 1634d4af9e69SDag-Erling Smørgrav sys_tun_outfilter, NULL, NULL); 1635d4af9e69SDag-Erling Smørgrav #endif 1636d4af9e69SDag-Erling Smørgrav 1637*19261079SEd Maste if (cb != NULL) 1638*19261079SEd Maste channel_register_open_confirm(ssh, c->self, cb, cbctx); 1639*19261079SEd Maste 1640*19261079SEd Maste if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN)) != 0 || 1641*19261079SEd Maste (r = sshpkt_put_cstring(ssh, "tun@openssh.com")) != 0 || 1642*19261079SEd Maste (r = sshpkt_put_u32(ssh, c->self)) != 0 || 1643*19261079SEd Maste (r = sshpkt_put_u32(ssh, c->local_window_max)) != 0 || 1644*19261079SEd Maste (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 || 1645*19261079SEd Maste (r = sshpkt_put_u32(ssh, tun_mode)) != 0 || 1646*19261079SEd Maste (r = sshpkt_put_u32(ssh, remote_tun)) != 0 || 1647*19261079SEd Maste (r = sshpkt_send(ssh)) != 0) 1648*19261079SEd Maste sshpkt_fatal(ssh, r, "%s: send reply", __func__); 1649d4af9e69SDag-Erling Smørgrav 165047dd1d1bSDag-Erling Smørgrav return ifname; 1651d4af9e69SDag-Erling Smørgrav } 1652d4af9e69SDag-Erling Smørgrav 1653a04a10f8SKris Kennaway /* XXXX move to generic input handler */ 1654bc5531deSDag-Erling Smørgrav static int 16554f52dfbbSDag-Erling Smørgrav client_input_channel_open(int type, u_int32_t seq, struct ssh *ssh) 1656a04a10f8SKris Kennaway { 1657a04a10f8SKris Kennaway Channel *c = NULL; 1658*19261079SEd Maste char *ctype = NULL; 1659*19261079SEd Maste int r; 1660*19261079SEd Maste u_int rchan; 1661*19261079SEd Maste size_t len; 1662*19261079SEd Maste u_int rmaxpack, rwindow; 1663a04a10f8SKris Kennaway 1664*19261079SEd Maste if ((r = sshpkt_get_cstring(ssh, &ctype, &len)) != 0 || 1665*19261079SEd Maste (r = sshpkt_get_u32(ssh, &rchan)) != 0 || 1666*19261079SEd Maste (r = sshpkt_get_u32(ssh, &rwindow)) != 0 || 1667*19261079SEd Maste (r = sshpkt_get_u32(ssh, &rmaxpack)) != 0) 1668*19261079SEd Maste goto out; 1669a04a10f8SKris Kennaway 1670a04a10f8SKris Kennaway debug("client_input_channel_open: ctype %s rchan %d win %d max %d", 1671a04a10f8SKris Kennaway ctype, rchan, rwindow, rmaxpack); 1672a04a10f8SKris Kennaway 16731e8db6e2SBrian Feldman if (strcmp(ctype, "forwarded-tcpip") == 0) { 16744f52dfbbSDag-Erling Smørgrav c = client_request_forwarded_tcpip(ssh, ctype, rchan, rwindow, 1675ca86bcf2SDag-Erling Smørgrav rmaxpack); 1676a0ee8cc6SDag-Erling Smørgrav } else if (strcmp(ctype, "forwarded-streamlocal@openssh.com") == 0) { 16774f52dfbbSDag-Erling Smørgrav c = client_request_forwarded_streamlocal(ssh, ctype, rchan); 16781e8db6e2SBrian Feldman } else if (strcmp(ctype, "x11") == 0) { 16794f52dfbbSDag-Erling Smørgrav c = client_request_x11(ssh, ctype, rchan); 16801e8db6e2SBrian Feldman } else if (strcmp(ctype, "auth-agent@openssh.com") == 0) { 16814f52dfbbSDag-Erling Smørgrav c = client_request_agent(ssh, ctype, rchan); 1682a04a10f8SKris Kennaway } 1683ca86bcf2SDag-Erling Smørgrav if (c != NULL && c->type == SSH_CHANNEL_MUX_CLIENT) { 1684ca86bcf2SDag-Erling Smørgrav debug3("proxied to downstream: %s", ctype); 1685ca86bcf2SDag-Erling Smørgrav } else if (c != NULL) { 1686a04a10f8SKris Kennaway debug("confirm %s", ctype); 1687a04a10f8SKris Kennaway c->remote_id = rchan; 16884f52dfbbSDag-Erling Smørgrav c->have_remote_id = 1; 1689a04a10f8SKris Kennaway c->remote_window = rwindow; 1690a04a10f8SKris Kennaway c->remote_maxpacket = rmaxpack; 1691ae1f160dSDag-Erling Smørgrav if (c->type != SSH_CHANNEL_CONNECTING) { 1692*19261079SEd Maste if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 || 1693*19261079SEd Maste (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || 1694*19261079SEd Maste (r = sshpkt_put_u32(ssh, c->self)) != 0 || 1695*19261079SEd Maste (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || 1696*19261079SEd Maste (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 || 1697*19261079SEd Maste (r = sshpkt_send(ssh)) != 0) 1698*19261079SEd Maste sshpkt_fatal(ssh, r, "%s: send reply", __func__); 1699ae1f160dSDag-Erling Smørgrav } 1700a04a10f8SKris Kennaway } else { 1701a04a10f8SKris Kennaway debug("failure %s", ctype); 1702*19261079SEd Maste if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 || 1703*19261079SEd Maste (r = sshpkt_put_u32(ssh, rchan)) != 0 || 1704*19261079SEd Maste (r = sshpkt_put_u32(ssh, SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED)) != 0 || 1705*19261079SEd Maste (r = sshpkt_put_cstring(ssh, "open failed")) != 0 || 1706*19261079SEd Maste (r = sshpkt_put_cstring(ssh, "")) != 0 || 1707*19261079SEd Maste (r = sshpkt_send(ssh)) != 0) 1708*19261079SEd Maste sshpkt_fatal(ssh, r, "%s: send failure", __func__); 1709a04a10f8SKris Kennaway } 1710*19261079SEd Maste r = 0; 1711*19261079SEd Maste out: 1712e4a9863fSDag-Erling Smørgrav free(ctype); 1713*19261079SEd Maste return r; 1714a04a10f8SKris Kennaway } 1715bc5531deSDag-Erling Smørgrav 1716bc5531deSDag-Erling Smørgrav static int 17174f52dfbbSDag-Erling Smørgrav client_input_channel_req(int type, u_int32_t seq, struct ssh *ssh) 17181e8db6e2SBrian Feldman { 17191e8db6e2SBrian Feldman Channel *c = NULL; 1720*19261079SEd Maste char *rtype = NULL; 1721*19261079SEd Maste u_char reply; 1722*19261079SEd Maste u_int id, exitval; 1723*19261079SEd Maste int r, success = 0; 17241e8db6e2SBrian Feldman 1725*19261079SEd Maste if ((r = sshpkt_get_u32(ssh, &id)) != 0) 1726*19261079SEd Maste return r; 1727*19261079SEd Maste if (id <= INT_MAX) 17284f52dfbbSDag-Erling Smørgrav c = channel_lookup(ssh, id); 17294f52dfbbSDag-Erling Smørgrav if (channel_proxy_upstream(c, type, seq, ssh)) 1730ca86bcf2SDag-Erling Smørgrav return 0; 1731*19261079SEd Maste if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 || 1732*19261079SEd Maste (r = sshpkt_get_u8(ssh, &reply)) != 0) 1733*19261079SEd Maste goto out; 17341e8db6e2SBrian Feldman 1735*19261079SEd Maste debug("client_input_channel_req: channel %u rtype %s reply %d", 17361e8db6e2SBrian Feldman id, rtype, reply); 17371e8db6e2SBrian Feldman 1738*19261079SEd Maste if (c == NULL) { 1739d4af9e69SDag-Erling Smørgrav error("client_input_channel_req: channel %d: " 1740d4af9e69SDag-Erling Smørgrav "unknown channel", id); 1741d4af9e69SDag-Erling Smørgrav } else if (strcmp(rtype, "eow@openssh.com") == 0) { 1742*19261079SEd Maste if ((r = sshpkt_get_end(ssh)) != 0) 1743*19261079SEd Maste goto out; 17444f52dfbbSDag-Erling Smørgrav chan_rcvd_eow(ssh, c); 17451e8db6e2SBrian Feldman } else if (strcmp(rtype, "exit-status") == 0) { 1746*19261079SEd Maste if ((r = sshpkt_get_u32(ssh, &exitval)) != 0) 1747*19261079SEd Maste goto out; 1748b15c8340SDag-Erling Smørgrav if (c->ctl_chan != -1) { 17494f52dfbbSDag-Erling Smørgrav mux_exit_message(ssh, c, exitval); 1750b15c8340SDag-Erling Smørgrav success = 1; 1751*19261079SEd Maste } else if ((int)id == session_ident) { 1752b15c8340SDag-Erling Smørgrav /* Record exit value of local session */ 17531e8db6e2SBrian Feldman success = 1; 1754d74d50a8SDag-Erling Smørgrav exit_status = exitval; 1755d74d50a8SDag-Erling Smørgrav } else { 1756b15c8340SDag-Erling Smørgrav /* Probably for a mux channel that has already closed */ 1757*19261079SEd Maste debug_f("no sink for exit-status on channel %d", 1758*19261079SEd Maste id); 1759d74d50a8SDag-Erling Smørgrav } 1760*19261079SEd Maste if ((r = sshpkt_get_end(ssh)) != 0) 1761*19261079SEd Maste goto out; 17621e8db6e2SBrian Feldman } 1763a0ee8cc6SDag-Erling Smørgrav if (reply && c != NULL && !(c->flags & CHAN_CLOSE_SENT)) { 17644f52dfbbSDag-Erling Smørgrav if (!c->have_remote_id) 1765*19261079SEd Maste fatal_f("channel %d: no remote_id", c->self); 1766*19261079SEd Maste if ((r = sshpkt_start(ssh, success ? 1767*19261079SEd Maste SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE)) != 0 || 1768*19261079SEd Maste (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || 1769*19261079SEd Maste (r = sshpkt_send(ssh)) != 0) 1770*19261079SEd Maste sshpkt_fatal(ssh, r, "%s: send failure", __func__); 17711e8db6e2SBrian Feldman } 1772*19261079SEd Maste r = 0; 1773*19261079SEd Maste out: 1774e4a9863fSDag-Erling Smørgrav free(rtype); 1775*19261079SEd Maste return r; 17761e8db6e2SBrian Feldman } 1777bc5531deSDag-Erling Smørgrav 1778bc5531deSDag-Erling Smørgrav struct hostkeys_update_ctx { 1779bc5531deSDag-Erling Smørgrav /* The hostname and (optionally) IP address string for the server */ 1780bc5531deSDag-Erling Smørgrav char *host_str, *ip_str; 1781bc5531deSDag-Erling Smørgrav 1782bc5531deSDag-Erling Smørgrav /* 1783bc5531deSDag-Erling Smørgrav * Keys received from the server and a flag for each indicating 1784bc5531deSDag-Erling Smørgrav * whether they already exist in known_hosts. 1785*19261079SEd Maste * keys_match is filled in by hostkeys_find() and later (for new 1786bc5531deSDag-Erling Smørgrav * keys) by client_global_hostkeys_private_confirm(). 1787bc5531deSDag-Erling Smørgrav */ 1788bc5531deSDag-Erling Smørgrav struct sshkey **keys; 1789*19261079SEd Maste u_int *keys_match; /* mask of HKF_MATCH_* from hostfile.h */ 1790*19261079SEd Maste int *keys_verified; /* flag for new keys verified by server */ 1791*19261079SEd Maste size_t nkeys, nnew, nincomplete; /* total, new keys, incomplete match */ 1792bc5531deSDag-Erling Smørgrav 1793bc5531deSDag-Erling Smørgrav /* 1794bc5531deSDag-Erling Smørgrav * Keys that are in known_hosts, but were not present in the update 1795bc5531deSDag-Erling Smørgrav * from the server (i.e. scheduled to be deleted). 1796bc5531deSDag-Erling Smørgrav * Filled in by hostkeys_find(). 1797bc5531deSDag-Erling Smørgrav */ 1798bc5531deSDag-Erling Smørgrav struct sshkey **old_keys; 1799bc5531deSDag-Erling Smørgrav size_t nold; 1800*19261079SEd Maste 1801*19261079SEd Maste /* Various special cases. */ 1802*19261079SEd Maste int complex_hostspec; /* wildcard or manual pattern-list host name */ 1803*19261079SEd Maste int ca_available; /* saw CA key for this host */ 1804*19261079SEd Maste int old_key_seen; /* saw old key with other name/addr */ 1805*19261079SEd Maste int other_name_seen; /* saw key with other name/addr */ 1806bc5531deSDag-Erling Smørgrav }; 1807bc5531deSDag-Erling Smørgrav 1808ae1f160dSDag-Erling Smørgrav static void 1809bc5531deSDag-Erling Smørgrav hostkeys_update_ctx_free(struct hostkeys_update_ctx *ctx) 1810bc5531deSDag-Erling Smørgrav { 1811bc5531deSDag-Erling Smørgrav size_t i; 1812bc5531deSDag-Erling Smørgrav 1813bc5531deSDag-Erling Smørgrav if (ctx == NULL) 1814bc5531deSDag-Erling Smørgrav return; 1815bc5531deSDag-Erling Smørgrav for (i = 0; i < ctx->nkeys; i++) 1816bc5531deSDag-Erling Smørgrav sshkey_free(ctx->keys[i]); 1817bc5531deSDag-Erling Smørgrav free(ctx->keys); 1818*19261079SEd Maste free(ctx->keys_match); 1819*19261079SEd Maste free(ctx->keys_verified); 1820bc5531deSDag-Erling Smørgrav for (i = 0; i < ctx->nold; i++) 1821bc5531deSDag-Erling Smørgrav sshkey_free(ctx->old_keys[i]); 1822bc5531deSDag-Erling Smørgrav free(ctx->old_keys); 1823bc5531deSDag-Erling Smørgrav free(ctx->host_str); 1824bc5531deSDag-Erling Smørgrav free(ctx->ip_str); 1825bc5531deSDag-Erling Smørgrav free(ctx); 1826bc5531deSDag-Erling Smørgrav } 1827bc5531deSDag-Erling Smørgrav 1828*19261079SEd Maste /* 1829*19261079SEd Maste * Returns non-zero if a known_hosts hostname list is not of a form that 1830*19261079SEd Maste * can be handled by UpdateHostkeys. These include wildcard hostnames and 1831*19261079SEd Maste * hostnames lists that do not follow the form host[,ip]. 1832*19261079SEd Maste */ 1833*19261079SEd Maste static int 1834*19261079SEd Maste hostspec_is_complex(const char *hosts) 1835*19261079SEd Maste { 1836*19261079SEd Maste char *cp; 1837*19261079SEd Maste 1838*19261079SEd Maste /* wildcard */ 1839*19261079SEd Maste if (strchr(hosts, '*') != NULL || strchr(hosts, '?') != NULL) 1840*19261079SEd Maste return 1; 1841*19261079SEd Maste /* single host/ip = ok */ 1842*19261079SEd Maste if ((cp = strchr(hosts, ',')) == NULL) 1843*19261079SEd Maste return 0; 1844*19261079SEd Maste /* more than two entries on the line */ 1845*19261079SEd Maste if (strchr(cp + 1, ',') != NULL) 1846*19261079SEd Maste return 1; 1847*19261079SEd Maste /* XXX maybe parse cp+1 and ensure it is an IP? */ 1848*19261079SEd Maste return 0; 1849*19261079SEd Maste } 1850*19261079SEd Maste 1851*19261079SEd Maste /* callback to search for ctx->keys in known_hosts */ 1852bc5531deSDag-Erling Smørgrav static int 1853bc5531deSDag-Erling Smørgrav hostkeys_find(struct hostkey_foreach_line *l, void *_ctx) 1854bc5531deSDag-Erling Smørgrav { 1855bc5531deSDag-Erling Smørgrav struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx; 1856bc5531deSDag-Erling Smørgrav size_t i; 1857bc5531deSDag-Erling Smørgrav struct sshkey **tmp; 1858bc5531deSDag-Erling Smørgrav 1859*19261079SEd Maste if (l->key == NULL) 1860bc5531deSDag-Erling Smørgrav return 0; 1861*19261079SEd Maste if (l->status != HKF_STATUS_MATCHED) { 1862*19261079SEd Maste /* Record if one of the keys appears on a non-matching line */ 1863*19261079SEd Maste for (i = 0; i < ctx->nkeys; i++) { 1864*19261079SEd Maste if (sshkey_equal(l->key, ctx->keys[i])) { 1865*19261079SEd Maste ctx->other_name_seen = 1; 1866*19261079SEd Maste debug3_f("found %s key under different " 1867*19261079SEd Maste "name/addr at %s:%ld", 1868*19261079SEd Maste sshkey_ssh_name(ctx->keys[i]), 1869*19261079SEd Maste l->path, l->linenum); 1870*19261079SEd Maste return 0; 1871*19261079SEd Maste } 1872*19261079SEd Maste } 1873*19261079SEd Maste return 0; 1874*19261079SEd Maste } 1875*19261079SEd Maste /* Don't proceed if revocation or CA markers are present */ 1876*19261079SEd Maste /* XXX relax this */ 1877*19261079SEd Maste if (l->marker != MRK_NONE) { 1878*19261079SEd Maste debug3_f("hostkeys file %s:%ld has CA/revocation marker", 1879*19261079SEd Maste l->path, l->linenum); 1880*19261079SEd Maste ctx->complex_hostspec = 1; 1881*19261079SEd Maste return 0; 1882*19261079SEd Maste } 1883*19261079SEd Maste 1884*19261079SEd Maste /* If CheckHostIP is enabled, then check for mismatched hostname/addr */ 1885*19261079SEd Maste if (ctx->ip_str != NULL && strchr(l->hosts, ',') != NULL) { 1886*19261079SEd Maste if ((l->match & HKF_MATCH_HOST) == 0) { 1887*19261079SEd Maste /* Record if address matched a different hostname. */ 1888*19261079SEd Maste ctx->other_name_seen = 1; 1889*19261079SEd Maste debug3_f("found address %s against different hostname " 1890*19261079SEd Maste "at %s:%ld", ctx->ip_str, l->path, l->linenum); 1891*19261079SEd Maste return 0; 1892*19261079SEd Maste } else if ((l->match & HKF_MATCH_IP) == 0) { 1893*19261079SEd Maste /* Record if hostname matched a different address. */ 1894*19261079SEd Maste ctx->other_name_seen = 1; 1895*19261079SEd Maste debug3_f("found hostname %s against different address " 1896*19261079SEd Maste "at %s:%ld", ctx->host_str, l->path, l->linenum); 1897*19261079SEd Maste } 1898*19261079SEd Maste } 1899*19261079SEd Maste 1900*19261079SEd Maste /* 1901*19261079SEd Maste * UpdateHostkeys is skipped for wildcard host names and hostnames 1902*19261079SEd Maste * that contain more than two entries (ssh never writes these). 1903*19261079SEd Maste */ 1904*19261079SEd Maste if (hostspec_is_complex(l->hosts)) { 1905*19261079SEd Maste debug3_f("hostkeys file %s:%ld complex host specification", 1906*19261079SEd Maste l->path, l->linenum); 1907*19261079SEd Maste ctx->complex_hostspec = 1; 1908*19261079SEd Maste return 0; 1909*19261079SEd Maste } 1910bc5531deSDag-Erling Smørgrav 1911bc5531deSDag-Erling Smørgrav /* Mark off keys we've already seen for this host */ 1912bc5531deSDag-Erling Smørgrav for (i = 0; i < ctx->nkeys; i++) { 1913*19261079SEd Maste if (!sshkey_equal(l->key, ctx->keys[i])) 1914*19261079SEd Maste continue; 1915*19261079SEd Maste debug3_f("found %s key at %s:%ld", 1916bc5531deSDag-Erling Smørgrav sshkey_ssh_name(ctx->keys[i]), l->path, l->linenum); 1917*19261079SEd Maste ctx->keys_match[i] |= l->match; 1918bc5531deSDag-Erling Smørgrav return 0; 1919bc5531deSDag-Erling Smørgrav } 1920bc5531deSDag-Erling Smørgrav /* This line contained a key that not offered by the server */ 1921*19261079SEd Maste debug3_f("deprecated %s key at %s:%ld", sshkey_ssh_name(l->key), 1922*19261079SEd Maste l->path, l->linenum); 19234f52dfbbSDag-Erling Smørgrav if ((tmp = recallocarray(ctx->old_keys, ctx->nold, ctx->nold + 1, 1924bc5531deSDag-Erling Smørgrav sizeof(*ctx->old_keys))) == NULL) 1925*19261079SEd Maste fatal_f("recallocarray failed nold = %zu", ctx->nold); 1926bc5531deSDag-Erling Smørgrav ctx->old_keys = tmp; 1927bc5531deSDag-Erling Smørgrav ctx->old_keys[ctx->nold++] = l->key; 1928bc5531deSDag-Erling Smørgrav l->key = NULL; 1929bc5531deSDag-Erling Smørgrav 1930bc5531deSDag-Erling Smørgrav return 0; 1931bc5531deSDag-Erling Smørgrav } 1932bc5531deSDag-Erling Smørgrav 1933*19261079SEd Maste /* callback to search for ctx->old_keys in known_hosts under other names */ 1934*19261079SEd Maste static int 1935*19261079SEd Maste hostkeys_check_old(struct hostkey_foreach_line *l, void *_ctx) 1936*19261079SEd Maste { 1937*19261079SEd Maste struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx; 1938*19261079SEd Maste size_t i; 1939*19261079SEd Maste int hashed; 1940*19261079SEd Maste 1941*19261079SEd Maste /* only care about lines that *don't* match the active host spec */ 1942*19261079SEd Maste if (l->status == HKF_STATUS_MATCHED || l->key == NULL) 1943*19261079SEd Maste return 0; 1944*19261079SEd Maste 1945*19261079SEd Maste hashed = l->match & (HKF_MATCH_HOST_HASHED|HKF_MATCH_IP_HASHED); 1946*19261079SEd Maste for (i = 0; i < ctx->nold; i++) { 1947*19261079SEd Maste if (!sshkey_equal(l->key, ctx->old_keys[i])) 1948*19261079SEd Maste continue; 1949*19261079SEd Maste debug3_f("found deprecated %s key at %s:%ld as %s", 1950*19261079SEd Maste sshkey_ssh_name(ctx->old_keys[i]), l->path, l->linenum, 1951*19261079SEd Maste hashed ? "[HASHED]" : l->hosts); 1952*19261079SEd Maste ctx->old_key_seen = 1; 1953*19261079SEd Maste break; 1954*19261079SEd Maste } 1955*19261079SEd Maste return 0; 1956*19261079SEd Maste } 1957*19261079SEd Maste 1958*19261079SEd Maste /* 1959*19261079SEd Maste * Check known_hosts files for deprecated keys under other names. Returns 0 1960*19261079SEd Maste * on success or -1 on failure. Updates ctx->old_key_seen if deprecated keys 1961*19261079SEd Maste * exist under names other than the active hostname/IP. 1962*19261079SEd Maste */ 1963*19261079SEd Maste static int 1964*19261079SEd Maste check_old_keys_othernames(struct hostkeys_update_ctx *ctx) 1965*19261079SEd Maste { 1966*19261079SEd Maste size_t i; 1967*19261079SEd Maste int r; 1968*19261079SEd Maste 1969*19261079SEd Maste debug2_f("checking for %zu deprecated keys", ctx->nold); 1970*19261079SEd Maste for (i = 0; i < options.num_user_hostfiles; i++) { 1971*19261079SEd Maste debug3_f("searching %s for %s / %s", 1972*19261079SEd Maste options.user_hostfiles[i], ctx->host_str, 1973*19261079SEd Maste ctx->ip_str ? ctx->ip_str : "(none)"); 1974*19261079SEd Maste if ((r = hostkeys_foreach(options.user_hostfiles[i], 1975*19261079SEd Maste hostkeys_check_old, ctx, ctx->host_str, ctx->ip_str, 1976*19261079SEd Maste HKF_WANT_PARSE_KEY, 0)) != 0) { 1977*19261079SEd Maste if (r == SSH_ERR_SYSTEM_ERROR && errno == ENOENT) { 1978*19261079SEd Maste debug_f("hostkeys file %s does not exist", 1979*19261079SEd Maste options.user_hostfiles[i]); 1980*19261079SEd Maste continue; 1981*19261079SEd Maste } 1982*19261079SEd Maste error_fr(r, "hostkeys_foreach failed for %s", 1983*19261079SEd Maste options.user_hostfiles[i]); 1984*19261079SEd Maste return -1; 1985*19261079SEd Maste } 1986*19261079SEd Maste } 1987*19261079SEd Maste return 0; 1988*19261079SEd Maste } 1989*19261079SEd Maste 1990*19261079SEd Maste static void 1991*19261079SEd Maste hostkey_change_preamble(LogLevel loglevel) 1992*19261079SEd Maste { 1993*19261079SEd Maste do_log2(loglevel, "The server has updated its host keys."); 1994*19261079SEd Maste do_log2(loglevel, "These changes were verified by the server's " 1995*19261079SEd Maste "existing trusted key."); 1996*19261079SEd Maste } 1997*19261079SEd Maste 1998bc5531deSDag-Erling Smørgrav static void 1999bc5531deSDag-Erling Smørgrav update_known_hosts(struct hostkeys_update_ctx *ctx) 2000bc5531deSDag-Erling Smørgrav { 2001*19261079SEd Maste int r, was_raw = 0, first = 1; 2002*19261079SEd Maste int asking = options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK; 2003*19261079SEd Maste LogLevel loglevel = asking ? SYSLOG_LEVEL_INFO : SYSLOG_LEVEL_VERBOSE; 2004bc5531deSDag-Erling Smørgrav char *fp, *response; 2005bc5531deSDag-Erling Smørgrav size_t i; 2006*19261079SEd Maste struct stat sb; 2007bc5531deSDag-Erling Smørgrav 2008bc5531deSDag-Erling Smørgrav for (i = 0; i < ctx->nkeys; i++) { 2009*19261079SEd Maste if (!ctx->keys_verified[i]) 2010bc5531deSDag-Erling Smørgrav continue; 2011bc5531deSDag-Erling Smørgrav if ((fp = sshkey_fingerprint(ctx->keys[i], 2012bc5531deSDag-Erling Smørgrav options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) 2013*19261079SEd Maste fatal_f("sshkey_fingerprint failed"); 2014*19261079SEd Maste if (first && asking) 2015*19261079SEd Maste hostkey_change_preamble(loglevel); 2016bc5531deSDag-Erling Smørgrav do_log2(loglevel, "Learned new hostkey: %s %s", 2017bc5531deSDag-Erling Smørgrav sshkey_type(ctx->keys[i]), fp); 2018*19261079SEd Maste first = 0; 2019bc5531deSDag-Erling Smørgrav free(fp); 2020bc5531deSDag-Erling Smørgrav } 2021bc5531deSDag-Erling Smørgrav for (i = 0; i < ctx->nold; i++) { 2022bc5531deSDag-Erling Smørgrav if ((fp = sshkey_fingerprint(ctx->old_keys[i], 2023bc5531deSDag-Erling Smørgrav options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) 2024*19261079SEd Maste fatal_f("sshkey_fingerprint failed"); 2025*19261079SEd Maste if (first && asking) 2026*19261079SEd Maste hostkey_change_preamble(loglevel); 2027bc5531deSDag-Erling Smørgrav do_log2(loglevel, "Deprecating obsolete hostkey: %s %s", 2028bc5531deSDag-Erling Smørgrav sshkey_type(ctx->old_keys[i]), fp); 2029*19261079SEd Maste first = 0; 2030bc5531deSDag-Erling Smørgrav free(fp); 2031bc5531deSDag-Erling Smørgrav } 2032bc5531deSDag-Erling Smørgrav if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK) { 2033bc5531deSDag-Erling Smørgrav if (get_saved_tio() != NULL) { 2034bc5531deSDag-Erling Smørgrav leave_raw_mode(1); 2035bc5531deSDag-Erling Smørgrav was_raw = 1; 2036bc5531deSDag-Erling Smørgrav } 2037bc5531deSDag-Erling Smørgrav response = NULL; 2038bc5531deSDag-Erling Smørgrav for (i = 0; !quit_pending && i < 3; i++) { 2039bc5531deSDag-Erling Smørgrav free(response); 2040bc5531deSDag-Erling Smørgrav response = read_passphrase("Accept updated hostkeys? " 2041bc5531deSDag-Erling Smørgrav "(yes/no): ", RP_ECHO); 2042bc5531deSDag-Erling Smørgrav if (strcasecmp(response, "yes") == 0) 2043bc5531deSDag-Erling Smørgrav break; 2044bc5531deSDag-Erling Smørgrav else if (quit_pending || response == NULL || 2045bc5531deSDag-Erling Smørgrav strcasecmp(response, "no") == 0) { 2046bc5531deSDag-Erling Smørgrav options.update_hostkeys = 0; 2047bc5531deSDag-Erling Smørgrav break; 2048bc5531deSDag-Erling Smørgrav } else { 2049bc5531deSDag-Erling Smørgrav do_log2(loglevel, "Please enter " 2050bc5531deSDag-Erling Smørgrav "\"yes\" or \"no\""); 2051bc5531deSDag-Erling Smørgrav } 2052bc5531deSDag-Erling Smørgrav } 2053bc5531deSDag-Erling Smørgrav if (quit_pending || i >= 3 || response == NULL) 2054bc5531deSDag-Erling Smørgrav options.update_hostkeys = 0; 2055bc5531deSDag-Erling Smørgrav free(response); 2056bc5531deSDag-Erling Smørgrav if (was_raw) 2057bc5531deSDag-Erling Smørgrav enter_raw_mode(1); 2058bc5531deSDag-Erling Smørgrav } 2059*19261079SEd Maste if (options.update_hostkeys == 0) 2060*19261079SEd Maste return; 2061bc5531deSDag-Erling Smørgrav /* 2062bc5531deSDag-Erling Smørgrav * Now that all the keys are verified, we can go ahead and replace 2063bc5531deSDag-Erling Smørgrav * them in known_hosts (assuming SSH_UPDATE_HOSTKEYS_ASK didn't 2064bc5531deSDag-Erling Smørgrav * cancel the operation). 2065bc5531deSDag-Erling Smørgrav */ 2066*19261079SEd Maste for (i = 0; i < options.num_user_hostfiles; i++) { 2067*19261079SEd Maste /* 2068*19261079SEd Maste * NB. keys are only added to hostfiles[0], for the rest we 2069*19261079SEd Maste * just delete the hostname entries. 2070*19261079SEd Maste */ 2071*19261079SEd Maste if (stat(options.user_hostfiles[i], &sb) != 0) { 2072*19261079SEd Maste if (errno == ENOENT) { 2073*19261079SEd Maste debug_f("known hosts file %s does not " 2074*19261079SEd Maste "exist", options.user_hostfiles[i]); 2075*19261079SEd Maste } else { 2076*19261079SEd Maste error_f("known hosts file %s " 2077*19261079SEd Maste "inaccessible: %s", 2078*19261079SEd Maste options.user_hostfiles[i], strerror(errno)); 2079*19261079SEd Maste } 2080*19261079SEd Maste continue; 2081*19261079SEd Maste } 2082*19261079SEd Maste if ((r = hostfile_replace_entries(options.user_hostfiles[i], 2083*19261079SEd Maste ctx->host_str, ctx->ip_str, 2084*19261079SEd Maste i == 0 ? ctx->keys : NULL, i == 0 ? ctx->nkeys : 0, 2085bc5531deSDag-Erling Smørgrav options.hash_known_hosts, 0, 2086*19261079SEd Maste options.fingerprint_hash)) != 0) { 2087*19261079SEd Maste error_fr(r, "hostfile_replace_entries failed for %s", 2088*19261079SEd Maste options.user_hostfiles[i]); 2089*19261079SEd Maste } 2090*19261079SEd Maste } 2091bc5531deSDag-Erling Smørgrav } 2092bc5531deSDag-Erling Smørgrav 2093bc5531deSDag-Erling Smørgrav static void 20944f52dfbbSDag-Erling Smørgrav client_global_hostkeys_private_confirm(struct ssh *ssh, int type, 20954f52dfbbSDag-Erling Smørgrav u_int32_t seq, void *_ctx) 2096bc5531deSDag-Erling Smørgrav { 2097bc5531deSDag-Erling Smørgrav struct hostkeys_update_ctx *ctx = (struct hostkeys_update_ctx *)_ctx; 2098bc5531deSDag-Erling Smørgrav size_t i, ndone; 2099bc5531deSDag-Erling Smørgrav struct sshbuf *signdata; 210047dd1d1bSDag-Erling Smørgrav int r, kexsigtype, use_kexsigtype; 2101bc5531deSDag-Erling Smørgrav const u_char *sig; 2102bc5531deSDag-Erling Smørgrav size_t siglen; 2103bc5531deSDag-Erling Smørgrav 2104bc5531deSDag-Erling Smørgrav if (ctx->nnew == 0) 2105*19261079SEd Maste fatal_f("ctx->nnew == 0"); /* sanity */ 2106bc5531deSDag-Erling Smørgrav if (type != SSH2_MSG_REQUEST_SUCCESS) { 2107bc5531deSDag-Erling Smørgrav error("Server failed to confirm ownership of " 2108bc5531deSDag-Erling Smørgrav "private host keys"); 2109bc5531deSDag-Erling Smørgrav hostkeys_update_ctx_free(ctx); 2110bc5531deSDag-Erling Smørgrav return; 2111bc5531deSDag-Erling Smørgrav } 211247dd1d1bSDag-Erling Smørgrav kexsigtype = sshkey_type_plain( 211347dd1d1bSDag-Erling Smørgrav sshkey_type_from_name(ssh->kex->hostkey_alg)); 211447dd1d1bSDag-Erling Smørgrav 2115bc5531deSDag-Erling Smørgrav if ((signdata = sshbuf_new()) == NULL) 2116*19261079SEd Maste fatal_f("sshbuf_new failed"); 2117bc5531deSDag-Erling Smørgrav /* 2118bc5531deSDag-Erling Smørgrav * Expect a signature for each of the ctx->nnew private keys we 2119bc5531deSDag-Erling Smørgrav * haven't seen before. They will be in the same order as the 2120*19261079SEd Maste * ctx->keys where the corresponding ctx->keys_match[i] == 0. 2121bc5531deSDag-Erling Smørgrav */ 2122bc5531deSDag-Erling Smørgrav for (ndone = i = 0; i < ctx->nkeys; i++) { 2123*19261079SEd Maste if (ctx->keys_match[i]) 2124bc5531deSDag-Erling Smørgrav continue; 2125bc5531deSDag-Erling Smørgrav /* Prepare data to be signed: session ID, unique string, key */ 2126bc5531deSDag-Erling Smørgrav sshbuf_reset(signdata); 2127bc5531deSDag-Erling Smørgrav if ( (r = sshbuf_put_cstring(signdata, 2128bc5531deSDag-Erling Smørgrav "hostkeys-prove-00@openssh.com")) != 0 || 2129*19261079SEd Maste (r = sshbuf_put_stringb(signdata, 2130*19261079SEd Maste ssh->kex->session_id)) != 0 || 2131bc5531deSDag-Erling Smørgrav (r = sshkey_puts(ctx->keys[i], signdata)) != 0) 2132*19261079SEd Maste fatal_fr(r, "compose signdata"); 2133bc5531deSDag-Erling Smørgrav /* Extract and verify signature */ 2134bc5531deSDag-Erling Smørgrav if ((r = sshpkt_get_string_direct(ssh, &sig, &siglen)) != 0) { 2135*19261079SEd Maste error_fr(r, "parse sig"); 2136bc5531deSDag-Erling Smørgrav goto out; 2137bc5531deSDag-Erling Smørgrav } 213847dd1d1bSDag-Erling Smørgrav /* 213947dd1d1bSDag-Erling Smørgrav * For RSA keys, prefer to use the signature type negotiated 214047dd1d1bSDag-Erling Smørgrav * during KEX to the default (SHA1). 214147dd1d1bSDag-Erling Smørgrav */ 214247dd1d1bSDag-Erling Smørgrav use_kexsigtype = kexsigtype == KEY_RSA && 214347dd1d1bSDag-Erling Smørgrav sshkey_type_plain(ctx->keys[i]->type) == KEY_RSA; 2144*19261079SEd Maste debug3_f("verify %s key %zu using %s sigalg", 2145*19261079SEd Maste sshkey_type(ctx->keys[i]), i, 2146*19261079SEd Maste use_kexsigtype ? ssh->kex->hostkey_alg : "default"); 2147bc5531deSDag-Erling Smørgrav if ((r = sshkey_verify(ctx->keys[i], sig, siglen, 214847dd1d1bSDag-Erling Smørgrav sshbuf_ptr(signdata), sshbuf_len(signdata), 2149*19261079SEd Maste use_kexsigtype ? ssh->kex->hostkey_alg : NULL, 0, 2150*19261079SEd Maste NULL)) != 0) { 2151*19261079SEd Maste error_fr(r, "server gave bad signature for %s key %zu", 2152*19261079SEd Maste sshkey_type(ctx->keys[i]), i); 2153bc5531deSDag-Erling Smørgrav goto out; 2154bc5531deSDag-Erling Smørgrav } 2155bc5531deSDag-Erling Smørgrav /* Key is good. Mark it as 'seen' */ 2156*19261079SEd Maste ctx->keys_verified[i] = 1; 2157bc5531deSDag-Erling Smørgrav ndone++; 2158bc5531deSDag-Erling Smørgrav } 2159*19261079SEd Maste /* Shouldn't happen */ 2160bc5531deSDag-Erling Smørgrav if (ndone != ctx->nnew) 2161*19261079SEd Maste fatal_f("ndone != ctx->nnew (%zu / %zu)", ndone, ctx->nnew); 2162*19261079SEd Maste if ((r = sshpkt_get_end(ssh)) != 0) { 2163*19261079SEd Maste error_f("protocol error"); 2164*19261079SEd Maste goto out; 2165*19261079SEd Maste } 2166bc5531deSDag-Erling Smørgrav 2167bc5531deSDag-Erling Smørgrav /* Make the edits to known_hosts */ 2168bc5531deSDag-Erling Smørgrav update_known_hosts(ctx); 2169bc5531deSDag-Erling Smørgrav out: 2170bc5531deSDag-Erling Smørgrav hostkeys_update_ctx_free(ctx); 2171bc5531deSDag-Erling Smørgrav } 2172bc5531deSDag-Erling Smørgrav 2173bc5531deSDag-Erling Smørgrav /* 2174d93a896eSDag-Erling Smørgrav * Returns non-zero if the key is accepted by HostkeyAlgorithms. 2175d93a896eSDag-Erling Smørgrav * Made slightly less trivial by the multiple RSA signature algorithm names. 2176d93a896eSDag-Erling Smørgrav */ 2177d93a896eSDag-Erling Smørgrav static int 2178d93a896eSDag-Erling Smørgrav key_accepted_by_hostkeyalgs(const struct sshkey *key) 2179d93a896eSDag-Erling Smørgrav { 2180d93a896eSDag-Erling Smørgrav const char *ktype = sshkey_ssh_name(key); 2181*19261079SEd Maste const char *hostkeyalgs = options.hostkeyalgorithms; 2182d93a896eSDag-Erling Smørgrav 2183d93a896eSDag-Erling Smørgrav if (key == NULL || key->type == KEY_UNSPEC) 2184d93a896eSDag-Erling Smørgrav return 0; 2185d93a896eSDag-Erling Smørgrav if (key->type == KEY_RSA && 2186d93a896eSDag-Erling Smørgrav (match_pattern_list("rsa-sha2-256", hostkeyalgs, 0) == 1 || 2187d93a896eSDag-Erling Smørgrav match_pattern_list("rsa-sha2-512", hostkeyalgs, 0) == 1)) 2188d93a896eSDag-Erling Smørgrav return 1; 2189d93a896eSDag-Erling Smørgrav return match_pattern_list(ktype, hostkeyalgs, 0) == 1; 2190d93a896eSDag-Erling Smørgrav } 2191d93a896eSDag-Erling Smørgrav 2192d93a896eSDag-Erling Smørgrav /* 2193bc5531deSDag-Erling Smørgrav * Handle hostkeys-00@openssh.com global request to inform the client of all 2194bc5531deSDag-Erling Smørgrav * the server's hostkeys. The keys are checked against the user's 2195bc5531deSDag-Erling Smørgrav * HostkeyAlgorithms preference before they are accepted. 2196bc5531deSDag-Erling Smørgrav */ 2197bc5531deSDag-Erling Smørgrav static int 2198*19261079SEd Maste client_input_hostkeys(struct ssh *ssh) 2199bc5531deSDag-Erling Smørgrav { 2200bc5531deSDag-Erling Smørgrav const u_char *blob = NULL; 2201bc5531deSDag-Erling Smørgrav size_t i, len = 0; 2202bc5531deSDag-Erling Smørgrav struct sshbuf *buf = NULL; 2203bc5531deSDag-Erling Smørgrav struct sshkey *key = NULL, **tmp; 2204bc5531deSDag-Erling Smørgrav int r; 2205bc5531deSDag-Erling Smørgrav char *fp; 2206bc5531deSDag-Erling Smørgrav static int hostkeys_seen = 0; /* XXX use struct ssh */ 2207bc5531deSDag-Erling Smørgrav extern struct sockaddr_storage hostaddr; /* XXX from ssh.c */ 2208bc5531deSDag-Erling Smørgrav struct hostkeys_update_ctx *ctx = NULL; 2209*19261079SEd Maste u_int want; 2210bc5531deSDag-Erling Smørgrav 2211bc5531deSDag-Erling Smørgrav if (hostkeys_seen) 2212*19261079SEd Maste fatal_f("server already sent hostkeys"); 2213bc5531deSDag-Erling Smørgrav if (options.update_hostkeys == SSH_UPDATE_HOSTKEYS_ASK && 2214bc5531deSDag-Erling Smørgrav options.batch_mode) 2215bc5531deSDag-Erling Smørgrav return 1; /* won't ask in batchmode, so don't even try */ 2216bc5531deSDag-Erling Smørgrav if (!options.update_hostkeys || options.num_user_hostfiles <= 0) 2217bc5531deSDag-Erling Smørgrav return 1; 2218bc5531deSDag-Erling Smørgrav 2219bc5531deSDag-Erling Smørgrav ctx = xcalloc(1, sizeof(*ctx)); 2220bc5531deSDag-Erling Smørgrav while (ssh_packet_remaining(ssh) > 0) { 2221bc5531deSDag-Erling Smørgrav sshkey_free(key); 2222bc5531deSDag-Erling Smørgrav key = NULL; 2223bc5531deSDag-Erling Smørgrav if ((r = sshpkt_get_string_direct(ssh, &blob, &len)) != 0) { 2224*19261079SEd Maste error_fr(r, "parse key"); 2225bc5531deSDag-Erling Smørgrav goto out; 2226bc5531deSDag-Erling Smørgrav } 2227bc5531deSDag-Erling Smørgrav if ((r = sshkey_from_blob(blob, len, &key)) != 0) { 2228*19261079SEd Maste do_log2_fr(r, r == SSH_ERR_KEY_TYPE_UNKNOWN ? 2229*19261079SEd Maste SYSLOG_LEVEL_DEBUG1 : SYSLOG_LEVEL_ERROR, 2230*19261079SEd Maste "convert key"); 2231*19261079SEd Maste continue; 2232bc5531deSDag-Erling Smørgrav } 2233bc5531deSDag-Erling Smørgrav fp = sshkey_fingerprint(key, options.fingerprint_hash, 2234bc5531deSDag-Erling Smørgrav SSH_FP_DEFAULT); 2235*19261079SEd Maste debug3_f("received %s key %s", sshkey_type(key), fp); 2236bc5531deSDag-Erling Smørgrav free(fp); 2237eccfee6eSDag-Erling Smørgrav 2238d93a896eSDag-Erling Smørgrav if (!key_accepted_by_hostkeyalgs(key)) { 2239*19261079SEd Maste debug3_f("%s key not permitted by " 2240*19261079SEd Maste "HostkeyAlgorithms", sshkey_ssh_name(key)); 2241bc5531deSDag-Erling Smørgrav continue; 2242bc5531deSDag-Erling Smørgrav } 2243bc5531deSDag-Erling Smørgrav /* Skip certs */ 2244bc5531deSDag-Erling Smørgrav if (sshkey_is_cert(key)) { 2245*19261079SEd Maste debug3_f("%s key is a certificate; skipping", 2246*19261079SEd Maste sshkey_ssh_name(key)); 2247bc5531deSDag-Erling Smørgrav continue; 2248bc5531deSDag-Erling Smørgrav } 2249bc5531deSDag-Erling Smørgrav /* Ensure keys are unique */ 2250bc5531deSDag-Erling Smørgrav for (i = 0; i < ctx->nkeys; i++) { 2251bc5531deSDag-Erling Smørgrav if (sshkey_equal(key, ctx->keys[i])) { 2252*19261079SEd Maste error_f("received duplicated %s host key", 2253*19261079SEd Maste sshkey_ssh_name(key)); 2254bc5531deSDag-Erling Smørgrav goto out; 2255bc5531deSDag-Erling Smørgrav } 2256bc5531deSDag-Erling Smørgrav } 2257bc5531deSDag-Erling Smørgrav /* Key is good, record it */ 22584f52dfbbSDag-Erling Smørgrav if ((tmp = recallocarray(ctx->keys, ctx->nkeys, ctx->nkeys + 1, 2259bc5531deSDag-Erling Smørgrav sizeof(*ctx->keys))) == NULL) 2260*19261079SEd Maste fatal_f("recallocarray failed nkeys = %zu", 2261*19261079SEd Maste ctx->nkeys); 2262bc5531deSDag-Erling Smørgrav ctx->keys = tmp; 2263bc5531deSDag-Erling Smørgrav ctx->keys[ctx->nkeys++] = key; 2264bc5531deSDag-Erling Smørgrav key = NULL; 2265bc5531deSDag-Erling Smørgrav } 2266bc5531deSDag-Erling Smørgrav 2267bc5531deSDag-Erling Smørgrav if (ctx->nkeys == 0) { 2268*19261079SEd Maste debug_f("server sent no hostkeys"); 2269bc5531deSDag-Erling Smørgrav goto out; 2270bc5531deSDag-Erling Smørgrav } 2271bc5531deSDag-Erling Smørgrav 2272*19261079SEd Maste if ((ctx->keys_match = calloc(ctx->nkeys, 2273*19261079SEd Maste sizeof(*ctx->keys_match))) == NULL || 2274*19261079SEd Maste (ctx->keys_verified = calloc(ctx->nkeys, 2275*19261079SEd Maste sizeof(*ctx->keys_verified))) == NULL) 2276*19261079SEd Maste fatal_f("calloc failed"); 2277bc5531deSDag-Erling Smørgrav 2278bc5531deSDag-Erling Smørgrav get_hostfile_hostname_ipaddr(host, 2279bc5531deSDag-Erling Smørgrav options.check_host_ip ? (struct sockaddr *)&hostaddr : NULL, 2280bc5531deSDag-Erling Smørgrav options.port, &ctx->host_str, 2281bc5531deSDag-Erling Smørgrav options.check_host_ip ? &ctx->ip_str : NULL); 2282bc5531deSDag-Erling Smørgrav 2283bc5531deSDag-Erling Smørgrav /* Find which keys we already know about. */ 2284*19261079SEd Maste for (i = 0; i < options.num_user_hostfiles; i++) { 2285*19261079SEd Maste debug_f("searching %s for %s / %s", 2286*19261079SEd Maste options.user_hostfiles[i], ctx->host_str, 2287*19261079SEd Maste ctx->ip_str ? ctx->ip_str : "(none)"); 2288*19261079SEd Maste if ((r = hostkeys_foreach(options.user_hostfiles[i], 2289*19261079SEd Maste hostkeys_find, ctx, ctx->host_str, ctx->ip_str, 2290*19261079SEd Maste HKF_WANT_PARSE_KEY, 0)) != 0) { 2291*19261079SEd Maste if (r == SSH_ERR_SYSTEM_ERROR && errno == ENOENT) { 2292*19261079SEd Maste debug_f("hostkeys file %s does not exist", 2293*19261079SEd Maste options.user_hostfiles[i]); 2294*19261079SEd Maste continue; 2295*19261079SEd Maste } 2296*19261079SEd Maste error_fr(r, "hostkeys_foreach failed for %s", 2297*19261079SEd Maste options.user_hostfiles[i]); 2298bc5531deSDag-Erling Smørgrav goto out; 2299bc5531deSDag-Erling Smørgrav } 2300*19261079SEd Maste } 2301bc5531deSDag-Erling Smørgrav 2302bc5531deSDag-Erling Smørgrav /* Figure out if we have any new keys to add */ 2303*19261079SEd Maste ctx->nnew = ctx->nincomplete = 0; 2304*19261079SEd Maste want = HKF_MATCH_HOST | ( options.check_host_ip ? HKF_MATCH_IP : 0); 2305bc5531deSDag-Erling Smørgrav for (i = 0; i < ctx->nkeys; i++) { 2306*19261079SEd Maste if (ctx->keys_match[i] == 0) 2307bc5531deSDag-Erling Smørgrav ctx->nnew++; 2308*19261079SEd Maste if ((ctx->keys_match[i] & want) != want) 2309*19261079SEd Maste ctx->nincomplete++; 2310bc5531deSDag-Erling Smørgrav } 2311bc5531deSDag-Erling Smørgrav 2312*19261079SEd Maste debug3_f("%zu server keys: %zu new, %zu retained, " 2313*19261079SEd Maste "%zu incomplete match. %zu to remove", ctx->nkeys, ctx->nnew, 2314*19261079SEd Maste ctx->nkeys - ctx->nnew - ctx->nincomplete, 2315*19261079SEd Maste ctx->nincomplete, ctx->nold); 2316bc5531deSDag-Erling Smørgrav 2317*19261079SEd Maste if (ctx->nnew == 0 && ctx->nold == 0) { 2318*19261079SEd Maste debug_f("no new or deprecated keys from server"); 2319*19261079SEd Maste goto out; 2320*19261079SEd Maste } 2321*19261079SEd Maste 2322*19261079SEd Maste /* Various reasons why we cannot proceed with the update */ 2323*19261079SEd Maste if (ctx->complex_hostspec) { 2324*19261079SEd Maste debug_f("CA/revocation marker, manual host list or wildcard " 2325*19261079SEd Maste "host pattern found, skipping UserKnownHostsFile update"); 2326*19261079SEd Maste goto out; 2327*19261079SEd Maste } 2328*19261079SEd Maste if (ctx->other_name_seen) { 2329*19261079SEd Maste debug_f("host key found matching a different name/address, " 2330*19261079SEd Maste "skipping UserKnownHostsFile update"); 2331*19261079SEd Maste goto out; 2332*19261079SEd Maste } 2333bc5531deSDag-Erling Smørgrav /* 2334*19261079SEd Maste * If removing keys, check whether they appear under different 2335*19261079SEd Maste * names/addresses and refuse to proceed if they do. This avoids 2336*19261079SEd Maste * cases such as hosts with multiple names becoming inconsistent 2337*19261079SEd Maste * with regards to CheckHostIP entries. 2338*19261079SEd Maste * XXX UpdateHostkeys=force to override this (and other) checks? 2339*19261079SEd Maste */ 2340*19261079SEd Maste if (ctx->nold != 0) { 2341*19261079SEd Maste if (check_old_keys_othernames(ctx) != 0) 2342*19261079SEd Maste goto out; /* error already logged */ 2343*19261079SEd Maste if (ctx->old_key_seen) { 2344*19261079SEd Maste debug_f("key(s) for %s%s%s exist under other names; " 2345*19261079SEd Maste "skipping UserKnownHostsFile update", 2346*19261079SEd Maste ctx->host_str, ctx->ip_str == NULL ? "" : ",", 2347*19261079SEd Maste ctx->ip_str == NULL ? "" : ctx->ip_str); 2348*19261079SEd Maste goto out; 2349*19261079SEd Maste } 2350*19261079SEd Maste } 2351*19261079SEd Maste 2352*19261079SEd Maste if (ctx->nnew == 0) { 2353*19261079SEd Maste /* 2354*19261079SEd Maste * We have some keys to remove or fix matching for. 2355*19261079SEd Maste * We can proceed to do this without requiring a fresh proof 2356*19261079SEd Maste * from the server. 2357*19261079SEd Maste */ 2358*19261079SEd Maste update_known_hosts(ctx); 2359*19261079SEd Maste goto out; 2360*19261079SEd Maste } 2361*19261079SEd Maste /* 2362*19261079SEd Maste * We have received previously-unseen keys from the server. 2363bc5531deSDag-Erling Smørgrav * Ask the server to confirm ownership of the private halves. 2364bc5531deSDag-Erling Smørgrav */ 2365*19261079SEd Maste debug3_f("asking server to prove ownership for %zu keys", ctx->nnew); 2366bc5531deSDag-Erling Smørgrav if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || 2367bc5531deSDag-Erling Smørgrav (r = sshpkt_put_cstring(ssh, 2368bc5531deSDag-Erling Smørgrav "hostkeys-prove-00@openssh.com")) != 0 || 2369bc5531deSDag-Erling Smørgrav (r = sshpkt_put_u8(ssh, 1)) != 0) /* bool: want reply */ 2370*19261079SEd Maste fatal_fr(r, "prepare hostkeys-prove"); 2371bc5531deSDag-Erling Smørgrav if ((buf = sshbuf_new()) == NULL) 2372*19261079SEd Maste fatal_f("sshbuf_new"); 2373bc5531deSDag-Erling Smørgrav for (i = 0; i < ctx->nkeys; i++) { 2374*19261079SEd Maste if (ctx->keys_match[i]) 2375bc5531deSDag-Erling Smørgrav continue; 2376bc5531deSDag-Erling Smørgrav sshbuf_reset(buf); 2377*19261079SEd Maste if ((r = sshkey_putb(ctx->keys[i], buf)) != 0 || 2378*19261079SEd Maste (r = sshpkt_put_stringb(ssh, buf)) != 0) 2379*19261079SEd Maste fatal_fr(r, "assemble hostkeys-prove"); 2380bc5531deSDag-Erling Smørgrav } 2381bc5531deSDag-Erling Smørgrav if ((r = sshpkt_send(ssh)) != 0) 2382*19261079SEd Maste fatal_fr(r, "send hostkeys-prove"); 2383bc5531deSDag-Erling Smørgrav client_register_global_confirm( 2384bc5531deSDag-Erling Smørgrav client_global_hostkeys_private_confirm, ctx); 2385bc5531deSDag-Erling Smørgrav ctx = NULL; /* will be freed in callback */ 2386bc5531deSDag-Erling Smørgrav 2387bc5531deSDag-Erling Smørgrav /* Success */ 2388bc5531deSDag-Erling Smørgrav out: 2389bc5531deSDag-Erling Smørgrav hostkeys_update_ctx_free(ctx); 2390bc5531deSDag-Erling Smørgrav sshkey_free(key); 2391bc5531deSDag-Erling Smørgrav sshbuf_free(buf); 2392bc5531deSDag-Erling Smørgrav /* 2393bc5531deSDag-Erling Smørgrav * NB. Return success for all cases. The server doesn't need to know 2394bc5531deSDag-Erling Smørgrav * what the client does with its hosts file. 2395bc5531deSDag-Erling Smørgrav */ 2396bc5531deSDag-Erling Smørgrav return 1; 2397bc5531deSDag-Erling Smørgrav } 2398bc5531deSDag-Erling Smørgrav 2399bc5531deSDag-Erling Smørgrav static int 24004f52dfbbSDag-Erling Smørgrav client_input_global_request(int type, u_int32_t seq, struct ssh *ssh) 2401ae1f160dSDag-Erling Smørgrav { 2402ae1f160dSDag-Erling Smørgrav char *rtype; 2403*19261079SEd Maste u_char want_reply; 2404*19261079SEd Maste int r, success = 0; 2405a04a10f8SKris Kennaway 2406*19261079SEd Maste if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 || 2407*19261079SEd Maste (r = sshpkt_get_u8(ssh, &want_reply)) != 0) 2408*19261079SEd Maste goto out; 2409efcad6b7SDag-Erling Smørgrav debug("client_input_global_request: rtype %s want_reply %d", 2410efcad6b7SDag-Erling Smørgrav rtype, want_reply); 2411bc5531deSDag-Erling Smørgrav if (strcmp(rtype, "hostkeys-00@openssh.com") == 0) 2412*19261079SEd Maste success = client_input_hostkeys(ssh); 2413ae1f160dSDag-Erling Smørgrav if (want_reply) { 2414*19261079SEd Maste if ((r = sshpkt_start(ssh, success ? SSH2_MSG_REQUEST_SUCCESS : 2415*19261079SEd Maste SSH2_MSG_REQUEST_FAILURE)) != 0 || 2416*19261079SEd Maste (r = sshpkt_send(ssh)) != 0 || 2417*19261079SEd Maste (r = ssh_packet_write_wait(ssh)) != 0) 2418*19261079SEd Maste goto out; 2419ae1f160dSDag-Erling Smørgrav } 2420*19261079SEd Maste r = 0; 2421*19261079SEd Maste out: 2422e4a9863fSDag-Erling Smørgrav free(rtype); 2423*19261079SEd Maste return r; 2424*19261079SEd Maste } 2425*19261079SEd Maste 2426*19261079SEd Maste static void 2427*19261079SEd Maste client_send_env(struct ssh *ssh, int id, const char *name, const char *val) 2428*19261079SEd Maste { 2429*19261079SEd Maste int r; 2430*19261079SEd Maste 2431*19261079SEd Maste debug("channel %d: setting env %s = \"%s\"", id, name, val); 2432*19261079SEd Maste channel_request_start(ssh, id, "env", 0); 2433*19261079SEd Maste if ((r = sshpkt_put_cstring(ssh, name)) != 0 || 2434*19261079SEd Maste (r = sshpkt_put_cstring(ssh, val)) != 0 || 2435*19261079SEd Maste (r = sshpkt_send(ssh)) != 0) 2436*19261079SEd Maste fatal_fr(r, "send setenv"); 2437ae1f160dSDag-Erling Smørgrav } 2438ae1f160dSDag-Erling Smørgrav 2439d74d50a8SDag-Erling Smørgrav void 24404f52dfbbSDag-Erling Smørgrav client_session2_setup(struct ssh *ssh, int id, int want_tty, int want_subsystem, 2441190cef3dSDag-Erling Smørgrav const char *term, struct termios *tiop, int in_fd, struct sshbuf *cmd, 2442190cef3dSDag-Erling Smørgrav char **env) 2443d74d50a8SDag-Erling Smørgrav { 2444*19261079SEd Maste int i, j, matched, len, r; 2445190cef3dSDag-Erling Smørgrav char *name, *val; 24465e8dbd04SDag-Erling Smørgrav Channel *c = NULL; 2447d74d50a8SDag-Erling Smørgrav 2448*19261079SEd Maste debug2_f("id %d", id); 2449d74d50a8SDag-Erling Smørgrav 24504f52dfbbSDag-Erling Smørgrav if ((c = channel_lookup(ssh, id)) == NULL) 2451*19261079SEd Maste fatal_f("channel %d: unknown channel", id); 24525e8dbd04SDag-Erling Smørgrav 2453*19261079SEd Maste ssh_packet_set_interactive(ssh, want_tty, 24544a421b63SDag-Erling Smørgrav options.ip_qos_interactive, options.ip_qos_bulk); 24554a421b63SDag-Erling Smørgrav 2456d74d50a8SDag-Erling Smørgrav if (want_tty) { 2457d74d50a8SDag-Erling Smørgrav struct winsize ws; 2458d74d50a8SDag-Erling Smørgrav 2459d74d50a8SDag-Erling Smørgrav /* Store window size in the packet. */ 2460*19261079SEd Maste if (ioctl(in_fd, TIOCGWINSZ, &ws) == -1) 2461d74d50a8SDag-Erling Smørgrav memset(&ws, 0, sizeof(ws)); 2462d74d50a8SDag-Erling Smørgrav 24634f52dfbbSDag-Erling Smørgrav channel_request_start(ssh, id, "pty-req", 1); 24644f52dfbbSDag-Erling Smørgrav client_expect_confirm(ssh, id, "PTY allocation", CONFIRM_TTY); 2465*19261079SEd Maste if ((r = sshpkt_put_cstring(ssh, term != NULL ? term : "")) 2466*19261079SEd Maste != 0 || 2467*19261079SEd Maste (r = sshpkt_put_u32(ssh, (u_int)ws.ws_col)) != 0 || 2468*19261079SEd Maste (r = sshpkt_put_u32(ssh, (u_int)ws.ws_row)) != 0 || 2469*19261079SEd Maste (r = sshpkt_put_u32(ssh, (u_int)ws.ws_xpixel)) != 0 || 2470*19261079SEd Maste (r = sshpkt_put_u32(ssh, (u_int)ws.ws_ypixel)) != 0) 2471*19261079SEd Maste fatal_fr(r, "build pty-req"); 2472d4af9e69SDag-Erling Smørgrav if (tiop == NULL) 2473d4af9e69SDag-Erling Smørgrav tiop = get_saved_tio(); 2474190cef3dSDag-Erling Smørgrav ssh_tty_make_modes(ssh, -1, tiop); 2475*19261079SEd Maste if ((r = sshpkt_send(ssh)) != 0) 2476*19261079SEd Maste fatal_fr(r, "send pty-req"); 2477d74d50a8SDag-Erling Smørgrav /* XXX wait for reply */ 24785e8dbd04SDag-Erling Smørgrav c->client_tty = 1; 2479d74d50a8SDag-Erling Smørgrav } 2480d74d50a8SDag-Erling Smørgrav 2481d74d50a8SDag-Erling Smørgrav /* Transfer any environment variables from client to server */ 2482d74d50a8SDag-Erling Smørgrav if (options.num_send_env != 0 && env != NULL) { 2483d74d50a8SDag-Erling Smørgrav debug("Sending environment."); 2484d74d50a8SDag-Erling Smørgrav for (i = 0; env[i] != NULL; i++) { 2485d74d50a8SDag-Erling Smørgrav /* Split */ 2486d74d50a8SDag-Erling Smørgrav name = xstrdup(env[i]); 2487d74d50a8SDag-Erling Smørgrav if ((val = strchr(name, '=')) == NULL) { 2488e4a9863fSDag-Erling Smørgrav free(name); 2489d74d50a8SDag-Erling Smørgrav continue; 2490d74d50a8SDag-Erling Smørgrav } 2491d74d50a8SDag-Erling Smørgrav *val++ = '\0'; 2492d74d50a8SDag-Erling Smørgrav 2493d74d50a8SDag-Erling Smørgrav matched = 0; 2494d74d50a8SDag-Erling Smørgrav for (j = 0; j < options.num_send_env; j++) { 2495d74d50a8SDag-Erling Smørgrav if (match_pattern(name, options.send_env[j])) { 2496d74d50a8SDag-Erling Smørgrav matched = 1; 2497d74d50a8SDag-Erling Smørgrav break; 2498d74d50a8SDag-Erling Smørgrav } 2499d74d50a8SDag-Erling Smørgrav } 2500d74d50a8SDag-Erling Smørgrav if (!matched) { 2501d74d50a8SDag-Erling Smørgrav debug3("Ignored env %s", name); 2502e4a9863fSDag-Erling Smørgrav free(name); 2503d74d50a8SDag-Erling Smørgrav continue; 2504d74d50a8SDag-Erling Smørgrav } 2505*19261079SEd Maste client_send_env(ssh, id, name, val); 2506e4a9863fSDag-Erling Smørgrav free(name); 2507d74d50a8SDag-Erling Smørgrav } 2508d74d50a8SDag-Erling Smørgrav } 2509190cef3dSDag-Erling Smørgrav for (i = 0; i < options.num_setenv; i++) { 2510190cef3dSDag-Erling Smørgrav /* Split */ 2511190cef3dSDag-Erling Smørgrav name = xstrdup(options.setenv[i]); 2512190cef3dSDag-Erling Smørgrav if ((val = strchr(name, '=')) == NULL) { 2513190cef3dSDag-Erling Smørgrav free(name); 2514190cef3dSDag-Erling Smørgrav continue; 2515190cef3dSDag-Erling Smørgrav } 2516190cef3dSDag-Erling Smørgrav *val++ = '\0'; 2517*19261079SEd Maste client_send_env(ssh, id, name, val); 2518190cef3dSDag-Erling Smørgrav free(name); 2519190cef3dSDag-Erling Smørgrav } 2520190cef3dSDag-Erling Smørgrav 2521190cef3dSDag-Erling Smørgrav len = sshbuf_len(cmd); 2522d74d50a8SDag-Erling Smørgrav if (len > 0) { 2523d74d50a8SDag-Erling Smørgrav if (len > 900) 2524d74d50a8SDag-Erling Smørgrav len = 900; 2525d74d50a8SDag-Erling Smørgrav if (want_subsystem) { 2526d4af9e69SDag-Erling Smørgrav debug("Sending subsystem: %.*s", 2527190cef3dSDag-Erling Smørgrav len, (const u_char*)sshbuf_ptr(cmd)); 25284f52dfbbSDag-Erling Smørgrav channel_request_start(ssh, id, "subsystem", 1); 25294f52dfbbSDag-Erling Smørgrav client_expect_confirm(ssh, id, "subsystem", 25304f52dfbbSDag-Erling Smørgrav CONFIRM_CLOSE); 2531d74d50a8SDag-Erling Smørgrav } else { 2532d4af9e69SDag-Erling Smørgrav debug("Sending command: %.*s", 2533190cef3dSDag-Erling Smørgrav len, (const u_char*)sshbuf_ptr(cmd)); 25344f52dfbbSDag-Erling Smørgrav channel_request_start(ssh, id, "exec", 1); 25354f52dfbbSDag-Erling Smørgrav client_expect_confirm(ssh, id, "exec", CONFIRM_CLOSE); 2536d74d50a8SDag-Erling Smørgrav } 2537*19261079SEd Maste if ((r = sshpkt_put_stringb(ssh, cmd)) != 0 || 2538*19261079SEd Maste (r = sshpkt_send(ssh)) != 0) 2539*19261079SEd Maste fatal_fr(r, "send command"); 2540d74d50a8SDag-Erling Smørgrav } else { 25414f52dfbbSDag-Erling Smørgrav channel_request_start(ssh, id, "shell", 1); 25424f52dfbbSDag-Erling Smørgrav client_expect_confirm(ssh, id, "shell", CONFIRM_CLOSE); 2543*19261079SEd Maste if ((r = sshpkt_send(ssh)) != 0) 2544*19261079SEd Maste fatal_fr(r, "send shell"); 2545d74d50a8SDag-Erling Smørgrav } 2546d74d50a8SDag-Erling Smørgrav } 2547d74d50a8SDag-Erling Smørgrav 2548ae1f160dSDag-Erling Smørgrav static void 2549*19261079SEd Maste client_init_dispatch(struct ssh *ssh) 2550a04a10f8SKris Kennaway { 2551*19261079SEd Maste ssh_dispatch_init(ssh, &dispatch_protocol_error); 2552545d5ecaSDag-Erling Smørgrav 2553*19261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); 2554*19261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_DATA, &channel_input_data); 2555*19261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); 2556*19261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); 2557*19261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN, &client_input_channel_open); 2558*19261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); 2559*19261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); 2560*19261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_REQUEST, &client_input_channel_req); 2561*19261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); 2562*19261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_SUCCESS, &channel_input_status_confirm); 2563*19261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_FAILURE, &channel_input_status_confirm); 2564*19261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_GLOBAL_REQUEST, &client_input_global_request); 25651e8db6e2SBrian Feldman 25661e8db6e2SBrian Feldman /* rekeying */ 2567*19261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit); 2568545d5ecaSDag-Erling Smørgrav 2569545d5ecaSDag-Erling Smørgrav /* global request reply messages */ 2570*19261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_FAILURE, &client_global_request_reply); 2571*19261079SEd Maste ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_SUCCESS, &client_global_request_reply); 2572a04a10f8SKris Kennaway } 2573d4af9e69SDag-Erling Smørgrav 2574e146993eSDag-Erling Smørgrav void 2575e146993eSDag-Erling Smørgrav client_stop_mux(void) 2576e146993eSDag-Erling Smørgrav { 2577e146993eSDag-Erling Smørgrav if (options.control_path != NULL && muxserver_sock != -1) 2578e146993eSDag-Erling Smørgrav unlink(options.control_path); 2579e146993eSDag-Erling Smørgrav /* 25806888a9beSDag-Erling Smørgrav * If we are in persist mode, or don't have a shell, signal that we 25816888a9beSDag-Erling Smørgrav * should close when all active channels are closed. 2582e146993eSDag-Erling Smørgrav */ 2583*19261079SEd Maste if (options.control_persist || options.session_type == SESSION_TYPE_NONE) { 2584e146993eSDag-Erling Smørgrav session_closed = 1; 2585e146993eSDag-Erling Smørgrav setproctitle("[stopped mux]"); 2586e146993eSDag-Erling Smørgrav } 2587e146993eSDag-Erling Smørgrav } 2588e146993eSDag-Erling Smørgrav 2589efcad6b7SDag-Erling Smørgrav /* client specific fatal cleanup */ 2590efcad6b7SDag-Erling Smørgrav void 2591efcad6b7SDag-Erling Smørgrav cleanup_exit(int i) 2592efcad6b7SDag-Erling Smørgrav { 2593e146993eSDag-Erling Smørgrav leave_raw_mode(options.request_tty == REQUEST_TTY_FORCE); 2594d4af9e69SDag-Erling Smørgrav if (options.control_path != NULL && muxserver_sock != -1) 2595d74d50a8SDag-Erling Smørgrav unlink(options.control_path); 25964a421b63SDag-Erling Smørgrav ssh_kill_proxy_command(); 2597efcad6b7SDag-Erling Smørgrav _exit(i); 2598efcad6b7SDag-Erling Smørgrav } 2599