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