dtucker@openbsd.org | 73ddb25 | 2018-07-27 05:13:02 +0000 | [diff] [blame] | 1 | /* $OpenBSD: serverloop.c,v 1.209 2018/07/27 05:13:02 dtucker Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * Server main loop for handling the interactive session. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 7 | * |
| 8 | * As far as I am concerned, the code I have written for this software |
| 9 | * can be used freely for any purpose. Any derived versions of this |
| 10 | * software must be clearly marked as such, and if the derived work is |
| 11 | * incompatible with the protocol description in the RFC file, it must be |
| 12 | * called by a name other than "ssh" or "Secure Shell". |
| 13 | * |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 14 | * SSH2 support by Markus Friedl. |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 15 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 16 | * |
| 17 | * Redistribution and use in source and binary forms, with or without |
| 18 | * modification, are permitted provided that the following conditions |
| 19 | * are met: |
| 20 | * 1. Redistributions of source code must retain the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer. |
| 22 | * 2. Redistributions in binary form must reproduce the above copyright |
| 23 | * notice, this list of conditions and the following disclaimer in the |
| 24 | * documentation and/or other materials provided with the distribution. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 27 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 28 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 29 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 30 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 31 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 33 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 35 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 36 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 37 | |
| 38 | #include "includes.h" |
Damien Miller | 9cf6d07 | 2006-03-15 11:29:24 +1100 | [diff] [blame] | 39 | |
| 40 | #include <sys/types.h> |
| 41 | #include <sys/wait.h> |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 42 | #include <sys/socket.h> |
Damien Miller | 9aec919 | 2006-08-05 10:57:45 +1000 | [diff] [blame] | 43 | #ifdef HAVE_SYS_TIME_H |
| 44 | # include <sys/time.h> |
| 45 | #endif |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 46 | |
| 47 | #include <netinet/in.h> |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 48 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 49 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 50 | #include <fcntl.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 51 | #include <pwd.h> |
Damien Miller | 6ff3cad | 2006-03-15 11:52:09 +1100 | [diff] [blame] | 52 | #include <signal.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 53 | #include <string.h> |
Damien Miller | 99bd21e | 2006-03-15 11:11:28 +1100 | [diff] [blame] | 54 | #include <termios.h> |
Damien Miller | 1cdde6f | 2006-07-24 14:07:35 +1000 | [diff] [blame] | 55 | #include <unistd.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 56 | #include <stdarg.h> |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 57 | |
Damien Miller | b84886b | 2008-05-19 15:05:07 +1000 | [diff] [blame] | 58 | #include "openbsd-compat/sys-queue.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 59 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 60 | #include "packet.h" |
markus@openbsd.org | c3cb779 | 2018-07-09 21:29:36 +0000 | [diff] [blame] | 61 | #include "sshbuf.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 62 | #include "log.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 63 | #include "misc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 64 | #include "servconf.h" |
Damien Miller | 16aed05 | 2002-09-22 01:26:27 +1000 | [diff] [blame] | 65 | #include "canohost.h" |
Ben Lindstrom | d95c09c | 2001-02-18 19:13:33 +0000 | [diff] [blame] | 66 | #include "sshpty.h" |
Ben Lindstrom | c763767 | 2001-06-09 00:36:26 +0000 | [diff] [blame] | 67 | #include "channels.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 68 | #include "compat.h" |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 69 | #include "ssh2.h" |
markus@openbsd.org | 5467fbc | 2018-07-11 18:53:29 +0000 | [diff] [blame] | 70 | #include "sshkey.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 71 | #include "cipher.h" |
| 72 | #include "kex.h" |
| 73 | #include "hostfile.h" |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 74 | #include "auth.h" |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 75 | #include "session.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 76 | #include "dispatch.h" |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 77 | #include "auth-options.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 78 | #include "serverloop.h" |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 79 | #include "ssherr.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 81 | extern ServerOptions options; |
| 82 | |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 83 | /* XXX */ |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 84 | extern Authctxt *the_authctxt; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 85 | extern struct sshauthopt *auth_opts; |
Damien Miller | 24ecf61 | 2005-11-05 15:16:52 +1100 | [diff] [blame] | 86 | extern int use_privsep; |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 87 | |
Darren Tucker | 8901fa9 | 2008-06-11 09:34:01 +1000 | [diff] [blame] | 88 | static int no_more_sessions = 0; /* Disallow further sessions. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 89 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 90 | /* |
| 91 | * This SIGCHLD kludge is used to detect when the child exits. The server |
| 92 | * will exit after that, as soon as forwarded connections have terminated. |
| 93 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 94 | |
Ben Lindstrom | 5e71c54 | 2001-12-06 16:48:14 +0000 | [diff] [blame] | 95 | static volatile sig_atomic_t child_terminated = 0; /* The child has terminated. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 96 | |
Damien Miller | 24ecf61 | 2005-11-05 15:16:52 +1100 | [diff] [blame] | 97 | /* Cleanup on signals (!use_privsep case only) */ |
| 98 | static volatile sig_atomic_t received_sigterm = 0; |
| 99 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 100 | /* prototypes */ |
| 101 | static void server_init_dispatch(void); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 102 | |
djm@openbsd.org | b7548b1 | 2017-10-23 05:08:00 +0000 | [diff] [blame] | 103 | /* requested tunnel forwarding interface(s), shared with session.c */ |
| 104 | char *tun_fwd_ifnames = NULL; |
| 105 | |
dtucker@openbsd.org | 73ddb25 | 2018-07-27 05:13:02 +0000 | [diff] [blame] | 106 | /* returns 1 if bind to specified port by specified user is permitted */ |
| 107 | static int |
| 108 | bind_permitted(int port, uid_t uid) |
| 109 | { |
| 110 | if (use_privsep) |
| 111 | return 1; /* allow system to decide */ |
| 112 | if (port < IPPORT_RESERVED && uid != 0) |
| 113 | return 0; |
| 114 | return 1; |
| 115 | } |
| 116 | |
Damien Miller | f6681a3 | 2001-12-21 14:53:11 +1100 | [diff] [blame] | 117 | /* |
| 118 | * we write to this pipe if a SIGCHLD is caught in order to avoid |
| 119 | * the race between select() and child_terminated |
| 120 | */ |
| 121 | static int notify_pipe[2]; |
| 122 | static void |
| 123 | notify_setup(void) |
| 124 | { |
| 125 | if (pipe(notify_pipe) < 0) { |
| 126 | error("pipe(notify_pipe) failed %s", strerror(errno)); |
Damien Miller | 814ace0 | 2011-05-20 19:02:47 +1000 | [diff] [blame] | 127 | } else if ((fcntl(notify_pipe[0], F_SETFD, FD_CLOEXEC) == -1) || |
| 128 | (fcntl(notify_pipe[1], F_SETFD, FD_CLOEXEC) == -1)) { |
Damien Miller | f6681a3 | 2001-12-21 14:53:11 +1100 | [diff] [blame] | 129 | error("fcntl(notify_pipe, F_SETFD) failed %s", strerror(errno)); |
| 130 | close(notify_pipe[0]); |
| 131 | close(notify_pipe[1]); |
| 132 | } else { |
| 133 | set_nonblock(notify_pipe[0]); |
| 134 | set_nonblock(notify_pipe[1]); |
| 135 | return; |
| 136 | } |
| 137 | notify_pipe[0] = -1; /* read end */ |
| 138 | notify_pipe[1] = -1; /* write end */ |
| 139 | } |
| 140 | static void |
| 141 | notify_parent(void) |
| 142 | { |
| 143 | if (notify_pipe[1] != -1) |
Darren Tucker | dbee308 | 2013-05-16 20:32:29 +1000 | [diff] [blame] | 144 | (void)write(notify_pipe[1], "", 1); |
Damien Miller | f6681a3 | 2001-12-21 14:53:11 +1100 | [diff] [blame] | 145 | } |
| 146 | static void |
| 147 | notify_prepare(fd_set *readset) |
| 148 | { |
| 149 | if (notify_pipe[0] != -1) |
| 150 | FD_SET(notify_pipe[0], readset); |
| 151 | } |
| 152 | static void |
| 153 | notify_done(fd_set *readset) |
| 154 | { |
| 155 | char c; |
| 156 | |
| 157 | if (notify_pipe[0] != -1 && FD_ISSET(notify_pipe[0], readset)) |
| 158 | while (read(notify_pipe[0], &c, 1) != -1) |
djm@openbsd.org | 027607f | 2018-06-08 01:55:40 +0000 | [diff] [blame] | 159 | debug2("%s: reading", __func__); |
Damien Miller | f6681a3 | 2001-12-21 14:53:11 +1100 | [diff] [blame] | 160 | } |
| 161 | |
Damien Miller | f0b15df | 2006-03-26 13:59:20 +1100 | [diff] [blame] | 162 | /*ARGSUSED*/ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 163 | static void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 164 | sigchld_handler(int sig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 165 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 166 | int save_errno = errno; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 167 | child_terminated = 1; |
Damien Miller | f6681a3 | 2001-12-21 14:53:11 +1100 | [diff] [blame] | 168 | notify_parent(); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 169 | errno = save_errno; |
| 170 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 171 | |
Damien Miller | f0b15df | 2006-03-26 13:59:20 +1100 | [diff] [blame] | 172 | /*ARGSUSED*/ |
Damien Miller | 24ecf61 | 2005-11-05 15:16:52 +1100 | [diff] [blame] | 173 | static void |
| 174 | sigterm_handler(int sig) |
| 175 | { |
| 176 | received_sigterm = sig; |
| 177 | } |
| 178 | |
Damien Miller | 8c3902a | 2001-10-10 15:01:40 +1000 | [diff] [blame] | 179 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 180 | client_alive_check(struct ssh *ssh) |
Damien Miller | 8c3902a | 2001-10-10 15:01:40 +1000 | [diff] [blame] | 181 | { |
Damien Miller | b5820f4 | 2003-12-17 16:27:32 +1100 | [diff] [blame] | 182 | int channel_id; |
dtucker@openbsd.org | 48c23a3 | 2017-12-10 05:55:29 +0000 | [diff] [blame] | 183 | char remote_id[512]; |
Damien Miller | 056cf73 | 2002-01-22 23:21:39 +1100 | [diff] [blame] | 184 | |
Damien Miller | 8c3902a | 2001-10-10 15:01:40 +1000 | [diff] [blame] | 185 | /* timeout, check to see how many we have had */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 186 | if (packet_inc_alive_timeouts() > options.client_alive_count_max) { |
dtucker@openbsd.org | 48c23a3 | 2017-12-10 05:55:29 +0000 | [diff] [blame] | 187 | sshpkt_fmt_connection_id(ssh, remote_id, sizeof(remote_id)); |
| 188 | logit("Timeout, client not responding from %s", remote_id); |
Damien Miller | 985a448 | 2006-10-24 03:02:41 +1000 | [diff] [blame] | 189 | cleanup_exit(255); |
| 190 | } |
Damien Miller | 8c3902a | 2001-10-10 15:01:40 +1000 | [diff] [blame] | 191 | |
Damien Miller | 8c3902a | 2001-10-10 15:01:40 +1000 | [diff] [blame] | 192 | /* |
Damien Miller | b5820f4 | 2003-12-17 16:27:32 +1100 | [diff] [blame] | 193 | * send a bogus global/channel request with "wantreply", |
Damien Miller | 8c3902a | 2001-10-10 15:01:40 +1000 | [diff] [blame] | 194 | * we should get back a failure |
| 195 | */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 196 | if ((channel_id = channel_find_open(ssh)) == -1) { |
Damien Miller | b5820f4 | 2003-12-17 16:27:32 +1100 | [diff] [blame] | 197 | packet_start(SSH2_MSG_GLOBAL_REQUEST); |
| 198 | packet_put_cstring("keepalive@openssh.com"); |
| 199 | packet_put_char(1); /* boolean: want reply */ |
| 200 | } else { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 201 | channel_request_start(ssh, channel_id, |
| 202 | "keepalive@openssh.com", 1); |
Damien Miller | b5820f4 | 2003-12-17 16:27:32 +1100 | [diff] [blame] | 203 | } |
Damien Miller | 8c3902a | 2001-10-10 15:01:40 +1000 | [diff] [blame] | 204 | packet_send(); |
| 205 | } |
| 206 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 207 | /* |
| 208 | * Sleep in select() until we can do something. This will initialize the |
| 209 | * select masks. Upon return, the masks will indicate which descriptors |
| 210 | * have data or can accept data. Optionally, a maximum time can be specified |
| 211 | * for the duration of the wait (0 = infinite). |
| 212 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 213 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 214 | wait_until_can_do_something(struct ssh *ssh, |
| 215 | int connection_in, int connection_out, |
markus@openbsd.org | 6cb6dcf | 2016-08-13 17:47:40 +0000 | [diff] [blame] | 216 | fd_set **readsetp, fd_set **writesetp, int *maxfdp, |
djm@openbsd.org | 988e429 | 2016-03-04 03:35:44 +0000 | [diff] [blame] | 217 | u_int *nallocp, u_int64_t max_time_ms) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 218 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 219 | struct timeval tv, *tvp; |
| 220 | int ret; |
Damien Miller | 6c6da33 | 2012-06-20 22:31:26 +1000 | [diff] [blame] | 221 | time_t minwait_secs = 0; |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 222 | int client_alive_scheduled = 0; |
dtucker@openbsd.org | b60ff20 | 2017-08-11 03:58:36 +0000 | [diff] [blame] | 223 | static time_t last_client_time; |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 224 | |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 225 | /* Allocate and update select() masks for channel descriptors. */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 226 | channel_prepare_select(ssh, readsetp, writesetp, maxfdp, |
djm@openbsd.org | 71e5a53 | 2017-08-30 03:59:08 +0000 | [diff] [blame] | 227 | nallocp, &minwait_secs); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 228 | |
djm@openbsd.org | 988e429 | 2016-03-04 03:35:44 +0000 | [diff] [blame] | 229 | /* XXX need proper deadline system for rekey/client alive */ |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 230 | if (minwait_secs != 0) |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 231 | max_time_ms = MINIMUM(max_time_ms, (u_int)minwait_secs * 1000); |
Damien Miller | a650875 | 2012-04-22 11:21:10 +1000 | [diff] [blame] | 232 | |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 233 | /* |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 234 | * if using client_alive, set the max timeout accordingly, |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 235 | * and indicate that this particular timeout was for client |
| 236 | * alive by setting the client_alive_scheduled flag. |
| 237 | * |
| 238 | * this could be randomized somewhat to make traffic |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 239 | * analysis more difficult, but we're not doing it yet. |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 240 | */ |
markus@openbsd.org | 6cb6dcf | 2016-08-13 17:47:40 +0000 | [diff] [blame] | 241 | if (options.client_alive_interval) { |
djm@openbsd.org | 988e429 | 2016-03-04 03:35:44 +0000 | [diff] [blame] | 242 | uint64_t keepalive_ms = |
| 243 | (uint64_t)options.client_alive_interval * 1000; |
| 244 | |
Ben Lindstrom | 2f0304c | 2001-04-29 19:49:14 +0000 | [diff] [blame] | 245 | client_alive_scheduled = 1; |
djm@openbsd.org | 988e429 | 2016-03-04 03:35:44 +0000 | [diff] [blame] | 246 | if (max_time_ms == 0 || max_time_ms > keepalive_ms) |
| 247 | max_time_ms = keepalive_ms; |
Ben Lindstrom | 36857f6 | 2001-07-18 15:48:57 +0000 | [diff] [blame] | 248 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 249 | |
Damien Miller | af5f2e6 | 2001-10-10 15:01:16 +1000 | [diff] [blame] | 250 | #if 0 |
markus@openbsd.org | 6cb6dcf | 2016-08-13 17:47:40 +0000 | [diff] [blame] | 251 | /* wrong: bad condition XXX */ |
| 252 | if (channel_not_very_much_buffered_data()) |
Damien Miller | af5f2e6 | 2001-10-10 15:01:16 +1000 | [diff] [blame] | 253 | #endif |
markus@openbsd.org | 6cb6dcf | 2016-08-13 17:47:40 +0000 | [diff] [blame] | 254 | FD_SET(connection_in, *readsetp); |
Damien Miller | f6681a3 | 2001-12-21 14:53:11 +1100 | [diff] [blame] | 255 | notify_prepare(*readsetp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 256 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 257 | /* |
| 258 | * If we have buffered packet data going to the client, mark that |
| 259 | * descriptor. |
| 260 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 261 | if (packet_have_data_to_write()) |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 262 | FD_SET(connection_out, *writesetp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 263 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 264 | /* |
| 265 | * If child has terminated and there is enough buffer space to read |
| 266 | * from it, then read as much as is available and exit. |
| 267 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 268 | if (child_terminated && packet_not_very_much_data_to_write()) |
djm@openbsd.org | 988e429 | 2016-03-04 03:35:44 +0000 | [diff] [blame] | 269 | if (max_time_ms == 0 || client_alive_scheduled) |
| 270 | max_time_ms = 100; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 271 | |
djm@openbsd.org | 988e429 | 2016-03-04 03:35:44 +0000 | [diff] [blame] | 272 | if (max_time_ms == 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 273 | tvp = NULL; |
| 274 | else { |
djm@openbsd.org | 988e429 | 2016-03-04 03:35:44 +0000 | [diff] [blame] | 275 | tv.tv_sec = max_time_ms / 1000; |
| 276 | tv.tv_usec = 1000 * (max_time_ms % 1000); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 277 | tvp = &tv; |
| 278 | } |
| 279 | |
| 280 | /* Wait for something to happen, or the timeout to expire. */ |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 281 | ret = select((*maxfdp)+1, *readsetp, *writesetp, NULL, tvp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 282 | |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 283 | if (ret == -1) { |
Damien Miller | 79faeff | 2001-11-12 11:06:32 +1100 | [diff] [blame] | 284 | memset(*readsetp, 0, *nallocp); |
| 285 | memset(*writesetp, 0, *nallocp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 286 | if (errno != EINTR) |
| 287 | error("select: %.100s", strerror(errno)); |
dtucker@openbsd.org | b60ff20 | 2017-08-11 03:58:36 +0000 | [diff] [blame] | 288 | } else if (client_alive_scheduled) { |
| 289 | time_t now = monotime(); |
| 290 | |
| 291 | if (ret == 0) { /* timeout */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 292 | client_alive_check(ssh); |
dtucker@openbsd.org | b60ff20 | 2017-08-11 03:58:36 +0000 | [diff] [blame] | 293 | } else if (FD_ISSET(connection_in, *readsetp)) { |
| 294 | last_client_time = now; |
| 295 | } else if (last_client_time != 0 && last_client_time + |
dtucker@openbsd.org | 42a8f8b | 2017-08-11 04:16:35 +0000 | [diff] [blame] | 296 | options.client_alive_interval <= now) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 297 | client_alive_check(ssh); |
dtucker@openbsd.org | b60ff20 | 2017-08-11 03:58:36 +0000 | [diff] [blame] | 298 | last_client_time = now; |
| 299 | } |
| 300 | } |
Damien Miller | f6681a3 | 2001-12-21 14:53:11 +1100 | [diff] [blame] | 301 | |
| 302 | notify_done(*readsetp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 303 | } |
| 304 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 305 | /* |
| 306 | * Processes input from the client and the program. Input data is stored |
| 307 | * in buffers and processed later. |
| 308 | */ |
markus@openbsd.org | 6cb6dcf | 2016-08-13 17:47:40 +0000 | [diff] [blame] | 309 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 310 | process_input(struct ssh *ssh, fd_set *readset, int connection_in) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 311 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 312 | int len; |
| 313 | char buf[16384]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 314 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 315 | /* Read and buffer any input data from the client. */ |
| 316 | if (FD_ISSET(connection_in, readset)) { |
markus@openbsd.org | a306863 | 2016-01-14 16:17:39 +0000 | [diff] [blame] | 317 | len = read(connection_in, buf, sizeof(buf)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 318 | if (len == 0) { |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 319 | verbose("Connection closed by %.100s port %d", |
| 320 | ssh_remote_ipaddr(ssh), ssh_remote_port(ssh)); |
markus@openbsd.org | 6cb6dcf | 2016-08-13 17:47:40 +0000 | [diff] [blame] | 321 | return -1; |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 322 | } else if (len < 0) { |
Damien Miller | d8968ad | 2008-07-04 23:10:49 +1000 | [diff] [blame] | 323 | if (errno != EINTR && errno != EAGAIN && |
| 324 | errno != EWOULDBLOCK) { |
Damien Miller | 16aed05 | 2002-09-22 01:26:27 +1000 | [diff] [blame] | 325 | verbose("Read error from remote host " |
djm@openbsd.org | 9576726 | 2016-03-07 19:02:43 +0000 | [diff] [blame] | 326 | "%.100s port %d: %.100s", |
| 327 | ssh_remote_ipaddr(ssh), |
| 328 | ssh_remote_port(ssh), strerror(errno)); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 329 | cleanup_exit(255); |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 330 | } |
| 331 | } else { |
| 332 | /* Buffer any received data. */ |
| 333 | packet_process_incoming(buf, len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 334 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 335 | } |
markus@openbsd.org | 6cb6dcf | 2016-08-13 17:47:40 +0000 | [diff] [blame] | 336 | return 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 337 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 338 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 339 | /* |
| 340 | * Sends data from internal buffers to client program stdin. |
| 341 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 342 | static void |
markus@openbsd.org | 6cb6dcf | 2016-08-13 17:47:40 +0000 | [diff] [blame] | 343 | process_output(fd_set *writeset, int connection_out) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 344 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 345 | /* Send any buffered packet data to the client. */ |
| 346 | if (FD_ISSET(connection_out, writeset)) |
| 347 | packet_write_poll(); |
| 348 | } |
| 349 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 350 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 351 | process_buffered_input_packets(struct ssh *ssh) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 352 | { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 353 | ssh_dispatch_run_fatal(ssh, DISPATCH_NONBLOCK, NULL); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 354 | } |
| 355 | |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 356 | static void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 357 | collect_children(struct ssh *ssh) |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 358 | { |
| 359 | pid_t pid; |
| 360 | sigset_t oset, nset; |
| 361 | int status; |
| 362 | |
| 363 | /* block SIGCHLD while we check for dead children */ |
| 364 | sigemptyset(&nset); |
| 365 | sigaddset(&nset, SIGCHLD); |
| 366 | sigprocmask(SIG_BLOCK, &nset, &oset); |
| 367 | if (child_terminated) { |
Damien Miller | ec04f36 | 2006-03-15 12:01:34 +1100 | [diff] [blame] | 368 | debug("Received SIGCHLD."); |
Ben Lindstrom | 47fd811 | 2002-04-02 20:48:19 +0000 | [diff] [blame] | 369 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0 || |
| 370 | (pid < 0 && errno == EINTR)) |
| 371 | if (pid > 0) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 372 | session_close_by_pid(ssh, pid, status); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 373 | child_terminated = 0; |
| 374 | } |
| 375 | sigprocmask(SIG_SETMASK, &oset, NULL); |
| 376 | } |
| 377 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 378 | void |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 379 | server_loop2(struct ssh *ssh, Authctxt *authctxt) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 380 | { |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 381 | fd_set *readset = NULL, *writeset = NULL; |
djm@openbsd.org | 19bcf2e | 2016-02-08 10:57:07 +0000 | [diff] [blame] | 382 | int max_fd; |
markus@openbsd.org | 6cb6dcf | 2016-08-13 17:47:40 +0000 | [diff] [blame] | 383 | u_int nalloc = 0, connection_in, connection_out; |
Darren Tucker | 5f96f3b | 2013-05-16 20:29:28 +1000 | [diff] [blame] | 384 | u_int64_t rekey_timeout_ms = 0; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 385 | |
| 386 | debug("Entering interactive session for SSH2."); |
| 387 | |
Darren Tucker | 389125b | 2018-02-15 21:43:01 +1100 | [diff] [blame] | 388 | signal(SIGCHLD, sigchld_handler); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 389 | child_terminated = 0; |
| 390 | connection_in = packet_get_connection_in(); |
| 391 | connection_out = packet_get_connection_out(); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 392 | |
Damien Miller | 24ecf61 | 2005-11-05 15:16:52 +1100 | [diff] [blame] | 393 | if (!use_privsep) { |
| 394 | signal(SIGTERM, sigterm_handler); |
| 395 | signal(SIGINT, sigterm_handler); |
| 396 | signal(SIGQUIT, sigterm_handler); |
| 397 | } |
| 398 | |
Damien Miller | f6681a3 | 2001-12-21 14:53:11 +1100 | [diff] [blame] | 399 | notify_setup(); |
| 400 | |
deraadt@openbsd.org | 9136ec1 | 2016-09-12 01:22:38 +0000 | [diff] [blame] | 401 | max_fd = MAXIMUM(connection_in, connection_out); |
| 402 | max_fd = MAXIMUM(max_fd, notify_pipe[0]); |
Damien Miller | f6681a3 | 2001-12-21 14:53:11 +1100 | [diff] [blame] | 403 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 404 | server_init_dispatch(); |
| 405 | |
| 406 | for (;;) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 407 | process_buffered_input_packets(ssh); |
Ben Lindstrom | 8e312f3 | 2001-04-04 23:50:21 +0000 | [diff] [blame] | 408 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 409 | if (!ssh_packet_is_rekeying(ssh) && |
djm@openbsd.org | 19bcf2e | 2016-02-08 10:57:07 +0000 | [diff] [blame] | 410 | packet_not_very_much_data_to_write()) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 411 | channel_output_poll(ssh); |
| 412 | if (options.rekey_interval > 0 && !ssh_packet_is_rekeying(ssh)) |
Darren Tucker | 5f96f3b | 2013-05-16 20:29:28 +1000 | [diff] [blame] | 413 | rekey_timeout_ms = packet_get_rekey_timeout() * 1000; |
| 414 | else |
| 415 | rekey_timeout_ms = 0; |
| 416 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 417 | wait_until_can_do_something(ssh, connection_in, connection_out, |
markus@openbsd.org | 6cb6dcf | 2016-08-13 17:47:40 +0000 | [diff] [blame] | 418 | &readset, &writeset, &max_fd, &nalloc, rekey_timeout_ms); |
Damien Miller | 52b77be | 2001-10-10 15:14:37 +1000 | [diff] [blame] | 419 | |
Damien Miller | 24ecf61 | 2005-11-05 15:16:52 +1100 | [diff] [blame] | 420 | if (received_sigterm) { |
Darren Tucker | 3e1027c | 2012-12-07 13:07:46 +1100 | [diff] [blame] | 421 | logit("Exiting on signal %d", (int)received_sigterm); |
Damien Miller | 24ecf61 | 2005-11-05 15:16:52 +1100 | [diff] [blame] | 422 | /* Clean up sessions, utmp, etc. */ |
| 423 | cleanup_exit(255); |
| 424 | } |
| 425 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 426 | collect_children(ssh); |
| 427 | if (!ssh_packet_is_rekeying(ssh)) |
| 428 | channel_after_select(ssh, readset, writeset); |
| 429 | if (process_input(ssh, readset, connection_in) < 0) |
Ben Lindstrom | e34ab4c | 2001-04-07 01:12:11 +0000 | [diff] [blame] | 430 | break; |
markus@openbsd.org | 6cb6dcf | 2016-08-13 17:47:40 +0000 | [diff] [blame] | 431 | process_output(writeset, connection_out); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 432 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 433 | collect_children(ssh); |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 434 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 435 | free(readset); |
| 436 | free(writeset); |
Damien Miller | 5e95321 | 2001-01-30 09:14:00 +1100 | [diff] [blame] | 437 | |
Damien Miller | 52b77be | 2001-10-10 15:14:37 +1000 | [diff] [blame] | 438 | /* free all channels, no more reads and writes */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 439 | channel_free_all(ssh); |
Ben Lindstrom | 4983d5e | 2001-07-04 05:17:40 +0000 | [diff] [blame] | 440 | |
Damien Miller | 3ec2759 | 2001-10-12 11:35:04 +1000 | [diff] [blame] | 441 | /* free remaining sessions, e.g. remove wtmp entries */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 442 | session_destroy_all(ssh, NULL); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 443 | } |
| 444 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 445 | static int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 446 | server_input_keep_alive(int type, u_int32_t seq, struct ssh *ssh) |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 447 | { |
Damien Miller | b5820f4 | 2003-12-17 16:27:32 +1100 | [diff] [blame] | 448 | debug("Got %d/%u for keepalive", type, seq); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 449 | /* |
Ben Lindstrom | 2f0304c | 2001-04-29 19:49:14 +0000 | [diff] [blame] | 450 | * reset timeout, since we got a sane answer from the client. |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 451 | * even if this was generated by something other than |
| 452 | * the bogus CHANNEL_REQUEST we send for keepalives. |
| 453 | */ |
Darren Tucker | f7288d7 | 2009-06-21 18:12:20 +1000 | [diff] [blame] | 454 | packet_set_alive_timeouts(0); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 455 | return 0; |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 458 | static Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 459 | server_request_direct_tcpip(struct ssh *ssh, int *reason, const char **errmsg) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 460 | { |
Damien Miller | aa5b3f8 | 2012-12-03 09:50:54 +1100 | [diff] [blame] | 461 | Channel *c = NULL; |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 462 | char *target, *originator; |
Damien Miller | 3dc71ad | 2009-01-28 16:31:22 +1100 | [diff] [blame] | 463 | u_short target_port, originator_port; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 464 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 465 | target = packet_get_string(NULL); |
| 466 | target_port = packet_get_int(); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 467 | originator = packet_get_string(NULL); |
| 468 | originator_port = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 469 | packet_check_eom(); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 470 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 471 | debug("%s: originator %s port %d, target %s port %d", __func__, |
| 472 | originator, originator_port, target, target_port); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 473 | |
Damien Miller | aa5b3f8 | 2012-12-03 09:50:54 +1100 | [diff] [blame] | 474 | /* XXX fine grained permissions */ |
| 475 | if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0 && |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 476 | auth_opts->permit_port_forwarding_flag && |
| 477 | !options.disable_forwarding) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 478 | c = channel_connect_to_port(ssh, target, target_port, |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 479 | "direct-tcpip", "direct-tcpip", reason, errmsg); |
Damien Miller | aa5b3f8 | 2012-12-03 09:50:54 +1100 | [diff] [blame] | 480 | } else { |
| 481 | logit("refused local port forward: " |
| 482 | "originator %s port %d, target %s port %d", |
| 483 | originator, originator_port, target, target_port); |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 484 | if (reason != NULL) |
| 485 | *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED; |
Damien Miller | aa5b3f8 | 2012-12-03 09:50:54 +1100 | [diff] [blame] | 486 | } |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 487 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 488 | free(originator); |
| 489 | free(target); |
Damien Miller | bd74025 | 2008-05-19 15:37:09 +1000 | [diff] [blame] | 490 | |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 491 | return c; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 492 | } |
| 493 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 494 | static Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 495 | server_request_direct_streamlocal(struct ssh *ssh) |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 496 | { |
| 497 | Channel *c = NULL; |
| 498 | char *target, *originator; |
| 499 | u_short originator_port; |
djm@openbsd.org | 5104586 | 2017-01-04 05:37:40 +0000 | [diff] [blame] | 500 | struct passwd *pw = the_authctxt->pw; |
| 501 | |
| 502 | if (pw == NULL || !the_authctxt->valid) |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 503 | fatal("%s: no/invalid user", __func__); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 504 | |
| 505 | target = packet_get_string(NULL); |
| 506 | originator = packet_get_string(NULL); |
| 507 | originator_port = packet_get_int(); |
| 508 | packet_check_eom(); |
| 509 | |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 510 | debug("%s: originator %s port %d, target %s", __func__, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 511 | originator, originator_port, target); |
| 512 | |
| 513 | /* XXX fine grained permissions */ |
| 514 | if ((options.allow_streamlocal_forwarding & FORWARD_LOCAL) != 0 && |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 515 | auth_opts->permit_port_forwarding_flag && |
| 516 | !options.disable_forwarding && (pw->pw_uid == 0 || use_privsep)) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 517 | c = channel_connect_to_path(ssh, target, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 518 | "direct-streamlocal@openssh.com", "direct-streamlocal"); |
| 519 | } else { |
| 520 | logit("refused streamlocal port forward: " |
| 521 | "originator %s port %d, target %s", |
| 522 | originator, originator_port, target); |
| 523 | } |
| 524 | |
| 525 | free(originator); |
| 526 | free(target); |
| 527 | |
| 528 | return c; |
| 529 | } |
| 530 | |
| 531 | static Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 532 | server_request_tun(struct ssh *ssh) |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 533 | { |
| 534 | Channel *c = NULL; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 535 | int mode, tun, sock; |
djm@openbsd.org | b7548b1 | 2017-10-23 05:08:00 +0000 | [diff] [blame] | 536 | char *tmp, *ifname = NULL; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 537 | |
Damien Miller | 7b58e80 | 2005-12-13 19:33:19 +1100 | [diff] [blame] | 538 | mode = packet_get_int(); |
| 539 | switch (mode) { |
| 540 | case SSH_TUNMODE_POINTOPOINT: |
| 541 | case SSH_TUNMODE_ETHERNET: |
| 542 | break; |
| 543 | default: |
| 544 | packet_send_debug("Unsupported tunnel device mode."); |
| 545 | return NULL; |
| 546 | } |
| 547 | if ((options.permit_tun & mode) == 0) { |
| 548 | packet_send_debug("Server has rejected tunnel device " |
| 549 | "forwarding"); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 550 | return NULL; |
| 551 | } |
| 552 | |
| 553 | tun = packet_get_int(); |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 554 | if (auth_opts->force_tun_device != -1) { |
| 555 | if (tun != SSH_TUNID_ANY && auth_opts->force_tun_device != tun) |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 556 | goto done; |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 557 | tun = auth_opts->force_tun_device; |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 558 | } |
djm@openbsd.org | b7548b1 | 2017-10-23 05:08:00 +0000 | [diff] [blame] | 559 | sock = tun_open(tun, mode, &ifname); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 560 | if (sock < 0) |
| 561 | goto done; |
djm@openbsd.org | b7548b1 | 2017-10-23 05:08:00 +0000 | [diff] [blame] | 562 | debug("Tunnel forwarding using interface %s", ifname); |
Darren Tucker | 3c51143 | 2018-02-13 09:07:29 +1100 | [diff] [blame] | 563 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 564 | c = channel_new(ssh, "tun", SSH_CHANNEL_OPEN, sock, sock, -1, |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 565 | CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, "tun", 1); |
| 566 | c->datagram = 1; |
Damien Miller | 598bbc2 | 2005-12-31 16:33:36 +1100 | [diff] [blame] | 567 | #if defined(SSH_TUN_FILTER) |
| 568 | if (mode == SSH_TUNMODE_POINTOPOINT) |
Damien Miller | 871f1e4 | 2017-09-12 18:01:35 +1000 | [diff] [blame] | 569 | channel_register_filter(ssh, c->self, sys_tun_infilter, |
Darren Tucker | 1cf65ae | 2008-06-13 05:09:18 +1000 | [diff] [blame] | 570 | sys_tun_outfilter, NULL, NULL); |
Damien Miller | 598bbc2 | 2005-12-31 16:33:36 +1100 | [diff] [blame] | 571 | #endif |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 572 | |
djm@openbsd.org | b7548b1 | 2017-10-23 05:08:00 +0000 | [diff] [blame] | 573 | /* |
| 574 | * Update the list of names exposed to the session |
| 575 | * XXX remove these if the tunnels are closed (won't matter |
| 576 | * much if they are already in the environment though) |
| 577 | */ |
| 578 | tmp = tun_fwd_ifnames; |
| 579 | xasprintf(&tun_fwd_ifnames, "%s%s%s", |
| 580 | tun_fwd_ifnames == NULL ? "" : tun_fwd_ifnames, |
| 581 | tun_fwd_ifnames == NULL ? "" : ",", |
| 582 | ifname); |
| 583 | free(tmp); |
| 584 | free(ifname); |
| 585 | |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 586 | done: |
| 587 | if (c == NULL) |
| 588 | packet_send_debug("Failed to open the tunnel device."); |
| 589 | return c; |
| 590 | } |
| 591 | |
| 592 | static Channel * |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 593 | server_request_session(struct ssh *ssh) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 594 | { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 595 | Channel *c; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 596 | |
| 597 | debug("input_session_request"); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 598 | packet_check_eom(); |
Darren Tucker | 8901fa9 | 2008-06-11 09:34:01 +1000 | [diff] [blame] | 599 | |
| 600 | if (no_more_sessions) { |
| 601 | packet_disconnect("Possible attack: attempt to open a session " |
| 602 | "after additional sessions disabled"); |
| 603 | } |
| 604 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 605 | /* |
| 606 | * A server session has no fd to read or write until a |
| 607 | * CHANNEL_REQUEST for a shell is made, so we set the type to |
| 608 | * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all |
| 609 | * CHANNEL_REQUEST messages is registered. |
| 610 | */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 611 | c = channel_new(ssh, "session", SSH_CHANNEL_LARVAL, |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 612 | -1, -1, -1, /*window size*/0, CHAN_SES_PACKET_DEFAULT, |
Damien Miller | b1ca8bb | 2003-05-14 13:45:42 +1000 | [diff] [blame] | 613 | 0, "server-session", 1); |
Darren Tucker | 3e33cec | 2003-10-02 16:12:36 +1000 | [diff] [blame] | 614 | if (session_open(the_authctxt, c->self) != 1) { |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 615 | debug("session open failed, free channel %d", c->self); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 616 | channel_free(ssh, c); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 617 | return NULL; |
| 618 | } |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 619 | channel_register_cleanup(ssh, c->self, session_close_by_channel, 0); |
Ben Lindstrom | 99c73b3 | 2001-05-05 04:09:47 +0000 | [diff] [blame] | 620 | return c; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 621 | } |
| 622 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 623 | static int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 624 | server_input_channel_open(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 625 | { |
| 626 | Channel *c = NULL; |
| 627 | char *ctype; |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 628 | const char *errmsg = NULL; |
| 629 | int rchan, reason = SSH2_OPEN_CONNECT_FAILED; |
Ben Lindstrom | 4fed2be | 2002-06-25 23:17:36 +0000 | [diff] [blame] | 630 | u_int rmaxpack, rwindow, len; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 631 | |
| 632 | ctype = packet_get_string(&len); |
| 633 | rchan = packet_get_int(); |
| 634 | rwindow = packet_get_int(); |
| 635 | rmaxpack = packet_get_int(); |
| 636 | |
djm@openbsd.org | 027607f | 2018-06-08 01:55:40 +0000 | [diff] [blame] | 637 | debug("%s: ctype %s rchan %d win %d max %d", __func__, |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 638 | ctype, rchan, rwindow, rmaxpack); |
| 639 | |
| 640 | if (strcmp(ctype, "session") == 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 641 | c = server_request_session(ssh); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 642 | } else if (strcmp(ctype, "direct-tcpip") == 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 643 | c = server_request_direct_tcpip(ssh, &reason, &errmsg); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 644 | } else if (strcmp(ctype, "direct-streamlocal@openssh.com") == 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 645 | c = server_request_direct_streamlocal(ssh); |
Damien Miller | d27b947 | 2005-12-13 19:29:02 +1100 | [diff] [blame] | 646 | } else if (strcmp(ctype, "tun@openssh.com") == 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 647 | c = server_request_tun(ssh); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 648 | } |
| 649 | if (c != NULL) { |
djm@openbsd.org | 027607f | 2018-06-08 01:55:40 +0000 | [diff] [blame] | 650 | debug("%s: confirm %s", __func__, ctype); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 651 | c->remote_id = rchan; |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 652 | c->have_remote_id = 1; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 653 | c->remote_window = rwindow; |
| 654 | c->remote_maxpacket = rmaxpack; |
Ben Lindstrom | 6912866 | 2001-05-08 20:07:39 +0000 | [diff] [blame] | 655 | if (c->type != SSH_CHANNEL_CONNECTING) { |
| 656 | packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 657 | packet_put_int(c->remote_id); |
| 658 | packet_put_int(c->self); |
| 659 | packet_put_int(c->local_window); |
| 660 | packet_put_int(c->local_maxpacket); |
| 661 | packet_send(); |
| 662 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 663 | } else { |
djm@openbsd.org | 027607f | 2018-06-08 01:55:40 +0000 | [diff] [blame] | 664 | debug("%s: failure %s", __func__, ctype); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 665 | packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
| 666 | packet_put_int(rchan); |
dtucker@openbsd.org | 858252f | 2017-02-01 02:59:09 +0000 | [diff] [blame] | 667 | packet_put_int(reason); |
djm@openbsd.org | 14b5c63 | 2018-01-23 05:27:21 +0000 | [diff] [blame] | 668 | packet_put_cstring(errmsg ? errmsg : "open failed"); |
| 669 | packet_put_cstring(""); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 670 | packet_send(); |
| 671 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 672 | free(ctype); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 673 | return 0; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 674 | } |
| 675 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 676 | static int |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 677 | server_input_hostkeys_prove(struct ssh *ssh, struct sshbuf **respp) |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 678 | { |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 679 | struct sshbuf *resp = NULL; |
| 680 | struct sshbuf *sigbuf = NULL; |
| 681 | struct sshkey *key = NULL, *key_pub = NULL, *key_prv = NULL; |
djm@openbsd.org | 7860731 | 2017-12-18 23:16:23 +0000 | [diff] [blame] | 682 | int r, ndx, kexsigtype, use_kexsigtype, success = 0; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 683 | const u_char *blob; |
| 684 | u_char *sig = 0; |
| 685 | size_t blen, slen; |
| 686 | |
| 687 | if ((resp = sshbuf_new()) == NULL || (sigbuf = sshbuf_new()) == NULL) |
| 688 | fatal("%s: sshbuf_new", __func__); |
| 689 | |
djm@openbsd.org | 7860731 | 2017-12-18 23:16:23 +0000 | [diff] [blame] | 690 | kexsigtype = sshkey_type_plain( |
| 691 | sshkey_type_from_name(ssh->kex->hostkey_alg)); |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 692 | while (ssh_packet_remaining(ssh) > 0) { |
| 693 | sshkey_free(key); |
| 694 | key = NULL; |
| 695 | if ((r = sshpkt_get_string_direct(ssh, &blob, &blen)) != 0 || |
| 696 | (r = sshkey_from_blob(blob, blen, &key)) != 0) { |
| 697 | error("%s: couldn't parse key: %s", |
| 698 | __func__, ssh_err(r)); |
| 699 | goto out; |
| 700 | } |
| 701 | /* |
| 702 | * Better check that this is actually one of our hostkeys |
| 703 | * before attempting to sign anything with it. |
| 704 | */ |
| 705 | if ((ndx = ssh->kex->host_key_index(key, 1, ssh)) == -1) { |
| 706 | error("%s: unknown host %s key", |
| 707 | __func__, sshkey_type(key)); |
| 708 | goto out; |
| 709 | } |
| 710 | /* |
| 711 | * XXX refactor: make kex->sign just use an index rather |
| 712 | * than passing in public and private keys |
| 713 | */ |
| 714 | if ((key_prv = get_hostkey_by_index(ndx)) == NULL && |
| 715 | (key_pub = get_hostkey_public_by_index(ndx, ssh)) == NULL) { |
| 716 | error("%s: can't retrieve hostkey %d", __func__, ndx); |
| 717 | goto out; |
| 718 | } |
| 719 | sshbuf_reset(sigbuf); |
| 720 | free(sig); |
| 721 | sig = NULL; |
djm@openbsd.org | 7860731 | 2017-12-18 23:16:23 +0000 | [diff] [blame] | 722 | /* |
| 723 | * For RSA keys, prefer to use the signature type negotiated |
| 724 | * during KEX to the default (SHA1). |
| 725 | */ |
| 726 | use_kexsigtype = kexsigtype == KEY_RSA && |
| 727 | sshkey_type_plain(key->type) == KEY_RSA; |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame] | 728 | if ((r = sshbuf_put_cstring(sigbuf, |
| 729 | "hostkeys-prove-00@openssh.com")) != 0 || |
| 730 | (r = sshbuf_put_string(sigbuf, |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 731 | ssh->kex->session_id, ssh->kex->session_id_len)) != 0 || |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 732 | (r = sshkey_puts(key, sigbuf)) != 0 || |
| 733 | (r = ssh->kex->sign(key_prv, key_pub, &sig, &slen, |
djm@openbsd.org | 04c7e28 | 2017-12-18 02:25:15 +0000 | [diff] [blame] | 734 | sshbuf_ptr(sigbuf), sshbuf_len(sigbuf), |
djm@openbsd.org | 7860731 | 2017-12-18 23:16:23 +0000 | [diff] [blame] | 735 | use_kexsigtype ? ssh->kex->hostkey_alg : NULL, 0)) != 0 || |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 736 | (r = sshbuf_put_string(resp, sig, slen)) != 0) { |
| 737 | error("%s: couldn't prepare signature: %s", |
| 738 | __func__, ssh_err(r)); |
| 739 | goto out; |
| 740 | } |
| 741 | } |
| 742 | /* Success */ |
| 743 | *respp = resp; |
| 744 | resp = NULL; /* don't free it */ |
| 745 | success = 1; |
| 746 | out: |
| 747 | free(sig); |
| 748 | sshbuf_free(resp); |
| 749 | sshbuf_free(sigbuf); |
| 750 | sshkey_free(key); |
| 751 | return success; |
| 752 | } |
| 753 | |
| 754 | static int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 755 | server_input_global_request(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 756 | { |
| 757 | char *rtype; |
| 758 | int want_reply; |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 759 | int r, success = 0, allocated_listen_port = 0; |
| 760 | struct sshbuf *resp = NULL; |
djm@openbsd.org | 5104586 | 2017-01-04 05:37:40 +0000 | [diff] [blame] | 761 | struct passwd *pw = the_authctxt->pw; |
| 762 | |
| 763 | if (pw == NULL || !the_authctxt->valid) |
djm@openbsd.org | 027607f | 2018-06-08 01:55:40 +0000 | [diff] [blame] | 764 | fatal("%s: no/invalid user", __func__); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 765 | |
| 766 | rtype = packet_get_string(NULL); |
| 767 | want_reply = packet_get_char(); |
djm@openbsd.org | 027607f | 2018-06-08 01:55:40 +0000 | [diff] [blame] | 768 | debug("%s: rtype %s want_reply %d", __func__, rtype, want_reply); |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 769 | |
Ben Lindstrom | 7bb8b49 | 2001-03-17 00:47:54 +0000 | [diff] [blame] | 770 | /* -R style forwarding */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 771 | if (strcmp(rtype, "tcpip-forward") == 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 772 | struct Forward fwd; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 773 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 774 | memset(&fwd, 0, sizeof(fwd)); |
| 775 | fwd.listen_host = packet_get_string(NULL); |
| 776 | fwd.listen_port = (u_short)packet_get_int(); |
djm@openbsd.org | 027607f | 2018-06-08 01:55:40 +0000 | [diff] [blame] | 777 | debug("%s: tcpip-forward listen %s port %d", __func__, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 778 | fwd.listen_host, fwd.listen_port); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 779 | |
| 780 | /* check permissions */ |
Damien Miller | aa5b3f8 | 2012-12-03 09:50:54 +1100 | [diff] [blame] | 781 | if ((options.allow_tcp_forwarding & FORWARD_REMOTE) == 0 || |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 782 | !auth_opts->permit_port_forwarding_flag || |
| 783 | options.disable_forwarding || |
Darren Tucker | 5f41f03 | 2016-04-08 21:14:13 +1000 | [diff] [blame] | 784 | (!want_reply && fwd.listen_port == 0) || |
dtucker@openbsd.org | 1c4ef0b | 2016-10-23 22:04:05 +0000 | [diff] [blame] | 785 | (fwd.listen_port != 0 && |
| 786 | !bind_permitted(fwd.listen_port, pw->pw_uid))) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 787 | success = 0; |
| 788 | packet_send_debug("Server has disabled port forwarding."); |
| 789 | } else { |
| 790 | /* Start listening on the port */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 791 | success = channel_setup_remote_fwd_listener(ssh, &fwd, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 792 | &allocated_listen_port, &options.fwd_opts); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 793 | } |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 794 | free(fwd.listen_host); |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 795 | if ((resp = sshbuf_new()) == NULL) |
| 796 | fatal("%s: sshbuf_new", __func__); |
djm@openbsd.org | b1d6b39 | 2015-11-28 06:41:03 +0000 | [diff] [blame] | 797 | if (allocated_listen_port != 0 && |
| 798 | (r = sshbuf_put_u32(resp, allocated_listen_port)) != 0) |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 799 | fatal("%s: sshbuf_put_u32: %s", __func__, ssh_err(r)); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 800 | } else if (strcmp(rtype, "cancel-tcpip-forward") == 0) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 801 | struct Forward fwd; |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 802 | |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 803 | memset(&fwd, 0, sizeof(fwd)); |
| 804 | fwd.listen_host = packet_get_string(NULL); |
| 805 | fwd.listen_port = (u_short)packet_get_int(); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 806 | debug("%s: cancel-tcpip-forward addr %s port %d", __func__, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 807 | fwd.listen_host, fwd.listen_port); |
Darren Tucker | e7066df | 2004-05-24 10:18:05 +1000 | [diff] [blame] | 808 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 809 | success = channel_cancel_rport_listener(ssh, &fwd); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 810 | free(fwd.listen_host); |
| 811 | } else if (strcmp(rtype, "streamlocal-forward@openssh.com") == 0) { |
| 812 | struct Forward fwd; |
| 813 | |
| 814 | memset(&fwd, 0, sizeof(fwd)); |
| 815 | fwd.listen_path = packet_get_string(NULL); |
djm@openbsd.org | 027607f | 2018-06-08 01:55:40 +0000 | [diff] [blame] | 816 | debug("%s: streamlocal-forward listen path %s", __func__, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 817 | fwd.listen_path); |
| 818 | |
| 819 | /* check permissions */ |
| 820 | if ((options.allow_streamlocal_forwarding & FORWARD_REMOTE) == 0 |
djm@openbsd.org | 7c85685 | 2018-03-03 03:15:51 +0000 | [diff] [blame] | 821 | || !auth_opts->permit_port_forwarding_flag || |
| 822 | options.disable_forwarding || |
djm@openbsd.org | 5104586 | 2017-01-04 05:37:40 +0000 | [diff] [blame] | 823 | (pw->pw_uid != 0 && !use_privsep)) { |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 824 | success = 0; |
djm@openbsd.org | 5104586 | 2017-01-04 05:37:40 +0000 | [diff] [blame] | 825 | packet_send_debug("Server has disabled " |
| 826 | "streamlocal forwarding."); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 827 | } else { |
| 828 | /* Start listening on the socket */ |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 829 | success = channel_setup_remote_fwd_listener(ssh, |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 830 | &fwd, NULL, &options.fwd_opts); |
| 831 | } |
| 832 | free(fwd.listen_path); |
| 833 | } else if (strcmp(rtype, "cancel-streamlocal-forward@openssh.com") == 0) { |
| 834 | struct Forward fwd; |
| 835 | |
| 836 | memset(&fwd, 0, sizeof(fwd)); |
| 837 | fwd.listen_path = packet_get_string(NULL); |
| 838 | debug("%s: cancel-streamlocal-forward path %s", __func__, |
| 839 | fwd.listen_path); |
| 840 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 841 | success = channel_cancel_rport_listener(ssh, &fwd); |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 842 | free(fwd.listen_path); |
Darren Tucker | 8901fa9 | 2008-06-11 09:34:01 +1000 | [diff] [blame] | 843 | } else if (strcmp(rtype, "no-more-sessions@openssh.com") == 0) { |
| 844 | no_more_sessions = 1; |
| 845 | success = 1; |
djm@openbsd.org | 44732de | 2015-02-20 22:17:21 +0000 | [diff] [blame] | 846 | } else if (strcmp(rtype, "hostkeys-prove-00@openssh.com") == 0) { |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 847 | success = server_input_hostkeys_prove(ssh, &resp); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 848 | } |
| 849 | if (want_reply) { |
| 850 | packet_start(success ? |
| 851 | SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 852 | if (success && resp != NULL) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 853 | ssh_packet_put_raw(ssh, sshbuf_ptr(resp), |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 854 | sshbuf_len(resp)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 855 | packet_send(); |
| 856 | packet_write_wait(); |
| 857 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 858 | free(rtype); |
djm@openbsd.org | 523463a | 2015-02-16 22:13:32 +0000 | [diff] [blame] | 859 | sshbuf_free(resp); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 860 | return 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 861 | } |
Damien Miller | 4f7becb | 2006-03-26 14:10:14 +1100 | [diff] [blame] | 862 | |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 863 | static int |
markus@openbsd.org | 2ae666a | 2017-05-30 14:23:52 +0000 | [diff] [blame] | 864 | server_input_channel_req(int type, u_int32_t seq, struct ssh *ssh) |
Damien Miller | c7ef63d | 2002-02-05 12:21:42 +1100 | [diff] [blame] | 865 | { |
| 866 | Channel *c; |
| 867 | int id, reply, success = 0; |
| 868 | char *rtype; |
| 869 | |
| 870 | id = packet_get_int(); |
| 871 | rtype = packet_get_string(NULL); |
| 872 | reply = packet_get_char(); |
| 873 | |
| 874 | debug("server_input_channel_req: channel %d request %s reply %d", |
| 875 | id, rtype, reply); |
| 876 | |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 877 | if ((c = channel_lookup(ssh, id)) == NULL) |
Damien Miller | c7ef63d | 2002-02-05 12:21:42 +1100 | [diff] [blame] | 878 | packet_disconnect("server_input_channel_req: " |
| 879 | "unknown channel %d", id); |
Damien Miller | bab9bd4 | 2008-05-19 16:06:47 +1000 | [diff] [blame] | 880 | if (!strcmp(rtype, "eow@openssh.com")) { |
| 881 | packet_check_eom(); |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 882 | chan_rcvd_eow(ssh, c); |
Darren Tucker | 8810dd4 | 2008-07-02 22:32:14 +1000 | [diff] [blame] | 883 | } else if ((c->type == SSH_CHANNEL_LARVAL || |
| 884 | c->type == SSH_CHANNEL_OPEN) && strcmp(c->ctype, "session") == 0) |
djm@openbsd.org | dbee411 | 2017-09-12 06:32:07 +0000 | [diff] [blame] | 885 | success = session_input_channel_req(ssh, c, rtype); |
Damien Miller | c589378 | 2014-05-15 13:48:49 +1000 | [diff] [blame] | 886 | if (reply && !(c->flags & CHAN_CLOSE_SENT)) { |
djm@openbsd.org | 9f53229 | 2017-09-12 06:35:31 +0000 | [diff] [blame] | 887 | if (!c->have_remote_id) |
| 888 | fatal("%s: channel %d: no remote_id", |
| 889 | __func__, c->self); |
Damien Miller | c7ef63d | 2002-02-05 12:21:42 +1100 | [diff] [blame] | 890 | packet_start(success ? |
| 891 | SSH2_MSG_CHANNEL_SUCCESS : SSH2_MSG_CHANNEL_FAILURE); |
| 892 | packet_put_int(c->remote_id); |
| 893 | packet_send(); |
| 894 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 895 | free(rtype); |
markus@openbsd.org | 3fdc88a | 2015-01-19 20:07:45 +0000 | [diff] [blame] | 896 | return 0; |
Damien Miller | c7ef63d | 2002-02-05 12:21:42 +1100 | [diff] [blame] | 897 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 898 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 899 | static void |
markus@openbsd.org | 6cb6dcf | 2016-08-13 17:47:40 +0000 | [diff] [blame] | 900 | server_init_dispatch(void) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 901 | { |
markus@openbsd.org | 6cb6dcf | 2016-08-13 17:47:40 +0000 | [diff] [blame] | 902 | debug("server_init_dispatch"); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 903 | dispatch_init(&dispatch_protocol_error); |
| 904 | dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); |
| 905 | dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); |
| 906 | dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); |
| 907 | dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); |
| 908 | dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open); |
| 909 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
| 910 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); |
Damien Miller | c7ef63d | 2002-02-05 12:21:42 +1100 | [diff] [blame] | 911 | dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &server_input_channel_req); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 912 | dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 913 | dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request); |
Ben Lindstrom | 5744dc4 | 2001-04-13 23:28:01 +0000 | [diff] [blame] | 914 | /* client_alive */ |
Damien Miller | 5a33ec6 | 2008-12-08 09:55:02 +1100 | [diff] [blame] | 915 | dispatch_set(SSH2_MSG_CHANNEL_SUCCESS, &server_input_keep_alive); |
| 916 | dispatch_set(SSH2_MSG_CHANNEL_FAILURE, &server_input_keep_alive); |
Damien Miller | b5820f4 | 2003-12-17 16:27:32 +1100 | [diff] [blame] | 917 | dispatch_set(SSH2_MSG_REQUEST_SUCCESS, &server_input_keep_alive); |
| 918 | dispatch_set(SSH2_MSG_REQUEST_FAILURE, &server_input_keep_alive); |
Ben Lindstrom | 8ac9106 | 2001-04-04 17:57:54 +0000 | [diff] [blame] | 919 | /* rekeying */ |
| 920 | dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 921 | } |