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