]> andersk Git - gssapi-openssh.git/blobdiff - openssh/clientloop.c
merged OpenSSH 3.9p1 to trunk
[gssapi-openssh.git] / openssh / clientloop.c
index 3089bcf9cf784689537a23a5248b6c9d2b37030e..afd68064fab9e160a78bfd5f210d1e0134537bfc 100644 (file)
@@ -1,3 +1,4 @@
+/* $OpenBSD: clientloop.c,v 1.188 2008/02/22 20:44:02 dtucker Exp $ */
 /*
  * Author: Tatu Ylonen <ylo@cs.hut.fi>
  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: clientloop.c,v 1.149 2005/12/30 15:56:37 reyk Exp $");
 
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/param.h>
+#ifdef HAVE_SYS_STAT_H
+# include <sys/stat.h>
+#endif
+#ifdef HAVE_SYS_TIME_H
+# include <sys/time.h>
+#endif
+#include <sys/socket.h>
+
+#include <ctype.h>
+#include <errno.h>
+#ifdef HAVE_PATHS_H
+#include <paths.h>
+#endif
+#include <signal.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <termios.h>
+#include <pwd.h>
+#include <unistd.h>
+
+#include "xmalloc.h"
 #include "ssh.h"
 #include "ssh1.h"
 #include "ssh2.h"
-#include "xmalloc.h"
 #include "packet.h"
 #include "buffer.h"
 #include "compat.h"
 #include "channels.h"
 #include "dispatch.h"
-#include "buffer.h"
-#include "bufaux.h"
 #include "key.h"
+#include "cipher.h"
 #include "kex.h"
 #include "log.h"
 #include "readconf.h"
@@ -118,7 +142,7 @@ static volatile sig_atomic_t received_signal = 0;
 static int in_non_blocking_mode = 0;
 
 /* Common data for the client loop code. */
-static int quit_pending;       /* Set to non-zero to quit the client loop. */
+static volatile sig_atomic_t quit_pending; /* Set non-zero to quit the loop. */
 static int escape_char;                /* Escape character. */
 static int escape_pending;     /* Last character was the escape character */
 static int last_was_cr;                /* Last character was a newline. */
@@ -133,7 +157,6 @@ static int connection_in;   /* Connection to server (input). */
 static int connection_out;     /* Connection to server (output). */
 static int need_rekeying;      /* Set to non-zero if rekeying is requested. */
 static int session_closed = 0; /* In SSH2: login session closed. */
-static int server_alive_timeouts = 0;
 
 static void client_init_dispatch(void);
 int    session_ident = -1;
@@ -178,7 +201,7 @@ enter_non_blocking(void)
  * Signal handler for the window change signal (SIGWINCH).  This just sets a
  * flag indicating that the window has changed.
  */
-
+/*ARGSUSED */
 static void
 window_change_handler(int sig)
 {
@@ -190,7 +213,7 @@ window_change_handler(int sig)
  * Signal handler for signals that cause the program to terminate.  These
  * signals must be trapped to restore terminal modes.
  */
-
+/*ARGSUSED */
 static void
 signal_handler(int sig)
 {
@@ -266,19 +289,29 @@ client_x11_get_proto(const char *display, const char *xauth_path,
                                        generated = 1;
                        }
                }
-               snprintf(cmd, sizeof(cmd),
-                   "%s %s%s list %s 2>" _PATH_DEVNULL,
-                   xauth_path,
-                   generated ? "-f " : "" ,
-                   generated ? xauthfile : "",
-                   display);
-               debug2("x11_get_proto: %s", cmd);
-               f = popen(cmd, "r");
-               if (f && fgets(line, sizeof(line), f) &&
-                   sscanf(line, "%*s %511s %511s", proto, data) == 2)
-                       got_data = 1;
-               if (f)
-                       pclose(f);
+
+               /*
+                * When in untrusted mode, we read the cookie only if it was
+                * successfully generated as an untrusted one in the step
+                * above.
+                */
+               if (trusted || generated) {
+                       snprintf(cmd, sizeof(cmd),
+                           "%s %s%s list %s 2>" _PATH_DEVNULL,
+                           xauth_path,
+                           generated ? "-f " : "" ,
+                           generated ? xauthfile : "",
+                           display);
+                       debug2("x11_get_proto: %s", cmd);
+                       f = popen(cmd, "r");
+                       if (f && fgets(line, sizeof(line), f) &&
+                           sscanf(line, "%*s %511s %511s", proto, data) == 2)
+                               got_data = 1;
+                       if (f)
+                               pclose(f);
+               } else
+                       error("Warning: untrusted X11 forwarding setup failed: "
+                           "xauth key data not generated");
        }
 
        if (do_unlink) {
@@ -422,10 +455,10 @@ client_check_window_change(void)
                if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) < 0)
                        return;
                packet_start(SSH_CMSG_WINDOW_SIZE);
