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