1*ce74bacaSMatthew Dillon /* $OpenBSD: serverloop.c,v 1.198 2017/09/12 06:35:32 djm Exp $ */ 218de8d7fSPeter Avalos /* 318de8d7fSPeter Avalos * Author: Tatu Ylonen <ylo@cs.hut.fi> 418de8d7fSPeter Avalos * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 518de8d7fSPeter Avalos * All rights reserved 618de8d7fSPeter Avalos * Server main loop for handling the interactive session. 718de8d7fSPeter Avalos * 818de8d7fSPeter Avalos * As far as I am concerned, the code I have written for this software 918de8d7fSPeter Avalos * can be used freely for any purpose. Any derived versions of this 1018de8d7fSPeter Avalos * software must be clearly marked as such, and if the derived work is 1118de8d7fSPeter Avalos * incompatible with the protocol description in the RFC file, it must be 1218de8d7fSPeter Avalos * called by a name other than "ssh" or "Secure Shell". 1318de8d7fSPeter Avalos * 1418de8d7fSPeter Avalos * SSH2 support by Markus Friedl. 1518de8d7fSPeter Avalos * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. 1618de8d7fSPeter Avalos * 1718de8d7fSPeter Avalos * Redistribution and use in source and binary forms, with or without 1818de8d7fSPeter Avalos * modification, are permitted provided that the following conditions 1918de8d7fSPeter Avalos * are met: 2018de8d7fSPeter Avalos * 1. Redistributions of source code must retain the above copyright 2118de8d7fSPeter Avalos * notice, this list of conditions and the following disclaimer. 2218de8d7fSPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright 2318de8d7fSPeter Avalos * notice, this list of conditions and the following disclaimer in the 2418de8d7fSPeter Avalos * documentation and/or other materials provided with the distribution. 2518de8d7fSPeter Avalos * 2618de8d7fSPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2718de8d7fSPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2818de8d7fSPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2918de8d7fSPeter Avalos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 3018de8d7fSPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 3118de8d7fSPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3218de8d7fSPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3318de8d7fSPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3418de8d7fSPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3518de8d7fSPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3618de8d7fSPeter Avalos */ 3718de8d7fSPeter Avalos 3818de8d7fSPeter Avalos #include "includes.h" 3918de8d7fSPeter Avalos 4018de8d7fSPeter Avalos #include <sys/types.h> 4118de8d7fSPeter Avalos #include <sys/wait.h> 4218de8d7fSPeter Avalos #include <sys/socket.h> 4318de8d7fSPeter Avalos #ifdef HAVE_SYS_TIME_H 4418de8d7fSPeter Avalos # include <sys/time.h> 4518de8d7fSPeter Avalos #endif 4618de8d7fSPeter Avalos 4718de8d7fSPeter Avalos #include <netinet/in.h> 4818de8d7fSPeter Avalos 4918de8d7fSPeter Avalos #include <errno.h> 5018de8d7fSPeter Avalos #include <fcntl.h> 5118de8d7fSPeter Avalos #include <pwd.h> 5218de8d7fSPeter Avalos #include <signal.h> 5318de8d7fSPeter Avalos #include <string.h> 5418de8d7fSPeter Avalos #include <termios.h> 5518de8d7fSPeter Avalos #include <unistd.h> 5618de8d7fSPeter Avalos #include <stdarg.h> 5718de8d7fSPeter Avalos 5818de8d7fSPeter Avalos #include "openbsd-compat/sys-queue.h" 5918de8d7fSPeter Avalos #include "xmalloc.h" 6018de8d7fSPeter Avalos #include "packet.h" 6118de8d7fSPeter Avalos #include "buffer.h" 6218de8d7fSPeter Avalos #include "log.h" 6336e94dc5SPeter Avalos #include "misc.h" 6418de8d7fSPeter Avalos #include "servconf.h" 6518de8d7fSPeter Avalos #include "canohost.h" 6618de8d7fSPeter Avalos #include "sshpty.h" 6718de8d7fSPeter Avalos #include "channels.h" 6818de8d7fSPeter Avalos #include "compat.h" 6918de8d7fSPeter Avalos #include "ssh2.h" 7018de8d7fSPeter Avalos #include "key.h" 7118de8d7fSPeter Avalos #include "cipher.h" 7218de8d7fSPeter Avalos #include "kex.h" 7318de8d7fSPeter Avalos #include "hostfile.h" 7418de8d7fSPeter Avalos #include "auth.h" 7518de8d7fSPeter Avalos #include "session.h" 7618de8d7fSPeter Avalos #include "dispatch.h" 7718de8d7fSPeter Avalos #include "auth-options.h" 7818de8d7fSPeter Avalos #include "serverloop.h" 79e9778795SPeter Avalos #include "ssherr.h" 8018de8d7fSPeter Avalos 8118de8d7fSPeter Avalos extern ServerOptions options; 8218de8d7fSPeter Avalos 8318de8d7fSPeter Avalos /* XXX */ 8418de8d7fSPeter Avalos extern Authctxt *the_authctxt; 8518de8d7fSPeter Avalos extern int use_privsep; 8618de8d7fSPeter Avalos 8718de8d7fSPeter Avalos static int no_more_sessions = 0; /* Disallow further sessions. */ 8818de8d7fSPeter Avalos 8918de8d7fSPeter Avalos /* 9018de8d7fSPeter Avalos * This SIGCHLD kludge is used to detect when the child exits. The server 9118de8d7fSPeter Avalos * will exit after that, as soon as forwarded connections have terminated. 9218de8d7fSPeter Avalos */ 9318de8d7fSPeter Avalos 9418de8d7fSPeter Avalos static volatile sig_atomic_t child_terminated = 0; /* The child has terminated. */ 9518de8d7fSPeter Avalos 9618de8d7fSPeter Avalos /* Cleanup on signals (!use_privsep case only) */ 9718de8d7fSPeter Avalos static volatile sig_atomic_t received_sigterm = 0; 9818de8d7fSPeter Avalos 9918de8d7fSPeter Avalos /* prototypes */ 10018de8d7fSPeter Avalos static void server_init_dispatch(void); 10118de8d7fSPeter Avalos 10218de8d7fSPeter Avalos /* 10318de8d7fSPeter Avalos * we write to this pipe if a SIGCHLD is caught in order to avoid 10418de8d7fSPeter Avalos * the race between select() and child_terminated 10518de8d7fSPeter Avalos */ 10618de8d7fSPeter Avalos static int notify_pipe[2]; 10718de8d7fSPeter Avalos static void 10818de8d7fSPeter Avalos notify_setup(void) 10918de8d7fSPeter Avalos { 11018de8d7fSPeter Avalos if (pipe(notify_pipe) < 0) { 11118de8d7fSPeter Avalos error("pipe(notify_pipe) failed %s", strerror(errno)); 1121c188a7fSPeter Avalos } else if ((fcntl(notify_pipe[0], F_SETFD, FD_CLOEXEC) == -1) || 1131c188a7fSPeter Avalos (fcntl(notify_pipe[1], F_SETFD, FD_CLOEXEC) == -1)) { 11418de8d7fSPeter Avalos error("fcntl(notify_pipe, F_SETFD) failed %s", strerror(errno)); 11518de8d7fSPeter Avalos close(notify_pipe[0]); 11618de8d7fSPeter Avalos close(notify_pipe[1]); 11718de8d7fSPeter Avalos } else { 11818de8d7fSPeter Avalos set_nonblock(notify_pipe[0]); 11918de8d7fSPeter Avalos set_nonblock(notify_pipe[1]); 12018de8d7fSPeter Avalos return; 12118de8d7fSPeter Avalos } 12218de8d7fSPeter Avalos notify_pipe[0] = -1; /* read end */ 12318de8d7fSPeter Avalos notify_pipe[1] = -1; /* write end */ 12418de8d7fSPeter Avalos } 12518de8d7fSPeter Avalos static void 12618de8d7fSPeter Avalos notify_parent(void) 12718de8d7fSPeter Avalos { 12818de8d7fSPeter Avalos if (notify_pipe[1] != -1) 12936e94dc5SPeter Avalos (void)write(notify_pipe[1], "", 1); 13018de8d7fSPeter Avalos } 13118de8d7fSPeter Avalos static void 13218de8d7fSPeter Avalos notify_prepare(fd_set *readset) 13318de8d7fSPeter Avalos { 13418de8d7fSPeter Avalos if (notify_pipe[0] != -1) 13518de8d7fSPeter Avalos FD_SET(notify_pipe[0], readset); 13618de8d7fSPeter Avalos } 13718de8d7fSPeter Avalos static void 13818de8d7fSPeter Avalos notify_done(fd_set *readset) 13918de8d7fSPeter Avalos { 14018de8d7fSPeter Avalos char c; 14118de8d7fSPeter Avalos 14218de8d7fSPeter Avalos if (notify_pipe[0] != -1 && FD_ISSET(notify_pipe[0], readset)) 14318de8d7fSPeter Avalos while (read(notify_pipe[0], &c, 1) != -1) 14418de8d7fSPeter Avalos debug2("notify_done: reading"); 14518de8d7fSPeter Avalos } 14618de8d7fSPeter Avalos 14718de8d7fSPeter Avalos /*ARGSUSED*/ 14818de8d7fSPeter Avalos static void 14918de8d7fSPeter Avalos sigchld_handler(int sig) 15018de8d7fSPeter Avalos { 15118de8d7fSPeter Avalos int save_errno = errno; 15218de8d7fSPeter Avalos child_terminated = 1; 15318de8d7fSPeter Avalos #ifndef _UNICOS 15418de8d7fSPeter Avalos mysignal(SIGCHLD, sigchld_handler); 15518de8d7fSPeter Avalos #endif 15618de8d7fSPeter Avalos notify_parent(); 15718de8d7fSPeter Avalos errno = save_errno; 15818de8d7fSPeter Avalos } 15918de8d7fSPeter Avalos 16018de8d7fSPeter Avalos /*ARGSUSED*/ 16118de8d7fSPeter Avalos static void 16218de8d7fSPeter Avalos sigterm_handler(int sig) 16318de8d7fSPeter Avalos { 16418de8d7fSPeter Avalos received_sigterm = sig; 16518de8d7fSPeter Avalos } 16618de8d7fSPeter Avalos 16718de8d7fSPeter Avalos static void 168*ce74bacaSMatthew Dillon client_alive_check(struct ssh *ssh) 16918de8d7fSPeter Avalos { 17018de8d7fSPeter Avalos int channel_id; 17118de8d7fSPeter Avalos 17218de8d7fSPeter Avalos /* timeout, check to see how many we have had */ 17340c002afSPeter Avalos if (packet_inc_alive_timeouts() > options.client_alive_count_max) { 17418de8d7fSPeter Avalos logit("Timeout, client not responding."); 17518de8d7fSPeter Avalos cleanup_exit(255); 17618de8d7fSPeter Avalos } 17718de8d7fSPeter Avalos 17818de8d7fSPeter Avalos /* 17918de8d7fSPeter Avalos * send a bogus global/channel request with "wantreply", 18018de8d7fSPeter Avalos * we should get back a failure 18118de8d7fSPeter Avalos */ 182*ce74bacaSMatthew Dillon if ((channel_id = channel_find_open(ssh)) == -1) { 18318de8d7fSPeter Avalos packet_start(SSH2_MSG_GLOBAL_REQUEST); 18418de8d7fSPeter Avalos packet_put_cstring("keepalive@openssh.com"); 18518de8d7fSPeter Avalos packet_put_char(1); /* boolean: want reply */ 18618de8d7fSPeter Avalos } else { 187*ce74bacaSMatthew Dillon channel_request_start(ssh, channel_id, 188*ce74bacaSMatthew Dillon "keepalive@openssh.com", 1); 18918de8d7fSPeter Avalos } 19018de8d7fSPeter Avalos packet_send(); 19118de8d7fSPeter Avalos } 19218de8d7fSPeter Avalos 19318de8d7fSPeter Avalos /* 19418de8d7fSPeter Avalos * Sleep in select() until we can do something. This will initialize the 19518de8d7fSPeter Avalos * select masks. Upon return, the masks will indicate which descriptors 19618de8d7fSPeter Avalos * have data or can accept data. Optionally, a maximum time can be specified 19718de8d7fSPeter Avalos * for the duration of the wait (0 = infinite). 19818de8d7fSPeter Avalos */ 19918de8d7fSPeter Avalos static void 200*ce74bacaSMatthew Dillon wait_until_can_do_something(struct ssh *ssh, 201*ce74bacaSMatthew Dillon int connection_in, int connection_out, 202*ce74bacaSMatthew Dillon fd_set **readsetp, fd_set **writesetp, int *maxfdp, 203e9778795SPeter Avalos u_int *nallocp, u_int64_t max_time_ms) 20418de8d7fSPeter Avalos { 20518de8d7fSPeter Avalos struct timeval tv, *tvp; 20618de8d7fSPeter Avalos int ret; 20799e85e0dSPeter Avalos time_t minwait_secs = 0; 20818de8d7fSPeter Avalos int client_alive_scheduled = 0; 209*ce74bacaSMatthew Dillon static time_t last_client_time; 21018de8d7fSPeter Avalos 21199e85e0dSPeter Avalos /* Allocate and update select() masks for channel descriptors. */ 212*ce74bacaSMatthew Dillon channel_prepare_select(ssh, readsetp, writesetp, maxfdp, 213*ce74bacaSMatthew Dillon nallocp, &minwait_secs); 21499e85e0dSPeter Avalos 215e9778795SPeter Avalos /* XXX need proper deadline system for rekey/client alive */ 21699e85e0dSPeter Avalos if (minwait_secs != 0) 217*ce74bacaSMatthew Dillon max_time_ms = MINIMUM(max_time_ms, (u_int)minwait_secs * 1000); 21899e85e0dSPeter Avalos 21918de8d7fSPeter Avalos /* 22018de8d7fSPeter Avalos * if using client_alive, set the max timeout accordingly, 22118de8d7fSPeter Avalos * and indicate that this particular timeout was for client 22218de8d7fSPeter Avalos * alive by setting the client_alive_scheduled flag. 22318de8d7fSPeter Avalos * 22418de8d7fSPeter Avalos * this could be randomized somewhat to make traffic 22518de8d7fSPeter Avalos * analysis more difficult, but we're not doing it yet. 22618de8d7fSPeter Avalos */ 227*ce74bacaSMatthew Dillon if (options.client_alive_interval) { 228e9778795SPeter Avalos uint64_t keepalive_ms = 229e9778795SPeter Avalos (uint64_t)options.client_alive_interval * 1000; 230e9778795SPeter Avalos 23118de8d7fSPeter Avalos client_alive_scheduled = 1; 232e9778795SPeter Avalos if (max_time_ms == 0 || max_time_ms > keepalive_ms) 233e9778795SPeter Avalos max_time_ms = keepalive_ms; 23418de8d7fSPeter Avalos } 23518de8d7fSPeter Avalos 23618de8d7fSPeter Avalos #if 0 23718de8d7fSPeter Avalos /* wrong: bad condition XXX */ 23818de8d7fSPeter Avalos if (channel_not_very_much_buffered_data()) 23918de8d7fSPeter Avalos #endif 24018de8d7fSPeter Avalos FD_SET(connection_in, *readsetp); 24118de8d7fSPeter Avalos notify_prepare(*readsetp); 24218de8d7fSPeter Avalos 24318de8d7fSPeter Avalos /* 24418de8d7fSPeter Avalos * If we have buffered packet data going to the client, mark that 24518de8d7fSPeter Avalos * descriptor. 24618de8d7fSPeter Avalos */ 24718de8d7fSPeter Avalos if (packet_have_data_to_write()) 24818de8d7fSPeter Avalos FD_SET(connection_out, *writesetp); 24918de8d7fSPeter Avalos 25018de8d7fSPeter Avalos /* 25118de8d7fSPeter Avalos * If child has terminated and there is enough buffer space to read 25218de8d7fSPeter Avalos * from it, then read as much as is available and exit. 25318de8d7fSPeter Avalos */ 25418de8d7fSPeter Avalos if (child_terminated && packet_not_very_much_data_to_write()) 255e9778795SPeter Avalos if (max_time_ms == 0 || client_alive_scheduled) 256e9778795SPeter Avalos max_time_ms = 100; 25718de8d7fSPeter Avalos 258e9778795SPeter Avalos if (max_time_ms == 0) 25918de8d7fSPeter Avalos tvp = NULL; 26018de8d7fSPeter Avalos else { 261e9778795SPeter Avalos tv.tv_sec = max_time_ms / 1000; 262e9778795SPeter Avalos tv.tv_usec = 1000 * (max_time_ms % 1000); 26318de8d7fSPeter Avalos tvp = &tv; 26418de8d7fSPeter Avalos } 26518de8d7fSPeter Avalos 26618de8d7fSPeter Avalos /* Wait for something to happen, or the timeout to expire. */ 26718de8d7fSPeter Avalos ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp); 26818de8d7fSPeter Avalos 26918de8d7fSPeter Avalos if (ret == -1) { 27018de8d7fSPeter Avalos memset(*readsetp, 0, *nallocp); 27118de8d7fSPeter Avalos memset(*writesetp, 0, *nallocp); 27218de8d7fSPeter Avalos if (errno != EINTR) 27318de8d7fSPeter Avalos error("select: %.100s", strerror(errno)); 274*ce74bacaSMatthew Dillon } else if (client_alive_scheduled) { 275*ce74bacaSMatthew Dillon time_t now = monotime(); 276*ce74bacaSMatthew Dillon 277*ce74bacaSMatthew Dillon if (ret == 0) { /* timeout */ 278*ce74bacaSMatthew Dillon client_alive_check(ssh); 279*ce74bacaSMatthew Dillon } else if (FD_ISSET(connection_in, *readsetp)) { 280*ce74bacaSMatthew Dillon last_client_time = now; 281*ce74bacaSMatthew Dillon } else if (last_client_time != 0 && last_client_time + 282*ce74bacaSMatthew Dillon options.client_alive_interval <= now) { 283*ce74bacaSMatthew Dillon client_alive_check(ssh); 284*ce74bacaSMatthew Dillon last_client_time = now; 28518de8d7fSPeter Avalos } 28618de8d7fSPeter Avalos } 28718de8d7fSPeter Avalos 28818de8d7fSPeter Avalos notify_done(*readsetp); 28918de8d7fSPeter Avalos } 29018de8d7fSPeter Avalos 29118de8d7fSPeter Avalos /* 29218de8d7fSPeter Avalos * Processes input from the client and the program. Input data is stored 29318de8d7fSPeter Avalos * in buffers and processed later. 29418de8d7fSPeter Avalos */ 295*ce74bacaSMatthew Dillon static int 296*ce74bacaSMatthew Dillon process_input(struct ssh *ssh, fd_set *readset, int connection_in) 29718de8d7fSPeter Avalos { 29818de8d7fSPeter Avalos int len; 29918de8d7fSPeter Avalos char buf[16384]; 30018de8d7fSPeter Avalos 30118de8d7fSPeter Avalos /* Read and buffer any input data from the client. */ 30218de8d7fSPeter Avalos if (FD_ISSET(connection_in, readset)) { 303e9778795SPeter Avalos len = read(connection_in, buf, sizeof(buf)); 30418de8d7fSPeter Avalos if (len == 0) { 305e9778795SPeter Avalos verbose("Connection closed by %.100s port %d", 306e9778795SPeter Avalos ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); 307*ce74bacaSMatthew Dillon return -1; 30818de8d7fSPeter Avalos } else if (len < 0) { 30918de8d7fSPeter Avalos if (errno != EINTR && errno != EAGAIN && 31018de8d7fSPeter Avalos errno != EWOULDBLOCK) { 31118de8d7fSPeter Avalos verbose("Read error from remote host " 312e9778795SPeter Avalos "%.100s port %d: %.100s", 313e9778795SPeter Avalos ssh_remote_ipaddr(ssh), 314e9778795SPeter Avalos ssh_remote_port(ssh), strerror(errno)); 31518de8d7fSPeter Avalos cleanup_exit(255); 31618de8d7fSPeter Avalos } 31718de8d7fSPeter Avalos } else { 31818de8d7fSPeter Avalos /* Buffer any received data. */ 31918de8d7fSPeter Avalos packet_process_incoming(buf, len); 32018de8d7fSPeter Avalos } 32118de8d7fSPeter Avalos } 322*ce74bacaSMatthew Dillon return 0; 32318de8d7fSPeter Avalos } 32418de8d7fSPeter Avalos 32518de8d7fSPeter Avalos /* 32618de8d7fSPeter Avalos * Sends data from internal buffers to client program stdin. 32718de8d7fSPeter Avalos */ 32818de8d7fSPeter Avalos static void 329*ce74bacaSMatthew Dillon process_output(fd_set *writeset, int connection_out) 33018de8d7fSPeter Avalos { 33118de8d7fSPeter Avalos /* Send any buffered packet data to the client. */ 33218de8d7fSPeter Avalos if (FD_ISSET(connection_out, writeset)) 33318de8d7fSPeter Avalos packet_write_poll(); 33418de8d7fSPeter Avalos } 33518de8d7fSPeter Avalos 33618de8d7fSPeter Avalos static void 337*ce74bacaSMatthew Dillon process_buffered_input_packets(struct ssh *ssh) 33818de8d7fSPeter Avalos { 339*ce74bacaSMatthew Dillon ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, NULL); 34018de8d7fSPeter Avalos } 34118de8d7fSPeter Avalos 34218de8d7fSPeter Avalos static void 343*ce74bacaSMatthew Dillon collect_children(struct ssh *ssh) 34418de8d7fSPeter Avalos { 34518de8d7fSPeter Avalos pid_t pid; 34618de8d7fSPeter Avalos sigset_t oset, nset; 34718de8d7fSPeter Avalos int status; 34818de8d7fSPeter Avalos 34918de8d7fSPeter Avalos /* block SIGCHLD while we check for dead children */ 35018de8d7fSPeter Avalos sigemptyset(&nset); 35118de8d7fSPeter Avalos sigaddset(&nset, SIGCHLD); 35218de8d7fSPeter Avalos sigprocmask(SIG_BLOCK, &nset, &oset); 35318de8d7fSPeter Avalos if (child_terminated) { 35418de8d7fSPeter Avalos debug("Received SIGCHLD."); 35518de8d7fSPeter Avalos while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || 35618de8d7fSPeter Avalos (pid < 0 && errno == EINTR)) 35718de8d7fSPeter Avalos if (pid > 0) 358*ce74bacaSMatthew Dillon session_close_by_pid(ssh, pid, status); 35918de8d7fSPeter Avalos child_terminated = 0; 36018de8d7fSPeter Avalos } 36118de8d7fSPeter Avalos sigprocmask(SIG_SETMASK, &oset, NULL); 36218de8d7fSPeter Avalos } 36318de8d7fSPeter Avalos 36418de8d7fSPeter Avalos void 365*ce74bacaSMatthew Dillon server_loop2(struct ssh *ssh, Authctxt *authctxt) 36618de8d7fSPeter Avalos { 36718de8d7fSPeter Avalos fd_set *readset = NULL, *writeset = NULL; 368e9778795SPeter Avalos int max_fd; 369*ce74bacaSMatthew Dillon u_int nalloc = 0, connection_in, connection_out; 37036e94dc5SPeter Avalos u_int64_t rekey_timeout_ms = 0; 37118de8d7fSPeter Avalos 37218de8d7fSPeter Avalos debug("Entering interactive session for SSH2."); 37318de8d7fSPeter Avalos 37418de8d7fSPeter Avalos mysignal(SIGCHLD, sigchld_handler); 37518de8d7fSPeter Avalos child_terminated = 0; 37618de8d7fSPeter Avalos connection_in = packet_get_connection_in(); 37718de8d7fSPeter Avalos connection_out = packet_get_connection_out(); 37818de8d7fSPeter Avalos 37918de8d7fSPeter Avalos if (!use_privsep) { 38018de8d7fSPeter Avalos signal(SIGTERM, sigterm_handler); 38118de8d7fSPeter Avalos signal(SIGINT, sigterm_handler); 38218de8d7fSPeter Avalos signal(SIGQUIT, sigterm_handler); 38318de8d7fSPeter Avalos } 38418de8d7fSPeter Avalos 38518de8d7fSPeter Avalos notify_setup(); 38618de8d7fSPeter Avalos 387*ce74bacaSMatthew Dillon max_fd = MAXIMUM(connection_in, connection_out); 388*ce74bacaSMatthew Dillon max_fd = MAXIMUM(max_fd, notify_pipe[0]); 38918de8d7fSPeter Avalos 39018de8d7fSPeter Avalos server_init_dispatch(); 39118de8d7fSPeter Avalos 39218de8d7fSPeter Avalos for (;;) { 393*ce74bacaSMatthew Dillon process_buffered_input_packets(ssh); 39418de8d7fSPeter Avalos 395*ce74bacaSMatthew Dillon if (!ssh_packet_is_rekeying(ssh) && 396e9778795SPeter Avalos packet_not_very_much_data_to_write()) 397*ce74bacaSMatthew Dillon channel_output_poll(ssh); 398*ce74bacaSMatthew Dillon if (options.rekey_interval > 0 && !ssh_packet_is_rekeying(ssh)) 39936e94dc5SPeter Avalos rekey_timeout_ms = packet_get_rekey_timeout() * 1000; 40036e94dc5SPeter Avalos else 40136e94dc5SPeter Avalos rekey_timeout_ms = 0; 40236e94dc5SPeter Avalos 403*ce74bacaSMatthew Dillon wait_until_can_do_something(ssh, connection_in, connection_out, 404*ce74bacaSMatthew Dillon &readset, &writeset, &max_fd, &nalloc, rekey_timeout_ms); 40518de8d7fSPeter Avalos 40618de8d7fSPeter Avalos if (received_sigterm) { 40736e94dc5SPeter Avalos logit("Exiting on signal %d", (int)received_sigterm); 40818de8d7fSPeter Avalos /* Clean up sessions, utmp, etc. */ 40918de8d7fSPeter Avalos cleanup_exit(255); 41018de8d7fSPeter Avalos } 41118de8d7fSPeter Avalos 412*ce74bacaSMatthew Dillon collect_children(ssh); 413*ce74bacaSMatthew Dillon if (!ssh_packet_is_rekeying(ssh)) 414*ce74bacaSMatthew Dillon channel_after_select(ssh, readset, writeset); 415*ce74bacaSMatthew Dillon if (process_input(ssh, readset, connection_in) < 0) 41618de8d7fSPeter Avalos break; 417*ce74bacaSMatthew Dillon process_output(writeset, connection_out); 41818de8d7fSPeter Avalos } 419*ce74bacaSMatthew Dillon collect_children(ssh); 42018de8d7fSPeter Avalos 42136e94dc5SPeter Avalos free(readset); 42236e94dc5SPeter Avalos free(writeset); 42318de8d7fSPeter Avalos 42418de8d7fSPeter Avalos /* free all channels, no more reads and writes */ 425*ce74bacaSMatthew Dillon channel_free_all(ssh); 42618de8d7fSPeter Avalos 42718de8d7fSPeter Avalos /* free remaining sessions, e.g. remove wtmp entries */ 428*ce74bacaSMatthew Dillon session_destroy_all(ssh, NULL); 42918de8d7fSPeter Avalos } 43018de8d7fSPeter Avalos 431e9778795SPeter Avalos static int 432*ce74bacaSMatthew Dillon server_input_keep_alive(int type, u_int32_t seq, struct ssh *ssh) 43318de8d7fSPeter Avalos { 43418de8d7fSPeter Avalos debug("Got %d/%u for keepalive", type, seq); 43518de8d7fSPeter Avalos /* 43618de8d7fSPeter Avalos * reset timeout, since we got a sane answer from the client. 43718de8d7fSPeter Avalos * even if this was generated by something other than 43818de8d7fSPeter Avalos * the bogus CHANNEL_REQUEST we send for keepalives. 43918de8d7fSPeter Avalos */ 44040c002afSPeter Avalos packet_set_alive_timeouts(0); 441e9778795SPeter Avalos return 0; 44218de8d7fSPeter Avalos } 44318de8d7fSPeter Avalos 44418de8d7fSPeter Avalos static Channel * 445*ce74bacaSMatthew Dillon server_request_direct_tcpip(struct ssh *ssh, int *reason, const char **errmsg) 44618de8d7fSPeter Avalos { 44736e94dc5SPeter Avalos Channel *c = NULL; 44818de8d7fSPeter Avalos char *target, *originator; 449cb5eb4f1SPeter Avalos u_short target_port, originator_port; 45018de8d7fSPeter Avalos 45118de8d7fSPeter Avalos target = packet_get_string(NULL); 45218de8d7fSPeter Avalos target_port = packet_get_int(); 45318de8d7fSPeter Avalos originator = packet_get_string(NULL); 45418de8d7fSPeter Avalos originator_port = packet_get_int(); 45518de8d7fSPeter Avalos packet_check_eom(); 45618de8d7fSPeter Avalos 45718de8d7fSPeter Avalos debug("server_request_direct_tcpip: originator %s port %d, target %s " 45818de8d7fSPeter Avalos "port %d", originator, originator_port, target, target_port); 45918de8d7fSPeter Avalos 46036e94dc5SPeter Avalos /* XXX fine grained permissions */ 46136e94dc5SPeter Avalos if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 && 462*ce74bacaSMatthew Dillon !no_port_forwarding_flag && !options.disable_forwarding) { 463*ce74bacaSMatthew Dillon c = channel_connect_to_port(ssh, target, target_port, 464*ce74bacaSMatthew Dillon "direct-tcpip", "direct-tcpip", reason, errmsg); 46536e94dc5SPeter Avalos } else { 46636e94dc5SPeter Avalos logit("refused local port forward: " 46736e94dc5SPeter Avalos "originator %s port %d, target %s port %d", 46836e94dc5SPeter Avalos originator, originator_port, target, target_port); 469*ce74bacaSMatthew Dillon if (reason != NULL) 470*ce74bacaSMatthew Dillon *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; 47136e94dc5SPeter Avalos } 47218de8d7fSPeter Avalos 47336e94dc5SPeter Avalos free(originator); 47436e94dc5SPeter Avalos free(target); 47536e94dc5SPeter Avalos 47636e94dc5SPeter Avalos return c; 47736e94dc5SPeter Avalos } 47836e94dc5SPeter Avalos 47936e94dc5SPeter Avalos static Channel * 480*ce74bacaSMatthew Dillon server_request_direct_streamlocal(struct ssh *ssh) 48136e94dc5SPeter Avalos { 48236e94dc5SPeter Avalos Channel *c = NULL; 48336e94dc5SPeter Avalos char *target, *originator; 48436e94dc5SPeter Avalos u_short originator_port; 485*ce74bacaSMatthew Dillon struct passwd *pw = the_authctxt->pw; 486*ce74bacaSMatthew Dillon 487*ce74bacaSMatthew Dillon if (pw == NULL || !the_authctxt->valid) 488*ce74bacaSMatthew Dillon fatal("server_input_global_request: no/invalid user"); 48936e94dc5SPeter Avalos 49036e94dc5SPeter Avalos target = packet_get_string(NULL); 49136e94dc5SPeter Avalos originator = packet_get_string(NULL); 49236e94dc5SPeter Avalos originator_port = packet_get_int(); 49336e94dc5SPeter Avalos packet_check_eom(); 49436e94dc5SPeter Avalos 49536e94dc5SPeter Avalos debug("server_request_direct_streamlocal: originator %s port %d, target %s", 49636e94dc5SPeter Avalos originator, originator_port, target); 49736e94dc5SPeter Avalos 49836e94dc5SPeter Avalos /* XXX fine grained permissions */ 49936e94dc5SPeter Avalos if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 && 500*ce74bacaSMatthew Dillon !no_port_forwarding_flag && !options.disable_forwarding && 501*ce74bacaSMatthew Dillon (pw->pw_uid == 0 || use_privsep)) { 502*ce74bacaSMatthew Dillon c = channel_connect_to_path(ssh, target, 50336e94dc5SPeter Avalos "direct-streamlocal@openssh.com", "direct-streamlocal"); 50436e94dc5SPeter Avalos } else { 50536e94dc5SPeter Avalos logit("refused streamlocal port forward: " 50636e94dc5SPeter Avalos "originator %s port %d, target %s", 50736e94dc5SPeter Avalos originator, originator_port, target); 50836e94dc5SPeter Avalos } 50936e94dc5SPeter Avalos 51036e94dc5SPeter Avalos free(originator); 51136e94dc5SPeter Avalos free(target); 51218de8d7fSPeter Avalos 51318de8d7fSPeter Avalos return c; 51418de8d7fSPeter Avalos } 51518de8d7fSPeter Avalos 51618de8d7fSPeter Avalos static Channel * 517*ce74bacaSMatthew Dillon server_request_tun(struct ssh *ssh) 51818de8d7fSPeter Avalos { 51918de8d7fSPeter Avalos Channel *c = NULL; 52018de8d7fSPeter Avalos int mode, tun; 52118de8d7fSPeter Avalos int sock; 52218de8d7fSPeter Avalos 52318de8d7fSPeter Avalos mode = packet_get_int(); 52418de8d7fSPeter Avalos switch (mode) { 52518de8d7fSPeter Avalos case SSH_TUNMODE_POINTOPOINT: 52618de8d7fSPeter Avalos case SSH_TUNMODE_ETHERNET: 52718de8d7fSPeter Avalos break; 52818de8d7fSPeter Avalos default: 52918de8d7fSPeter Avalos packet_send_debug("Unsupported tunnel device mode."); 53018de8d7fSPeter Avalos return NULL; 53118de8d7fSPeter Avalos } 53218de8d7fSPeter Avalos if ((options.permit_tun & mode) == 0) { 53318de8d7fSPeter Avalos packet_send_debug("Server has rejected tunnel device " 53418de8d7fSPeter Avalos "forwarding"); 53518de8d7fSPeter Avalos return NULL; 53618de8d7fSPeter Avalos } 53718de8d7fSPeter Avalos 53818de8d7fSPeter Avalos tun = packet_get_int(); 53918de8d7fSPeter Avalos if (forced_tun_device != -1) { 54018de8d7fSPeter Avalos if (tun != SSH_TUNID_ANY && forced_tun_device != tun) 54118de8d7fSPeter Avalos goto done; 54218de8d7fSPeter Avalos tun = forced_tun_device; 54318de8d7fSPeter Avalos } 54418de8d7fSPeter Avalos sock = tun_open(tun, mode); 54518de8d7fSPeter Avalos if (sock < 0) 54618de8d7fSPeter Avalos goto done; 547*ce74bacaSMatthew Dillon c = channel_new(ssh, "tun", SSH_CHANNEL_OPEN, sock, sock, -1, 54818de8d7fSPeter Avalos CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); 54918de8d7fSPeter Avalos c->datagram = 1; 55018de8d7fSPeter Avalos #if defined(SSH_TUN_FILTER) 55118de8d7fSPeter Avalos if (mode == SSH_TUNMODE_POINTOPOINT) 552*ce74bacaSMatthew Dillon channel_register_filter(ssh, c->self, sys_tun_infilter, 55318de8d7fSPeter Avalos sys_tun_outfilter, NULL, NULL); 55418de8d7fSPeter Avalos #endif 55518de8d7fSPeter Avalos 55618de8d7fSPeter Avalos done: 55718de8d7fSPeter Avalos if (c == NULL) 55818de8d7fSPeter Avalos packet_send_debug("Failed to open the tunnel device."); 55918de8d7fSPeter Avalos return c; 56018de8d7fSPeter Avalos } 56118de8d7fSPeter Avalos 56218de8d7fSPeter Avalos static Channel * 563*ce74bacaSMatthew Dillon server_request_session(struct ssh *ssh) 56418de8d7fSPeter Avalos { 56518de8d7fSPeter Avalos Channel *c; 56618de8d7fSPeter Avalos 56718de8d7fSPeter Avalos debug("input_session_request"); 56818de8d7fSPeter Avalos packet_check_eom(); 56918de8d7fSPeter Avalos 57018de8d7fSPeter Avalos if (no_more_sessions) { 57118de8d7fSPeter Avalos packet_disconnect("Possible attack: attempt to open a session " 57218de8d7fSPeter Avalos "after additional sessions disabled"); 57318de8d7fSPeter Avalos } 57418de8d7fSPeter Avalos 57518de8d7fSPeter Avalos /* 57618de8d7fSPeter Avalos * A server session has no fd to read or write until a 57718de8d7fSPeter Avalos * CHANNEL_REQUEST for a shell is made, so we set the type to 57818de8d7fSPeter Avalos * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all 57918de8d7fSPeter Avalos * CHANNEL_REQUEST messages is registered. 58018de8d7fSPeter Avalos */ 581*ce74bacaSMatthew Dillon c = channel_new(ssh, "session", SSH_CHANNEL_LARVAL, 58218de8d7fSPeter Avalos -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT, 58318de8d7fSPeter Avalos 0, "server-session", 1); 58418de8d7fSPeter Avalos if (session_open(the_authctxt, c->self) != 1) { 58518de8d7fSPeter Avalos debug("session open failed, free channel %d", c->self); 586*ce74bacaSMatthew Dillon channel_free(ssh, c); 58718de8d7fSPeter Avalos return NULL; 58818de8d7fSPeter Avalos } 589*ce74bacaSMatthew Dillon channel_register_cleanup(ssh, c->self, session_close_by_channel, 0); 59018de8d7fSPeter Avalos return c; 59118de8d7fSPeter Avalos } 59218de8d7fSPeter Avalos 593e9778795SPeter Avalos static int 594*ce74bacaSMatthew Dillon server_input_channel_open(int type, u_int32_t seq, struct ssh *ssh) 59518de8d7fSPeter Avalos { 59618de8d7fSPeter Avalos Channel *c = NULL; 59718de8d7fSPeter Avalos char *ctype; 598*ce74bacaSMatthew Dillon const char *errmsg = NULL; 599*ce74bacaSMatthew Dillon int rchan, reason = SSH2_OPEN_CONNECT_FAILED; 60018de8d7fSPeter Avalos u_int rmaxpack, rwindow, len; 60118de8d7fSPeter Avalos 60218de8d7fSPeter Avalos ctype = packet_get_string(&len); 60318de8d7fSPeter Avalos rchan = packet_get_int(); 60418de8d7fSPeter Avalos rwindow = packet_get_int(); 60518de8d7fSPeter Avalos rmaxpack = packet_get_int(); 60618de8d7fSPeter Avalos 60718de8d7fSPeter Avalos debug("server_input_channel_open: ctype %s rchan %d win %d max %d", 60818de8d7fSPeter Avalos ctype, rchan, rwindow, rmaxpack); 60918de8d7fSPeter Avalos 61018de8d7fSPeter Avalos if (strcmp(ctype, "session") == 0) { 611*ce74bacaSMatthew Dillon c = server_request_session(ssh); 61218de8d7fSPeter Avalos } else if (strcmp(ctype, "direct-tcpip") == 0) { 613*ce74bacaSMatthew Dillon c = server_request_direct_tcpip(ssh, &reason, &errmsg); 61436e94dc5SPeter Avalos } else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) { 615*ce74bacaSMatthew Dillon c = server_request_direct_streamlocal(ssh); 61618de8d7fSPeter Avalos } else if (strcmp(ctype, "tun@openssh.com") == 0) { 617*ce74bacaSMatthew Dillon c = server_request_tun(ssh); 61818de8d7fSPeter Avalos } 61918de8d7fSPeter Avalos if (c != NULL) { 62018de8d7fSPeter Avalos debug("server_input_channel_open: confirm %s", ctype); 62118de8d7fSPeter Avalos c->remote_id = rchan; 622*ce74bacaSMatthew Dillon c->have_remote_id = 1; 62318de8d7fSPeter Avalos c->remote_window = rwindow; 62418de8d7fSPeter Avalos c->remote_maxpacket = rmaxpack; 62518de8d7fSPeter Avalos if (c->type != SSH_CHANNEL_CONNECTING) { 62618de8d7fSPeter Avalos packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); 62718de8d7fSPeter Avalos packet_put_int(c->remote_id); 62818de8d7fSPeter Avalos packet_put_int(c->self); 62918de8d7fSPeter Avalos packet_put_int(c->local_window); 63018de8d7fSPeter Avalos packet_put_int(c->local_maxpacket); 63118de8d7fSPeter Avalos packet_send(); 63218de8d7fSPeter Avalos } 63318de8d7fSPeter Avalos } else { 63418de8d7fSPeter Avalos debug("server_input_channel_open: failure %s", ctype); 63518de8d7fSPeter Avalos packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); 63618de8d7fSPeter Avalos packet_put_int(rchan); 637*ce74bacaSMatthew Dillon packet_put_int(reason); 63818de8d7fSPeter Avalos if (!(datafellows & SSH_BUG_OPENFAILURE)) { 639*ce74bacaSMatthew Dillon packet_put_cstring(errmsg ? errmsg : "open failed"); 64018de8d7fSPeter Avalos packet_put_cstring(""); 64118de8d7fSPeter Avalos } 64218de8d7fSPeter Avalos packet_send(); 64318de8d7fSPeter Avalos } 64436e94dc5SPeter Avalos free(ctype); 645e9778795SPeter Avalos return 0; 64618de8d7fSPeter Avalos } 64718de8d7fSPeter Avalos 648e9778795SPeter Avalos static int 649*ce74bacaSMatthew Dillon server_input_hostkeys_prove(struct ssh *ssh, struct sshbuf **respp) 650e9778795SPeter Avalos { 651e9778795SPeter Avalos struct sshbuf *resp = NULL; 652e9778795SPeter Avalos struct sshbuf *sigbuf = NULL; 653e9778795SPeter Avalos struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL; 654e9778795SPeter Avalos int r, ndx, success = 0; 655e9778795SPeter Avalos const u_char *blob; 656e9778795SPeter Avalos u_char *sig = 0; 657e9778795SPeter Avalos size_t blen, slen; 658e9778795SPeter Avalos 659e9778795SPeter Avalos if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL) 660e9778795SPeter Avalos fatal("%s: sshbuf_new", __func__); 661e9778795SPeter Avalos 662e9778795SPeter Avalos while (ssh_packet_remaining(ssh) > 0) { 663e9778795SPeter Avalos sshkey_free(key); 664e9778795SPeter Avalos key = NULL; 665e9778795SPeter Avalos if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 || 666e9778795SPeter Avalos (r = sshkey_from_blob(blob, blen, &key)) != 0) { 667e9778795SPeter Avalos error("%s: couldn't parse key: %s", 668e9778795SPeter Avalos __func__, ssh_err(r)); 669e9778795SPeter Avalos goto out; 670e9778795SPeter Avalos } 671e9778795SPeter Avalos /* 672e9778795SPeter Avalos * Better check that this is actually one of our hostkeys 673e9778795SPeter Avalos * before attempting to sign anything with it. 674e9778795SPeter Avalos */ 675e9778795SPeter Avalos if ((ndx = ssh->kex->host_key_index(key, 1, ssh)) == -1) { 676e9778795SPeter Avalos error("%s: unknown host %s key", 677e9778795SPeter Avalos __func__, sshkey_type(key)); 678e9778795SPeter Avalos goto out; 679e9778795SPeter Avalos } 680e9778795SPeter Avalos /* 681e9778795SPeter Avalos * XXX refactor: make kex->sign just use an index rather 682e9778795SPeter Avalos * than passing in public and private keys 683e9778795SPeter Avalos */ 684e9778795SPeter Avalos if ((key_prv = get_hostkey_by_index(ndx)) == NULL && 685e9778795SPeter Avalos (key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) { 686e9778795SPeter Avalos error("%s: can't retrieve hostkey %d", __func__, ndx); 687e9778795SPeter Avalos goto out; 688e9778795SPeter Avalos } 689e9778795SPeter Avalos sshbuf_reset(sigbuf); 690e9778795SPeter Avalos free(sig); 691e9778795SPeter Avalos sig = NULL; 692e9778795SPeter Avalos if ((r = sshbuf_put_cstring(sigbuf, 693e9778795SPeter Avalos "hostkeys-prove-00@openssh.com")) != 0 || 694e9778795SPeter Avalos (r = sshbuf_put_string(sigbuf, 695e9778795SPeter Avalos ssh->kex->session_id, ssh->kex->session_id_len)) != 0 || 696e9778795SPeter Avalos (r = sshkey_puts(key, sigbuf)) != 0 || 697e9778795SPeter Avalos (r = ssh->kex->sign(key_prv, key_pub, &sig, &slen, 698e9778795SPeter Avalos sshbuf_ptr(sigbuf), sshbuf_len(sigbuf), NULL, 0)) != 0 || 699e9778795SPeter Avalos (r = sshbuf_put_string(resp, sig, slen)) != 0) { 700e9778795SPeter Avalos error("%s: couldn't prepare signature: %s", 701e9778795SPeter Avalos __func__, ssh_err(r)); 702e9778795SPeter Avalos goto out; 703e9778795SPeter Avalos } 704e9778795SPeter Avalos } 705e9778795SPeter Avalos /* Success */ 706e9778795SPeter Avalos *respp = resp; 707e9778795SPeter Avalos resp = NULL; /* don't free it */ 708e9778795SPeter Avalos success = 1; 709e9778795SPeter Avalos out: 710e9778795SPeter Avalos free(sig); 711e9778795SPeter Avalos sshbuf_free(resp); 712e9778795SPeter Avalos sshbuf_free(sigbuf); 713e9778795SPeter Avalos sshkey_free(key); 714e9778795SPeter Avalos return success; 715e9778795SPeter Avalos } 716e9778795SPeter Avalos 717e9778795SPeter Avalos static int 718*ce74bacaSMatthew Dillon server_input_global_request(int type, u_int32_t seq, struct ssh *ssh) 71918de8d7fSPeter Avalos { 72018de8d7fSPeter Avalos char *rtype; 72118de8d7fSPeter Avalos int want_reply; 722e9778795SPeter Avalos int r, success = 0, allocated_listen_port = 0; 723e9778795SPeter Avalos struct sshbuf *resp = NULL; 724*ce74bacaSMatthew Dillon struct passwd *pw = the_authctxt->pw; 725*ce74bacaSMatthew Dillon 726*ce74bacaSMatthew Dillon if (pw == NULL || !the_authctxt->valid) 727*ce74bacaSMatthew Dillon fatal("server_input_global_request: no/invalid user"); 72818de8d7fSPeter Avalos 72918de8d7fSPeter Avalos rtype = packet_get_string(NULL); 73018de8d7fSPeter Avalos want_reply = packet_get_char(); 73118de8d7fSPeter Avalos debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply); 73218de8d7fSPeter Avalos 73318de8d7fSPeter Avalos /* -R style forwarding */ 73418de8d7fSPeter Avalos if (strcmp(rtype, "tcpip-forward") == 0) { 73536e94dc5SPeter Avalos struct Forward fwd; 73618de8d7fSPeter Avalos 73736e94dc5SPeter Avalos memset(&fwd, 0, sizeof(fwd)); 73836e94dc5SPeter Avalos fwd.listen_host = packet_get_string(NULL); 73936e94dc5SPeter Avalos fwd.listen_port = (u_short)packet_get_int(); 74018de8d7fSPeter Avalos debug("server_input_global_request: tcpip-forward listen %s port %d", 74136e94dc5SPeter Avalos fwd.listen_host, fwd.listen_port); 74218de8d7fSPeter Avalos 74318de8d7fSPeter Avalos /* check permissions */ 74436e94dc5SPeter Avalos if ((options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 || 745*ce74bacaSMatthew Dillon no_port_forwarding_flag || options.disable_forwarding || 746e9778795SPeter Avalos (!want_reply && fwd.listen_port == 0) || 747*ce74bacaSMatthew Dillon (fwd.listen_port != 0 && 748*ce74bacaSMatthew Dillon !bind_permitted(fwd.listen_port, pw->pw_uid))) { 74918de8d7fSPeter Avalos success = 0; 75018de8d7fSPeter Avalos packet_send_debug("Server has disabled port forwarding."); 75118de8d7fSPeter Avalos } else { 75218de8d7fSPeter Avalos /* Start listening on the port */ 753*ce74bacaSMatthew Dillon success = channel_setup_remote_fwd_listener(ssh, &fwd, 75436e94dc5SPeter Avalos &allocated_listen_port, &options.fwd_opts); 75518de8d7fSPeter Avalos } 75636e94dc5SPeter Avalos free(fwd.listen_host); 757e9778795SPeter Avalos if ((resp = sshbuf_new()) == NULL) 758e9778795SPeter Avalos fatal("%s: sshbuf_new", __func__); 759e9778795SPeter Avalos if (allocated_listen_port != 0 && 760e9778795SPeter Avalos (r = sshbuf_put_u32(resp, allocated_listen_port)) != 0) 761e9778795SPeter Avalos fatal("%s: sshbuf_put_u32: %s", __func__, ssh_err(r)); 76218de8d7fSPeter Avalos } else if (strcmp(rtype, "cancel-tcpip-forward") == 0) { 76336e94dc5SPeter Avalos struct Forward fwd; 76418de8d7fSPeter Avalos 76536e94dc5SPeter Avalos memset(&fwd, 0, sizeof(fwd)); 76636e94dc5SPeter Avalos fwd.listen_host = packet_get_string(NULL); 76736e94dc5SPeter Avalos fwd.listen_port = (u_short)packet_get_int(); 76818de8d7fSPeter Avalos debug("%s: cancel-tcpip-forward addr %s port %d", __func__, 76936e94dc5SPeter Avalos fwd.listen_host, fwd.listen_port); 77018de8d7fSPeter Avalos 771*ce74bacaSMatthew Dillon success = channel_cancel_rport_listener(ssh, &fwd); 77236e94dc5SPeter Avalos free(fwd.listen_host); 77336e94dc5SPeter Avalos } else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) { 77436e94dc5SPeter Avalos struct Forward fwd; 77536e94dc5SPeter Avalos 77636e94dc5SPeter Avalos memset(&fwd, 0, sizeof(fwd)); 77736e94dc5SPeter Avalos fwd.listen_path = packet_get_string(NULL); 77836e94dc5SPeter Avalos debug("server_input_global_request: streamlocal-forward listen path %s", 77936e94dc5SPeter Avalos fwd.listen_path); 78036e94dc5SPeter Avalos 78136e94dc5SPeter Avalos /* check permissions */ 78236e94dc5SPeter Avalos if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0 783*ce74bacaSMatthew Dillon || no_port_forwarding_flag || options.disable_forwarding || 784*ce74bacaSMatthew Dillon (pw->pw_uid != 0 && !use_privsep)) { 78536e94dc5SPeter Avalos success = 0; 786*ce74bacaSMatthew Dillon packet_send_debug("Server has disabled " 787*ce74bacaSMatthew Dillon "streamlocal forwarding."); 78836e94dc5SPeter Avalos } else { 78936e94dc5SPeter Avalos /* Start listening on the socket */ 790*ce74bacaSMatthew Dillon success = channel_setup_remote_fwd_listener(ssh, 79136e94dc5SPeter Avalos &fwd, NULL, &options.fwd_opts); 79236e94dc5SPeter Avalos } 79336e94dc5SPeter Avalos free(fwd.listen_path); 79436e94dc5SPeter Avalos } else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) { 79536e94dc5SPeter Avalos struct Forward fwd; 79636e94dc5SPeter Avalos 79736e94dc5SPeter Avalos memset(&fwd, 0, sizeof(fwd)); 79836e94dc5SPeter Avalos fwd.listen_path = packet_get_string(NULL); 79936e94dc5SPeter Avalos debug("%s: cancel-streamlocal-forward path %s", __func__, 80036e94dc5SPeter Avalos fwd.listen_path); 80136e94dc5SPeter Avalos 802*ce74bacaSMatthew Dillon success = channel_cancel_rport_listener(ssh, &fwd); 80336e94dc5SPeter Avalos free(fwd.listen_path); 80418de8d7fSPeter Avalos } else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) { 80518de8d7fSPeter Avalos no_more_sessions = 1; 80618de8d7fSPeter Avalos success = 1; 807e9778795SPeter Avalos } else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) { 808*ce74bacaSMatthew Dillon success = server_input_hostkeys_prove(ssh, &resp); 80918de8d7fSPeter Avalos } 81018de8d7fSPeter Avalos if (want_reply) { 81118de8d7fSPeter Avalos packet_start(success ? 81218de8d7fSPeter Avalos SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); 813e9778795SPeter Avalos if (success && resp != NULL) 814*ce74bacaSMatthew Dillon ssh_packet_put_raw(ssh, sshbuf_ptr(resp), 815e9778795SPeter Avalos sshbuf_len(resp)); 81618de8d7fSPeter Avalos packet_send(); 81718de8d7fSPeter Avalos packet_write_wait(); 81818de8d7fSPeter Avalos } 81936e94dc5SPeter Avalos free(rtype); 820e9778795SPeter Avalos sshbuf_free(resp); 821e9778795SPeter Avalos return 0; 82218de8d7fSPeter Avalos } 82318de8d7fSPeter Avalos 824e9778795SPeter Avalos static int 825*ce74bacaSMatthew Dillon server_input_channel_req(int type, u_int32_t seq, struct ssh *ssh) 82618de8d7fSPeter Avalos { 82718de8d7fSPeter Avalos Channel *c; 82818de8d7fSPeter Avalos int id, reply, success = 0; 82918de8d7fSPeter Avalos char *rtype; 83018de8d7fSPeter Avalos 83118de8d7fSPeter Avalos id = packet_get_int(); 83218de8d7fSPeter Avalos rtype = packet_get_string(NULL); 83318de8d7fSPeter Avalos reply = packet_get_char(); 83418de8d7fSPeter Avalos 83518de8d7fSPeter Avalos debug("server_input_channel_req: channel %d request %s reply %d", 83618de8d7fSPeter Avalos id, rtype, reply); 83718de8d7fSPeter Avalos 838*ce74bacaSMatthew Dillon if ((c = channel_lookup(ssh, id)) == NULL) 83918de8d7fSPeter Avalos packet_disconnect("server_input_channel_req: " 84018de8d7fSPeter Avalos "unknown channel %d", id); 84118de8d7fSPeter Avalos if (!strcmp(rtype, "eow@openssh.com")) { 84218de8d7fSPeter Avalos packet_check_eom(); 843*ce74bacaSMatthew Dillon chan_rcvd_eow(ssh, c); 84418de8d7fSPeter Avalos } else if ((c->type == SSH_CHANNEL_LARVAL || 84518de8d7fSPeter Avalos c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0) 846*ce74bacaSMatthew Dillon success = session_input_channel_req(ssh, c, rtype); 84736e94dc5SPeter Avalos if (reply && !(c->flags & CHAN_CLOSE_SENT)) { 848*ce74bacaSMatthew Dillon if (!c->have_remote_id) 849*ce74bacaSMatthew Dillon fatal("%s: channel %d: no remote_id", 850*ce74bacaSMatthew Dillon __func__, c->self); 85118de8d7fSPeter Avalos packet_start(success ? 85218de8d7fSPeter Avalos SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); 85318de8d7fSPeter Avalos packet_put_int(c->remote_id); 85418de8d7fSPeter Avalos packet_send(); 85518de8d7fSPeter Avalos } 85636e94dc5SPeter Avalos free(rtype); 857e9778795SPeter Avalos return 0; 85818de8d7fSPeter Avalos } 85918de8d7fSPeter Avalos 86018de8d7fSPeter Avalos static void 861*ce74bacaSMatthew Dillon server_init_dispatch(void) 86218de8d7fSPeter Avalos { 863*ce74bacaSMatthew Dillon debug("server_init_dispatch"); 86418de8d7fSPeter Avalos dispatch_init(&dispatch_protocol_error); 86518de8d7fSPeter Avalos dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); 86618de8d7fSPeter Avalos dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); 86718de8d7fSPeter Avalos dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); 86818de8d7fSPeter Avalos dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); 86918de8d7fSPeter Avalos dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open); 87018de8d7fSPeter Avalos dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); 87118de8d7fSPeter Avalos dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); 87218de8d7fSPeter Avalos dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req); 87318de8d7fSPeter Avalos dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); 87418de8d7fSPeter Avalos dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request); 87518de8d7fSPeter Avalos /* client_alive */ 876cb5eb4f1SPeter Avalos dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive); 877cb5eb4f1SPeter Avalos dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive); 87818de8d7fSPeter Avalos dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive); 87918de8d7fSPeter Avalos dispatch_set(SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive); 88018de8d7fSPeter Avalos /* rekeying */ 88118de8d7fSPeter Avalos dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); 88218de8d7fSPeter Avalos } 883