-               packet_put_int(ws.ws_row);
-               packet_put_int(ws.ws_col);
-               packet_put_int(ws.ws_xpixel);
-               packet_put_int(ws.ws_ypixel);
+               packet_put_int((u_int)ws.ws_row);
+               packet_put_int((u_int)ws.ws_col);
+               packet_put_int((u_int)ws.ws_xpixel);
+               packet_put_int((u_int)ws.ws_ypixel);
                packet_send();
        }
 }
@@ -433,15 +466,17 @@ client_check_window_change(void)
 static void
 client_global_request_reply(int type, u_int32_t seq, void *ctxt)
 {
-       server_alive_timeouts = 0;
+       keep_alive_timeouts = 0;
        client_global_request_reply_fwd(type, seq, ctxt);
 }
 
 static void
 server_alive_check(void)
 {
-       if (++server_alive_timeouts > options.server_alive_count_max)
-               packet_disconnect("Timeout, server not responding.");
+       if (++keep_alive_timeouts > options.server_alive_count_max) {
+               logit("Timeout, server not responding.");
+               cleanup_exit(255);
+       }
        packet_start(SSH2_MSG_GLOBAL_REQUEST);
        packet_put_cstring("keepalive@openssh.com");
        packet_put_char(1);     /* boolean: want reply */
@@ -569,7 +604,7 @@ client_suspend_self(Buffer *bin, Buffer *bout, Buffer *berr)
 }
 
 static void
-client_process_net_input(fd_set * readset)
+client_process_net_input(fd_set *readset)
 {
        int len;
        char buf[8192];
@@ -677,22 +712,18 @@ client_extra_session2_setup(int id, void *arg)
 }
 
 static void
-client_process_control(fd_set * readset)
+client_process_control(fd_set *readset)
 {
        Buffer m;
        Channel *c;
-       int client_fd, new_fd[3], ver, allowed;
+       int client_fd, new_fd[3], ver, allowed, window, packetmax;
        socklen_t addrlen;
        struct sockaddr_storage addr;
        struct confirm_ctx *cctx;
        char *cmd;
-       u_int i, len, env_len, command, flags;
+       u_int i, j, len, env_len, command, flags;
        uid_t euid;
        gid_t egid;
-       int listen_port = 0;
-       int connect_port = 0;
-       char * listen_host = NULL;
-       char * connect_host = NULL;
 
        /*
         * Accept connection on control socket
@@ -741,13 +772,6 @@ client_process_control(fd_set * readset)
        command = buffer_get_int(&m);
        flags = buffer_get_int(&m);
 
-       if (SSHMUX_FLAG_PORTFORWARD & flags)
-       {
-               listen_host = buffer_get_string(&m,NULL);
-               listen_port = buffer_get_int(&m);
-               connect_host = buffer_get_string(&m,NULL);
-               connect_port = buffer_get_int(&m);
-       }
        buffer_clear(&m);
 
        switch (command) {
@@ -787,31 +811,6 @@ client_process_control(fd_set * readset)
                return;
        }
 
-       if (allowed && (SSHMUX_FLAG_PORTFORWARD & flags) && listen_host && connect_host)
-       {
-               int ret;
-               Forward * fwd;
-
-               fwd = &options.local_forwards[options.num_local_forwards++];
-               fwd->listen_host = xstrdup(listen_host);
-               fwd->listen_port = listen_port;
-               fwd->connect_host = xstrdup(connect_host);
-               fwd->connect_port = connect_port;
-               ret = channel_setup_local_fwd_listener(
-                       options.local_forwards[options.num_local_forwards-1].listen_host,
-                       options.local_forwards[options.num_local_forwards-1].listen_port,
-                       options.local_forwards[options.num_local_forwards-1].connect_host,
-                       options.local_forwards[options.num_local_forwards-1].connect_port,
-                        options.gateway_ports, options.hpn_disabled, options.hpn_buffer_size);
-
-        }
-
-       
-       if (listen_host)
-               xfree(listen_host);
-       if (connect_host)
-               xfree(connect_host);
-
        /* Reply for SSHMUX_COMMAND_OPEN */
        buffer_clear(&m);
        buffer_put_int(&m, allowed);
@@ -844,8 +843,7 @@ client_process_control(fd_set * readset)
                return;
        }
 
