summaryrefslogtreecommitdiff
path: root/clientloop.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>2000-08-23 10:46:23 +1000
committerDamien Miller <djm@mindrot.org>2000-08-23 10:46:23 +1000
commitad833b3e65c1887674714d514eb818d862bb499a (patch)
tree16fa75fe42aede072c5d3edac562c2870d1bb0e5 /clientloop.c
parentb078567bf5de1dcf12d265d98acb4dc9eba8b325 (diff)
- (djm) Pick up LOGIN_PROGRAM from environment or PATH if not set by headers
- (djm) OpenBSD CVS updates: - deraadt@cvs.openbsd.org 2000/08/18 20:07:23 [ssh.c] accept remsh as a valid name as well; roman@buildpoint.com - deraadt@cvs.openbsd.org 2000/08/18 20:17:13 [deattack.c crc32.c packet.c] rename crc32() to ssh_crc32() to avoid zlib name clash. do not move to libz crc32 function yet, because it has ugly "long"'s in it; oneill@cs.sfu.ca - deraadt@cvs.openbsd.org 2000/08/18 20:26:08 [scp.1 scp.c] -S prog support; tv@debian.org - deraadt@cvs.openbsd.org 2000/08/18 20:50:07 [scp.c] knf - deraadt@cvs.openbsd.org 2000/08/18 20:57:33 [log-client.c] shorten - markus@cvs.openbsd.org 2000/08/19 12:48:11 [channels.c channels.h clientloop.c ssh.c ssh.h] support for ~. in ssh2 - deraadt@cvs.openbsd.org 2000/08/19 15:29:40 [crc32.h] proper prototype - markus@cvs.openbsd.org 2000/08/19 15:34:44 [authfd.c authfd.h key.c key.h ssh-add.1 ssh-add.c ssh-agent.1] [ssh-agent.c ssh-keygen.c sshconnect1.c sshconnect2.c Makefile] [fingerprint.c fingerprint.h] add SSH2/DSA support to the agent and some other DSA related cleanups. (note that we cannot talk to ssh.com's ssh2 agents) - markus@cvs.openbsd.org 2000/08/19 15:55:52 [channels.c channels.h clientloop.c] more ~ support for ssh2 - markus@cvs.openbsd.org 2000/08/19 16:21:19 [clientloop.c] oops - millert@cvs.openbsd.org 2000/08/20 12:25:53 [session.c] We have to stash the result of get_remote_name_or_ip() before we close our socket or getpeername() will get EBADF and the process will exit. Only a problem for "UseLogin yes". - millert@cvs.openbsd.org 2000/08/20 12:30:59 [session.c] Only check /etc/nologin if "UseLogin no" since login(1) may have its own policy on determining who is allowed to login when /etc/nologin is present. Also use the _PATH_NOLOGIN define. - millert@cvs.openbsd.org 2000/08/20 12:42:43 [auth1.c auth2.c session.c ssh.c] Add calls to setusercontext() and login_get*(). We basically call setusercontext() in most places where previously we did a setlogin(). Add default login.conf file and put root in the "daemon" login class. - millert@cvs.openbsd.org 2000/08/21 10:23:31 [session.c] Fix incorrect PATH setting; noted by Markus.
Diffstat (limited to 'clientloop.c')
-rw-r--r--clientloop.c340
1 files changed, 181 insertions, 159 deletions
diff --git a/clientloop.c b/clientloop.c
index 67fa36d91..d339e1274 100644
--- a/clientloop.c
+++ b/clientloop.c
@@ -16,7 +16,7 @@
16 */ 16 */
17 17
18#include "includes.h" 18#include "includes.h"
19RCSID("$OpenBSD: clientloop.c,v 1.29 2000/07/16 08:27:21 markus Exp $"); 19RCSID("$OpenBSD: clientloop.c,v 1.32 2000/08/19 22:21:19 markus Exp $");
20 20
21#include "xmalloc.h" 21#include "xmalloc.h"
22#include "ssh.h" 22#include "ssh.h"
@@ -29,6 +29,9 @@ RCSID("$OpenBSD: clientloop.c,v 1.29 2000/07/16 08:27:21 markus Exp $");
29#include "channels.h" 29#include "channels.h"
30#include "dispatch.h" 30#include "dispatch.h"
31 31
32#include "buffer.h"
33#include "bufaux.h"
34
32/* Flag indicating that stdin should be redirected from /dev/null. */ 35/* Flag indicating that stdin should be redirected from /dev/null. */
33extern int stdin_null_flag; 36extern int stdin_null_flag;
34 37
@@ -60,6 +63,8 @@ static int in_raw_mode = 0;
60static int in_non_blocking_mode = 0; 63static int in_non_blocking_mode = 0;
61 64
62/* Common data for the client loop code. */ 65/* Common data for the client loop code. */
66static int quit_pending; /* Set to non-zero to quit the client loop. */
67static int escape_char; /* Escape character. */
63static int escape_pending; /* Last character was the escape character */ 68static int escape_pending; /* Last character was the escape character */
64static int last_was_cr; /* Last character was a newline. */ 69static int last_was_cr; /* Last character was a newline. */
65static int exit_status; /* Used to store the exit status of the command. */ 70static int exit_status; /* Used to store the exit status of the command. */
@@ -67,13 +72,11 @@ static int stdin_eof; /* EOF has been encountered on standard error. */
67static Buffer stdin_buffer; /* Buffer for stdin data. */ 72static Buffer stdin_buffer; /* Buffer for stdin data. */
68static Buffer stdout_buffer; /* Buffer for stdout data. */ 73static Buffer stdout_buffer; /* Buffer for stdout data. */
69static Buffer stderr_buffer; /* Buffer for stderr data. */ 74static Buffer stderr_buffer; /* Buffer for stderr data. */
75static unsigned long stdin_bytes, stdout_bytes, stderr_bytes;
70static unsigned int buffer_high;/* Soft max buffer size. */ 76static unsigned int buffer_high;/* Soft max buffer size. */
71static int max_fd; /* Maximum file descriptor number in select(). */ 77static int max_fd; /* Maximum file descriptor number in select(). */
72static int connection_in; /* Connection to server (input). */ 78static int connection_in; /* Connection to server (input). */
73static int connection_out; /* Connection to server (output). */ 79static int connection_out; /* Connection to server (output). */
74static unsigned long stdin_bytes, stdout_bytes, stderr_bytes;
75static int quit_pending; /* Set to non-zero to quit the client loop. */
76static int escape_char; /* Escape character. */
77 80
78 81
79void client_init_dispatch(void); 82void client_init_dispatch(void);
@@ -379,17 +382,15 @@ client_wait_until_can_do_something(fd_set * readset, fd_set * writeset)
379} 382}
380 383
381void 384void
382client_suspend_self() 385client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
383{ 386{
384 struct winsize oldws, newws; 387 struct winsize oldws, newws;
385 388
386 /* Flush stdout and stderr buffers. */ 389 /* Flush stdout and stderr buffers. */
387 if (buffer_len(&stdout_buffer) > 0) 390 if (buffer_len(bout) > 0)
388 atomicio(write, fileno(stdout), buffer_ptr(&stdout_buffer), 391 atomicio(write, fileno(stdout), buffer_ptr(bout), buffer_len(bout));
389 buffer_len(&stdout_buffer)); 392 if (buffer_len(berr) > 0)
390 if (buffer_len(&stderr_buffer) > 0) 393 atomicio(write, fileno(stderr), buffer_ptr(berr), buffer_len(berr));
391 atomicio(write, fileno(stderr), buffer_ptr(&stderr_buffer),
392 buffer_len(&stderr_buffer));
393 394
394 leave_raw_mode(); 395 leave_raw_mode();
395 396
@@ -397,9 +398,9 @@ client_suspend_self()
397 * Free (and clear) the buffer to reduce the amount of data that gets 398 * Free (and clear) the buffer to reduce the amount of data that gets
398 * written to swap. 399 * written to swap.
399 */ 400 */
400 buffer_free(&stdin_buffer); 401 buffer_free(bin);
401 buffer_free(&stdout_buffer); 402 buffer_free(bout);
402 buffer_free(&stderr_buffer); 403 buffer_free(berr);
403 404
404 /* Save old window size. */ 405 /* Save old window size. */
405 ioctl(fileno(stdin), TIOCGWINSZ, &oldws); 406 ioctl(fileno(stdin), TIOCGWINSZ, &oldws);
@@ -416,9 +417,9 @@ client_suspend_self()
416 received_window_change_signal = 1; 417 received_window_change_signal = 1;
417 418
418 /* OK, we have been continued by the user. Reinitialize buffers. */ 419 /* OK, we have been continued by the user. Reinitialize buffers. */
419 buffer_init(&stdin_buffer); 420 buffer_init(bin);
420 buffer_init(&stdout_buffer); 421 buffer_init(bout);
421 buffer_init(&stderr_buffer); 422 buffer_init(berr);
422 423
423 enter_raw_mode(); 424 enter_raw_mode();
424} 425}
@@ -466,12 +467,155 @@ client_process_net_input(fd_set * readset)
466 } 467 }
467} 468}
468 469
470/* process the characters one by one */
471int
472process_escapes(Buffer *bin, Buffer *bout, Buffer *berr, char *buf, int len)
473{
474 char string[1024];
475 pid_t pid;
476 int bytes = 0;
477 unsigned int i;
478 unsigned char ch;
479 char *s;
480
481 for (i = 0; i < len; i++) {
482 /* Get one character at a time. */
483 ch = buf[i];
484
485 if (escape_pending) {
486 /* We have previously seen an escape character. */
487 /* Clear the flag now. */
488 escape_pending = 0;
489
490 /* Process the escaped character. */
491 switch (ch) {
492 case '.':
493 /* Terminate the connection. */
494 snprintf(string, sizeof string, "%c.\r\n", escape_char);
495 buffer_append(berr, string, strlen(string));
496 /*stderr_bytes += strlen(string); XXX*/
497
498 quit_pending = 1;
499 return -1;
500
501 case 'Z' - 64:
502 /* Suspend the program. */
503 /* Print a message to that effect to the user. */
504 snprintf(string, sizeof string, "%c^Z [suspend ssh]\r\n", escape_char);
505 buffer_append(berr, string, strlen(string));
506 /*stderr_bytes += strlen(string); XXX*/
507
508 /* Restore terminal modes and suspend. */
509 client_suspend_self(bin, bout, berr);
510
511 /* We have been continued. */
512 continue;
513
514 case '&':
515 /* XXX does not work yet with proto 2 */
516 if (compat20)
517 continue;
518 /*
519 * Detach the program (continue to serve connections,
520 * but put in background and no more new connections).
521 */
522 if (!stdin_eof) {
523 /*
524 * Sending SSH_CMSG_EOF alone does not always appear
525 * to be enough. So we try to send an EOF character
526 * first.
527 */
528 packet_start(SSH_CMSG_STDIN_DATA);
529 packet_put_string("\004", 1);
530 packet_send();
531 /* Close stdin. */
532 stdin_eof = 1;
533 if (buffer_len(bin) == 0) {
534 packet_start(SSH_CMSG_EOF);
535 packet_send();
536 }
537 }
538 /* Restore tty modes. */
539 leave_raw_mode();
540
541 /* Stop listening for new connections. */
542 channel_stop_listening();
543
544 printf("%c& [backgrounded]\n", escape_char);
545
546 /* Fork into background. */
547 pid = fork();
548 if (pid < 0) {
549 error("fork: %.100s", strerror(errno));
550 continue;
551 }
552 if (pid != 0) { /* This is the parent. */
553 /* The parent just exits. */
554 exit(0);
555 }
556 /* The child continues serving connections. */
557 continue; /*XXX ? */
558
559 case '?':
560 snprintf(string, sizeof string,
561"%c?\r\n\
562Supported escape sequences:\r\n\
563~. - terminate connection\r\n\
564~^Z - suspend ssh\r\n\
565~# - list forwarded connections\r\n\
566~& - background ssh (when waiting for connections to terminate)\r\n\
567~? - this message\r\n\
568~~ - send the escape character by typing it twice\r\n\
569(Note that escapes are only recognized immediately after newline.)\r\n",
570 escape_char);
571 buffer_append(berr, string, strlen(string));
572 continue;
573
574 case '#':
575 snprintf(string, sizeof string, "%c#\r\n", escape_char);
576 buffer_append(berr, string, strlen(string));
577 s = channel_open_message();
578 buffer_append(berr, s, strlen(s));
579 xfree(s);
580 continue;
581
582 default:
583 if (ch != escape_char) {
584 buffer_put_char(bin, escape_char);
585 bytes++;
586 }
587 /* Escaped characters fall through here */
588 break;
589 }
590 } else {
591 /*
592 * The previous character was not an escape char. Check if this
593 * is an escape.
594 */
595 if (last_was_cr && ch == escape_char) {
596 /* It is. Set the flag and continue to next character. */
597 escape_pending = 1;
598 continue;
599 }
600 }
601
602 /*
603 * Normal character. Record whether it was a newline,
604 * and append it to the buffer.
605 */
606 last_was_cr = (ch == '\r' || ch == '\n');
607 buffer_put_char(bin, ch);
608 bytes++;
609 }
610 return bytes;
611}
612
469void 613void
470client_process_input(fd_set * readset) 614client_process_input(fd_set * readset)
471{ 615{
616 int ret;
472 int len; 617 int len;
473 pid_t pid; 618 char buf[8192];
474 char buf[8192], *s;
475 619
476 /* Read input from stdin. */ 620 /* Read input from stdin. */
477 if (FD_ISSET(fileno(stdin), readset)) { 621 if (FD_ISSET(fileno(stdin), readset)) {
@@ -513,145 +657,10 @@ client_process_input(fd_set * readset)
513 * Normal, successful read. But we have an escape character 657 * Normal, successful read. But we have an escape character
514 * and have to process the characters one by one. 658 * and have to process the characters one by one.
515 */ 659 */
516 unsigned int i; 660 ret = process_escapes(&stdin_buffer, &stdout_buffer, &stderr_buffer, buf, len);
517 for (i = 0; i < len; i++) { 661 if (ret == -1)
518 unsigned char ch; 662 return;
519 /* Get one character at a time. */ 663 stdout_bytes += ret;
520 ch = buf[i];
521
522 if (escape_pending) {
523 /* We have previously seen an escape character. */
524 /* Clear the flag now. */
525 escape_pending = 0;
526 /* Process the escaped character. */
527 switch (ch) {
528 case '.':
529 /* Terminate the connection. */
530 snprintf(buf, sizeof buf, "%c.\r\n", escape_char);
531 buffer_append(&stderr_buffer, buf, strlen(buf));
532 stderr_bytes += strlen(buf);
533 quit_pending = 1;
534 return;
535
536 case 'Z' - 64:
537 /* Suspend the program. */
538 /* Print a message to that effect to the user. */
539 snprintf(buf, sizeof buf, "%c^Z\r\n", escape_char);
540 buffer_append(&stderr_buffer, buf, strlen(buf));
541 stderr_bytes += strlen(buf);
542
543 /* Restore terminal modes and suspend. */
544 client_suspend_self();
545
546 /* We have been continued. */
547 continue;
548
549 case '&':
550 /*
551 * Detach the program (continue to serve connections,
552 * but put in background and no more new connections).
553 */
554 if (!stdin_eof) {
555 /*
556 * Sending SSH_CMSG_EOF alone does not always appear
557 * to be enough. So we try to send an EOF character
558 * first.
559 */
560 packet_start(SSH_CMSG_STDIN_DATA);
561 packet_put_string("\004", 1);
562 packet_send();
563 /* Close stdin. */
564 stdin_eof = 1;
565 if (buffer_len(&stdin_buffer) == 0) {
566 packet_start(SSH_CMSG_EOF);
567 packet_send();
568 }
569 }
570 /* Restore tty modes. */
571 leave_raw_mode();
572
573 /* Stop listening for new connections. */
574 channel_stop_listening();
575
576 printf("%c& [backgrounded]\n", escape_char);
577
578 /* Fork into background. */
579 pid = fork();
580 if (pid < 0) {
581 error("fork: %.100s", strerror(errno));
582 continue;
583 }
584 if (pid != 0) { /* This is the parent. */
585 /* The parent just exits. */
586 exit(0);
587 }
588 /* The child continues serving connections. */
589 continue;
590
591 case '?':
592 snprintf(buf, sizeof buf,
593"%c?\r\n\
594Supported escape sequences:\r\n\
595~. - terminate connection\r\n\
596~^Z - suspend ssh\r\n\
597~# - list forwarded connections\r\n\
598~& - background ssh (when waiting for connections to terminate)\r\n\
599~? - this message\r\n\
600~~ - send the escape character by typing it twice\r\n\
601(Note that escapes are only recognized immediately after newline.)\r\n",
602 escape_char);
603 buffer_append(&stderr_buffer, buf, strlen(buf));
604 continue;
605
606 case '#':
607 snprintf(buf, sizeof buf, "%c#\r\n", escape_char);
608 buffer_append(&stderr_buffer, buf, strlen(buf));
609 s = channel_open_message();
610 buffer_append(&stderr_buffer, s, strlen(s));
611 xfree(s);
612 continue;
613
614 default:
615 if (ch != escape_char) {
616 /*
617 * Escape character followed by non-special character.
618 * Append both to the input buffer.
619 */
620 buf[0] = escape_char;
621 buf[1] = ch;
622 buffer_append(&stdin_buffer, buf, 2);
623 stdin_bytes += 2;
624 continue;
625 }
626 /*
627 * Note that escape character typed twice
628 * falls through here; the latter gets processed
629 * as a normal character below.
630 */
631 break;
632 }
633 } else {
634 /*
635 * The previous character was not an escape char. Check if this
636 * is an escape.
637 */
638 if (last_was_cr && ch == escape_char) {
639 /* It is. Set the flag and continue to next character. */
640 escape_pending = 1;
641 continue;
642 }
643 }
644
645 /*
646 * Normal character. Record whether it was a newline,
647 * and append it to the buffer.
648 */
649 last_was_cr = (ch == '\r' || ch == '\n');
650 buf[0] = ch;
651 buffer_append(&stdin_buffer, buf, 1);
652 stdin_bytes += 1;
653 continue;
654 }
655 } 664 }
656 } 665 }
657} 666}
@@ -722,6 +731,15 @@ client_process_buffered_input_packets()
722 dispatch_run(DISPATCH_NONBLOCK, &quit_pending); 731 dispatch_run(DISPATCH_NONBLOCK, &quit_pending);
723} 732}
724 733
734/* scan buf[] for '~' before sending data to the peer */
735
736int
737simple_escape_filter(Channel *c, char *buf, int len)
738{
739 /* XXX we assume c->extended is writeable */
740 return process_escapes(&c->input, &c->output, &c->extended, buf, len);
741}
742
725/* 743/*
726 * Implements the interactive session with the server. This is called after 744 * Implements the interactive session with the server. This is called after
727 * the user has been authenticated, and a command has been started on the 745 * the user has been authenticated, and a command has been started on the
@@ -730,7 +748,7 @@ client_process_buffered_input_packets()
730 */ 748 */
731 749
732int 750int
733client_loop(int have_pty, int escape_char_arg) 751client_loop(int have_pty, int escape_char_arg, int ssh2_chan_id)
734{ 752{
735 extern Options options; 753 extern Options options;
736 double start_time, total_time; 754 double start_time, total_time;
@@ -780,6 +798,9 @@ client_loop(int have_pty, int escape_char_arg)
780 if (!compat20) 798 if (!compat20)
781 client_check_initial_eof_on_stdin(); 799 client_check_initial_eof_on_stdin();
782 800
801 if (compat20 && escape_char != -1)
802 channel_register_filter(ssh2_chan_id, simple_escape_filter);
803
783 /* Main loop of the client for the interactive session mode. */ 804 /* Main loop of the client for the interactive session mode. */
784 while (!quit_pending) { 805 while (!quit_pending) {
785 fd_set readset, writeset; 806 fd_set readset, writeset;
@@ -989,6 +1010,7 @@ client_input_channel_open(int type, int plen)
989 /* XXX move to channels.c */ 1010 /* XXX move to channels.c */
990 sock = x11_connect_display(); 1011 sock = x11_connect_display();
991 if (sock >= 0) { 1012 if (sock >= 0) {
1013/*XXX MAXPACK */
992 id = channel_new("x11", SSH_CHANNEL_X11_OPEN, 1014 id = channel_new("x11", SSH_CHANNEL_X11_OPEN,
993 sock, sock, -1, 4*1024, 32*1024, 0, 1015 sock, sock, -1, 4*1024, 32*1024, 0,
994 xstrdup("x11")); 1016 xstrdup("x11"));