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