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