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