1*0cbfa66cSDaniel Fojt /* $OpenBSD: serverloop.c,v 1.222 2020/01/30 07:21:38 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> 52664f4763Szrj #include <limits.h> 5318de8d7fSPeter Avalos #include <signal.h> 5418de8d7fSPeter Avalos #include <string.h> 5518de8d7fSPeter Avalos #include <termios.h> 5618de8d7fSPeter Avalos #include <unistd.h> 5718de8d7fSPeter Avalos #include <stdarg.h> 5818de8d7fSPeter Avalos 5918de8d7fSPeter Avalos #include "openbsd-compat/sys-queue.h" 6018de8d7fSPeter Avalos #include "xmalloc.h" 6118de8d7fSPeter Avalos #include "packet.h" 62664f4763Szrj #include "sshbuf.h" 6318de8d7fSPeter Avalos #include "log.h" 6436e94dc5SPeter Avalos #include "misc.h" 6518de8d7fSPeter Avalos #include "servconf.h" 6618de8d7fSPeter Avalos #include "canohost.h" 6718de8d7fSPeter Avalos #include "sshpty.h" 6818de8d7fSPeter Avalos #include "channels.h" 6918de8d7fSPeter Avalos #include "compat.h" 7018de8d7fSPeter Avalos #include "ssh2.h" 71664f4763Szrj #include "sshkey.h" 7218de8d7fSPeter Avalos #include "cipher.h" 7318de8d7fSPeter Avalos #include "kex.h" 7418de8d7fSPeter Avalos #include "hostfile.h" 7518de8d7fSPeter Avalos #include "auth.h" 7618de8d7fSPeter Avalos #include "session.h" 7718de8d7fSPeter Avalos #include "dispatch.h" 7818de8d7fSPeter Avalos #include "auth-options.h" 7918de8d7fSPeter Avalos #include "serverloop.h" 80e9778795SPeter Avalos #include "ssherr.h" 8118de8d7fSPeter Avalos 8218de8d7fSPeter Avalos extern ServerOptions options; 8318de8d7fSPeter Avalos 8418de8d7fSPeter Avalos /* XXX */ 8518de8d7fSPeter Avalos extern Authctxt *the_authctxt; 86664f4763Szrj extern struct sshauthopt *auth_opts; 8718de8d7fSPeter Avalos extern int use_privsep; 8818de8d7fSPeter Avalos 8918de8d7fSPeter Avalos static int no_more_sessions = 0; /* Disallow further sessions. */ 9018de8d7fSPeter Avalos 9118de8d7fSPeter Avalos /* 9218de8d7fSPeter Avalos * This SIGCHLD kludge is used to detect when the child exits. The server 9318de8d7fSPeter Avalos * will exit after that, as soon as forwarded connections have terminated. 9418de8d7fSPeter Avalos */ 9518de8d7fSPeter Avalos 9618de8d7fSPeter Avalos static volatile sig_atomic_t child_terminated = 0; /* The child has terminated. */ 9718de8d7fSPeter Avalos 9818de8d7fSPeter Avalos /* Cleanup on signals (!use_privsep case only) */ 9918de8d7fSPeter Avalos static volatile sig_atomic_t received_sigterm = 0; 10018de8d7fSPeter Avalos 10118de8d7fSPeter Avalos /* prototypes */ 102664f4763Szrj static void server_init_dispatch(struct ssh *); 103664f4763Szrj 104664f4763Szrj /* requested tunnel forwarding interface(s), shared with session.c */ 105664f4763Szrj char *tun_fwd_ifnames = NULL; 106664f4763Szrj 107664f4763Szrj /* returns 1 if bind to specified port by specified user is permitted */ 108664f4763Szrj static int 109664f4763Szrj bind_permitted(int port, uid_t uid) 110664f4763Szrj { 111664f4763Szrj if (use_privsep) 112664f4763Szrj return 1; /* allow system to decide */ 113664f4763Szrj if (port < IPPORT_RESERVED && uid != 0) 114664f4763Szrj return 0; 115664f4763Szrj return 1; 116664f4763Szrj } 11718de8d7fSPeter Avalos 11818de8d7fSPeter Avalos /* 11918de8d7fSPeter Avalos * we write to this pipe if a SIGCHLD is caught in order to avoid 12018de8d7fSPeter Avalos * the race between select() and child_terminated 12118de8d7fSPeter Avalos */ 12218de8d7fSPeter Avalos static int notify_pipe[2]; 12318de8d7fSPeter Avalos static void 12418de8d7fSPeter Avalos notify_setup(void) 12518de8d7fSPeter Avalos { 126*0cbfa66cSDaniel Fojt if (pipe(notify_pipe) == -1) { 12718de8d7fSPeter Avalos error("pipe(notify_pipe) failed %s", strerror(errno)); 1281c188a7fSPeter Avalos } else if ((fcntl(notify_pipe[0], F_SETFD, FD_CLOEXEC) == -1) || 1291c188a7fSPeter Avalos (fcntl(notify_pipe[1], F_SETFD, FD_CLOEXEC) == -1)) { 13018de8d7fSPeter Avalos error("fcntl(notify_pipe, F_SETFD) failed %s", strerror(errno)); 13118de8d7fSPeter Avalos close(notify_pipe[0]); 13218de8d7fSPeter Avalos close(notify_pipe[1]); 13318de8d7fSPeter Avalos } else { 13418de8d7fSPeter Avalos set_nonblock(notify_pipe[0]); 13518de8d7fSPeter Avalos set_nonblock(notify_pipe[1]); 13618de8d7fSPeter Avalos return; 13718de8d7fSPeter Avalos } 13818de8d7fSPeter Avalos notify_pipe[0] = -1; /* read end */ 13918de8d7fSPeter Avalos notify_pipe[1] = -1; /* write end */ 14018de8d7fSPeter Avalos } 14118de8d7fSPeter Avalos static void 14218de8d7fSPeter Avalos notify_parent(void) 14318de8d7fSPeter Avalos { 14418de8d7fSPeter Avalos if (notify_pipe[1] != -1) 14536e94dc5SPeter Avalos (void)write(notify_pipe[1], "", 1); 14618de8d7fSPeter Avalos } 14718de8d7fSPeter Avalos static void 14818de8d7fSPeter Avalos notify_prepare(fd_set *readset) 14918de8d7fSPeter Avalos { 15018de8d7fSPeter Avalos if (notify_pipe[0] != -1) 15118de8d7fSPeter Avalos FD_SET(notify_pipe[0], readset); 15218de8d7fSPeter Avalos } 15318de8d7fSPeter Avalos static void 15418de8d7fSPeter Avalos notify_done(fd_set *readset) 15518de8d7fSPeter Avalos { 15618de8d7fSPeter Avalos char c; 15718de8d7fSPeter Avalos 15818de8d7fSPeter Avalos if (notify_pipe[0] != -1 && FD_ISSET(notify_pipe[0], readset)) 15918de8d7fSPeter Avalos while (read(notify_pipe[0], &c, 1) != -1) 160664f4763Szrj debug2("%s: reading", __func__); 16118de8d7fSPeter Avalos } 16218de8d7fSPeter Avalos 16318de8d7fSPeter Avalos /*ARGSUSED*/ 16418de8d7fSPeter Avalos static void 16518de8d7fSPeter Avalos sigchld_handler(int sig) 16618de8d7fSPeter Avalos { 16718de8d7fSPeter Avalos int save_errno = errno; 16818de8d7fSPeter Avalos child_terminated = 1; 16918de8d7fSPeter Avalos notify_parent(); 17018de8d7fSPeter Avalos errno = save_errno; 17118de8d7fSPeter Avalos } 17218de8d7fSPeter Avalos 17318de8d7fSPeter Avalos /*ARGSUSED*/ 17418de8d7fSPeter Avalos static void 17518de8d7fSPeter Avalos sigterm_handler(int sig) 17618de8d7fSPeter Avalos { 17718de8d7fSPeter Avalos received_sigterm = sig; 17818de8d7fSPeter Avalos } 17918de8d7fSPeter Avalos 18018de8d7fSPeter Avalos static void 181ce74bacaSMatthew Dillon client_alive_check(struct ssh *ssh) 18218de8d7fSPeter Avalos { 183664f4763Szrj char remote_id[512]; 184664f4763Szrj int r, channel_id; 18518de8d7fSPeter Avalos 18618de8d7fSPeter Avalos /* timeout, check to see how many we have had */ 187*0cbfa66cSDaniel Fojt if (options.client_alive_count_max > 0 && 188*0cbfa66cSDaniel Fojt ssh_packet_inc_alive_timeouts(ssh) > 189664f4763Szrj options.client_alive_count_max) { 190664f4763Szrj sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id)); 191664f4763Szrj logit("Timeout, client not responding from %s", remote_id); 19218de8d7fSPeter Avalos cleanup_exit(255); 19318de8d7fSPeter Avalos } 19418de8d7fSPeter Avalos 19518de8d7fSPeter Avalos /* 19618de8d7fSPeter Avalos * send a bogus global/channel request with "wantreply", 19718de8d7fSPeter Avalos * we should get back a failure 19818de8d7fSPeter Avalos */ 199ce74bacaSMatthew Dillon if ((channel_id = channel_find_open(ssh)) == -1) { 200664f4763Szrj if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || 201664f4763Szrj (r = sshpkt_put_cstring(ssh, "keepalive@openssh.com")) 202664f4763Szrj != 0 || 203664f4763Szrj (r = sshpkt_put_u8(ssh, 1)) != 0) /* boolean: want reply */ 204664f4763Szrj fatal("%s: %s", __func__, ssh_err(r)); 20518de8d7fSPeter Avalos } else { 206ce74bacaSMatthew Dillon channel_request_start(ssh, channel_id, 207ce74bacaSMatthew Dillon "keepalive@openssh.com", 1); 20818de8d7fSPeter Avalos } 209664f4763Szrj if ((r = sshpkt_send(ssh)) != 0) 210664f4763Szrj fatal("%s: %s", __func__, ssh_err(r)); 21118de8d7fSPeter Avalos } 21218de8d7fSPeter Avalos 21318de8d7fSPeter Avalos /* 21418de8d7fSPeter Avalos * Sleep in select() until we can do something. This will initialize the 21518de8d7fSPeter Avalos * select masks. Upon return, the masks will indicate which descriptors 21618de8d7fSPeter Avalos * have data or can accept data. Optionally, a maximum time can be specified 21718de8d7fSPeter Avalos * for the duration of the wait (0 = infinite). 21818de8d7fSPeter Avalos */ 21918de8d7fSPeter Avalos static void 220ce74bacaSMatthew Dillon wait_until_can_do_something(struct ssh *ssh, 221ce74bacaSMatthew Dillon int connection_in, int connection_out, 222ce74bacaSMatthew Dillon fd_set **readsetp, fd_set **writesetp, int *maxfdp, 223e9778795SPeter Avalos u_int *nallocp, u_int64_t max_time_ms) 22418de8d7fSPeter Avalos { 22518de8d7fSPeter Avalos struct timeval tv, *tvp; 22618de8d7fSPeter Avalos int ret; 22799e85e0dSPeter Avalos time_t minwait_secs = 0; 22818de8d7fSPeter Avalos int client_alive_scheduled = 0; 229664f4763Szrj /* time we last heard from the client OR sent a keepalive */ 230ce74bacaSMatthew Dillon static time_t last_client_time; 23118de8d7fSPeter Avalos 23299e85e0dSPeter Avalos /* Allocate and update select() masks for channel descriptors. */ 233ce74bacaSMatthew Dillon channel_prepare_select(ssh, readsetp, writesetp, maxfdp, 234ce74bacaSMatthew Dillon nallocp, &minwait_secs); 23599e85e0dSPeter Avalos 236e9778795SPeter Avalos /* XXX need proper deadline system for rekey/client alive */ 23799e85e0dSPeter Avalos if (minwait_secs != 0) 238ce74bacaSMatthew Dillon max_time_ms = MINIMUM(max_time_ms, (u_int)minwait_secs * 1000); 23999e85e0dSPeter Avalos 24018de8d7fSPeter Avalos /* 24118de8d7fSPeter Avalos * if using client_alive, set the max timeout accordingly, 24218de8d7fSPeter Avalos * and indicate that this particular timeout was for client 24318de8d7fSPeter Avalos * alive by setting the client_alive_scheduled flag. 24418de8d7fSPeter Avalos * 24518de8d7fSPeter Avalos * this could be randomized somewhat to make traffic 24618de8d7fSPeter Avalos * analysis more difficult, but we're not doing it yet. 24718de8d7fSPeter Avalos */ 248ce74bacaSMatthew Dillon if (options.client_alive_interval) { 249e9778795SPeter Avalos uint64_t keepalive_ms = 250e9778795SPeter Avalos (uint64_t)options.client_alive_interval * 1000; 251e9778795SPeter Avalos 252664f4763Szrj if (max_time_ms == 0 || max_time_ms > keepalive_ms) { 253e9778795SPeter Avalos max_time_ms = keepalive_ms; 254664f4763Szrj client_alive_scheduled = 1; 255664f4763Szrj } 25618de8d7fSPeter Avalos } 25718de8d7fSPeter Avalos 25818de8d7fSPeter Avalos #if 0 25918de8d7fSPeter Avalos /* wrong: bad condition XXX */ 26018de8d7fSPeter Avalos if (channel_not_very_much_buffered_data()) 26118de8d7fSPeter Avalos #endif 26218de8d7fSPeter Avalos FD_SET(connection_in, *readsetp); 26318de8d7fSPeter Avalos notify_prepare(*readsetp); 26418de8d7fSPeter Avalos 26518de8d7fSPeter Avalos /* 26618de8d7fSPeter Avalos * If we have buffered packet data going to the client, mark that 26718de8d7fSPeter Avalos * descriptor. 26818de8d7fSPeter Avalos */ 269664f4763Szrj if (ssh_packet_have_data_to_write(ssh)) 27018de8d7fSPeter Avalos FD_SET(connection_out, *writesetp); 27118de8d7fSPeter Avalos 27218de8d7fSPeter Avalos /* 27318de8d7fSPeter Avalos * If child has terminated and there is enough buffer space to read 27418de8d7fSPeter Avalos * from it, then read as much as is available and exit. 27518de8d7fSPeter Avalos */ 276664f4763Szrj if (child_terminated && ssh_packet_not_very_much_data_to_write(ssh)) 277e9778795SPeter Avalos if (max_time_ms == 0 || client_alive_scheduled) 278e9778795SPeter Avalos max_time_ms = 100; 27918de8d7fSPeter Avalos 280e9778795SPeter Avalos if (max_time_ms == 0) 28118de8d7fSPeter Avalos tvp = NULL; 28218de8d7fSPeter Avalos else { 283e9778795SPeter Avalos tv.tv_sec = max_time_ms / 1000; 284e9778795SPeter Avalos tv.tv_usec = 1000 * (max_time_ms % 1000); 28518de8d7fSPeter Avalos tvp = &tv; 28618de8d7fSPeter Avalos } 28718de8d7fSPeter Avalos 28818de8d7fSPeter Avalos /* Wait for something to happen, or the timeout to expire. */ 28918de8d7fSPeter Avalos ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp); 29018de8d7fSPeter Avalos 29118de8d7fSPeter Avalos if (ret == -1) { 29218de8d7fSPeter Avalos memset(*readsetp, 0, *nallocp); 29318de8d7fSPeter Avalos memset(*writesetp, 0, *nallocp); 29418de8d7fSPeter Avalos if (errno != EINTR) 29518de8d7fSPeter Avalos error("select: %.100s", strerror(errno)); 296ce74bacaSMatthew Dillon } else if (client_alive_scheduled) { 297ce74bacaSMatthew Dillon time_t now = monotime(); 298ce74bacaSMatthew Dillon 299664f4763Szrj /* 300664f4763Szrj * If the select timed out, or returned for some other reason 301664f4763Szrj * but we haven't heard from the client in time, send keepalive. 302664f4763Szrj */ 303664f4763Szrj if (ret == 0 || (last_client_time != 0 && last_client_time + 304664f4763Szrj options.client_alive_interval <= now)) { 305ce74bacaSMatthew Dillon client_alive_check(ssh); 306ce74bacaSMatthew Dillon last_client_time = now; 307664f4763Szrj } else if (FD_ISSET(connection_in, *readsetp)) { 308ce74bacaSMatthew Dillon last_client_time = now; 30918de8d7fSPeter Avalos } 31018de8d7fSPeter Avalos } 31118de8d7fSPeter Avalos 31218de8d7fSPeter Avalos notify_done(*readsetp); 31318de8d7fSPeter Avalos } 31418de8d7fSPeter Avalos 31518de8d7fSPeter Avalos /* 31618de8d7fSPeter Avalos * Processes input from the client and the program. Input data is stored 31718de8d7fSPeter Avalos * in buffers and processed later. 31818de8d7fSPeter Avalos */ 319ce74bacaSMatthew Dillon static int 320ce74bacaSMatthew Dillon process_input(struct ssh *ssh, fd_set *readset, int connection_in) 32118de8d7fSPeter Avalos { 322664f4763Szrj int r, len; 32318de8d7fSPeter Avalos char buf[16384]; 32418de8d7fSPeter Avalos 32518de8d7fSPeter Avalos /* Read and buffer any input data from the client. */ 32618de8d7fSPeter Avalos if (FD_ISSET(connection_in, readset)) { 327e9778795SPeter Avalos len = read(connection_in, buf, sizeof(buf)); 32818de8d7fSPeter Avalos if (len == 0) { 329e9778795SPeter Avalos verbose("Connection closed by %.100s port %d", 330e9778795SPeter Avalos ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); 331ce74bacaSMatthew Dillon return -1; 332*0cbfa66cSDaniel Fojt } else if (len == -1) { 33318de8d7fSPeter Avalos if (errno != EINTR && errno != EAGAIN && 33418de8d7fSPeter Avalos errno != EWOULDBLOCK) { 33518de8d7fSPeter Avalos verbose("Read error from remote host " 336e9778795SPeter Avalos "%.100s port %d: %.100s", 337e9778795SPeter Avalos ssh_remote_ipaddr(ssh), 338e9778795SPeter Avalos ssh_remote_port(ssh), strerror(errno)); 33918de8d7fSPeter Avalos cleanup_exit(255); 34018de8d7fSPeter Avalos } 34118de8d7fSPeter Avalos } else { 34218de8d7fSPeter Avalos /* Buffer any received data. */ 343664f4763Szrj if ((r = ssh_packet_process_incoming(ssh, buf, len)) 344664f4763Szrj != 0) 345664f4763Szrj fatal("%s: ssh_packet_process_incoming: %s", 346664f4763Szrj __func__, ssh_err(r)); 34718de8d7fSPeter Avalos } 34818de8d7fSPeter Avalos } 349ce74bacaSMatthew Dillon return 0; 35018de8d7fSPeter Avalos } 35118de8d7fSPeter Avalos 35218de8d7fSPeter Avalos /* 35318de8d7fSPeter Avalos * Sends data from internal buffers to client program stdin. 35418de8d7fSPeter Avalos */ 35518de8d7fSPeter Avalos static void 356664f4763Szrj process_output(struct ssh *ssh, fd_set *writeset, int connection_out) 35718de8d7fSPeter Avalos { 358664f4763Szrj int r; 359664f4763Szrj 36018de8d7fSPeter Avalos /* Send any buffered packet data to the client. */ 361664f4763Szrj if (FD_ISSET(connection_out, writeset)) { 362*0cbfa66cSDaniel Fojt if ((r = ssh_packet_write_poll(ssh)) != 0) { 363*0cbfa66cSDaniel Fojt sshpkt_fatal(ssh, r, "%s: ssh_packet_write_poll", 364*0cbfa66cSDaniel Fojt __func__); 365*0cbfa66cSDaniel Fojt } 366664f4763Szrj } 36718de8d7fSPeter Avalos } 36818de8d7fSPeter Avalos 36918de8d7fSPeter Avalos static void 370ce74bacaSMatthew Dillon process_buffered_input_packets(struct ssh *ssh) 37118de8d7fSPeter Avalos { 372ce74bacaSMatthew Dillon ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, NULL); 37318de8d7fSPeter Avalos } 37418de8d7fSPeter Avalos 37518de8d7fSPeter Avalos static void 376ce74bacaSMatthew Dillon collect_children(struct ssh *ssh) 37718de8d7fSPeter Avalos { 37818de8d7fSPeter Avalos pid_t pid; 37918de8d7fSPeter Avalos sigset_t oset, nset; 38018de8d7fSPeter Avalos int status; 38118de8d7fSPeter Avalos 38218de8d7fSPeter Avalos /* block SIGCHLD while we check for dead children */ 38318de8d7fSPeter Avalos sigemptyset(&nset); 38418de8d7fSPeter Avalos sigaddset(&nset, SIGCHLD); 38518de8d7fSPeter Avalos sigprocmask(SIG_BLOCK, &nset, &oset); 38618de8d7fSPeter Avalos if (child_terminated) { 38718de8d7fSPeter Avalos debug("Received SIGCHLD."); 38818de8d7fSPeter Avalos while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || 389*0cbfa66cSDaniel Fojt (pid == -1 && errno == EINTR)) 39018de8d7fSPeter Avalos if (pid > 0) 391ce74bacaSMatthew Dillon session_close_by_pid(ssh, pid, status); 39218de8d7fSPeter Avalos child_terminated = 0; 39318de8d7fSPeter Avalos } 39418de8d7fSPeter Avalos sigprocmask(SIG_SETMASK, &oset, NULL); 39518de8d7fSPeter Avalos } 39618de8d7fSPeter Avalos 39718de8d7fSPeter Avalos void 398ce74bacaSMatthew Dillon server_loop2(struct ssh *ssh, Authctxt *authctxt) 39918de8d7fSPeter Avalos { 40018de8d7fSPeter Avalos fd_set *readset = NULL, *writeset = NULL; 401e9778795SPeter Avalos int max_fd; 402ce74bacaSMatthew Dillon u_int nalloc = 0, connection_in, connection_out; 40336e94dc5SPeter Avalos u_int64_t rekey_timeout_ms = 0; 40418de8d7fSPeter Avalos 40518de8d7fSPeter Avalos debug("Entering interactive session for SSH2."); 40618de8d7fSPeter Avalos 407*0cbfa66cSDaniel Fojt ssh_signal(SIGCHLD, sigchld_handler); 40818de8d7fSPeter Avalos child_terminated = 0; 409664f4763Szrj connection_in = ssh_packet_get_connection_in(ssh); 410664f4763Szrj connection_out = ssh_packet_get_connection_out(ssh); 41118de8d7fSPeter Avalos 41218de8d7fSPeter Avalos if (!use_privsep) { 413*0cbfa66cSDaniel Fojt ssh_signal(SIGTERM, sigterm_handler); 414*0cbfa66cSDaniel Fojt ssh_signal(SIGINT, sigterm_handler); 415*0cbfa66cSDaniel Fojt ssh_signal(SIGQUIT, sigterm_handler); 41618de8d7fSPeter Avalos } 41718de8d7fSPeter Avalos 41818de8d7fSPeter Avalos notify_setup(); 41918de8d7fSPeter Avalos 420ce74bacaSMatthew Dillon max_fd = MAXIMUM(connection_in, connection_out); 421ce74bacaSMatthew Dillon max_fd = MAXIMUM(max_fd, notify_pipe[0]); 42218de8d7fSPeter Avalos 423664f4763Szrj server_init_dispatch(ssh); 42418de8d7fSPeter Avalos 42518de8d7fSPeter Avalos for (;;) { 426ce74bacaSMatthew Dillon process_buffered_input_packets(ssh); 42718de8d7fSPeter Avalos 428ce74bacaSMatthew Dillon if (!ssh_packet_is_rekeying(ssh) && 429664f4763Szrj ssh_packet_not_very_much_data_to_write(ssh)) 430ce74bacaSMatthew Dillon channel_output_poll(ssh); 431664f4763Szrj if (options.rekey_interval > 0 && 432664f4763Szrj !ssh_packet_is_rekeying(ssh)) { 433664f4763Szrj rekey_timeout_ms = ssh_packet_get_rekey_timeout(ssh) * 434664f4763Szrj 1000; 435664f4763Szrj } else { 43636e94dc5SPeter Avalos rekey_timeout_ms = 0; 437664f4763Szrj } 43836e94dc5SPeter Avalos 439ce74bacaSMatthew Dillon wait_until_can_do_something(ssh, connection_in, connection_out, 440ce74bacaSMatthew Dillon &readset, &writeset, &max_fd, &nalloc, rekey_timeout_ms); 44118de8d7fSPeter Avalos 44218de8d7fSPeter Avalos if (received_sigterm) { 44336e94dc5SPeter Avalos logit("Exiting on signal %d", (int)received_sigterm); 44418de8d7fSPeter Avalos /* Clean up sessions, utmp, etc. */ 44518de8d7fSPeter Avalos cleanup_exit(255); 44618de8d7fSPeter Avalos } 44718de8d7fSPeter Avalos 448ce74bacaSMatthew Dillon collect_children(ssh); 449ce74bacaSMatthew Dillon if (!ssh_packet_is_rekeying(ssh)) 450ce74bacaSMatthew Dillon channel_after_select(ssh, readset, writeset); 451ce74bacaSMatthew Dillon if (process_input(ssh, readset, connection_in) < 0) 45218de8d7fSPeter Avalos break; 453664f4763Szrj process_output(ssh, writeset, connection_out); 45418de8d7fSPeter Avalos } 455ce74bacaSMatthew Dillon collect_children(ssh); 45618de8d7fSPeter Avalos 45736e94dc5SPeter Avalos free(readset); 45836e94dc5SPeter Avalos free(writeset); 45918de8d7fSPeter Avalos 46018de8d7fSPeter Avalos /* free all channels, no more reads and writes */ 461ce74bacaSMatthew Dillon channel_free_all(ssh); 46218de8d7fSPeter Avalos 46318de8d7fSPeter Avalos /* free remaining sessions, e.g. remove wtmp entries */ 464ce74bacaSMatthew Dillon session_destroy_all(ssh, NULL); 46518de8d7fSPeter Avalos } 46618de8d7fSPeter Avalos 467e9778795SPeter Avalos static int 468ce74bacaSMatthew Dillon server_input_keep_alive(int type, u_int32_t seq, struct ssh *ssh) 46918de8d7fSPeter Avalos { 47018de8d7fSPeter Avalos debug("Got %d/%u for keepalive", type, seq); 47118de8d7fSPeter Avalos /* 47218de8d7fSPeter Avalos * reset timeout, since we got a sane answer from the client. 47318de8d7fSPeter Avalos * even if this was generated by something other than 47418de8d7fSPeter Avalos * the bogus CHANNEL_REQUEST we send for keepalives. 47518de8d7fSPeter Avalos */ 476664f4763Szrj ssh_packet_set_alive_timeouts(ssh, 0); 477e9778795SPeter Avalos return 0; 47818de8d7fSPeter Avalos } 47918de8d7fSPeter Avalos 48018de8d7fSPeter Avalos static Channel * 481ce74bacaSMatthew Dillon server_request_direct_tcpip(struct ssh *ssh, int *reason, const char **errmsg) 48218de8d7fSPeter Avalos { 48336e94dc5SPeter Avalos Channel *c = NULL; 484664f4763Szrj char *target = NULL, *originator = NULL; 485664f4763Szrj u_int target_port = 0, originator_port = 0; 486664f4763Szrj int r; 48718de8d7fSPeter Avalos 488664f4763Szrj if ((r = sshpkt_get_cstring(ssh, &target, NULL)) != 0 || 489664f4763Szrj (r = sshpkt_get_u32(ssh, &target_port)) != 0 || 490664f4763Szrj (r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 || 491664f4763Szrj (r = sshpkt_get_u32(ssh, &originator_port)) != 0 || 492664f4763Szrj (r = sshpkt_get_end(ssh)) != 0) 493664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 494664f4763Szrj if (target_port > 0xFFFF) { 495664f4763Szrj error("%s: invalid target port", __func__); 496664f4763Szrj *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; 497664f4763Szrj goto out; 498664f4763Szrj } 499664f4763Szrj if (originator_port > 0xFFFF) { 500664f4763Szrj error("%s: invalid originator port", __func__); 501664f4763Szrj *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; 502664f4763Szrj goto out; 503664f4763Szrj } 50418de8d7fSPeter Avalos 505664f4763Szrj debug("%s: originator %s port %u, target %s port %u", __func__, 506664f4763Szrj originator, originator_port, target, target_port); 50718de8d7fSPeter Avalos 50836e94dc5SPeter Avalos /* XXX fine grained permissions */ 50936e94dc5SPeter Avalos if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 && 510664f4763Szrj auth_opts->permit_port_forwarding_flag && 511664f4763Szrj !options.disable_forwarding) { 512ce74bacaSMatthew Dillon c = channel_connect_to_port(ssh, target, target_port, 513ce74bacaSMatthew Dillon "direct-tcpip", "direct-tcpip", reason, errmsg); 51436e94dc5SPeter Avalos } else { 51536e94dc5SPeter Avalos logit("refused local port forward: " 51636e94dc5SPeter Avalos "originator %s port %d, target %s port %d", 51736e94dc5SPeter Avalos originator, originator_port, target, target_port); 518ce74bacaSMatthew Dillon if (reason != NULL) 519ce74bacaSMatthew Dillon *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; 52036e94dc5SPeter Avalos } 52118de8d7fSPeter Avalos 522664f4763Szrj out: 52336e94dc5SPeter Avalos free(originator); 52436e94dc5SPeter Avalos free(target); 52536e94dc5SPeter Avalos return c; 52636e94dc5SPeter Avalos } 52736e94dc5SPeter Avalos 52836e94dc5SPeter Avalos static Channel * 529ce74bacaSMatthew Dillon server_request_direct_streamlocal(struct ssh *ssh) 53036e94dc5SPeter Avalos { 53136e94dc5SPeter Avalos Channel *c = NULL; 532664f4763Szrj char *target = NULL, *originator = NULL; 533664f4763Szrj u_int originator_port = 0; 534ce74bacaSMatthew Dillon struct passwd *pw = the_authctxt->pw; 535664f4763Szrj int r; 536ce74bacaSMatthew Dillon 537ce74bacaSMatthew Dillon if (pw == NULL || !the_authctxt->valid) 538664f4763Szrj fatal("%s: no/invalid user", __func__); 53936e94dc5SPeter Avalos 540664f4763Szrj if ((r = sshpkt_get_cstring(ssh, &target, NULL)) != 0 || 541664f4763Szrj (r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 || 542664f4763Szrj (r = sshpkt_get_u32(ssh, &originator_port)) != 0 || 543664f4763Szrj (r = sshpkt_get_end(ssh)) != 0) 544664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 545664f4763Szrj if (originator_port > 0xFFFF) { 546664f4763Szrj error("%s: invalid originator port", __func__); 547664f4763Szrj goto out; 548664f4763Szrj } 54936e94dc5SPeter Avalos 550664f4763Szrj debug("%s: originator %s port %d, target %s", __func__, 55136e94dc5SPeter Avalos originator, originator_port, target); 55236e94dc5SPeter Avalos 55336e94dc5SPeter Avalos /* XXX fine grained permissions */ 55436e94dc5SPeter Avalos if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 && 555664f4763Szrj auth_opts->permit_port_forwarding_flag && 556664f4763Szrj !options.disable_forwarding && (pw->pw_uid == 0 || use_privsep)) { 557ce74bacaSMatthew Dillon c = channel_connect_to_path(ssh, target, 55836e94dc5SPeter Avalos "direct-streamlocal@openssh.com", "direct-streamlocal"); 55936e94dc5SPeter Avalos } else { 56036e94dc5SPeter Avalos logit("refused streamlocal port forward: " 56136e94dc5SPeter Avalos "originator %s port %d, target %s", 56236e94dc5SPeter Avalos originator, originator_port, target); 56336e94dc5SPeter Avalos } 56436e94dc5SPeter Avalos 565664f4763Szrj out: 56636e94dc5SPeter Avalos free(originator); 56736e94dc5SPeter Avalos free(target); 56818de8d7fSPeter Avalos return c; 56918de8d7fSPeter Avalos } 57018de8d7fSPeter Avalos 57118de8d7fSPeter Avalos static Channel * 572ce74bacaSMatthew Dillon server_request_tun(struct ssh *ssh) 57318de8d7fSPeter Avalos { 57418de8d7fSPeter Avalos Channel *c = NULL; 575664f4763Szrj u_int mode, tun; 576664f4763Szrj int r, sock; 577664f4763Szrj char *tmp, *ifname = NULL; 57818de8d7fSPeter Avalos 579664f4763Szrj if ((r = sshpkt_get_u32(ssh, &mode)) != 0) 580664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse mode", __func__); 58118de8d7fSPeter Avalos switch (mode) { 58218de8d7fSPeter Avalos case SSH_TUNMODE_POINTOPOINT: 58318de8d7fSPeter Avalos case SSH_TUNMODE_ETHERNET: 58418de8d7fSPeter Avalos break; 58518de8d7fSPeter Avalos default: 586664f4763Szrj ssh_packet_send_debug(ssh, "Unsupported tunnel device mode."); 58718de8d7fSPeter Avalos return NULL; 58818de8d7fSPeter Avalos } 58918de8d7fSPeter Avalos if ((options.permit_tun & mode) == 0) { 590664f4763Szrj ssh_packet_send_debug(ssh, "Server has rejected tunnel device " 59118de8d7fSPeter Avalos "forwarding"); 59218de8d7fSPeter Avalos return NULL; 59318de8d7fSPeter Avalos } 59418de8d7fSPeter Avalos 595664f4763Szrj if ((r = sshpkt_get_u32(ssh, &tun)) != 0) 596664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse device", __func__); 597664f4763Szrj if (tun > INT_MAX) { 598664f4763Szrj debug("%s: invalid tun", __func__); 59918de8d7fSPeter Avalos goto done; 60018de8d7fSPeter Avalos } 601664f4763Szrj if (auth_opts->force_tun_device != -1) { 602664f4763Szrj if (tun != SSH_TUNID_ANY && 603664f4763Szrj auth_opts->force_tun_device != (int)tun) 604664f4763Szrj goto done; 605664f4763Szrj tun = auth_opts->force_tun_device; 606664f4763Szrj } 607664f4763Szrj sock = tun_open(tun, mode, &ifname); 60818de8d7fSPeter Avalos if (sock < 0) 60918de8d7fSPeter Avalos goto done; 610664f4763Szrj debug("Tunnel forwarding using interface %s", ifname); 611664f4763Szrj 612ce74bacaSMatthew Dillon c = channel_new(ssh, "tun", SSH_CHANNEL_OPEN, sock, sock, -1, 61318de8d7fSPeter Avalos CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); 61418de8d7fSPeter Avalos c->datagram = 1; 61518de8d7fSPeter Avalos #if defined(SSH_TUN_FILTER) 61618de8d7fSPeter Avalos if (mode == SSH_TUNMODE_POINTOPOINT) 617ce74bacaSMatthew Dillon channel_register_filter(ssh, c->self, sys_tun_infilter, 61818de8d7fSPeter Avalos sys_tun_outfilter, NULL, NULL); 61918de8d7fSPeter Avalos #endif 62018de8d7fSPeter Avalos 621664f4763Szrj /* 622664f4763Szrj * Update the list of names exposed to the session 623664f4763Szrj * XXX remove these if the tunnels are closed (won't matter 624664f4763Szrj * much if they are already in the environment though) 625664f4763Szrj */ 626664f4763Szrj tmp = tun_fwd_ifnames; 627664f4763Szrj xasprintf(&tun_fwd_ifnames, "%s%s%s", 628664f4763Szrj tun_fwd_ifnames == NULL ? "" : tun_fwd_ifnames, 629664f4763Szrj tun_fwd_ifnames == NULL ? "" : ",", 630664f4763Szrj ifname); 631664f4763Szrj free(tmp); 632664f4763Szrj free(ifname); 633664f4763Szrj 63418de8d7fSPeter Avalos done: 63518de8d7fSPeter Avalos if (c == NULL) 636664f4763Szrj ssh_packet_send_debug(ssh, "Failed to open the tunnel device."); 63718de8d7fSPeter Avalos return c; 63818de8d7fSPeter Avalos } 63918de8d7fSPeter Avalos 64018de8d7fSPeter Avalos static Channel * 641ce74bacaSMatthew Dillon server_request_session(struct ssh *ssh) 64218de8d7fSPeter Avalos { 64318de8d7fSPeter Avalos Channel *c; 644664f4763Szrj int r; 64518de8d7fSPeter Avalos 64618de8d7fSPeter Avalos debug("input_session_request"); 647664f4763Szrj if ((r = sshpkt_get_end(ssh)) != 0) 648664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 64918de8d7fSPeter Avalos 65018de8d7fSPeter Avalos if (no_more_sessions) { 651664f4763Szrj ssh_packet_disconnect(ssh, "Possible attack: attempt to open a " 652664f4763Szrj "session after additional sessions disabled"); 65318de8d7fSPeter Avalos } 65418de8d7fSPeter Avalos 65518de8d7fSPeter Avalos /* 65618de8d7fSPeter Avalos * A server session has no fd to read or write until a 65718de8d7fSPeter Avalos * CHANNEL_REQUEST for a shell is made, so we set the type to 65818de8d7fSPeter Avalos * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all 65918de8d7fSPeter Avalos * CHANNEL_REQUEST messages is registered. 66018de8d7fSPeter Avalos */ 661ce74bacaSMatthew Dillon c = channel_new(ssh, "session", SSH_CHANNEL_LARVAL, 66218de8d7fSPeter Avalos -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT, 66318de8d7fSPeter Avalos 0, "server-session", 1); 66418de8d7fSPeter Avalos if (session_open(the_authctxt, c->self) != 1) { 66518de8d7fSPeter Avalos debug("session open failed, free channel %d", c->self); 666ce74bacaSMatthew Dillon channel_free(ssh, c); 66718de8d7fSPeter Avalos return NULL; 66818de8d7fSPeter Avalos } 669ce74bacaSMatthew Dillon channel_register_cleanup(ssh, c->self, session_close_by_channel, 0); 67018de8d7fSPeter Avalos return c; 67118de8d7fSPeter Avalos } 67218de8d7fSPeter Avalos 673e9778795SPeter Avalos static int 674ce74bacaSMatthew Dillon server_input_channel_open(int type, u_int32_t seq, struct ssh *ssh) 67518de8d7fSPeter Avalos { 67618de8d7fSPeter Avalos Channel *c = NULL; 677664f4763Szrj char *ctype = NULL; 678ce74bacaSMatthew Dillon const char *errmsg = NULL; 679664f4763Szrj int r, reason = SSH2_OPEN_CONNECT_FAILED; 680664f4763Szrj u_int rchan = 0, rmaxpack = 0, rwindow = 0; 68118de8d7fSPeter Avalos 682664f4763Szrj if ((r = sshpkt_get_cstring(ssh, &ctype, NULL)) != 0 || 683664f4763Szrj (r = sshpkt_get_u32(ssh, &rchan)) != 0 || 684664f4763Szrj (r = sshpkt_get_u32(ssh, &rwindow)) != 0 || 685664f4763Szrj (r = sshpkt_get_u32(ssh, &rmaxpack)) != 0) 686664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 687664f4763Szrj debug("%s: ctype %s rchan %u win %u max %u", __func__, 68818de8d7fSPeter Avalos ctype, rchan, rwindow, rmaxpack); 68918de8d7fSPeter Avalos 690*0cbfa66cSDaniel Fojt if (strcmp(ctype, "session") == 0) { 691ce74bacaSMatthew Dillon c = server_request_session(ssh); 69218de8d7fSPeter Avalos } else if (strcmp(ctype, "direct-tcpip") == 0) { 693ce74bacaSMatthew Dillon c = server_request_direct_tcpip(ssh, &reason, &errmsg); 69436e94dc5SPeter Avalos } else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) { 695ce74bacaSMatthew Dillon c = server_request_direct_streamlocal(ssh); 69618de8d7fSPeter Avalos } else if (strcmp(ctype, "tun@openssh.com") == 0) { 697ce74bacaSMatthew Dillon c = server_request_tun(ssh); 69818de8d7fSPeter Avalos } 69918de8d7fSPeter Avalos if (c != NULL) { 700664f4763Szrj debug("%s: confirm %s", __func__, ctype); 701*0cbfa66cSDaniel Fojt c->remote_id = rchan; 702ce74bacaSMatthew Dillon c->have_remote_id = 1; 70318de8d7fSPeter Avalos c->remote_window = rwindow; 70418de8d7fSPeter Avalos c->remote_maxpacket = rmaxpack; 70518de8d7fSPeter Avalos if (c->type != SSH_CHANNEL_CONNECTING) { 706664f4763Szrj if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 || 707664f4763Szrj (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || 708664f4763Szrj (r = sshpkt_put_u32(ssh, c->self)) != 0 || 709664f4763Szrj (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || 710664f4763Szrj (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 || 711664f4763Szrj (r = sshpkt_send(ssh)) != 0) { 712664f4763Szrj sshpkt_fatal(ssh, r, 713664f4763Szrj "%s: send open confirm", __func__); 714664f4763Szrj } 71518de8d7fSPeter Avalos } 71618de8d7fSPeter Avalos } else { 717664f4763Szrj debug("%s: failure %s", __func__, ctype); 718664f4763Szrj if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 || 719664f4763Szrj (r = sshpkt_put_u32(ssh, rchan)) != 0 || 720664f4763Szrj (r = sshpkt_put_u32(ssh, reason)) != 0 || 721664f4763Szrj (r = sshpkt_put_cstring(ssh, errmsg ? errmsg : "open failed")) != 0 || 722664f4763Szrj (r = sshpkt_put_cstring(ssh, "")) != 0 || 723664f4763Szrj (r = sshpkt_send(ssh)) != 0) { 724664f4763Szrj sshpkt_fatal(ssh, r, 725664f4763Szrj "%s: send open failure", __func__); 72618de8d7fSPeter Avalos } 72718de8d7fSPeter Avalos } 72836e94dc5SPeter Avalos free(ctype); 729e9778795SPeter Avalos return 0; 73018de8d7fSPeter Avalos } 73118de8d7fSPeter Avalos 732e9778795SPeter Avalos static int 733ce74bacaSMatthew Dillon server_input_hostkeys_prove(struct ssh *ssh, struct sshbuf **respp) 734e9778795SPeter Avalos { 735e9778795SPeter Avalos struct sshbuf *resp = NULL; 736e9778795SPeter Avalos struct sshbuf *sigbuf = NULL; 737e9778795SPeter Avalos struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL; 738664f4763Szrj int r, ndx, kexsigtype, use_kexsigtype, success = 0; 739e9778795SPeter Avalos const u_char *blob; 740e9778795SPeter Avalos u_char *sig = 0; 741e9778795SPeter Avalos size_t blen, slen; 742e9778795SPeter Avalos 743e9778795SPeter Avalos if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL) 744e9778795SPeter Avalos fatal("%s: sshbuf_new", __func__); 745e9778795SPeter Avalos 746664f4763Szrj kexsigtype = sshkey_type_plain( 747664f4763Szrj sshkey_type_from_name(ssh->kex->hostkey_alg)); 748e9778795SPeter Avalos while (ssh_packet_remaining(ssh) > 0) { 749e9778795SPeter Avalos sshkey_free(key); 750e9778795SPeter Avalos key = NULL; 751e9778795SPeter Avalos if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 || 752e9778795SPeter Avalos (r = sshkey_from_blob(blob, blen, &key)) != 0) { 753e9778795SPeter Avalos error("%s: couldn't parse key: %s", 754e9778795SPeter Avalos __func__, ssh_err(r)); 755e9778795SPeter Avalos goto out; 756e9778795SPeter Avalos } 757e9778795SPeter Avalos /* 758e9778795SPeter Avalos * Better check that this is actually one of our hostkeys 759e9778795SPeter Avalos * before attempting to sign anything with it. 760e9778795SPeter Avalos */ 761e9778795SPeter Avalos if ((ndx = ssh->kex->host_key_index(key, 1, ssh)) == -1) { 762e9778795SPeter Avalos error("%s: unknown host %s key", 763e9778795SPeter Avalos __func__, sshkey_type(key)); 764e9778795SPeter Avalos goto out; 765e9778795SPeter Avalos } 766e9778795SPeter Avalos /* 767e9778795SPeter Avalos * XXX refactor: make kex->sign just use an index rather 768e9778795SPeter Avalos * than passing in public and private keys 769e9778795SPeter Avalos */ 770e9778795SPeter Avalos if ((key_prv = get_hostkey_by_index(ndx)) == NULL && 771e9778795SPeter Avalos (key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) { 772e9778795SPeter Avalos error("%s: can't retrieve hostkey %d", __func__, ndx); 773e9778795SPeter Avalos goto out; 774e9778795SPeter Avalos } 775e9778795SPeter Avalos sshbuf_reset(sigbuf); 776e9778795SPeter Avalos free(sig); 777e9778795SPeter Avalos sig = NULL; 778664f4763Szrj /* 779664f4763Szrj * For RSA keys, prefer to use the signature type negotiated 780664f4763Szrj * during KEX to the default (SHA1). 781664f4763Szrj */ 782664f4763Szrj use_kexsigtype = kexsigtype == KEY_RSA && 783664f4763Szrj sshkey_type_plain(key->type) == KEY_RSA; 784e9778795SPeter Avalos if ((r = sshbuf_put_cstring(sigbuf, 785e9778795SPeter Avalos "hostkeys-prove-00@openssh.com")) != 0 || 786e9778795SPeter Avalos (r = sshbuf_put_string(sigbuf, 787e9778795SPeter Avalos ssh->kex->session_id, ssh->kex->session_id_len)) != 0 || 788e9778795SPeter Avalos (r = sshkey_puts(key, sigbuf)) != 0 || 789664f4763Szrj (r = ssh->kex->sign(ssh, key_prv, key_pub, &sig, &slen, 790664f4763Szrj sshbuf_ptr(sigbuf), sshbuf_len(sigbuf), 791664f4763Szrj use_kexsigtype ? ssh->kex->hostkey_alg : NULL)) != 0 || 792e9778795SPeter Avalos (r = sshbuf_put_string(resp, sig, slen)) != 0) { 793e9778795SPeter Avalos error("%s: couldn't prepare signature: %s", 794e9778795SPeter Avalos __func__, ssh_err(r)); 795e9778795SPeter Avalos goto out; 796e9778795SPeter Avalos } 797e9778795SPeter Avalos } 798e9778795SPeter Avalos /* Success */ 799e9778795SPeter Avalos *respp = resp; 800e9778795SPeter Avalos resp = NULL; /* don't free it */ 801e9778795SPeter Avalos success = 1; 802e9778795SPeter Avalos out: 803e9778795SPeter Avalos free(sig); 804e9778795SPeter Avalos sshbuf_free(resp); 805e9778795SPeter Avalos sshbuf_free(sigbuf); 806e9778795SPeter Avalos sshkey_free(key); 807e9778795SPeter Avalos return success; 808e9778795SPeter Avalos } 809e9778795SPeter Avalos 810e9778795SPeter Avalos static int 811ce74bacaSMatthew Dillon server_input_global_request(int type, u_int32_t seq, struct ssh *ssh) 81218de8d7fSPeter Avalos { 813664f4763Szrj char *rtype = NULL; 814664f4763Szrj u_char want_reply = 0; 815e9778795SPeter Avalos int r, success = 0, allocated_listen_port = 0; 816664f4763Szrj u_int port = 0; 817e9778795SPeter Avalos struct sshbuf *resp = NULL; 818ce74bacaSMatthew Dillon struct passwd *pw = the_authctxt->pw; 81936e94dc5SPeter Avalos struct Forward fwd; 82018de8d7fSPeter Avalos 82136e94dc5SPeter Avalos memset(&fwd, 0, sizeof(fwd)); 822664f4763Szrj if (pw == NULL || !the_authctxt->valid) 823664f4763Szrj fatal("%s: no/invalid user", __func__); 82418de8d7fSPeter Avalos 825664f4763Szrj if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 || 826664f4763Szrj (r = sshpkt_get_u8(ssh, &want_reply)) != 0) 827664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 828664f4763Szrj debug("%s: rtype %s want_reply %d", __func__, rtype, want_reply); 829664f4763Szrj 830664f4763Szrj /* -R style forwarding */ 831664f4763Szrj if (strcmp(rtype, "tcpip-forward") == 0) { 832664f4763Szrj if ((r = sshpkt_get_cstring(ssh, &fwd.listen_host, NULL)) != 0 || 833664f4763Szrj (r = sshpkt_get_u32(ssh, &port)) != 0) 834664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse tcpip-forward", __func__); 835664f4763Szrj debug("%s: tcpip-forward listen %s port %u", __func__, 836664f4763Szrj fwd.listen_host, port); 837664f4763Szrj if (port <= INT_MAX) 838664f4763Szrj fwd.listen_port = (int)port; 83918de8d7fSPeter Avalos /* check permissions */ 840664f4763Szrj if (port > INT_MAX || 841664f4763Szrj (options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 || 842664f4763Szrj !auth_opts->permit_port_forwarding_flag || 843664f4763Szrj options.disable_forwarding || 844e9778795SPeter Avalos (!want_reply && fwd.listen_port == 0) || 845ce74bacaSMatthew Dillon (fwd.listen_port != 0 && 846ce74bacaSMatthew Dillon !bind_permitted(fwd.listen_port, pw->pw_uid))) { 84718de8d7fSPeter Avalos success = 0; 848664f4763Szrj ssh_packet_send_debug(ssh, "Server has disabled port forwarding."); 84918de8d7fSPeter Avalos } else { 85018de8d7fSPeter Avalos /* Start listening on the port */ 851ce74bacaSMatthew Dillon success = channel_setup_remote_fwd_listener(ssh, &fwd, 85236e94dc5SPeter Avalos &allocated_listen_port, &options.fwd_opts); 85318de8d7fSPeter Avalos } 854e9778795SPeter Avalos if ((resp = sshbuf_new()) == NULL) 855e9778795SPeter Avalos fatal("%s: sshbuf_new", __func__); 856e9778795SPeter Avalos if (allocated_listen_port != 0 && 857e9778795SPeter Avalos (r = sshbuf_put_u32(resp, allocated_listen_port)) != 0) 858e9778795SPeter Avalos fatal("%s: sshbuf_put_u32: %s", __func__, ssh_err(r)); 85918de8d7fSPeter Avalos } else if (strcmp(rtype, "cancel-tcpip-forward") == 0) { 860664f4763Szrj if ((r = sshpkt_get_cstring(ssh, &fwd.listen_host, NULL)) != 0 || 861664f4763Szrj (r = sshpkt_get_u32(ssh, &port)) != 0) 862664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse cancel-tcpip-forward", __func__); 86318de8d7fSPeter Avalos 86418de8d7fSPeter Avalos debug("%s: cancel-tcpip-forward addr %s port %d", __func__, 865664f4763Szrj fwd.listen_host, port); 866664f4763Szrj if (port <= INT_MAX) { 867664f4763Szrj fwd.listen_port = (int)port; 868ce74bacaSMatthew Dillon success = channel_cancel_rport_listener(ssh, &fwd); 869664f4763Szrj } 87036e94dc5SPeter Avalos } else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) { 871664f4763Szrj if ((r = sshpkt_get_cstring(ssh, &fwd.listen_path, NULL)) != 0) 872664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse streamlocal-forward@openssh.com", __func__); 873664f4763Szrj debug("%s: streamlocal-forward listen path %s", __func__, 87436e94dc5SPeter Avalos fwd.listen_path); 87536e94dc5SPeter Avalos 87636e94dc5SPeter Avalos /* check permissions */ 87736e94dc5SPeter Avalos if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0 878664f4763Szrj || !auth_opts->permit_port_forwarding_flag || 879664f4763Szrj options.disable_forwarding || 880ce74bacaSMatthew Dillon (pw->pw_uid != 0 && !use_privsep)) { 88136e94dc5SPeter Avalos success = 0; 882664f4763Szrj ssh_packet_send_debug(ssh, "Server has disabled " 883ce74bacaSMatthew Dillon "streamlocal forwarding."); 88436e94dc5SPeter Avalos } else { 88536e94dc5SPeter Avalos /* Start listening on the socket */ 886ce74bacaSMatthew Dillon success = channel_setup_remote_fwd_listener(ssh, 88736e94dc5SPeter Avalos &fwd, NULL, &options.fwd_opts); 88836e94dc5SPeter Avalos } 88936e94dc5SPeter Avalos } else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) { 890664f4763Szrj if ((r = sshpkt_get_cstring(ssh, &fwd.listen_path, NULL)) != 0) 891664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse cancel-streamlocal-forward@openssh.com", __func__); 89236e94dc5SPeter Avalos debug("%s: cancel-streamlocal-forward path %s", __func__, 89336e94dc5SPeter Avalos fwd.listen_path); 89436e94dc5SPeter Avalos 895ce74bacaSMatthew Dillon success = channel_cancel_rport_listener(ssh, &fwd); 89618de8d7fSPeter Avalos } else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) { 89718de8d7fSPeter Avalos no_more_sessions = 1; 89818de8d7fSPeter Avalos success = 1; 899e9778795SPeter Avalos } else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) { 900ce74bacaSMatthew Dillon success = server_input_hostkeys_prove(ssh, &resp); 90118de8d7fSPeter Avalos } 902664f4763Szrj /* XXX sshpkt_get_end() */ 90318de8d7fSPeter Avalos if (want_reply) { 904664f4763Szrj if ((r = sshpkt_start(ssh, success ? 905664f4763Szrj SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE)) != 0 || 906664f4763Szrj (success && resp != NULL && (r = sshpkt_putb(ssh, resp)) != 0) || 907664f4763Szrj (r = sshpkt_send(ssh)) != 0 || 908664f4763Szrj (r = ssh_packet_write_wait(ssh)) != 0) 909664f4763Szrj sshpkt_fatal(ssh, r, "%s: send reply", __func__); 91018de8d7fSPeter Avalos } 911664f4763Szrj free(fwd.listen_host); 912664f4763Szrj free(fwd.listen_path); 91336e94dc5SPeter Avalos free(rtype); 914e9778795SPeter Avalos sshbuf_free(resp); 915e9778795SPeter Avalos return 0; 91618de8d7fSPeter Avalos } 91718de8d7fSPeter Avalos 918e9778795SPeter Avalos static int 919ce74bacaSMatthew Dillon server_input_channel_req(int type, u_int32_t seq, struct ssh *ssh) 92018de8d7fSPeter Avalos { 92118de8d7fSPeter Avalos Channel *c; 922664f4763Szrj int r, success = 0; 923664f4763Szrj char *rtype = NULL; 924664f4763Szrj u_char want_reply = 0; 925664f4763Szrj u_int id = 0; 92618de8d7fSPeter Avalos 927664f4763Szrj if ((r = sshpkt_get_u32(ssh, &id)) != 0 || 928664f4763Szrj (r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 || 929664f4763Szrj (r = sshpkt_get_u8(ssh, &want_reply)) != 0) 930664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 93118de8d7fSPeter Avalos 932664f4763Szrj debug("server_input_channel_req: channel %u request %s reply %d", 933664f4763Szrj id, rtype, want_reply); 93418de8d7fSPeter Avalos 935664f4763Szrj if (id >= INT_MAX || (c = channel_lookup(ssh, (int)id)) == NULL) { 936664f4763Szrj ssh_packet_disconnect(ssh, "%s: unknown channel %d", 937664f4763Szrj __func__, id); 938664f4763Szrj } 93918de8d7fSPeter Avalos if (!strcmp(rtype, "eow@openssh.com")) { 940664f4763Szrj if ((r = sshpkt_get_end(ssh)) != 0) 941664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 942ce74bacaSMatthew Dillon chan_rcvd_eow(ssh, c); 94318de8d7fSPeter Avalos } else if ((c->type == SSH_CHANNEL_LARVAL || 94418de8d7fSPeter Avalos c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0) 945ce74bacaSMatthew Dillon success = session_input_channel_req(ssh, c, rtype); 946664f4763Szrj if (want_reply && !(c->flags & CHAN_CLOSE_SENT)) { 947ce74bacaSMatthew Dillon if (!c->have_remote_id) 948ce74bacaSMatthew Dillon fatal("%s: channel %d: no remote_id", 949ce74bacaSMatthew Dillon __func__, c->self); 950664f4763Szrj if ((r = sshpkt_start(ssh, success ? 951664f4763Szrj SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE)) != 0 || 952664f4763Szrj (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || 953664f4763Szrj (r = sshpkt_send(ssh)) != 0) 954664f4763Szrj sshpkt_fatal(ssh, r, "%s: send reply", __func__); 95518de8d7fSPeter Avalos } 95636e94dc5SPeter Avalos free(rtype); 957e9778795SPeter Avalos return 0; 95818de8d7fSPeter Avalos } 95918de8d7fSPeter Avalos 96018de8d7fSPeter Avalos static void 961664f4763Szrj server_init_dispatch(struct ssh *ssh) 96218de8d7fSPeter Avalos { 963ce74bacaSMatthew Dillon debug("server_init_dispatch"); 964664f4763Szrj ssh_dispatch_init(ssh, &dispatch_protocol_error); 965664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); 966664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_DATA, &channel_input_data); 967664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); 968664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); 969664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open); 970664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); 971664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); 972664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req); 973664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); 974664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request); 97518de8d7fSPeter Avalos /* client_alive */ 976664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive); 977664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive); 978664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive); 979664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive); 98018de8d7fSPeter Avalos /* rekeying */ 981664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit); 98218de8d7fSPeter Avalos } 983