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