1*ee116499SAntonio Huete Jimenez /* $OpenBSD: serverloop.c,v 1.232 2022/04/20 04:19:11 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> 53*ee116499SAntonio Huete Jimenez #ifdef HAVE_POLL_H 54*ee116499SAntonio Huete Jimenez #include <poll.h> 55*ee116499SAntonio Huete Jimenez #endif 5618de8d7fSPeter Avalos #include <signal.h> 5718de8d7fSPeter Avalos #include <string.h> 5818de8d7fSPeter Avalos #include <termios.h> 5918de8d7fSPeter Avalos #include <unistd.h> 6018de8d7fSPeter Avalos #include <stdarg.h> 6118de8d7fSPeter Avalos 6218de8d7fSPeter Avalos #include "openbsd-compat/sys-queue.h" 6318de8d7fSPeter Avalos #include "xmalloc.h" 6418de8d7fSPeter Avalos #include "packet.h" 65664f4763Szrj #include "sshbuf.h" 6618de8d7fSPeter Avalos #include "log.h" 6736e94dc5SPeter Avalos #include "misc.h" 6818de8d7fSPeter Avalos #include "servconf.h" 6918de8d7fSPeter Avalos #include "canohost.h" 7018de8d7fSPeter Avalos #include "sshpty.h" 7118de8d7fSPeter Avalos #include "channels.h" 7218de8d7fSPeter Avalos #include "compat.h" 7318de8d7fSPeter Avalos #include "ssh2.h" 74664f4763Szrj #include "sshkey.h" 7518de8d7fSPeter Avalos #include "cipher.h" 7618de8d7fSPeter Avalos #include "kex.h" 7718de8d7fSPeter Avalos #include "hostfile.h" 7818de8d7fSPeter Avalos #include "auth.h" 7918de8d7fSPeter Avalos #include "session.h" 8018de8d7fSPeter Avalos #include "dispatch.h" 8118de8d7fSPeter Avalos #include "auth-options.h" 8218de8d7fSPeter Avalos #include "serverloop.h" 83e9778795SPeter Avalos #include "ssherr.h" 8418de8d7fSPeter Avalos 8518de8d7fSPeter Avalos extern ServerOptions options; 8618de8d7fSPeter Avalos 8718de8d7fSPeter Avalos /* XXX */ 8818de8d7fSPeter Avalos extern Authctxt *the_authctxt; 89664f4763Szrj extern struct sshauthopt *auth_opts; 9018de8d7fSPeter Avalos extern int use_privsep; 9118de8d7fSPeter Avalos 9218de8d7fSPeter Avalos static int no_more_sessions = 0; /* Disallow further sessions. */ 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 */ 100664f4763Szrj static void server_init_dispatch(struct ssh *); 101664f4763Szrj 102664f4763Szrj /* requested tunnel forwarding interface(s), shared with session.c */ 103664f4763Szrj char *tun_fwd_ifnames = NULL; 104664f4763Szrj 105664f4763Szrj /* returns 1 if bind to specified port by specified user is permitted */ 106664f4763Szrj static int 107664f4763Szrj bind_permitted(int port, uid_t uid) 108664f4763Szrj { 109664f4763Szrj if (use_privsep) 110664f4763Szrj return 1; /* allow system to decide */ 111664f4763Szrj if (port < IPPORT_RESERVED && uid != 0) 112664f4763Szrj return 0; 113664f4763Szrj return 1; 114664f4763Szrj } 11518de8d7fSPeter Avalos 11618de8d7fSPeter Avalos /*ARGSUSED*/ 11718de8d7fSPeter Avalos static void 11818de8d7fSPeter Avalos sigchld_handler(int sig) 11918de8d7fSPeter Avalos { 12018de8d7fSPeter Avalos child_terminated = 1; 12118de8d7fSPeter Avalos } 12218de8d7fSPeter Avalos 12318de8d7fSPeter Avalos /*ARGSUSED*/ 12418de8d7fSPeter Avalos static void 12518de8d7fSPeter Avalos sigterm_handler(int sig) 12618de8d7fSPeter Avalos { 12718de8d7fSPeter Avalos received_sigterm = sig; 12818de8d7fSPeter Avalos } 12918de8d7fSPeter Avalos 13018de8d7fSPeter Avalos static void 131ce74bacaSMatthew Dillon client_alive_check(struct ssh *ssh) 13218de8d7fSPeter Avalos { 133664f4763Szrj char remote_id[512]; 134664f4763Szrj int r, channel_id; 13518de8d7fSPeter Avalos 13618de8d7fSPeter Avalos /* timeout, check to see how many we have had */ 1370cbfa66cSDaniel Fojt if (options.client_alive_count_max > 0 && 1380cbfa66cSDaniel Fojt ssh_packet_inc_alive_timeouts(ssh) > 139664f4763Szrj options.client_alive_count_max) { 140664f4763Szrj sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id)); 141664f4763Szrj logit("Timeout, client not responding from %s", remote_id); 14218de8d7fSPeter Avalos cleanup_exit(255); 14318de8d7fSPeter Avalos } 14418de8d7fSPeter Avalos 14518de8d7fSPeter Avalos /* 14618de8d7fSPeter Avalos * send a bogus global/channel request with "wantreply", 14718de8d7fSPeter Avalos * we should get back a failure 14818de8d7fSPeter Avalos */ 149ce74bacaSMatthew Dillon if ((channel_id = channel_find_open(ssh)) == -1) { 150664f4763Szrj if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 || 151664f4763Szrj (r = sshpkt_put_cstring(ssh, "keepalive@openssh.com")) 152664f4763Szrj != 0 || 153664f4763Szrj (r = sshpkt_put_u8(ssh, 1)) != 0) /* boolean: want reply */ 15450a69bb5SSascha Wildner fatal_fr(r, "compose"); 15518de8d7fSPeter Avalos } else { 156ce74bacaSMatthew Dillon channel_request_start(ssh, channel_id, 157ce74bacaSMatthew Dillon "keepalive@openssh.com", 1); 15818de8d7fSPeter Avalos } 159664f4763Szrj if ((r = sshpkt_send(ssh)) != 0) 16050a69bb5SSascha Wildner fatal_fr(r, "send"); 16118de8d7fSPeter Avalos } 16218de8d7fSPeter Avalos 16318de8d7fSPeter Avalos /* 164*ee116499SAntonio Huete Jimenez * Sleep in ppoll() until we can do something. 165*ee116499SAntonio Huete Jimenez * Optionally, a maximum time can be specified for the duration of 166*ee116499SAntonio Huete Jimenez * the wait (0 = infinite). 16718de8d7fSPeter Avalos */ 16818de8d7fSPeter Avalos static void 169ce74bacaSMatthew Dillon wait_until_can_do_something(struct ssh *ssh, 170*ee116499SAntonio Huete Jimenez int connection_in, int connection_out, struct pollfd **pfdp, 171*ee116499SAntonio Huete Jimenez u_int *npfd_allocp, u_int *npfd_activep, u_int64_t max_time_ms, 172*ee116499SAntonio Huete Jimenez sigset_t *sigsetp, int *conn_in_readyp, int *conn_out_readyp) 17318de8d7fSPeter Avalos { 17450a69bb5SSascha Wildner struct timespec ts, *tsp; 17518de8d7fSPeter Avalos int ret; 17699e85e0dSPeter Avalos time_t minwait_secs = 0; 17718de8d7fSPeter Avalos int client_alive_scheduled = 0; 178*ee116499SAntonio Huete Jimenez u_int p; 179664f4763Szrj /* time we last heard from the client OR sent a keepalive */ 180ce74bacaSMatthew Dillon static time_t last_client_time; 18118de8d7fSPeter Avalos 182*ee116499SAntonio Huete Jimenez *conn_in_readyp = *conn_out_readyp = 0; 183*ee116499SAntonio Huete Jimenez 184*ee116499SAntonio Huete Jimenez /* Prepare channel poll. First two pollfd entries are reserved */ 185*ee116499SAntonio Huete Jimenez channel_prepare_poll(ssh, pfdp, npfd_allocp, npfd_activep, 186*ee116499SAntonio Huete Jimenez 2, &minwait_secs); 187*ee116499SAntonio Huete Jimenez if (*npfd_activep < 2) 188*ee116499SAntonio Huete Jimenez fatal_f("bad npfd %u", *npfd_activep); /* shouldn't happen */ 18999e85e0dSPeter Avalos 190e9778795SPeter Avalos /* XXX need proper deadline system for rekey/client alive */ 19199e85e0dSPeter Avalos if (minwait_secs != 0) 192ce74bacaSMatthew Dillon max_time_ms = MINIMUM(max_time_ms, (u_int)minwait_secs * 1000); 19399e85e0dSPeter Avalos 19418de8d7fSPeter Avalos /* 19518de8d7fSPeter Avalos * if using client_alive, set the max timeout accordingly, 19618de8d7fSPeter Avalos * and indicate that this particular timeout was for client 19718de8d7fSPeter Avalos * alive by setting the client_alive_scheduled flag. 19818de8d7fSPeter Avalos * 19918de8d7fSPeter Avalos * this could be randomized somewhat to make traffic 20018de8d7fSPeter Avalos * analysis more difficult, but we're not doing it yet. 20118de8d7fSPeter Avalos */ 202ce74bacaSMatthew Dillon if (options.client_alive_interval) { 203e9778795SPeter Avalos uint64_t keepalive_ms = 204e9778795SPeter Avalos (uint64_t)options.client_alive_interval * 1000; 205e9778795SPeter Avalos 206664f4763Szrj if (max_time_ms == 0 || max_time_ms > keepalive_ms) { 207e9778795SPeter Avalos max_time_ms = keepalive_ms; 208664f4763Szrj client_alive_scheduled = 1; 209664f4763Szrj } 21050a69bb5SSascha Wildner if (last_client_time == 0) 21150a69bb5SSascha Wildner last_client_time = monotime(); 21218de8d7fSPeter Avalos } 21318de8d7fSPeter Avalos 21418de8d7fSPeter Avalos #if 0 21518de8d7fSPeter Avalos /* wrong: bad condition XXX */ 21618de8d7fSPeter Avalos if (channel_not_very_much_buffered_data()) 21718de8d7fSPeter Avalos #endif 218*ee116499SAntonio Huete Jimenez /* Monitor client connection on reserved pollfd entries */ 219*ee116499SAntonio Huete Jimenez (*pfdp)[0].fd = connection_in; 220*ee116499SAntonio Huete Jimenez (*pfdp)[0].events = POLLIN; 221*ee116499SAntonio Huete Jimenez (*pfdp)[1].fd = connection_out; 222*ee116499SAntonio Huete Jimenez (*pfdp)[1].events = ssh_packet_have_data_to_write(ssh) ? POLLOUT : 0; 22318de8d7fSPeter Avalos 22418de8d7fSPeter Avalos /* 22518de8d7fSPeter Avalos * If child has terminated and there is enough buffer space to read 22618de8d7fSPeter Avalos * from it, then read as much as is available and exit. 22718de8d7fSPeter Avalos */ 228664f4763Szrj if (child_terminated && ssh_packet_not_very_much_data_to_write(ssh)) 229e9778795SPeter Avalos if (max_time_ms == 0 || client_alive_scheduled) 230e9778795SPeter Avalos max_time_ms = 100; 23118de8d7fSPeter Avalos 232e9778795SPeter Avalos if (max_time_ms == 0) 23350a69bb5SSascha Wildner tsp = NULL; 23418de8d7fSPeter Avalos else { 23550a69bb5SSascha Wildner ts.tv_sec = max_time_ms / 1000; 23650a69bb5SSascha Wildner ts.tv_nsec = 1000000 * (max_time_ms % 1000); 23750a69bb5SSascha Wildner tsp = &ts; 23818de8d7fSPeter Avalos } 23918de8d7fSPeter Avalos 24018de8d7fSPeter Avalos /* Wait for something to happen, or the timeout to expire. */ 241*ee116499SAntonio Huete Jimenez ret = ppoll(*pfdp, *npfd_activep, tsp, sigsetp); 24218de8d7fSPeter Avalos 24318de8d7fSPeter Avalos if (ret == -1) { 244*ee116499SAntonio Huete Jimenez for (p = 0; p < *npfd_activep; p++) 245*ee116499SAntonio Huete Jimenez (*pfdp)[p].revents = 0; 24618de8d7fSPeter Avalos if (errno != EINTR) 247*ee116499SAntonio Huete Jimenez fatal_f("ppoll: %.100s", strerror(errno)); 248*ee116499SAntonio Huete Jimenez return; 249*ee116499SAntonio Huete Jimenez } 250*ee116499SAntonio Huete Jimenez 251*ee116499SAntonio Huete Jimenez *conn_in_readyp = (*pfdp)[0].revents != 0; 252*ee116499SAntonio Huete Jimenez *conn_out_readyp = (*pfdp)[1].revents != 0; 253*ee116499SAntonio Huete Jimenez 254*ee116499SAntonio Huete Jimenez if (client_alive_scheduled) { 255ce74bacaSMatthew Dillon time_t now = monotime(); 256ce74bacaSMatthew Dillon 257664f4763Szrj /* 258*ee116499SAntonio Huete Jimenez * If the ppoll timed out, or returned for some other reason 259664f4763Szrj * but we haven't heard from the client in time, send keepalive. 260664f4763Szrj */ 261664f4763Szrj if (ret == 0 || (last_client_time != 0 && last_client_time + 262664f4763Szrj options.client_alive_interval <= now)) { 263ce74bacaSMatthew Dillon client_alive_check(ssh); 264ce74bacaSMatthew Dillon last_client_time = now; 265*ee116499SAntonio Huete Jimenez } else if (*conn_in_readyp) 266ce74bacaSMatthew Dillon last_client_time = now; 26718de8d7fSPeter Avalos } 26818de8d7fSPeter Avalos } 26918de8d7fSPeter Avalos 27018de8d7fSPeter Avalos /* 27118de8d7fSPeter Avalos * Processes input from the client and the program. Input data is stored 27218de8d7fSPeter Avalos * in buffers and processed later. 27318de8d7fSPeter Avalos */ 274ce74bacaSMatthew Dillon static int 275*ee116499SAntonio Huete Jimenez process_input(struct ssh *ssh, int connection_in) 27618de8d7fSPeter Avalos { 277*ee116499SAntonio Huete Jimenez int r; 27818de8d7fSPeter Avalos 279*ee116499SAntonio Huete Jimenez if ((r = ssh_packet_process_read(ssh, connection_in)) == 0) 280*ee116499SAntonio Huete Jimenez return 0; /* success */ 281*ee116499SAntonio Huete Jimenez if (r == SSH_ERR_SYSTEM_ERROR) { 282*ee116499SAntonio Huete Jimenez if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK) 283*ee116499SAntonio Huete Jimenez return 0; 284*ee116499SAntonio Huete Jimenez if (errno == EPIPE) { 285e9778795SPeter Avalos verbose("Connection closed by %.100s port %d", 286e9778795SPeter Avalos ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); 287ce74bacaSMatthew Dillon return -1; 288*ee116499SAntonio Huete Jimenez } 28950a69bb5SSascha Wildner verbose("Read error from remote host %s port %d: %s", 29050a69bb5SSascha Wildner ssh_remote_ipaddr(ssh), ssh_remote_port(ssh), 29150a69bb5SSascha Wildner strerror(errno)); 29218de8d7fSPeter Avalos cleanup_exit(255); 29318de8d7fSPeter Avalos } 294*ee116499SAntonio Huete Jimenez return -1; 29518de8d7fSPeter Avalos } 29618de8d7fSPeter Avalos 29718de8d7fSPeter Avalos /* 29818de8d7fSPeter Avalos * Sends data from internal buffers to client program stdin. 29918de8d7fSPeter Avalos */ 30018de8d7fSPeter Avalos static void 301*ee116499SAntonio Huete Jimenez process_output(struct ssh *ssh, int connection_out) 30218de8d7fSPeter Avalos { 303664f4763Szrj int r; 304664f4763Szrj 30518de8d7fSPeter Avalos /* Send any buffered packet data to the client. */ 3060cbfa66cSDaniel Fojt if ((r = ssh_packet_write_poll(ssh)) != 0) { 3070cbfa66cSDaniel Fojt sshpkt_fatal(ssh, r, "%s: ssh_packet_write_poll", 3080cbfa66cSDaniel Fojt __func__); 3090cbfa66cSDaniel Fojt } 310664f4763Szrj } 31118de8d7fSPeter Avalos 31218de8d7fSPeter Avalos static void 313ce74bacaSMatthew Dillon process_buffered_input_packets(struct ssh *ssh) 31418de8d7fSPeter Avalos { 315ce74bacaSMatthew Dillon ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, NULL); 31618de8d7fSPeter Avalos } 31718de8d7fSPeter Avalos 31818de8d7fSPeter Avalos static void 319ce74bacaSMatthew Dillon collect_children(struct ssh *ssh) 32018de8d7fSPeter Avalos { 32118de8d7fSPeter Avalos pid_t pid; 32218de8d7fSPeter Avalos int status; 32318de8d7fSPeter Avalos 32418de8d7fSPeter Avalos if (child_terminated) { 32518de8d7fSPeter Avalos debug("Received SIGCHLD."); 32618de8d7fSPeter Avalos while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || 3270cbfa66cSDaniel Fojt (pid == -1 && errno == EINTR)) 32818de8d7fSPeter Avalos if (pid > 0) 329ce74bacaSMatthew Dillon session_close_by_pid(ssh, pid, status); 33018de8d7fSPeter Avalos child_terminated = 0; 33118de8d7fSPeter Avalos } 33218de8d7fSPeter Avalos } 33318de8d7fSPeter Avalos 33418de8d7fSPeter Avalos void 335ce74bacaSMatthew Dillon server_loop2(struct ssh *ssh, Authctxt *authctxt) 33618de8d7fSPeter Avalos { 337*ee116499SAntonio Huete Jimenez struct pollfd *pfd = NULL; 338*ee116499SAntonio Huete Jimenez u_int npfd_alloc = 0, npfd_active = 0; 339*ee116499SAntonio Huete Jimenez int r, conn_in_ready, conn_out_ready; 340*ee116499SAntonio Huete Jimenez u_int connection_in, connection_out; 34136e94dc5SPeter Avalos u_int64_t rekey_timeout_ms = 0; 34250a69bb5SSascha Wildner sigset_t bsigset, osigset; 34318de8d7fSPeter Avalos 34418de8d7fSPeter Avalos debug("Entering interactive session for SSH2."); 34518de8d7fSPeter Avalos 34650a69bb5SSascha Wildner if (sigemptyset(&bsigset) == -1 || sigaddset(&bsigset, SIGCHLD) == -1) 34750a69bb5SSascha Wildner error_f("bsigset setup: %s", strerror(errno)); 3480cbfa66cSDaniel Fojt ssh_signal(SIGCHLD, sigchld_handler); 34918de8d7fSPeter Avalos child_terminated = 0; 350664f4763Szrj connection_in = ssh_packet_get_connection_in(ssh); 351664f4763Szrj connection_out = ssh_packet_get_connection_out(ssh); 35218de8d7fSPeter Avalos 35318de8d7fSPeter Avalos if (!use_privsep) { 3540cbfa66cSDaniel Fojt ssh_signal(SIGTERM, sigterm_handler); 3550cbfa66cSDaniel Fojt ssh_signal(SIGINT, sigterm_handler); 3560cbfa66cSDaniel Fojt ssh_signal(SIGQUIT, sigterm_handler); 35718de8d7fSPeter Avalos } 35818de8d7fSPeter Avalos 359664f4763Szrj server_init_dispatch(ssh); 36018de8d7fSPeter Avalos 36118de8d7fSPeter Avalos for (;;) { 362ce74bacaSMatthew Dillon process_buffered_input_packets(ssh); 36318de8d7fSPeter Avalos 364ce74bacaSMatthew Dillon if (!ssh_packet_is_rekeying(ssh) && 365664f4763Szrj ssh_packet_not_very_much_data_to_write(ssh)) 366ce74bacaSMatthew Dillon channel_output_poll(ssh); 367664f4763Szrj if (options.rekey_interval > 0 && 368664f4763Szrj !ssh_packet_is_rekeying(ssh)) { 369664f4763Szrj rekey_timeout_ms = ssh_packet_get_rekey_timeout(ssh) * 370664f4763Szrj 1000; 371664f4763Szrj } else { 37236e94dc5SPeter Avalos rekey_timeout_ms = 0; 373664f4763Szrj } 37436e94dc5SPeter Avalos 37550a69bb5SSascha Wildner /* 37650a69bb5SSascha Wildner * Block SIGCHLD while we check for dead children, then pass 377*ee116499SAntonio Huete Jimenez * the old signal mask through to ppoll() so that it'll wake 37850a69bb5SSascha Wildner * up immediately if a child exits after we've called waitpid(). 37950a69bb5SSascha Wildner */ 38050a69bb5SSascha Wildner if (sigprocmask(SIG_BLOCK, &bsigset, &osigset) == -1) 38150a69bb5SSascha Wildner error_f("bsigset sigprocmask: %s", strerror(errno)); 38250a69bb5SSascha Wildner collect_children(ssh); 383ce74bacaSMatthew Dillon wait_until_can_do_something(ssh, connection_in, connection_out, 384*ee116499SAntonio Huete Jimenez &pfd, &npfd_alloc, &npfd_active, rekey_timeout_ms, &osigset, 385*ee116499SAntonio Huete Jimenez &conn_in_ready, &conn_out_ready); 38650a69bb5SSascha Wildner if (sigprocmask(SIG_UNBLOCK, &bsigset, &osigset) == -1) 38750a69bb5SSascha Wildner error_f("osigset sigprocmask: %s", strerror(errno)); 38818de8d7fSPeter Avalos 38918de8d7fSPeter Avalos if (received_sigterm) { 39036e94dc5SPeter Avalos logit("Exiting on signal %d", (int)received_sigterm); 39118de8d7fSPeter Avalos /* Clean up sessions, utmp, etc. */ 39218de8d7fSPeter Avalos cleanup_exit(255); 39318de8d7fSPeter Avalos } 39418de8d7fSPeter Avalos 395*ee116499SAntonio Huete Jimenez channel_after_poll(ssh, pfd, npfd_active); 396*ee116499SAntonio Huete Jimenez if (conn_in_ready && 397*ee116499SAntonio Huete Jimenez process_input(ssh, connection_in) < 0) 39818de8d7fSPeter Avalos break; 39950a69bb5SSascha Wildner /* A timeout may have triggered rekeying */ 40050a69bb5SSascha Wildner if ((r = ssh_packet_check_rekey(ssh)) != 0) 40150a69bb5SSascha Wildner fatal_fr(r, "cannot start rekeying"); 402*ee116499SAntonio Huete Jimenez if (conn_out_ready) 403*ee116499SAntonio Huete Jimenez process_output(ssh, connection_out); 40418de8d7fSPeter Avalos } 405ce74bacaSMatthew Dillon collect_children(ssh); 406*ee116499SAntonio Huete Jimenez free(pfd); 40718de8d7fSPeter Avalos 40818de8d7fSPeter Avalos /* free all channels, no more reads and writes */ 409ce74bacaSMatthew Dillon channel_free_all(ssh); 41018de8d7fSPeter Avalos 41118de8d7fSPeter Avalos /* free remaining sessions, e.g. remove wtmp entries */ 412ce74bacaSMatthew Dillon session_destroy_all(ssh, NULL); 41318de8d7fSPeter Avalos } 41418de8d7fSPeter Avalos 415e9778795SPeter Avalos static int 416ce74bacaSMatthew Dillon server_input_keep_alive(int type, u_int32_t seq, struct ssh *ssh) 41718de8d7fSPeter Avalos { 41818de8d7fSPeter Avalos debug("Got %d/%u for keepalive", type, seq); 41918de8d7fSPeter Avalos /* 42018de8d7fSPeter Avalos * reset timeout, since we got a sane answer from the client. 42118de8d7fSPeter Avalos * even if this was generated by something other than 42218de8d7fSPeter Avalos * the bogus CHANNEL_REQUEST we send for keepalives. 42318de8d7fSPeter Avalos */ 424664f4763Szrj ssh_packet_set_alive_timeouts(ssh, 0); 425e9778795SPeter Avalos return 0; 42618de8d7fSPeter Avalos } 42718de8d7fSPeter Avalos 42818de8d7fSPeter Avalos static Channel * 429ce74bacaSMatthew Dillon server_request_direct_tcpip(struct ssh *ssh, int *reason, const char **errmsg) 43018de8d7fSPeter Avalos { 43136e94dc5SPeter Avalos Channel *c = NULL; 432664f4763Szrj char *target = NULL, *originator = NULL; 433664f4763Szrj u_int target_port = 0, originator_port = 0; 434664f4763Szrj int r; 43518de8d7fSPeter Avalos 436664f4763Szrj if ((r = sshpkt_get_cstring(ssh, &target, NULL)) != 0 || 437664f4763Szrj (r = sshpkt_get_u32(ssh, &target_port)) != 0 || 438664f4763Szrj (r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 || 439664f4763Szrj (r = sshpkt_get_u32(ssh, &originator_port)) != 0 || 440664f4763Szrj (r = sshpkt_get_end(ssh)) != 0) 441664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 442664f4763Szrj if (target_port > 0xFFFF) { 44350a69bb5SSascha Wildner error_f("invalid target port"); 444664f4763Szrj *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; 445664f4763Szrj goto out; 446664f4763Szrj } 447664f4763Szrj if (originator_port > 0xFFFF) { 44850a69bb5SSascha Wildner error_f("invalid originator port"); 449664f4763Szrj *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; 450664f4763Szrj goto out; 451664f4763Szrj } 45218de8d7fSPeter Avalos 45350a69bb5SSascha Wildner debug_f("originator %s port %u, target %s port %u", 454664f4763Szrj originator, originator_port, target, target_port); 45518de8d7fSPeter Avalos 45636e94dc5SPeter Avalos /* XXX fine grained permissions */ 45736e94dc5SPeter Avalos if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 && 458664f4763Szrj auth_opts->permit_port_forwarding_flag && 459664f4763Szrj !options.disable_forwarding) { 460ce74bacaSMatthew Dillon c = channel_connect_to_port(ssh, target, target_port, 461ce74bacaSMatthew Dillon "direct-tcpip", "direct-tcpip", reason, errmsg); 46236e94dc5SPeter Avalos } else { 46336e94dc5SPeter Avalos logit("refused local port forward: " 46436e94dc5SPeter Avalos "originator %s port %d, target %s port %d", 46536e94dc5SPeter Avalos originator, originator_port, target, target_port); 466ce74bacaSMatthew Dillon if (reason != NULL) 467ce74bacaSMatthew Dillon *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; 46836e94dc5SPeter Avalos } 46918de8d7fSPeter Avalos 470664f4763Szrj out: 47136e94dc5SPeter Avalos free(originator); 47236e94dc5SPeter Avalos free(target); 47336e94dc5SPeter Avalos return c; 47436e94dc5SPeter Avalos } 47536e94dc5SPeter Avalos 47636e94dc5SPeter Avalos static Channel * 477ce74bacaSMatthew Dillon server_request_direct_streamlocal(struct ssh *ssh) 47836e94dc5SPeter Avalos { 47936e94dc5SPeter Avalos Channel *c = NULL; 480664f4763Szrj char *target = NULL, *originator = NULL; 481664f4763Szrj u_int originator_port = 0; 482ce74bacaSMatthew Dillon struct passwd *pw = the_authctxt->pw; 483664f4763Szrj int r; 484ce74bacaSMatthew Dillon 485ce74bacaSMatthew Dillon if (pw == NULL || !the_authctxt->valid) 48650a69bb5SSascha Wildner fatal_f("no/invalid user"); 48736e94dc5SPeter Avalos 488664f4763Szrj if ((r = sshpkt_get_cstring(ssh, &target, NULL)) != 0 || 489664f4763Szrj (r = sshpkt_get_cstring(ssh, &originator, NULL)) != 0 || 490664f4763Szrj (r = sshpkt_get_u32(ssh, &originator_port)) != 0 || 491664f4763Szrj (r = sshpkt_get_end(ssh)) != 0) 492664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 493664f4763Szrj if (originator_port > 0xFFFF) { 49450a69bb5SSascha Wildner error_f("invalid originator port"); 495664f4763Szrj goto out; 496664f4763Szrj } 49736e94dc5SPeter Avalos 49850a69bb5SSascha Wildner debug_f("originator %s port %d, target %s", 49936e94dc5SPeter Avalos originator, originator_port, target); 50036e94dc5SPeter Avalos 50136e94dc5SPeter Avalos /* XXX fine grained permissions */ 50236e94dc5SPeter Avalos if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 && 503664f4763Szrj auth_opts->permit_port_forwarding_flag && 504664f4763Szrj !options.disable_forwarding && (pw->pw_uid == 0 || use_privsep)) { 505ce74bacaSMatthew Dillon c = channel_connect_to_path(ssh, target, 50636e94dc5SPeter Avalos "direct-streamlocal@openssh.com", "direct-streamlocal"); 50736e94dc5SPeter Avalos } else { 50836e94dc5SPeter Avalos logit("refused streamlocal port forward: " 50936e94dc5SPeter Avalos "originator %s port %d, target %s", 51036e94dc5SPeter Avalos originator, originator_port, target); 51136e94dc5SPeter Avalos } 51236e94dc5SPeter Avalos 513664f4763Szrj out: 51436e94dc5SPeter Avalos free(originator); 51536e94dc5SPeter Avalos free(target); 51618de8d7fSPeter Avalos return c; 51718de8d7fSPeter Avalos } 51818de8d7fSPeter Avalos 51918de8d7fSPeter Avalos static Channel * 520ce74bacaSMatthew Dillon server_request_tun(struct ssh *ssh) 52118de8d7fSPeter Avalos { 52218de8d7fSPeter Avalos Channel *c = NULL; 523664f4763Szrj u_int mode, tun; 524664f4763Szrj int r, sock; 525664f4763Szrj char *tmp, *ifname = NULL; 52618de8d7fSPeter Avalos 527664f4763Szrj if ((r = sshpkt_get_u32(ssh, &mode)) != 0) 528664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse mode", __func__); 52918de8d7fSPeter Avalos switch (mode) { 53018de8d7fSPeter Avalos case SSH_TUNMODE_POINTOPOINT: 53118de8d7fSPeter Avalos case SSH_TUNMODE_ETHERNET: 53218de8d7fSPeter Avalos break; 53318de8d7fSPeter Avalos default: 534664f4763Szrj ssh_packet_send_debug(ssh, "Unsupported tunnel device mode."); 53518de8d7fSPeter Avalos return NULL; 53618de8d7fSPeter Avalos } 53718de8d7fSPeter Avalos if ((options.permit_tun & mode) == 0) { 538664f4763Szrj ssh_packet_send_debug(ssh, "Server has rejected tunnel device " 53918de8d7fSPeter Avalos "forwarding"); 54018de8d7fSPeter Avalos return NULL; 54118de8d7fSPeter Avalos } 54218de8d7fSPeter Avalos 543664f4763Szrj if ((r = sshpkt_get_u32(ssh, &tun)) != 0) 544664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse device", __func__); 545664f4763Szrj if (tun > INT_MAX) { 54650a69bb5SSascha Wildner debug_f("invalid tun"); 54718de8d7fSPeter Avalos goto done; 54818de8d7fSPeter Avalos } 549664f4763Szrj if (auth_opts->force_tun_device != -1) { 550664f4763Szrj if (tun != SSH_TUNID_ANY && 551664f4763Szrj auth_opts->force_tun_device != (int)tun) 552664f4763Szrj goto done; 553664f4763Szrj tun = auth_opts->force_tun_device; 554664f4763Szrj } 555664f4763Szrj sock = tun_open(tun, mode, &ifname); 55618de8d7fSPeter Avalos if (sock < 0) 55718de8d7fSPeter Avalos goto done; 558664f4763Szrj debug("Tunnel forwarding using interface %s", ifname); 559664f4763Szrj 560ce74bacaSMatthew Dillon c = channel_new(ssh, "tun", SSH_CHANNEL_OPEN, sock, sock, -1, 56118de8d7fSPeter Avalos CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); 56218de8d7fSPeter Avalos c->datagram = 1; 56318de8d7fSPeter Avalos #if defined(SSH_TUN_FILTER) 56418de8d7fSPeter Avalos if (mode == SSH_TUNMODE_POINTOPOINT) 565ce74bacaSMatthew Dillon channel_register_filter(ssh, c->self, sys_tun_infilter, 56618de8d7fSPeter Avalos sys_tun_outfilter, NULL, NULL); 56718de8d7fSPeter Avalos #endif 56818de8d7fSPeter Avalos 569664f4763Szrj /* 570664f4763Szrj * Update the list of names exposed to the session 571664f4763Szrj * XXX remove these if the tunnels are closed (won't matter 572664f4763Szrj * much if they are already in the environment though) 573664f4763Szrj */ 574664f4763Szrj tmp = tun_fwd_ifnames; 575664f4763Szrj xasprintf(&tun_fwd_ifnames, "%s%s%s", 576664f4763Szrj tun_fwd_ifnames == NULL ? "" : tun_fwd_ifnames, 577664f4763Szrj tun_fwd_ifnames == NULL ? "" : ",", 578664f4763Szrj ifname); 579664f4763Szrj free(tmp); 580664f4763Szrj free(ifname); 581664f4763Szrj 58218de8d7fSPeter Avalos done: 58318de8d7fSPeter Avalos if (c == NULL) 584664f4763Szrj ssh_packet_send_debug(ssh, "Failed to open the tunnel device."); 58518de8d7fSPeter Avalos return c; 58618de8d7fSPeter Avalos } 58718de8d7fSPeter Avalos 58818de8d7fSPeter Avalos static Channel * 589ce74bacaSMatthew Dillon server_request_session(struct ssh *ssh) 59018de8d7fSPeter Avalos { 59118de8d7fSPeter Avalos Channel *c; 592664f4763Szrj int r; 59318de8d7fSPeter Avalos 59418de8d7fSPeter Avalos debug("input_session_request"); 595664f4763Szrj if ((r = sshpkt_get_end(ssh)) != 0) 596664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 59718de8d7fSPeter Avalos 59818de8d7fSPeter Avalos if (no_more_sessions) { 599664f4763Szrj ssh_packet_disconnect(ssh, "Possible attack: attempt to open a " 600664f4763Szrj "session after additional sessions disabled"); 60118de8d7fSPeter Avalos } 60218de8d7fSPeter Avalos 60318de8d7fSPeter Avalos /* 60418de8d7fSPeter Avalos * A server session has no fd to read or write until a 60518de8d7fSPeter Avalos * CHANNEL_REQUEST for a shell is made, so we set the type to 60618de8d7fSPeter Avalos * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all 60718de8d7fSPeter Avalos * CHANNEL_REQUEST messages is registered. 60818de8d7fSPeter Avalos */ 609ce74bacaSMatthew Dillon c = channel_new(ssh, "session", SSH_CHANNEL_LARVAL, 61018de8d7fSPeter Avalos -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT, 61118de8d7fSPeter Avalos 0, "server-session", 1); 61218de8d7fSPeter Avalos if (session_open(the_authctxt, c->self) != 1) { 61318de8d7fSPeter Avalos debug("session open failed, free channel %d", c->self); 614ce74bacaSMatthew Dillon channel_free(ssh, c); 61518de8d7fSPeter Avalos return NULL; 61618de8d7fSPeter Avalos } 617ce74bacaSMatthew Dillon channel_register_cleanup(ssh, c->self, session_close_by_channel, 0); 61818de8d7fSPeter Avalos return c; 61918de8d7fSPeter Avalos } 62018de8d7fSPeter Avalos 621e9778795SPeter Avalos static int 622ce74bacaSMatthew Dillon server_input_channel_open(int type, u_int32_t seq, struct ssh *ssh) 62318de8d7fSPeter Avalos { 62418de8d7fSPeter Avalos Channel *c = NULL; 625664f4763Szrj char *ctype = NULL; 626ce74bacaSMatthew Dillon const char *errmsg = NULL; 627664f4763Szrj int r, reason = SSH2_OPEN_CONNECT_FAILED; 628664f4763Szrj u_int rchan = 0, rmaxpack = 0, rwindow = 0; 62918de8d7fSPeter Avalos 630664f4763Szrj if ((r = sshpkt_get_cstring(ssh, &ctype, NULL)) != 0 || 631664f4763Szrj (r = sshpkt_get_u32(ssh, &rchan)) != 0 || 632664f4763Szrj (r = sshpkt_get_u32(ssh, &rwindow)) != 0 || 633664f4763Szrj (r = sshpkt_get_u32(ssh, &rmaxpack)) != 0) 634664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 63550a69bb5SSascha Wildner debug_f("ctype %s rchan %u win %u max %u", 63618de8d7fSPeter Avalos ctype, rchan, rwindow, rmaxpack); 63718de8d7fSPeter Avalos 6380cbfa66cSDaniel Fojt if (strcmp(ctype, "session") == 0) { 639ce74bacaSMatthew Dillon c = server_request_session(ssh); 64018de8d7fSPeter Avalos } else if (strcmp(ctype, "direct-tcpip") == 0) { 641ce74bacaSMatthew Dillon c = server_request_direct_tcpip(ssh, &reason, &errmsg); 64236e94dc5SPeter Avalos } else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) { 643ce74bacaSMatthew Dillon c = server_request_direct_streamlocal(ssh); 64418de8d7fSPeter Avalos } else if (strcmp(ctype, "tun@openssh.com") == 0) { 645ce74bacaSMatthew Dillon c = server_request_tun(ssh); 64618de8d7fSPeter Avalos } 64718de8d7fSPeter Avalos if (c != NULL) { 64850a69bb5SSascha Wildner debug_f("confirm %s", ctype); 6490cbfa66cSDaniel Fojt c->remote_id = rchan; 650ce74bacaSMatthew Dillon c->have_remote_id = 1; 65118de8d7fSPeter Avalos c->remote_window = rwindow; 65218de8d7fSPeter Avalos c->remote_maxpacket = rmaxpack; 65318de8d7fSPeter Avalos if (c->type != SSH_CHANNEL_CONNECTING) { 654664f4763Szrj if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION)) != 0 || 655664f4763Szrj (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || 656664f4763Szrj (r = sshpkt_put_u32(ssh, c->self)) != 0 || 657664f4763Szrj (r = sshpkt_put_u32(ssh, c->local_window)) != 0 || 658664f4763Szrj (r = sshpkt_put_u32(ssh, c->local_maxpacket)) != 0 || 659664f4763Szrj (r = sshpkt_send(ssh)) != 0) { 660664f4763Szrj sshpkt_fatal(ssh, r, 661664f4763Szrj "%s: send open confirm", __func__); 662664f4763Szrj } 66318de8d7fSPeter Avalos } 66418de8d7fSPeter Avalos } else { 66550a69bb5SSascha Wildner debug_f("failure %s", ctype); 666664f4763Szrj if ((r = sshpkt_start(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE)) != 0 || 667664f4763Szrj (r = sshpkt_put_u32(ssh, rchan)) != 0 || 668664f4763Szrj (r = sshpkt_put_u32(ssh, reason)) != 0 || 669664f4763Szrj (r = sshpkt_put_cstring(ssh, errmsg ? errmsg : "open failed")) != 0 || 670664f4763Szrj (r = sshpkt_put_cstring(ssh, "")) != 0 || 671664f4763Szrj (r = sshpkt_send(ssh)) != 0) { 672664f4763Szrj sshpkt_fatal(ssh, r, 673664f4763Szrj "%s: send open failure", __func__); 67418de8d7fSPeter Avalos } 67518de8d7fSPeter Avalos } 67636e94dc5SPeter Avalos free(ctype); 677e9778795SPeter Avalos return 0; 67818de8d7fSPeter Avalos } 67918de8d7fSPeter Avalos 680e9778795SPeter Avalos static int 681ce74bacaSMatthew Dillon server_input_hostkeys_prove(struct ssh *ssh, struct sshbuf **respp) 682e9778795SPeter Avalos { 683e9778795SPeter Avalos struct sshbuf *resp = NULL; 684e9778795SPeter Avalos struct sshbuf *sigbuf = NULL; 685e9778795SPeter Avalos struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL; 686*ee116499SAntonio Huete Jimenez int r, ndx, success = 0; 687e9778795SPeter Avalos const u_char *blob; 688*ee116499SAntonio Huete Jimenez const char *sigalg, *kex_rsa_sigalg = NULL; 689e9778795SPeter Avalos u_char *sig = 0; 690e9778795SPeter Avalos size_t blen, slen; 691e9778795SPeter Avalos 692e9778795SPeter Avalos if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL) 69350a69bb5SSascha Wildner fatal_f("sshbuf_new"); 694*ee116499SAntonio Huete Jimenez if (sshkey_type_plain(sshkey_type_from_name( 695*ee116499SAntonio Huete Jimenez ssh->kex->hostkey_alg)) == KEY_RSA) 696*ee116499SAntonio Huete Jimenez kex_rsa_sigalg = ssh->kex->hostkey_alg; 697e9778795SPeter Avalos while (ssh_packet_remaining(ssh) > 0) { 698e9778795SPeter Avalos sshkey_free(key); 699e9778795SPeter Avalos key = NULL; 700e9778795SPeter Avalos if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 || 701e9778795SPeter Avalos (r = sshkey_from_blob(blob, blen, &key)) != 0) { 70250a69bb5SSascha Wildner error_fr(r, "parse key"); 703e9778795SPeter Avalos goto out; 704e9778795SPeter Avalos } 705e9778795SPeter Avalos /* 706e9778795SPeter Avalos * Better check that this is actually one of our hostkeys 707e9778795SPeter Avalos * before attempting to sign anything with it. 708e9778795SPeter Avalos */ 709e9778795SPeter Avalos if ((ndx = ssh->kex->host_key_index(key, 1, ssh)) == -1) { 71050a69bb5SSascha Wildner error_f("unknown host %s key", sshkey_type(key)); 711e9778795SPeter Avalos goto out; 712e9778795SPeter Avalos } 713e9778795SPeter Avalos /* 714e9778795SPeter Avalos * XXX refactor: make kex->sign just use an index rather 715e9778795SPeter Avalos * than passing in public and private keys 716e9778795SPeter Avalos */ 717e9778795SPeter Avalos if ((key_prv = get_hostkey_by_index(ndx)) == NULL && 718e9778795SPeter Avalos (key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) { 71950a69bb5SSascha Wildner error_f("can't retrieve hostkey %d", ndx); 720e9778795SPeter Avalos goto out; 721e9778795SPeter Avalos } 722e9778795SPeter Avalos sshbuf_reset(sigbuf); 723e9778795SPeter Avalos free(sig); 724e9778795SPeter Avalos sig = NULL; 725664f4763Szrj /* 726664f4763Szrj * For RSA keys, prefer to use the signature type negotiated 727664f4763Szrj * during KEX to the default (SHA1). 728664f4763Szrj */ 729*ee116499SAntonio Huete Jimenez sigalg = NULL; 730*ee116499SAntonio Huete Jimenez if (sshkey_type_plain(key->type) == KEY_RSA) { 731*ee116499SAntonio Huete Jimenez if (kex_rsa_sigalg != NULL) 732*ee116499SAntonio Huete Jimenez sigalg = kex_rsa_sigalg; 733*ee116499SAntonio Huete Jimenez else if (ssh->kex->flags & KEX_RSA_SHA2_512_SUPPORTED) 734*ee116499SAntonio Huete Jimenez sigalg = "rsa-sha2-512"; 735*ee116499SAntonio Huete Jimenez else if (ssh->kex->flags & KEX_RSA_SHA2_256_SUPPORTED) 736*ee116499SAntonio Huete Jimenez sigalg = "rsa-sha2-256"; 737*ee116499SAntonio Huete Jimenez } 738*ee116499SAntonio Huete Jimenez debug3_f("sign %s key (index %d) using sigalg %s", 739*ee116499SAntonio Huete Jimenez sshkey_type(key), ndx, sigalg == NULL ? "default" : sigalg); 740e9778795SPeter Avalos if ((r = sshbuf_put_cstring(sigbuf, 741e9778795SPeter Avalos "hostkeys-prove-00@openssh.com")) != 0 || 74250a69bb5SSascha Wildner (r = sshbuf_put_stringb(sigbuf, 74350a69bb5SSascha Wildner ssh->kex->session_id)) != 0 || 744e9778795SPeter Avalos (r = sshkey_puts(key, sigbuf)) != 0 || 745664f4763Szrj (r = ssh->kex->sign(ssh, key_prv, key_pub, &sig, &slen, 746*ee116499SAntonio Huete Jimenez sshbuf_ptr(sigbuf), sshbuf_len(sigbuf), sigalg)) != 0 || 747e9778795SPeter Avalos (r = sshbuf_put_string(resp, sig, slen)) != 0) { 74850a69bb5SSascha Wildner error_fr(r, "assemble signature"); 749e9778795SPeter Avalos goto out; 750e9778795SPeter Avalos } 751e9778795SPeter Avalos } 752e9778795SPeter Avalos /* Success */ 753e9778795SPeter Avalos *respp = resp; 754e9778795SPeter Avalos resp = NULL; /* don't free it */ 755e9778795SPeter Avalos success = 1; 756e9778795SPeter Avalos out: 757e9778795SPeter Avalos free(sig); 758e9778795SPeter Avalos sshbuf_free(resp); 759e9778795SPeter Avalos sshbuf_free(sigbuf); 760e9778795SPeter Avalos sshkey_free(key); 761e9778795SPeter Avalos return success; 762e9778795SPeter Avalos } 763e9778795SPeter Avalos 764e9778795SPeter Avalos static int 765ce74bacaSMatthew Dillon server_input_global_request(int type, u_int32_t seq, struct ssh *ssh) 76618de8d7fSPeter Avalos { 767664f4763Szrj char *rtype = NULL; 768664f4763Szrj u_char want_reply = 0; 769e9778795SPeter Avalos int r, success = 0, allocated_listen_port = 0; 770664f4763Szrj u_int port = 0; 771e9778795SPeter Avalos struct sshbuf *resp = NULL; 772ce74bacaSMatthew Dillon struct passwd *pw = the_authctxt->pw; 77336e94dc5SPeter Avalos struct Forward fwd; 77418de8d7fSPeter Avalos 77536e94dc5SPeter Avalos memset(&fwd, 0, sizeof(fwd)); 776664f4763Szrj if (pw == NULL || !the_authctxt->valid) 77750a69bb5SSascha Wildner fatal_f("no/invalid user"); 77818de8d7fSPeter Avalos 779664f4763Szrj if ((r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 || 780664f4763Szrj (r = sshpkt_get_u8(ssh, &want_reply)) != 0) 781664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 78250a69bb5SSascha Wildner debug_f("rtype %s want_reply %d", rtype, want_reply); 783664f4763Szrj 784664f4763Szrj /* -R style forwarding */ 785664f4763Szrj if (strcmp(rtype, "tcpip-forward") == 0) { 786664f4763Szrj if ((r = sshpkt_get_cstring(ssh, &fwd.listen_host, NULL)) != 0 || 787664f4763Szrj (r = sshpkt_get_u32(ssh, &port)) != 0) 788664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse tcpip-forward", __func__); 78950a69bb5SSascha Wildner debug_f("tcpip-forward listen %s port %u", 790664f4763Szrj fwd.listen_host, port); 791664f4763Szrj if (port <= INT_MAX) 792664f4763Szrj fwd.listen_port = (int)port; 79318de8d7fSPeter Avalos /* check permissions */ 794664f4763Szrj if (port > INT_MAX || 795664f4763Szrj (options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 || 796664f4763Szrj !auth_opts->permit_port_forwarding_flag || 797664f4763Szrj options.disable_forwarding || 798e9778795SPeter Avalos (!want_reply && fwd.listen_port == 0) || 799ce74bacaSMatthew Dillon (fwd.listen_port != 0 && 800ce74bacaSMatthew Dillon !bind_permitted(fwd.listen_port, pw->pw_uid))) { 80118de8d7fSPeter Avalos success = 0; 802664f4763Szrj ssh_packet_send_debug(ssh, "Server has disabled port forwarding."); 80318de8d7fSPeter Avalos } else { 80418de8d7fSPeter Avalos /* Start listening on the port */ 805ce74bacaSMatthew Dillon success = channel_setup_remote_fwd_listener(ssh, &fwd, 80636e94dc5SPeter Avalos &allocated_listen_port, &options.fwd_opts); 80718de8d7fSPeter Avalos } 808e9778795SPeter Avalos if ((resp = sshbuf_new()) == NULL) 80950a69bb5SSascha Wildner fatal_f("sshbuf_new"); 810e9778795SPeter Avalos if (allocated_listen_port != 0 && 811e9778795SPeter Avalos (r = sshbuf_put_u32(resp, allocated_listen_port)) != 0) 81250a69bb5SSascha Wildner fatal_fr(r, "sshbuf_put_u32"); 81318de8d7fSPeter Avalos } else if (strcmp(rtype, "cancel-tcpip-forward") == 0) { 814664f4763Szrj if ((r = sshpkt_get_cstring(ssh, &fwd.listen_host, NULL)) != 0 || 815664f4763Szrj (r = sshpkt_get_u32(ssh, &port)) != 0) 816664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse cancel-tcpip-forward", __func__); 81718de8d7fSPeter Avalos 81850a69bb5SSascha Wildner debug_f("cancel-tcpip-forward addr %s port %d", 819664f4763Szrj fwd.listen_host, port); 820664f4763Szrj if (port <= INT_MAX) { 821664f4763Szrj fwd.listen_port = (int)port; 822ce74bacaSMatthew Dillon success = channel_cancel_rport_listener(ssh, &fwd); 823664f4763Szrj } 82436e94dc5SPeter Avalos } else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) { 825664f4763Szrj if ((r = sshpkt_get_cstring(ssh, &fwd.listen_path, NULL)) != 0) 826664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse streamlocal-forward@openssh.com", __func__); 82750a69bb5SSascha Wildner debug_f("streamlocal-forward listen path %s", 82836e94dc5SPeter Avalos fwd.listen_path); 82936e94dc5SPeter Avalos 83036e94dc5SPeter Avalos /* check permissions */ 83136e94dc5SPeter Avalos if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0 832664f4763Szrj || !auth_opts->permit_port_forwarding_flag || 833664f4763Szrj options.disable_forwarding || 834ce74bacaSMatthew Dillon (pw->pw_uid != 0 && !use_privsep)) { 83536e94dc5SPeter Avalos success = 0; 836664f4763Szrj ssh_packet_send_debug(ssh, "Server has disabled " 837ce74bacaSMatthew Dillon "streamlocal forwarding."); 83836e94dc5SPeter Avalos } else { 83936e94dc5SPeter Avalos /* Start listening on the socket */ 840ce74bacaSMatthew Dillon success = channel_setup_remote_fwd_listener(ssh, 84136e94dc5SPeter Avalos &fwd, NULL, &options.fwd_opts); 84236e94dc5SPeter Avalos } 84336e94dc5SPeter Avalos } else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) { 844664f4763Szrj if ((r = sshpkt_get_cstring(ssh, &fwd.listen_path, NULL)) != 0) 845664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse cancel-streamlocal-forward@openssh.com", __func__); 84650a69bb5SSascha Wildner debug_f("cancel-streamlocal-forward path %s", 84736e94dc5SPeter Avalos fwd.listen_path); 84836e94dc5SPeter Avalos 849ce74bacaSMatthew Dillon success = channel_cancel_rport_listener(ssh, &fwd); 85018de8d7fSPeter Avalos } else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) { 85118de8d7fSPeter Avalos no_more_sessions = 1; 85218de8d7fSPeter Avalos success = 1; 853e9778795SPeter Avalos } else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) { 854ce74bacaSMatthew Dillon success = server_input_hostkeys_prove(ssh, &resp); 85518de8d7fSPeter Avalos } 856664f4763Szrj /* XXX sshpkt_get_end() */ 85718de8d7fSPeter Avalos if (want_reply) { 858664f4763Szrj if ((r = sshpkt_start(ssh, success ? 859664f4763Szrj SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE)) != 0 || 860664f4763Szrj (success && resp != NULL && (r = sshpkt_putb(ssh, resp)) != 0) || 861664f4763Szrj (r = sshpkt_send(ssh)) != 0 || 862664f4763Szrj (r = ssh_packet_write_wait(ssh)) != 0) 863664f4763Szrj sshpkt_fatal(ssh, r, "%s: send reply", __func__); 86418de8d7fSPeter Avalos } 865664f4763Szrj free(fwd.listen_host); 866664f4763Szrj free(fwd.listen_path); 86736e94dc5SPeter Avalos free(rtype); 868e9778795SPeter Avalos sshbuf_free(resp); 869e9778795SPeter Avalos return 0; 87018de8d7fSPeter Avalos } 87118de8d7fSPeter Avalos 872e9778795SPeter Avalos static int 873ce74bacaSMatthew Dillon server_input_channel_req(int type, u_int32_t seq, struct ssh *ssh) 87418de8d7fSPeter Avalos { 87518de8d7fSPeter Avalos Channel *c; 876664f4763Szrj int r, success = 0; 877664f4763Szrj char *rtype = NULL; 878664f4763Szrj u_char want_reply = 0; 879664f4763Szrj u_int id = 0; 88018de8d7fSPeter Avalos 881664f4763Szrj if ((r = sshpkt_get_u32(ssh, &id)) != 0 || 882664f4763Szrj (r = sshpkt_get_cstring(ssh, &rtype, NULL)) != 0 || 883664f4763Szrj (r = sshpkt_get_u8(ssh, &want_reply)) != 0) 884664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 88518de8d7fSPeter Avalos 886664f4763Szrj debug("server_input_channel_req: channel %u request %s reply %d", 887664f4763Szrj id, rtype, want_reply); 88818de8d7fSPeter Avalos 889664f4763Szrj if (id >= INT_MAX || (c = channel_lookup(ssh, (int)id)) == NULL) { 890664f4763Szrj ssh_packet_disconnect(ssh, "%s: unknown channel %d", 891664f4763Szrj __func__, id); 892664f4763Szrj } 89318de8d7fSPeter Avalos if (!strcmp(rtype, "eow@openssh.com")) { 894664f4763Szrj if ((r = sshpkt_get_end(ssh)) != 0) 895664f4763Szrj sshpkt_fatal(ssh, r, "%s: parse packet", __func__); 896ce74bacaSMatthew Dillon chan_rcvd_eow(ssh, c); 89718de8d7fSPeter Avalos } else if ((c->type == SSH_CHANNEL_LARVAL || 89818de8d7fSPeter Avalos c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0) 899ce74bacaSMatthew Dillon success = session_input_channel_req(ssh, c, rtype); 900664f4763Szrj if (want_reply && !(c->flags & CHAN_CLOSE_SENT)) { 901ce74bacaSMatthew Dillon if (!c->have_remote_id) 90250a69bb5SSascha Wildner fatal_f("channel %d: no remote_id", c->self); 903664f4763Szrj if ((r = sshpkt_start(ssh, success ? 904664f4763Szrj SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE)) != 0 || 905664f4763Szrj (r = sshpkt_put_u32(ssh, c->remote_id)) != 0 || 906664f4763Szrj (r = sshpkt_send(ssh)) != 0) 907664f4763Szrj sshpkt_fatal(ssh, r, "%s: send reply", __func__); 90818de8d7fSPeter Avalos } 90936e94dc5SPeter Avalos free(rtype); 910e9778795SPeter Avalos return 0; 91118de8d7fSPeter Avalos } 91218de8d7fSPeter Avalos 91318de8d7fSPeter Avalos static void 914664f4763Szrj server_init_dispatch(struct ssh *ssh) 91518de8d7fSPeter Avalos { 916ce74bacaSMatthew Dillon debug("server_init_dispatch"); 917664f4763Szrj ssh_dispatch_init(ssh, &dispatch_protocol_error); 918664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); 919664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_DATA, &channel_input_data); 920664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); 921664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); 922664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open); 923664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); 924664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); 925664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req); 926664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); 927664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request); 92818de8d7fSPeter Avalos /* client_alive */ 929664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive); 930664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive); 931664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive); 932664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive); 93318de8d7fSPeter Avalos /* rekeying */ 934664f4763Szrj ssh_dispatch_set(ssh, SSH2_MSG_KEXINIT, &kex_input_kexinit); 93518de8d7fSPeter Avalos } 936