]> andersk Git - openssh.git/blobdiff - channels.c
- djm@cvs.openbsd.org 2010/01/30 02:54:53
[openssh.git] / channels.c
index 2319afd4b949254eccfefd8ab2796cd22d6ce24e..81261679a8f56b0815b3e70c95b2e57e06cf329b 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: channels.c,v 1.292 2009/01/14 01:38:06 djm Exp $ */
+/* $OpenBSD: channels.c,v 1.302 2010/01/26 01:28:35 djm Exp $ */
 /*
  * Author: Tatu Ylonen <ylo@cs.hut.fi>
  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -53,6 +53,7 @@
 #include <arpa/inet.h>
 
 #include <errno.h>
+#include <fcntl.h>
 #include <netdb.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -228,12 +229,16 @@ channel_register_fds(Channel *c, int rfd, int wfd, int efd,
        channel_max_fd = MAX(channel_max_fd, wfd);
        channel_max_fd = MAX(channel_max_fd, efd);
 
-       /* XXX set close-on-exec -markus */
+       if (rfd != -1)
+               fcntl(rfd, F_SETFD, FD_CLOEXEC);
+       if (wfd != -1 && wfd != rfd)
+               fcntl(wfd, F_SETFD, FD_CLOEXEC);
+       if (efd != -1 && efd != rfd && efd != wfd)
+               fcntl(efd, F_SETFD, FD_CLOEXEC);
 
        c->rfd = rfd;
        c->wfd = wfd;
        c->sock = (rfd == wfd) ? rfd : -1;
-       c->ctl_fd = -1; /* XXX: set elsewhere */
        c->efd = efd;
        c->extended_usage = extusage;
 
@@ -296,6 +301,7 @@ channel_new(char *ctype, int type, int rfd, int wfd, int efd,
        buffer_init(&c->input);
        buffer_init(&c->output);
        buffer_init(&c->extended);
+       c->path = NULL;
        c->ostate = CHAN_OUTPUT_OPEN;
        c->istate = CHAN_INPUT_OPEN;
        c->flags = 0;
@@ -321,6 +327,10 @@ channel_new(char *ctype, int type, int rfd, int wfd, int efd,
        c->output_filter = NULL;
        c->filter_ctx = NULL;
        c->filter_cleanup = NULL;
+       c->ctl_chan = -1;
+       c->mux_rcb = NULL;
+       c->mux_ctx = NULL;
+       c->delayed = 1;         /* prevent call to channel_post handler */
        TAILQ_INIT(&c->status_confirms);
        debug("channel %d: new [%s]", found, remote_name);
        return c;
@@ -362,11 +372,10 @@ channel_close_fd(int *fdp)
 static void
 channel_close_fds(Channel *c)
 {
-       debug3("channel %d: close_fds r %d w %d e %d c %d",
-           c->self, c->rfd, c->wfd, c->efd, c->ctl_fd);
+       debug3("channel %d: close_fds r %d w %d e %d",
+           c->self, c->rfd, c->wfd, c->efd);
 
        channel_close_fd(&c->sock);
-       channel_close_fd(&c->ctl_fd);
        channel_close_fd(&c->rfd);
        channel_close_fd(&c->wfd);
        channel_close_fd(&c->efd);
@@ -392,8 +401,6 @@ channel_free(Channel *c)
 
        if (c->sock != -1)
                shutdown(c->sock, SHUT_RDWR);
-       if (c->ctl_fd != -1)
-               shutdown(c->ctl_fd, SHUT_RDWR);
        channel_close_fds(c);
        buffer_free(&c->input);
        buffer_free(&c->output);
@@ -402,6 +409,10 @@ channel_free(Channel *c)
                xfree(c->remote_name);
                c->remote_name = NULL;
        }
+       if (c->path) {
+               xfree(c->path);
+               c->path = NULL;
+       }
        while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
                if (cc->abandon_cb != NULL)
                        cc->abandon_cb(c, cc->ctx);
@@ -511,6 +522,7 @@ channel_still_open(void)
                case SSH_CHANNEL_X11_LISTENER:
                case SSH_CHANNEL_PORT_LISTENER:
                case SSH_CHANNEL_RPORT_LISTENER:
+               case SSH_CHANNEL_MUX_LISTENER:
                case SSH_CHANNEL_CLOSED:
                case SSH_CHANNEL_AUTH_SOCKET:
                case SSH_CHANNEL_DYNAMIC:
@@ -524,6 +536,7 @@ channel_still_open(void)
                case SSH_CHANNEL_OPENING:
                case SSH_CHANNEL_OPEN:
                case SSH_CHANNEL_X11_OPEN:
+               case SSH_CHANNEL_MUX_CLIENT:
                        return 1;
                case SSH_CHANNEL_INPUT_DRAINING:
                case SSH_CHANNEL_OUTPUT_DRAINING:
@@ -555,6 +568,8 @@ channel_find_open(void)
                case SSH_CHANNEL_X11_LISTENER:
                case SSH_CHANNEL_PORT_LISTENER:
                case SSH_CHANNEL_RPORT_LISTENER:
+               case SSH_CHANNEL_MUX_LISTENER:
+               case SSH_CHANNEL_MUX_CLIENT:
                case SSH_CHANNEL_OPENING:
                case SSH_CHANNEL_CONNECTING:
                case SSH_CHANNEL_ZOMBIE:
@@ -605,6 +620,8 @@ channel_open_message(void)
                case SSH_CHANNEL_CLOSED:
                case SSH_CHANNEL_AUTH_SOCKET:
                case SSH_CHANNEL_ZOMBIE:
+               case SSH_CHANNEL_MUX_CLIENT:
+               case SSH_CHANNEL_MUX_LISTENER:
                        continue;
                case SSH_CHANNEL_LARVAL:
                case SSH_CHANNEL_OPENING:
@@ -615,12 +632,12 @@ channel_open_message(void)
                case SSH_CHANNEL_INPUT_DRAINING:
                case SSH_CHANNEL_OUTPUT_DRAINING:
                        snprintf(buf, sizeof buf,
-                           "  #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d cfd %d)\r\n",
+                           "  #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d cc %d)\r\n",
                            c->self, c->remote_name,
                            c->type, c->remote_id,
                            c->istate, buffer_len(&c->input),
                            c->ostate, buffer_len(&c->output),
-                           c->rfd, c->wfd, c->ctl_fd);
+                           c->rfd, c->wfd, c->ctl_chan);
                        buffer_append(&buffer, buf, strlen(buf));
                        continue;
                default:
@@ -827,9 +844,6 @@ channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
                        FD_SET(c->efd, readset);
        }
        /* XXX: What about efd? races? */
