]> andersk Git - openssh.git/blobdiff - channels.c
- (djm) OpenBSD CVS updates:
[openssh.git] / channels.c
index ccc7e5c0b5466aa7923b73df99e6b3a46c9ff2cd..ea395293a6b68f83ee23a9d7f83919c77d062515 100644 (file)
@@ -1,29 +1,28 @@
 /*
- * 
+ *
  * channels.c
- * 
+ *
  * Author: Tatu Ylonen <ylo@cs.hut.fi>
- * 
+ *
  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  *                    All rights reserved
- * 
+ *
  * Created: Fri Mar 24 16:35:24 1995 ylo
- * 
+ *
  * This file contains functions for generic socket connection forwarding.
  * There is also code for initiating connection forwarding for X11 connections,
  * arbitrary tcp/ip connections, and the authentication agent connection.
- * 
+ *
  * SSH2 support added by Markus Friedl.
  */
 
 #include "includes.h"
-RCSID("$Id$");
+RCSID("$OpenBSD: channels.c,v 1.64 2000/07/16 08:27:21 markus Exp $");
 
 #include "ssh.h"
 #include "packet.h"
 #include "xmalloc.h"
 #include "buffer.h"
-#include "authfd.h"
 #include "uidswap.h"
 #include "readconf.h"
 #include "servconf.h"
@@ -34,15 +33,22 @@ RCSID("$Id$");
 
 #include "ssh2.h"
 
+#include <openssl/rsa.h>
+#include <openssl/dsa.h>
+#include "key.h"
+#include "authfd.h"
+
 /* Maximum number of fake X11 displays to try. */
 #define MAX_DISPLAYS  1000
 
 /* Max len of agent socket */
 #define MAX_SOCKET_NAME 100
 
-/* default buffer for tcp-fwd-channel */
-#define CHAN_WINDOW_DEFAULT      (8*1024)
-#define CHAN_PACKET_DEFAULT     (CHAN_WINDOW_DEFAULT/2)
+/* default window/packet sizes for tcp/x11-fwd-channel */
+#define CHAN_TCP_WINDOW_DEFAULT        (8*1024)
+#define CHAN_TCP_PACKET_DEFAULT        (CHAN_TCP_WINDOW_DEFAULT/2)
+#define CHAN_X11_WINDOW_DEFAULT        (4*1024)
+#define CHAN_X11_PACKET_DEFAULT        (CHAN_X11_WINDOW_DEFAULT/2)
 
 /*
  * Pointer to an array containing all allocated channels.  The array is
@@ -109,7 +115,7 @@ static int have_hostname_in_open = 0;
 
 /* Sets specific protocol options. */
 
