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