-       cctx = xmalloc(sizeof(*cctx));
-       memset(cctx, 0, sizeof(*cctx));
+       cctx = xcalloc(1, sizeof(*cctx));
        cctx->want_tty = (flags & SSHMUX_FLAG_TTY) != 0;
        cctx->want_subsys = (flags & SSHMUX_FLAG_SUBSYS) != 0;
        cctx->want_x_fwd = (flags & SSHMUX_FLAG_X11_FWD) != 0;
@@ -860,7 +858,7 @@ client_process_control(fd_set * readset)
        env_len = MIN(env_len, 4096);
        debug3("%s: receiving %d env vars", __func__, env_len);
        if (env_len != 0) {
-               cctx->env = xmalloc(sizeof(*cctx->env) * (env_len + 1));
+               cctx->env = xcalloc(env_len + 1, sizeof(*cctx->env));
                for (i = 0; i < env_len; i++)
                        cctx->env[i] = buffer_get_string(&m, &len);
                cctx->env[i] = NULL;
@@ -868,11 +866,26 @@ client_process_control(fd_set * readset)
 
        debug2("%s: accepted tty %d, subsys %d, cmd %s", __func__,
            cctx->want_tty, cctx->want_subsys, cmd);
+       xfree(cmd);
 
        /* Gather fds from client */
-       new_fd[0] = mm_receive_fd(client_fd);
-       new_fd[1] = mm_receive_fd(client_fd);
-       new_fd[2] = mm_receive_fd(client_fd);
+       for(i = 0; i < 3; i++) {
+               if ((new_fd[i] = mm_receive_fd(client_fd)) == -1) {
+                       error("%s: failed to receive fd %d from slave",
+                           __func__, i);
+                       for (j = 0; j < i; j++)
+                               close(new_fd[j]);
+                       for (j = 0; j < env_len; j++)
+                               xfree(cctx->env[j]);
+                       if (env_len > 0)
+                               xfree(cctx->env);
+                       xfree(cctx->term);
+                       buffer_free(&cctx->cmd);
+                       close(client_fd);
+                       xfree(cctx);
+                       return;
+               }
+       }
 
        debug2("%s: got fds stdin %d, stdout %d, stderr %d", __func__,
            new_fd[0], new_fd[1], new_fd[2]);
@@ -911,15 +924,20 @@ client_process_control(fd_set * readset)
        set_nonblock(client_fd);
 
        if (options.hpn_disabled) 
-               c = channel_new("session", SSH_CHANNEL_OPENING,
-                   new_fd[0], new_fd[1], new_fd[2],
-                   CHAN_SES_WINDOW_DEFAULT, CHAN_SES_PACKET_DEFAULT,
-                   CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
-       else 
-               c = channel_new("session", SSH_CHANNEL_OPENING,
-                   new_fd[0], new_fd[1], new_fd[2],
-                   options.hpn_buffer_size, CHAN_SES_PACKET_DEFAULT,
-                   CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
+         window = CHAN_SES_WINDOW_DEFAULT;
+       else
+         window = options.hpn_buffer_size;
+
+       packetmax = CHAN_SES_PACKET_DEFAULT;
+       if (cctx->want_tty) {
+               window >>= 1;
+               packetmax >>= 1;
+       }
+       
+       c = channel_new("session", SSH_CHANNEL_OPENING,
+           new_fd[0], new_fd[1], new_fd[2], window, packetmax,
+           CHAN_EXTENDED_WRITE, "client-session", /*nonblock*/0);
+
        /* XXX */
        c->ctl_fd = client_fd;
 
@@ -939,12 +957,15 @@ process_cmdline(void)
        u_short cancel_port;
        Forward fwd;
 
+       bzero(&fwd, sizeof(fwd));
+       fwd.listen_host = fwd.connect_host = NULL;
+
        leave_raw_mode();
        handler = signal(SIGINT, SIG_IGN);
        cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
        if (s == NULL)
                goto out;
-       while (*s && isspace(*s))
+       while (isspace(*s))
                s++;
        if (*s == '-')
                s++;    /* Skip cmdline '-', if any */
@@ -953,12 +974,16 @@ process_cmdline(void)
 
        if (*s == 'h' || *s == 'H' || *s == '?') {
                logit("Commands:");
-               logit("      -Lport:host:hostport    Request local forward");
-               logit("      -Rport:host:hostport    Request remote forward");
-               logit("      -KRhostport             Cancel remote forward");
+               logit("      -L[bind_address:]port:host:hostport    "
+                   "Request local forward");
+               logit("      -R[bind_address:]port:host:hostport    "
+                   "Request remote forward");
+               logit("      -KR[bind_address:]port                 "
+                   "Cancel remote forward");
                if (!options.permit_local_command)
                        goto out;
-               logit("      !args                   Execute local command");
+               logit("      !args                                  "
+                   "Execute local command");
                goto out;
        }
 
@@ -987,9 +1012,8 @@ process_cmdline(void)
                goto out;
        }
 
-       s++;
-       while (*s && isspace(*s))
-               s++;
+       while (isspace(*++s))
+               ;
 
        if (delete) {
                cancel_port = 0;
@@ -1020,9 +1044,12 @@ process_cmdline(void)
                                goto out;
                        }
                } else {
-                       channel_request_remote_forwarding(fwd.listen_host,
+                       if (channel_request_remote_forwarding(fwd.listen_host,
                            fwd.listen_port, fwd.connect_host,
-                           fwd.connect_port);
+                           fwd.connect_port) < 0) {
+                               logit("Port forwarding failed.");
+                               goto out;
+                       }
                }
 
                logit("Forwarding port.");
@@ -1033,6 +1060,10 @@ out:
        enter_raw_mode();
        if (cmd)
                xfree(cmd);
+       if (fwd.listen_host != NULL)
+               xfree(fwd.listen_host);
+       if (fwd.connect_host != NULL)
+               xfree(fwd.connect_host);
 }
 
 /* process the characters one by one */