-void 
+void
 channel_set_options(int hostname_in_open)
 {
        have_hostname_in_open = hostname_in_open;
@@ -121,7 +127,7 @@ channel_set_options(int hostname_in_open)
  * and the server has no way to know but to trust the client anyway.
  */
 
-void 
+void
 channel_permit_all_opens()
 {
        all_opens_permitted = 1;
@@ -133,7 +139,7 @@ Channel *
 channel_lookup(int id)
 {
        Channel *c;
-       if (id < 0 && id > channels_alloc) {
+       if (id < 0 || id > channels_alloc) {
                log("channel_lookup: %d: bad id", id);
                return NULL;
        }
@@ -146,17 +152,13 @@ channel_lookup(int id)
 }
 
 /*
- * Allocate a new channel object and set its type and socket. This will cause
- * remote_name to be freed.
+ * Register filedescriptors for a channel, used when allocating a channel or
+ * when the channel consumer/producer is ready, e.g. shell exec'd
  */
 
-int 
-channel_new(char *ctype, int type, int rfd, int wfd, int efd,
-    int window, int maxpack, int extended_usage, char *remote_name)
+void
+channel_register_fds(Channel *c, int rfd, int wfd, int efd, int extusage)
 {
-       int i, found;
-       Channel *c;
-
        /* Update the maximum file descriptor value. */
        if (rfd > channel_max_fd_value)
                channel_max_fd_value = rfd;
@@ -166,6 +168,31 @@ channel_new(char *ctype, int type, int rfd, int wfd, int efd,
                channel_max_fd_value = efd;
        /* XXX set close-on-exec -markus */
 
+       c->rfd = rfd;
+       c->wfd = wfd;
+       c->sock = (rfd == wfd) ? rfd : -1;
+       c->efd = efd;
+       c->extended_usage = extusage;
+       if (rfd != -1)
+               set_nonblock(rfd);
+       if (wfd != -1)
+               set_nonblock(wfd);
+       if (efd != -1)
+               set_nonblock(efd);
+}
+
+/*
+ * Allocate a new channel object and set its type and socket. This will cause
+ * remote_name to be freed.
+ */
+
+int
+channel_new(char *ctype, int type, int rfd, int wfd, int efd,
+    int window, int maxpack, int extusage, char *remote_name)
+{
+       int i, found;
+       Channel *c;
+
        /* Do initial allocation if this is the first call. */
        if (channels_alloc == 0) {
                chan_init();
@@ -201,6 +228,7 @@ channel_new(char *ctype, int type, int rfd, int wfd, int efd,
        buffer_init(&c->output);
        buffer_init(&c->extended);
        chan_init_iostates(c);
+       channel_register_fds(c, rfd, wfd, efd, extusage);
        c->self = found;
        c->type = type;
        c->ctype = ctype;
@@ -208,13 +236,6 @@ channel_new(char *ctype, int type, int rfd, int wfd, int efd,
        c->local_window_max = window;
        c->local_consumed = 0;
        c->local_maxpacket = maxpack;
-       c->remote_window = 0;
-       c->remote_maxpacket = 0;
-       c->rfd = rfd;
-       c->wfd = wfd;
-       c->sock = (rfd == wfd) ? rfd : -1;
-       c->efd = efd;
-       c->extended_usage = extended_usage;
        c->remote_id = -1;
        c->remote_name = remote_name;
        c->remote_window = 0;
@@ -226,15 +247,40 @@ channel_new(char *ctype, int type, int rfd, int wfd, int efd,
        debug("channel %d: new [%s]", found, remote_name);
        return found;
 }
-int 
+/* old interface XXX */
+int
 channel_allocate(int type, int sock, char *remote_name)
 {
        return channel_new("", type, sock, sock, -1, 0, 0, 0, remote_name);
 }
 
-/* Free the channel and close its socket. */
 
-void 
+/* Close all channel fd/socket. */
+
+void
+channel_close_fds(Channel *c)
+{
+       if (c->sock != -1) {
+               close(c->sock);
+               c->sock = -1;
+       }
+       if (c->rfd != -1) {
+               close(c->rfd);
+               c->rfd = -1;
+       }
+       if (c->wfd != -1) {
+               close(c->wfd);
+               c->wfd = -1;
+       }
+       if (c->efd != -1) {
+               close(c->efd);
+               c->efd = -1;
+       }
+}
+
+/* Free the channel and close its fd/socket. */
+
+void
 channel_free(int id)
 {
        Channel *c = channel_lookup(id);
@@ -245,25 +291,9 @@ channel_free(int id)
                debug("channel_free: channel %d: dettaching channel user", id);
                c->dettach_user(c->self, NULL);
        }
-       if (c->sock != -1) {
+       if (c->sock != -1)
                shutdown(c->sock, SHUT_RDWR);
-               close(c->sock);
-               c->sock = -1;
-       }
-       if (compat20) {
-               if (c->rfd != -1) {
-                       close(c->rfd);
-                       c->rfd = -1;
-               }
-               if (c->wfd != -1) {
-                       close(c->wfd);
-                       c->wfd = -1;
-               }
-               if (c->efd != -1) {
-                       close(c->efd);
-                       c->efd = -1;
-               }
-       }
+       channel_close_fds(c);
        buffer_free(&c->input);
        buffer_free(&c->output);
        buffer_free(&c->extended);
@@ -361,7 +391,7 @@ channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
 {
        if (buffer_len(&c->output) == 0)
                channel_free(c->self);
-       else 
+       else
                FD_SET(c->sock, writeset);
 }
 
@@ -371,6 +401,7 @@ channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
  * state until the first packet has been completely read.  The authentication
  * data in that packet is then substituted by the real data if it matches the
  * fake data, and the channel is put into normal mode.
+ * XXX All this happens at the client side.
  */
 int
 x11_open_helper(Channel *c)
@@ -437,6 +468,7 @@ channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
        if (ret == 1) {
                /* Start normal processing for the channel. */
                c->type = SSH_CHANNEL_OPEN;
+               channel_pre_open_13(c, readset, writeset);
        } else if (ret == -1) {
                /*
                 * We have received an X11 connection that has bad
@@ -455,14 +487,18 @@ channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
 }
 
 void
-channel_pre_x11_open_15(Channel *c, fd_set * readset, fd_set * writeset)
+channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
 {
        int ret = x11_open_helper(c);
        if (ret == 1) {
                c->type = SSH_CHANNEL_OPEN;
+               if (compat20)
+                       channel_pre_open_20(c, readset, writeset);
+               else
+                       channel_pre_open_15(c, readset, writeset);
        } else if (ret == -1) {
                debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
-               chan_read_failed(c);
+               chan_read_failed(c);    /** force close? */
                chan_write_failed(c);
                debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
        }
@@ -476,6 +512,7 @@ channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
        int newsock, newch;
        socklen_t addrlen;
        char buf[16384], *remote_hostname;
+       int remote_port;
 
        if (FD_ISSET(c->sock, readset)) {
                debug("X11 connection requested.");
@@ -486,16 +523,36 @@ channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
                        return;
                }
                remote_hostname = get_remote_hostname(newsock);
+               remote_port = get_peer_port(newsock);
                snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
-               remote_hostname, get_peer_port(newsock));
+                   remote_hostname, remote_port);
+
+               newch = channel_new("x11",
+                   SSH_CHANNEL_OPENING, newsock, newsock, -1,
+                   c->local_window_max, c->local_maxpacket,
+                   0, xstrdup(buf));
+               if (compat20) {
+                       packet_start(SSH2_MSG_CHANNEL_OPEN);
+                       packet_put_cstring("x11");
+                       packet_put_int(newch);
+                       packet_put_int(c->local_window_max);
+                       packet_put_int(c->local_maxpacket);
+                       /* originator host and port */
+                       packet_put_cstring(remote_hostname);
+                       if (datafellows & SSH_BUG_X11FWD) {
+                               debug("ssh2 x11 bug compat mode");
+                       } else {
+                               packet_put_int(remote_port);
+                       }
+                       packet_send();
+               } else {
+                       packet_start(SSH_SMSG_X11_OPEN);
+                       packet_put_int(newch);
+                       if (have_hostname_in_open)
+                               packet_put_string(buf, strlen(buf));
+                       packet_send();
+               }
                xfree(remote_hostname);
-               newch = channel_allocate(SSH_CHANNEL_OPENING, newsock,
-                                        xstrdup(buf));
-               packet_start(SSH_SMSG_X11_OPEN);
-               packet_put_int(newch);
-               if (have_hostname_in_open)
-                       packet_put_string(buf, strlen(buf));
-               packet_send();
        }
 }
 
@@ -538,8 +595,10 @@ channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
                        packet_put_int(newch);
                        packet_put_int(c->local_window_max);
                        packet_put_int(c->local_maxpacket);
+                       /* target host and port */
                        packet_put_string(c->path, strlen(c->path));
                        packet_put_int(c->host_port);
+                       /* originator host and port */
                        packet_put_cstring(remote_hostname);
                        packet_put_int(remote_port);
                        packet_send();
@@ -592,8 +651,10 @@ channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
        if (c->rfd != -1 &&
            FD_ISSET(c->rfd, readset)) {
                len = read(c->rfd, buf, sizeof(buf));
+               if (len < 0 && (errno == EINTR || errno == EAGAIN))
+                       return 1;
                if (len <= 0) {
-                       debug("channel %d: read<0 rfd %d len %d",
+                       debug("channel %d: read<=0 rfd %d len %d",
                            c->self, c->rfd, len);
                        if (compat13) {
                                buffer_consume(&c->output, buffer_len(&c->output));
@@ -618,7 +679,9 @@ channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
            FD_ISSET(c->wfd, writeset) &&
            buffer_len(&c->output) > 0) {
                len = write(c->wfd, buffer_ptr(&c->output),
-                           buffer_len(&c->output));
+                   buffer_len(&c->output));
+               if (len < 0 && (errno == EINTR || errno == EAGAIN))
+                       return 1;
                if (len <= 0) {
                        if (compat13) {
                                buffer_consume(&c->output, buffer_len(&c->output));
@@ -725,10 +788,13 @@ void
 channel_handler_init_20(void)
 {
        channel_pre[SSH_CHANNEL_OPEN] =                 &channel_pre_open_20;
+       channel_pre[SSH_CHANNEL_X11_OPEN] =             &channel_pre_x11_open;
        channel_pre[SSH_CHANNEL_PORT_LISTENER] =        &channel_pre_listener;
+       channel_pre[SSH_CHANNEL_X11_LISTENER] =         &channel_pre_listener;
 
        channel_post[SSH_CHANNEL_OPEN] =                &channel_post_open_2;
        channel_post[SSH_CHANNEL_PORT_LISTENER] =       &channel_post_port_listener;
+       channel_post[SSH_CHANNEL_X11_LISTENER] =        &channel_post_x11_listener;
 }
 
 void
@@ -753,7 +819,7 @@ void
 channel_handler_init_15(void)
 {
        channel_pre[SSH_CHANNEL_OPEN] =                 &channel_pre_open_15;
-       channel_pre[SSH_CHANNEL_X11_OPEN] =             &channel_pre_x11_open_15;
+       channel_pre[SSH_CHANNEL_X11_OPEN] =             &channel_pre_x11_open;
        channel_pre[SSH_CHANNEL_X11_LISTENER] =         &channel_pre_listener;
        channel_pre[SSH_CHANNEL_PORT_LISTENER] =        &channel_pre_listener;
        channel_pre[SSH_CHANNEL_AUTH_SOCKET] =          &channel_pre_listener;
@@ -780,7 +846,7 @@ channel_handler_init(void)
                channel_handler_init_15();
 }
 
-void 
+void
 channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
 {
        static int did_init = 0;
@@ -802,13 +868,13 @@ channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
        }
 }
 
-void 
+void
 channel_prepare_select(fd_set * readset, fd_set * writeset)
 {
        channel_handler(channel_pre, readset, writeset);
 }
 
-void 
+void
 channel_after_select(fd_set * readset, fd_set * writeset)
 {
        channel_handler(channel_post, readset, writeset);
@@ -816,7 +882,7 @@ channel_after_select(fd_set * readset, fd_set * writeset)
 
 /* If there is data to send to the connection, send some of it now. */
 
-void 
+void
 channel_output_poll()
 {
        int len, i;
@@ -870,7 +936,6 @@ channel_output_poll()
                                packet_send();
                                buffer_consume(&c->input, len);
                                c->remote_window -= len;
-                               debug("channel %d: send data len %d", c->self, len);
                        }
                } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
                        if (compat13)
@@ -907,7 +972,7 @@ channel_output_poll()
  * still there.
  */
 
-void 
+void
 channel_input_data(int type, int plen)
 {
        int id;
@@ -932,6 +997,7 @@ channel_input_data(int type, int plen)
 
        /* Get the data. */
        data = packet_get_string(&data_len);
+       packet_done();
 
        if (compat20){
                if (data_len > c->local_maxpacket) {
@@ -951,7 +1017,7 @@ channel_input_data(int type, int plen)
        buffer_append(&c->output, data, data_len);
        xfree(data);
 }
-void 
+void
 channel_input_extended_data(int type, int plen)
 {
        int id;
@@ -978,6 +1044,7 @@ channel_input_extended_data(int type, int plen)
                return;
        }
        data = packet_get_string(&data_len);
+       packet_done();
        if (data_len > c->local_window) {
                log("channel %d: rcvd too much extended_data %d, win %d",
                    c->self, data_len, c->local_window);
@@ -996,7 +1063,7 @@ channel_input_extended_data(int type, int plen)
  * more channel is overfull.
  */
 
-int 
+int
 channel_not_very_much_buffered_data()
 {
        unsigned int i;
@@ -1020,7 +1087,7 @@ channel_not_very_much_buffered_data()
        return 1;
 }
 
-void 
+void
 channel_input_ieof(int type, int plen)
 {
        int id;
@@ -1035,7 +1102,7 @@ channel_input_ieof(int type, int plen)
        chan_rcvd_ieof(c);
 }
 
-void 
+void
 channel_input_close(int type, int plen)
 {
        int id;
@@ -1074,7 +1141,7 @@ channel_input_close(int type, int plen)
 }
 
 /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
-void 
+void
 channel_input_oclose(int type, int plen)
 {
        int id = packet_get_int();
@@ -1085,12 +1152,13 @@ channel_input_oclose(int type, int plen)
        chan_rcvd_oclose(c);
 }
 
-void 
+void
 channel_input_close_confirmation(int type, int plen)
 {
        int id = packet_get_int();
        Channel *c = channel_lookup(id);
 
+       packet_done();
        if (c == NULL)
                packet_disconnect("Received close confirmation for "
                    "out-of-range channel %d.", id);
@@ -1100,7 +1168,7 @@ channel_input_close_confirmation(int type, int plen)
        channel_free(c->self);
 }
 
-void 
+void
 channel_input_open_confirmation(int type, int plen)
 {
        int id, remote_id;
@@ -1123,6 +1191,7 @@ channel_input_open_confirmation(int type, int plen)
        if (compat20) {
                c->remote_window = packet_get_int();
                c->remote_maxpacket = packet_get_int();
+               packet_done();
                if (c->cb_fn != NULL && c->cb_event == type) {
                        debug("callback start");
                        c->cb_fn(c->self, c->cb_arg);
@@ -1133,7 +1202,7 @@ channel_input_open_confirmation(int type, int plen)
        }
 }
 
-void 
+void
 channel_input_open_failure(int type, int plen)
 {
        int id;
@@ -1151,8 +1220,11 @@ channel_input_open_failure(int type, int plen)
        if (compat20) {
                int reason = packet_get_int();
                char *msg  = packet_get_string(NULL);
+               char *lang  = packet_get_string(NULL);
                log("channel_open_failure: %d: reason %d: %s", id, reason, msg);
+               packet_done();
                xfree(msg);
+               xfree(lang);
        }
        /* Free the channel.  This will also close the socket. */
        channel_free(id);
@@ -1183,7 +1255,7 @@ debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
        }
 }
 
-void 
+void
 channel_input_window_adjust(int type, int plen)
 {
        Channel *c;
@@ -1202,6 +1274,7 @@ channel_input_window_adjust(int type, int plen)
                return;
        }
        adjust = packet_get_int();
+       packet_done();
        debug("channel %d: rcvd adjust %d", id, adjust);
        c->remote_window += adjust;
 }
@@ -1211,7 +1284,7 @@ channel_input_window_adjust(int type, int plen)
  * might have.
  */
 
-void 
+void
 channel_stop_listening()
 {
        int i;
@@ -1234,23 +1307,22 @@ channel_stop_listening()
 }
 
 /*
- * Closes the sockets of all channels.  This is used to close extra file
+ * Closes the sockets/fds of all channels.  This is used to close extra file
  * descriptors after a fork.
  */
 
-void 
+void
 channel_close_all()
 {
        int i;
-       for (i = 0; i < channels_alloc; i++) {
+       for (i = 0; i < channels_alloc; i++)
                if (channels[i].type != SSH_CHANNEL_FREE)
-                       close(channels[i].sock);
-       }
+                       channel_close_fds(&channels[i]);
 }
 
 /* Returns the maximum file descriptor number used by the channels. */
 
-int 
+int
 channel_max_fd()
 {
        return channel_max_fd_value;
@@ -1258,7 +1330,7 @@ channel_max_fd()
 
 /* Returns true if any channel is still open. */
 
-int 
+int
 channel_still_open()
 {
        unsigned int i;
@@ -1345,7 +1417,7 @@ channel_open_message()
  * channel to host:port from remote side.
  */
 
-void 
+void
 channel_request_local_forwarding(u_short port, const char *host,
                                 u_short host_port, int gateway_ports)
 {
@@ -1416,7 +1488,7 @@ channel_request_local_forwarding(u_short port, const char *host,
                ch = channel_new(
                    "port listener", SSH_CHANNEL_PORT_LISTENER,
                    sock, sock, -1,
-                   CHAN_WINDOW_DEFAULT, CHAN_PACKET_DEFAULT,
+                   CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
                    0, xstrdup("port listener"));
                strlcpy(channels[ch].path, host, sizeof(channels[ch].path));
                channels[ch].host_port = host_port;
@@ -1433,7 +1505,7 @@ channel_request_local_forwarding(u_short port, const char *host,
  * the secure channel to host:port from local side.
  */
 
-void 
+void
 channel_request_remote_forwarding(u_short listen_port, const char *host_to_connect,
                                  u_short port_to_connect)
 {
@@ -1457,9 +1529,9 @@ channel_request_remote_forwarding(u_short listen_port, const char *host_to_conne
                packet_put_int(listen_port);
        } else {
                packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
-               packet_put_int(port_to_connect);
-               packet_put_cstring(host_to_connect);
                packet_put_int(listen_port);
+               packet_put_cstring(host_to_connect);
+               packet_put_int(port_to_connect);
                packet_send();
                packet_write_wait();
                /*
@@ -1476,8 +1548,8 @@ channel_request_remote_forwarding(u_short listen_port, const char *host_to_conne
  * message if there was an error).  This never returns if there was an error.
  */
 
-void 
-channel_input_port_forward_request(int is_root)
+void
+channel_input_port_forward_request(int is_root, int gateway_ports)
 {
        u_short port, host_port;
        char *hostname;
@@ -1496,9 +1568,8 @@ channel_input_port_forward_request(int is_root)
                                  port);
        /*
         * Initiate forwarding,
-        * bind port to localhost only (gateway ports == 0).
         */
-       channel_request_local_forwarding(port, hostname, host_port, 0);
+       channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
 
        /* Free the argument string. */
        xfree(hostname);
@@ -1560,7 +1631,7 @@ channel_connect_to(const char *host, u_short host_port)
  * or CHANNEL_OPEN_FAILURE.
  */
 
-void 
+void
 channel_input_port_open(int type, int plen)
 {
        u_short host_port;
@@ -1757,8 +1828,10 @@ x11_create_display_inet(int screen_number, int x11_display_offset)
        /* Allocate a channel for each socket. */
        for (n = 0; n < num_socks; n++) {
                sock = socks[n];
-               (void) channel_allocate(SSH_CHANNEL_X11_LISTENER, sock,
-                                       xstrdup("X11 inet listener"));
+               (void) channel_new("x11 listener",
+                   SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
+                   CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
+                   0, xstrdup("X11 inet listener"));
        }
 
        /* Return a suitable value for the DISPLAY environment variable. */
@@ -1798,44 +1871,21 @@ connect_local_xsocket(unsigned int dnr)
        return -1;
 }
 
-
-/*
- * This is called when SSH_SMSG_X11_OPEN is received.  The packet contains
- * the remote channel number.  We should do whatever we want, and respond
- * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
- */
-
-void 
-x11_input_open(int type, int plen)
+int
+x11_connect_display(void)
 {
-       int remote_channel, display_number, sock = 0, newch;
+       int display_number, sock = 0;
        const char *display;
-       char buf[1024], *cp, *remote_host;
-       unsigned int remote_len;
+       char buf[1024], *cp;
        struct addrinfo hints, *ai, *aitop;
        char strport[NI_MAXSERV];
        int gaierr;
 
-       /* Get remote channel number. */
-       remote_channel = packet_get_int();
-
-       /* Get remote originator name. */
-       if (have_hostname_in_open) {
-               remote_host = packet_get_string(&remote_len);
-               remote_len += 4;
-       } else {
-               remote_host = xstrdup("unknown (remote did not supply name)");
-               remote_len = 0;
-       }
-
-       debug("Received X11 open request.");
-       packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN);
-
        /* Try to open a socket for the local X server. */
        display = getenv("DISPLAY");
        if (!display) {
                error("DISPLAY not set.");
-               goto fail;
+               return -1;
        }
        /*
         * Now we decode the value of the DISPLAY variable and make a
@@ -1852,15 +1902,15 @@ x11_input_open(int type, int plen)
                if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
                        error("Could not parse display number from DISPLAY: %.100s",
                              display);
-                       goto fail;
+                       return -1;
                }
                /* Create a socket. */
                sock = connect_local_xsocket(display_number);
                if (sock < 0)
-                       goto fail;
+                       return -1;
 
                /* OK, we now have a connection to the display. */
-               goto success;
+               return sock;
        }
        /*
         * Connect to an inet socket.  The DISPLAY value is supposedly
@@ -1871,14 +1921,14 @@ x11_input_open(int type, int plen)
        cp = strchr(buf, ':');
        if (!cp) {
                error("Could not find ':' in DISPLAY: %.100s", display);
-               goto fail;
+               return -1;
        }
        *cp = 0;
        /* buf now contains the host name.  But first we parse the display number. */
        if (sscanf(cp + 1, "%d", &display_number) != 1) {
                error("Could not parse display number from DISPLAY: %.100s",
                      display);
-               goto fail;
+               return -1;
        }
 
        /* Look up the host address */
@@ -1888,7 +1938,7 @@ x11_input_open(int type, int plen)
        snprintf(strport, sizeof strport, "%d", 6000 + display_number);
        if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
                error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
-               goto fail;
+               return -1;
        }
        for (ai = aitop; ai; ai = ai->ai_next) {
                /* Create a socket. */
@@ -1909,33 +1959,62 @@ x11_input_open(int type, int plen)
        }
        freeaddrinfo(aitop);
        if (!ai) {
-               error("connect %.100s port %d: %.100s", buf, 6000 + display_number, 
+               error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
                    strerror(errno));
-               goto fail;
+               return -1;
        }
-success:
-       /* We have successfully obtained a connection to the real X display. */
+       return sock;
+}
 
-       /* Allocate a channel for this connection. */
-       if (x11_saved_proto == NULL)
-               newch = channel_allocate(SSH_CHANNEL_OPEN, sock, remote_host);
-       else
-               newch = channel_allocate(SSH_CHANNEL_X11_OPEN, sock, remote_host);
-       channels[newch].remote_id = remote_channel;
+/*
+ * This is called when SSH_SMSG_X11_OPEN is received.  The packet contains
+ * the remote channel number.  We should do whatever we want, and respond
+ * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
+ */
 
-       /* Send a confirmation to the remote host. */
-       packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
-       packet_put_int(remote_channel);
-       packet_put_int(newch);
-       packet_send();
+void
+x11_input_open(int type, int plen)
+{
+       int remote_channel, sock = 0, newch;
+       char *remote_host;
+       unsigned int remote_len;
 
-       return;
+       /* Get remote channel number. */
+       remote_channel = packet_get_int();
 
-fail:
-       /* Send refusal to the remote host. */
-       packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
-       packet_put_int(remote_channel);
-       packet_send();
+       /* Get remote originator name. */
+       if (have_hostname_in_open) {
+               remote_host = packet_get_string(&remote_len);
+               remote_len += 4;
+       } else {
+               remote_host = xstrdup("unknown (remote did not supply name)");
+               remote_len = 0;
+       }
+
+       debug("Received X11 open request.");
+       packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN);
+
+       /* Obtain a connection to the real X display. */
+       sock = x11_connect_display();
+       if (sock == -1) {
+               /* Send refusal to the remote host. */
+               packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
+               packet_put_int(remote_channel);
+               packet_send();
+       } else {
+               /* Allocate a channel for this connection. */
+               newch = channel_allocate(
+                    (x11_saved_proto == NULL) ?
+                    SSH_CHANNEL_OPEN : SSH_CHANNEL_X11_OPEN,
+                    sock, remote_host);
+               channels[newch].remote_id = remote_channel;
+
+               /* Send a confirmation to the remote host. */
+               packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
+               packet_put_int(remote_channel);
+               packet_put_int(newch);
+               packet_send();
+       }
 }
 
 /*
@@ -1943,8 +2022,9 @@ fail:
  * data, and enables authentication spoofing.
  */
 
-void 
-x11_request_forwarding_with_spoofing(const char *proto, const char *data)
+void
+x11_request_forwarding_with_spoofing(int client_session_id,
+    const char *proto, const char *data)
 {
        unsigned int data_len = (unsigned int) strlen(data) / 2;
        unsigned int i, value;
@@ -1990,9 +2070,14 @@ x11_request_forwarding_with_spoofing(const char *proto, const char *data)
                sprintf(new_data + 2 * i, "%02x", (unsigned char) x11_fake_data[i]);
 
        /* Send the request packet. */
-       packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
-       packet_put_string(proto, strlen(proto));
-       packet_put_string(new_data, strlen(new_data));
+       if (compat20) {
+               channel_request_start(client_session_id, "x11-req", 0);
+               packet_put_char(0);     /* XXX bool single connection */
+       } else {
+               packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
+       }
+       packet_put_cstring(proto);
+       packet_put_cstring(new_data);
        packet_put_int(screen_number);
        packet_send();
        packet_write_wait();
@@ -2001,7 +2086,7 @@ x11_request_forwarding_with_spoofing(const char *proto, const char *data)
 
 /* Sends a message to the server to request authentication fd forwarding. */
 
-void 
+void
 auth_request_forwarding()
 {
        packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
@@ -2023,7 +2108,7 @@ auth_get_socket_name()
 
 /* removes the agent forwarding socket */
 
-void 
+void
 cleanup_socket(void)
 {
        remove(channel_forwarded_auth_socket_name);
@@ -2031,11 +2116,11 @@ cleanup_socket(void)
 }
 
 /*
- * This if called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
+ * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
  * This starts forwarding authentication requests.
  */
 
-void 
+int
 auth_input_request_forwarding(struct passwd * pw)
 {
        int sock, newch;
@@ -2053,8 +2138,16 @@ auth_input_request_forwarding(struct passwd * pw)
        strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
 
        /* Create private directory for socket */
-       if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL)
-               packet_disconnect("mkdtemp: %.100s", strerror(errno));
+       if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL) {
+               packet_send_debug("Agent forwarding disabled: mkdtemp() failed: %.100s",
+                   strerror(errno));
+               restore_uid();
+               xfree(channel_forwarded_auth_socket_name);
+               xfree(channel_forwarded_auth_socket_dir);
+               channel_forwarded_auth_socket_name = NULL;
+               channel_forwarded_auth_socket_dir = NULL;
+               return 0;
+       }
        snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
                 channel_forwarded_auth_socket_dir, (int) getpid());
 
@@ -2089,11 +2182,12 @@ auth_input_request_forwarding(struct passwd * pw)
                                 xstrdup("auth socket"));
        strlcpy(channels[newch].path, channel_forwarded_auth_socket_name,
            sizeof(channels[newch].path));
+       return 1;
 }
 
 /* This is called to process an SSH_SMSG_AGENT_OPEN message. */
 
-void 
+void
 auth_input_open_request(int type, int plen)
 {
        int remch, sock, newch;
@@ -2142,20 +2236,26 @@ auth_input_open_request(int type, int plen)
 }
 
 void
-channel_open(int id)
+channel_start_open(int id)
 {
        Channel *c = channel_lookup(id);
        if (c == NULL) {
                log("channel_open: %d: bad id", id);
                return;
        }
+       debug("send channel open %d", id);
        packet_start(SSH2_MSG_CHANNEL_OPEN);
        packet_put_cstring(c->ctype);
        packet_put_int(c->self);
        packet_put_int(c->local_window);
        packet_put_int(c->local_maxpacket);
+}
+void
+channel_open(int id)
+{
+       /* XXX REMOVE ME */
+       channel_start_open(id);
        packet_send();
-       debug("channel open %d", id);
 }
 void
 channel_request(int id, char *service, int wantconfirm)
@@ -2216,17 +2316,9 @@ channel_set_fds(int id, int rfd, int wfd, int efd, int extusage)
        Channel *c = channel_lookup(id);
        if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
                fatal("channel_activate for non-larval channel %d.", id);
-       if (rfd > channel_max_fd_value)
-               channel_max_fd_value = rfd;
-       if (wfd > channel_max_fd_value)
-               channel_max_fd_value = wfd;
-       if (efd > channel_max_fd_value)
-               channel_max_fd_value = efd;
+
+       channel_register_fds(c, rfd, wfd, efd, extusage);
        c->type = SSH_CHANNEL_OPEN;
-       c->rfd = rfd;
-       c->wfd = wfd;
-       c->efd = efd;
-       c->extended_usage = extusage;
        /* XXX window size? */
        c->local_window = c->local_window_max = c->local_maxpacket/2;
        packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
This page took 0.077236 seconds and 4 git commands to generate.