-       if (compat20 && c->ctl_fd != -1 &&
-           c->istate == CHAN_INPUT_OPEN && c->ostate == CHAN_OUTPUT_OPEN)
-               FD_SET(c->ctl_fd, readset);
 }
 
 /* ARGSUSED */
@@ -974,6 +988,28 @@ channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
        }
 }
 
+static void
+channel_pre_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
+{
+       if (c->istate == CHAN_INPUT_OPEN &&
+           buffer_check_alloc(&c->input, CHAN_RBUF))
+               FD_SET(c->rfd, readset);
+       if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
+               /* clear buffer immediately (discard any partial packet) */
+               buffer_clear(&c->input);
+               chan_ibuf_empty(c);
+               /* Start output drain. XXX just kill chan? */
+               chan_rcvd_oclose(c);
+       }
+       if (c->ostate == CHAN_OUTPUT_OPEN ||
+           c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
+               if (buffer_len(&c->output) > 0)
+                       FD_SET(c->wfd, writeset);
+               else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
+                       chan_obuf_empty(c);
+       }
+}
+
 /* try to decode a socks4 header */
 /* ARGSUSED */
 static int
@@ -1035,9 +1071,13 @@ channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
        strlcpy(username, p, sizeof(username));
        buffer_consume(&c->input, len);
 
+       if (c->path != NULL) {
+               xfree(c->path);
+               c->path = NULL;
+       }
        if (need == 1) {                        /* SOCKS4: one string */
                host = inet_ntoa(s4_req.dest_addr);
-               strlcpy(c->path, host, sizeof(c->path));
+               c->path = xstrdup(host);
        } else {                                /* SOCKS4A: two strings */
                have = buffer_len(&c->input);
                p = buffer_ptr(&c->input);
@@ -1048,11 +1088,12 @@ channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
                if (len > have)
                        fatal("channel %d: decode socks4a: len %d > have %d",
                            c->self, len, have);
-               if (strlcpy(c->path, p, sizeof(c->path)) >= sizeof(c->path)) {
+               if (len > NI_MAXHOST) {
                        error("channel %d: hostname \"%.100s\" too long",
                            c->self, p);
                        return -1;
                }
+               c->path = xstrdup(p);
                buffer_consume(&c->input, len);
        }
        c->host_port = ntohs(s4_req.dest_port);
@@ -1093,7 +1134,7 @@ channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
                u_int8_t atyp;
        } s5_req, s5_rsp;
        u_int16_t dest_port;