@@ -1214,7 +1245,7 @@ Supported escape sequences:\r\n\
 }
 
 static void
-client_process_input(fd_set * readset)
+client_process_input(fd_set *readset)
 {
        int len;
        char buf[8192];
@@ -1267,7 +1298,7 @@ client_process_input(fd_set * readset)
 }
 
 static void
-client_process_output(fd_set * writeset)
+client_process_output(fd_set *writeset)
 {
        int len;
        char buf[100];
@@ -1712,10 +1743,10 @@ client_request_forwarded_tcpip(const char *request_type, int rchan)
                return NULL;
        }
        if (options.hpn_disabled) 
-               c = channel_new("forwarded-tcpip",
-                   SSH_CHANNEL_CONNECTING, sock, sock, -1,
-                   CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
-                   originator_address, 1);
+       c = channel_new("forwarded-tcpip",
+           SSH_CHANNEL_CONNECTING, sock, sock, -1,
+           CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
+           originator_address, 1);
        else
                c = channel_new("forwarded-tcpip",
                    SSH_CHANNEL_CONNECTING, sock, sock, -1,
@@ -1754,10 +1785,11 @@ client_request_x11(const char *request_type, int rchan)
        sock = x11_connect_display();
        if (sock < 0)
                return NULL;
+       /* again is this really necessary for X11? */
        if (options.hpn_disabled) 
-               c = channel_new("x11",
-                   SSH_CHANNEL_X11_OPEN, sock, sock, -1,
-                   CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
+       c = channel_new("x11",
+           SSH_CHANNEL_X11_OPEN, sock, sock, -1,
+           CHAN_TCP_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT, 0, "x11", 1);
        else 
                c = channel_new("x11",
                    SSH_CHANNEL_X11_OPEN, sock, sock, -1,
@@ -1777,14 +1809,15 @@ client_request_agent(const char *request_type, int rchan)
                error("Warning: this is probably a break-in attempt by a malicious server.");
                return NULL;
        }
-       sock =  ssh_get_authentication_socket();
+       sock = ssh_get_authentication_socket();
        if (sock < 0)
                return NULL;
+       /* not sure this is really needed here either */
        if (options.hpn_disabled) 
-               c = channel_new("authentication agent connection",
-                   SSH_CHANNEL_OPEN, sock, sock, -1,
-                   CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_WINDOW_DEFAULT, 0,
-                   "authentication agent connection", 1);
+       c = channel_new("authentication agent connection",
+           SSH_CHANNEL_OPEN, sock, sock, -1,
+           CHAN_X11_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0,
+           "authentication agent connection", 1);
        else
                c = channel_new("authentication agent connection",
                    SSH_CHANNEL_OPEN, sock, sock, -1,
@@ -1794,6 +1827,56 @@ client_request_agent(const char *request_type, int rchan)
        return c;
 }
 
+int
+client_request_tun_fwd(int tun_mode, int local_tun, int remote_tun)
+{
+       Channel *c;
+       int fd;
+
+       if (tun_mode == SSH_TUNMODE_NO)
+               return 0;
+
+       if (!compat20) {
+               error("Tunnel forwarding is not support for protocol 1");
+               return -1;
+       }
+
+       debug("Requesting tun unit %d in mode %d", local_tun, tun_mode);
+
+       /* Open local tunnel device */
+       if ((fd = tun_open(local_tun, tun_mode)) == -1) {
+               error("Tunnel device open failed.");
+               return -1;
+       }
+
+       if(options.hpn_disabled)
+       c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
+                               CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
+                               0, "tun", 1);
+       else
+       c = channel_new("tun", SSH_CHANNEL_OPENING, fd, fd, -1,
+                               options.hpn_buffer_size, CHAN_TCP_PACKET_DEFAULT,
+                               0, "tun", 1);
+       c->datagram = 1;
+
+#if defined(SSH_TUN_FILTER)
+       if (options.tun_open == SSH_TUNMODE_POINTOPOINT)
+               channel_register_filter(c->self, sys_tun_infilter,
+                   sys_tun_outfilter);
+#endif
+
+       packet_start(SSH2_MSG_CHANNEL_OPEN);
+       packet_put_cstring("tun@openssh.com");
+       packet_put_int(c->self);
+       packet_put_int(c->local_window_max);
+       packet_put_int(c->local_maxpacket);
+       packet_put_int(tun_mode);
+       packet_put_int(remote_tun);
+       packet_send();
+
+       return 0;
+}
+
 /* XXXX move to generic input handler */
 static void
 client_input_channel_open(int type, u_int32_t seq, void *ctxt)
@@ -1928,10 +2011,10 @@ client_session2_setup(int id, int want_tty, int want_subsystem,
 
                channel_request_start(id, "pty-req", 0);
                packet_put_cstring(term != NULL ? term : "");
-               packet_put_int(ws.ws_col);
-               packet_put_int(ws.ws_row);
-               packet_put_int(ws.ws_xpixel);
-               packet_put_int(ws.ws_ypixel);
+               packet_put_int((u_int)ws.ws_col);
+               packet_put_int((u_int)ws.ws_row);
+               packet_put_int((u_int)ws.ws_xpixel);
+               packet_put_int((u_int)ws.ws_ypixel);
                tio = get_saved_tio();
                tty_make_modes(-1, tiop != NULL ? tiop : &tio);
                packet_send();
This page took 0.245302 seconds and 4 git commands to generate.