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