-       u_char *p, dest_addr[255+1];
+       u_char *p, dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
        u_int have, need, i, found, nmethods, addrlen, af;
 
        debug2("channel %d: decode socks5", c->self);
@@ -1166,10 +1207,22 @@ channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
        buffer_get(&c->input, (char *)&dest_addr, addrlen);
        buffer_get(&c->input, (char *)&dest_port, 2);
        dest_addr[addrlen] = '\0';
-       if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
-               strlcpy(c->path, (char *)dest_addr, sizeof(c->path));
-       else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL)
-               return -1;
+       if (c->path != NULL) {
+               xfree(c->path);
+               c->path = NULL;
+       }
+       if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
+               if (addrlen >= NI_MAXHOST) {
+                       error("channel %d: dynamic request: socks5 hostname "
+                           "\"%.100s\" too long", c->self, dest_addr);
+                       return -1;
+               }
+               c->path = xstrdup(dest_addr);
+       } else {
+               if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
+                       return -1;
+               c->path = xstrdup(ntop);
+       }
        c->host_port = ntohs(dest_port);
 
        debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
@@ -1188,6 +1241,30 @@ channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
        return 1;
 }
 
+Channel *
+channel_connect_stdio_fwd(const char *host_to_connect, u_short port_to_connect,
+    int in, int out)
+{
+       Channel *c;
+
+       debug("channel_connect_stdio_fwd %s:%d", host_to_connect,
+           port_to_connect);
+
+       c = channel_new("stdio-forward", SSH_CHANNEL_OPENING, in, out,
+           -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
+           0, "stdio-forward", /*nonblock*/0);
+
+       c->path = xstrdup(host_to_connect);
+       c->host_port = port_to_connect;
+       c->listening_port = 0;
+       c->force_drain = 1;
+
+       channel_register_fds(c, in, out, -1, 0, 1, 0);
+       port_open_helper(c, "direct-tcpip");
+
+       return c;
+}
+
 /* dynamic port forwarding */
 static void
 channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
@@ -1197,7 +1274,6 @@ channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
        int ret;
 
        have = buffer_len(&c->input);
-       c->delayed = 0;
        debug2("channel %d: pre_dynamic: have %d", c->self, have);
        /* buffer_dump(&c->input); */
        /* check if the fixed size part of the packet is in buffer. */
@@ -1398,18 +1474,11 @@ channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
                    c->local_window_max, c->local_maxpacket, 0, rtype, 1);
                nc->listening_port = c->listening_port;
                nc->host_port = c->host_port;
-               strlcpy(nc->path, c->path, sizeof(nc->path));
+               if (c->path != NULL)
+                       nc->path = xstrdup(c->path);
 
-               if (nextstate == SSH_CHANNEL_DYNAMIC) {
-                       /*
-                        * do not call the channel_post handler until
-                        * this flag has been reset by a pre-handler.
-                        * otherwise the FD_ISSET calls might overflow
-                        */
-                       nc->delayed = 1;
-               } else {
+               if (nextstate != SSH_CHANNEL_DYNAMIC)
                        port_open_helper(nc, rtype);
-               }
        }
 }
 
@@ -1630,6 +1699,7 @@ channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
                        }
                        return -1;
                }
+#ifndef BROKEN_TCGETATTR_ICANON
                if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
                        if (tcgetattr(c->wfd, &tio) == 0 &&
                            !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
@@ -1643,6 +1713,7 @@ channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
                                packet_send();
                        }
                }
+#endif
                buffer_consume(&c->output, len);
                if (compat20 && len > 0) {
                        c->local_consumed += len;
@@ -1697,36 +1768,6 @@ channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
        return 1;
 }
 
