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