Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Server main loop for handling the interactive session. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 6 | * |
| 7 | * As far as I am concerned, the code I have written for this software |
| 8 | * can be used freely for any purpose. Any derived versions of this |
| 9 | * software must be clearly marked as such, and if the derived work is |
| 10 | * incompatible with the protocol description in the RFC file, it must be |
| 11 | * called by a name other than "ssh" or "Secure Shell". |
| 12 | * |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 13 | * SSH2 support by Markus Friedl. |
| 14 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 15 | * |
| 16 | * Redistribution and use in source and binary forms, with or without |
| 17 | * modification, are permitted provided that the following conditions |
| 18 | * are met: |
| 19 | * 1. Redistributions of source code must retain the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer. |
| 21 | * 2. Redistributions in binary form must reproduce the above copyright |
| 22 | * notice, this list of conditions and the following disclaimer in the |
| 23 | * documentation and/or other materials provided with the distribution. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 26 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 27 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 28 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 30 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 31 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 32 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 33 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 34 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 35 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 36 | |
| 37 | #include "includes.h" |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 38 | RCSID("$OpenBSD: serverloop.c,v 1.39 2000/12/27 14:19:21 markus Exp $"); |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 39 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 40 | #include "xmalloc.h" |
| 41 | #include "ssh.h" |
| 42 | #include "packet.h" |
| 43 | #include "buffer.h" |
| 44 | #include "servconf.h" |
| 45 | #include "pty.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 46 | #include "channels.h" |
| 47 | |
| 48 | #include "compat.h" |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 49 | #include "ssh2.h" |
| 50 | #include "session.h" |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 51 | #include "dispatch.h" |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 52 | #include "auth-options.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 53 | #include "auth.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 54 | |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 55 | extern ServerOptions options; |
| 56 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 57 | static Buffer stdin_buffer; /* Buffer for stdin data. */ |
| 58 | static Buffer stdout_buffer; /* Buffer for stdout data. */ |
| 59 | static Buffer stderr_buffer; /* Buffer for stderr data. */ |
| 60 | static int fdin; /* Descriptor for stdin (for writing) */ |
| 61 | static int fdout; /* Descriptor for stdout (for reading); |
| 62 | May be same number as fdin. */ |
| 63 | static int fderr; /* Descriptor for stderr. May be -1. */ |
| 64 | static long stdin_bytes = 0; /* Number of bytes written to stdin. */ |
| 65 | static long stdout_bytes = 0; /* Number of stdout bytes sent to client. */ |
| 66 | static long stderr_bytes = 0; /* Number of stderr bytes sent to client. */ |
| 67 | static long fdout_bytes = 0; /* Number of stdout bytes read from program. */ |
| 68 | static int stdin_eof = 0; /* EOF message received from client. */ |
| 69 | static int fdout_eof = 0; /* EOF encountered reading from fdout. */ |
| 70 | static int fderr_eof = 0; /* EOF encountered readung from fderr. */ |
| 71 | static int connection_in; /* Connection to client (input). */ |
| 72 | static int connection_out; /* Connection to client (output). */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 73 | static u_int buffer_high;/* "Soft" max buffer size. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 74 | static int max_fd; /* Max file descriptor number for select(). */ |
| 75 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 76 | /* |
| 77 | * This SIGCHLD kludge is used to detect when the child exits. The server |
| 78 | * will exit after that, as soon as forwarded connections have terminated. |
| 79 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 81 | static pid_t child_pid; /* Pid of the child. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 82 | static volatile int child_terminated; /* The child has terminated. */ |
| 83 | static volatile int child_wait_status; /* Status from wait(). */ |
| 84 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 85 | void server_init_dispatch(void); |
| 86 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 87 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 88 | sigchld_handler(int sig) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 89 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 90 | int save_errno = errno; |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 91 | pid_t wait_pid; |
| 92 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 93 | debug("Received SIGCHLD."); |
| 94 | wait_pid = wait((int *) &child_wait_status); |
| 95 | if (wait_pid != -1) { |
| 96 | if (wait_pid != child_pid) |
| 97 | error("Strange, got SIGCHLD and wait returned pid %d but child is %d", |
| 98 | wait_pid, child_pid); |
| 99 | if (WIFEXITED(child_wait_status) || |
Damien Miller | 43dc8da | 2000-11-29 15:55:17 +1100 | [diff] [blame] | 100 | WIFSIGNALED(child_wait_status)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 101 | child_terminated = 1; |
| 102 | } |
| 103 | signal(SIGCHLD, sigchld_handler); |
| 104 | errno = save_errno; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 105 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 106 | void |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 107 | sigchld_handler2(int sig) |
| 108 | { |
| 109 | int save_errno = errno; |
| 110 | debug("Received SIGCHLD."); |
| 111 | child_terminated = 1; |
Damien Miller | 43dc8da | 2000-11-29 15:55:17 +1100 | [diff] [blame] | 112 | signal(SIGCHLD, sigchld_handler2); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 113 | errno = save_errno; |
| 114 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 115 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 116 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 117 | * Make packets from buffered stderr data, and buffer it for sending |
| 118 | * to the client. |
| 119 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 120 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 121 | make_packets_from_stderr_data() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 122 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 123 | int len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 124 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 125 | /* Send buffered stderr data to the client. */ |
| 126 | while (buffer_len(&stderr_buffer) > 0 && |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 127 | packet_not_very_much_data_to_write()) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 128 | len = buffer_len(&stderr_buffer); |
| 129 | if (packet_is_interactive()) { |
| 130 | if (len > 512) |
| 131 | len = 512; |
| 132 | } else { |
| 133 | /* Keep the packets at reasonable size. */ |
| 134 | if (len > packet_get_maxsize()) |
| 135 | len = packet_get_maxsize(); |
| 136 | } |
| 137 | packet_start(SSH_SMSG_STDERR_DATA); |
| 138 | packet_put_string(buffer_ptr(&stderr_buffer), len); |
| 139 | packet_send(); |
| 140 | buffer_consume(&stderr_buffer, len); |
| 141 | stderr_bytes += len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 142 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 143 | } |
| 144 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 145 | /* |
| 146 | * Make packets from buffered stdout data, and buffer it for sending to the |
| 147 | * client. |
| 148 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 149 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 150 | make_packets_from_stdout_data() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 151 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 152 | int len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 153 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 154 | /* Send buffered stdout data to the client. */ |
| 155 | while (buffer_len(&stdout_buffer) > 0 && |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 156 | packet_not_very_much_data_to_write()) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 157 | len = buffer_len(&stdout_buffer); |
| 158 | if (packet_is_interactive()) { |
| 159 | if (len > 512) |
| 160 | len = 512; |
| 161 | } else { |
| 162 | /* Keep the packets at reasonable size. */ |
| 163 | if (len > packet_get_maxsize()) |
| 164 | len = packet_get_maxsize(); |
| 165 | } |
| 166 | packet_start(SSH_SMSG_STDOUT_DATA); |
| 167 | packet_put_string(buffer_ptr(&stdout_buffer), len); |
| 168 | packet_send(); |
| 169 | buffer_consume(&stdout_buffer, len); |
| 170 | stdout_bytes += len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 171 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 172 | } |
| 173 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 174 | /* |
| 175 | * Sleep in select() until we can do something. This will initialize the |
| 176 | * select masks. Upon return, the masks will indicate which descriptors |
| 177 | * have data or can accept data. Optionally, a maximum time can be specified |
| 178 | * for the duration of the wait (0 = infinite). |
| 179 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 180 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 181 | wait_until_can_do_something(fd_set * readset, fd_set * writeset, |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 182 | u_int max_time_milliseconds) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 183 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 184 | struct timeval tv, *tvp; |
| 185 | int ret; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 186 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 187 | /* When select fails we restart from here. */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 188 | retry_select: |
| 189 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 190 | /* Initialize select() masks. */ |
| 191 | FD_ZERO(readset); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 192 | FD_ZERO(writeset); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 193 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 194 | if (compat20) { |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 195 | /* wrong: bad condition XXX */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 196 | if (channel_not_very_much_buffered_data()) |
| 197 | FD_SET(connection_in, readset); |
| 198 | } else { |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 199 | /* |
| 200 | * Read packets from the client unless we have too much |
| 201 | * buffered stdin or channel data. |
| 202 | */ |
| 203 | if (buffer_len(&stdin_buffer) < buffer_high && |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 204 | channel_not_very_much_buffered_data()) |
| 205 | FD_SET(connection_in, readset); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 206 | /* |
| 207 | * If there is not too much data already buffered going to |
| 208 | * the client, try to get some more data from the program. |
| 209 | */ |
| 210 | if (packet_not_very_much_data_to_write()) { |
| 211 | if (!fdout_eof) |
| 212 | FD_SET(fdout, readset); |
| 213 | if (!fderr_eof) |
| 214 | FD_SET(fderr, readset); |
| 215 | } |
| 216 | /* |
| 217 | * If we have buffered data, try to write some of that data |
| 218 | * to the program. |
| 219 | */ |
| 220 | if (fdin != -1 && buffer_len(&stdin_buffer) > 0) |
| 221 | FD_SET(fdin, writeset); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 222 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 223 | /* Set masks for channel descriptors. */ |
| 224 | channel_prepare_select(readset, writeset); |
| 225 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 226 | /* |
| 227 | * If we have buffered packet data going to the client, mark that |
| 228 | * descriptor. |
| 229 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 230 | if (packet_have_data_to_write()) |
| 231 | FD_SET(connection_out, writeset); |
| 232 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 233 | /* Update the maximum descriptor number if appropriate. */ |
| 234 | if (channel_max_fd() > max_fd) |
| 235 | max_fd = channel_max_fd(); |
| 236 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 237 | /* |
| 238 | * If child has terminated and there is enough buffer space to read |
| 239 | * from it, then read as much as is available and exit. |
| 240 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 241 | if (child_terminated && packet_not_very_much_data_to_write()) |
| 242 | if (max_time_milliseconds == 0) |
| 243 | max_time_milliseconds = 100; |
| 244 | |
| 245 | if (max_time_milliseconds == 0) |
| 246 | tvp = NULL; |
| 247 | else { |
| 248 | tv.tv_sec = max_time_milliseconds / 1000; |
| 249 | tv.tv_usec = 1000 * (max_time_milliseconds % 1000); |
| 250 | tvp = &tv; |
| 251 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 252 | if (tvp!=NULL) |
Ben Lindstrom | 4dccfa5 | 2000-12-28 16:40:05 +0000 | [diff] [blame] | 253 | debug2("tvp!=NULL kid %d mili %d", child_terminated, max_time_milliseconds); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 254 | |
| 255 | /* Wait for something to happen, or the timeout to expire. */ |
| 256 | ret = select(max_fd + 1, readset, writeset, NULL, tvp); |
| 257 | |
| 258 | if (ret < 0) { |
| 259 | if (errno != EINTR) |
| 260 | error("select: %.100s", strerror(errno)); |
| 261 | else |
| 262 | goto retry_select; |
| 263 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 264 | } |
| 265 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 266 | /* |
| 267 | * Processes input from the client and the program. Input data is stored |
| 268 | * in buffers and processed later. |
| 269 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 270 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 271 | process_input(fd_set * readset) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 272 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 273 | int len; |
| 274 | char buf[16384]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 275 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 276 | /* Read and buffer any input data from the client. */ |
| 277 | if (FD_ISSET(connection_in, readset)) { |
| 278 | len = read(connection_in, buf, sizeof(buf)); |
| 279 | if (len == 0) { |
| 280 | verbose("Connection closed by remote host."); |
| 281 | fatal_cleanup(); |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 282 | } else if (len < 0) { |
| 283 | if (errno != EINTR && errno != EAGAIN) { |
| 284 | verbose("Read error from remote host: %.100s", strerror(errno)); |
| 285 | fatal_cleanup(); |
| 286 | } |
| 287 | } else { |
| 288 | /* Buffer any received data. */ |
| 289 | packet_process_incoming(buf, len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 290 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 291 | } |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 292 | if (compat20) |
| 293 | return; |
| 294 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 295 | /* Read and buffer any available stdout data from the program. */ |
| 296 | if (!fdout_eof && FD_ISSET(fdout, readset)) { |
| 297 | len = read(fdout, buf, sizeof(buf)); |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 298 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) { |
| 299 | /* do nothing */ |
| 300 | } else if (len <= 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 301 | fdout_eof = 1; |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 302 | } else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 303 | buffer_append(&stdout_buffer, buf, len); |
| 304 | fdout_bytes += len; |
| 305 | } |
| 306 | } |
| 307 | /* Read and buffer any available stderr data from the program. */ |
| 308 | if (!fderr_eof && FD_ISSET(fderr, readset)) { |
| 309 | len = read(fderr, buf, sizeof(buf)); |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 310 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) { |
| 311 | /* do nothing */ |
| 312 | } else if (len <= 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 313 | fderr_eof = 1; |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 314 | } else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 315 | buffer_append(&stderr_buffer, buf, len); |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 316 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 317 | } |
| 318 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 319 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 320 | /* |
| 321 | * Sends data from internal buffers to client program stdin. |
| 322 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 323 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 324 | process_output(fd_set * writeset) |
| 325 | { |
| 326 | int len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 327 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 328 | /* Write buffered data to program stdin. */ |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 329 | if (!compat20 && fdin != -1 && FD_ISSET(fdin, writeset)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 330 | len = write(fdin, buffer_ptr(&stdin_buffer), |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 331 | buffer_len(&stdin_buffer)); |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 332 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) { |
| 333 | /* do nothing */ |
| 334 | } else if (len <= 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 335 | #ifdef USE_PIPES |
| 336 | close(fdin); |
| 337 | #else |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 338 | if (fdin != fdout) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 339 | close(fdin); |
| 340 | else |
| 341 | shutdown(fdin, SHUT_WR); /* We will no longer send. */ |
| 342 | #endif |
| 343 | fdin = -1; |
| 344 | } else { |
| 345 | /* Successful write. Consume the data from the buffer. */ |
| 346 | buffer_consume(&stdin_buffer, len); |
| 347 | /* Update the count of bytes written to the program. */ |
| 348 | stdin_bytes += len; |
| 349 | } |
| 350 | } |
| 351 | /* Send any buffered packet data to the client. */ |
| 352 | if (FD_ISSET(connection_out, writeset)) |
| 353 | packet_write_poll(); |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * Wait until all buffered output has been sent to the client. |
| 358 | * This is used when the program terminates. |
| 359 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 360 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 361 | drain_output() |
| 362 | { |
| 363 | /* Send any buffered stdout data to the client. */ |
| 364 | if (buffer_len(&stdout_buffer) > 0) { |
| 365 | packet_start(SSH_SMSG_STDOUT_DATA); |
| 366 | packet_put_string(buffer_ptr(&stdout_buffer), |
| 367 | buffer_len(&stdout_buffer)); |
| 368 | packet_send(); |
| 369 | /* Update the count of sent bytes. */ |
| 370 | stdout_bytes += buffer_len(&stdout_buffer); |
| 371 | } |
| 372 | /* Send any buffered stderr data to the client. */ |
| 373 | if (buffer_len(&stderr_buffer) > 0) { |
| 374 | packet_start(SSH_SMSG_STDERR_DATA); |
| 375 | packet_put_string(buffer_ptr(&stderr_buffer), |
| 376 | buffer_len(&stderr_buffer)); |
| 377 | packet_send(); |
| 378 | /* Update the count of sent bytes. */ |
| 379 | stderr_bytes += buffer_len(&stderr_buffer); |
| 380 | } |
| 381 | /* Wait until all buffered data has been written to the client. */ |
| 382 | packet_write_wait(); |
| 383 | } |
| 384 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 385 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 386 | process_buffered_input_packets() |
| 387 | { |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 388 | dispatch_run(DISPATCH_NONBLOCK, NULL, NULL); |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 389 | } |
| 390 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 391 | /* |
| 392 | * Performs the interactive session. This handles data transmission between |
| 393 | * the client and the program. Note that the notion of stdin, stdout, and |
| 394 | * stderr in this function is sort of reversed: this function writes to |
| 395 | * stdin (of the child program), and reads from stdout and stderr (of the |
| 396 | * child program). |
| 397 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 398 | void |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 399 | server_loop(pid_t pid, int fdin_arg, int fdout_arg, int fderr_arg) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 400 | { |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 401 | fd_set readset, writeset; |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 402 | int wait_status; /* Status returned by wait(). */ |
| 403 | pid_t wait_pid; /* pid returned by wait(). */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 404 | int waiting_termination = 0; /* Have displayed waiting close message. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 405 | u_int max_time_milliseconds; |
| 406 | u_int previous_stdout_buffer_bytes; |
| 407 | u_int stdout_buffer_bytes; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 408 | int type; |
| 409 | |
| 410 | debug("Entering interactive session."); |
| 411 | |
| 412 | /* Initialize the SIGCHLD kludge. */ |
| 413 | child_pid = pid; |
| 414 | child_terminated = 0; |
| 415 | signal(SIGCHLD, sigchld_handler); |
Damien Miller | cf3888d | 2000-09-30 14:17:52 +1100 | [diff] [blame] | 416 | signal(SIGPIPE, SIG_IGN); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 417 | |
| 418 | /* Initialize our global variables. */ |
| 419 | fdin = fdin_arg; |
| 420 | fdout = fdout_arg; |
| 421 | fderr = fderr_arg; |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 422 | |
| 423 | /* nonblocking IO */ |
| 424 | set_nonblock(fdin); |
| 425 | set_nonblock(fdout); |
Damien Miller | 7d6656c | 2000-05-20 15:22:36 +1000 | [diff] [blame] | 426 | /* we don't have stderr for interactive terminal sessions, see below */ |
| 427 | if (fderr != -1) |
| 428 | set_nonblock(fderr); |
Damien Miller | dcb6ecd | 2000-05-17 22:34:22 +1000 | [diff] [blame] | 429 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 430 | connection_in = packet_get_connection_in(); |
| 431 | connection_out = packet_get_connection_out(); |
| 432 | |
| 433 | previous_stdout_buffer_bytes = 0; |
| 434 | |
| 435 | /* Set approximate I/O buffer size. */ |
| 436 | if (packet_is_interactive()) |
| 437 | buffer_high = 4096; |
| 438 | else |
| 439 | buffer_high = 64 * 1024; |
| 440 | |
| 441 | /* Initialize max_fd to the maximum of the known file descriptors. */ |
| 442 | max_fd = fdin; |
| 443 | if (fdout > max_fd) |
| 444 | max_fd = fdout; |
| 445 | if (fderr != -1 && fderr > max_fd) |
| 446 | max_fd = fderr; |
| 447 | if (connection_in > max_fd) |
| 448 | max_fd = connection_in; |
| 449 | if (connection_out > max_fd) |
| 450 | max_fd = connection_out; |
| 451 | |
| 452 | /* Initialize Initialize buffers. */ |
| 453 | buffer_init(&stdin_buffer); |
| 454 | buffer_init(&stdout_buffer); |
| 455 | buffer_init(&stderr_buffer); |
| 456 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 457 | /* |
| 458 | * If we have no separate fderr (which is the case when we have a pty |
| 459 | * - there we cannot make difference between data sent to stdout and |
| 460 | * stderr), indicate that we have seen an EOF from stderr. This way |
| 461 | * we don\'t need to check the descriptor everywhere. |
| 462 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 463 | if (fderr == -1) |
| 464 | fderr_eof = 1; |
| 465 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 466 | server_init_dispatch(); |
| 467 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 468 | /* Main loop of the server for the interactive session mode. */ |
| 469 | for (;;) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 470 | |
| 471 | /* Process buffered packets from the client. */ |
| 472 | process_buffered_input_packets(); |
| 473 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 474 | /* |
| 475 | * If we have received eof, and there is no more pending |
| 476 | * input data, cause a real eof by closing fdin. |
| 477 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 478 | if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) { |
| 479 | #ifdef USE_PIPES |
| 480 | close(fdin); |
| 481 | #else |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 482 | if (fdin != fdout) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 483 | close(fdin); |
| 484 | else |
| 485 | shutdown(fdin, SHUT_WR); /* We will no longer send. */ |
| 486 | #endif |
| 487 | fdin = -1; |
| 488 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 489 | /* Make packets from buffered stderr data to send to the client. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 490 | make_packets_from_stderr_data(); |
| 491 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 492 | /* |
| 493 | * Make packets from buffered stdout data to send to the |
| 494 | * client. If there is very little to send, this arranges to |
| 495 | * not send them now, but to wait a short while to see if we |
| 496 | * are getting more data. This is necessary, as some systems |
| 497 | * wake up readers from a pty after each separate character. |
| 498 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 499 | max_time_milliseconds = 0; |
| 500 | stdout_buffer_bytes = buffer_len(&stdout_buffer); |
| 501 | if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 && |
| 502 | stdout_buffer_bytes != previous_stdout_buffer_bytes) { |
| 503 | /* try again after a while */ |
| 504 | max_time_milliseconds = 10; |
| 505 | } else { |
| 506 | /* Send it now. */ |
| 507 | make_packets_from_stdout_data(); |
| 508 | } |
| 509 | previous_stdout_buffer_bytes = buffer_len(&stdout_buffer); |
| 510 | |
| 511 | /* Send channel data to the client. */ |
| 512 | if (packet_not_very_much_data_to_write()) |
| 513 | channel_output_poll(); |
| 514 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 515 | /* |
| 516 | * Bail out of the loop if the program has closed its output |
| 517 | * descriptors, and we have no more data to send to the |
| 518 | * client, and there is no pending buffered data. |
| 519 | */ |
Damien Miller | 43dc8da | 2000-11-29 15:55:17 +1100 | [diff] [blame] | 520 | if (fdout_eof && fderr_eof && !packet_have_data_to_write() && |
| 521 | buffer_len(&stdout_buffer) == 0 && buffer_len(&stderr_buffer) == 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 522 | if (!channel_still_open()) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 523 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 524 | if (!waiting_termination) { |
| 525 | const char *s = "Waiting for forwarded connections to terminate...\r\n"; |
| 526 | char *cp; |
| 527 | waiting_termination = 1; |
| 528 | buffer_append(&stderr_buffer, s, strlen(s)); |
| 529 | |
| 530 | /* Display list of open channels. */ |
| 531 | cp = channel_open_message(); |
| 532 | buffer_append(&stderr_buffer, cp, strlen(cp)); |
| 533 | xfree(cp); |
| 534 | } |
| 535 | } |
| 536 | /* Sleep in select() until we can do something. */ |
| 537 | wait_until_can_do_something(&readset, &writeset, |
| 538 | max_time_milliseconds); |
| 539 | |
| 540 | /* Process any channel events. */ |
| 541 | channel_after_select(&readset, &writeset); |
| 542 | |
| 543 | /* Process input from the client and from program stdout/stderr. */ |
| 544 | process_input(&readset); |
| 545 | |
| 546 | /* Process output to the client and to program stdin. */ |
| 547 | process_output(&writeset); |
| 548 | } |
| 549 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 550 | /* Cleanup and termination code. */ |
| 551 | |
| 552 | /* Wait until all output has been sent to the client. */ |
| 553 | drain_output(); |
| 554 | |
| 555 | debug("End of interactive session; stdin %ld, stdout (read %ld, sent %ld), stderr %ld bytes.", |
| 556 | stdin_bytes, fdout_bytes, stdout_bytes, stderr_bytes); |
| 557 | |
| 558 | /* Free and clear the buffers. */ |
| 559 | buffer_free(&stdin_buffer); |
| 560 | buffer_free(&stdout_buffer); |
| 561 | buffer_free(&stderr_buffer); |
| 562 | |
| 563 | /* Close the file descriptors. */ |
| 564 | if (fdout != -1) |
| 565 | close(fdout); |
| 566 | fdout = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 567 | fdout_eof = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 568 | if (fderr != -1) |
| 569 | close(fderr); |
| 570 | fderr = -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 571 | fderr_eof = 1; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 572 | if (fdin != -1) |
| 573 | close(fdin); |
| 574 | fdin = -1; |
| 575 | |
| 576 | /* Stop listening for channels; this removes unix domain sockets. */ |
| 577 | channel_stop_listening(); |
| 578 | |
| 579 | /* Wait for the child to exit. Get its exit status. */ |
| 580 | wait_pid = wait(&wait_status); |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 581 | if (wait_pid == -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 582 | /* |
| 583 | * It is possible that the wait was handled by SIGCHLD |
| 584 | * handler. This may result in either: this call |
| 585 | * returning with EINTR, or: this call returning ECHILD. |
| 586 | */ |
| 587 | if (child_terminated) |
| 588 | wait_status = child_wait_status; |
| 589 | else |
| 590 | packet_disconnect("wait: %.100s", strerror(errno)); |
| 591 | } else { |
| 592 | /* Check if it matches the process we forked. */ |
| 593 | if (wait_pid != pid) |
| 594 | error("Strange, wait returned pid %d, expected %d", |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 595 | wait_pid, pid); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /* We no longer want our SIGCHLD handler to be called. */ |
| 599 | signal(SIGCHLD, SIG_DFL); |
| 600 | |
| 601 | /* Check if it exited normally. */ |
| 602 | if (WIFEXITED(wait_status)) { |
| 603 | /* Yes, normal exit. Get exit status and send it to the client. */ |
| 604 | debug("Command exited with status %d.", WEXITSTATUS(wait_status)); |
| 605 | packet_start(SSH_SMSG_EXITSTATUS); |
| 606 | packet_put_int(WEXITSTATUS(wait_status)); |
| 607 | packet_send(); |
| 608 | packet_write_wait(); |
| 609 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 610 | /* |
| 611 | * Wait for exit confirmation. Note that there might be |
| 612 | * other packets coming before it; however, the program has |
| 613 | * already died so we just ignore them. The client is |
| 614 | * supposed to respond with the confirmation when it receives |
| 615 | * the exit status. |
| 616 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 617 | do { |
| 618 | int plen; |
| 619 | type = packet_read(&plen); |
| 620 | } |
| 621 | while (type != SSH_CMSG_EXIT_CONFIRMATION); |
| 622 | |
| 623 | debug("Received exit confirmation."); |
| 624 | return; |
| 625 | } |
| 626 | /* Check if the program terminated due to a signal. */ |
| 627 | if (WIFSIGNALED(wait_status)) |
| 628 | packet_disconnect("Command terminated on signal %d.", |
| 629 | WTERMSIG(wait_status)); |
| 630 | |
| 631 | /* Some weird exit cause. Just exit. */ |
| 632 | packet_disconnect("wait returned status %04x.", wait_status); |
| 633 | /* NOTREACHED */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 634 | } |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 635 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 636 | void |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 637 | server_loop2(void) |
| 638 | { |
| 639 | fd_set readset, writeset; |
| 640 | int had_channel = 0; |
| 641 | int status; |
| 642 | pid_t pid; |
| 643 | |
| 644 | debug("Entering interactive session for SSH2."); |
| 645 | |
| 646 | signal(SIGCHLD, sigchld_handler2); |
Damien Miller | cf3888d | 2000-09-30 14:17:52 +1100 | [diff] [blame] | 647 | signal(SIGPIPE, SIG_IGN); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 648 | child_terminated = 0; |
| 649 | connection_in = packet_get_connection_in(); |
| 650 | connection_out = packet_get_connection_out(); |
| 651 | max_fd = connection_in; |
| 652 | if (connection_out > max_fd) |
| 653 | max_fd = connection_out; |
| 654 | server_init_dispatch(); |
| 655 | |
| 656 | for (;;) { |
| 657 | process_buffered_input_packets(); |
| 658 | if (!had_channel && channel_still_open()) |
| 659 | had_channel = 1; |
| 660 | if (had_channel && !channel_still_open()) { |
| 661 | debug("!channel_still_open."); |
| 662 | break; |
| 663 | } |
| 664 | if (packet_not_very_much_data_to_write()) |
| 665 | channel_output_poll(); |
| 666 | wait_until_can_do_something(&readset, &writeset, 0); |
Damien Miller | 43dc8da | 2000-11-29 15:55:17 +1100 | [diff] [blame] | 667 | if (child_terminated) { |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 668 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) |
| 669 | session_close_by_pid(pid, status); |
| 670 | child_terminated = 0; |
| 671 | } |
| 672 | channel_after_select(&readset, &writeset); |
| 673 | process_input(&readset); |
| 674 | process_output(&writeset); |
| 675 | } |
| 676 | signal(SIGCHLD, SIG_DFL); |
| 677 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) |
| 678 | session_close_by_pid(pid, status); |
| 679 | channel_stop_listening(); |
| 680 | } |
| 681 | |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 682 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 683 | server_input_stdin_data(int type, int plen, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 684 | { |
| 685 | char *data; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 686 | u_int data_len; |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 687 | |
| 688 | /* Stdin data from the client. Append it to the buffer. */ |
| 689 | /* Ignore any data if the client has closed stdin. */ |
| 690 | if (fdin == -1) |
| 691 | return; |
| 692 | data = packet_get_string(&data_len); |
| 693 | packet_integrity_check(plen, (4 + data_len), type); |
| 694 | buffer_append(&stdin_buffer, data, data_len); |
| 695 | memset(data, 0, data_len); |
| 696 | xfree(data); |
| 697 | } |
| 698 | |
| 699 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 700 | server_input_eof(int type, int plen, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 701 | { |
| 702 | /* |
| 703 | * Eof from the client. The stdin descriptor to the |
| 704 | * program will be closed when all buffered data has |
| 705 | * drained. |
| 706 | */ |
| 707 | debug("EOF received for stdin."); |
| 708 | packet_integrity_check(plen, 0, type); |
| 709 | stdin_eof = 1; |
| 710 | } |
| 711 | |
| 712 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 713 | server_input_window_size(int type, int plen, void *ctxt) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 714 | { |
| 715 | int row = packet_get_int(); |
| 716 | int col = packet_get_int(); |
| 717 | int xpixel = packet_get_int(); |
| 718 | int ypixel = packet_get_int(); |
| 719 | |
| 720 | debug("Window change received."); |
| 721 | packet_integrity_check(plen, 4 * 4, type); |
| 722 | if (fdin != -1) |
| 723 | pty_change_window_size(fdin, row, col, xpixel, ypixel); |
| 724 | } |
| 725 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 726 | Channel * |
| 727 | server_request_direct_tcpip(char *ctype) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 728 | { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 729 | int sock, newch; |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 730 | char *target, *originator; |
| 731 | int target_port, originator_port; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 732 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 733 | target = packet_get_string(NULL); |
| 734 | target_port = packet_get_int(); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 735 | originator = packet_get_string(NULL); |
| 736 | originator_port = packet_get_int(); |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 737 | packet_done(); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 738 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 739 | debug("server_request_direct_tcpip: originator %s port %d, target %s port %d", |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 740 | originator, originator_port, target, target_port); |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 741 | |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 742 | /* XXX check permission */ |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 743 | if (no_port_forwarding_flag || !options.allow_tcp_forwarding) { |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 744 | xfree(target); |
| 745 | xfree(originator); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 746 | return NULL; |
Damien Miller | f6d9e22 | 2000-06-18 14:50:44 +1000 | [diff] [blame] | 747 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 748 | sock = channel_connect_to(target, target_port); |
| 749 | xfree(target); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 750 | xfree(originator); |
| 751 | if (sock < 0) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 752 | return NULL; |
Ben Lindstrom | 7ad9710 | 2000-12-06 01:42:49 +0000 | [diff] [blame] | 753 | newch = channel_new(ctype, SSH_CHANNEL_CONNECTING, |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 754 | sock, sock, -1, CHAN_TCP_WINDOW_DEFAULT, |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 755 | CHAN_TCP_PACKET_DEFAULT, 0, xstrdup("direct-tcpip"), 1); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 756 | return (newch >= 0) ? channel_lookup(newch) : NULL; |
| 757 | } |
| 758 | |
| 759 | Channel * |
| 760 | server_request_session(char *ctype) |
| 761 | { |
| 762 | int newch; |
| 763 | |
| 764 | debug("input_session_request"); |
| 765 | packet_done(); |
| 766 | /* |
| 767 | * A server session has no fd to read or write until a |
| 768 | * CHANNEL_REQUEST for a shell is made, so we set the type to |
| 769 | * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all |
| 770 | * CHANNEL_REQUEST messages is registered. |
| 771 | */ |
| 772 | newch = channel_new(ctype, SSH_CHANNEL_LARVAL, |
| 773 | -1, -1, -1, 0, CHAN_SES_PACKET_DEFAULT, |
| 774 | 0, xstrdup("server-session"), 1); |
| 775 | if (session_open(newch) == 1) { |
| 776 | channel_register_callback(newch, SSH2_MSG_CHANNEL_REQUEST, |
| 777 | session_input_channel_req, (void *)0); |
| 778 | channel_register_cleanup(newch, session_close_by_channel); |
| 779 | return channel_lookup(newch); |
| 780 | } else { |
| 781 | debug("session open failed, free channel %d", newch); |
| 782 | channel_free(newch); |
| 783 | } |
| 784 | return NULL; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 785 | } |
| 786 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 787 | void |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 788 | server_input_channel_open(int type, int plen, void *ctxt) |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 789 | { |
| 790 | Channel *c = NULL; |
| 791 | char *ctype; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 792 | u_int len; |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 793 | int rchan; |
| 794 | int rmaxpack; |
| 795 | int rwindow; |
| 796 | |
| 797 | ctype = packet_get_string(&len); |
| 798 | rchan = packet_get_int(); |
| 799 | rwindow = packet_get_int(); |
| 800 | rmaxpack = packet_get_int(); |
| 801 | |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 802 | 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] | 803 | ctype, rchan, rwindow, rmaxpack); |
| 804 | |
| 805 | if (strcmp(ctype, "session") == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 806 | c = server_request_session(ctype); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 807 | } else if (strcmp(ctype, "direct-tcpip") == 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 808 | c = server_request_direct_tcpip(ctype); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 809 | } |
| 810 | if (c != NULL) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 811 | debug("server_input_channel_open: confirm %s", ctype); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 812 | c->remote_id = rchan; |
| 813 | c->remote_window = rwindow; |
| 814 | c->remote_maxpacket = rmaxpack; |
| 815 | |
| 816 | packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); |
| 817 | packet_put_int(c->remote_id); |
| 818 | packet_put_int(c->self); |
| 819 | packet_put_int(c->local_window); |
| 820 | packet_put_int(c->local_maxpacket); |
| 821 | packet_send(); |
| 822 | } else { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 823 | debug("server_input_channel_open: failure %s", ctype); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 824 | packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
| 825 | packet_put_int(rchan); |
| 826 | packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED); |
| 827 | packet_put_cstring("bla bla"); |
| 828 | packet_put_cstring(""); |
| 829 | packet_send(); |
| 830 | } |
| 831 | xfree(ctype); |
| 832 | } |
| 833 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 834 | void |
| 835 | server_input_global_request(int type, int plen, void *ctxt) |
| 836 | { |
| 837 | char *rtype; |
| 838 | int want_reply; |
| 839 | int success = 0; |
| 840 | |
| 841 | rtype = packet_get_string(NULL); |
| 842 | want_reply = packet_get_char(); |
| 843 | debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply); |
| 844 | |
| 845 | if (strcmp(rtype, "tcpip-forward") == 0) { |
| 846 | struct passwd *pw; |
| 847 | char *listen_address; |
| 848 | u_short listen_port; |
| 849 | |
| 850 | pw = auth_get_user(); |
| 851 | if (pw == NULL) |
| 852 | fatal("server_input_global_request: no user"); |
| 853 | listen_address = packet_get_string(NULL); /* XXX currently ignored */ |
| 854 | listen_port = (u_short)packet_get_int(); |
| 855 | debug("server_input_global_request: tcpip-forward listen %s port %d", |
| 856 | listen_address, listen_port); |
| 857 | |
| 858 | /* check permissions */ |
| 859 | if (!options.allow_tcp_forwarding || |
| 860 | no_port_forwarding_flag || |
| 861 | (listen_port < IPPORT_RESERVED && pw->pw_uid != 0)) { |
| 862 | success = 0; |
| 863 | packet_send_debug("Server has disabled port forwarding."); |
| 864 | } else { |
| 865 | /* Start listening on the port */ |
| 866 | channel_request_forwarding( |
| 867 | listen_address, listen_port, |
| 868 | /*unspec host_to_connect*/ "<unspec host>", |
| 869 | /*unspec port_to_connect*/ 0, |
| 870 | options.gateway_ports, /*remote*/ 1); |
| 871 | success = 1; |
| 872 | } |
| 873 | xfree(listen_address); |
| 874 | } |
| 875 | if (want_reply) { |
| 876 | packet_start(success ? |
| 877 | SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); |
| 878 | packet_send(); |
| 879 | packet_write_wait(); |
| 880 | } |
| 881 | xfree(rtype); |
| 882 | } |
| 883 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 884 | void |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 885 | server_init_dispatch_20() |
| 886 | { |
| 887 | debug("server_init_dispatch_20"); |
| 888 | dispatch_init(&dispatch_protocol_error); |
| 889 | dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); |
| 890 | dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); |
| 891 | dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); |
| 892 | dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); |
| 893 | dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open); |
| 894 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
| 895 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); |
| 896 | dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request); |
| 897 | dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 898 | dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request); |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 899 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 900 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 901 | server_init_dispatch_13() |
| 902 | { |
| 903 | debug("server_init_dispatch_13"); |
| 904 | dispatch_init(NULL); |
| 905 | dispatch_set(SSH_CMSG_EOF, &server_input_eof); |
| 906 | dispatch_set(SSH_CMSG_STDIN_DATA, &server_input_stdin_data); |
| 907 | dispatch_set(SSH_CMSG_WINDOW_SIZE, &server_input_window_size); |
| 908 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close); |
| 909 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation); |
| 910 | dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data); |
| 911 | dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); |
| 912 | dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); |
| 913 | dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open); |
| 914 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 915 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 916 | server_init_dispatch_15() |
| 917 | { |
| 918 | server_init_dispatch_13(); |
| 919 | debug("server_init_dispatch_15"); |
| 920 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof); |
| 921 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_oclose); |
| 922 | } |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 923 | void |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 924 | server_init_dispatch() |
| 925 | { |
Damien Miller | efb4afe | 2000-04-12 18:45:05 +1000 | [diff] [blame] | 926 | if (compat20) |
| 927 | server_init_dispatch_20(); |
| 928 | else if (compat13) |
Damien Miller | b38eff8 | 2000-04-01 11:09:21 +1000 | [diff] [blame] | 929 | server_init_dispatch_13(); |
| 930 | else |
| 931 | server_init_dispatch_15(); |
| 932 | } |