-/* ARGSUSED */
-static int
-channel_handle_ctl(Channel *c, fd_set *readset, fd_set *writeset)
-{
-       char buf[16];
-       int len;
-
-       /* Monitor control fd to detect if the slave client exits */
-       if (c->ctl_fd != -1 && FD_ISSET(c->ctl_fd, readset)) {
-               len = read(c->ctl_fd, buf, sizeof(buf));
-               if (len < 0 &&
-                   (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
-                       return 1;
-               if (len <= 0) {
-                       debug2("channel %d: ctl read<=0", c->self);
-                       if (c->type != SSH_CHANNEL_OPEN) {
-                               debug2("channel %d: not open", c->self);
-                               chan_mark_dead(c);
-                               return -1;
-                       } else {
-                               chan_read_failed(c);
-                               chan_write_failed(c);
-                       }
-                       return -1;
-               } else
-                       fatal("%s: unexpected data on ctl fd", __func__);
-       }
-       return 1;
-}
-
 static int
 channel_check_window(Channel *c)
 {
@@ -1752,17 +1793,136 @@ channel_check_window(Channel *c)
 static void
 channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
 {
-       if (c->delayed)
-               return;
        channel_handle_rfd(c, readset, writeset);
        channel_handle_wfd(c, readset, writeset);
        if (!compat20)
                return;
        channel_handle_efd(c, readset, writeset);
-       channel_handle_ctl(c, readset, writeset);
        channel_check_window(c);
 }
 
+static u_int
+read_mux(Channel *c, u_int need)
+{
+       char buf[CHAN_RBUF];
+       int len;
+       u_int rlen;
+
+       if (buffer_len(&c->input) < need) {
+               rlen = need - buffer_len(&c->input);
+               len = read(c->rfd, buf, MIN(rlen, CHAN_RBUF));
+               if (len <= 0) {
+                       if (errno != EINTR && errno != EAGAIN) {
+                               debug2("channel %d: ctl read<=0 rfd %d len %d",
+                                   c->self, c->rfd, len);
+                               chan_read_failed(c);
+                               return 0;
+                       }
+               } else
+                       buffer_append(&c->input, buf, len);
+       }
+       return buffer_len(&c->input);
+}
+
+static void
+channel_post_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
+{
+       u_int need;
+       ssize_t len;
+
+       if (!compat20)
+               fatal("%s: entered with !compat20", __func__);
+
+       if (c->rfd != -1 && FD_ISSET(c->rfd, readset) &&
+           (c->istate == CHAN_INPUT_OPEN ||
+           c->istate == CHAN_INPUT_WAIT_DRAIN)) {
+               /*
+                * Don't not read past the precise end of packets to
+                * avoid disrupting fd passing.
+                */
+               if (read_mux(c, 4) < 4) /* read header */
+                       return;
+               need = get_u32(buffer_ptr(&c->input));
+#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
+               if (need > CHANNEL_MUX_MAX_PACKET) {
+                       debug2("channel %d: packet too big %u > %u",
+                           c->self, CHANNEL_MUX_MAX_PACKET, need);
+                       chan_rcvd_oclose(c);
+                       return;
+               }
+               if (read_mux(c, need + 4) < need + 4) /* read body */
+                       return;
+               if (c->mux_rcb(c) != 0) {
+                       debug("channel %d: mux_rcb failed", c->self);
+                       chan_mark_dead(c);
+                       return;
+               }
+       }
+
+       if (c->wfd != -1 && FD_ISSET(c->wfd, writeset) &&
+           buffer_len(&c->output) > 0) {
+               len = write(c->wfd, buffer_ptr(&c->output),
+                   buffer_len(&c->output));
+               if (len < 0 && (errno == EINTR || errno == EAGAIN))
+                       return;
+               if (len <= 0) {
+                       chan_mark_dead(c);
+                       return;
+               }
+               buffer_consume(&c->output, len);
+       }
+}
+
+static void
+channel_post_mux_listener(Channel *c, fd_set *readset, fd_set *writeset)
+{
+       Channel *nc;
+       struct sockaddr_storage addr;
+       socklen_t addrlen;
+       int newsock;
+       uid_t euid;
+       gid_t egid;
+
+       if (!FD_ISSET(c->sock, readset))
+               return;
+
+       debug("multiplexing control connection");
+
+       /*
+        * Accept connection on control socket
+        */
+       memset(&addr, 0, sizeof(addr));
+       addrlen = sizeof(addr);
+       if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
+           &addrlen)) == -1) {
+               error("%s accept: %s", __func__, strerror(errno));
+               return;
+       }
+
+       if (getpeereid(newsock, &euid, &egid) < 0) {
+               error("%s getpeereid failed: %s", __func__,
+                   strerror(errno));
+               close(newsock);
+               return;
+       }
+       if ((euid != 0) && (getuid() != euid)) {
+               error("multiplex uid mismatch: peer euid %u != uid %u",
+                   (u_int)euid, (u_int)getuid());
+               close(newsock);
+               return;
+       }
+       nc = channel_new("multiplex client", SSH_CHANNEL_MUX_CLIENT,
+           newsock, newsock, -1, c->local_window_max,
+           c->local_maxpacket, 0, "mux-control", 1);
+       nc->mux_rcb = c->mux_rcb;
+       debug3("%s: new mux channel %d fd %d", __func__,
+           nc->self, nc->sock);
+       /* establish state */
+       nc->mux_rcb(nc);
+       /* mux state transitions must not elicit protocol messages */
+       nc->flags |= CHAN_LOCAL;
+}
+
 /* ARGSUSED */
 static void
 channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
