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