17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Author: Tatu Ylonen <ylo@cs.hut.fi>
37c478bd9Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
47c478bd9Sstevel@tonic-gate * All rights reserved
57c478bd9Sstevel@tonic-gate * This file contains functions for generic socket connection forwarding.
67c478bd9Sstevel@tonic-gate * There is also code for initiating connection forwarding for X11 connections,
77c478bd9Sstevel@tonic-gate * arbitrary tcp/ip connections, and the authentication agent connection.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software
107c478bd9Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this
117c478bd9Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is
127c478bd9Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be
137c478bd9Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell".
147c478bd9Sstevel@tonic-gate *
157c478bd9Sstevel@tonic-gate * SSH2 support added by Markus Friedl.
167c478bd9Sstevel@tonic-gate * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
177c478bd9Sstevel@tonic-gate * Copyright (c) 1999 Dug Song. All rights reserved.
187c478bd9Sstevel@tonic-gate * Copyright (c) 1999 Theo de Raadt. All rights reserved.
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
217c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
227c478bd9Sstevel@tonic-gate * are met:
237c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
247c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
257c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
267c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
277c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
287c478bd9Sstevel@tonic-gate *
297c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
307c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
317c478bd9Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
327c478bd9Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
337c478bd9Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
347c478bd9Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
357c478bd9Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
367c478bd9Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
377c478bd9Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
387c478bd9Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
397c478bd9Sstevel@tonic-gate */
409b03ea0fSjp161948 /*
41*8b0ef7edSZdenek Kotala * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
429b03ea0fSjp161948 */
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate #include "includes.h"
457c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: channels.c,v 1.183 2002/09/17 07:47:02 itojun Exp $");
467c478bd9Sstevel@tonic-gate
477c478bd9Sstevel@tonic-gate #include "ssh.h"
487c478bd9Sstevel@tonic-gate #include "ssh1.h"
497c478bd9Sstevel@tonic-gate #include "ssh2.h"
507c478bd9Sstevel@tonic-gate #include "packet.h"
517c478bd9Sstevel@tonic-gate #include "xmalloc.h"
527c478bd9Sstevel@tonic-gate #include "log.h"
537c478bd9Sstevel@tonic-gate #include "misc.h"
547c478bd9Sstevel@tonic-gate #include "channels.h"
557c478bd9Sstevel@tonic-gate #include "compat.h"
567c478bd9Sstevel@tonic-gate #include "canohost.h"
577c478bd9Sstevel@tonic-gate #include "key.h"
587c478bd9Sstevel@tonic-gate #include "authfd.h"
597c478bd9Sstevel@tonic-gate #include "pathnames.h"
60d80e6060Sjp161948 #include "bufaux.h"
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate /* -- channel core */
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate * Pointer to an array containing all allocated channels. The array is
677c478bd9Sstevel@tonic-gate * dynamically extended as needed.
687c478bd9Sstevel@tonic-gate */
697c478bd9Sstevel@tonic-gate static Channel **channels = NULL;
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate * Size of the channel array. All slots of the array must always be
737c478bd9Sstevel@tonic-gate * initialized (at least the type field); unused slots set to NULL
747c478bd9Sstevel@tonic-gate */
757c478bd9Sstevel@tonic-gate static int channels_alloc = 0;
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate * Maximum file descriptor value used in any of the channels. This is
797c478bd9Sstevel@tonic-gate * updated in channel_new.
807c478bd9Sstevel@tonic-gate */
817c478bd9Sstevel@tonic-gate static int channel_max_fd = 0;
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate /* -- tcp forwarding */
857c478bd9Sstevel@tonic-gate
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate * Data structure for storing which hosts are permitted for forward requests.
887c478bd9Sstevel@tonic-gate * The local sides of any remote forwards are stored in this array to prevent
897c478bd9Sstevel@tonic-gate * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
907c478bd9Sstevel@tonic-gate * network (which might be behind a firewall).
917c478bd9Sstevel@tonic-gate */
927c478bd9Sstevel@tonic-gate typedef struct {
937c478bd9Sstevel@tonic-gate char *host_to_connect; /* Connect to 'host'. */
947c478bd9Sstevel@tonic-gate u_short port_to_connect; /* Connect to 'port'. */
957c478bd9Sstevel@tonic-gate u_short listen_port; /* Remote side should listen port number. */
967c478bd9Sstevel@tonic-gate } ForwardPermission;
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /* List of all permitted host/port pairs to connect. */
997c478bd9Sstevel@tonic-gate static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate /* Number of permitted host/port pairs in the array. */
1027c478bd9Sstevel@tonic-gate static int num_permitted_opens = 0;
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate * If this is true, all opens are permitted. This is the case on the server
1057c478bd9Sstevel@tonic-gate * on which we have to trust the client anyway, and the user could do
1067c478bd9Sstevel@tonic-gate * anything after logging in anyway.
1077c478bd9Sstevel@tonic-gate */
1087c478bd9Sstevel@tonic-gate static int all_opens_permitted = 0;
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate /* -- X11 forwarding */
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate /* Maximum number of fake X11 displays to try. */
1147c478bd9Sstevel@tonic-gate #define MAX_DISPLAYS 1000
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate /* Saved X11 authentication protocol name. */
1177c478bd9Sstevel@tonic-gate static char *x11_saved_proto = NULL;
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate /* Saved X11 authentication data. This is the real data. */
1207c478bd9Sstevel@tonic-gate static char *x11_saved_data = NULL;
1217c478bd9Sstevel@tonic-gate static u_int x11_saved_data_len = 0;
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate * Fake X11 authentication data. This is what the server will be sending us;
1257c478bd9Sstevel@tonic-gate * we should replace any occurrences of this by the real data.
1267c478bd9Sstevel@tonic-gate */
127383a1232Sjp161948 static u_char *x11_fake_data = NULL;
1287c478bd9Sstevel@tonic-gate static u_int x11_fake_data_len;
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate /* -- agent forwarding */
1327c478bd9Sstevel@tonic-gate
1337c478bd9Sstevel@tonic-gate #define NUM_SOCKS 10
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate /* AF_UNSPEC or AF_INET or AF_INET6 */
1367c478bd9Sstevel@tonic-gate static int IPv4or6 = AF_UNSPEC;
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /* helper */
1397c478bd9Sstevel@tonic-gate static void port_open_helper(Channel *c, char *rtype);
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate /* -- channel core */
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate Channel *
channel_lookup(int id)1447c478bd9Sstevel@tonic-gate channel_lookup(int id)
1457c478bd9Sstevel@tonic-gate {
1467c478bd9Sstevel@tonic-gate Channel *c;
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate if (id < 0 || id >= channels_alloc) {
1497c478bd9Sstevel@tonic-gate log("channel_lookup: %d: bad id", id);
1507c478bd9Sstevel@tonic-gate return NULL;
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate c = channels[id];
1537c478bd9Sstevel@tonic-gate if (c == NULL) {
1547c478bd9Sstevel@tonic-gate log("channel_lookup: %d: bad id: channel free", id);
1557c478bd9Sstevel@tonic-gate return NULL;
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate return c;
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate * Register filedescriptors for a channel, used when allocating a channel or
1627c478bd9Sstevel@tonic-gate * when the channel consumer/producer is ready, e.g. shell exec'd
1637c478bd9Sstevel@tonic-gate */
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate static void
channel_register_fds(Channel * c,int rfd,int wfd,int efd,int extusage,int nonblock)1667c478bd9Sstevel@tonic-gate channel_register_fds(Channel *c, int rfd, int wfd, int efd,
1677c478bd9Sstevel@tonic-gate int extusage, int nonblock)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate /* Update the maximum file descriptor value. */
1707c478bd9Sstevel@tonic-gate channel_max_fd = MAX(channel_max_fd, rfd);
1717c478bd9Sstevel@tonic-gate channel_max_fd = MAX(channel_max_fd, wfd);
1727c478bd9Sstevel@tonic-gate channel_max_fd = MAX(channel_max_fd, efd);
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate /* XXX set close-on-exec -markus */
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate c->rfd = rfd;
1777c478bd9Sstevel@tonic-gate c->wfd = wfd;
1787c478bd9Sstevel@tonic-gate c->sock = (rfd == wfd) ? rfd : -1;
1797c478bd9Sstevel@tonic-gate c->efd = efd;
1807c478bd9Sstevel@tonic-gate c->extended_usage = extusage;
1817c478bd9Sstevel@tonic-gate
1827c478bd9Sstevel@tonic-gate /* XXX ugly hack: nonblock is only set by the server */
1837c478bd9Sstevel@tonic-gate if (nonblock && isatty(c->rfd)) {
1847c478bd9Sstevel@tonic-gate debug("channel %d: rfd %d isatty", c->self, c->rfd);
1857c478bd9Sstevel@tonic-gate c->isatty = 1;
1867c478bd9Sstevel@tonic-gate if (!isatty(c->wfd)) {
1877c478bd9Sstevel@tonic-gate error("channel %d: wfd %d is not a tty?",
1887c478bd9Sstevel@tonic-gate c->self, c->wfd);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate } else {
1917c478bd9Sstevel@tonic-gate c->isatty = 0;
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate c->wfd_isatty = isatty(c->wfd);
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate /* enable nonblocking mode */
1967c478bd9Sstevel@tonic-gate if (nonblock) {
1977c478bd9Sstevel@tonic-gate if (rfd != -1)
1987c478bd9Sstevel@tonic-gate set_nonblock(rfd);
1997c478bd9Sstevel@tonic-gate if (wfd != -1)
2007c478bd9Sstevel@tonic-gate set_nonblock(wfd);
2017c478bd9Sstevel@tonic-gate if (efd != -1)
2027c478bd9Sstevel@tonic-gate set_nonblock(efd);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate * Allocate a new channel object and set its type and socket. This will cause
2087c478bd9Sstevel@tonic-gate * remote_name to be freed.
2097c478bd9Sstevel@tonic-gate */
2107c478bd9Sstevel@tonic-gate
2117c478bd9Sstevel@tonic-gate Channel *
channel_new(char * ctype,int type,int rfd,int wfd,int efd,u_int window,u_int maxpack,int extusage,char * remote_name,int nonblock)2127c478bd9Sstevel@tonic-gate channel_new(char *ctype, int type, int rfd, int wfd, int efd,
2137c478bd9Sstevel@tonic-gate u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate int i, found;
2167c478bd9Sstevel@tonic-gate Channel *c;
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate /* Do initial allocation if this is the first call. */
2197c478bd9Sstevel@tonic-gate if (channels_alloc == 0) {
2207c478bd9Sstevel@tonic-gate channels_alloc = 10;
2217c478bd9Sstevel@tonic-gate channels = xmalloc(channels_alloc * sizeof(Channel *));
2227c478bd9Sstevel@tonic-gate for (i = 0; i < channels_alloc; i++)
2237c478bd9Sstevel@tonic-gate channels[i] = NULL;
2247c478bd9Sstevel@tonic-gate fatal_add_cleanup((void (*) (void *)) channel_free_all, NULL);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate /* Try to find a free slot where to put the new channel. */
2277c478bd9Sstevel@tonic-gate for (found = -1, i = 0; i < channels_alloc; i++)
2287c478bd9Sstevel@tonic-gate if (channels[i] == NULL) {
2297c478bd9Sstevel@tonic-gate /* Found a free slot. */
2307c478bd9Sstevel@tonic-gate found = i;
2317c478bd9Sstevel@tonic-gate break;
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate if (found == -1) {
2347c478bd9Sstevel@tonic-gate /* There are no free slots. Take last+1 slot and expand the array. */
2357c478bd9Sstevel@tonic-gate found = channels_alloc;
2367c478bd9Sstevel@tonic-gate if (channels_alloc > 10000)
2377c478bd9Sstevel@tonic-gate fatal("channel_new: internal error: channels_alloc %d "
2387c478bd9Sstevel@tonic-gate "too big.", channels_alloc);
2397c478bd9Sstevel@tonic-gate channels = xrealloc(channels,
2407c478bd9Sstevel@tonic-gate (channels_alloc + 10) * sizeof(Channel *));
2417c478bd9Sstevel@tonic-gate channels_alloc += 10;
2427c478bd9Sstevel@tonic-gate debug2("channel: expanding %d", channels_alloc);
2437c478bd9Sstevel@tonic-gate for (i = found; i < channels_alloc; i++)
2447c478bd9Sstevel@tonic-gate channels[i] = NULL;
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate /* Initialize and return new channel. */
2477c478bd9Sstevel@tonic-gate c = channels[found] = xmalloc(sizeof(Channel));
2487c478bd9Sstevel@tonic-gate memset(c, 0, sizeof(Channel));
2497c478bd9Sstevel@tonic-gate buffer_init(&c->input);
2507c478bd9Sstevel@tonic-gate buffer_init(&c->output);
2517c478bd9Sstevel@tonic-gate buffer_init(&c->extended);
2527c478bd9Sstevel@tonic-gate c->ostate = CHAN_OUTPUT_OPEN;
2537c478bd9Sstevel@tonic-gate c->istate = CHAN_INPUT_OPEN;
2547c478bd9Sstevel@tonic-gate c->flags = 0;
2557c478bd9Sstevel@tonic-gate channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
2567c478bd9Sstevel@tonic-gate c->self = found;
2577c478bd9Sstevel@tonic-gate c->type = type;
2587c478bd9Sstevel@tonic-gate c->ctype = ctype;
2597c478bd9Sstevel@tonic-gate c->local_window = window;
2607c478bd9Sstevel@tonic-gate c->local_window_max = window;
2617c478bd9Sstevel@tonic-gate c->local_consumed = 0;
2627c478bd9Sstevel@tonic-gate c->local_maxpacket = maxpack;
2637c478bd9Sstevel@tonic-gate c->remote_id = -1;
2647c478bd9Sstevel@tonic-gate c->remote_name = remote_name;
2657c478bd9Sstevel@tonic-gate c->remote_window = 0;
2667c478bd9Sstevel@tonic-gate c->remote_maxpacket = 0;
2677c478bd9Sstevel@tonic-gate c->force_drain = 0;
2687c478bd9Sstevel@tonic-gate c->single_connection = 0;
2697c478bd9Sstevel@tonic-gate c->detach_user = NULL;
2707c478bd9Sstevel@tonic-gate c->confirm = NULL;
2717c478bd9Sstevel@tonic-gate c->input_filter = NULL;
272*8b0ef7edSZdenek Kotala c->delayed = 1; /* prevent call to channel_post handler */
2737c478bd9Sstevel@tonic-gate debug("channel %d: new [%s]", found, remote_name);
2747c478bd9Sstevel@tonic-gate return c;
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate static int
channel_find_maxfd(void)2787c478bd9Sstevel@tonic-gate channel_find_maxfd(void)
2797c478bd9Sstevel@tonic-gate {
2807c478bd9Sstevel@tonic-gate int i, max = 0;
2817c478bd9Sstevel@tonic-gate Channel *c;
2827c478bd9Sstevel@tonic-gate
2837c478bd9Sstevel@tonic-gate for (i = 0; i < channels_alloc; i++) {
2847c478bd9Sstevel@tonic-gate c = channels[i];
2857c478bd9Sstevel@tonic-gate if (c != NULL) {
2867c478bd9Sstevel@tonic-gate max = MAX(max, c->rfd);
2877c478bd9Sstevel@tonic-gate max = MAX(max, c->wfd);
2887c478bd9Sstevel@tonic-gate max = MAX(max, c->efd);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate return max;
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate int
channel_close_fd(int * fdp)2957c478bd9Sstevel@tonic-gate channel_close_fd(int *fdp)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate int ret = 0, fd = *fdp;
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate if (fd != -1) {
3007c478bd9Sstevel@tonic-gate ret = close(fd);
3017c478bd9Sstevel@tonic-gate *fdp = -1;
3027c478bd9Sstevel@tonic-gate if (fd == channel_max_fd)
3037c478bd9Sstevel@tonic-gate channel_max_fd = channel_find_maxfd();
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate return ret;
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate /* Close all channel fd/socket. */
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate static void
channel_close_fds(Channel * c)3117c478bd9Sstevel@tonic-gate channel_close_fds(Channel *c)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate debug3("channel_close_fds: channel %d: r %d w %d e %d",
3147c478bd9Sstevel@tonic-gate c->self, c->rfd, c->wfd, c->efd);
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate channel_close_fd(&c->sock);
3177c478bd9Sstevel@tonic-gate channel_close_fd(&c->rfd);
3187c478bd9Sstevel@tonic-gate channel_close_fd(&c->wfd);
3197c478bd9Sstevel@tonic-gate channel_close_fd(&c->efd);
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate
3227c478bd9Sstevel@tonic-gate /* Free the channel and close its fd/socket. */
3237c478bd9Sstevel@tonic-gate
3247c478bd9Sstevel@tonic-gate void
channel_free(Channel * c)3257c478bd9Sstevel@tonic-gate channel_free(Channel *c)
3267c478bd9Sstevel@tonic-gate {
3277c478bd9Sstevel@tonic-gate char *s;
3287c478bd9Sstevel@tonic-gate int i, n;
3297c478bd9Sstevel@tonic-gate
3307c478bd9Sstevel@tonic-gate for (n = 0, i = 0; i < channels_alloc; i++)
3317c478bd9Sstevel@tonic-gate if (channels[i])
3327c478bd9Sstevel@tonic-gate n++;
3337c478bd9Sstevel@tonic-gate debug("channel_free: channel %d: %s, nchannels %d", c->self,
3347c478bd9Sstevel@tonic-gate c->remote_name ? c->remote_name : "???", n);
3357c478bd9Sstevel@tonic-gate
3367c478bd9Sstevel@tonic-gate s = channel_open_message();
3377c478bd9Sstevel@tonic-gate debug3("channel_free: status: %s", s);
3387c478bd9Sstevel@tonic-gate xfree(s);
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate if (c->sock != -1)
3417c478bd9Sstevel@tonic-gate shutdown(c->sock, SHUT_RDWR);
3427c478bd9Sstevel@tonic-gate channel_close_fds(c);
3437c478bd9Sstevel@tonic-gate buffer_free(&c->input);
3447c478bd9Sstevel@tonic-gate buffer_free(&c->output);
3457c478bd9Sstevel@tonic-gate buffer_free(&c->extended);
3467c478bd9Sstevel@tonic-gate if (c->remote_name) {
3477c478bd9Sstevel@tonic-gate xfree(c->remote_name);
3487c478bd9Sstevel@tonic-gate c->remote_name = NULL;
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate channels[c->self] = NULL;
3517c478bd9Sstevel@tonic-gate xfree(c);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate
3547c478bd9Sstevel@tonic-gate void
channel_free_all(void)3557c478bd9Sstevel@tonic-gate channel_free_all(void)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate int i;
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate for (i = 0; i < channels_alloc; i++)
3607c478bd9Sstevel@tonic-gate if (channels[i] != NULL)
3617c478bd9Sstevel@tonic-gate channel_free(channels[i]);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate * Closes the sockets/fds of all channels. This is used to close extra file
3667c478bd9Sstevel@tonic-gate * descriptors after a fork.
3677c478bd9Sstevel@tonic-gate */
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate void
channel_close_all(void)3707c478bd9Sstevel@tonic-gate channel_close_all(void)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate int i;
3737c478bd9Sstevel@tonic-gate
3747c478bd9Sstevel@tonic-gate for (i = 0; i < channels_alloc; i++)
3757c478bd9Sstevel@tonic-gate if (channels[i] != NULL)
3767c478bd9Sstevel@tonic-gate channel_close_fds(channels[i]);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate /*
3807c478bd9Sstevel@tonic-gate * Stop listening to channels.
3817c478bd9Sstevel@tonic-gate */
3827c478bd9Sstevel@tonic-gate
3837c478bd9Sstevel@tonic-gate void
channel_stop_listening(void)3847c478bd9Sstevel@tonic-gate channel_stop_listening(void)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate int i;
3877c478bd9Sstevel@tonic-gate Channel *c;
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate for (i = 0; i < channels_alloc; i++) {
3907c478bd9Sstevel@tonic-gate c = channels[i];
3917c478bd9Sstevel@tonic-gate if (c != NULL) {
3927c478bd9Sstevel@tonic-gate switch (c->type) {
3937c478bd9Sstevel@tonic-gate case SSH_CHANNEL_AUTH_SOCKET:
3947c478bd9Sstevel@tonic-gate case SSH_CHANNEL_PORT_LISTENER:
3957c478bd9Sstevel@tonic-gate case SSH_CHANNEL_RPORT_LISTENER:
3967c478bd9Sstevel@tonic-gate case SSH_CHANNEL_X11_LISTENER:
3977c478bd9Sstevel@tonic-gate channel_close_fd(&c->sock);
3987c478bd9Sstevel@tonic-gate channel_free(c);
3997c478bd9Sstevel@tonic-gate break;
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate * Returns true if no channel has too much buffered data, and false if one or
4077c478bd9Sstevel@tonic-gate * more channel is overfull.
4087c478bd9Sstevel@tonic-gate */
4097c478bd9Sstevel@tonic-gate
4107c478bd9Sstevel@tonic-gate int
channel_not_very_much_buffered_data(void)4117c478bd9Sstevel@tonic-gate channel_not_very_much_buffered_data(void)
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate u_int i;
4147c478bd9Sstevel@tonic-gate Channel *c;
4157c478bd9Sstevel@tonic-gate
4167c478bd9Sstevel@tonic-gate for (i = 0; i < channels_alloc; i++) {
4177c478bd9Sstevel@tonic-gate c = channels[i];
4187c478bd9Sstevel@tonic-gate if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
4197c478bd9Sstevel@tonic-gate #if 0
4207c478bd9Sstevel@tonic-gate if (!compat20 &&
4217c478bd9Sstevel@tonic-gate buffer_len(&c->input) > packet_get_maxsize()) {
4227c478bd9Sstevel@tonic-gate debug("channel %d: big input buffer %d",
4237c478bd9Sstevel@tonic-gate c->self, buffer_len(&c->input));
4247c478bd9Sstevel@tonic-gate return 0;
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate #endif
4277c478bd9Sstevel@tonic-gate if (buffer_len(&c->output) > packet_get_maxsize()) {
4287c478bd9Sstevel@tonic-gate debug("channel %d: big output buffer %d > %d",
4297c478bd9Sstevel@tonic-gate c->self, buffer_len(&c->output),
4307c478bd9Sstevel@tonic-gate packet_get_maxsize());
4317c478bd9Sstevel@tonic-gate return 0;
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate return 1;
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate /* Returns true if any channel is still open. */
4397c478bd9Sstevel@tonic-gate
4407c478bd9Sstevel@tonic-gate int
channel_still_open(void)4417c478bd9Sstevel@tonic-gate channel_still_open(void)
4427c478bd9Sstevel@tonic-gate {
4437c478bd9Sstevel@tonic-gate int i;
4447c478bd9Sstevel@tonic-gate Channel *c;
4457c478bd9Sstevel@tonic-gate
4467c478bd9Sstevel@tonic-gate for (i = 0; i < channels_alloc; i++) {
4477c478bd9Sstevel@tonic-gate c = channels[i];
4487c478bd9Sstevel@tonic-gate if (c == NULL)
4497c478bd9Sstevel@tonic-gate continue;
4507c478bd9Sstevel@tonic-gate switch (c->type) {
4517c478bd9Sstevel@tonic-gate case SSH_CHANNEL_X11_LISTENER:
4527c478bd9Sstevel@tonic-gate case SSH_CHANNEL_PORT_LISTENER:
4537c478bd9Sstevel@tonic-gate case SSH_CHANNEL_RPORT_LISTENER:
4547c478bd9Sstevel@tonic-gate case SSH_CHANNEL_CLOSED:
4557c478bd9Sstevel@tonic-gate case SSH_CHANNEL_AUTH_SOCKET:
4567c478bd9Sstevel@tonic-gate case SSH_CHANNEL_DYNAMIC:
4577c478bd9Sstevel@tonic-gate case SSH_CHANNEL_CONNECTING:
4587c478bd9Sstevel@tonic-gate case SSH_CHANNEL_ZOMBIE:
4597c478bd9Sstevel@tonic-gate continue;
4607c478bd9Sstevel@tonic-gate case SSH_CHANNEL_LARVAL:
4617c478bd9Sstevel@tonic-gate if (!compat20)
4627c478bd9Sstevel@tonic-gate fatal("cannot happen: SSH_CHANNEL_LARVAL");
4637c478bd9Sstevel@tonic-gate continue;
4647c478bd9Sstevel@tonic-gate case SSH_CHANNEL_OPENING:
4657c478bd9Sstevel@tonic-gate case SSH_CHANNEL_OPEN:
4667c478bd9Sstevel@tonic-gate case SSH_CHANNEL_X11_OPEN:
4677c478bd9Sstevel@tonic-gate return 1;
4687c478bd9Sstevel@tonic-gate case SSH_CHANNEL_INPUT_DRAINING:
4697c478bd9Sstevel@tonic-gate case SSH_CHANNEL_OUTPUT_DRAINING:
4707c478bd9Sstevel@tonic-gate if (!compat13)
4717c478bd9Sstevel@tonic-gate fatal("cannot happen: OUT_DRAIN");
4727c478bd9Sstevel@tonic-gate return 1;
4737c478bd9Sstevel@tonic-gate default:
4747c478bd9Sstevel@tonic-gate fatal("channel_still_open: bad channel type %d", c->type);
4757c478bd9Sstevel@tonic-gate /* NOTREACHED */
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate return 0;
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate
4817c478bd9Sstevel@tonic-gate /* Returns the id of an open channel suitable for keepaliving */
4827c478bd9Sstevel@tonic-gate
4837c478bd9Sstevel@tonic-gate int
channel_find_open(void)4847c478bd9Sstevel@tonic-gate channel_find_open(void)
4857c478bd9Sstevel@tonic-gate {
4867c478bd9Sstevel@tonic-gate int i;
4877c478bd9Sstevel@tonic-gate Channel *c;
4887c478bd9Sstevel@tonic-gate
4897c478bd9Sstevel@tonic-gate for (i = 0; i < channels_alloc; i++) {
4907c478bd9Sstevel@tonic-gate c = channels[i];
4917c478bd9Sstevel@tonic-gate if (c == NULL)
4927c478bd9Sstevel@tonic-gate continue;
4937c478bd9Sstevel@tonic-gate switch (c->type) {
4947c478bd9Sstevel@tonic-gate case SSH_CHANNEL_CLOSED:
4957c478bd9Sstevel@tonic-gate case SSH_CHANNEL_DYNAMIC:
4967c478bd9Sstevel@tonic-gate case SSH_CHANNEL_X11_LISTENER:
4977c478bd9Sstevel@tonic-gate case SSH_CHANNEL_PORT_LISTENER:
4987c478bd9Sstevel@tonic-gate case SSH_CHANNEL_RPORT_LISTENER:
4997c478bd9Sstevel@tonic-gate case SSH_CHANNEL_OPENING:
5007c478bd9Sstevel@tonic-gate case SSH_CHANNEL_CONNECTING:
5017c478bd9Sstevel@tonic-gate case SSH_CHANNEL_ZOMBIE:
5027c478bd9Sstevel@tonic-gate continue;
5037c478bd9Sstevel@tonic-gate case SSH_CHANNEL_LARVAL:
5047c478bd9Sstevel@tonic-gate case SSH_CHANNEL_AUTH_SOCKET:
5057c478bd9Sstevel@tonic-gate case SSH_CHANNEL_OPEN:
5067c478bd9Sstevel@tonic-gate case SSH_CHANNEL_X11_OPEN:
5077c478bd9Sstevel@tonic-gate return i;
5087c478bd9Sstevel@tonic-gate case SSH_CHANNEL_INPUT_DRAINING:
5097c478bd9Sstevel@tonic-gate case SSH_CHANNEL_OUTPUT_DRAINING:
5107c478bd9Sstevel@tonic-gate if (!compat13)
5117c478bd9Sstevel@tonic-gate fatal("cannot happen: OUT_DRAIN");
5127c478bd9Sstevel@tonic-gate return i;
5137c478bd9Sstevel@tonic-gate default:
5147c478bd9Sstevel@tonic-gate fatal("channel_find_open: bad channel type %d", c->type);
5157c478bd9Sstevel@tonic-gate /* NOTREACHED */
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate return -1;
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate
5217c478bd9Sstevel@tonic-gate
5227c478bd9Sstevel@tonic-gate /*
5237c478bd9Sstevel@tonic-gate * Returns a message describing the currently open forwarded connections,
5247c478bd9Sstevel@tonic-gate * suitable for sending to the client. The message contains crlf pairs for
5257c478bd9Sstevel@tonic-gate * newlines.
5267c478bd9Sstevel@tonic-gate */
5277c478bd9Sstevel@tonic-gate
5287c478bd9Sstevel@tonic-gate char *
channel_open_message(void)5297c478bd9Sstevel@tonic-gate channel_open_message(void)
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate Buffer buffer;
5327c478bd9Sstevel@tonic-gate Channel *c;
5337c478bd9Sstevel@tonic-gate char buf[1024], *cp;
5347c478bd9Sstevel@tonic-gate int i;
5357c478bd9Sstevel@tonic-gate
5367c478bd9Sstevel@tonic-gate buffer_init(&buffer);
5377c478bd9Sstevel@tonic-gate snprintf(buf, sizeof buf, "The following connections are open:\r\n");
5387c478bd9Sstevel@tonic-gate buffer_append(&buffer, buf, strlen(buf));
5397c478bd9Sstevel@tonic-gate for (i = 0; i < channels_alloc; i++) {
5407c478bd9Sstevel@tonic-gate c = channels[i];
5417c478bd9Sstevel@tonic-gate if (c == NULL)
5427c478bd9Sstevel@tonic-gate continue;
5437c478bd9Sstevel@tonic-gate switch (c->type) {
5447c478bd9Sstevel@tonic-gate case SSH_CHANNEL_X11_LISTENER:
5457c478bd9Sstevel@tonic-gate case SSH_CHANNEL_PORT_LISTENER:
5467c478bd9Sstevel@tonic-gate case SSH_CHANNEL_RPORT_LISTENER:
5477c478bd9Sstevel@tonic-gate case SSH_CHANNEL_CLOSED:
5487c478bd9Sstevel@tonic-gate case SSH_CHANNEL_AUTH_SOCKET:
5497c478bd9Sstevel@tonic-gate case SSH_CHANNEL_ZOMBIE:
5507c478bd9Sstevel@tonic-gate continue;
5517c478bd9Sstevel@tonic-gate case SSH_CHANNEL_LARVAL:
5527c478bd9Sstevel@tonic-gate case SSH_CHANNEL_OPENING:
5537c478bd9Sstevel@tonic-gate case SSH_CHANNEL_CONNECTING:
5547c478bd9Sstevel@tonic-gate case SSH_CHANNEL_DYNAMIC:
5557c478bd9Sstevel@tonic-gate case SSH_CHANNEL_OPEN:
5567c478bd9Sstevel@tonic-gate case SSH_CHANNEL_X11_OPEN:
5577c478bd9Sstevel@tonic-gate case SSH_CHANNEL_INPUT_DRAINING:
5587c478bd9Sstevel@tonic-gate case SSH_CHANNEL_OUTPUT_DRAINING:
5597c478bd9Sstevel@tonic-gate snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
5607c478bd9Sstevel@tonic-gate c->self, c->remote_name,
5617c478bd9Sstevel@tonic-gate c->type, c->remote_id,
5627c478bd9Sstevel@tonic-gate c->istate, buffer_len(&c->input),
5637c478bd9Sstevel@tonic-gate c->ostate, buffer_len(&c->output),
5647c478bd9Sstevel@tonic-gate c->rfd, c->wfd);
5657c478bd9Sstevel@tonic-gate buffer_append(&buffer, buf, strlen(buf));
5667c478bd9Sstevel@tonic-gate continue;
5677c478bd9Sstevel@tonic-gate default:
5687c478bd9Sstevel@tonic-gate fatal("channel_open_message: bad channel type %d", c->type);
5697c478bd9Sstevel@tonic-gate /* NOTREACHED */
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate }
5727c478bd9Sstevel@tonic-gate buffer_append(&buffer, "\0", 1);
5737c478bd9Sstevel@tonic-gate cp = xstrdup(buffer_ptr(&buffer));
5747c478bd9Sstevel@tonic-gate buffer_free(&buffer);
5757c478bd9Sstevel@tonic-gate return cp;
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate
5787c478bd9Sstevel@tonic-gate void
channel_send_open(int id)5797c478bd9Sstevel@tonic-gate channel_send_open(int id)
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate Channel *c = channel_lookup(id);
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate if (c == NULL) {
5847c478bd9Sstevel@tonic-gate log("channel_send_open: %d: bad id", id);
5857c478bd9Sstevel@tonic-gate return;
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate debug("send channel open %d", id);
5887c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_CHANNEL_OPEN);
5897c478bd9Sstevel@tonic-gate packet_put_cstring(c->ctype);
5907c478bd9Sstevel@tonic-gate packet_put_int(c->self);
5917c478bd9Sstevel@tonic-gate packet_put_int(c->local_window);
5927c478bd9Sstevel@tonic-gate packet_put_int(c->local_maxpacket);
5937c478bd9Sstevel@tonic-gate packet_send();
5947c478bd9Sstevel@tonic-gate }
5957c478bd9Sstevel@tonic-gate
5967c478bd9Sstevel@tonic-gate void
channel_request_start(int local_id,char * service,int wantconfirm)5977c478bd9Sstevel@tonic-gate channel_request_start(int local_id, char *service, int wantconfirm)
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate Channel *c = channel_lookup(local_id);
6007c478bd9Sstevel@tonic-gate
601*8b0ef7edSZdenek Kotala debug("channel request %d: %s", local_id, service);
6027c478bd9Sstevel@tonic-gate if (c == NULL) {
6037c478bd9Sstevel@tonic-gate log("channel_request_start: %d: unknown channel id", local_id);
6047c478bd9Sstevel@tonic-gate return;
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_CHANNEL_REQUEST);
6077c478bd9Sstevel@tonic-gate packet_put_int(c->remote_id);
6087c478bd9Sstevel@tonic-gate packet_put_cstring(service);
6097c478bd9Sstevel@tonic-gate packet_put_char(wantconfirm);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate void
channel_register_confirm(int id,channel_callback_fn * fn)6127c478bd9Sstevel@tonic-gate channel_register_confirm(int id, channel_callback_fn *fn)
6137c478bd9Sstevel@tonic-gate {
6147c478bd9Sstevel@tonic-gate Channel *c = channel_lookup(id);
6157c478bd9Sstevel@tonic-gate
6167c478bd9Sstevel@tonic-gate if (c == NULL) {
6177c478bd9Sstevel@tonic-gate log("channel_register_comfirm: %d: bad id", id);
6187c478bd9Sstevel@tonic-gate return;
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate c->confirm = fn;
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate void
channel_register_cleanup(int id,channel_callback_fn * fn)6237c478bd9Sstevel@tonic-gate channel_register_cleanup(int id, channel_callback_fn *fn)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate Channel *c = channel_lookup(id);
6267c478bd9Sstevel@tonic-gate
6277c478bd9Sstevel@tonic-gate if (c == NULL) {
6287c478bd9Sstevel@tonic-gate log("channel_register_cleanup: %d: bad id", id);
6297c478bd9Sstevel@tonic-gate return;
6307c478bd9Sstevel@tonic-gate }
6317c478bd9Sstevel@tonic-gate c->detach_user = fn;
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate void
channel_cancel_cleanup(int id)6347c478bd9Sstevel@tonic-gate channel_cancel_cleanup(int id)
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate Channel *c = channel_lookup(id);
6377c478bd9Sstevel@tonic-gate
6387c478bd9Sstevel@tonic-gate if (c == NULL) {
6397c478bd9Sstevel@tonic-gate log("channel_cancel_cleanup: %d: bad id", id);
6407c478bd9Sstevel@tonic-gate return;
6417c478bd9Sstevel@tonic-gate }
6427c478bd9Sstevel@tonic-gate c->detach_user = NULL;
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate void
channel_register_filter(int id,channel_filter_fn * fn)6457c478bd9Sstevel@tonic-gate channel_register_filter(int id, channel_filter_fn *fn)
6467c478bd9Sstevel@tonic-gate {
6477c478bd9Sstevel@tonic-gate Channel *c = channel_lookup(id);
6487c478bd9Sstevel@tonic-gate
6497c478bd9Sstevel@tonic-gate if (c == NULL) {
6507c478bd9Sstevel@tonic-gate log("channel_register_filter: %d: bad id", id);
6517c478bd9Sstevel@tonic-gate return;
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate c->input_filter = fn;
6547c478bd9Sstevel@tonic-gate }
6557c478bd9Sstevel@tonic-gate
6567c478bd9Sstevel@tonic-gate void
channel_set_fds(int id,int rfd,int wfd,int efd,int extusage,int nonblock,u_int window_max)6577c478bd9Sstevel@tonic-gate channel_set_fds(int id, int rfd, int wfd, int efd,
6587c478bd9Sstevel@tonic-gate int extusage, int nonblock, u_int window_max)
6597c478bd9Sstevel@tonic-gate {
6607c478bd9Sstevel@tonic-gate Channel *c = channel_lookup(id);
6617c478bd9Sstevel@tonic-gate
6627c478bd9Sstevel@tonic-gate if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
6637c478bd9Sstevel@tonic-gate fatal("channel_activate for non-larval channel %d.", id);
6647c478bd9Sstevel@tonic-gate channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
6657c478bd9Sstevel@tonic-gate c->type = SSH_CHANNEL_OPEN;
6667c478bd9Sstevel@tonic-gate c->local_window = c->local_window_max = window_max;
6677c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
6687c478bd9Sstevel@tonic-gate packet_put_int(c->remote_id);
6697c478bd9Sstevel@tonic-gate packet_put_int(c->local_window);
6707c478bd9Sstevel@tonic-gate packet_send();
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate
6737c478bd9Sstevel@tonic-gate void
channel_set_wait_for_exit(int id,int wait_for_exit)6747c478bd9Sstevel@tonic-gate channel_set_wait_for_exit(int id, int wait_for_exit)
6757c478bd9Sstevel@tonic-gate {
6767c478bd9Sstevel@tonic-gate Channel *c = channel_lookup(id);
6777c478bd9Sstevel@tonic-gate
6787c478bd9Sstevel@tonic-gate if (c == NULL || c->type != SSH_CHANNEL_OPEN)
6797c478bd9Sstevel@tonic-gate fatal("channel_set_wait_for_exit for non-open channel %d.", id);
6807c478bd9Sstevel@tonic-gate
6817c478bd9Sstevel@tonic-gate debug3("channel_set_wait_for_exit %d, %d (type: %d)", id, wait_for_exit, c->type);
6827c478bd9Sstevel@tonic-gate c->wait_for_exit = wait_for_exit;
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate
6857c478bd9Sstevel@tonic-gate /*
6867c478bd9Sstevel@tonic-gate * 'channel_pre*' are called just before select() to add any bits relevant to
6877c478bd9Sstevel@tonic-gate * channels in the select bitmasks.
6887c478bd9Sstevel@tonic-gate */
6897c478bd9Sstevel@tonic-gate /*
6907c478bd9Sstevel@tonic-gate * 'channel_post*': perform any appropriate operations for channels which
6917c478bd9Sstevel@tonic-gate * have events pending.
6927c478bd9Sstevel@tonic-gate */
6937c478bd9Sstevel@tonic-gate typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
6947c478bd9Sstevel@tonic-gate chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
6957c478bd9Sstevel@tonic-gate chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
6967c478bd9Sstevel@tonic-gate
6977c478bd9Sstevel@tonic-gate static void
channel_pre_listener(Channel * c,fd_set * readset,fd_set * writeset)6987c478bd9Sstevel@tonic-gate channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
6997c478bd9Sstevel@tonic-gate {
7007c478bd9Sstevel@tonic-gate FD_SET(c->sock, readset);
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate
7037c478bd9Sstevel@tonic-gate static void
channel_pre_connecting(Channel * c,fd_set * readset,fd_set * writeset)7047c478bd9Sstevel@tonic-gate channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
7057c478bd9Sstevel@tonic-gate {
7067c478bd9Sstevel@tonic-gate debug3("channel %d: waiting for connection", c->self);
7077c478bd9Sstevel@tonic-gate FD_SET(c->sock, writeset);
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate
7107c478bd9Sstevel@tonic-gate static void
channel_pre_open_13(Channel * c,fd_set * readset,fd_set * writeset)7117c478bd9Sstevel@tonic-gate channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate if (buffer_len(&c->input) < packet_get_maxsize())
7147c478bd9Sstevel@tonic-gate FD_SET(c->sock, readset);
7157c478bd9Sstevel@tonic-gate if (buffer_len(&c->output) > 0)
7167c478bd9Sstevel@tonic-gate FD_SET(c->sock, writeset);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate
7197c478bd9Sstevel@tonic-gate static void
channel_pre_open(Channel * c,fd_set * readset,fd_set * writeset)7207c478bd9Sstevel@tonic-gate channel_pre_open(Channel *c, fd_set * readset, fd_set * writeset)
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
7237c478bd9Sstevel@tonic-gate
7247c478bd9Sstevel@tonic-gate if (c->istate == CHAN_INPUT_OPEN &&
7257c478bd9Sstevel@tonic-gate limit > 0 &&
726d92fc072SZdenek Kotala buffer_len(&c->input) < limit &&
727d92fc072SZdenek Kotala buffer_check_alloc(&c->input, CHAN_RBUF))
7287c478bd9Sstevel@tonic-gate FD_SET(c->rfd, readset);
7297c478bd9Sstevel@tonic-gate if (c->ostate == CHAN_OUTPUT_OPEN ||
7307c478bd9Sstevel@tonic-gate c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
7317c478bd9Sstevel@tonic-gate if (buffer_len(&c->output) > 0) {
7327c478bd9Sstevel@tonic-gate FD_SET(c->wfd, writeset);
7337c478bd9Sstevel@tonic-gate } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
7347c478bd9Sstevel@tonic-gate if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
7357c478bd9Sstevel@tonic-gate debug2("channel %d: obuf_empty delayed efd %d/(%d)",
7367c478bd9Sstevel@tonic-gate c->self, c->efd, buffer_len(&c->extended));
7377c478bd9Sstevel@tonic-gate else
7387c478bd9Sstevel@tonic-gate chan_obuf_empty(c);
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate /** XXX check close conditions, too */
7427c478bd9Sstevel@tonic-gate if (compat20 && c->efd != -1) {
7437c478bd9Sstevel@tonic-gate if (c->extended_usage == CHAN_EXTENDED_WRITE &&
7447c478bd9Sstevel@tonic-gate buffer_len(&c->extended) > 0)
7457c478bd9Sstevel@tonic-gate FD_SET(c->efd, writeset);
7467c478bd9Sstevel@tonic-gate else if (!(c->flags & CHAN_EOF_SENT) &&
7477c478bd9Sstevel@tonic-gate c->extended_usage == CHAN_EXTENDED_READ &&
7487c478bd9Sstevel@tonic-gate buffer_len(&c->extended) < c->remote_window)
7497c478bd9Sstevel@tonic-gate FD_SET(c->efd, readset);
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate
7537c478bd9Sstevel@tonic-gate static void
channel_pre_input_draining(Channel * c,fd_set * readset,fd_set * writeset)7547c478bd9Sstevel@tonic-gate channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate if (buffer_len(&c->input) == 0) {
7577c478bd9Sstevel@tonic-gate packet_start(SSH_MSG_CHANNEL_CLOSE);
7587c478bd9Sstevel@tonic-gate packet_put_int(c->remote_id);
7597c478bd9Sstevel@tonic-gate packet_send();
7607c478bd9Sstevel@tonic-gate c->type = SSH_CHANNEL_CLOSED;
7617c478bd9Sstevel@tonic-gate debug("channel %d: closing after input drain.", c->self);
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate
7657c478bd9Sstevel@tonic-gate static void
channel_pre_output_draining(Channel * c,fd_set * readset,fd_set * writeset)7667c478bd9Sstevel@tonic-gate channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate if (buffer_len(&c->output) == 0)
7697c478bd9Sstevel@tonic-gate chan_mark_dead(c);
7707c478bd9Sstevel@tonic-gate else
7717c478bd9Sstevel@tonic-gate FD_SET(c->sock, writeset);
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate
7747c478bd9Sstevel@tonic-gate /*
7757c478bd9Sstevel@tonic-gate * This is a special state for X11 authentication spoofing. An opened X11
7767c478bd9Sstevel@tonic-gate * connection (when authentication spoofing is being done) remains in this
7777c478bd9Sstevel@tonic-gate * state until the first packet has been completely read. The authentication
7787c478bd9Sstevel@tonic-gate * data in that packet is then substituted by the real data if it matches the
7797c478bd9Sstevel@tonic-gate * fake data, and the channel is put into normal mode.
7807c478bd9Sstevel@tonic-gate * XXX All this happens at the client side.
7817c478bd9Sstevel@tonic-gate * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
7827c478bd9Sstevel@tonic-gate */
7837c478bd9Sstevel@tonic-gate static int
x11_open_helper(Buffer * b)7847c478bd9Sstevel@tonic-gate x11_open_helper(Buffer *b)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate u_char *ucp;
7877c478bd9Sstevel@tonic-gate u_int proto_len, data_len;
7887c478bd9Sstevel@tonic-gate
7897c478bd9Sstevel@tonic-gate /* Check if the fixed size part of the packet is in buffer. */
7907c478bd9Sstevel@tonic-gate if (buffer_len(b) < 12)
7917c478bd9Sstevel@tonic-gate return 0;
7927c478bd9Sstevel@tonic-gate
7937c478bd9Sstevel@tonic-gate /* Parse the lengths of variable-length fields. */
7947c478bd9Sstevel@tonic-gate ucp = buffer_ptr(b);
7957c478bd9Sstevel@tonic-gate if (ucp[0] == 0x42) { /* Byte order MSB first. */
7967c478bd9Sstevel@tonic-gate proto_len = 256 * ucp[6] + ucp[7];
7977c478bd9Sstevel@tonic-gate data_len = 256 * ucp[8] + ucp[9];
7987c478bd9Sstevel@tonic-gate } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
7997c478bd9Sstevel@tonic-gate proto_len = ucp[6] + 256 * ucp[7];
8007c478bd9Sstevel@tonic-gate data_len = ucp[8] + 256 * ucp[9];
8017c478bd9Sstevel@tonic-gate } else {
8027c478bd9Sstevel@tonic-gate debug("Initial X11 packet contains bad byte order byte: 0x%x",
8037c478bd9Sstevel@tonic-gate ucp[0]);
8047c478bd9Sstevel@tonic-gate return -1;
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate
8077c478bd9Sstevel@tonic-gate /* Check if the whole packet is in buffer. */
8087c478bd9Sstevel@tonic-gate if (buffer_len(b) <
8097c478bd9Sstevel@tonic-gate 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
8107c478bd9Sstevel@tonic-gate return 0;
8117c478bd9Sstevel@tonic-gate
8127c478bd9Sstevel@tonic-gate /* Check if authentication protocol matches. */
8137c478bd9Sstevel@tonic-gate if (proto_len != strlen(x11_saved_proto) ||
8147c478bd9Sstevel@tonic-gate memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
8157c478bd9Sstevel@tonic-gate debug("X11 connection uses different authentication protocol.");
8167c478bd9Sstevel@tonic-gate return -1;
8177c478bd9Sstevel@tonic-gate }
8187c478bd9Sstevel@tonic-gate /* Check if authentication data matches our fake data. */
8197c478bd9Sstevel@tonic-gate if (data_len != x11_fake_data_len ||
8207c478bd9Sstevel@tonic-gate memcmp(ucp + 12 + ((proto_len + 3) & ~3),
8217c478bd9Sstevel@tonic-gate x11_fake_data, x11_fake_data_len) != 0) {
8227c478bd9Sstevel@tonic-gate debug("X11 auth data does not match fake data.");
8237c478bd9Sstevel@tonic-gate return -1;
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate /* Check fake data length */
8267c478bd9Sstevel@tonic-gate if (x11_fake_data_len != x11_saved_data_len) {
8277c478bd9Sstevel@tonic-gate error("X11 fake_data_len %d != saved_data_len %d",
8287c478bd9Sstevel@tonic-gate x11_fake_data_len, x11_saved_data_len);
8297c478bd9Sstevel@tonic-gate return -1;
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate /*
8327c478bd9Sstevel@tonic-gate * Received authentication protocol and data match
8337c478bd9Sstevel@tonic-gate * our fake data. Substitute the fake data with real
8347c478bd9Sstevel@tonic-gate * data.
8357c478bd9Sstevel@tonic-gate */
8367c478bd9Sstevel@tonic-gate memcpy(ucp + 12 + ((proto_len + 3) & ~3),
8377c478bd9Sstevel@tonic-gate x11_saved_data, x11_saved_data_len);
8387c478bd9Sstevel@tonic-gate return 1;
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate
8417c478bd9Sstevel@tonic-gate static void
channel_pre_x11_open_13(Channel * c,fd_set * readset,fd_set * writeset)8427c478bd9Sstevel@tonic-gate channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
8437c478bd9Sstevel@tonic-gate {
8447c478bd9Sstevel@tonic-gate int ret = x11_open_helper(&c->output);
8457c478bd9Sstevel@tonic-gate
8467c478bd9Sstevel@tonic-gate if (ret == 1) {
8477c478bd9Sstevel@tonic-gate /* Start normal processing for the channel. */
8487c478bd9Sstevel@tonic-gate c->type = SSH_CHANNEL_OPEN;
8497c478bd9Sstevel@tonic-gate channel_pre_open_13(c, readset, writeset);
8507c478bd9Sstevel@tonic-gate } else if (ret == -1) {
8517c478bd9Sstevel@tonic-gate /*
8527c478bd9Sstevel@tonic-gate * We have received an X11 connection that has bad
8537c478bd9Sstevel@tonic-gate * authentication information.
8547c478bd9Sstevel@tonic-gate */
8557c478bd9Sstevel@tonic-gate log("X11 connection rejected because of wrong authentication.");
8567c478bd9Sstevel@tonic-gate buffer_clear(&c->input);
8577c478bd9Sstevel@tonic-gate buffer_clear(&c->output);
8587c478bd9Sstevel@tonic-gate channel_close_fd(&c->sock);
8597c478bd9Sstevel@tonic-gate c->sock = -1;
8607c478bd9Sstevel@tonic-gate c->type = SSH_CHANNEL_CLOSED;
8617c478bd9Sstevel@tonic-gate packet_start(SSH_MSG_CHANNEL_CLOSE);
8627c478bd9Sstevel@tonic-gate packet_put_int(c->remote_id);
8637c478bd9Sstevel@tonic-gate packet_send();
8647c478bd9Sstevel@tonic-gate }
8657c478bd9Sstevel@tonic-gate }
8667c478bd9Sstevel@tonic-gate
8677c478bd9Sstevel@tonic-gate static void
channel_pre_x11_open(Channel * c,fd_set * readset,fd_set * writeset)8687c478bd9Sstevel@tonic-gate channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate int ret = x11_open_helper(&c->output);
8717c478bd9Sstevel@tonic-gate
8727c478bd9Sstevel@tonic-gate /* c->force_drain = 1; */
8737c478bd9Sstevel@tonic-gate
8747c478bd9Sstevel@tonic-gate if (ret == 1) {
8757c478bd9Sstevel@tonic-gate c->type = SSH_CHANNEL_OPEN;
8767c478bd9Sstevel@tonic-gate channel_pre_open(c, readset, writeset);
8777c478bd9Sstevel@tonic-gate } else if (ret == -1) {
8787c478bd9Sstevel@tonic-gate log("X11 connection rejected because of wrong authentication.");
8797c478bd9Sstevel@tonic-gate debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
8807c478bd9Sstevel@tonic-gate chan_read_failed(c);
8817c478bd9Sstevel@tonic-gate buffer_clear(&c->input);
8827c478bd9Sstevel@tonic-gate chan_ibuf_empty(c);
8837c478bd9Sstevel@tonic-gate buffer_clear(&c->output);
8847c478bd9Sstevel@tonic-gate /* for proto v1, the peer will send an IEOF */
8857c478bd9Sstevel@tonic-gate if (compat20)
8867c478bd9Sstevel@tonic-gate chan_write_failed(c);
8877c478bd9Sstevel@tonic-gate else
8887c478bd9Sstevel@tonic-gate c->type = SSH_CHANNEL_OPEN;
8897c478bd9Sstevel@tonic-gate debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate
8937c478bd9Sstevel@tonic-gate /* try to decode a socks4 header */
8947c478bd9Sstevel@tonic-gate static int
channel_decode_socks4(Channel * c,fd_set * readset,fd_set * writeset)8957c478bd9Sstevel@tonic-gate channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
8967c478bd9Sstevel@tonic-gate {
8977c478bd9Sstevel@tonic-gate char *p, *host;
8987c478bd9Sstevel@tonic-gate int len, have, i, found;
8997c478bd9Sstevel@tonic-gate char username[256];
9007c478bd9Sstevel@tonic-gate struct {
9017c478bd9Sstevel@tonic-gate u_int8_t version;
9027c478bd9Sstevel@tonic-gate u_int8_t command;
9037c478bd9Sstevel@tonic-gate u_int16_t dest_port;
9047c478bd9Sstevel@tonic-gate struct in_addr dest_addr;
9057c478bd9Sstevel@tonic-gate } s4_req, s4_rsp;
9067c478bd9Sstevel@tonic-gate
9077c478bd9Sstevel@tonic-gate debug2("channel %d: decode socks4", c->self);
9087c478bd9Sstevel@tonic-gate
9097c478bd9Sstevel@tonic-gate have = buffer_len(&c->input);
9107c478bd9Sstevel@tonic-gate len = sizeof(s4_req);
9117c478bd9Sstevel@tonic-gate if (have < len)
9127c478bd9Sstevel@tonic-gate return 0;
9137c478bd9Sstevel@tonic-gate p = buffer_ptr(&c->input);
9147c478bd9Sstevel@tonic-gate for (found = 0, i = len; i < have; i++) {
9157c478bd9Sstevel@tonic-gate if (p[i] == '\0') {
9167c478bd9Sstevel@tonic-gate found = 1;
9177c478bd9Sstevel@tonic-gate break;
9187c478bd9Sstevel@tonic-gate }
9197c478bd9Sstevel@tonic-gate if (i > 1024) {
9207c478bd9Sstevel@tonic-gate /* the peer is probably sending garbage */
9217c478bd9Sstevel@tonic-gate debug("channel %d: decode socks4: too long",
9227c478bd9Sstevel@tonic-gate c->self);
9237c478bd9Sstevel@tonic-gate return -1;
9247c478bd9Sstevel@tonic-gate }
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate if (!found)
9277c478bd9Sstevel@tonic-gate return 0;
9287c478bd9Sstevel@tonic-gate buffer_get(&c->input, (char *)&s4_req.version, 1);
9297c478bd9Sstevel@tonic-gate buffer_get(&c->input, (char *)&s4_req.command, 1);
9307c478bd9Sstevel@tonic-gate buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
9317c478bd9Sstevel@tonic-gate buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
9327c478bd9Sstevel@tonic-gate have = buffer_len(&c->input);
9337c478bd9Sstevel@tonic-gate p = buffer_ptr(&c->input);
9347c478bd9Sstevel@tonic-gate len = strlen(p);
9357c478bd9Sstevel@tonic-gate debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
9367c478bd9Sstevel@tonic-gate if (len > have)
9377c478bd9Sstevel@tonic-gate fatal("channel %d: decode socks4: len %d > have %d",
9387c478bd9Sstevel@tonic-gate c->self, len, have);
9397c478bd9Sstevel@tonic-gate strlcpy(username, p, sizeof(username));
9407c478bd9Sstevel@tonic-gate buffer_consume(&c->input, len);
9417c478bd9Sstevel@tonic-gate buffer_consume(&c->input, 1); /* trailing '\0' */
9427c478bd9Sstevel@tonic-gate
9437c478bd9Sstevel@tonic-gate host = inet_ntoa(s4_req.dest_addr);
9447c478bd9Sstevel@tonic-gate strlcpy(c->path, host, sizeof(c->path));
9457c478bd9Sstevel@tonic-gate c->host_port = ntohs(s4_req.dest_port);
9467c478bd9Sstevel@tonic-gate
9477c478bd9Sstevel@tonic-gate debug("channel %d: dynamic request: socks4 host %s port %u command %u",
9487c478bd9Sstevel@tonic-gate c->self, host, c->host_port, s4_req.command);
9497c478bd9Sstevel@tonic-gate
9507c478bd9Sstevel@tonic-gate if (s4_req.command != 1) {
9517c478bd9Sstevel@tonic-gate debug("channel %d: cannot handle: socks4 cn %d",
9527c478bd9Sstevel@tonic-gate c->self, s4_req.command);
9537c478bd9Sstevel@tonic-gate return -1;
9547c478bd9Sstevel@tonic-gate }
9557c478bd9Sstevel@tonic-gate s4_rsp.version = 0; /* vn: 0 for reply */
9567c478bd9Sstevel@tonic-gate s4_rsp.command = 90; /* cd: req granted */
9577c478bd9Sstevel@tonic-gate s4_rsp.dest_port = 0; /* ignored */
9587c478bd9Sstevel@tonic-gate s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
9597c478bd9Sstevel@tonic-gate buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
9607c478bd9Sstevel@tonic-gate return 1;
9617c478bd9Sstevel@tonic-gate }
9627c478bd9Sstevel@tonic-gate
963d80e6060Sjp161948 /* try to decode a socks5 header */
964d80e6060Sjp161948 #define SSH_SOCKS5_AUTHDONE 0x1000
965d80e6060Sjp161948 #define SSH_SOCKS5_NOAUTH 0x00
966d80e6060Sjp161948 #define SSH_SOCKS5_IPV4 0x01
967d80e6060Sjp161948 #define SSH_SOCKS5_DOMAIN 0x03
968d80e6060Sjp161948 #define SSH_SOCKS5_IPV6 0x04
969d80e6060Sjp161948 #define SSH_SOCKS5_CONNECT 0x01
970d80e6060Sjp161948 #define SSH_SOCKS5_SUCCESS 0x00
971d80e6060Sjp161948
972d80e6060Sjp161948 /* ARGSUSED */
973d80e6060Sjp161948 static int
channel_decode_socks5(Channel * c,fd_set * readset,fd_set * writeset)974d80e6060Sjp161948 channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
975d80e6060Sjp161948 {
976d80e6060Sjp161948 struct {
977d80e6060Sjp161948 u_int8_t version;
978d80e6060Sjp161948 u_int8_t command;
979d80e6060Sjp161948 u_int8_t reserved;
980d80e6060Sjp161948 u_int8_t atyp;
981d80e6060Sjp161948 } s5_req, s5_rsp;
982d80e6060Sjp161948 u_int16_t dest_port;
983d80e6060Sjp161948 u_char *p, dest_addr[255+1];
984d80e6060Sjp161948 u_int have, need, i, found, nmethods, addrlen;
985d80e6060Sjp161948 struct in_addr bnd_addr;
986d80e6060Sjp161948 int af;
987d80e6060Sjp161948
988d80e6060Sjp161948 debug2("channel %d: decode socks5", c->self);
989d80e6060Sjp161948 p = buffer_ptr(&c->input);
990d80e6060Sjp161948 if (p[0] != 0x05)
991d80e6060Sjp161948 return -1;
992d80e6060Sjp161948 have = buffer_len(&c->input);
993d80e6060Sjp161948 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
994d80e6060Sjp161948 /* format: ver | nmethods | methods */
995d80e6060Sjp161948 if (have < 2)
996d80e6060Sjp161948 return 0;
997d80e6060Sjp161948 nmethods = p[1];
998d80e6060Sjp161948 if (have < nmethods + 2)
999d80e6060Sjp161948 return 0;
1000d80e6060Sjp161948 /* look for method: "NO AUTHENTICATION REQUIRED" */
1001d80e6060Sjp161948 for (found = 0, i = 2 ; i < nmethods + 2; i++) {
1002d80e6060Sjp161948 if (p[i] == SSH_SOCKS5_NOAUTH) {
1003d80e6060Sjp161948 found = 1;
1004d80e6060Sjp161948 break;
1005d80e6060Sjp161948 }
1006d80e6060Sjp161948 }
1007d80e6060Sjp161948 if (!found) {
1008d80e6060Sjp161948 error("channel %d: socks5 authentication methods not implemented",
1009d80e6060Sjp161948 c->self);
1010d80e6060Sjp161948 error("channel %d: forwarding failed: "
1011d80e6060Sjp161948 "SSH_SOCKS5_NOAUTH method not found", c->self);
1012d80e6060Sjp161948 return -1;
1013d80e6060Sjp161948 }
1014d80e6060Sjp161948 buffer_consume(&c->input, nmethods + 2);
1015d80e6060Sjp161948 buffer_put_char(&c->output, 0x05); /* version */
1016d80e6060Sjp161948 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1017d80e6060Sjp161948 FD_SET(c->sock, writeset);
1018d80e6060Sjp161948 c->flags |= SSH_SOCKS5_AUTHDONE;
1019d80e6060Sjp161948 debug2("channel %d: socks5 auth done", c->self);
1020d80e6060Sjp161948 return 0; /* need more */
1021d80e6060Sjp161948 }
1022d80e6060Sjp161948 debug2("channel %d: socks5 post auth", c->self);
1023d80e6060Sjp161948 if (have < sizeof(s5_req)+1)
1024d80e6060Sjp161948 return 0; /* need more */
1025d80e6060Sjp161948 memcpy(&s5_req, p, sizeof(s5_req));
1026d80e6060Sjp161948 if (s5_req.version != 0x05 ||
1027d80e6060Sjp161948 s5_req.command != SSH_SOCKS5_CONNECT ||
1028d80e6060Sjp161948 s5_req.reserved != 0x00) {
1029d80e6060Sjp161948 error("channel %d: forwarding failed: "
1030d80e6060Sjp161948 "only socks5 connect is supported", c->self);
1031d80e6060Sjp161948 return -1;
1032d80e6060Sjp161948 }
1033d80e6060Sjp161948 switch (s5_req.atyp){
1034d80e6060Sjp161948 case SSH_SOCKS5_IPV4:
1035d80e6060Sjp161948 addrlen = 4;
1036d80e6060Sjp161948 af = AF_INET;
1037d80e6060Sjp161948 break;
1038d80e6060Sjp161948 case SSH_SOCKS5_DOMAIN:
1039d80e6060Sjp161948 addrlen = p[sizeof(s5_req)];
1040d80e6060Sjp161948 af = -1;
1041d80e6060Sjp161948 break;
1042d80e6060Sjp161948 case SSH_SOCKS5_IPV6:
1043d80e6060Sjp161948 addrlen = 16;
1044d80e6060Sjp161948 af = AF_INET6;
1045d80e6060Sjp161948 break;
1046d80e6060Sjp161948 default:
1047d80e6060Sjp161948 error("channel %d: forwarding failed: "
1048d80e6060Sjp161948 "bad socks5 atyp %d", c->self, s5_req.atyp);
1049d80e6060Sjp161948 return -1;
1050d80e6060Sjp161948 }
1051d80e6060Sjp161948 need = sizeof(s5_req) + addrlen + 2;
1052d80e6060Sjp161948 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1053d80e6060Sjp161948 need++;
1054d80e6060Sjp161948 if (have < need)
1055d80e6060Sjp161948 return 0;
1056d80e6060Sjp161948 buffer_consume(&c->input, sizeof(s5_req));
1057d80e6060Sjp161948 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1058d80e6060Sjp161948 buffer_consume(&c->input, 1); /* host string length */
1059d80e6060Sjp161948 buffer_get(&c->input, (char *)&dest_addr, addrlen);
1060d80e6060Sjp161948 buffer_get(&c->input, (char *)&dest_port, 2);
1061d80e6060Sjp161948 dest_addr[addrlen] = '\0';
1062d80e6060Sjp161948 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1063d80e6060Sjp161948 strlcpy(c->path, (char *)dest_addr, sizeof(c->path));
1064d80e6060Sjp161948 else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL)
1065d80e6060Sjp161948 return -1;
1066d80e6060Sjp161948 c->host_port = ntohs(dest_port);
1067d80e6060Sjp161948
1068d80e6060Sjp161948 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
1069d80e6060Sjp161948 c->self, c->path, c->host_port, s5_req.command);
1070d80e6060Sjp161948
1071d80e6060Sjp161948 s5_rsp.version = 0x05;
1072d80e6060Sjp161948 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1073d80e6060Sjp161948 s5_rsp.reserved = 0; /* ignored */
1074d80e6060Sjp161948 s5_rsp.atyp = SSH_SOCKS5_IPV4;
1075d80e6060Sjp161948 bzero(&bnd_addr, sizeof(bnd_addr));
1076d80e6060Sjp161948 bnd_addr.s_addr = htonl(INADDR_ANY);
1077d80e6060Sjp161948 dest_port = 0; /* ignored */
1078d80e6060Sjp161948
1079d80e6060Sjp161948 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
1080d80e6060Sjp161948 buffer_append(&c->output, &bnd_addr, sizeof(struct in_addr));
1081d80e6060Sjp161948 buffer_append(&c->output, &dest_port, sizeof(dest_port));
1082d80e6060Sjp161948 return 1;
1083d80e6060Sjp161948 }
1084d80e6060Sjp161948
10857c478bd9Sstevel@tonic-gate /* dynamic port forwarding */
10867c478bd9Sstevel@tonic-gate static void
channel_pre_dynamic(Channel * c,fd_set * readset,fd_set * writeset)10877c478bd9Sstevel@tonic-gate channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
10887c478bd9Sstevel@tonic-gate {
10897c478bd9Sstevel@tonic-gate u_char *p;
10907c478bd9Sstevel@tonic-gate int have, ret;
10917c478bd9Sstevel@tonic-gate
10927c478bd9Sstevel@tonic-gate have = buffer_len(&c->input);
10937c478bd9Sstevel@tonic-gate debug2("channel %d: pre_dynamic: have %d", c->self, have);
10947c478bd9Sstevel@tonic-gate /* buffer_dump(&c->input); */
10957c478bd9Sstevel@tonic-gate /* check if the fixed size part of the packet is in buffer. */
1096d80e6060Sjp161948 if (have < 3) {
10977c478bd9Sstevel@tonic-gate /* need more */
10987c478bd9Sstevel@tonic-gate FD_SET(c->sock, readset);
10997c478bd9Sstevel@tonic-gate return;
11007c478bd9Sstevel@tonic-gate }
11017c478bd9Sstevel@tonic-gate /* try to guess the protocol */
11027c478bd9Sstevel@tonic-gate p = buffer_ptr(&c->input);
11037c478bd9Sstevel@tonic-gate switch (p[0]) {
11047c478bd9Sstevel@tonic-gate case 0x04:
11057c478bd9Sstevel@tonic-gate ret = channel_decode_socks4(c, readset, writeset);
11067c478bd9Sstevel@tonic-gate break;
1107d80e6060Sjp161948 case 0x05:
1108d80e6060Sjp161948 ret = channel_decode_socks5(c, readset, writeset);
1109d80e6060Sjp161948 break;
11107c478bd9Sstevel@tonic-gate default:
1111d80e6060Sjp161948 error("channel %d: forwarding failed: unknown socks "
1112d80e6060Sjp161948 "version 0x%02X", c->self, p[0]);
11137c478bd9Sstevel@tonic-gate ret = -1;
11147c478bd9Sstevel@tonic-gate break;
11157c478bd9Sstevel@tonic-gate }
11167c478bd9Sstevel@tonic-gate if (ret < 0) {
11177c478bd9Sstevel@tonic-gate chan_mark_dead(c);
11187c478bd9Sstevel@tonic-gate } else if (ret == 0) {
11197c478bd9Sstevel@tonic-gate debug2("channel %d: pre_dynamic: need more", c->self);
11207c478bd9Sstevel@tonic-gate /* need more */
11217c478bd9Sstevel@tonic-gate FD_SET(c->sock, readset);
11227c478bd9Sstevel@tonic-gate } else {
11237c478bd9Sstevel@tonic-gate /* switch to the next state */
11247c478bd9Sstevel@tonic-gate c->type = SSH_CHANNEL_OPENING;
11257c478bd9Sstevel@tonic-gate port_open_helper(c, "direct-tcpip");
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate }
11287c478bd9Sstevel@tonic-gate
11297c478bd9Sstevel@tonic-gate /* This is our fake X11 server socket. */
11307c478bd9Sstevel@tonic-gate static void
channel_post_x11_listener(Channel * c,fd_set * readset,fd_set * writeset)11317c478bd9Sstevel@tonic-gate channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
11327c478bd9Sstevel@tonic-gate {
11337c478bd9Sstevel@tonic-gate Channel *nc;
11347c478bd9Sstevel@tonic-gate struct sockaddr addr;
11357c478bd9Sstevel@tonic-gate int newsock;
11367c478bd9Sstevel@tonic-gate socklen_t addrlen;
11377c478bd9Sstevel@tonic-gate char buf[16384], *remote_ipaddr;
11387c478bd9Sstevel@tonic-gate int remote_port;
11397c478bd9Sstevel@tonic-gate
11407c478bd9Sstevel@tonic-gate if (FD_ISSET(c->sock, readset)) {
11417c478bd9Sstevel@tonic-gate debug("X11 connection requested.");
11427c478bd9Sstevel@tonic-gate addrlen = sizeof(addr);
11437c478bd9Sstevel@tonic-gate newsock = accept(c->sock, &addr, &addrlen);
11447c478bd9Sstevel@tonic-gate if (c->single_connection) {
11457c478bd9Sstevel@tonic-gate debug("single_connection: closing X11 listener.");
11467c478bd9Sstevel@tonic-gate channel_close_fd(&c->sock);
11477c478bd9Sstevel@tonic-gate chan_mark_dead(c);
11487c478bd9Sstevel@tonic-gate }
11497c478bd9Sstevel@tonic-gate if (newsock < 0) {
11507c478bd9Sstevel@tonic-gate error("accept: %.100s", strerror(errno));
11517c478bd9Sstevel@tonic-gate return;
11527c478bd9Sstevel@tonic-gate }
11537c478bd9Sstevel@tonic-gate set_nodelay(newsock);
11547c478bd9Sstevel@tonic-gate remote_ipaddr = get_peer_ipaddr(newsock);
11557c478bd9Sstevel@tonic-gate remote_port = get_peer_port(newsock);
11567c478bd9Sstevel@tonic-gate snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
11577c478bd9Sstevel@tonic-gate remote_ipaddr, remote_port);
11587c478bd9Sstevel@tonic-gate
11597c478bd9Sstevel@tonic-gate nc = channel_new("accepted x11 socket",
11607c478bd9Sstevel@tonic-gate SSH_CHANNEL_OPENING, newsock, newsock, -1,
11617c478bd9Sstevel@tonic-gate c->local_window_max, c->local_maxpacket,
11627c478bd9Sstevel@tonic-gate 0, xstrdup(buf), 1);
11637c478bd9Sstevel@tonic-gate if (compat20) {
11647c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_CHANNEL_OPEN);
11657c478bd9Sstevel@tonic-gate packet_put_cstring("x11");
11667c478bd9Sstevel@tonic-gate packet_put_int(nc->self);
11677c478bd9Sstevel@tonic-gate packet_put_int(nc->local_window_max);
11687c478bd9Sstevel@tonic-gate packet_put_int(nc->local_maxpacket);
11697c478bd9Sstevel@tonic-gate /* originator ipaddr and port */
11707c478bd9Sstevel@tonic-gate packet_put_cstring(remote_ipaddr);
11717c478bd9Sstevel@tonic-gate if (datafellows & SSH_BUG_X11FWD) {
11727c478bd9Sstevel@tonic-gate debug("ssh2 x11 bug compat mode");
11737c478bd9Sstevel@tonic-gate } else {
11747c478bd9Sstevel@tonic-gate packet_put_int(remote_port);
11757c478bd9Sstevel@tonic-gate }
11767c478bd9Sstevel@tonic-gate packet_send();
11777c478bd9Sstevel@tonic-gate } else {
11787c478bd9Sstevel@tonic-gate packet_start(SSH_SMSG_X11_OPEN);
11797c478bd9Sstevel@tonic-gate packet_put_int(nc->self);
11807c478bd9Sstevel@tonic-gate if (packet_get_protocol_flags() &
11817c478bd9Sstevel@tonic-gate SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
11827c478bd9Sstevel@tonic-gate packet_put_cstring(buf);
11837c478bd9Sstevel@tonic-gate packet_send();
11847c478bd9Sstevel@tonic-gate }
11857c478bd9Sstevel@tonic-gate xfree(remote_ipaddr);
11867c478bd9Sstevel@tonic-gate }
11877c478bd9Sstevel@tonic-gate }
11887c478bd9Sstevel@tonic-gate
11897c478bd9Sstevel@tonic-gate static void
port_open_helper(Channel * c,char * rtype)11907c478bd9Sstevel@tonic-gate port_open_helper(Channel *c, char *rtype)
11917c478bd9Sstevel@tonic-gate {
11927c478bd9Sstevel@tonic-gate int direct;
11937c478bd9Sstevel@tonic-gate char buf[1024];
11947c478bd9Sstevel@tonic-gate char *remote_ipaddr = get_peer_ipaddr(c->sock);
11957c478bd9Sstevel@tonic-gate u_short remote_port = get_peer_port(c->sock);
11967c478bd9Sstevel@tonic-gate
11977c478bd9Sstevel@tonic-gate direct = (strcmp(rtype, "direct-tcpip") == 0);
11987c478bd9Sstevel@tonic-gate
11997c478bd9Sstevel@tonic-gate snprintf(buf, sizeof buf,
12007c478bd9Sstevel@tonic-gate "%s: listening port %d for %.100s port %d, "
12017c478bd9Sstevel@tonic-gate "connect from %.200s port %d",
12027c478bd9Sstevel@tonic-gate rtype, c->listening_port, c->path, c->host_port,
12037c478bd9Sstevel@tonic-gate remote_ipaddr, remote_port);
12047c478bd9Sstevel@tonic-gate
12057c478bd9Sstevel@tonic-gate xfree(c->remote_name);
12067c478bd9Sstevel@tonic-gate c->remote_name = xstrdup(buf);
12077c478bd9Sstevel@tonic-gate
12087c478bd9Sstevel@tonic-gate if (compat20) {
12097c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_CHANNEL_OPEN);
12107c478bd9Sstevel@tonic-gate packet_put_cstring(rtype);
12117c478bd9Sstevel@tonic-gate packet_put_int(c->self);
12127c478bd9Sstevel@tonic-gate packet_put_int(c->local_window_max);
12137c478bd9Sstevel@tonic-gate packet_put_int(c->local_maxpacket);
12147c478bd9Sstevel@tonic-gate if (direct) {
12157c478bd9Sstevel@tonic-gate /* target host, port */
12167c478bd9Sstevel@tonic-gate packet_put_cstring(c->path);
12177c478bd9Sstevel@tonic-gate packet_put_int(c->host_port);
12187c478bd9Sstevel@tonic-gate } else {
12197c478bd9Sstevel@tonic-gate /* listen address, port */
12207c478bd9Sstevel@tonic-gate packet_put_cstring(c->path);
12217c478bd9Sstevel@tonic-gate packet_put_int(c->listening_port);
12227c478bd9Sstevel@tonic-gate }
12237c478bd9Sstevel@tonic-gate /* originator host and port */
12247c478bd9Sstevel@tonic-gate packet_put_cstring(remote_ipaddr);
12257c478bd9Sstevel@tonic-gate packet_put_int(remote_port);
12267c478bd9Sstevel@tonic-gate packet_send();
12277c478bd9Sstevel@tonic-gate } else {
12287c478bd9Sstevel@tonic-gate packet_start(SSH_MSG_PORT_OPEN);
12297c478bd9Sstevel@tonic-gate packet_put_int(c->self);
12307c478bd9Sstevel@tonic-gate packet_put_cstring(c->path);
12317c478bd9Sstevel@tonic-gate packet_put_int(c->host_port);
12327c478bd9Sstevel@tonic-gate if (packet_get_protocol_flags() &
12337c478bd9Sstevel@tonic-gate SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
12347c478bd9Sstevel@tonic-gate packet_put_cstring(c->remote_name);
12357c478bd9Sstevel@tonic-gate packet_send();
12367c478bd9Sstevel@tonic-gate }
12377c478bd9Sstevel@tonic-gate xfree(remote_ipaddr);
12387c478bd9Sstevel@tonic-gate }
12397c478bd9Sstevel@tonic-gate
12407c478bd9Sstevel@tonic-gate /*
12417c478bd9Sstevel@tonic-gate * This socket is listening for connections to a forwarded TCP/IP port.
12427c478bd9Sstevel@tonic-gate */
12437c478bd9Sstevel@tonic-gate static void
channel_post_port_listener(Channel * c,fd_set * readset,fd_set * writeset)12447c478bd9Sstevel@tonic-gate channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
12457c478bd9Sstevel@tonic-gate {
12467c478bd9Sstevel@tonic-gate Channel *nc;
12477c478bd9Sstevel@tonic-gate struct sockaddr addr;
12487c478bd9Sstevel@tonic-gate int newsock, nextstate;
12497c478bd9Sstevel@tonic-gate socklen_t addrlen;
12507c478bd9Sstevel@tonic-gate char *rtype;
12517c478bd9Sstevel@tonic-gate
12527c478bd9Sstevel@tonic-gate if (FD_ISSET(c->sock, readset)) {
12537c478bd9Sstevel@tonic-gate debug("Connection to port %d forwarding "
12547c478bd9Sstevel@tonic-gate "to %.100s port %d requested.",
12557c478bd9Sstevel@tonic-gate c->listening_port, c->path, c->host_port);
12567c478bd9Sstevel@tonic-gate
12577c478bd9Sstevel@tonic-gate if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
12587c478bd9Sstevel@tonic-gate nextstate = SSH_CHANNEL_OPENING;
12597c478bd9Sstevel@tonic-gate rtype = "forwarded-tcpip";
12607c478bd9Sstevel@tonic-gate } else {
12617c478bd9Sstevel@tonic-gate if (c->host_port == 0) {
12627c478bd9Sstevel@tonic-gate nextstate = SSH_CHANNEL_DYNAMIC;
12637c478bd9Sstevel@tonic-gate rtype = "dynamic-tcpip";
12647c478bd9Sstevel@tonic-gate } else {
12657c478bd9Sstevel@tonic-gate nextstate = SSH_CHANNEL_OPENING;
12667c478bd9Sstevel@tonic-gate rtype = "direct-tcpip";
12677c478bd9Sstevel@tonic-gate }
12687c478bd9Sstevel@tonic-gate }
12697c478bd9Sstevel@tonic-gate
12707c478bd9Sstevel@tonic-gate addrlen = sizeof(addr);
12717c478bd9Sstevel@tonic-gate newsock = accept(c->sock, &addr, &addrlen);
12727c478bd9Sstevel@tonic-gate if (newsock < 0) {
12737c478bd9Sstevel@tonic-gate error("accept: %.100s", strerror(errno));
12747c478bd9Sstevel@tonic-gate return;
12757c478bd9Sstevel@tonic-gate }
12767c478bd9Sstevel@tonic-gate set_nodelay(newsock);
12777c478bd9Sstevel@tonic-gate nc = channel_new(rtype,
12787c478bd9Sstevel@tonic-gate nextstate, newsock, newsock, -1,
12797c478bd9Sstevel@tonic-gate c->local_window_max, c->local_maxpacket,
12807c478bd9Sstevel@tonic-gate 0, xstrdup(rtype), 1);
12817c478bd9Sstevel@tonic-gate nc->listening_port = c->listening_port;
12827c478bd9Sstevel@tonic-gate nc->host_port = c->host_port;
12837c478bd9Sstevel@tonic-gate strlcpy(nc->path, c->path, sizeof(nc->path));
12847c478bd9Sstevel@tonic-gate
1285*8b0ef7edSZdenek Kotala if (nextstate != SSH_CHANNEL_DYNAMIC)
12867c478bd9Sstevel@tonic-gate port_open_helper(nc, rtype);
12877c478bd9Sstevel@tonic-gate }
12887c478bd9Sstevel@tonic-gate }
12897c478bd9Sstevel@tonic-gate
12907c478bd9Sstevel@tonic-gate /*
12917c478bd9Sstevel@tonic-gate * This is the authentication agent socket listening for connections from
12927c478bd9Sstevel@tonic-gate * clients.
12937c478bd9Sstevel@tonic-gate */
12947c478bd9Sstevel@tonic-gate static void
channel_post_auth_listener(Channel * c,fd_set * readset,fd_set * writeset)12957c478bd9Sstevel@tonic-gate channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
12967c478bd9Sstevel@tonic-gate {
12977c478bd9Sstevel@tonic-gate Channel *nc;
12987c478bd9Sstevel@tonic-gate char *name;
12997c478bd9Sstevel@tonic-gate int newsock;
13007c478bd9Sstevel@tonic-gate struct sockaddr addr;
13017c478bd9Sstevel@tonic-gate socklen_t addrlen;
13027c478bd9Sstevel@tonic-gate
13037c478bd9Sstevel@tonic-gate if (FD_ISSET(c->sock, readset)) {
13047c478bd9Sstevel@tonic-gate addrlen = sizeof(addr);
13057c478bd9Sstevel@tonic-gate newsock = accept(c->sock, &addr, &addrlen);
13067c478bd9Sstevel@tonic-gate if (newsock < 0) {
13077c478bd9Sstevel@tonic-gate error("accept from auth socket: %.100s", strerror(errno));
13087c478bd9Sstevel@tonic-gate return;
13097c478bd9Sstevel@tonic-gate }
13107c478bd9Sstevel@tonic-gate name = xstrdup("accepted auth socket");
13117c478bd9Sstevel@tonic-gate nc = channel_new("accepted auth socket",
13127c478bd9Sstevel@tonic-gate SSH_CHANNEL_OPENING, newsock, newsock, -1,
13137c478bd9Sstevel@tonic-gate c->local_window_max, c->local_maxpacket,
13147c478bd9Sstevel@tonic-gate 0, name, 1);
13157c478bd9Sstevel@tonic-gate if (compat20) {
13167c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_CHANNEL_OPEN);
13177c478bd9Sstevel@tonic-gate packet_put_cstring("auth-agent@openssh.com");
13187c478bd9Sstevel@tonic-gate packet_put_int(nc->self);
13197c478bd9Sstevel@tonic-gate packet_put_int(c->local_window_max);
13207c478bd9Sstevel@tonic-gate packet_put_int(c->local_maxpacket);
13217c478bd9Sstevel@tonic-gate } else {
13227c478bd9Sstevel@tonic-gate packet_start(SSH_SMSG_AGENT_OPEN);
13237c478bd9Sstevel@tonic-gate packet_put_int(nc->self);
13247c478bd9Sstevel@tonic-gate }
13257c478bd9Sstevel@tonic-gate packet_send();
13267c478bd9Sstevel@tonic-gate }
13277c478bd9Sstevel@tonic-gate }
13287c478bd9Sstevel@tonic-gate
13297c478bd9Sstevel@tonic-gate static void
channel_post_connecting(Channel * c,fd_set * readset,fd_set * writeset)13307c478bd9Sstevel@tonic-gate channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
13317c478bd9Sstevel@tonic-gate {
13327c478bd9Sstevel@tonic-gate int err = 0;
13337c478bd9Sstevel@tonic-gate socklen_t sz = sizeof(err);
13347c478bd9Sstevel@tonic-gate
13357c478bd9Sstevel@tonic-gate if (FD_ISSET(c->sock, writeset)) {
13367c478bd9Sstevel@tonic-gate if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
13377c478bd9Sstevel@tonic-gate err = errno;
13387c478bd9Sstevel@tonic-gate error("getsockopt SO_ERROR failed");
13397c478bd9Sstevel@tonic-gate }
13407c478bd9Sstevel@tonic-gate if (err == 0) {
13417c478bd9Sstevel@tonic-gate debug("channel %d: connected", c->self);
13427c478bd9Sstevel@tonic-gate c->type = SSH_CHANNEL_OPEN;
13437c478bd9Sstevel@tonic-gate if (compat20) {
13447c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
13457c478bd9Sstevel@tonic-gate packet_put_int(c->remote_id);
13467c478bd9Sstevel@tonic-gate packet_put_int(c->self);
13477c478bd9Sstevel@tonic-gate packet_put_int(c->local_window);
13487c478bd9Sstevel@tonic-gate packet_put_int(c->local_maxpacket);
13497c478bd9Sstevel@tonic-gate } else {
13507c478bd9Sstevel@tonic-gate packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
13517c478bd9Sstevel@tonic-gate packet_put_int(c->remote_id);
13527c478bd9Sstevel@tonic-gate packet_put_int(c->self);
13537c478bd9Sstevel@tonic-gate }
13547c478bd9Sstevel@tonic-gate } else {
13557c478bd9Sstevel@tonic-gate debug("channel %d: not connected: %s",
13567c478bd9Sstevel@tonic-gate c->self, strerror(err));
13577c478bd9Sstevel@tonic-gate if (compat20) {
13587c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
13597c478bd9Sstevel@tonic-gate packet_put_int(c->remote_id);
13607c478bd9Sstevel@tonic-gate packet_put_int(SSH2_OPEN_CONNECT_FAILED);
13617c478bd9Sstevel@tonic-gate if (!(datafellows & SSH_BUG_OPENFAILURE)) {
13626f786aceSNobutomo Nakano packet_put_utf8_cstring(strerror(err));
13637c478bd9Sstevel@tonic-gate packet_put_cstring("");
13647c478bd9Sstevel@tonic-gate }
13657c478bd9Sstevel@tonic-gate } else {
13667c478bd9Sstevel@tonic-gate packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
13677c478bd9Sstevel@tonic-gate packet_put_int(c->remote_id);
13687c478bd9Sstevel@tonic-gate }
13697c478bd9Sstevel@tonic-gate chan_mark_dead(c);
13707c478bd9Sstevel@tonic-gate }
13717c478bd9Sstevel@tonic-gate packet_send();
13727c478bd9Sstevel@tonic-gate }
13737c478bd9Sstevel@tonic-gate }
13747c478bd9Sstevel@tonic-gate
13757c478bd9Sstevel@tonic-gate static int
channel_handle_rfd(Channel * c,fd_set * readset,fd_set * writeset)13767c478bd9Sstevel@tonic-gate channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
13777c478bd9Sstevel@tonic-gate {
1378d92fc072SZdenek Kotala char buf[CHAN_RBUF];
13797c478bd9Sstevel@tonic-gate int len;
13807c478bd9Sstevel@tonic-gate
13817c478bd9Sstevel@tonic-gate if (c->rfd != -1 &&
13827c478bd9Sstevel@tonic-gate FD_ISSET(c->rfd, readset)) {
13837c478bd9Sstevel@tonic-gate len = read(c->rfd, buf, sizeof(buf));
13847c478bd9Sstevel@tonic-gate if (len < 0 && (errno == EINTR || errno == EAGAIN))
13857c478bd9Sstevel@tonic-gate return 1;
13867c478bd9Sstevel@tonic-gate if (len <= 0) {
13877c478bd9Sstevel@tonic-gate debug("channel %d: read<=0 rfd %d len %d",
13887c478bd9Sstevel@tonic-gate c->self, c->rfd, len);
13897c478bd9Sstevel@tonic-gate if (c->type != SSH_CHANNEL_OPEN) {
13907c478bd9Sstevel@tonic-gate debug("channel %d: not open", c->self);
13917c478bd9Sstevel@tonic-gate chan_mark_dead(c);
13927c478bd9Sstevel@tonic-gate return -1;
13937c478bd9Sstevel@tonic-gate } else if (compat13) {
13947c478bd9Sstevel@tonic-gate buffer_clear(&c->output);
13957c478bd9Sstevel@tonic-gate c->type = SSH_CHANNEL_INPUT_DRAINING;
13967c478bd9Sstevel@tonic-gate debug("channel %d: input draining.", c->self);
13977c478bd9Sstevel@tonic-gate } else {
13987c478bd9Sstevel@tonic-gate chan_read_failed(c);
13997c478bd9Sstevel@tonic-gate }
14007c478bd9Sstevel@tonic-gate return -1;
14017c478bd9Sstevel@tonic-gate }
14027c478bd9Sstevel@tonic-gate if (c->input_filter != NULL) {
14037c478bd9Sstevel@tonic-gate if (c->input_filter(c, buf, len) == -1) {
14047c478bd9Sstevel@tonic-gate debug("channel %d: filter stops", c->self);
14057c478bd9Sstevel@tonic-gate chan_read_failed(c);
14067c478bd9Sstevel@tonic-gate }
14077c478bd9Sstevel@tonic-gate } else {
14087c478bd9Sstevel@tonic-gate buffer_append(&c->input, buf, len);
14097c478bd9Sstevel@tonic-gate }
14107c478bd9Sstevel@tonic-gate }
14117c478bd9Sstevel@tonic-gate return 1;
14127c478bd9Sstevel@tonic-gate }
14137c478bd9Sstevel@tonic-gate static int
channel_handle_wfd(Channel * c,fd_set * readset,fd_set * writeset)14147c478bd9Sstevel@tonic-gate channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
14157c478bd9Sstevel@tonic-gate {
14167c478bd9Sstevel@tonic-gate struct termios tio;
14177c478bd9Sstevel@tonic-gate u_char *data;
14187c478bd9Sstevel@tonic-gate u_int dlen;
14197c478bd9Sstevel@tonic-gate int len;
14207c478bd9Sstevel@tonic-gate
14217c478bd9Sstevel@tonic-gate /* Send buffered output data to the socket. */
14227c478bd9Sstevel@tonic-gate if (c->wfd != -1 &&
14237c478bd9Sstevel@tonic-gate FD_ISSET(c->wfd, writeset) &&
14247c478bd9Sstevel@tonic-gate buffer_len(&c->output) > 0) {
14257c478bd9Sstevel@tonic-gate data = buffer_ptr(&c->output);
14267c478bd9Sstevel@tonic-gate dlen = buffer_len(&c->output);
14277c478bd9Sstevel@tonic-gate #ifdef _AIX
14287c478bd9Sstevel@tonic-gate /* XXX: Later AIX versions can't push as much data to tty */
14297c478bd9Sstevel@tonic-gate if (compat20 && c->wfd_isatty && dlen > 8*1024)
14307c478bd9Sstevel@tonic-gate dlen = 8*1024;
14317c478bd9Sstevel@tonic-gate #endif
14327c478bd9Sstevel@tonic-gate len = write(c->wfd, data, dlen);
14337c478bd9Sstevel@tonic-gate if (len < 0 && (errno == EINTR || errno == EAGAIN))
14347c478bd9Sstevel@tonic-gate return 1;
14357c478bd9Sstevel@tonic-gate if (len <= 0) {
14367c478bd9Sstevel@tonic-gate if (c->type != SSH_CHANNEL_OPEN) {
14377c478bd9Sstevel@tonic-gate debug("channel %d: not open", c->self);
14387c478bd9Sstevel@tonic-gate chan_mark_dead(c);
14397c478bd9Sstevel@tonic-gate return -1;
14407c478bd9Sstevel@tonic-gate } else if (compat13) {
14417c478bd9Sstevel@tonic-gate buffer_clear(&c->output);
14427c478bd9Sstevel@tonic-gate debug("channel %d: input draining.", c->self);
14437c478bd9Sstevel@tonic-gate c->type = SSH_CHANNEL_INPUT_DRAINING;
14447c478bd9Sstevel@tonic-gate } else {
14457c478bd9Sstevel@tonic-gate chan_write_failed(c);
14467c478bd9Sstevel@tonic-gate }
14477c478bd9Sstevel@tonic-gate return -1;
14487c478bd9Sstevel@tonic-gate }
14497c478bd9Sstevel@tonic-gate if (compat20 && c->isatty && dlen >= 1 && data[0] != '\r') {
14507c478bd9Sstevel@tonic-gate if (tcgetattr(c->wfd, &tio) == 0 &&
14517c478bd9Sstevel@tonic-gate !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
14527c478bd9Sstevel@tonic-gate /*
14537c478bd9Sstevel@tonic-gate * Simulate echo to reduce the impact of
14547c478bd9Sstevel@tonic-gate * traffic analysis. We need to match the
14557c478bd9Sstevel@tonic-gate * size of a SSH2_MSG_CHANNEL_DATA message
14567c478bd9Sstevel@tonic-gate * (4 byte channel id + data)
14577c478bd9Sstevel@tonic-gate */
14587c478bd9Sstevel@tonic-gate packet_send_ignore(4 + len);
14597c478bd9Sstevel@tonic-gate packet_send();
14607c478bd9Sstevel@tonic-gate }
14617c478bd9Sstevel@tonic-gate }
14627c478bd9Sstevel@tonic-gate buffer_consume(&c->output, len);
14637c478bd9Sstevel@tonic-gate if (compat20 && len > 0) {
14647c478bd9Sstevel@tonic-gate c->local_consumed += len;
14657c478bd9Sstevel@tonic-gate }
14667c478bd9Sstevel@tonic-gate }
14677c478bd9Sstevel@tonic-gate return 1;
14687c478bd9Sstevel@tonic-gate }
14697c478bd9Sstevel@tonic-gate static int
channel_handle_efd(Channel * c,fd_set * readset,fd_set * writeset)14707c478bd9Sstevel@tonic-gate channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
14717c478bd9Sstevel@tonic-gate {
1472d92fc072SZdenek Kotala char buf[CHAN_RBUF];
14737c478bd9Sstevel@tonic-gate int len;
14747c478bd9Sstevel@tonic-gate
14757c478bd9Sstevel@tonic-gate /** XXX handle drain efd, too */
14767c478bd9Sstevel@tonic-gate if (c->efd != -1) {
14777c478bd9Sstevel@tonic-gate if (c->extended_usage == CHAN_EXTENDED_WRITE &&
14787c478bd9Sstevel@tonic-gate FD_ISSET(c->efd, writeset) &&
14797c478bd9Sstevel@tonic-gate buffer_len(&c->extended) > 0) {
14807c478bd9Sstevel@tonic-gate len = write(c->efd, buffer_ptr(&c->extended),
14817c478bd9Sstevel@tonic-gate buffer_len(&c->extended));
14827c478bd9Sstevel@tonic-gate debug2("channel %d: written %d to efd %d",
14837c478bd9Sstevel@tonic-gate c->self, len, c->efd);
14847c478bd9Sstevel@tonic-gate if (len < 0 && (errno == EINTR || errno == EAGAIN))
14857c478bd9Sstevel@tonic-gate return 1;
14867c478bd9Sstevel@tonic-gate if (len <= 0) {
14877c478bd9Sstevel@tonic-gate debug2("channel %d: closing write-efd %d",
14887c478bd9Sstevel@tonic-gate c->self, c->efd);
14897c478bd9Sstevel@tonic-gate channel_close_fd(&c->efd);
14907c478bd9Sstevel@tonic-gate } else {
14917c478bd9Sstevel@tonic-gate buffer_consume(&c->extended, len);
14927c478bd9Sstevel@tonic-gate c->local_consumed += len;
14937c478bd9Sstevel@tonic-gate }
14947c478bd9Sstevel@tonic-gate } else if (c->extended_usage == CHAN_EXTENDED_READ &&
14957c478bd9Sstevel@tonic-gate FD_ISSET(c->efd, readset)) {
14967c478bd9Sstevel@tonic-gate len = read(c->efd, buf, sizeof(buf));
14977c478bd9Sstevel@tonic-gate debug2("channel %d: read %d from efd %d",
14987c478bd9Sstevel@tonic-gate c->self, len, c->efd);
14997c478bd9Sstevel@tonic-gate if (len < 0 && (errno == EINTR || errno == EAGAIN))
15007c478bd9Sstevel@tonic-gate return 1;
15017c478bd9Sstevel@tonic-gate if (len <= 0) {
15027c478bd9Sstevel@tonic-gate debug2("channel %d: closing read-efd %d",
15037c478bd9Sstevel@tonic-gate c->self, c->efd);
15047c478bd9Sstevel@tonic-gate channel_close_fd(&c->efd);
15057c478bd9Sstevel@tonic-gate } else {
15067c478bd9Sstevel@tonic-gate buffer_append(&c->extended, buf, len);
15077c478bd9Sstevel@tonic-gate }
15087c478bd9Sstevel@tonic-gate }
15097c478bd9Sstevel@tonic-gate }
15107c478bd9Sstevel@tonic-gate return 1;
15117c478bd9Sstevel@tonic-gate }
15127c478bd9Sstevel@tonic-gate static int
channel_check_window(Channel * c)15137c478bd9Sstevel@tonic-gate channel_check_window(Channel *c)
15147c478bd9Sstevel@tonic-gate {
15157c478bd9Sstevel@tonic-gate if (c->type == SSH_CHANNEL_OPEN &&
15167c478bd9Sstevel@tonic-gate !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
15177c478bd9Sstevel@tonic-gate c->local_window < c->local_window_max/2 &&
15187c478bd9Sstevel@tonic-gate c->local_consumed > 0) {
15197c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
15207c478bd9Sstevel@tonic-gate packet_put_int(c->remote_id);
15217c478bd9Sstevel@tonic-gate packet_put_int(c->local_consumed);
15227c478bd9Sstevel@tonic-gate packet_send();
15237c478bd9Sstevel@tonic-gate debug2("channel %d: window %d sent adjust %d",
15247c478bd9Sstevel@tonic-gate c->self, c->local_window,
15257c478bd9Sstevel@tonic-gate c->local_consumed);
15267c478bd9Sstevel@tonic-gate c->local_window += c->local_consumed;
15277c478bd9Sstevel@tonic-gate c->local_consumed = 0;
15287c478bd9Sstevel@tonic-gate }
15297c478bd9Sstevel@tonic-gate return 1;
15307c478bd9Sstevel@tonic-gate }
15317c478bd9Sstevel@tonic-gate
15327c478bd9Sstevel@tonic-gate static void
channel_post_open(Channel * c,fd_set * readset,fd_set * writeset)15337c478bd9Sstevel@tonic-gate channel_post_open(Channel *c, fd_set * readset, fd_set * writeset)
15347c478bd9Sstevel@tonic-gate {
15357c478bd9Sstevel@tonic-gate channel_handle_rfd(c, readset, writeset);
15367c478bd9Sstevel@tonic-gate channel_handle_wfd(c, readset, writeset);
15377c478bd9Sstevel@tonic-gate if (!compat20)
15387c478bd9Sstevel@tonic-gate return;
15397c478bd9Sstevel@tonic-gate channel_handle_efd(c, readset, writeset);
15407c478bd9Sstevel@tonic-gate channel_check_window(c);
15417c478bd9Sstevel@tonic-gate }
15427c478bd9Sstevel@tonic-gate
15437c478bd9Sstevel@tonic-gate static void
channel_post_output_drain_13(Channel * c,fd_set * readset,fd_set * writeset)15447c478bd9Sstevel@tonic-gate channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
15457c478bd9Sstevel@tonic-gate {
15467c478bd9Sstevel@tonic-gate int len;
15477c478bd9Sstevel@tonic-gate
15487c478bd9Sstevel@tonic-gate /* Send buffered output data to the socket. */
15497c478bd9Sstevel@tonic-gate if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
15507c478bd9Sstevel@tonic-gate len = write(c->sock, buffer_ptr(&c->output),
15517c478bd9Sstevel@tonic-gate buffer_len(&c->output));
15527c478bd9Sstevel@tonic-gate if (len <= 0)
15537c478bd9Sstevel@tonic-gate buffer_clear(&c->output);
15547c478bd9Sstevel@tonic-gate else
15557c478bd9Sstevel@tonic-gate buffer_consume(&c->output, len);
15567c478bd9Sstevel@tonic-gate }
15577c478bd9Sstevel@tonic-gate }
15587c478bd9Sstevel@tonic-gate
15597c478bd9Sstevel@tonic-gate static void
channel_handler_init_20(void)15607c478bd9Sstevel@tonic-gate channel_handler_init_20(void)
15617c478bd9Sstevel@tonic-gate {
15627c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
15637c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
15647c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
15657c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
15667c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
15677c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
15687c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
15697c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
15707c478bd9Sstevel@tonic-gate
15717c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
15727c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
15737c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
15747c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
15757c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
15767c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
15777c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
15787c478bd9Sstevel@tonic-gate }
15797c478bd9Sstevel@tonic-gate
15807c478bd9Sstevel@tonic-gate static void
channel_handler_init_13(void)15817c478bd9Sstevel@tonic-gate channel_handler_init_13(void)
15827c478bd9Sstevel@tonic-gate {
15837c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
15847c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
15857c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
15867c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
15877c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
15887c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
15897c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
15907c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
15917c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
15927c478bd9Sstevel@tonic-gate
15937c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
15947c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
15957c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
15967c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
15977c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
15987c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
15997c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
16007c478bd9Sstevel@tonic-gate }
16017c478bd9Sstevel@tonic-gate
16027c478bd9Sstevel@tonic-gate static void
channel_handler_init_15(void)16037c478bd9Sstevel@tonic-gate channel_handler_init_15(void)
16047c478bd9Sstevel@tonic-gate {
16057c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
16067c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
16077c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
16087c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
16097c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
16107c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
16117c478bd9Sstevel@tonic-gate channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
16127c478bd9Sstevel@tonic-gate
16137c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
16147c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
16157c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
16167c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
16177c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
16187c478bd9Sstevel@tonic-gate channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
16197c478bd9Sstevel@tonic-gate }
16207c478bd9Sstevel@tonic-gate
16217c478bd9Sstevel@tonic-gate static void
channel_handler_init(void)16227c478bd9Sstevel@tonic-gate channel_handler_init(void)
16237c478bd9Sstevel@tonic-gate {
16247c478bd9Sstevel@tonic-gate int i;
16257c478bd9Sstevel@tonic-gate
16267c478bd9Sstevel@tonic-gate for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
16277c478bd9Sstevel@tonic-gate channel_pre[i] = NULL;
16287c478bd9Sstevel@tonic-gate channel_post[i] = NULL;
16297c478bd9Sstevel@tonic-gate }
16307c478bd9Sstevel@tonic-gate if (compat20)
16317c478bd9Sstevel@tonic-gate channel_handler_init_20();
16327c478bd9Sstevel@tonic-gate else if (compat13)
16337c478bd9Sstevel@tonic-gate channel_handler_init_13();
16347c478bd9Sstevel@tonic-gate else
16357c478bd9Sstevel@tonic-gate channel_handler_init_15();
16367c478bd9Sstevel@tonic-gate }
16377c478bd9Sstevel@tonic-gate
16387c478bd9Sstevel@tonic-gate /* gc dead channels */
16397c478bd9Sstevel@tonic-gate static void
channel_garbage_collect(Channel * c)16407c478bd9Sstevel@tonic-gate channel_garbage_collect(Channel *c)
16417c478bd9Sstevel@tonic-gate {
16427c478bd9Sstevel@tonic-gate if (c == NULL)
16437c478bd9Sstevel@tonic-gate return;
16447c478bd9Sstevel@tonic-gate if (c->detach_user != NULL) {
16457c478bd9Sstevel@tonic-gate if (!chan_is_dead(c, 0))
16467c478bd9Sstevel@tonic-gate return;
16477c478bd9Sstevel@tonic-gate debug("channel %d: gc: notify user", c->self);
16487c478bd9Sstevel@tonic-gate c->detach_user(c->self, NULL);
16497c478bd9Sstevel@tonic-gate /* if we still have a callback */
16507c478bd9Sstevel@tonic-gate if (c->detach_user != NULL)
16517c478bd9Sstevel@tonic-gate return;
16527c478bd9Sstevel@tonic-gate debug("channel %d: gc: user detached", c->self);
16537c478bd9Sstevel@tonic-gate }
16547c478bd9Sstevel@tonic-gate if (!c->wait_for_exit && !chan_is_dead(c, 1))
16557c478bd9Sstevel@tonic-gate return;
16567c478bd9Sstevel@tonic-gate debug("channel %d: garbage collecting", c->self);
16577c478bd9Sstevel@tonic-gate channel_free(c);
16587c478bd9Sstevel@tonic-gate }
16597c478bd9Sstevel@tonic-gate
16607c478bd9Sstevel@tonic-gate static void
channel_handler(chan_fn * ftab[],fd_set * readset,fd_set * writeset)16617c478bd9Sstevel@tonic-gate channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
16627c478bd9Sstevel@tonic-gate {
16637c478bd9Sstevel@tonic-gate static int did_init = 0;
1664*8b0ef7edSZdenek Kotala int i, oalloc;
16657c478bd9Sstevel@tonic-gate Channel *c;
16667c478bd9Sstevel@tonic-gate
16677c478bd9Sstevel@tonic-gate if (!did_init) {
16687c478bd9Sstevel@tonic-gate channel_handler_init();
16697c478bd9Sstevel@tonic-gate did_init = 1;
16707c478bd9Sstevel@tonic-gate }
1671*8b0ef7edSZdenek Kotala for (i = 0, oalloc = channels_alloc; i < oalloc; i++) {
16727c478bd9Sstevel@tonic-gate c = channels[i];
16737c478bd9Sstevel@tonic-gate if (c == NULL)
16747c478bd9Sstevel@tonic-gate continue;
1675*8b0ef7edSZdenek Kotala if (c->delayed) {
1676*8b0ef7edSZdenek Kotala if (ftab == channel_pre)
1677*8b0ef7edSZdenek Kotala c->delayed = 0;
1678*8b0ef7edSZdenek Kotala else
1679*8b0ef7edSZdenek Kotala continue;
1680*8b0ef7edSZdenek Kotala }
16817c478bd9Sstevel@tonic-gate if (ftab[c->type] != NULL)
16827c478bd9Sstevel@tonic-gate (*ftab[c->type])(c, readset, writeset);
16837c478bd9Sstevel@tonic-gate channel_garbage_collect(c);
16847c478bd9Sstevel@tonic-gate }
16857c478bd9Sstevel@tonic-gate }
16867c478bd9Sstevel@tonic-gate
16877c478bd9Sstevel@tonic-gate /*
16887c478bd9Sstevel@tonic-gate * Allocate/update select bitmasks and add any bits relevant to channels in
16897c478bd9Sstevel@tonic-gate * select bitmasks.
16907c478bd9Sstevel@tonic-gate */
16917c478bd9Sstevel@tonic-gate void
channel_prepare_select(fd_set ** readsetp,fd_set ** writesetp,int * maxfdp,int * nallocp,int rekeying)16927c478bd9Sstevel@tonic-gate channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
16937c478bd9Sstevel@tonic-gate int *nallocp, int rekeying)
16947c478bd9Sstevel@tonic-gate {
16957c478bd9Sstevel@tonic-gate int n;
16967c478bd9Sstevel@tonic-gate u_int sz;
16977c478bd9Sstevel@tonic-gate
16987c478bd9Sstevel@tonic-gate n = MAX(*maxfdp, channel_max_fd);
16997c478bd9Sstevel@tonic-gate
17007c478bd9Sstevel@tonic-gate sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
17017c478bd9Sstevel@tonic-gate /* perhaps check sz < nalloc/2 and shrink? */
17027c478bd9Sstevel@tonic-gate if (*readsetp == NULL || sz > *nallocp) {
17037c478bd9Sstevel@tonic-gate *readsetp = xrealloc(*readsetp, sz);
17047c478bd9Sstevel@tonic-gate *writesetp = xrealloc(*writesetp, sz);
17057c478bd9Sstevel@tonic-gate *nallocp = sz;
17067c478bd9Sstevel@tonic-gate }
17077c478bd9Sstevel@tonic-gate *maxfdp = n;
17087c478bd9Sstevel@tonic-gate memset(*readsetp, 0, sz);
17097c478bd9Sstevel@tonic-gate memset(*writesetp, 0, sz);
17107c478bd9Sstevel@tonic-gate
17117c478bd9Sstevel@tonic-gate if (!rekeying)
17127c478bd9Sstevel@tonic-gate channel_handler(channel_pre, *readsetp, *writesetp);
17137c478bd9Sstevel@tonic-gate }
17147c478bd9Sstevel@tonic-gate
17157c478bd9Sstevel@tonic-gate /*
17167c478bd9Sstevel@tonic-gate * After select, perform any appropriate operations for channels which have
17177c478bd9Sstevel@tonic-gate * events pending.
17187c478bd9Sstevel@tonic-gate */
17197c478bd9Sstevel@tonic-gate void
channel_after_select(fd_set * readset,fd_set * writeset)17207c478bd9Sstevel@tonic-gate channel_after_select(fd_set * readset, fd_set * writeset)
17217c478bd9Sstevel@tonic-gate {
17227c478bd9Sstevel@tonic-gate channel_handler(channel_post, readset, writeset);
17237c478bd9Sstevel@tonic-gate }
17247c478bd9Sstevel@tonic-gate
17257c478bd9Sstevel@tonic-gate
17267c478bd9Sstevel@tonic-gate /* If there is data to send to the connection, enqueue some of it now. */
17277c478bd9Sstevel@tonic-gate
17287c478bd9Sstevel@tonic-gate void
channel_output_poll(void)17297c478bd9Sstevel@tonic-gate channel_output_poll(void)
17307c478bd9Sstevel@tonic-gate {
17317c478bd9Sstevel@tonic-gate Channel *c;
17327c478bd9Sstevel@tonic-gate int i;
17337c478bd9Sstevel@tonic-gate u_int len;
17347c478bd9Sstevel@tonic-gate
17357c478bd9Sstevel@tonic-gate for (i = 0; i < channels_alloc; i++) {
17367c478bd9Sstevel@tonic-gate c = channels[i];
17377c478bd9Sstevel@tonic-gate if (c == NULL)
17387c478bd9Sstevel@tonic-gate continue;
17397c478bd9Sstevel@tonic-gate
17407c478bd9Sstevel@tonic-gate /*
17417c478bd9Sstevel@tonic-gate * We are only interested in channels that can have buffered
17427c478bd9Sstevel@tonic-gate * incoming data.
17437c478bd9Sstevel@tonic-gate */
17447c478bd9Sstevel@tonic-gate if (compat13) {
17457c478bd9Sstevel@tonic-gate if (c->type != SSH_CHANNEL_OPEN &&
17467c478bd9Sstevel@tonic-gate c->type != SSH_CHANNEL_INPUT_DRAINING)
17477c478bd9Sstevel@tonic-gate continue;
17487c478bd9Sstevel@tonic-gate } else {
17497c478bd9Sstevel@tonic-gate if (c->type != SSH_CHANNEL_OPEN)
17507c478bd9Sstevel@tonic-gate continue;
17517c478bd9Sstevel@tonic-gate }
17527c478bd9Sstevel@tonic-gate if (compat20 &&
17537c478bd9Sstevel@tonic-gate (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
17547c478bd9Sstevel@tonic-gate /* XXX is this true? */
17557c478bd9Sstevel@tonic-gate debug3("channel %d: will not send data after close", c->self);
17567c478bd9Sstevel@tonic-gate continue;
17577c478bd9Sstevel@tonic-gate }
17587c478bd9Sstevel@tonic-gate
17597c478bd9Sstevel@tonic-gate /* Get the amount of buffered data for this channel. */
17607c478bd9Sstevel@tonic-gate if ((c->istate == CHAN_INPUT_OPEN ||
17617c478bd9Sstevel@tonic-gate c->istate == CHAN_INPUT_WAIT_DRAIN) &&
17627c478bd9Sstevel@tonic-gate (len = buffer_len(&c->input)) > 0) {
17637c478bd9Sstevel@tonic-gate /*
17647c478bd9Sstevel@tonic-gate * Send some data for the other side over the secure
17657c478bd9Sstevel@tonic-gate * connection.
17667c478bd9Sstevel@tonic-gate */
17677c478bd9Sstevel@tonic-gate if (compat20) {
17687c478bd9Sstevel@tonic-gate if (len > c->remote_window)
17697c478bd9Sstevel@tonic-gate len = c->remote_window;
17707c478bd9Sstevel@tonic-gate if (len > c->remote_maxpacket)
17717c478bd9Sstevel@tonic-gate len = c->remote_maxpacket;
17727c478bd9Sstevel@tonic-gate } else {
17737c478bd9Sstevel@tonic-gate if (packet_is_interactive()) {
17747c478bd9Sstevel@tonic-gate if (len > 1024)
17757c478bd9Sstevel@tonic-gate len = 512;
17767c478bd9Sstevel@tonic-gate } else {
17777c478bd9Sstevel@tonic-gate /* Keep the packets at reasonable size. */
17787c478bd9Sstevel@tonic-gate if (len > packet_get_maxsize()/2)
17797c478bd9Sstevel@tonic-gate len = packet_get_maxsize()/2;
17807c478bd9Sstevel@tonic-gate }
17817c478bd9Sstevel@tonic-gate }
17827c478bd9Sstevel@tonic-gate if (len > 0) {
17837c478bd9Sstevel@tonic-gate packet_start(compat20 ?
17847c478bd9Sstevel@tonic-gate SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
17857c478bd9Sstevel@tonic-gate packet_put_int(c->remote_id);
17867c478bd9Sstevel@tonic-gate packet_put_string(buffer_ptr(&c->input), len);
17877c478bd9Sstevel@tonic-gate packet_send();
17887c478bd9Sstevel@tonic-gate buffer_consume(&c->input, len);
17897c478bd9Sstevel@tonic-gate c->remote_window -= len;
17907c478bd9Sstevel@tonic-gate }
17917c478bd9Sstevel@tonic-gate } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
17927c478bd9Sstevel@tonic-gate if (compat13)
17937c478bd9Sstevel@tonic-gate fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
17947c478bd9Sstevel@tonic-gate /*
17957c478bd9Sstevel@tonic-gate * input-buffer is empty and read-socket shutdown:
17967c478bd9Sstevel@tonic-gate * tell peer, that we will not send more data: send IEOF.
17977c478bd9Sstevel@tonic-gate * hack for extended data: delay EOF if EFD still in use.
17987c478bd9Sstevel@tonic-gate */
17997c478bd9Sstevel@tonic-gate if (CHANNEL_EFD_INPUT_ACTIVE(c))
18007c478bd9Sstevel@tonic-gate debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
18017c478bd9Sstevel@tonic-gate c->self, c->efd, buffer_len(&c->extended));
18027c478bd9Sstevel@tonic-gate else
18037c478bd9Sstevel@tonic-gate chan_ibuf_empty(c);
18047c478bd9Sstevel@tonic-gate }
18057c478bd9Sstevel@tonic-gate /* Send extended data, i.e. stderr */
18067c478bd9Sstevel@tonic-gate if (compat20 &&
18077c478bd9Sstevel@tonic-gate !(c->flags & CHAN_EOF_SENT) &&
18087c478bd9Sstevel@tonic-gate c->remote_window > 0 &&
18097c478bd9Sstevel@tonic-gate (len = buffer_len(&c->extended)) > 0 &&
18107c478bd9Sstevel@tonic-gate c->extended_usage == CHAN_EXTENDED_READ) {
18117c478bd9Sstevel@tonic-gate debug2("channel %d: rwin %u elen %u euse %d",
18127c478bd9Sstevel@tonic-gate c->self, c->remote_window, buffer_len(&c->extended),
18137c478bd9Sstevel@tonic-gate c->extended_usage);
18147c478bd9Sstevel@tonic-gate if (len > c->remote_window)
18157c478bd9Sstevel@tonic-gate len = c->remote_window;
18167c478bd9Sstevel@tonic-gate if (len > c->remote_maxpacket)
18177c478bd9Sstevel@tonic-gate len = c->remote_maxpacket;
18187c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
18197c478bd9Sstevel@tonic-gate packet_put_int(c->remote_id);
18207c478bd9Sstevel@tonic-gate packet_put_int(SSH2_EXTENDED_DATA_STDERR);
18217c478bd9Sstevel@tonic-gate packet_put_string(buffer_ptr(&c->extended), len);
18227c478bd9Sstevel@tonic-gate packet_send();
18237c478bd9Sstevel@tonic-gate buffer_consume(&c->extended, len);
18247c478bd9Sstevel@tonic-gate c->remote_window -= len;
18257c478bd9Sstevel@tonic-gate debug2("channel %d: sent ext data %d", c->self, len);
18267c478bd9Sstevel@tonic-gate }
18277c478bd9Sstevel@tonic-gate }
18287c478bd9Sstevel@tonic-gate }
18297c478bd9Sstevel@tonic-gate
18307c478bd9Sstevel@tonic-gate
18317c478bd9Sstevel@tonic-gate /* -- protocol input */
18327c478bd9Sstevel@tonic-gate
18337c478bd9Sstevel@tonic-gate void
channel_input_data(int type,u_int32_t seq,void * ctxt)18347c478bd9Sstevel@tonic-gate channel_input_data(int type, u_int32_t seq, void *ctxt)
18357c478bd9Sstevel@tonic-gate {
18367c478bd9Sstevel@tonic-gate int id;
18377c478bd9Sstevel@tonic-gate char *data;
18387c478bd9Sstevel@tonic-gate u_int data_len;
18397c478bd9Sstevel@tonic-gate Channel *c;
18407c478bd9Sstevel@tonic-gate
18417c478bd9Sstevel@tonic-gate /* Get the channel number and verify it. */
18427c478bd9Sstevel@tonic-gate id = packet_get_int();
18437c478bd9Sstevel@tonic-gate c = channel_lookup(id);
18447c478bd9Sstevel@tonic-gate if (c == NULL)
18457c478bd9Sstevel@tonic-gate packet_disconnect("Received data for nonexistent channel %d.", id);
18467c478bd9Sstevel@tonic-gate
18477c478bd9Sstevel@tonic-gate /* Ignore any data for non-open channels (might happen on close) */
18487c478bd9Sstevel@tonic-gate if (c->type != SSH_CHANNEL_OPEN &&
18497c478bd9Sstevel@tonic-gate c->type != SSH_CHANNEL_X11_OPEN)
18507c478bd9Sstevel@tonic-gate return;
18517c478bd9Sstevel@tonic-gate
18527c478bd9Sstevel@tonic-gate /* same for protocol 1.5 if output end is no longer open */
18537c478bd9Sstevel@tonic-gate if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
18547c478bd9Sstevel@tonic-gate return;
18557c478bd9Sstevel@tonic-gate
18567c478bd9Sstevel@tonic-gate /* Get the data. */
18577c478bd9Sstevel@tonic-gate data = packet_get_string(&data_len);
18587c478bd9Sstevel@tonic-gate
18597c478bd9Sstevel@tonic-gate if (compat20) {
18607c478bd9Sstevel@tonic-gate if (data_len > c->local_maxpacket) {
18617c478bd9Sstevel@tonic-gate log("channel %d: rcvd big packet %d, maxpack %d",
18627c478bd9Sstevel@tonic-gate c->self, data_len, c->local_maxpacket);
18637c478bd9Sstevel@tonic-gate }
18647c478bd9Sstevel@tonic-gate if (data_len > c->local_window) {
18657c478bd9Sstevel@tonic-gate log("channel %d: rcvd too much data %d, win %d",
18667c478bd9Sstevel@tonic-gate c->self, data_len, c->local_window);
18677c478bd9Sstevel@tonic-gate xfree(data);
18687c478bd9Sstevel@tonic-gate return;
18697c478bd9Sstevel@tonic-gate }
18707c478bd9Sstevel@tonic-gate c->local_window -= data_len;
18717c478bd9Sstevel@tonic-gate }
18727c478bd9Sstevel@tonic-gate packet_check_eom();
18737c478bd9Sstevel@tonic-gate buffer_append(&c->output, data, data_len);
18747c478bd9Sstevel@tonic-gate xfree(data);
18757c478bd9Sstevel@tonic-gate }
18767c478bd9Sstevel@tonic-gate
18777c478bd9Sstevel@tonic-gate void
channel_input_extended_data(int type,u_int32_t seq,void * ctxt)18787c478bd9Sstevel@tonic-gate channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
18797c478bd9Sstevel@tonic-gate {
18807c478bd9Sstevel@tonic-gate int id;
18817c478bd9Sstevel@tonic-gate char *data;
18827c478bd9Sstevel@tonic-gate u_int data_len, tcode;
18837c478bd9Sstevel@tonic-gate Channel *c;
18847c478bd9Sstevel@tonic-gate
18857c478bd9Sstevel@tonic-gate /* Get the channel number and verify it. */
18867c478bd9Sstevel@tonic-gate id = packet_get_int();
18877c478bd9Sstevel@tonic-gate c = channel_lookup(id);
18887c478bd9Sstevel@tonic-gate
18897c478bd9Sstevel@tonic-gate if (c == NULL)
18907c478bd9Sstevel@tonic-gate packet_disconnect("Received extended_data for bad channel %d.", id);
18917c478bd9Sstevel@tonic-gate if (c->type != SSH_CHANNEL_OPEN) {
18927c478bd9Sstevel@tonic-gate log("channel %d: ext data for non open", id);
18937c478bd9Sstevel@tonic-gate return;
18947c478bd9Sstevel@tonic-gate }
18957c478bd9Sstevel@tonic-gate if (c->flags & CHAN_EOF_RCVD) {
18967c478bd9Sstevel@tonic-gate if (datafellows & SSH_BUG_EXTEOF)
18977c478bd9Sstevel@tonic-gate debug("channel %d: accepting ext data after eof", id);
18987c478bd9Sstevel@tonic-gate else
18997c478bd9Sstevel@tonic-gate packet_disconnect("Received extended_data after EOF "
19007c478bd9Sstevel@tonic-gate "on channel %d.", id);
19017c478bd9Sstevel@tonic-gate }
19027c478bd9Sstevel@tonic-gate tcode = packet_get_int();
19037c478bd9Sstevel@tonic-gate if (c->efd == -1 ||
19047c478bd9Sstevel@tonic-gate c->extended_usage != CHAN_EXTENDED_WRITE ||
19057c478bd9Sstevel@tonic-gate tcode != SSH2_EXTENDED_DATA_STDERR) {
19067c478bd9Sstevel@tonic-gate log("channel %d: bad ext data", c->self);
19077c478bd9Sstevel@tonic-gate return;
19087c478bd9Sstevel@tonic-gate }
19097c478bd9Sstevel@tonic-gate data = packet_get_string(&data_len);
19107c478bd9Sstevel@tonic-gate packet_check_eom();
19117c478bd9Sstevel@tonic-gate if (data_len > c->local_window) {
19127c478bd9Sstevel@tonic-gate log("channel %d: rcvd too much extended_data %d, win %d",
19137c478bd9Sstevel@tonic-gate c->self, data_len, c->local_window);
19147c478bd9Sstevel@tonic-gate xfree(data);
19157c478bd9Sstevel@tonic-gate return;
19167c478bd9Sstevel@tonic-gate }
19177c478bd9Sstevel@tonic-gate debug2("channel %d: rcvd ext data %d", c->self, data_len);
19187c478bd9Sstevel@tonic-gate c->local_window -= data_len;
19197c478bd9Sstevel@tonic-gate buffer_append(&c->extended, data, data_len);
19207c478bd9Sstevel@tonic-gate xfree(data);
19217c478bd9Sstevel@tonic-gate }
19227c478bd9Sstevel@tonic-gate
19237c478bd9Sstevel@tonic-gate void
channel_input_ieof(int type,u_int32_t seq,void * ctxt)19247c478bd9Sstevel@tonic-gate channel_input_ieof(int type, u_int32_t seq, void *ctxt)
19257c478bd9Sstevel@tonic-gate {
19267c478bd9Sstevel@tonic-gate int id;
19277c478bd9Sstevel@tonic-gate Channel *c;
19287c478bd9Sstevel@tonic-gate
19297c478bd9Sstevel@tonic-gate id = packet_get_int();
19307c478bd9Sstevel@tonic-gate packet_check_eom();
19317c478bd9Sstevel@tonic-gate c = channel_lookup(id);
19327c478bd9Sstevel@tonic-gate if (c == NULL)
19337c478bd9Sstevel@tonic-gate packet_disconnect("Received ieof for nonexistent channel %d.", id);
19347c478bd9Sstevel@tonic-gate chan_rcvd_ieof(c);
19357c478bd9Sstevel@tonic-gate
19367c478bd9Sstevel@tonic-gate /* XXX force input close */
19377c478bd9Sstevel@tonic-gate if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
19387c478bd9Sstevel@tonic-gate debug("channel %d: FORCE input drain", c->self);
19397c478bd9Sstevel@tonic-gate c->istate = CHAN_INPUT_WAIT_DRAIN;
19407c478bd9Sstevel@tonic-gate if (buffer_len(&c->input) == 0)
19417c478bd9Sstevel@tonic-gate chan_ibuf_empty(c);
19427c478bd9Sstevel@tonic-gate }
19437c478bd9Sstevel@tonic-gate
19447c478bd9Sstevel@tonic-gate }
19457c478bd9Sstevel@tonic-gate
19467c478bd9Sstevel@tonic-gate void
channel_input_close(int type,u_int32_t seq,void * ctxt)19477c478bd9Sstevel@tonic-gate channel_input_close(int type, u_int32_t seq, void *ctxt)
19487c478bd9Sstevel@tonic-gate {
19497c478bd9Sstevel@tonic-gate int id;
19507c478bd9Sstevel@tonic-gate Channel *c;
19517c478bd9Sstevel@tonic-gate
19527c478bd9Sstevel@tonic-gate id = packet_get_int();
19537c478bd9Sstevel@tonic-gate packet_check_eom();
19547c478bd9Sstevel@tonic-gate c = channel_lookup(id);
19557c478bd9Sstevel@tonic-gate if (c == NULL)
19567c478bd9Sstevel@tonic-gate packet_disconnect("Received close for nonexistent channel %d.", id);
19577c478bd9Sstevel@tonic-gate
19587c478bd9Sstevel@tonic-gate /*
19597c478bd9Sstevel@tonic-gate * Send a confirmation that we have closed the channel and no more
19607c478bd9Sstevel@tonic-gate * data is coming for it.
19617c478bd9Sstevel@tonic-gate */
19627c478bd9Sstevel@tonic-gate packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
19637c478bd9Sstevel@tonic-gate packet_put_int(c->remote_id);
19647c478bd9Sstevel@tonic-gate packet_send();
19657c478bd9Sstevel@tonic-gate
19667c478bd9Sstevel@tonic-gate /*
19677c478bd9Sstevel@tonic-gate * If the channel is in closed state, we have sent a close request,
19687c478bd9Sstevel@tonic-gate * and the other side will eventually respond with a confirmation.
19697c478bd9Sstevel@tonic-gate * Thus, we cannot free the channel here, because then there would be
19707c478bd9Sstevel@tonic-gate * no-one to receive the confirmation. The channel gets freed when
19717c478bd9Sstevel@tonic-gate * the confirmation arrives.
19727c478bd9Sstevel@tonic-gate */
19737c478bd9Sstevel@tonic-gate if (c->type != SSH_CHANNEL_CLOSED) {
19747c478bd9Sstevel@tonic-gate /*
19757c478bd9Sstevel@tonic-gate * Not a closed channel - mark it as draining, which will
19767c478bd9Sstevel@tonic-gate * cause it to be freed later.
19777c478bd9Sstevel@tonic-gate */
19787c478bd9Sstevel@tonic-gate buffer_clear(&c->input);
19797c478bd9Sstevel@tonic-gate c->type = SSH_CHANNEL_OUTPUT_DRAINING;
19807c478bd9Sstevel@tonic-gate }
19817c478bd9Sstevel@tonic-gate }
19827c478bd9Sstevel@tonic-gate
19837c478bd9Sstevel@tonic-gate /* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
19847c478bd9Sstevel@tonic-gate void
channel_input_oclose(int type,u_int32_t seq,void * ctxt)19857c478bd9Sstevel@tonic-gate channel_input_oclose(int type, u_int32_t seq, void *ctxt)
19867c478bd9Sstevel@tonic-gate {
19877c478bd9Sstevel@tonic-gate int id = packet_get_int();
19887c478bd9Sstevel@tonic-gate Channel *c = channel_lookup(id);
19897c478bd9Sstevel@tonic-gate
19907c478bd9Sstevel@tonic-gate packet_check_eom();
19917c478bd9Sstevel@tonic-gate if (c == NULL)
19927c478bd9Sstevel@tonic-gate packet_disconnect("Received oclose for nonexistent channel %d.", id);
19937c478bd9Sstevel@tonic-gate chan_rcvd_oclose(c);
19947c478bd9Sstevel@tonic-gate }
19957c478bd9Sstevel@tonic-gate
19967c478bd9Sstevel@tonic-gate void
channel_input_close_confirmation(int type,u_int32_t seq,void * ctxt)19977c478bd9Sstevel@tonic-gate channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
19987c478bd9Sstevel@tonic-gate {
19997c478bd9Sstevel@tonic-gate int id = packet_get_int();
20007c478bd9Sstevel@tonic-gate Channel *c = channel_lookup(id);
20017c478bd9Sstevel@tonic-gate
20027c478bd9Sstevel@tonic-gate packet_check_eom();
20037c478bd9Sstevel@tonic-gate if (c == NULL)
20047c478bd9Sstevel@tonic-gate packet_disconnect("Received close confirmation for "
20057c478bd9Sstevel@tonic-gate "out-of-range channel %d.", id);
20067c478bd9Sstevel@tonic-gate if (c->type != SSH_CHANNEL_CLOSED)
20077c478bd9Sstevel@tonic-gate packet_disconnect("Received close confirmation for "
20087c478bd9Sstevel@tonic-gate "non-closed channel %d (type %d).", id, c->type);
20097c478bd9Sstevel@tonic-gate channel_free(c);
20107c478bd9Sstevel@tonic-gate }
20117c478bd9Sstevel@tonic-gate
20127c478bd9Sstevel@tonic-gate void
channel_input_open_confirmation(int type,u_int32_t seq,void * ctxt)20137c478bd9Sstevel@tonic-gate channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
20147c478bd9Sstevel@tonic-gate {
20157c478bd9Sstevel@tonic-gate int id, remote_id;
20167c478bd9Sstevel@tonic-gate Channel *c;
20177c478bd9Sstevel@tonic-gate
20187c478bd9Sstevel@tonic-gate id = packet_get_int();
20197c478bd9Sstevel@tonic-gate c = channel_lookup(id);
20207c478bd9Sstevel@tonic-gate
20217c478bd9Sstevel@tonic-gate if (c==NULL || c->type != SSH_CHANNEL_OPENING)
20227c478bd9Sstevel@tonic-gate packet_disconnect("Received open confirmation for "
20237c478bd9Sstevel@tonic-gate "non-opening channel %d.", id);
20247c478bd9Sstevel@tonic-gate remote_id = packet_get_int();
20257c478bd9Sstevel@tonic-gate /* Record the remote channel number and mark that the channel is now open. */
20267c478bd9Sstevel@tonic-gate c->remote_id = remote_id;
20277c478bd9Sstevel@tonic-gate c->type = SSH_CHANNEL_OPEN;
20287c478bd9Sstevel@tonic-gate
20297c478bd9Sstevel@tonic-gate if (compat20) {
20307c478bd9Sstevel@tonic-gate c->remote_window = packet_get_int();
20317c478bd9Sstevel@tonic-gate c->remote_maxpacket = packet_get_int();
20327c478bd9Sstevel@tonic-gate if (c->confirm) {
20337c478bd9Sstevel@tonic-gate debug2("callback start");
20347c478bd9Sstevel@tonic-gate c->confirm(c->self, NULL);
20357c478bd9Sstevel@tonic-gate debug2("callback done");
20367c478bd9Sstevel@tonic-gate }
20377c478bd9Sstevel@tonic-gate debug("channel %d: open confirm rwindow %u rmax %u", c->self,
20387c478bd9Sstevel@tonic-gate c->remote_window, c->remote_maxpacket);
20397c478bd9Sstevel@tonic-gate }
20407c478bd9Sstevel@tonic-gate packet_check_eom();
20417c478bd9Sstevel@tonic-gate }
20427c478bd9Sstevel@tonic-gate
20437c478bd9Sstevel@tonic-gate static char *
reason2txt(int reason)20447c478bd9Sstevel@tonic-gate reason2txt(int reason)
20457c478bd9Sstevel@tonic-gate {
20467c478bd9Sstevel@tonic-gate switch (reason) {
20477c478bd9Sstevel@tonic-gate case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
20487c478bd9Sstevel@tonic-gate return "administratively prohibited";
20497c478bd9Sstevel@tonic-gate case SSH2_OPEN_CONNECT_FAILED:
20507c478bd9Sstevel@tonic-gate return "connect failed";
20517c478bd9Sstevel@tonic-gate case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
20527c478bd9Sstevel@tonic-gate return "unknown channel type";
20537c478bd9Sstevel@tonic-gate case SSH2_OPEN_RESOURCE_SHORTAGE:
20547c478bd9Sstevel@tonic-gate return "resource shortage";
20557c478bd9Sstevel@tonic-gate }
20567c478bd9Sstevel@tonic-gate return "unknown reason";
20577c478bd9Sstevel@tonic-gate }
20587c478bd9Sstevel@tonic-gate
20597c478bd9Sstevel@tonic-gate void
channel_input_open_failure(int type,u_int32_t seq,void * ctxt)20607c478bd9Sstevel@tonic-gate channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
20617c478bd9Sstevel@tonic-gate {
20627c478bd9Sstevel@tonic-gate int id, reason;
20637c478bd9Sstevel@tonic-gate char *msg = NULL, *lang = NULL;
20647c478bd9Sstevel@tonic-gate Channel *c;
20657c478bd9Sstevel@tonic-gate
20667c478bd9Sstevel@tonic-gate id = packet_get_int();
20677c478bd9Sstevel@tonic-gate c = channel_lookup(id);
20687c478bd9Sstevel@tonic-gate
20697c478bd9Sstevel@tonic-gate if (c==NULL || c->type != SSH_CHANNEL_OPENING)
20707c478bd9Sstevel@tonic-gate packet_disconnect("Received open failure for "
20717c478bd9Sstevel@tonic-gate "non-opening channel %d.", id);
20727c478bd9Sstevel@tonic-gate if (compat20) {
20737c478bd9Sstevel@tonic-gate reason = packet_get_int();
20747c478bd9Sstevel@tonic-gate if (!(datafellows & SSH_BUG_OPENFAILURE)) {
20757c478bd9Sstevel@tonic-gate msg = packet_get_string(NULL);
20767c478bd9Sstevel@tonic-gate lang = packet_get_string(NULL);
20777c478bd9Sstevel@tonic-gate }
20787c478bd9Sstevel@tonic-gate log("channel %d: open failed: %s%s%s", id,
20797c478bd9Sstevel@tonic-gate reason2txt(reason), msg ? ": ": "", msg ? msg : "");
20807c478bd9Sstevel@tonic-gate if (msg != NULL)
20817c478bd9Sstevel@tonic-gate xfree(msg);
20827c478bd9Sstevel@tonic-gate if (lang != NULL)
20837c478bd9Sstevel@tonic-gate xfree(lang);
20847c478bd9Sstevel@tonic-gate }
20857c478bd9Sstevel@tonic-gate packet_check_eom();
20867c478bd9Sstevel@tonic-gate /* Free the channel. This will also close the socket. */
20877c478bd9Sstevel@tonic-gate channel_free(c);
20887c478bd9Sstevel@tonic-gate }
20897c478bd9Sstevel@tonic-gate
20907c478bd9Sstevel@tonic-gate void
channel_input_window_adjust(int type,u_int32_t seq,void * ctxt)20917c478bd9Sstevel@tonic-gate channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
20927c478bd9Sstevel@tonic-gate {
20937c478bd9Sstevel@tonic-gate Channel *c;
20947c478bd9Sstevel@tonic-gate int id;
20957c478bd9Sstevel@tonic-gate u_int adjust;
20967c478bd9Sstevel@tonic-gate
20977c478bd9Sstevel@tonic-gate if (!compat20)
20987c478bd9Sstevel@tonic-gate return;
20997c478bd9Sstevel@tonic-gate
21007c478bd9Sstevel@tonic-gate /* Get the channel number and verify it. */
21017c478bd9Sstevel@tonic-gate id = packet_get_int();
21027c478bd9Sstevel@tonic-gate c = channel_lookup(id);
21037c478bd9Sstevel@tonic-gate
21047c478bd9Sstevel@tonic-gate if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
21057c478bd9Sstevel@tonic-gate log("Received window adjust for "
21067c478bd9Sstevel@tonic-gate "non-open channel %d.", id);
21077c478bd9Sstevel@tonic-gate return;
21087c478bd9Sstevel@tonic-gate }
21097c478bd9Sstevel@tonic-gate adjust = packet_get_int();
21107c478bd9Sstevel@tonic-gate packet_check_eom();
21117c478bd9Sstevel@tonic-gate debug2("channel %d: rcvd adjust %u", id, adjust);
21127c478bd9Sstevel@tonic-gate c->remote_window += adjust;
21137c478bd9Sstevel@tonic-gate }
21147c478bd9Sstevel@tonic-gate
21157c478bd9Sstevel@tonic-gate void
channel_input_port_open(int type,u_int32_t seq,void * ctxt)21167c478bd9Sstevel@tonic-gate channel_input_port_open(int type, u_int32_t seq, void *ctxt)
21177c478bd9Sstevel@tonic-gate {
21187c478bd9Sstevel@tonic-gate Channel *c = NULL;
21197c478bd9Sstevel@tonic-gate u_short host_port;
21207c478bd9Sstevel@tonic-gate char *host, *originator_string;
21217c478bd9Sstevel@tonic-gate int remote_id, sock = -1;
21227c478bd9Sstevel@tonic-gate
21237c478bd9Sstevel@tonic-gate remote_id = packet_get_int();
21247c478bd9Sstevel@tonic-gate host = packet_get_string(NULL);
21257c478bd9Sstevel@tonic-gate host_port = packet_get_int();
21267c478bd9Sstevel@tonic-gate
21277c478bd9Sstevel@tonic-gate if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
21287c478bd9Sstevel@tonic-gate originator_string = packet_get_string(NULL);
21297c478bd9Sstevel@tonic-gate } else {
21307c478bd9Sstevel@tonic-gate originator_string = xstrdup("unknown (remote did not supply name)");
21317c478bd9Sstevel@tonic-gate }
21327c478bd9Sstevel@tonic-gate packet_check_eom();
21337c478bd9Sstevel@tonic-gate sock = channel_connect_to(host, host_port);
21347c478bd9Sstevel@tonic-gate if (sock != -1) {
21357c478bd9Sstevel@tonic-gate c = channel_new("connected socket",
21367c478bd9Sstevel@tonic-gate SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
21377c478bd9Sstevel@tonic-gate originator_string, 1);
21387c478bd9Sstevel@tonic-gate c->remote_id = remote_id;
21397c478bd9Sstevel@tonic-gate }
21407c478bd9Sstevel@tonic-gate if (c == NULL) {
21417c478bd9Sstevel@tonic-gate packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
21427c478bd9Sstevel@tonic-gate packet_put_int(remote_id);
21437c478bd9Sstevel@tonic-gate packet_send();
21447c478bd9Sstevel@tonic-gate }
21457c478bd9Sstevel@tonic-gate xfree(host);
21467c478bd9Sstevel@tonic-gate }
21477c478bd9Sstevel@tonic-gate
21487c478bd9Sstevel@tonic-gate
21497c478bd9Sstevel@tonic-gate /* -- tcp forwarding */
21507c478bd9Sstevel@tonic-gate
21517c478bd9Sstevel@tonic-gate void
channel_set_af(int af)21527c478bd9Sstevel@tonic-gate channel_set_af(int af)
21537c478bd9Sstevel@tonic-gate {
21547c478bd9Sstevel@tonic-gate IPv4or6 = af;
21557c478bd9Sstevel@tonic-gate }
21567c478bd9Sstevel@tonic-gate
21577c478bd9Sstevel@tonic-gate static int
channel_setup_fwd_listener(int type,const char * listen_addr,u_short listen_port,const char * host_to_connect,u_short port_to_connect,int gateway_ports)21587c478bd9Sstevel@tonic-gate channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
21597c478bd9Sstevel@tonic-gate const char *host_to_connect, u_short port_to_connect, int gateway_ports)
21607c478bd9Sstevel@tonic-gate {
21617c478bd9Sstevel@tonic-gate Channel *c;
21629b03ea0fSjp161948 int sock, r, is_client, on = 1, wildcard = 0, success = 0;
21637c478bd9Sstevel@tonic-gate struct addrinfo hints, *ai, *aitop;
21649b03ea0fSjp161948 const char *host, *addr;
21657c478bd9Sstevel@tonic-gate char ntop[NI_MAXHOST], strport[NI_MAXSERV];
21667c478bd9Sstevel@tonic-gate
21677c478bd9Sstevel@tonic-gate host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
21687c478bd9Sstevel@tonic-gate listen_addr : host_to_connect;
21699b03ea0fSjp161948 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
21707c478bd9Sstevel@tonic-gate
21717c478bd9Sstevel@tonic-gate if (host == NULL) {
21727c478bd9Sstevel@tonic-gate error("No forward host name.");
21739b03ea0fSjp161948 return 0;
21747c478bd9Sstevel@tonic-gate }
21757c478bd9Sstevel@tonic-gate if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
21767c478bd9Sstevel@tonic-gate error("Forward host name too long.");
21779b03ea0fSjp161948 return 0;
21787c478bd9Sstevel@tonic-gate }
21797c478bd9Sstevel@tonic-gate
21807c478bd9Sstevel@tonic-gate /*
21819b03ea0fSjp161948 * Determine whether or not a port forward listens to loopback,
21829b03ea0fSjp161948 * specified address or wildcard. On the client, a specified bind
21839b03ea0fSjp161948 * address will always override gateway_ports. On the server, a
21849b03ea0fSjp161948 * gateway_ports of 1 (``yes'') will override the client's
21859b03ea0fSjp161948 * specification and force a wildcard bind, whereas a value of 2
21869b03ea0fSjp161948 * (``clientspecified'') will bind to whatever address the client
21879b03ea0fSjp161948 * asked for.
21889b03ea0fSjp161948 *
21899b03ea0fSjp161948 * Special-case listen_addrs are:
21909b03ea0fSjp161948 *
21919b03ea0fSjp161948 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
21929b03ea0fSjp161948 * "" (empty string), "*" -> wildcard v4/v6
21939b03ea0fSjp161948 * "localhost" -> loopback v4/v6
21949b03ea0fSjp161948 */
21959b03ea0fSjp161948 addr = NULL;
21969b03ea0fSjp161948 if (listen_addr == NULL) {
21979b03ea0fSjp161948 /* No address specified: default to gateway_ports setting */
21989b03ea0fSjp161948 if (gateway_ports)
21999b03ea0fSjp161948 wildcard = 1;
22009b03ea0fSjp161948 } else if (gateway_ports || is_client) {
22019b03ea0fSjp161948 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
22029b03ea0fSjp161948 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
22039b03ea0fSjp161948 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
22049b03ea0fSjp161948 (!is_client && gateway_ports == 1))
22059b03ea0fSjp161948 wildcard = 1;
22069b03ea0fSjp161948 else if (strcmp(listen_addr, "localhost") != 0)
22079b03ea0fSjp161948 addr = listen_addr;
22089b03ea0fSjp161948 }
22099b03ea0fSjp161948
22109b03ea0fSjp161948 debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s",
22119b03ea0fSjp161948 type, wildcard, (addr == NULL) ? "NULL" : addr);
22129b03ea0fSjp161948
22139b03ea0fSjp161948 /*
22147c478bd9Sstevel@tonic-gate * getaddrinfo returns a loopback address if the hostname is
22157c478bd9Sstevel@tonic-gate * set to NULL and hints.ai_flags is not AI_PASSIVE
22167c478bd9Sstevel@tonic-gate */
22177c478bd9Sstevel@tonic-gate memset(&hints, 0, sizeof(hints));
22187c478bd9Sstevel@tonic-gate hints.ai_family = IPv4or6;
22199b03ea0fSjp161948 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
22207c478bd9Sstevel@tonic-gate hints.ai_socktype = SOCK_STREAM;
22217c478bd9Sstevel@tonic-gate snprintf(strport, sizeof strport, "%d", listen_port);
22229b03ea0fSjp161948 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
22239b03ea0fSjp161948 if (addr == NULL) {
22249b03ea0fSjp161948 /* This really shouldn't happen */
22259b03ea0fSjp161948 packet_disconnect("getaddrinfo: fatal error: %s",
22269b03ea0fSjp161948 gai_strerror(r));
22279b03ea0fSjp161948 } else {
22289b03ea0fSjp161948 error("channel_setup_fwd_listener: "
22299b03ea0fSjp161948 "getaddrinfo(%.64s): %s", addr, gai_strerror(r));
22309b03ea0fSjp161948 }
22319b03ea0fSjp161948 return 0;
22329b03ea0fSjp161948 }
22337c478bd9Sstevel@tonic-gate
22347c478bd9Sstevel@tonic-gate for (ai = aitop; ai; ai = ai->ai_next) {
22357c478bd9Sstevel@tonic-gate if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
22367c478bd9Sstevel@tonic-gate continue;
22377c478bd9Sstevel@tonic-gate if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
22387c478bd9Sstevel@tonic-gate strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
22397c478bd9Sstevel@tonic-gate error("channel_setup_fwd_listener: getnameinfo failed");
22407c478bd9Sstevel@tonic-gate continue;
22417c478bd9Sstevel@tonic-gate }
22427c478bd9Sstevel@tonic-gate /* Create a port to listen for the host. */
22439b03ea0fSjp161948 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
22447c478bd9Sstevel@tonic-gate if (sock < 0) {
22457c478bd9Sstevel@tonic-gate /* this is no error since kernel may not support ipv6 */
22467c478bd9Sstevel@tonic-gate verbose("socket: %.100s", strerror(errno));
22477c478bd9Sstevel@tonic-gate continue;
22487c478bd9Sstevel@tonic-gate }
22497c478bd9Sstevel@tonic-gate /*
22507c478bd9Sstevel@tonic-gate * Set socket options.
22517c478bd9Sstevel@tonic-gate * Allow local port reuse in TIME_WAIT.
22527c478bd9Sstevel@tonic-gate */
22537c478bd9Sstevel@tonic-gate if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on,
22547c478bd9Sstevel@tonic-gate sizeof(on)) == -1)
22557c478bd9Sstevel@tonic-gate error("setsockopt SO_REUSEADDR: %s", strerror(errno));
22567c478bd9Sstevel@tonic-gate
22577c478bd9Sstevel@tonic-gate debug("Local forwarding listening on %s port %s.", ntop, strport);
22587c478bd9Sstevel@tonic-gate
22597c478bd9Sstevel@tonic-gate /* Bind the socket to the address. */
22607c478bd9Sstevel@tonic-gate if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
22617c478bd9Sstevel@tonic-gate /* address can be in use ipv6 address is already bound */
22627c478bd9Sstevel@tonic-gate if (!ai->ai_next)
22637c478bd9Sstevel@tonic-gate error("bind: %.100s", strerror(errno));
22647c478bd9Sstevel@tonic-gate else
22657c478bd9Sstevel@tonic-gate verbose("bind: %.100s", strerror(errno));
22667c478bd9Sstevel@tonic-gate
22677c478bd9Sstevel@tonic-gate close(sock);
22687c478bd9Sstevel@tonic-gate continue;
22697c478bd9Sstevel@tonic-gate }
22707c478bd9Sstevel@tonic-gate /* Start listening for connections on the socket. */
22717c478bd9Sstevel@tonic-gate if (listen(sock, 5) < 0) {
22727c478bd9Sstevel@tonic-gate error("listen: %.100s", strerror(errno));
22737c478bd9Sstevel@tonic-gate close(sock);
22747c478bd9Sstevel@tonic-gate continue;
22757c478bd9Sstevel@tonic-gate }
22767c478bd9Sstevel@tonic-gate /* Allocate a channel number for the socket. */
22777c478bd9Sstevel@tonic-gate c = channel_new("port listener", type, sock, sock, -1,
22787c478bd9Sstevel@tonic-gate CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
22797c478bd9Sstevel@tonic-gate 0, xstrdup("port listener"), 1);
22807c478bd9Sstevel@tonic-gate strlcpy(c->path, host, sizeof(c->path));
22817c478bd9Sstevel@tonic-gate c->host_port = port_to_connect;
22827c478bd9Sstevel@tonic-gate c->listening_port = listen_port;
22837c478bd9Sstevel@tonic-gate success = 1;
22847c478bd9Sstevel@tonic-gate }
22857c478bd9Sstevel@tonic-gate if (success == 0)
22867c478bd9Sstevel@tonic-gate error("channel_setup_fwd_listener: cannot listen to port: %d",
22877c478bd9Sstevel@tonic-gate listen_port);
22887c478bd9Sstevel@tonic-gate freeaddrinfo(aitop);
22897c478bd9Sstevel@tonic-gate return success;
22907c478bd9Sstevel@tonic-gate }
22917c478bd9Sstevel@tonic-gate
22929b03ea0fSjp161948 int
channel_cancel_rport_listener(const char * host,u_short port)22939b03ea0fSjp161948 channel_cancel_rport_listener(const char *host, u_short port)
22949b03ea0fSjp161948 {
22959b03ea0fSjp161948 u_int i;
22969b03ea0fSjp161948 int found = 0;
22979b03ea0fSjp161948
22989b03ea0fSjp161948 for (i = 0; i < channels_alloc; i++) {
22999b03ea0fSjp161948 Channel *c = channels[i];
23009b03ea0fSjp161948
23019b03ea0fSjp161948 if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
23029b03ea0fSjp161948 strncmp(c->path, host, sizeof(c->path)) == 0 &&
23039b03ea0fSjp161948 c->listening_port == port) {
23049b03ea0fSjp161948 debug2("%s: close channel %d", __func__, i);
23059b03ea0fSjp161948 channel_free(c);
23069b03ea0fSjp161948 found = 1;
23079b03ea0fSjp161948 }
23089b03ea0fSjp161948 }
23099b03ea0fSjp161948
23109b03ea0fSjp161948 return (found);
23119b03ea0fSjp161948 }
23129b03ea0fSjp161948
23137c478bd9Sstevel@tonic-gate /* protocol local port fwd, used by ssh (and sshd in v1) */
23147c478bd9Sstevel@tonic-gate int
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)23159b03ea0fSjp161948 channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
23167c478bd9Sstevel@tonic-gate const char *host_to_connect, u_short port_to_connect, int gateway_ports)
23177c478bd9Sstevel@tonic-gate {
23187c478bd9Sstevel@tonic-gate return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
23199b03ea0fSjp161948 listen_host, listen_port, host_to_connect, port_to_connect,
23209b03ea0fSjp161948 gateway_ports);
23217c478bd9Sstevel@tonic-gate }
23227c478bd9Sstevel@tonic-gate
23237c478bd9Sstevel@tonic-gate /* protocol v2 remote port fwd, used by sshd */
23247c478bd9Sstevel@tonic-gate int
channel_setup_remote_fwd_listener(const char * listen_address,u_short listen_port,int gateway_ports)23257c478bd9Sstevel@tonic-gate channel_setup_remote_fwd_listener(const char *listen_address,
23267c478bd9Sstevel@tonic-gate u_short listen_port, int gateway_ports)
23277c478bd9Sstevel@tonic-gate {
23287c478bd9Sstevel@tonic-gate return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
23297c478bd9Sstevel@tonic-gate listen_address, listen_port, NULL, 0, gateway_ports);
23307c478bd9Sstevel@tonic-gate }
23317c478bd9Sstevel@tonic-gate
23327c478bd9Sstevel@tonic-gate /*
23337c478bd9Sstevel@tonic-gate * Initiate forwarding of connections to port "port" on remote host through
23347c478bd9Sstevel@tonic-gate * the secure channel to host:port from local side.
23357c478bd9Sstevel@tonic-gate */
23367c478bd9Sstevel@tonic-gate
23379b03ea0fSjp161948 int
channel_request_remote_forwarding(const char * listen_host,u_short listen_port,const char * host_to_connect,u_short port_to_connect)23389b03ea0fSjp161948 channel_request_remote_forwarding(const char *listen_host, u_short listen_port,
23397c478bd9Sstevel@tonic-gate const char *host_to_connect, u_short port_to_connect)
23407c478bd9Sstevel@tonic-gate {
23417c478bd9Sstevel@tonic-gate int type, success = 0;
23427c478bd9Sstevel@tonic-gate
23437c478bd9Sstevel@tonic-gate /* Record locally that connection to this host/port is permitted. */
23447c478bd9Sstevel@tonic-gate if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
23457c478bd9Sstevel@tonic-gate fatal("channel_request_remote_forwarding: too many forwards");
23467c478bd9Sstevel@tonic-gate
23479b03ea0fSjp161948 if (listen_host != NULL &&
23489b03ea0fSjp161948 strlen(listen_host) > SSH_CHANNEL_PATH_LEN - 1) {
23499b03ea0fSjp161948 error("Binding address too long.");
23509b03ea0fSjp161948 return -1;
23519b03ea0fSjp161948 }
23529b03ea0fSjp161948
23537c478bd9Sstevel@tonic-gate /* Send the forward request to the remote side. */
23547c478bd9Sstevel@tonic-gate if (compat20) {
23559b03ea0fSjp161948 const char *address_to_bind;
23569b03ea0fSjp161948 if (listen_host == NULL) {
23579b03ea0fSjp161948 if (datafellows & SSH_BUG_RFWD_ADDR)
23589b03ea0fSjp161948 address_to_bind = "127.0.0.1";
23599b03ea0fSjp161948 else
23609b03ea0fSjp161948 address_to_bind = "localhost";
23619b03ea0fSjp161948 } else if (*listen_host == '\0' ||
23629b03ea0fSjp161948 strcmp(listen_host, "*") == 0) {
23639b03ea0fSjp161948 if (datafellows & SSH_BUG_RFWD_ADDR)
23649b03ea0fSjp161948 address_to_bind = "0.0.0.0";
23659b03ea0fSjp161948 else
23669b03ea0fSjp161948 address_to_bind = "";
23679b03ea0fSjp161948 } else
23689b03ea0fSjp161948 address_to_bind = listen_host;
23699b03ea0fSjp161948
23707c478bd9Sstevel@tonic-gate packet_start(SSH2_MSG_GLOBAL_REQUEST);
23717c478bd9Sstevel@tonic-gate packet_put_cstring("tcpip-forward");
23727c478bd9Sstevel@tonic-gate packet_put_char(1); /* boolean: want reply */
23737c478bd9Sstevel@tonic-gate packet_put_cstring(address_to_bind);
23747c478bd9Sstevel@tonic-gate packet_put_int(listen_port);
23757c478bd9Sstevel@tonic-gate packet_send();
23767c478bd9Sstevel@tonic-gate packet_write_wait();
23777c478bd9Sstevel@tonic-gate /* Assume that server accepts the request */
23787c478bd9Sstevel@tonic-gate success = 1;
23797c478bd9Sstevel@tonic-gate } else {
23807c478bd9Sstevel@tonic-gate packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
23817c478bd9Sstevel@tonic-gate packet_put_int(listen_port);
23827c478bd9Sstevel@tonic-gate packet_put_cstring(host_to_connect);
23837c478bd9Sstevel@tonic-gate packet_put_int(port_to_connect);
23847c478bd9Sstevel@tonic-gate packet_send();
23857c478bd9Sstevel@tonic-gate packet_write_wait();
23867c478bd9Sstevel@tonic-gate
23877c478bd9Sstevel@tonic-gate /* Wait for response from the remote side. */
23887c478bd9Sstevel@tonic-gate type = packet_read();
23897c478bd9Sstevel@tonic-gate switch (type) {
23907c478bd9Sstevel@tonic-gate case SSH_SMSG_SUCCESS:
23917c478bd9Sstevel@tonic-gate success = 1;
23927c478bd9Sstevel@tonic-gate break;
23937c478bd9Sstevel@tonic-gate case SSH_SMSG_FAILURE:
23947c478bd9Sstevel@tonic-gate log("Warning: Server denied remote port forwarding.");
23957c478bd9Sstevel@tonic-gate break;
23967c478bd9Sstevel@tonic-gate default:
23977c478bd9Sstevel@tonic-gate /* Unknown packet */
23987c478bd9Sstevel@tonic-gate packet_disconnect("Protocol error for port forward request:"
23997c478bd9Sstevel@tonic-gate "received packet type %d.", type);
24007c478bd9Sstevel@tonic-gate }
24017c478bd9Sstevel@tonic-gate }
24027c478bd9Sstevel@tonic-gate if (success) {
24037c478bd9Sstevel@tonic-gate permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
24047c478bd9Sstevel@tonic-gate permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
24057c478bd9Sstevel@tonic-gate permitted_opens[num_permitted_opens].listen_port = listen_port;
24067c478bd9Sstevel@tonic-gate num_permitted_opens++;
24077c478bd9Sstevel@tonic-gate }
24089b03ea0fSjp161948 return (success ? 0 : -1);
24099b03ea0fSjp161948 }
24109b03ea0fSjp161948
24119b03ea0fSjp161948 /*
24129b03ea0fSjp161948 * Request cancellation of remote forwarding of connection host:port from
24139b03ea0fSjp161948 * local side.
24149b03ea0fSjp161948 */
24159b03ea0fSjp161948 void
channel_request_rforward_cancel(const char * host,u_short port)24169b03ea0fSjp161948 channel_request_rforward_cancel(const char *host, u_short port)
24179b03ea0fSjp161948 {
24189b03ea0fSjp161948 int i;
24199b03ea0fSjp161948
24209b03ea0fSjp161948 if (!compat20)
24219b03ea0fSjp161948 return;
24229b03ea0fSjp161948
24239b03ea0fSjp161948 for (i = 0; i < num_permitted_opens; i++) {
24249b03ea0fSjp161948 if (permitted_opens[i].host_to_connect != NULL &&
24259b03ea0fSjp161948 permitted_opens[i].listen_port == port)
24269b03ea0fSjp161948 break;
24279b03ea0fSjp161948 }
24289b03ea0fSjp161948 if (i >= num_permitted_opens) {
24299b03ea0fSjp161948 debug("%s: requested forward not found", __func__);
24309b03ea0fSjp161948 return;
24319b03ea0fSjp161948 }
24329b03ea0fSjp161948 packet_start(SSH2_MSG_GLOBAL_REQUEST);
24339b03ea0fSjp161948 packet_put_cstring("cancel-tcpip-forward");
24349b03ea0fSjp161948 packet_put_char(0);
24359b03ea0fSjp161948 packet_put_cstring(host == NULL ? "" : host);
24369b03ea0fSjp161948 packet_put_int(port);
24379b03ea0fSjp161948 packet_send();
24389b03ea0fSjp161948
24399b03ea0fSjp161948 permitted_opens[i].listen_port = 0;
24409b03ea0fSjp161948 permitted_opens[i].port_to_connect = 0;
24419b03ea0fSjp161948 xfree(permitted_opens[i].host_to_connect);
24429b03ea0fSjp161948 permitted_opens[i].host_to_connect = NULL;
24437c478bd9Sstevel@tonic-gate }
24447c478bd9Sstevel@tonic-gate
24457c478bd9Sstevel@tonic-gate /*
24467c478bd9Sstevel@tonic-gate * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
24477c478bd9Sstevel@tonic-gate * listening for the port, and sends back a success reply (or disconnect
24487c478bd9Sstevel@tonic-gate * message if there was an error). This never returns if there was an error.
24497c478bd9Sstevel@tonic-gate */
24507c478bd9Sstevel@tonic-gate
24517c478bd9Sstevel@tonic-gate void
channel_input_port_forward_request(int is_root,int gateway_ports)24527c478bd9Sstevel@tonic-gate channel_input_port_forward_request(int is_root, int gateway_ports)
24537c478bd9Sstevel@tonic-gate {
24547c478bd9Sstevel@tonic-gate u_short port, host_port;
24557c478bd9Sstevel@tonic-gate char *hostname;
24567c478bd9Sstevel@tonic-gate
24577c478bd9Sstevel@tonic-gate /* Get arguments from the packet. */
24587c478bd9Sstevel@tonic-gate port = packet_get_int();
24597c478bd9Sstevel@tonic-gate hostname = packet_get_string(NULL);
24607c478bd9Sstevel@tonic-gate host_port = packet_get_int();
24617c478bd9Sstevel@tonic-gate
24627c478bd9Sstevel@tonic-gate #ifndef HAVE_CYGWIN
24637c478bd9Sstevel@tonic-gate /*
24647c478bd9Sstevel@tonic-gate * Check that an unprivileged user is not trying to forward a
24657c478bd9Sstevel@tonic-gate * privileged port.
24667c478bd9Sstevel@tonic-gate */
24677c478bd9Sstevel@tonic-gate if (port < IPPORT_RESERVED && !is_root)
24687c478bd9Sstevel@tonic-gate packet_disconnect("Requested forwarding of port %d but user is not root.",
24697c478bd9Sstevel@tonic-gate port);
24707c478bd9Sstevel@tonic-gate #endif
24717c478bd9Sstevel@tonic-gate /* Initiate forwarding */
24729b03ea0fSjp161948 channel_setup_local_fwd_listener(NULL, port, hostname,
24739b03ea0fSjp161948 host_port, gateway_ports);
24747c478bd9Sstevel@tonic-gate
24757c478bd9Sstevel@tonic-gate /* Free the argument string. */
24767c478bd9Sstevel@tonic-gate xfree(hostname);
24777c478bd9Sstevel@tonic-gate }
24787c478bd9Sstevel@tonic-gate
24797c478bd9Sstevel@tonic-gate /*
24807c478bd9Sstevel@tonic-gate * Permits opening to any host/port if permitted_opens[] is empty. This is
24817c478bd9Sstevel@tonic-gate * usually called by the server, because the user could connect to any port
24827c478bd9Sstevel@tonic-gate * anyway, and the server has no way to know but to trust the client anyway.
24837c478bd9Sstevel@tonic-gate */
24847c478bd9Sstevel@tonic-gate void
channel_permit_all_opens(void)24857c478bd9Sstevel@tonic-gate channel_permit_all_opens(void)
24867c478bd9Sstevel@tonic-gate {
24877c478bd9Sstevel@tonic-gate if (num_permitted_opens == 0)
24887c478bd9Sstevel@tonic-gate all_opens_permitted = 1;
24897c478bd9Sstevel@tonic-gate }
24907c478bd9Sstevel@tonic-gate
24917c478bd9Sstevel@tonic-gate void
channel_add_permitted_opens(char * host,int port)24927c478bd9Sstevel@tonic-gate channel_add_permitted_opens(char *host, int port)
24937c478bd9Sstevel@tonic-gate {
24947c478bd9Sstevel@tonic-gate if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
24959b03ea0fSjp161948 fatal("channel_add_permitted_opens: too many forwards");
24967c478bd9Sstevel@tonic-gate debug("allow port forwarding to host %s port %d", host, port);
24977c478bd9Sstevel@tonic-gate
24987c478bd9Sstevel@tonic-gate permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
24997c478bd9Sstevel@tonic-gate permitted_opens[num_permitted_opens].port_to_connect = port;
25007c478bd9Sstevel@tonic-gate num_permitted_opens++;
25017c478bd9Sstevel@tonic-gate
25027c478bd9Sstevel@tonic-gate all_opens_permitted = 0;
25037c478bd9Sstevel@tonic-gate }
25047c478bd9Sstevel@tonic-gate
25057c478bd9Sstevel@tonic-gate void
channel_clear_permitted_opens(void)25067c478bd9Sstevel@tonic-gate channel_clear_permitted_opens(void)
25077c478bd9Sstevel@tonic-gate {
25087c478bd9Sstevel@tonic-gate int i;
25097c478bd9Sstevel@tonic-gate
25107c478bd9Sstevel@tonic-gate for (i = 0; i < num_permitted_opens; i++)
25117c478bd9Sstevel@tonic-gate xfree(permitted_opens[i].host_to_connect);
25127c478bd9Sstevel@tonic-gate num_permitted_opens = 0;
25137c478bd9Sstevel@tonic-gate }
25147c478bd9Sstevel@tonic-gate
25157c478bd9Sstevel@tonic-gate
25167c478bd9Sstevel@tonic-gate /* return socket to remote host, port */
25177c478bd9Sstevel@tonic-gate static int
connect_to(const char * host,u_short port)25187c478bd9Sstevel@tonic-gate connect_to(const char *host, u_short port)
25197c478bd9Sstevel@tonic-gate {
25207c478bd9Sstevel@tonic-gate struct addrinfo hints, *ai, *aitop;
25217c478bd9Sstevel@tonic-gate char ntop[NI_MAXHOST], strport[NI_MAXSERV];
25227c478bd9Sstevel@tonic-gate int gaierr;
25237c478bd9Sstevel@tonic-gate int sock = -1;
25247c478bd9Sstevel@tonic-gate
25257c478bd9Sstevel@tonic-gate memset(&hints, 0, sizeof(hints));
25267c478bd9Sstevel@tonic-gate hints.ai_family = IPv4or6;
25277c478bd9Sstevel@tonic-gate hints.ai_socktype = SOCK_STREAM;
25287c478bd9Sstevel@tonic-gate snprintf(strport, sizeof strport, "%d", port);
25297c478bd9Sstevel@tonic-gate if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
25307c478bd9Sstevel@tonic-gate error("connect_to %.100s: unknown host (%s)", host,
25317c478bd9Sstevel@tonic-gate gai_strerror(gaierr));
25327c478bd9Sstevel@tonic-gate return -1;
25337c478bd9Sstevel@tonic-gate }
25347c478bd9Sstevel@tonic-gate for (ai = aitop; ai; ai = ai->ai_next) {
25357c478bd9Sstevel@tonic-gate if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
25367c478bd9Sstevel@tonic-gate continue;
25377c478bd9Sstevel@tonic-gate if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
25387c478bd9Sstevel@tonic-gate strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
25397c478bd9Sstevel@tonic-gate error("connect_to: getnameinfo failed");
25407c478bd9Sstevel@tonic-gate continue;
25417c478bd9Sstevel@tonic-gate }
25427c478bd9Sstevel@tonic-gate sock = socket(ai->ai_family, SOCK_STREAM, 0);
25437c478bd9Sstevel@tonic-gate if (sock < 0) {
25447c478bd9Sstevel@tonic-gate error("socket: %.100s", strerror(errno));
25457c478bd9Sstevel@tonic-gate continue;
25467c478bd9Sstevel@tonic-gate }
25477c478bd9Sstevel@tonic-gate if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
25487c478bd9Sstevel@tonic-gate fatal("connect_to: F_SETFL: %s", strerror(errno));
25497c478bd9Sstevel@tonic-gate if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
25507c478bd9Sstevel@tonic-gate errno != EINPROGRESS) {
25517c478bd9Sstevel@tonic-gate error("connect_to %.100s port %s: %.100s", ntop, strport,
25527c478bd9Sstevel@tonic-gate strerror(errno));
25537c478bd9Sstevel@tonic-gate close(sock);
25547c478bd9Sstevel@tonic-gate continue; /* fail -- try next */
25557c478bd9Sstevel@tonic-gate }
25567c478bd9Sstevel@tonic-gate break; /* success */
25577c478bd9Sstevel@tonic-gate
25587c478bd9Sstevel@tonic-gate }
25597c478bd9Sstevel@tonic-gate freeaddrinfo(aitop);
25607c478bd9Sstevel@tonic-gate if (!ai) {
25617c478bd9Sstevel@tonic-gate error("connect_to %.100s port %d: failed.", host, port);
25627c478bd9Sstevel@tonic-gate return -1;
25637c478bd9Sstevel@tonic-gate }
25647c478bd9Sstevel@tonic-gate /* success */
25657c478bd9Sstevel@tonic-gate set_nodelay(sock);
25667c478bd9Sstevel@tonic-gate return sock;
25677c478bd9Sstevel@tonic-gate }
25687c478bd9Sstevel@tonic-gate
25697c478bd9Sstevel@tonic-gate int
channel_connect_by_listen_address(u_short listen_port)25707c478bd9Sstevel@tonic-gate channel_connect_by_listen_address(u_short listen_port)
25717c478bd9Sstevel@tonic-gate {
25727c478bd9Sstevel@tonic-gate int i;
25737c478bd9Sstevel@tonic-gate
25747c478bd9Sstevel@tonic-gate for (i = 0; i < num_permitted_opens; i++)
25757c478bd9Sstevel@tonic-gate if (permitted_opens[i].listen_port == listen_port)
25767c478bd9Sstevel@tonic-gate return connect_to(
25777c478bd9Sstevel@tonic-gate permitted_opens[i].host_to_connect,
25787c478bd9Sstevel@tonic-gate permitted_opens[i].port_to_connect);
25797c478bd9Sstevel@tonic-gate error("WARNING: Server requests forwarding for unknown listen_port %d",
25807c478bd9Sstevel@tonic-gate listen_port);
25817c478bd9Sstevel@tonic-gate return -1;
25827c478bd9Sstevel@tonic-gate }
25837c478bd9Sstevel@tonic-gate
25847c478bd9Sstevel@tonic-gate /* Check if connecting to that port is permitted and connect. */
25857c478bd9Sstevel@tonic-gate int
channel_connect_to(const char * host,u_short port)25867c478bd9Sstevel@tonic-gate channel_connect_to(const char *host, u_short port)
25877c478bd9Sstevel@tonic-gate {
25887c478bd9Sstevel@tonic-gate int i, permit;
25897c478bd9Sstevel@tonic-gate
25907c478bd9Sstevel@tonic-gate permit = all_opens_permitted;
25917c478bd9Sstevel@tonic-gate if (!permit) {
25927c478bd9Sstevel@tonic-gate for (i = 0; i < num_permitted_opens; i++)
25937c478bd9Sstevel@tonic-gate if (permitted_opens[i].port_to_connect == port &&
25947c478bd9Sstevel@tonic-gate strcmp(permitted_opens[i].host_to_connect, host) == 0)
25957c478bd9Sstevel@tonic-gate permit = 1;
25967c478bd9Sstevel@tonic-gate
25977c478bd9Sstevel@tonic-gate }
25987c478bd9Sstevel@tonic-gate if (!permit) {
25997c478bd9Sstevel@tonic-gate log("Received request to connect to host %.100s port %d, "
26007c478bd9Sstevel@tonic-gate "but the request was denied.", host, port);
26017c478bd9Sstevel@tonic-gate return -1;
26027c478bd9Sstevel@tonic-gate }
26037c478bd9Sstevel@tonic-gate return connect_to(host, port);
26047c478bd9Sstevel@tonic-gate }
26057c478bd9Sstevel@tonic-gate
26067c478bd9Sstevel@tonic-gate /* -- X11 forwarding */
26077c478bd9Sstevel@tonic-gate
26087c478bd9Sstevel@tonic-gate /*
26097c478bd9Sstevel@tonic-gate * Creates an internet domain socket for listening for X11 connections.
26107c478bd9Sstevel@tonic-gate * Returns 0 and a suitable display number for the DISPLAY variable
26117c478bd9Sstevel@tonic-gate * stored in display_numberp , or -1 if an error occurs.
26127c478bd9Sstevel@tonic-gate */
26137c478bd9Sstevel@tonic-gate int
x11_create_display_inet(int x11_display_offset,int x11_use_localhost,int single_connection,u_int * display_numberp)26147c478bd9Sstevel@tonic-gate x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
26157c478bd9Sstevel@tonic-gate int single_connection, u_int *display_numberp)
26167c478bd9Sstevel@tonic-gate {
26177c478bd9Sstevel@tonic-gate Channel *nc = NULL;
26187c478bd9Sstevel@tonic-gate int display_number, sock;
26197c478bd9Sstevel@tonic-gate u_short port;
26207c478bd9Sstevel@tonic-gate struct addrinfo hints, *ai, *aitop;
26217c478bd9Sstevel@tonic-gate char strport[NI_MAXSERV];
26227c478bd9Sstevel@tonic-gate int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
26237c478bd9Sstevel@tonic-gate
26247c478bd9Sstevel@tonic-gate for (display_number = x11_display_offset;
26257c478bd9Sstevel@tonic-gate display_number < MAX_DISPLAYS;
26267c478bd9Sstevel@tonic-gate display_number++) {
26277c478bd9Sstevel@tonic-gate port = 6000 + display_number;
26287c478bd9Sstevel@tonic-gate memset(&hints, 0, sizeof(hints));
26297c478bd9Sstevel@tonic-gate hints.ai_family = IPv4or6;
26307c478bd9Sstevel@tonic-gate hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
26317c478bd9Sstevel@tonic-gate hints.ai_socktype = SOCK_STREAM;
26327c478bd9Sstevel@tonic-gate snprintf(strport, sizeof strport, "%d", port);
26337c478bd9Sstevel@tonic-gate if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
26347c478bd9Sstevel@tonic-gate error("getaddrinfo: %.100s", gai_strerror(gaierr));
26357c478bd9Sstevel@tonic-gate return -1;
26367c478bd9Sstevel@tonic-gate }
26377c478bd9Sstevel@tonic-gate for (ai = aitop; ai; ai = ai->ai_next) {
26387c478bd9Sstevel@tonic-gate if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
26397c478bd9Sstevel@tonic-gate continue;
26407c478bd9Sstevel@tonic-gate sock = socket(ai->ai_family, SOCK_STREAM, 0);
26417c478bd9Sstevel@tonic-gate if (sock < 0) {
26427c478bd9Sstevel@tonic-gate if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
26437c478bd9Sstevel@tonic-gate error("socket: %.100s", strerror(errno));
264459b1e613SZdenek Kotala freeaddrinfo(aitop);
264559b1e613SZdenek Kotala for (n = 0; n < num_socks; n++)
264659b1e613SZdenek Kotala close(socks[n]);
26477c478bd9Sstevel@tonic-gate return -1;
26487c478bd9Sstevel@tonic-gate } else {
26497c478bd9Sstevel@tonic-gate debug("x11_create_display_inet: Socket family %d not supported",
26507c478bd9Sstevel@tonic-gate ai->ai_family);
26517c478bd9Sstevel@tonic-gate continue;
26527c478bd9Sstevel@tonic-gate }
26537c478bd9Sstevel@tonic-gate }
26547c478bd9Sstevel@tonic-gate #ifdef IPV6_V6ONLY
26557c478bd9Sstevel@tonic-gate if (ai->ai_family == AF_INET6) {
26567c478bd9Sstevel@tonic-gate int on = 1;
26577c478bd9Sstevel@tonic-gate if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
26587c478bd9Sstevel@tonic-gate error("setsockopt IPV6_V6ONLY: %.100s", strerror(errno));
26597c478bd9Sstevel@tonic-gate }
26607c478bd9Sstevel@tonic-gate #endif
26617c478bd9Sstevel@tonic-gate if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
266259b1e613SZdenek Kotala /*
266359b1e613SZdenek Kotala * If bind() fails with EADDRNOTAVAIL, we
266459b1e613SZdenek Kotala * should not break immediately but rather
266559b1e613SZdenek Kotala * try the next address available.
266659b1e613SZdenek Kotala */
266759b1e613SZdenek Kotala if (errno == EADDRNOTAVAIL) {
266859b1e613SZdenek Kotala close(sock);
266959b1e613SZdenek Kotala continue;
267059b1e613SZdenek Kotala }
267159b1e613SZdenek Kotala
26724c487f6fSjp161948 debug("bind port %d: %.100s; skipping this port", port,
26734c487f6fSjp161948 strerror(errno));
26747c478bd9Sstevel@tonic-gate close(sock);
26757c478bd9Sstevel@tonic-gate
26767c478bd9Sstevel@tonic-gate for (n = 0; n < num_socks; n++) {
26777c478bd9Sstevel@tonic-gate close(socks[n]);
26787c478bd9Sstevel@tonic-gate }
26797c478bd9Sstevel@tonic-gate num_socks = 0;
26807c478bd9Sstevel@tonic-gate break;
26817c478bd9Sstevel@tonic-gate }
26827c478bd9Sstevel@tonic-gate socks[num_socks++] = sock;
26837c478bd9Sstevel@tonic-gate #ifndef DONT_TRY_OTHER_AF
26847c478bd9Sstevel@tonic-gate if (num_socks == NUM_SOCKS)
26857c478bd9Sstevel@tonic-gate break;
26867c478bd9Sstevel@tonic-gate #else
26877c478bd9Sstevel@tonic-gate if (x11_use_localhost) {
26887c478bd9Sstevel@tonic-gate if (num_socks == NUM_SOCKS)
26897c478bd9Sstevel@tonic-gate break;
26907c478bd9Sstevel@tonic-gate } else {
26917c478bd9Sstevel@tonic-gate break;
26927c478bd9Sstevel@tonic-gate }
26937c478bd9Sstevel@tonic-gate #endif
26947c478bd9Sstevel@tonic-gate }
26957c478bd9Sstevel@tonic-gate freeaddrinfo(aitop);
26967c478bd9Sstevel@tonic-gate if (num_socks > 0)
26977c478bd9Sstevel@tonic-gate break;
26987c478bd9Sstevel@tonic-gate }
26997c478bd9Sstevel@tonic-gate if (display_number >= MAX_DISPLAYS) {
27007c478bd9Sstevel@tonic-gate error("Failed to allocate internet-domain X11 display socket.");
27017c478bd9Sstevel@tonic-gate return -1;
27027c478bd9Sstevel@tonic-gate }
27037c478bd9Sstevel@tonic-gate /* Start listening for connections on the socket. */
27047c478bd9Sstevel@tonic-gate for (n = 0; n < num_socks; n++) {
27057c478bd9Sstevel@tonic-gate sock = socks[n];
27067c478bd9Sstevel@tonic-gate if (listen(sock, 5) < 0) {
270759b1e613SZdenek Kotala int i;
27087c478bd9Sstevel@tonic-gate error("listen: %.100s", strerror(errno));
270959b1e613SZdenek Kotala for (i = 0; i < num_socks; i++)
271059b1e613SZdenek Kotala close(socks[i]);
27117c478bd9Sstevel@tonic-gate return -1;
27127c478bd9Sstevel@tonic-gate }
27137c478bd9Sstevel@tonic-gate }
27147c478bd9Sstevel@tonic-gate
27157c478bd9Sstevel@tonic-gate /* Allocate a channel for each socket. */
27167c478bd9Sstevel@tonic-gate for (n = 0; n < num_socks; n++) {
27177c478bd9Sstevel@tonic-gate sock = socks[n];
27187c478bd9Sstevel@tonic-gate nc = channel_new("x11 listener",
27197c478bd9Sstevel@tonic-gate SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
27207c478bd9Sstevel@tonic-gate CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
27217c478bd9Sstevel@tonic-gate 0, xstrdup("X11 inet listener"), 1);
27227c478bd9Sstevel@tonic-gate nc->single_connection = single_connection;
27237c478bd9Sstevel@tonic-gate }
27247c478bd9Sstevel@tonic-gate
27257c478bd9Sstevel@tonic-gate /* Return the display number for the DISPLAY environment variable. */
27267c478bd9Sstevel@tonic-gate *display_numberp = display_number;
27277c478bd9Sstevel@tonic-gate return (0);
27287c478bd9Sstevel@tonic-gate }
27297c478bd9Sstevel@tonic-gate
27307c478bd9Sstevel@tonic-gate static int
connect_local_xsocket(u_int dnr)27317c478bd9Sstevel@tonic-gate connect_local_xsocket(u_int dnr)
27327c478bd9Sstevel@tonic-gate {
27337c478bd9Sstevel@tonic-gate int sock;
27347c478bd9Sstevel@tonic-gate struct sockaddr_un addr;
27357c478bd9Sstevel@tonic-gate
27367c478bd9Sstevel@tonic-gate sock = socket(AF_UNIX, SOCK_STREAM, 0);
27377c478bd9Sstevel@tonic-gate if (sock < 0)
27387c478bd9Sstevel@tonic-gate error("socket: %.100s", strerror(errno));
27397c478bd9Sstevel@tonic-gate memset(&addr, 0, sizeof(addr));
27407c478bd9Sstevel@tonic-gate addr.sun_family = AF_UNIX;
27417c478bd9Sstevel@tonic-gate snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
27427c478bd9Sstevel@tonic-gate if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
27437c478bd9Sstevel@tonic-gate return sock;
27447c478bd9Sstevel@tonic-gate close(sock);
27457c478bd9Sstevel@tonic-gate error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
27467c478bd9Sstevel@tonic-gate return -1;
27477c478bd9Sstevel@tonic-gate }
27487c478bd9Sstevel@tonic-gate
27497c478bd9Sstevel@tonic-gate int
x11_connect_display(void)27507c478bd9Sstevel@tonic-gate x11_connect_display(void)
27517c478bd9Sstevel@tonic-gate {
27527c478bd9Sstevel@tonic-gate int display_number, sock = 0;
27537c478bd9Sstevel@tonic-gate const char *display;
27547c478bd9Sstevel@tonic-gate char buf[1024], *cp;
27557c478bd9Sstevel@tonic-gate struct addrinfo hints, *ai, *aitop;
27567c478bd9Sstevel@tonic-gate char strport[NI_MAXSERV];
27577c478bd9Sstevel@tonic-gate int gaierr;
27587c478bd9Sstevel@tonic-gate
27597c478bd9Sstevel@tonic-gate /* Try to open a socket for the local X server. */
27607c478bd9Sstevel@tonic-gate display = getenv("DISPLAY");
27617c478bd9Sstevel@tonic-gate if (!display) {
27627c478bd9Sstevel@tonic-gate error("DISPLAY not set.");
27637c478bd9Sstevel@tonic-gate return -1;
27647c478bd9Sstevel@tonic-gate }
27657c478bd9Sstevel@tonic-gate /*
27667c478bd9Sstevel@tonic-gate * Now we decode the value of the DISPLAY variable and make a
27677c478bd9Sstevel@tonic-gate * connection to the real X server.
27687c478bd9Sstevel@tonic-gate */
27697c478bd9Sstevel@tonic-gate
27707c478bd9Sstevel@tonic-gate /*
27717c478bd9Sstevel@tonic-gate * Check if it is a unix domain socket. Unix domain displays are in
27727c478bd9Sstevel@tonic-gate * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
27737c478bd9Sstevel@tonic-gate */
27747c478bd9Sstevel@tonic-gate if (strncmp(display, "unix:", 5) == 0 ||
27757c478bd9Sstevel@tonic-gate display[0] == ':') {
27767c478bd9Sstevel@tonic-gate /* Connect to the unix domain socket. */
27777c478bd9Sstevel@tonic-gate if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
27787c478bd9Sstevel@tonic-gate error("Could not parse display number from DISPLAY: %.100s",
27797c478bd9Sstevel@tonic-gate display);
27807c478bd9Sstevel@tonic-gate return -1;
27817c478bd9Sstevel@tonic-gate }
27827c478bd9Sstevel@tonic-gate /* Create a socket. */
27837c478bd9Sstevel@tonic-gate sock = connect_local_xsocket(display_number);
27847c478bd9Sstevel@tonic-gate if (sock < 0)
27857c478bd9Sstevel@tonic-gate return -1;
27867c478bd9Sstevel@tonic-gate
27877c478bd9Sstevel@tonic-gate /* OK, we now have a connection to the display. */
27887c478bd9Sstevel@tonic-gate return sock;
27897c478bd9Sstevel@tonic-gate }
27907c478bd9Sstevel@tonic-gate /*
27917c478bd9Sstevel@tonic-gate * Connect to an inet socket. The DISPLAY value is supposedly
27927c478bd9Sstevel@tonic-gate * hostname:d[.s], where hostname may also be numeric IP address.
27937c478bd9Sstevel@tonic-gate */
27947c478bd9Sstevel@tonic-gate strlcpy(buf, display, sizeof(buf));
27957c478bd9Sstevel@tonic-gate cp = strchr(buf, ':');
27967c478bd9Sstevel@tonic-gate if (!cp) {
27977c478bd9Sstevel@tonic-gate error("Could not find ':' in DISPLAY: %.100s", display);
27987c478bd9Sstevel@tonic-gate return -1;
27997c478bd9Sstevel@tonic-gate }
28007c478bd9Sstevel@tonic-gate *cp = 0;
28017c478bd9Sstevel@tonic-gate /* buf now contains the host name. But first we parse the display number. */
28027c478bd9Sstevel@tonic-gate if (sscanf(cp + 1, "%d", &display_number) != 1) {
28037c478bd9Sstevel@tonic-gate error("Could not parse display number from DISPLAY: %.100s",
28047c478bd9Sstevel@tonic-gate display);
28057c478bd9Sstevel@tonic-gate return -1;
28067c478bd9Sstevel@tonic-gate }
28077c478bd9Sstevel@tonic-gate
28087c478bd9Sstevel@tonic-gate /* Look up the host address */
28097c478bd9Sstevel@tonic-gate memset(&hints, 0, sizeof(hints));
28107c478bd9Sstevel@tonic-gate hints.ai_family = IPv4or6;
28117c478bd9Sstevel@tonic-gate hints.ai_socktype = SOCK_STREAM;
28127c478bd9Sstevel@tonic-gate snprintf(strport, sizeof strport, "%d", 6000 + display_number);
28137c478bd9Sstevel@tonic-gate if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
28147c478bd9Sstevel@tonic-gate error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
28157c478bd9Sstevel@tonic-gate return -1;
28167c478bd9Sstevel@tonic-gate }
28177c478bd9Sstevel@tonic-gate for (ai = aitop; ai; ai = ai->ai_next) {
28187c478bd9Sstevel@tonic-gate /* Create a socket. */
28197c478bd9Sstevel@tonic-gate sock = socket(ai->ai_family, SOCK_STREAM, 0);
28207c478bd9Sstevel@tonic-gate if (sock < 0) {
28217c478bd9Sstevel@tonic-gate debug("socket: %.100s", strerror(errno));
28227c478bd9Sstevel@tonic-gate continue;
28237c478bd9Sstevel@tonic-gate }
28247c478bd9Sstevel@tonic-gate /* Connect it to the display. */
28257c478bd9Sstevel@tonic-gate if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
28267c478bd9Sstevel@tonic-gate debug("connect %.100s port %d: %.100s", buf,
28277c478bd9Sstevel@tonic-gate 6000 + display_number, strerror(errno));
28287c478bd9Sstevel@tonic-gate close(sock);
28297c478bd9Sstevel@tonic-gate continue;
28307c478bd9Sstevel@tonic-gate }
28317c478bd9Sstevel@tonic-gate /* Success */
28327c478bd9Sstevel@tonic-gate break;
28337c478bd9Sstevel@tonic-gate }
28347c478bd9Sstevel@tonic-gate freeaddrinfo(aitop);
28357c478bd9Sstevel@tonic-gate if (!ai) {
28367c478bd9Sstevel@tonic-gate error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
28377c478bd9Sstevel@tonic-gate strerror(errno));
28387c478bd9Sstevel@tonic-gate return -1;
28397c478bd9Sstevel@tonic-gate }
28407c478bd9Sstevel@tonic-gate set_nodelay(sock);
28417c478bd9Sstevel@tonic-gate return sock;
28427c478bd9Sstevel@tonic-gate }
28437c478bd9Sstevel@tonic-gate
28447c478bd9Sstevel@tonic-gate /*
28457c478bd9Sstevel@tonic-gate * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
28467c478bd9Sstevel@tonic-gate * the remote channel number. We should do whatever we want, and respond
28477c478bd9Sstevel@tonic-gate * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
28487c478bd9Sstevel@tonic-gate */
28497c478bd9Sstevel@tonic-gate
28507c478bd9Sstevel@tonic-gate void
x11_input_open(int type,u_int32_t seq,void * ctxt)28517c478bd9Sstevel@tonic-gate x11_input_open(int type, u_int32_t seq, void *ctxt)
28527c478bd9Sstevel@tonic-gate {
28537c478bd9Sstevel@tonic-gate Channel *c = NULL;
28547c478bd9Sstevel@tonic-gate int remote_id, sock = 0;
28557c478bd9Sstevel@tonic-gate char *remote_host;
28567c478bd9Sstevel@tonic-gate
28577c478bd9Sstevel@tonic-gate debug("Received X11 open request.");
28587c478bd9Sstevel@tonic-gate
28597c478bd9Sstevel@tonic-gate remote_id = packet_get_int();
28607c478bd9Sstevel@tonic-gate
28617c478bd9Sstevel@tonic-gate if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
28627c478bd9Sstevel@tonic-gate remote_host = packet_get_string(NULL);
28637c478bd9Sstevel@tonic-gate } else {
28647c478bd9Sstevel@tonic-gate remote_host = xstrdup("unknown (remote did not supply name)");
28657c478bd9Sstevel@tonic-gate }
28667c478bd9Sstevel@tonic-gate packet_check_eom();
28677c478bd9Sstevel@tonic-gate
28687c478bd9Sstevel@tonic-gate /* Obtain a connection to the real X display. */
28697c478bd9Sstevel@tonic-gate sock = x11_connect_display();
28707c478bd9Sstevel@tonic-gate if (sock != -1) {
28717c478bd9Sstevel@tonic-gate /* Allocate a channel for this connection. */
28727c478bd9Sstevel@tonic-gate c = channel_new("connected x11 socket",
28737c478bd9Sstevel@tonic-gate SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
28747c478bd9Sstevel@tonic-gate remote_host, 1);
28757c478bd9Sstevel@tonic-gate c->remote_id = remote_id;
28767c478bd9Sstevel@tonic-gate c->force_drain = 1;
28777c478bd9Sstevel@tonic-gate }
28787c478bd9Sstevel@tonic-gate if (c == NULL) {
28797c478bd9Sstevel@tonic-gate /* Send refusal to the remote host. */
28807c478bd9Sstevel@tonic-gate packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
28817c478bd9Sstevel@tonic-gate packet_put_int(remote_id);
28827c478bd9Sstevel@tonic-gate } else {
28837c478bd9Sstevel@tonic-gate /* Send a confirmation to the remote host. */
28847c478bd9Sstevel@tonic-gate packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
28857c478bd9Sstevel@tonic-gate packet_put_int(remote_id);
28867c478bd9Sstevel@tonic-gate packet_put_int(c->self);
28877c478bd9Sstevel@tonic-gate }
28887c478bd9Sstevel@tonic-gate packet_send();
28897c478bd9Sstevel@tonic-gate }
28907c478bd9Sstevel@tonic-gate
28917c478bd9Sstevel@tonic-gate /* dummy protocol handler that denies SSH-1 requests (agent/x11) */
28927c478bd9Sstevel@tonic-gate void
deny_input_open(int type,u_int32_t seq,void * ctxt)28937c478bd9Sstevel@tonic-gate deny_input_open(int type, u_int32_t seq, void *ctxt)
28947c478bd9Sstevel@tonic-gate {
28957c478bd9Sstevel@tonic-gate int rchan = packet_get_int();
28967c478bd9Sstevel@tonic-gate
28977c478bd9Sstevel@tonic-gate switch (type) {
28987c478bd9Sstevel@tonic-gate case SSH_SMSG_AGENT_OPEN:
28997c478bd9Sstevel@tonic-gate error("Warning: ssh server tried agent forwarding.");
29007c478bd9Sstevel@tonic-gate break;
29017c478bd9Sstevel@tonic-gate case SSH_SMSG_X11_OPEN:
29027c478bd9Sstevel@tonic-gate error("Warning: ssh server tried X11 forwarding.");
29037c478bd9Sstevel@tonic-gate break;
29047c478bd9Sstevel@tonic-gate default:
29057c478bd9Sstevel@tonic-gate error("deny_input_open: type %d", type);
29067c478bd9Sstevel@tonic-gate break;
29077c478bd9Sstevel@tonic-gate }
29087c478bd9Sstevel@tonic-gate error("Warning: this is probably a break in attempt by a malicious server.");
29097c478bd9Sstevel@tonic-gate packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
29107c478bd9Sstevel@tonic-gate packet_put_int(rchan);
29117c478bd9Sstevel@tonic-gate packet_send();
29127c478bd9Sstevel@tonic-gate }
29137c478bd9Sstevel@tonic-gate
29147c478bd9Sstevel@tonic-gate /*
29157c478bd9Sstevel@tonic-gate * Requests forwarding of X11 connections, generates fake authentication
29167c478bd9Sstevel@tonic-gate * data, and enables authentication spoofing.
29177c478bd9Sstevel@tonic-gate * This should be called in the client only.
29187c478bd9Sstevel@tonic-gate */
29197c478bd9Sstevel@tonic-gate void
x11_request_forwarding_with_spoofing(int client_session_id,const char * disp,const char * proto,const char * data)2920383a1232Sjp161948 x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
29217c478bd9Sstevel@tonic-gate const char *proto, const char *data)
29227c478bd9Sstevel@tonic-gate {
29237c478bd9Sstevel@tonic-gate u_int data_len = (u_int) strlen(data) / 2;
2924383a1232Sjp161948 u_int i, value;
29257c478bd9Sstevel@tonic-gate char *new_data;
29267c478bd9Sstevel@tonic-gate int screen_number;
29277c478bd9Sstevel@tonic-gate const char *cp;
29287c478bd9Sstevel@tonic-gate u_int32_t rand = 0;
29297c478bd9Sstevel@tonic-gate
2930383a1232Sjp161948 cp = disp;
2931383a1232Sjp161948 if (disp)
2932383a1232Sjp161948 cp = strchr(disp, ':');
29337c478bd9Sstevel@tonic-gate if (cp)
29347c478bd9Sstevel@tonic-gate cp = strchr(cp, '.');
29357c478bd9Sstevel@tonic-gate if (cp)
29367c478bd9Sstevel@tonic-gate screen_number = atoi(cp + 1);
29377c478bd9Sstevel@tonic-gate else
29387c478bd9Sstevel@tonic-gate screen_number = 0;
29397c478bd9Sstevel@tonic-gate
29407c478bd9Sstevel@tonic-gate /* Save protocol name. */
29417c478bd9Sstevel@tonic-gate x11_saved_proto = xstrdup(proto);
29427c478bd9Sstevel@tonic-gate
29437c478bd9Sstevel@tonic-gate /*
29447c478bd9Sstevel@tonic-gate * Extract real authentication data and generate fake data of the
29457c478bd9Sstevel@tonic-gate * same length.
29467c478bd9Sstevel@tonic-gate */
29477c478bd9Sstevel@tonic-gate x11_saved_data = xmalloc(data_len);
29487c478bd9Sstevel@tonic-gate x11_fake_data = xmalloc(data_len);
29497c478bd9Sstevel@tonic-gate for (i = 0; i < data_len; i++) {
29507c478bd9Sstevel@tonic-gate if (sscanf(data + 2 * i, "%2x", &value) != 1)
29517c478bd9Sstevel@tonic-gate fatal("x11_request_forwarding: bad authentication data: %.100s", data);
29527c478bd9Sstevel@tonic-gate if (i % 4 == 0)
29537c478bd9Sstevel@tonic-gate rand = arc4random();
29547c478bd9Sstevel@tonic-gate x11_saved_data[i] = value;
29557c478bd9Sstevel@tonic-gate x11_fake_data[i] = rand & 0xff;
29567c478bd9Sstevel@tonic-gate rand >>= 8;
29577c478bd9Sstevel@tonic-gate }
29587c478bd9Sstevel@tonic-gate x11_saved_data_len = data_len;
29597c478bd9Sstevel@tonic-gate x11_fake_data_len = data_len;
29607c478bd9Sstevel@tonic-gate
29617c478bd9Sstevel@tonic-gate /* Convert the fake data into hex. */
2962383a1232Sjp161948 new_data = tohex(x11_fake_data, data_len);
29637c478bd9Sstevel@tonic-gate
29647c478bd9Sstevel@tonic-gate /* Send the request packet. */
29657c478bd9Sstevel@tonic-gate if (compat20) {
29667c478bd9Sstevel@tonic-gate channel_request_start(client_session_id, "x11-req", 0);
29677c478bd9Sstevel@tonic-gate packet_put_char(0); /* XXX bool single connection */
29687c478bd9Sstevel@tonic-gate } else {
29697c478bd9Sstevel@tonic-gate packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
29707c478bd9Sstevel@tonic-gate }
29717c478bd9Sstevel@tonic-gate packet_put_cstring(proto);
29727c478bd9Sstevel@tonic-gate packet_put_cstring(new_data);
29737c478bd9Sstevel@tonic-gate packet_put_int(screen_number);
29747c478bd9Sstevel@tonic-gate packet_send();
29757c478bd9Sstevel@tonic-gate packet_write_wait();
29767c478bd9Sstevel@tonic-gate xfree(new_data);
29777c478bd9Sstevel@tonic-gate }
29787c478bd9Sstevel@tonic-gate
29797c478bd9Sstevel@tonic-gate
29807c478bd9Sstevel@tonic-gate /* -- agent forwarding */
29817c478bd9Sstevel@tonic-gate
29827c478bd9Sstevel@tonic-gate /* Sends a message to the server to request authentication fd forwarding. */
29837c478bd9Sstevel@tonic-gate
29847c478bd9Sstevel@tonic-gate void
auth_request_forwarding(void)29857c478bd9Sstevel@tonic-gate auth_request_forwarding(void)
29867c478bd9Sstevel@tonic-gate {
29877c478bd9Sstevel@tonic-gate packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
29887c478bd9Sstevel@tonic-gate packet_send();
29897c478bd9Sstevel@tonic-gate packet_write_wait();
29907c478bd9Sstevel@tonic-gate }
29917c478bd9Sstevel@tonic-gate
29927c478bd9Sstevel@tonic-gate /* This is called to process an SSH_SMSG_AGENT_OPEN message. */
29937c478bd9Sstevel@tonic-gate
29947c478bd9Sstevel@tonic-gate void
auth_input_open_request(int type,u_int32_t seq,void * ctxt)29957c478bd9Sstevel@tonic-gate auth_input_open_request(int type, u_int32_t seq, void *ctxt)
29967c478bd9Sstevel@tonic-gate {
29977c478bd9Sstevel@tonic-gate Channel *c = NULL;
29987c478bd9Sstevel@tonic-gate int remote_id, sock;
29997c478bd9Sstevel@tonic-gate char *name;
30007c478bd9Sstevel@tonic-gate
30017c478bd9Sstevel@tonic-gate /* Read the remote channel number from the message. */
30027c478bd9Sstevel@tonic-gate remote_id = packet_get_int();
30037c478bd9Sstevel@tonic-gate packet_check_eom();
30047c478bd9Sstevel@tonic-gate
30057c478bd9Sstevel@tonic-gate /*
30067c478bd9Sstevel@tonic-gate * Get a connection to the local authentication agent (this may again
30077c478bd9Sstevel@tonic-gate * get forwarded).
30087c478bd9Sstevel@tonic-gate */
30097c478bd9Sstevel@tonic-gate sock = ssh_get_authentication_socket();
30107c478bd9Sstevel@tonic-gate
30117c478bd9Sstevel@tonic-gate /*
30127c478bd9Sstevel@tonic-gate * If we could not connect the agent, send an error message back to
30137c478bd9Sstevel@tonic-gate * the server. This should never happen unless the agent dies,
30147c478bd9Sstevel@tonic-gate * because authentication forwarding is only enabled if we have an
30157c478bd9Sstevel@tonic-gate * agent.
30167c478bd9Sstevel@tonic-gate */
30177c478bd9Sstevel@tonic-gate if (sock >= 0) {
30187c478bd9Sstevel@tonic-gate name = xstrdup("authentication agent connection");
30197c478bd9Sstevel@tonic-gate c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
30207c478bd9Sstevel@tonic-gate -1, 0, 0, 0, name, 1);
30217c478bd9Sstevel@tonic-gate c->remote_id = remote_id;
30227c478bd9Sstevel@tonic-gate c->force_drain = 1;
30237c478bd9Sstevel@tonic-gate }
30247c478bd9Sstevel@tonic-gate if (c == NULL) {
30257c478bd9Sstevel@tonic-gate packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
30267c478bd9Sstevel@tonic-gate packet_put_int(remote_id);
30277c478bd9Sstevel@tonic-gate } else {
30287c478bd9Sstevel@tonic-gate /* Send a confirmation to the remote host. */
30297c478bd9Sstevel@tonic-gate debug("Forwarding authentication connection.");
30307c478bd9Sstevel@tonic-gate packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
30317c478bd9Sstevel@tonic-gate packet_put_int(remote_id);
30327c478bd9Sstevel@tonic-gate packet_put_int(c->self);
30337c478bd9Sstevel@tonic-gate }
30347c478bd9Sstevel@tonic-gate packet_send();
30357c478bd9Sstevel@tonic-gate }
3036