@@ -1791,6 +1951,8 @@ channel_handler_init_20(void)
        channel_pre[SSH_CHANNEL_AUTH_SOCKET] =          &channel_pre_listener;
        channel_pre[SSH_CHANNEL_CONNECTING] =           &channel_pre_connecting;
        channel_pre[SSH_CHANNEL_DYNAMIC] =              &channel_pre_dynamic;
+       channel_pre[SSH_CHANNEL_MUX_LISTENER] =         &channel_pre_listener;
+       channel_pre[SSH_CHANNEL_MUX_CLIENT] =           &channel_pre_mux_client;
 
        channel_post[SSH_CHANNEL_OPEN] =                &channel_post_open;
        channel_post[SSH_CHANNEL_PORT_LISTENER] =       &channel_post_port_listener;
@@ -1799,6 +1961,8 @@ channel_handler_init_20(void)
        channel_post[SSH_CHANNEL_AUTH_SOCKET] =         &channel_post_auth_listener;
        channel_post[SSH_CHANNEL_CONNECTING] =          &channel_post_connecting;
        channel_post[SSH_CHANNEL_DYNAMIC] =             &channel_post_open;
+       channel_post[SSH_CHANNEL_MUX_LISTENER] =        &channel_post_mux_listener;
+       channel_post[SSH_CHANNEL_MUX_CLIENT] =          &channel_post_mux_client;
 }
 
 static void
@@ -1885,17 +2049,23 @@ static void
 channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset)
 {
        static int did_init = 0;
-       u_int i;
+       u_int i, oalloc;
        Channel *c;
 
        if (!did_init) {
                channel_handler_init();
                did_init = 1;
        }
-       for (i = 0; i < channels_alloc; i++) {
+       for (i = 0, oalloc = channels_alloc; i < oalloc; i++) {
                c = channels[i];
                if (c == NULL)
                        continue;
+               if (c->delayed) {
+                       if (ftab == channel_pre)
+                               c->delayed = 0;
+                       else
+                               continue;
+               }
                if (ftab[c->type] != NULL)
                        (*ftab[c->type])(c, readset, writeset);
                channel_garbage_collect(c);
@@ -2408,7 +2578,7 @@ channel_input_status_confirm(int type, u_int32_t seq, void *ctxt)
        int id;
 
        /* Reset keepalive timeout */
-       keep_alive_timeouts = 0;
+       packet_set_alive_timeouts(0);
 
        id = packet_get_int();
        packet_check_eom();
@@ -2437,7 +2607,8 @@ channel_set_af(int af)
 }
 
 static int
-channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
+channel_setup_fwd_listener(int type, const char *listen_addr,
+    u_short listen_port, int *allocated_listen_port,
     const char *host_to_connect, u_short port_to_connect, int gateway_ports)
 {
        Channel *c;
@@ -2445,6 +2616,7 @@ channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_por
        struct addrinfo hints, *ai, *aitop;
        const char *host, *addr;
        char ntop[NI_MAXHOST], strport[NI_MAXSERV];
+       in_port_t *lport_p;
 
        host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
            listen_addr : host_to_connect;
@@ -2454,7 +2626,7 @@ channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_por
                error("No forward host name.");
                return 0;
        }
-       if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
+       if (strlen(host) >= NI_MAXHOST) {
                error("Forward host name too long.");
                return 0;
        }
@@ -2513,10 +2685,29 @@ channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_por
                }
                return 0;
        }
