]>
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. |
f3c7c613 | 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" | |
cc94bd38 | 38 | RCSID("$OpenBSD: serverloop.c,v 1.60 2001/04/05 23:39:20 markus Exp $"); |
a22aff1f | 39 | |
8efc0c15 | 40 | #include "xmalloc.h" |
8efc0c15 | 41 | #include "packet.h" |
42 | #include "buffer.h" | |
42f11eb2 | 43 | #include "log.h" |
8efc0c15 | 44 | #include "servconf.h" |
1729c161 | 45 | #include "sshpty.h" |
7368a6c8 | 46 | #include "channels.h" |
7368a6c8 | 47 | #include "compat.h" |
42f11eb2 | 48 | #include "ssh1.h" |
e78a59f5 | 49 | #include "ssh2.h" |
59c97189 | 50 | #include "auth.h" |
e78a59f5 | 51 | #include "session.h" |
7368a6c8 | 52 | #include "dispatch.h" |
38c295d6 | 53 | #include "auth-options.h" |
42f11eb2 | 54 | #include "serverloop.h" |
55 | #include "misc.h" | |
7a37c112 | 56 | #include "kex.h" |
8efc0c15 | 57 | |
33de75a3 | 58 | extern ServerOptions options; |
59 | ||
7a37c112 | 60 | /* XXX */ |
61 | extern Kex *xxx_kex; | |
62 | ||
8efc0c15 | 63 | static Buffer stdin_buffer; /* Buffer for stdin data. */ |
64 | static Buffer stdout_buffer; /* Buffer for stdout data. */ | |
65 | static Buffer stderr_buffer; /* Buffer for stderr data. */ | |
66 | static int fdin; /* Descriptor for stdin (for writing) */ | |
67 | static int fdout; /* Descriptor for stdout (for reading); | |
68 | May be same number as fdin. */ | |
69 | static int fderr; /* Descriptor for stderr. May be -1. */ | |
70 | static long stdin_bytes = 0; /* Number of bytes written to stdin. */ | |
71 | static long stdout_bytes = 0; /* Number of stdout bytes sent to client. */ | |
72 | static long stderr_bytes = 0; /* Number of stderr bytes sent to client. */ | |
73 | static long fdout_bytes = 0; /* Number of stdout bytes read from program. */ | |
74 | static int stdin_eof = 0; /* EOF message received from client. */ | |
75 | static int fdout_eof = 0; /* EOF encountered reading from fdout. */ | |
76 | static int fderr_eof = 0; /* EOF encountered readung from fderr. */ | |
e68225b2 | 77 | static int fdin_is_tty = 0; /* fdin points to a tty. */ |
8efc0c15 | 78 | static int connection_in; /* Connection to client (input). */ |
79 | static int connection_out; /* Connection to client (output). */ | |
cc94bd38 | 80 | static int connection_closed = 0; /* Connection to client closed. */ |
81 | static u_int buffer_high; /* "Soft" max buffer size. */ | |
8efc0c15 | 82 | |
aa3378df | 83 | /* |
84 | * This SIGCHLD kludge is used to detect when the child exits. The server | |
85 | * will exit after that, as soon as forwarded connections have terminated. | |
86 | */ | |
8efc0c15 | 87 | |
9da5c3c9 | 88 | static pid_t child_pid; /* Pid of the child. */ |
8efc0c15 | 89 | static volatile int child_terminated; /* The child has terminated. */ |
90 | static volatile int child_wait_status; /* Status from wait(). */ | |
91 | ||
7368a6c8 | 92 | void server_init_dispatch(void); |
93 | ||
6ae2364d | 94 | void |
5260325f | 95 | sigchld_handler(int sig) |
8efc0c15 | 96 | { |
5260325f | 97 | int save_errno = errno; |
9da5c3c9 | 98 | pid_t wait_pid; |
99 | ||
5260325f | 100 | debug("Received SIGCHLD."); |
101 | wait_pid = wait((int *) &child_wait_status); | |
102 | if (wait_pid != -1) { | |
103 | if (wait_pid != child_pid) | |
104 | error("Strange, got SIGCHLD and wait returned pid %d but child is %d", | |
105 | wait_pid, child_pid); | |
106 | if (WIFEXITED(child_wait_status) || | |
7062c40f | 107 | WIFSIGNALED(child_wait_status)) |
5260325f | 108 | child_terminated = 1; |
109 | } | |
110 | signal(SIGCHLD, sigchld_handler); | |
111 | errno = save_errno; | |
8efc0c15 | 112 | } |
6ae2364d | 113 | void |
e78a59f5 | 114 | sigchld_handler2(int sig) |
115 | { | |
116 | int save_errno = errno; | |
117 | debug("Received SIGCHLD."); | |
118 | child_terminated = 1; | |
1aa00dcb | 119 | mysignal(SIGCHLD, sigchld_handler2); |
e78a59f5 | 120 | errno = save_errno; |
121 | } | |
8efc0c15 | 122 | |
5260325f | 123 | /* |
124 | * Make packets from buffered stderr data, and buffer it for sending | |
125 | * to the client. | |
126 | */ | |
6ae2364d | 127 | void |
5ca51e19 | 128 | make_packets_from_stderr_data(void) |
8efc0c15 | 129 | { |
5260325f | 130 | int len; |
131 | ||
132 | /* Send buffered stderr data to the client. */ | |
133 | while (buffer_len(&stderr_buffer) > 0 && | |
a408af76 | 134 | packet_not_very_much_data_to_write()) { |
5260325f | 135 | len = buffer_len(&stderr_buffer); |
136 | if (packet_is_interactive()) { | |
137 | if (len > 512) | |
138 | len = 512; | |
139 | } else { | |
140 | /* Keep the packets at reasonable size. */ | |
141 | if (len > packet_get_maxsize()) | |
142 | len = packet_get_maxsize(); | |
143 | } | |
144 | packet_start(SSH_SMSG_STDERR_DATA); | |
145 | packet_put_string(buffer_ptr(&stderr_buffer), len); | |
146 | packet_send(); | |
147 | buffer_consume(&stderr_buffer, len); | |
148 | stderr_bytes += len; | |
8efc0c15 | 149 | } |
8efc0c15 | 150 | } |
151 | ||
5260325f | 152 | /* |
153 | * Make packets from buffered stdout data, and buffer it for sending to the | |
154 | * client. | |
155 | */ | |
6ae2364d | 156 | void |
5ca51e19 | 157 | make_packets_from_stdout_data(void) |
8efc0c15 | 158 | { |
5260325f | 159 | int len; |
160 | ||
161 | /* Send buffered stdout data to the client. */ | |
162 | while (buffer_len(&stdout_buffer) > 0 && | |
a408af76 | 163 | packet_not_very_much_data_to_write()) { |
5260325f | 164 | len = buffer_len(&stdout_buffer); |
165 | if (packet_is_interactive()) { | |
166 | if (len > 512) | |
167 | len = 512; | |
168 | } else { | |
169 | /* Keep the packets at reasonable size. */ | |
170 | if (len > packet_get_maxsize()) | |
2b87da3b | 171 | len = packet_get_maxsize(); |
5260325f | 172 | } |
173 | packet_start(SSH_SMSG_STDOUT_DATA); | |
174 | packet_put_string(buffer_ptr(&stdout_buffer), len); | |
175 | packet_send(); | |
176 | buffer_consume(&stdout_buffer, len); | |
177 | stdout_bytes += len; | |
8efc0c15 | 178 | } |
8efc0c15 | 179 | } |
180 | ||
5260325f | 181 | /* |
182 | * Sleep in select() until we can do something. This will initialize the | |
183 | * select masks. Upon return, the masks will indicate which descriptors | |
184 | * have data or can accept data. Optionally, a maximum time can be specified | |
185 | * for the duration of the wait (0 = infinite). | |
186 | */ | |
6ae2364d | 187 | void |
39929cdb | 188 | wait_until_can_do_something(fd_set **readsetp, fd_set **writesetp, int *maxfdp, |
189 | u_int max_time_milliseconds) | |
8efc0c15 | 190 | { |
5260325f | 191 | struct timeval tv, *tvp; |
192 | int ret; | |
8efc0c15 | 193 | |
5260325f | 194 | /* When select fails we restart from here. */ |
8efc0c15 | 195 | retry_select: |
196 | ||
39929cdb | 197 | /* Allocate and update select() masks for channel descriptors. */ |
c422989b | 198 | channel_prepare_select(readsetp, writesetp, maxfdp, 0); |
5260325f | 199 | |
e78a59f5 | 200 | if (compat20) { |
1d1ffb87 | 201 | /* wrong: bad condition XXX */ |
e78a59f5 | 202 | if (channel_not_very_much_buffered_data()) |
39929cdb | 203 | FD_SET(connection_in, *readsetp); |
e78a59f5 | 204 | } else { |
71276795 | 205 | /* |
206 | * Read packets from the client unless we have too much | |
207 | * buffered stdin or channel data. | |
208 | */ | |
209 | if (buffer_len(&stdin_buffer) < buffer_high && | |
e78a59f5 | 210 | channel_not_very_much_buffered_data()) |
39929cdb | 211 | FD_SET(connection_in, *readsetp); |
71276795 | 212 | /* |
213 | * If there is not too much data already buffered going to | |
214 | * the client, try to get some more data from the program. | |
215 | */ | |
216 | if (packet_not_very_much_data_to_write()) { | |
217 | if (!fdout_eof) | |
39929cdb | 218 | FD_SET(fdout, *readsetp); |
71276795 | 219 | if (!fderr_eof) |
39929cdb | 220 | FD_SET(fderr, *readsetp); |
71276795 | 221 | } |
222 | /* | |
223 | * If we have buffered data, try to write some of that data | |
224 | * to the program. | |
225 | */ | |
226 | if (fdin != -1 && buffer_len(&stdin_buffer) > 0) | |
39929cdb | 227 | FD_SET(fdin, *writesetp); |
e78a59f5 | 228 | } |
5260325f | 229 | |
aa3378df | 230 | /* |
231 | * If we have buffered packet data going to the client, mark that | |
232 | * descriptor. | |
233 | */ | |
5260325f | 234 | if (packet_have_data_to_write()) |
39929cdb | 235 | FD_SET(connection_out, *writesetp); |
5260325f | 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) |
19d57054 | 253 | debug3("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. */ |
39929cdb | 256 | ret = select((*maxfdp)+1, *readsetp, *writesetp, 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 | } |
8efc0c15 | 264 | } |
265 | ||
5260325f | 266 | /* |
267 | * Processes input from the client and the program. Input data is stored | |
268 | * in buffers and processed later. | |
269 | */ | |
6ae2364d | 270 | void |
5260325f | 271 | process_input(fd_set * readset) |
272 | { | |
273 | int len; | |
274 | char buf[16384]; | |
275 | ||
276 | /* Read and buffer any input data from the client. */ | |
277 | if (FD_ISSET(connection_in, readset)) { | |
278 | len = read(connection_in, buf, sizeof(buf)); | |
279 | if (len == 0) { | |
280 | verbose("Connection closed by remote host."); | |
cc94bd38 | 281 | connection_closed = 1; |
282 | if (compat20) | |
283 | return; | |
5260325f | 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 | { |
4ef922e3 | 329 | struct termios tio; |
5260325f | 330 | int len; |
331 | ||
332 | /* Write buffered data to program stdin. */ | |
e78a59f5 | 333 | if (!compat20 && fdin != -1 && FD_ISSET(fdin, writeset)) { |
5260325f | 334 | len = write(fdin, buffer_ptr(&stdin_buffer), |
a408af76 | 335 | buffer_len(&stdin_buffer)); |
0e73cc53 | 336 | if (len < 0 && (errno == EINTR || errno == EAGAIN)) { |
337 | /* do nothing */ | |
338 | } else if (len <= 0) { | |
8efc0c15 | 339 | #ifdef USE_PIPES |
5260325f | 340 | close(fdin); |
8efc0c15 | 341 | #else |
7368a6c8 | 342 | if (fdin != fdout) |
5260325f | 343 | close(fdin); |
344 | else | |
345 | shutdown(fdin, SHUT_WR); /* We will no longer send. */ | |
8efc0c15 | 346 | #endif |
5260325f | 347 | fdin = -1; |
348 | } else { | |
4ef922e3 | 349 | /* Successful write. */ |
e68225b2 | 350 | if (fdin_is_tty && tcgetattr(fdin, &tio) == 0 && |
0ae4fe1d | 351 | !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) { |
62da27dd | 352 | /* |
353 | * Simulate echo to reduce the impact of | |
354 | * traffic analysis | |
355 | */ | |
95ce5599 | 356 | packet_send_ignore(len); |
4ef922e3 | 357 | packet_send(); |
358 | } | |
359 | /* Consume the data from the buffer. */ | |
5260325f | 360 | buffer_consume(&stdin_buffer, len); |
361 | /* Update the count of bytes written to the program. */ | |
362 | stdin_bytes += len; | |
363 | } | |
8efc0c15 | 364 | } |
5260325f | 365 | /* Send any buffered packet data to the client. */ |
366 | if (FD_ISSET(connection_out, writeset)) | |
367 | packet_write_poll(); | |
8efc0c15 | 368 | } |
369 | ||
5260325f | 370 | /* |
371 | * Wait until all buffered output has been sent to the client. | |
372 | * This is used when the program terminates. | |
373 | */ | |
6ae2364d | 374 | void |
5ca51e19 | 375 | drain_output(void) |
8efc0c15 | 376 | { |
5260325f | 377 | /* Send any buffered stdout data to the client. */ |
378 | if (buffer_len(&stdout_buffer) > 0) { | |
379 | packet_start(SSH_SMSG_STDOUT_DATA); | |
380 | packet_put_string(buffer_ptr(&stdout_buffer), | |
381 | buffer_len(&stdout_buffer)); | |
382 | packet_send(); | |
383 | /* Update the count of sent bytes. */ | |
384 | stdout_bytes += buffer_len(&stdout_buffer); | |
385 | } | |
386 | /* Send any buffered stderr data to the client. */ | |
387 | if (buffer_len(&stderr_buffer) > 0) { | |
388 | packet_start(SSH_SMSG_STDERR_DATA); | |
389 | packet_put_string(buffer_ptr(&stderr_buffer), | |
390 | buffer_len(&stderr_buffer)); | |
391 | packet_send(); | |
392 | /* Update the count of sent bytes. */ | |
393 | stderr_bytes += buffer_len(&stderr_buffer); | |
394 | } | |
395 | /* Wait until all buffered data has been written to the client. */ | |
396 | packet_write_wait(); | |
8efc0c15 | 397 | } |
398 | ||
6ae2364d | 399 | void |
5ca51e19 | 400 | process_buffered_input_packets(void) |
7368a6c8 | 401 | { |
7a37c112 | 402 | dispatch_run(DISPATCH_NONBLOCK, NULL, compat20 ? xxx_kex : NULL); |
7368a6c8 | 403 | } |
404 | ||
5260325f | 405 | /* |
406 | * Performs the interactive session. This handles data transmission between | |
407 | * the client and the program. Note that the notion of stdin, stdout, and | |
408 | * stderr in this function is sort of reversed: this function writes to | |
409 | * stdin (of the child program), and reads from stdout and stderr (of the | |
410 | * child program). | |
411 | */ | |
6ae2364d | 412 | void |
9da5c3c9 | 413 | server_loop(pid_t pid, int fdin_arg, int fdout_arg, int fderr_arg) |
8efc0c15 | 414 | { |
39929cdb | 415 | fd_set *readset = NULL, *writeset = NULL; |
416 | int max_fd; | |
9da5c3c9 | 417 | int wait_status; /* Status returned by wait(). */ |
418 | pid_t wait_pid; /* pid returned by wait(). */ | |
5260325f | 419 | int waiting_termination = 0; /* Have displayed waiting close message. */ |
1e3b8b07 | 420 | u_int max_time_milliseconds; |
421 | u_int previous_stdout_buffer_bytes; | |
422 | u_int stdout_buffer_bytes; | |
5260325f | 423 | int type; |
424 | ||
425 | debug("Entering interactive session."); | |
426 | ||
427 | /* Initialize the SIGCHLD kludge. */ | |
428 | child_pid = pid; | |
429 | child_terminated = 0; | |
430 | signal(SIGCHLD, sigchld_handler); | |
431 | ||
432 | /* Initialize our global variables. */ | |
433 | fdin = fdin_arg; | |
434 | fdout = fdout_arg; | |
435 | fderr = fderr_arg; | |
0e73cc53 | 436 | |
437 | /* nonblocking IO */ | |
438 | set_nonblock(fdin); | |
439 | set_nonblock(fdout); | |
ff873e98 | 440 | /* we don't have stderr for interactive terminal sessions, see below */ |
441 | if (fderr != -1) | |
442 | set_nonblock(fderr); | |
0e73cc53 | 443 | |
e68225b2 | 444 | if (!(datafellows & SSH_BUG_IGNOREMSG) && isatty(fdin)) |
445 | fdin_is_tty = 1; | |
446 | ||
5260325f | 447 | connection_in = packet_get_connection_in(); |
448 | connection_out = packet_get_connection_out(); | |
449 | ||
450 | previous_stdout_buffer_bytes = 0; | |
451 | ||
452 | /* Set approximate I/O buffer size. */ | |
453 | if (packet_is_interactive()) | |
454 | buffer_high = 4096; | |
455 | else | |
456 | buffer_high = 64 * 1024; | |
457 | ||
458 | /* Initialize max_fd to the maximum of the known file descriptors. */ | |
39929cdb | 459 | max_fd = MAX(fdin, fdout); |
460 | if (fderr != -1) | |
461 | max_fd = MAX(max_fd, fderr); | |
462 | max_fd = MAX(max_fd, connection_in); | |
463 | max_fd = MAX(max_fd, connection_out); | |
5260325f | 464 | |
465 | /* Initialize Initialize buffers. */ | |
466 | buffer_init(&stdin_buffer); | |
467 | buffer_init(&stdout_buffer); | |
468 | buffer_init(&stderr_buffer); | |
469 | ||
aa3378df | 470 | /* |
471 | * If we have no separate fderr (which is the case when we have a pty | |
472 | * - there we cannot make difference between data sent to stdout and | |
473 | * stderr), indicate that we have seen an EOF from stderr. This way | |
474 | * we don\'t need to check the descriptor everywhere. | |
475 | */ | |
5260325f | 476 | if (fderr == -1) |
477 | fderr_eof = 1; | |
478 | ||
7368a6c8 | 479 | server_init_dispatch(); |
480 | ||
5260325f | 481 | /* Main loop of the server for the interactive session mode. */ |
482 | for (;;) { | |
5260325f | 483 | |
484 | /* Process buffered packets from the client. */ | |
485 | process_buffered_input_packets(); | |
486 | ||
aa3378df | 487 | /* |
488 | * If we have received eof, and there is no more pending | |
489 | * input data, cause a real eof by closing fdin. | |
490 | */ | |
5260325f | 491 | if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) { |
8efc0c15 | 492 | #ifdef USE_PIPES |
5260325f | 493 | close(fdin); |
8efc0c15 | 494 | #else |
7368a6c8 | 495 | if (fdin != fdout) |
5260325f | 496 | close(fdin); |
497 | else | |
498 | shutdown(fdin, SHUT_WR); /* We will no longer send. */ | |
8efc0c15 | 499 | #endif |
5260325f | 500 | fdin = -1; |
501 | } | |
aa3378df | 502 | /* Make packets from buffered stderr data to send to the client. */ |
5260325f | 503 | make_packets_from_stderr_data(); |
504 | ||
aa3378df | 505 | /* |
506 | * Make packets from buffered stdout data to send to the | |
507 | * client. If there is very little to send, this arranges to | |
508 | * not send them now, but to wait a short while to see if we | |
509 | * are getting more data. This is necessary, as some systems | |
510 | * wake up readers from a pty after each separate character. | |
511 | */ | |
5260325f | 512 | max_time_milliseconds = 0; |
513 | stdout_buffer_bytes = buffer_len(&stdout_buffer); | |
514 | if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 && | |
515 | stdout_buffer_bytes != previous_stdout_buffer_bytes) { | |
516 | /* try again after a while */ | |
517 | max_time_milliseconds = 10; | |
518 | } else { | |
519 | /* Send it now. */ | |
520 | make_packets_from_stdout_data(); | |
521 | } | |
522 | previous_stdout_buffer_bytes = buffer_len(&stdout_buffer); | |
523 | ||
524 | /* Send channel data to the client. */ | |
525 | if (packet_not_very_much_data_to_write()) | |
526 | channel_output_poll(); | |
527 | ||
aa3378df | 528 | /* |
529 | * Bail out of the loop if the program has closed its output | |
530 | * descriptors, and we have no more data to send to the | |
531 | * client, and there is no pending buffered data. | |
532 | */ | |
7062c40f | 533 | if (fdout_eof && fderr_eof && !packet_have_data_to_write() && |
534 | buffer_len(&stdout_buffer) == 0 && buffer_len(&stderr_buffer) == 0) { | |
5260325f | 535 | if (!channel_still_open()) |
7368a6c8 | 536 | break; |
5260325f | 537 | if (!waiting_termination) { |
538 | const char *s = "Waiting for forwarded connections to terminate...\r\n"; | |
539 | char *cp; | |
540 | waiting_termination = 1; | |
541 | buffer_append(&stderr_buffer, s, strlen(s)); | |
542 | ||
543 | /* Display list of open channels. */ | |
544 | cp = channel_open_message(); | |
545 | buffer_append(&stderr_buffer, cp, strlen(cp)); | |
546 | xfree(cp); | |
547 | } | |
548 | } | |
549 | /* Sleep in select() until we can do something. */ | |
39929cdb | 550 | wait_until_can_do_something(&readset, &writeset, &max_fd, |
551 | max_time_milliseconds); | |
5260325f | 552 | |
553 | /* Process any channel events. */ | |
39929cdb | 554 | channel_after_select(readset, writeset); |
5260325f | 555 | |
556 | /* Process input from the client and from program stdout/stderr. */ | |
39929cdb | 557 | process_input(readset); |
5260325f | 558 | |
559 | /* Process output to the client and to program stdin. */ | |
39929cdb | 560 | process_output(writeset); |
8efc0c15 | 561 | } |
39929cdb | 562 | if (readset) |
563 | xfree(readset); | |
564 | if (writeset) | |
565 | xfree(writeset); | |
8efc0c15 | 566 | |
5260325f | 567 | /* Cleanup and termination code. */ |
8efc0c15 | 568 | |
5260325f | 569 | /* Wait until all output has been sent to the client. */ |
570 | drain_output(); | |
8efc0c15 | 571 | |
5260325f | 572 | debug("End of interactive session; stdin %ld, stdout (read %ld, sent %ld), stderr %ld bytes.", |
573 | stdin_bytes, fdout_bytes, stdout_bytes, stderr_bytes); | |
8efc0c15 | 574 | |
5260325f | 575 | /* Free and clear the buffers. */ |
576 | buffer_free(&stdin_buffer); | |
577 | buffer_free(&stdout_buffer); | |
578 | buffer_free(&stderr_buffer); | |
8efc0c15 | 579 | |
5260325f | 580 | /* Close the file descriptors. */ |
581 | if (fdout != -1) | |
582 | close(fdout); | |
583 | fdout = -1; | |
584 | fdout_eof = 1; | |
585 | if (fderr != -1) | |
586 | close(fderr); | |
587 | fderr = -1; | |
588 | fderr_eof = 1; | |
589 | if (fdin != -1) | |
590 | close(fdin); | |
591 | fdin = -1; | |
592 | ||
593 | /* Stop listening for channels; this removes unix domain sockets. */ | |
594 | channel_stop_listening(); | |
595 | ||
596 | /* Wait for the child to exit. Get its exit status. */ | |
597 | wait_pid = wait(&wait_status); | |
1e3b8b07 | 598 | if (wait_pid == -1) { |
5260325f | 599 | /* |
600 | * It is possible that the wait was handled by SIGCHLD | |
601 | * handler. This may result in either: this call | |
602 | * returning with EINTR, or: this call returning ECHILD. | |
603 | */ | |
604 | if (child_terminated) | |
605 | wait_status = child_wait_status; | |
606 | else | |
607 | packet_disconnect("wait: %.100s", strerror(errno)); | |
608 | } else { | |
609 | /* Check if it matches the process we forked. */ | |
610 | if (wait_pid != pid) | |
611 | error("Strange, wait returned pid %d, expected %d", | |
57112b5a | 612 | wait_pid, pid); |
5260325f | 613 | } |
8efc0c15 | 614 | |
5260325f | 615 | /* We no longer want our SIGCHLD handler to be called. */ |
616 | signal(SIGCHLD, SIG_DFL); | |
617 | ||
618 | /* Check if it exited normally. */ | |
619 | if (WIFEXITED(wait_status)) { | |
620 | /* Yes, normal exit. Get exit status and send it to the client. */ | |
621 | debug("Command exited with status %d.", WEXITSTATUS(wait_status)); | |
622 | packet_start(SSH_SMSG_EXITSTATUS); | |
623 | packet_put_int(WEXITSTATUS(wait_status)); | |
624 | packet_send(); | |
625 | packet_write_wait(); | |
626 | ||
aa3378df | 627 | /* |
628 | * Wait for exit confirmation. Note that there might be | |
629 | * other packets coming before it; however, the program has | |
630 | * already died so we just ignore them. The client is | |
631 | * supposed to respond with the confirmation when it receives | |
632 | * the exit status. | |
633 | */ | |
5260325f | 634 | do { |
635 | int plen; | |
636 | type = packet_read(&plen); | |
637 | } | |
638 | while (type != SSH_CMSG_EXIT_CONFIRMATION); | |
639 | ||
640 | debug("Received exit confirmation."); | |
641 | return; | |
642 | } | |
643 | /* Check if the program terminated due to a signal. */ | |
644 | if (WIFSIGNALED(wait_status)) | |
645 | packet_disconnect("Command terminated on signal %d.", | |
646 | WTERMSIG(wait_status)); | |
647 | ||
648 | /* Some weird exit cause. Just exit. */ | |
649 | packet_disconnect("wait returned status %04x.", wait_status); | |
650 | /* NOTREACHED */ | |
651 | } | |
7368a6c8 | 652 | |
6ae2364d | 653 | void |
e78a59f5 | 654 | server_loop2(void) |
655 | { | |
39929cdb | 656 | fd_set *readset = NULL, *writeset = NULL; |
cc94bd38 | 657 | int rekeying = 0, max_fd, status; |
e78a59f5 | 658 | pid_t pid; |
659 | ||
660 | debug("Entering interactive session for SSH2."); | |
661 | ||
1aa00dcb | 662 | mysignal(SIGCHLD, sigchld_handler2); |
e78a59f5 | 663 | child_terminated = 0; |
664 | connection_in = packet_get_connection_in(); | |
665 | connection_out = packet_get_connection_out(); | |
39929cdb | 666 | |
667 | max_fd = MAX(connection_in, connection_out); | |
668 | ||
e78a59f5 | 669 | server_init_dispatch(); |
670 | ||
671 | for (;;) { | |
672 | process_buffered_input_packets(); | |
bbb4cc1b | 673 | |
cd332296 | 674 | rekeying = (xxx_kex != NULL && !xxx_kex->done); |
bbb4cc1b | 675 | |
bbb4cc1b | 676 | if (!rekeying && packet_not_very_much_data_to_write()) |
e78a59f5 | 677 | channel_output_poll(); |
bbb4cc1b | 678 | wait_until_can_do_something(&readset, &writeset, &max_fd, |
679 | rekeying); | |
7062c40f | 680 | if (child_terminated) { |
e78a59f5 | 681 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) |
682 | session_close_by_pid(pid, status); | |
683 | child_terminated = 0; | |
684 | } | |
bbb4cc1b | 685 | if (!rekeying) |
686 | channel_after_select(readset, writeset); | |
39929cdb | 687 | process_input(readset); |
cc94bd38 | 688 | if (connection_closed) |
689 | break; | |
39929cdb | 690 | process_output(writeset); |
e78a59f5 | 691 | } |
39929cdb | 692 | if (readset) |
693 | xfree(readset); | |
694 | if (writeset) | |
695 | xfree(writeset); | |
696 | ||
e78a59f5 | 697 | signal(SIGCHLD, SIG_DFL); |
698 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) | |
699 | session_close_by_pid(pid, status); | |
700 | channel_stop_listening(); | |
701 | } | |
702 | ||
7368a6c8 | 703 | void |
188adeb2 | 704 | server_input_stdin_data(int type, int plen, void *ctxt) |
7368a6c8 | 705 | { |
706 | char *data; | |
1e3b8b07 | 707 | u_int data_len; |
7368a6c8 | 708 | |
709 | /* Stdin data from the client. Append it to the buffer. */ | |
710 | /* Ignore any data if the client has closed stdin. */ | |
711 | if (fdin == -1) | |
712 | return; | |
713 | data = packet_get_string(&data_len); | |
714 | packet_integrity_check(plen, (4 + data_len), type); | |
715 | buffer_append(&stdin_buffer, data, data_len); | |
716 | memset(data, 0, data_len); | |
717 | xfree(data); | |
718 | } | |
719 | ||
720 | void | |
188adeb2 | 721 | server_input_eof(int type, int plen, void *ctxt) |
7368a6c8 | 722 | { |
723 | /* | |
724 | * Eof from the client. The stdin descriptor to the | |
725 | * program will be closed when all buffered data has | |
726 | * drained. | |
727 | */ | |
728 | debug("EOF received for stdin."); | |
729 | packet_integrity_check(plen, 0, type); | |
730 | stdin_eof = 1; | |
731 | } | |
732 | ||
733 | void | |
188adeb2 | 734 | server_input_window_size(int type, int plen, void *ctxt) |
7368a6c8 | 735 | { |
736 | int row = packet_get_int(); | |
737 | int col = packet_get_int(); | |
738 | int xpixel = packet_get_int(); | |
739 | int ypixel = packet_get_int(); | |
740 | ||
741 | debug("Window change received."); | |
742 | packet_integrity_check(plen, 4 * 4, type); | |
743 | if (fdin != -1) | |
744 | pty_change_window_size(fdin, row, col, xpixel, ypixel); | |
745 | } | |
746 | ||
fa08c86b | 747 | Channel * |
748 | server_request_direct_tcpip(char *ctype) | |
e78a59f5 | 749 | { |
fa08c86b | 750 | int sock, newch; |
6ae2364d | 751 | char *target, *originator; |
752 | int target_port, originator_port; | |
e78a59f5 | 753 | |
6ae2364d | 754 | target = packet_get_string(NULL); |
755 | target_port = packet_get_int(); | |
e78a59f5 | 756 | originator = packet_get_string(NULL); |
757 | originator_port = packet_get_int(); | |
6ae2364d | 758 | packet_done(); |
71276795 | 759 | |
fa08c86b | 760 | debug("server_request_direct_tcpip: originator %s port %d, target %s port %d", |
71276795 | 761 | originator, originator_port, target, target_port); |
38c295d6 | 762 | |
e78a59f5 | 763 | /* XXX check permission */ |
6ae2364d | 764 | sock = channel_connect_to(target, target_port); |
765 | xfree(target); | |
e78a59f5 | 766 | xfree(originator); |
767 | if (sock < 0) | |
fa08c86b | 768 | return NULL; |
97fb6912 | 769 | newch = channel_new(ctype, SSH_CHANNEL_CONNECTING, |
bcbf86ec | 770 | sock, sock, -1, CHAN_TCP_WINDOW_DEFAULT, |
a22aff1f | 771 | CHAN_TCP_PACKET_DEFAULT, 0, xstrdup("direct-tcpip"), 1); |
fa08c86b | 772 | return (newch >= 0) ? channel_lookup(newch) : NULL; |
773 | } | |
774 | ||
775 | Channel * | |
776 | server_request_session(char *ctype) | |
777 | { | |
778 | int newch; | |
779 | ||
780 | debug("input_session_request"); | |
781 | packet_done(); | |
782 | /* | |
783 | * A server session has no fd to read or write until a | |
784 | * CHANNEL_REQUEST for a shell is made, so we set the type to | |
785 | * SSH_CHANNEL_LARVAL. Additionally, a callback for handling all | |
786 | * CHANNEL_REQUEST messages is registered. | |
787 | */ | |
788 | newch = channel_new(ctype, SSH_CHANNEL_LARVAL, | |
789 | -1, -1, -1, 0, CHAN_SES_PACKET_DEFAULT, | |
790 | 0, xstrdup("server-session"), 1); | |
791 | if (session_open(newch) == 1) { | |
792 | channel_register_callback(newch, SSH2_MSG_CHANNEL_REQUEST, | |
793 | session_input_channel_req, (void *)0); | |
794 | channel_register_cleanup(newch, session_close_by_channel); | |
795 | return channel_lookup(newch); | |
796 | } else { | |
797 | debug("session open failed, free channel %d", newch); | |
798 | channel_free(newch); | |
799 | } | |
800 | return NULL; | |
e78a59f5 | 801 | } |
802 | ||
6ae2364d | 803 | void |
188adeb2 | 804 | server_input_channel_open(int type, int plen, void *ctxt) |
e78a59f5 | 805 | { |
806 | Channel *c = NULL; | |
807 | char *ctype; | |
1e3b8b07 | 808 | u_int len; |
e78a59f5 | 809 | int rchan; |
810 | int rmaxpack; | |
811 | int rwindow; | |
812 | ||
813 | ctype = packet_get_string(&len); | |
814 | rchan = packet_get_int(); | |
815 | rwindow = packet_get_int(); | |
816 | rmaxpack = packet_get_int(); | |
817 | ||
188adeb2 | 818 | debug("server_input_channel_open: ctype %s rchan %d win %d max %d", |
e78a59f5 | 819 | ctype, rchan, rwindow, rmaxpack); |
820 | ||
821 | if (strcmp(ctype, "session") == 0) { | |
fa08c86b | 822 | c = server_request_session(ctype); |
e78a59f5 | 823 | } else if (strcmp(ctype, "direct-tcpip") == 0) { |
fa08c86b | 824 | c = server_request_direct_tcpip(ctype); |
e78a59f5 | 825 | } |
826 | if (c != NULL) { | |
fa08c86b | 827 | debug("server_input_channel_open: confirm %s", ctype); |
e78a59f5 | 828 | c->remote_id = rchan; |
829 | c->remote_window = rwindow; | |
830 | c->remote_maxpacket = rmaxpack; | |
831 | ||
832 | packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION); | |
833 | packet_put_int(c->remote_id); | |
834 | packet_put_int(c->self); | |
835 | packet_put_int(c->local_window); | |
836 | packet_put_int(c->local_maxpacket); | |
837 | packet_send(); | |
838 | } else { | |
fa08c86b | 839 | debug("server_input_channel_open: failure %s", ctype); |
e78a59f5 | 840 | packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE); |
841 | packet_put_int(rchan); | |
842 | packet_put_int(SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED); | |
843 | packet_put_cstring("bla bla"); | |
844 | packet_put_cstring(""); | |
845 | packet_send(); | |
846 | } | |
847 | xfree(ctype); | |
848 | } | |
849 | ||
2b87da3b | 850 | void |
fa08c86b | 851 | server_input_global_request(int type, int plen, void *ctxt) |
852 | { | |
853 | char *rtype; | |
854 | int want_reply; | |
855 | int success = 0; | |
856 | ||
857 | rtype = packet_get_string(NULL); | |
858 | want_reply = packet_get_char(); | |
859 | debug("server_input_global_request: rtype %s want_reply %d", rtype, want_reply); | |
2b87da3b | 860 | |
01794848 | 861 | /* -R style forwarding */ |
fa08c86b | 862 | if (strcmp(rtype, "tcpip-forward") == 0) { |
863 | struct passwd *pw; | |
864 | char *listen_address; | |
865 | u_short listen_port; | |
866 | ||
867 | pw = auth_get_user(); | |
868 | if (pw == NULL) | |
869 | fatal("server_input_global_request: no user"); | |
870 | listen_address = packet_get_string(NULL); /* XXX currently ignored */ | |
871 | listen_port = (u_short)packet_get_int(); | |
872 | debug("server_input_global_request: tcpip-forward listen %s port %d", | |
873 | listen_address, listen_port); | |
874 | ||
875 | /* check permissions */ | |
876 | if (!options.allow_tcp_forwarding || | |
877 | no_port_forwarding_flag || | |
878 | (listen_port < IPPORT_RESERVED && pw->pw_uid != 0)) { | |
879 | success = 0; | |
880 | packet_send_debug("Server has disabled port forwarding."); | |
881 | } else { | |
882 | /* Start listening on the port */ | |
02a024dd | 883 | success = channel_request_forwarding( |
fa08c86b | 884 | listen_address, listen_port, |
885 | /*unspec host_to_connect*/ "<unspec host>", | |
886 | /*unspec port_to_connect*/ 0, | |
887 | options.gateway_ports, /*remote*/ 1); | |
fa08c86b | 888 | } |
889 | xfree(listen_address); | |
890 | } | |
891 | if (want_reply) { | |
892 | packet_start(success ? | |
893 | SSH2_MSG_REQUEST_SUCCESS : SSH2_MSG_REQUEST_FAILURE); | |
894 | packet_send(); | |
895 | packet_write_wait(); | |
896 | } | |
897 | xfree(rtype); | |
898 | } | |
899 | ||
6ae2364d | 900 | void |
5ca51e19 | 901 | server_init_dispatch_20(void) |
e78a59f5 | 902 | { |
903 | debug("server_init_dispatch_20"); | |
904 | dispatch_init(&dispatch_protocol_error); | |
905 | dispatch_set(SSH2_MSG_CHANNEL_CLOSE, &channel_input_oclose); | |
906 | dispatch_set(SSH2_MSG_CHANNEL_DATA, &channel_input_data); | |
907 | dispatch_set(SSH2_MSG_CHANNEL_EOF, &channel_input_ieof); | |
908 | dispatch_set(SSH2_MSG_CHANNEL_EXTENDED_DATA, &channel_input_extended_data); | |
909 | dispatch_set(SSH2_MSG_CHANNEL_OPEN, &server_input_channel_open); | |
910 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); | |
911 | dispatch_set(SSH2_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); | |
912 | dispatch_set(SSH2_MSG_CHANNEL_REQUEST, &channel_input_channel_request); | |
913 | dispatch_set(SSH2_MSG_CHANNEL_WINDOW_ADJUST, &channel_input_window_adjust); | |
fa08c86b | 914 | dispatch_set(SSH2_MSG_GLOBAL_REQUEST, &server_input_global_request); |
7a37c112 | 915 | |
916 | /* rekeying */ | |
917 | dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit); | |
e78a59f5 | 918 | } |
6ae2364d | 919 | void |
5ca51e19 | 920 | server_init_dispatch_13(void) |
7368a6c8 | 921 | { |
922 | debug("server_init_dispatch_13"); | |
923 | dispatch_init(NULL); | |
924 | dispatch_set(SSH_CMSG_EOF, &server_input_eof); | |
925 | dispatch_set(SSH_CMSG_STDIN_DATA, &server_input_stdin_data); | |
926 | dispatch_set(SSH_CMSG_WINDOW_SIZE, &server_input_window_size); | |
927 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_close); | |
928 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_close_confirmation); | |
929 | dispatch_set(SSH_MSG_CHANNEL_DATA, &channel_input_data); | |
930 | dispatch_set(SSH_MSG_CHANNEL_OPEN_CONFIRMATION, &channel_input_open_confirmation); | |
931 | dispatch_set(SSH_MSG_CHANNEL_OPEN_FAILURE, &channel_input_open_failure); | |
932 | dispatch_set(SSH_MSG_PORT_OPEN, &channel_input_port_open); | |
933 | } | |
6ae2364d | 934 | void |
5ca51e19 | 935 | server_init_dispatch_15(void) |
7368a6c8 | 936 | { |
937 | server_init_dispatch_13(); | |
938 | debug("server_init_dispatch_15"); | |
939 | dispatch_set(SSH_MSG_CHANNEL_CLOSE, &channel_input_ieof); | |
940 | dispatch_set(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION, &channel_input_oclose); | |
941 | } | |
6ae2364d | 942 | void |
5ca51e19 | 943 | server_init_dispatch(void) |
7368a6c8 | 944 | { |
e78a59f5 | 945 | if (compat20) |
946 | server_init_dispatch_20(); | |
947 | else if (compat13) | |
7368a6c8 | 948 | server_init_dispatch_13(); |
949 | else | |
950 | server_init_dispatch_15(); | |
951 | } |