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