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