123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- #include "includes.h"
- #include "packet.h"
- #include "buffer.h"
- #include "session.h"
- #include "dbutil.h"
- #include "channel.h"
- #include "ssh.h"
- #include "runopts.h"
- #include "termcodes.h"
- #include "chansession.h"
- #include "agentfwd.h"
- static void cli_closechansess(const struct Channel *channel);
- static int cli_initchansess(struct Channel *channel);
- static void cli_chansessreq(struct Channel *channel);
- static void send_chansess_pty_req(const struct Channel *channel);
- static void send_chansess_shell_req(const struct Channel *channel);
- static void cli_escape_handler(const struct Channel *channel, const unsigned char* buf, int *len);
- static int cli_init_netcat(struct Channel *channel);
- static void cli_tty_setup(void);
- const struct ChanType clichansess = {
- "session",
- cli_initchansess,
- NULL,
- cli_chansessreq,
- cli_closechansess,
- NULL,
- };
- static void cli_chansessreq(struct Channel *channel) {
- char* type = NULL;
- int wantreply;
- TRACE(("enter cli_chansessreq"))
- type = buf_getstring(ses.payload, NULL);
- wantreply = buf_getbool(ses.payload);
- if (strcmp(type, "exit-status") == 0) {
- cli_ses.retval = buf_getint(ses.payload);
- TRACE(("got exit-status of '%d'", cli_ses.retval))
- } else if (strcmp(type, "exit-signal") == 0) {
- TRACE(("got exit-signal, ignoring it"))
- } else {
- TRACE(("unknown request '%s'", type))
- if (wantreply) {
- send_msg_channel_failure(channel);
- }
- goto out;
- }
-
- out:
- m_free(type);
- }
-
- static void cli_closechansess(const struct Channel *UNUSED(channel)) {
- cli_tty_cleanup();
-
- if (ses.chancount > 1) {
- dropbear_log(LOG_INFO, "Waiting for other channels to close...");
- }
- }
- static void cli_tty_setup() {
- struct termios tio;
- TRACE(("enter cli_pty_setup"))
- if (cli_ses.tty_raw_mode == 1) {
- TRACE(("leave cli_tty_setup: already in raw mode!"))
- return;
- }
- if (tcgetattr(STDIN_FILENO, &tio) == -1) {
- dropbear_exit("Failed to set raw TTY mode");
- }
-
- cli_ses.saved_tio = tio;
- tio.c_iflag |= IGNPAR;
- tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
- #ifdef IUCLC
- tio.c_iflag &= ~IUCLC;
- #endif
- tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
- #ifdef IEXTEN
- tio.c_lflag &= ~IEXTEN;
- #endif
- tio.c_oflag &= ~OPOST;
- tio.c_cc[VMIN] = 1;
- tio.c_cc[VTIME] = 0;
- if (tcsetattr(STDIN_FILENO, TCSADRAIN, &tio) == -1) {
- dropbear_exit("Failed to set raw TTY mode");
- }
- cli_ses.tty_raw_mode = 1;
- TRACE(("leave cli_tty_setup"))
- }
- void cli_tty_cleanup() {
- TRACE(("enter cli_tty_cleanup"))
- if (cli_ses.tty_raw_mode == 0) {
- TRACE(("leave cli_tty_cleanup: not in raw mode"))
- return;
- }
- if (tcsetattr(STDIN_FILENO, TCSADRAIN, &cli_ses.saved_tio) == -1) {
- dropbear_log(LOG_WARNING, "Failed restoring TTY");
- } else {
- cli_ses.tty_raw_mode = 0;
- }
- TRACE(("leave cli_tty_cleanup"))
- }
- static void put_termcodes() {
- struct termios tio;
- unsigned int sshcode;
- const struct TermCode *termcode;
- unsigned int value;
- unsigned int mapcode;
- unsigned int bufpos1, bufpos2;
- TRACE(("enter put_termcodes"))
- if (tcgetattr(STDIN_FILENO, &tio) == -1) {
- dropbear_log(LOG_WARNING, "Failed reading termmodes");
- buf_putint(ses.writepayload, 1);
- buf_putbyte(ses.writepayload, 0);
- return;
- }
- bufpos1 = ses.writepayload->pos;
- buf_putint(ses.writepayload, 0);
-
- for (sshcode = 1; sshcode < MAX_TERMCODE; sshcode++) {
- termcode = &termcodes[sshcode];
- mapcode = termcode->mapcode;
- switch (termcode->type) {
- case TERMCODE_NONE:
- continue;
- case TERMCODE_CONTROLCHAR:
- value = tio.c_cc[mapcode];
- break;
- case TERMCODE_INPUT:
- value = tio.c_iflag & mapcode;
- break;
- case TERMCODE_OUTPUT:
- value = tio.c_oflag & mapcode;
- break;
- case TERMCODE_LOCAL:
- value = tio.c_lflag & mapcode;
- break;
- case TERMCODE_CONTROL:
- value = tio.c_cflag & mapcode;
- break;
- default:
- continue;
- }
-
- buf_putbyte(ses.writepayload, sshcode);
- buf_putint(ses.writepayload, value);
- }
- buf_putbyte(ses.writepayload, 0);
-
- bufpos2 = ses.writepayload->pos;
- buf_setpos(ses.writepayload, bufpos1);
- buf_putint(ses.writepayload, bufpos2 - bufpos1 - 4);
- buf_setpos(ses.writepayload, bufpos2);
- TRACE(("leave put_termcodes"))
- }
- static void put_winsize() {
- struct winsize ws;
- if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0) {
-
- ws.ws_row = 25;
- ws.ws_col = 80;
- ws.ws_xpixel = 0;
- ws.ws_ypixel = 0;
- }
- buf_putint(ses.writepayload, ws.ws_col);
- buf_putint(ses.writepayload, ws.ws_row);
- buf_putint(ses.writepayload, ws.ws_xpixel);
- buf_putint(ses.writepayload, ws.ws_ypixel);
- }
- static void sigwinch_handler(int UNUSED(unused)) {
- cli_ses.winchange = 1;
- }
- void cli_chansess_winchange() {
- unsigned int i;
- struct Channel *channel = NULL;
- for (i = 0; i < ses.chansize; i++) {
- channel = ses.channels[i];
- if (channel != NULL && channel->type == &clichansess) {
- CHECKCLEARTOWRITE();
- buf_putbyte(ses.writepayload, SSH_MSG_CHANNEL_REQUEST);
- buf_putint(ses.writepayload, channel->remotechan);
- buf_putstring(ses.writepayload, "window-change", 13);
- buf_putbyte(ses.writepayload, 0);
- put_winsize();
- encrypt_packet();
- }
- }
- cli_ses.winchange = 0;
- }
- static void send_chansess_pty_req(const struct Channel *channel) {
- char* term = NULL;
- TRACE(("enter send_chansess_pty_req"))
- start_send_channel_request(channel, "pty-req");
-
- buf_putbyte(ses.writepayload, 0);
-
- term = getenv("TERM");
- if (term == NULL) {
- term = "vt100";
- }
- buf_putstring(ses.writepayload, term, strlen(term));
-
- put_winsize();
-
- put_termcodes();
- encrypt_packet();
-
- if (signal(SIGWINCH, sigwinch_handler) == SIG_ERR) {
- dropbear_exit("Signal error");
- }
- TRACE(("leave send_chansess_pty_req"))
- }
- static void send_chansess_shell_req(const struct Channel *channel) {
- char* reqtype = NULL;
- TRACE(("enter send_chansess_shell_req"))
- if (cli_opts.cmd) {
- if (cli_opts.is_subsystem) {
- reqtype = "subsystem";
- } else {
- reqtype = "exec";
- }
- } else {
- reqtype = "shell";
- }
- start_send_channel_request(channel, reqtype);
-
- buf_putbyte(ses.writepayload, 0);
- if (cli_opts.cmd) {
- buf_putstring(ses.writepayload, cli_opts.cmd, strlen(cli_opts.cmd));
- }
- encrypt_packet();
- TRACE(("leave send_chansess_shell_req"))
- }
- static int cli_init_stdpipe_sess(struct Channel *channel) {
- channel->writefd = STDOUT_FILENO;
- setnonblocking(STDOUT_FILENO);
- channel->readfd = STDIN_FILENO;
- setnonblocking(STDIN_FILENO);
- channel->errfd = STDERR_FILENO;
- setnonblocking(STDERR_FILENO);
- channel->extrabuf = cbuf_new(opts.recv_window);
- channel->bidir_fd = 0;
- return 0;
- }
- static int cli_init_netcat(struct Channel *channel) {
- return cli_init_stdpipe_sess(channel);
- }
- static int cli_initchansess(struct Channel *channel) {
- cli_init_stdpipe_sess(channel);
- #if DROPBEAR_CLI_AGENTFWD
- if (cli_opts.agent_fwd) {
- cli_setup_agent(channel);
- }
- #endif
- if (cli_opts.wantpty) {
- send_chansess_pty_req(channel);
- channel->prio = DROPBEAR_PRIO_LOWDELAY;
- }
- send_chansess_shell_req(channel);
- if (cli_opts.wantpty) {
- cli_tty_setup();
- channel->read_mangler = cli_escape_handler;
- cli_ses.last_char = '\r';
- }
- return 0;
- }
- #if DROPBEAR_CLI_NETCAT
- static const struct ChanType cli_chan_netcat = {
- "direct-tcpip",
- cli_init_netcat,
- NULL,
- NULL,
- cli_closechansess,
- NULL,
- };
- void cli_send_netcat_request() {
- const char* source_host = "127.0.0.1";
- const int source_port = 22;
- TRACE(("enter cli_send_netcat_request"))
- cli_opts.wantpty = 0;
- if (send_msg_channel_open_init(STDIN_FILENO, &cli_chan_netcat)
- == DROPBEAR_FAILURE) {
- dropbear_exit("Couldn't open initial channel");
- }
- buf_putstring(ses.writepayload, cli_opts.netcat_host,
- strlen(cli_opts.netcat_host));
- buf_putint(ses.writepayload, cli_opts.netcat_port);
-
- buf_putstring(ses.writepayload, source_host, strlen(source_host));
- buf_putint(ses.writepayload, source_port);
- encrypt_packet();
- TRACE(("leave cli_send_netcat_request"))
- }
- #endif
- void cli_send_chansess_request() {
- TRACE(("enter cli_send_chansess_request"))
- if (send_msg_channel_open_init(STDIN_FILENO, &clichansess)
- == DROPBEAR_FAILURE) {
- dropbear_exit("Couldn't open initial channel");
- }
-
- encrypt_packet();
- TRACE(("leave cli_send_chansess_request"))
- }
- static int
- do_escape(unsigned char c) {
- switch (c) {
- case '.':
- dropbear_exit("Terminated");
- return 1;
- case 0x1a:
-
- cli_tty_cleanup();
- kill(getpid(), SIGTSTP);
-
- cli_tty_setup();
- cli_ses.winchange = 1;
- return 1;
- default:
- return 0;
- }
- }
- static
- void cli_escape_handler(const struct Channel* UNUSED(channel), const unsigned char* buf, int *len) {
- char c;
- int skip_char = 0;
-
- if (*len != 1) {
- cli_ses.last_char = 0x0;
- return;
- }
- c = buf[0];
- if (cli_ses.last_char == DROPBEAR_ESCAPE_CHAR) {
- skip_char = do_escape(c);
- cli_ses.last_char = 0x0;
- } else {
- if (c == DROPBEAR_ESCAPE_CHAR) {
- if (cli_ses.last_char == '\r') {
- cli_ses.last_char = DROPBEAR_ESCAPE_CHAR;
- skip_char = 1;
- } else {
- cli_ses.last_char = 0x0;
- }
- } else {
- cli_ses.last_char = c;
- }
- }
- if (skip_char) {
- *len = 0;
- }
- }
|