-
+       if (allocated_listen_port != NULL)
+               *allocated_listen_port = 0;
        for (ai = aitop; ai; ai = ai->ai_next) {
-               if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
+               switch (ai->ai_family) {
+               case AF_INET:
+                       lport_p = &((struct sockaddr_in *)ai->ai_addr)->
+                           sin_port;
+                       break;
+               case AF_INET6:
+                       lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
+                           sin6_port;
+                       break;
+               default:
                        continue;
+               }
+               /*
+                * If allocating a port for -R forwards, then use the
+                * same port for all address families.
+                */
+               if (type == SSH_CHANNEL_RPORT_LISTENER && listen_port == 0 &&
+                   allocated_listen_port != NULL && *allocated_listen_port > 0)
+                       *lport_p = htons(*allocated_listen_port);
+
                if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
                    strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
                        error("channel_setup_fwd_listener: getnameinfo failed");
@@ -2531,8 +2722,11 @@ channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_por
                }
 
                channel_set_reuseaddr(sock);
+               if (ai->ai_family == AF_INET6)
+                       sock_set_v6only(sock);
 
-               debug("Local forwarding listening on %s port %s.", ntop, strport);
+               debug("Local forwarding listening on %s port %s.",
+                   ntop, strport);
 
                /* Bind the socket to the address. */
                if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
@@ -2551,11 +2745,24 @@ channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_por
                        close(sock);
                        continue;
                }
+
+               /*
+                * listen_port == 0 requests a dynamically allocated port -
+                * record what we got.
+                */
+               if (type == SSH_CHANNEL_RPORT_LISTENER && listen_port == 0 &&
+                   allocated_listen_port != NULL &&
+                   *allocated_listen_port == 0) {
+                       *allocated_listen_port = get_sock_port(sock, 1);
+                       debug("Allocated listen port %d",
+                           *allocated_listen_port);
+               }
+
                /* Allocate a channel number for the socket. */
                c = channel_new("port listener", type, sock, sock, -1,
                    CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
                    0, "port listener", 1);
-               strlcpy(c->path, host, sizeof(c->path));
+               c->path = xstrdup(host);
                c->host_port = port_to_connect;
                c->listening_port = listen_port;
                success = 1;
@@ -2577,8 +2784,7 @@ channel_cancel_rport_listener(const char *host, u_short port)
                Channel *c = channels[i];
 
                if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
-                   strncmp(c->path, host, sizeof(c->path)) == 0 &&
-                   c->listening_port == port) {
+                   strcmp(c->path, host) == 0 && c->listening_port == port) {
                        debug2("%s: close channel %d", __func__, i);
                        channel_free(c);
                        found = 1;
@@ -2594,17 +2800,18 @@ channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
     const char *host_to_connect, u_short port_to_connect, int gateway_ports)
 {
        return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
-           listen_host, listen_port, host_to_connect, port_to_connect,
+           listen_host, listen_port, NULL, host_to_connect, port_to_connect,
            gateway_ports);
 }
 
 /* protocol v2 remote port fwd, used by sshd */
 int
 channel_setup_remote_fwd_listener(const char *listen_address,
-    u_short listen_port, int gateway_ports)
+    u_short listen_port, int *allocated_listen_port, int gateway_ports)
 {
        return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
-           listen_address, listen_port, NULL, 0, gateway_ports);
+           listen_address, listen_port, allocated_listen_port,
+           NULL, 0, gateway_ports);
 }
 
 /*
@@ -3048,13 +3255,8 @@ x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
                                        continue;
                                }
                        }
-#ifdef IPV6_V6ONLY
-                       if (ai->ai_family == AF_INET6) {
-                               int on = 1;
-                               if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
-                                       error("setsockopt IPV6_V6ONLY: %.100s", strerror(errno));
-                       }
-#endif
+                       if (ai->ai_family == AF_INET6)
+                               sock_set_v6only(sock);
                        if (x11_use_localhost)
                                channel_set_reuseaddr(sock);
                        if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
This page took 0.069807 seconds and 4 git commands to generate.