blob: 895c43f6a0e37d25c70ca5140ce3e931ffc1a2dd [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: channels.c,v 1.260 2006/07/22 20:48:22 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains functions for generic socket connection forwarding.
7 * There is also code for initiating connection forwarding for X11 connections,
8 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Millera90fc082002-01-22 23:19:38 +110017 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 * Copyright (c) 1999 Dug Song. All rights reserved.
19 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110043
44#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110045#include <sys/types.h>
46#include <sys/un.h>
Damien Millerefc04e72006-07-10 20:26:27 +100047#include <sys/socket.h>
48
49#include <netinet/in.h>
50#include <arpa/inet.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110051
Darren Tucker39972492006-07-12 22:22:46 +100052#include <errno.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100053#include <netdb.h>
Damien Millere3476ed2006-07-24 14:13:33 +100054#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110055#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100056#include <unistd.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057
58#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000059#include "ssh1.h"
60#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061#include "packet.h"
62#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "log.h"
64#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000067#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100068#include "key.h"
69#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110070#include "pathnames.h"
Darren Tucker46471c92003-07-03 13:55:19 +100071#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072
Ben Lindstrome9c99912001-06-09 00:41:05 +000073/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074
Damien Miller5428f641999-11-25 11:54:57 +110075/*
76 * Pointer to an array containing all allocated channels. The array is
77 * dynamically extended as needed.
78 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000079static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
Damien Miller5428f641999-11-25 11:54:57 +110081/*
82 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000083 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110084 */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +100085static u_int channels_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Damien Miller5428f641999-11-25 11:54:57 +110087/*
88 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +000089 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +110090 */
Damien Miller5e953212001-01-30 09:14:00 +110091static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Ben Lindstrome9c99912001-06-09 00:41:05 +000094/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
Damien Miller5428f641999-11-25 11:54:57 +110096/*
97 * Data structure for storing which hosts are permitted for forward requests.
98 * The local sides of any remote forwards are stored in this array to prevent
99 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
100 * network (which might be behind a firewall).
101 */
Damien Miller95def091999-11-25 00:26:21 +1100102typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000103 char *host_to_connect; /* Connect to 'host'. */
104 u_short port_to_connect; /* Connect to 'port'. */
105 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106} ForwardPermission;
107
Damien Miller9b439df2006-07-24 14:04:00 +1000108/* List of all permitted host/port pairs to connect by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000110
Damien Miller9b439df2006-07-24 14:04:00 +1000111/* List of all permitted host/port pairs to connect by the admin. */
112static ForwardPermission permitted_adm_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
113
114/* Number of permitted host/port pairs in the array permitted by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115static int num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +1000116
117/* Number of permitted host/port pair in the array permitted by the admin. */
118static int num_adm_permitted_opens = 0;
119
Damien Miller5428f641999-11-25 11:54:57 +1100120/*
121 * If this is true, all opens are permitted. This is the case on the server
122 * on which we have to trust the client anyway, and the user could do
123 * anything after logging in anyway.
124 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125static int all_opens_permitted = 0;
126
Ben Lindstrome9c99912001-06-09 00:41:05 +0000127
128/* -- X11 forwarding */
129
130/* Maximum number of fake X11 displays to try. */
131#define MAX_DISPLAYS 1000
132
Damien Miller13390022005-07-06 09:44:19 +1000133/* Saved X11 local (client) display. */
134static char *x11_saved_display = NULL;
135
Ben Lindstrome9c99912001-06-09 00:41:05 +0000136/* Saved X11 authentication protocol name. */
137static char *x11_saved_proto = NULL;
138
139/* Saved X11 authentication data. This is the real data. */
140static char *x11_saved_data = NULL;
141static u_int x11_saved_data_len = 0;
142
143/*
144 * Fake X11 authentication data. This is what the server will be sending us;
145 * we should replace any occurrences of this by the real data.
146 */
Damien Miller4ae97f12006-03-26 14:08:10 +1100147static u_char *x11_fake_data = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000148static u_int x11_fake_data_len;
149
150
151/* -- agent forwarding */
152
153#define NUM_SOCKS 10
154
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000155/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000156static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000157
Ben Lindstrome9c99912001-06-09 00:41:05 +0000158/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000159static void port_open_helper(Channel *c, char *rtype);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000160
Ben Lindstrome9c99912001-06-09 00:41:05 +0000161/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000162
163Channel *
Damien Millerd47c62a2005-12-13 19:33:57 +1100164channel_by_id(int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000165{
166 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000167
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000168 if (id < 0 || (u_int)id >= channels_alloc) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100169 logit("channel_by_id: %d: bad id", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000170 return NULL;
171 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000172 c = channels[id];
173 if (c == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100174 logit("channel_by_id: %d: bad id: channel free", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000175 return NULL;
176 }
177 return c;
178}
179
Damien Miller5428f641999-11-25 11:54:57 +1100180/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100181 * Returns the channel if it is allowed to receive protocol messages.
182 * Private channels, like listening sockets, may not receive messages.
183 */
184Channel *
185channel_lookup(int id)
186{
187 Channel *c;
188
189 if ((c = channel_by_id(id)) == NULL)
190 return (NULL);
191
Damien Millerd62f2ca2006-03-26 13:57:41 +1100192 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100193 case SSH_CHANNEL_X11_OPEN:
194 case SSH_CHANNEL_LARVAL:
195 case SSH_CHANNEL_CONNECTING:
196 case SSH_CHANNEL_DYNAMIC:
197 case SSH_CHANNEL_OPENING:
198 case SSH_CHANNEL_OPEN:
199 case SSH_CHANNEL_INPUT_DRAINING:
200 case SSH_CHANNEL_OUTPUT_DRAINING:
201 return (c);
Damien Millerd47c62a2005-12-13 19:33:57 +1100202 }
203 logit("Non-public channel %d, type %d.", id, c->type);
204 return (NULL);
205}
206
207/*
Damien Millere247cc42000-05-07 12:03:14 +1000208 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000209 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100210 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000211static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100212channel_register_fds(Channel *c, int rfd, int wfd, int efd,
213 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214{
Damien Miller95def091999-11-25 00:26:21 +1100215 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100216 channel_max_fd = MAX(channel_max_fd, rfd);
217 channel_max_fd = MAX(channel_max_fd, wfd);
218 channel_max_fd = MAX(channel_max_fd, efd);
219
Damien Miller5428f641999-11-25 11:54:57 +1100220 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000221
Damien Miller6f83b8e2000-05-02 09:23:45 +1000222 c->rfd = rfd;
223 c->wfd = wfd;
224 c->sock = (rfd == wfd) ? rfd : -1;
Damien Miller0e220db2004-06-15 10:34:08 +1000225 c->ctl_fd = -1; /* XXX: set elsewhere */
Damien Miller6f83b8e2000-05-02 09:23:45 +1000226 c->efd = efd;
227 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100228
Damien Miller79438cc2001-02-16 12:34:57 +1100229 /* XXX ugly hack: nonblock is only set by the server */
230 if (nonblock && isatty(c->rfd)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000231 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100232 c->isatty = 1;
233 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000234 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100235 c->self, c->wfd);
236 }
237 } else {
238 c->isatty = 0;
239 }
Ben Lindstrombeb5f332002-07-22 15:28:53 +0000240 c->wfd_isatty = isatty(c->wfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100241
Damien Miller69b69aa2000-10-28 14:19:58 +1100242 /* enable nonblocking mode */
243 if (nonblock) {
244 if (rfd != -1)
245 set_nonblock(rfd);
246 if (wfd != -1)
247 set_nonblock(wfd);
248 if (efd != -1)
249 set_nonblock(efd);
250 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000251}
252
253/*
254 * Allocate a new channel object and set its type and socket. This will cause
255 * remote_name to be freed.
256 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000257Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000258channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000259 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000260{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000261 int found;
262 u_int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000263 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264
Damien Miller95def091999-11-25 00:26:21 +1100265 /* Do initial allocation if this is the first call. */
266 if (channels_alloc == 0) {
267 channels_alloc = 10;
Damien Miller07d86be2006-03-26 14:19:21 +1100268 channels = xcalloc(channels_alloc, sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100269 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000270 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100271 }
272 /* Try to find a free slot where to put the new channel. */
273 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000274 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100275 /* Found a free slot. */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000276 found = (int)i;
Damien Miller95def091999-11-25 00:26:21 +1100277 break;
278 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000279 if (found < 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100280 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100281 found = channels_alloc;
Damien Miller9403aa22002-06-26 19:14:43 +1000282 if (channels_alloc > 10000)
283 fatal("channel_new: internal error: channels_alloc %d "
284 "too big.", channels_alloc);
Damien Miller36812092006-03-26 14:22:47 +1100285 channels = xrealloc(channels, channels_alloc + 10,
286 sizeof(Channel *));
Damien Miller5efcecc2003-09-17 07:31:14 +1000287 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100288 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100289 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000290 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100291 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000292 /* Initialize and return new channel. */
Damien Miller07d86be2006-03-26 14:19:21 +1100293 c = channels[found] = xcalloc(1, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100294 buffer_init(&c->input);
295 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000296 buffer_init(&c->extended);
Damien Miller5144df92002-01-22 23:28:45 +1100297 c->ostate = CHAN_OUTPUT_OPEN;
298 c->istate = CHAN_INPUT_OPEN;
299 c->flags = 0;
Damien Miller69b69aa2000-10-28 14:19:58 +1100300 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100301 c->self = found;
302 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000303 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000304 c->local_window = window;
305 c->local_window_max = window;
306 c->local_consumed = 0;
307 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100308 c->remote_id = -1;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000309 c->remote_name = xstrdup(remote_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000310 c->remote_window = 0;
311 c->remote_maxpacket = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000312 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100313 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000314 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100315 c->detach_close = 0;
Damien Miller67f0bc02002-02-05 12:23:08 +1100316 c->confirm = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +1000317 c->confirm_ctx = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000318 c->input_filter = NULL;
Damien Miller077b2382005-12-31 16:22:32 +1100319 c->output_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100320 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000321 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000323
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000324static int
325channel_find_maxfd(void)
326{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000327 u_int i;
328 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000329 Channel *c;
330
331 for (i = 0; i < channels_alloc; i++) {
332 c = channels[i];
333 if (c != NULL) {
334 max = MAX(max, c->rfd);
335 max = MAX(max, c->wfd);
336 max = MAX(max, c->efd);
337 }
338 }
339 return max;
340}
341
342int
343channel_close_fd(int *fdp)
344{
345 int ret = 0, fd = *fdp;
346
347 if (fd != -1) {
348 ret = close(fd);
349 *fdp = -1;
350 if (fd == channel_max_fd)
351 channel_max_fd = channel_find_maxfd();
352 }
353 return ret;
354}
355
Damien Miller6f83b8e2000-05-02 09:23:45 +1000356/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000357static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000358channel_close_fds(Channel *c)
359{
Damien Miller0e220db2004-06-15 10:34:08 +1000360 debug3("channel %d: close_fds r %d w %d e %d c %d",
361 c->self, c->rfd, c->wfd, c->efd, c->ctl_fd);
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000362
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000363 channel_close_fd(&c->sock);
Damien Miller0e220db2004-06-15 10:34:08 +1000364 channel_close_fd(&c->ctl_fd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000365 channel_close_fd(&c->rfd);
366 channel_close_fd(&c->wfd);
367 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000368}
369
370/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000371void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000372channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000373{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000374 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000375 u_int i, n;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000376
377 for (n = 0, i = 0; i < channels_alloc; i++)
378 if (channels[i])
379 n++;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000380 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000381 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000382
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000383 s = channel_open_message();
Damien Millerfbdeece2003-09-02 22:52:31 +1000384 debug3("channel %d: status: %s", c->self, s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000385 xfree(s);
386
Damien Miller6f83b8e2000-05-02 09:23:45 +1000387 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000388 shutdown(c->sock, SHUT_RDWR);
Damien Miller0e220db2004-06-15 10:34:08 +1000389 if (c->ctl_fd != -1)
390 shutdown(c->ctl_fd, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000391 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000392 buffer_free(&c->input);
393 buffer_free(&c->output);
394 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000395 if (c->remote_name) {
396 xfree(c->remote_name);
397 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100398 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000399 channels[c->self] = NULL;
400 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000401}
402
Ben Lindstrome9c99912001-06-09 00:41:05 +0000403void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000404channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000405{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000406 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000407
Ben Lindstrom601e4362001-06-21 03:19:23 +0000408 for (i = 0; i < channels_alloc; i++)
409 if (channels[i] != NULL)
410 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000411}
412
413/*
414 * Closes the sockets/fds of all channels. This is used to close extra file
415 * descriptors after a fork.
416 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000417void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000418channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000419{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000420 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000421
422 for (i = 0; i < channels_alloc; i++)
423 if (channels[i] != NULL)
424 channel_close_fds(channels[i]);
425}
426
427/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000428 * Stop listening to channels.
429 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000430void
431channel_stop_listening(void)
432{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000433 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000434 Channel *c;
435
436 for (i = 0; i < channels_alloc; i++) {
437 c = channels[i];
438 if (c != NULL) {
439 switch (c->type) {
440 case SSH_CHANNEL_AUTH_SOCKET:
441 case SSH_CHANNEL_PORT_LISTENER:
442 case SSH_CHANNEL_RPORT_LISTENER:
443 case SSH_CHANNEL_X11_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000444 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000445 channel_free(c);
446 break;
447 }
448 }
449 }
450}
451
452/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000453 * Returns true if no channel has too much buffered data, and false if one or
454 * more channel is overfull.
455 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000456int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000457channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000458{
459 u_int i;
460 Channel *c;
461
462 for (i = 0; i < channels_alloc; i++) {
463 c = channels[i];
464 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000465#if 0
466 if (!compat20 &&
467 buffer_len(&c->input) > packet_get_maxsize()) {
Damien Miller275295e2003-01-08 14:04:09 +1100468 debug2("channel %d: big input buffer %d",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000469 c->self, buffer_len(&c->input));
470 return 0;
471 }
Damien Milleraf5f2e62001-10-10 15:01:16 +1000472#endif
Ben Lindstrome9c99912001-06-09 00:41:05 +0000473 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000474 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000475 c->self, buffer_len(&c->output),
476 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000477 return 0;
478 }
479 }
480 }
481 return 1;
482}
483
484/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000485int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000486channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000487{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000488 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000489 Channel *c;
490
491 for (i = 0; i < channels_alloc; i++) {
492 c = channels[i];
493 if (c == NULL)
494 continue;
495 switch (c->type) {
496 case SSH_CHANNEL_X11_LISTENER:
497 case SSH_CHANNEL_PORT_LISTENER:
498 case SSH_CHANNEL_RPORT_LISTENER:
499 case SSH_CHANNEL_CLOSED:
500 case SSH_CHANNEL_AUTH_SOCKET:
501 case SSH_CHANNEL_DYNAMIC:
502 case SSH_CHANNEL_CONNECTING:
503 case SSH_CHANNEL_ZOMBIE:
504 continue;
505 case SSH_CHANNEL_LARVAL:
506 if (!compat20)
507 fatal("cannot happen: SSH_CHANNEL_LARVAL");
508 continue;
509 case SSH_CHANNEL_OPENING:
510 case SSH_CHANNEL_OPEN:
511 case SSH_CHANNEL_X11_OPEN:
512 return 1;
513 case SSH_CHANNEL_INPUT_DRAINING:
514 case SSH_CHANNEL_OUTPUT_DRAINING:
515 if (!compat13)
516 fatal("cannot happen: OUT_DRAIN");
517 return 1;
518 default:
519 fatal("channel_still_open: bad channel type %d", c->type);
520 /* NOTREACHED */
521 }
522 }
523 return 0;
524}
525
526/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000527int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000528channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000529{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000530 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000531 Channel *c;
532
533 for (i = 0; i < channels_alloc; i++) {
534 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000535 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000536 continue;
537 switch (c->type) {
538 case SSH_CHANNEL_CLOSED:
539 case SSH_CHANNEL_DYNAMIC:
540 case SSH_CHANNEL_X11_LISTENER:
541 case SSH_CHANNEL_PORT_LISTENER:
542 case SSH_CHANNEL_RPORT_LISTENER:
543 case SSH_CHANNEL_OPENING:
544 case SSH_CHANNEL_CONNECTING:
545 case SSH_CHANNEL_ZOMBIE:
546 continue;
547 case SSH_CHANNEL_LARVAL:
548 case SSH_CHANNEL_AUTH_SOCKET:
549 case SSH_CHANNEL_OPEN:
550 case SSH_CHANNEL_X11_OPEN:
551 return i;
552 case SSH_CHANNEL_INPUT_DRAINING:
553 case SSH_CHANNEL_OUTPUT_DRAINING:
554 if (!compat13)
555 fatal("cannot happen: OUT_DRAIN");
556 return i;
557 default:
558 fatal("channel_find_open: bad channel type %d", c->type);
559 /* NOTREACHED */
560 }
561 }
562 return -1;
563}
564
565
566/*
567 * Returns a message describing the currently open forwarded connections,
568 * suitable for sending to the client. The message contains crlf pairs for
569 * newlines.
570 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000571char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000572channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000573{
574 Buffer buffer;
575 Channel *c;
576 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000577 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000578
579 buffer_init(&buffer);
580 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
581 buffer_append(&buffer, buf, strlen(buf));
582 for (i = 0; i < channels_alloc; i++) {
583 c = channels[i];
584 if (c == NULL)
585 continue;
586 switch (c->type) {
587 case SSH_CHANNEL_X11_LISTENER:
588 case SSH_CHANNEL_PORT_LISTENER:
589 case SSH_CHANNEL_RPORT_LISTENER:
590 case SSH_CHANNEL_CLOSED:
591 case SSH_CHANNEL_AUTH_SOCKET:
592 case SSH_CHANNEL_ZOMBIE:
593 continue;
594 case SSH_CHANNEL_LARVAL:
595 case SSH_CHANNEL_OPENING:
596 case SSH_CHANNEL_CONNECTING:
597 case SSH_CHANNEL_DYNAMIC:
598 case SSH_CHANNEL_OPEN:
599 case SSH_CHANNEL_X11_OPEN:
600 case SSH_CHANNEL_INPUT_DRAINING:
601 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Miller0e220db2004-06-15 10:34:08 +1000602 snprintf(buf, sizeof buf,
603 " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d cfd %d)\r\n",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000604 c->self, c->remote_name,
605 c->type, c->remote_id,
606 c->istate, buffer_len(&c->input),
607 c->ostate, buffer_len(&c->output),
Damien Miller0e220db2004-06-15 10:34:08 +1000608 c->rfd, c->wfd, c->ctl_fd);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000609 buffer_append(&buffer, buf, strlen(buf));
610 continue;
611 default:
612 fatal("channel_open_message: bad channel type %d", c->type);
613 /* NOTREACHED */
614 }
615 }
616 buffer_append(&buffer, "\0", 1);
617 cp = xstrdup(buffer_ptr(&buffer));
618 buffer_free(&buffer);
619 return cp;
620}
621
622void
623channel_send_open(int id)
624{
625 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000626
Ben Lindstrome9c99912001-06-09 00:41:05 +0000627 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000628 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000629 return;
630 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000631 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000632 packet_start(SSH2_MSG_CHANNEL_OPEN);
633 packet_put_cstring(c->ctype);
634 packet_put_int(c->self);
635 packet_put_int(c->local_window);
636 packet_put_int(c->local_maxpacket);
637 packet_send();
638}
639
640void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000641channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000642{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000643 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000644
Ben Lindstrome9c99912001-06-09 00:41:05 +0000645 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000646 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000647 return;
648 }
Damien Miller0e220db2004-06-15 10:34:08 +1000649 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000650 packet_start(SSH2_MSG_CHANNEL_REQUEST);
651 packet_put_int(c->remote_id);
652 packet_put_cstring(service);
653 packet_put_char(wantconfirm);
654}
Damien Miller4f7becb2006-03-26 14:10:14 +1100655
Ben Lindstrome9c99912001-06-09 00:41:05 +0000656void
Damien Miller0e220db2004-06-15 10:34:08 +1000657channel_register_confirm(int id, channel_callback_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000658{
659 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000660
Ben Lindstrome9c99912001-06-09 00:41:05 +0000661 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000662 logit("channel_register_comfirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000663 return;
664 }
Damien Miller67f0bc02002-02-05 12:23:08 +1100665 c->confirm = fn;
Damien Miller0e220db2004-06-15 10:34:08 +1000666 c->confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000667}
Damien Miller4f7becb2006-03-26 14:10:14 +1100668
Ben Lindstrome9c99912001-06-09 00:41:05 +0000669void
Damien Miller39eda6e2005-11-05 14:52:50 +1100670channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000671{
Damien Millerd47c62a2005-12-13 19:33:57 +1100672 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000673
Ben Lindstrome9c99912001-06-09 00:41:05 +0000674 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000675 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000676 return;
677 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000678 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100679 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000680}
Damien Miller4f7becb2006-03-26 14:10:14 +1100681
Ben Lindstrome9c99912001-06-09 00:41:05 +0000682void
683channel_cancel_cleanup(int id)
684{
Damien Millerd47c62a2005-12-13 19:33:57 +1100685 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000686
Ben Lindstrome9c99912001-06-09 00:41:05 +0000687 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000688 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000689 return;
690 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000691 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100692 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000693}
Damien Miller4f7becb2006-03-26 14:10:14 +1100694
Ben Lindstrome9c99912001-06-09 00:41:05 +0000695void
Damien Miller077b2382005-12-31 16:22:32 +1100696channel_register_filter(int id, channel_infilter_fn *ifn,
697 channel_outfilter_fn *ofn)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000698{
699 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000700
Ben Lindstrome9c99912001-06-09 00:41:05 +0000701 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000702 logit("channel_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000703 return;
704 }
Damien Miller077b2382005-12-31 16:22:32 +1100705 c->input_filter = ifn;
706 c->output_filter = ofn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000707}
708
709void
710channel_set_fds(int id, int rfd, int wfd, int efd,
Damien Miller2aa0c192002-02-19 15:20:08 +1100711 int extusage, int nonblock, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000712{
713 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000714
Ben Lindstrome9c99912001-06-09 00:41:05 +0000715 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
716 fatal("channel_activate for non-larval channel %d.", id);
717 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
718 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100719 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000720 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
721 packet_put_int(c->remote_id);
722 packet_put_int(c->local_window);
723 packet_send();
724}
725
Damien Miller5428f641999-11-25 11:54:57 +1100726/*
Damien Millerb38eff82000-04-01 11:09:21 +1000727 * 'channel_pre*' are called just before select() to add any bits relevant to
728 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100729 */
Damien Millerb38eff82000-04-01 11:09:21 +1000730/*
731 * 'channel_post*': perform any appropriate operations for channels which
732 * have events pending.
733 */
Damien Millerd62f2ca2006-03-26 13:57:41 +1100734typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000735chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
736chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
737
Damien Miller8473dd82006-07-24 14:08:32 +1000738/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000739static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100740channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000741{
742 FD_SET(c->sock, readset);
743}
744
Damien Miller8473dd82006-07-24 14:08:32 +1000745/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000746static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100747channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000748{
749 debug3("channel %d: waiting for connection", c->self);
750 FD_SET(c->sock, writeset);
751}
752
Ben Lindstrombba81212001-06-25 05:01:22 +0000753static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100754channel_pre_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000755{
756 if (buffer_len(&c->input) < packet_get_maxsize())
757 FD_SET(c->sock, readset);
758 if (buffer_len(&c->output) > 0)
759 FD_SET(c->sock, writeset);
760}
761
Ben Lindstrombba81212001-06-25 05:01:22 +0000762static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100763channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000764{
Damien Millerde6987c2002-01-22 23:20:40 +1100765 u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
Damien Millerb38eff82000-04-01 11:09:21 +1000766
Damien Miller33b13562000-04-04 14:38:59 +1000767 if (c->istate == CHAN_INPUT_OPEN &&
Damien Millerde6987c2002-01-22 23:20:40 +1100768 limit > 0 &&
Damien Miller499a0d52006-04-23 12:06:03 +1000769 buffer_len(&c->input) < limit &&
770 buffer_check_alloc(&c->input, CHAN_RBUF))
Damien Miller33b13562000-04-04 14:38:59 +1000771 FD_SET(c->rfd, readset);
772 if (c->ostate == CHAN_OUTPUT_OPEN ||
773 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
774 if (buffer_len(&c->output) > 0) {
775 FD_SET(c->wfd, writeset);
776 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000777 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +1000778 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
779 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000780 else
781 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000782 }
783 }
784 /** XXX check close conditions, too */
Damien Millerde6987c2002-01-22 23:20:40 +1100785 if (compat20 && c->efd != -1) {
Damien Miller33b13562000-04-04 14:38:59 +1000786 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
787 buffer_len(&c->extended) > 0)
788 FD_SET(c->efd, writeset);
Ben Lindstromcf159442002-03-26 03:26:24 +0000789 else if (!(c->flags & CHAN_EOF_SENT) &&
790 c->extended_usage == CHAN_EXTENDED_READ &&
Damien Miller33b13562000-04-04 14:38:59 +1000791 buffer_len(&c->extended) < c->remote_window)
792 FD_SET(c->efd, readset);
793 }
Damien Miller0e220db2004-06-15 10:34:08 +1000794 /* XXX: What about efd? races? */
Darren Tuckerfc959702004-07-17 16:12:08 +1000795 if (compat20 && c->ctl_fd != -1 &&
Damien Miller0e220db2004-06-15 10:34:08 +1000796 c->istate == CHAN_INPUT_OPEN && c->ostate == CHAN_OUTPUT_OPEN)
797 FD_SET(c->ctl_fd, readset);
Damien Miller33b13562000-04-04 14:38:59 +1000798}
799
Damien Miller8473dd82006-07-24 14:08:32 +1000800/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000801static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100802channel_pre_input_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000803{
804 if (buffer_len(&c->input) == 0) {
805 packet_start(SSH_MSG_CHANNEL_CLOSE);
806 packet_put_int(c->remote_id);
807 packet_send();
808 c->type = SSH_CHANNEL_CLOSED;
Damien Millerfbdeece2003-09-02 22:52:31 +1000809 debug2("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000810 }
811}
812
Damien Miller8473dd82006-07-24 14:08:32 +1000813/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000814static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100815channel_pre_output_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000816{
817 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000818 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000819 else
Damien Millerb38eff82000-04-01 11:09:21 +1000820 FD_SET(c->sock, writeset);
821}
822
823/*
824 * This is a special state for X11 authentication spoofing. An opened X11
825 * connection (when authentication spoofing is being done) remains in this
826 * state until the first packet has been completely read. The authentication
827 * data in that packet is then substituted by the real data if it matches the
828 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000829 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000830 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000831 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000832static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000833x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000834{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000835 u_char *ucp;
836 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000837
838 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000839 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000840 return 0;
841
842 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100843 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000844 if (ucp[0] == 0x42) { /* Byte order MSB first. */
845 proto_len = 256 * ucp[6] + ucp[7];
846 data_len = 256 * ucp[8] + ucp[9];
847 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
848 proto_len = ucp[6] + 256 * ucp[7];
849 data_len = ucp[8] + 256 * ucp[9];
850 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +1000851 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100852 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000853 return -1;
854 }
855
856 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000857 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000858 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
859 return 0;
860
861 /* Check if authentication protocol matches. */
862 if (proto_len != strlen(x11_saved_proto) ||
863 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000864 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +1000865 return -1;
866 }
867 /* Check if authentication data matches our fake data. */
868 if (data_len != x11_fake_data_len ||
869 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
870 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000871 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +1000872 return -1;
873 }
874 /* Check fake data length */
875 if (x11_fake_data_len != x11_saved_data_len) {
876 error("X11 fake_data_len %d != saved_data_len %d",
877 x11_fake_data_len, x11_saved_data_len);
878 return -1;
879 }
880 /*
881 * Received authentication protocol and data match
882 * our fake data. Substitute the fake data with real
883 * data.
884 */
885 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
886 x11_saved_data, x11_saved_data_len);
887 return 1;
888}
889
Ben Lindstrombba81212001-06-25 05:01:22 +0000890static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100891channel_pre_x11_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000892{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000893 int ret = x11_open_helper(&c->output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000894
Damien Millerb38eff82000-04-01 11:09:21 +1000895 if (ret == 1) {
896 /* Start normal processing for the channel. */
897 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000898 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000899 } else if (ret == -1) {
900 /*
901 * We have received an X11 connection that has bad
902 * authentication information.
903 */
Damien Miller996acd22003-04-09 20:59:48 +1000904 logit("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000905 buffer_clear(&c->input);
906 buffer_clear(&c->output);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000907 channel_close_fd(&c->sock);
Damien Millerb38eff82000-04-01 11:09:21 +1000908 c->sock = -1;
909 c->type = SSH_CHANNEL_CLOSED;
910 packet_start(SSH_MSG_CHANNEL_CLOSE);
911 packet_put_int(c->remote_id);
912 packet_send();
913 }
914}
915
Ben Lindstrombba81212001-06-25 05:01:22 +0000916static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100917channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000918{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000919 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000920
921 /* c->force_drain = 1; */
922
Damien Millerb38eff82000-04-01 11:09:21 +1000923 if (ret == 1) {
924 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +1100925 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000926 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +1000927 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +1000928 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +1100929 chan_read_failed(c);
930 buffer_clear(&c->input);
931 chan_ibuf_empty(c);
932 buffer_clear(&c->output);
933 /* for proto v1, the peer will send an IEOF */
934 if (compat20)
935 chan_write_failed(c);
936 else
937 c->type = SSH_CHANNEL_OPEN;
Damien Millerfbdeece2003-09-02 22:52:31 +1000938 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +1000939 }
940}
941
Ben Lindstromb3921512001-04-11 15:57:50 +0000942/* try to decode a socks4 header */
Damien Miller8473dd82006-07-24 14:08:32 +1000943/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000944static int
Damien Millerd62f2ca2006-03-26 13:57:41 +1100945channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000946{
Damien Millera10f5612002-09-12 09:49:15 +1000947 char *p, *host;
Damien Millereccb9de2005-06-17 12:59:34 +1000948 u_int len, have, i, found;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100949 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000950 struct {
951 u_int8_t version;
952 u_int8_t command;
953 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000954 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000955 } s4_req, s4_rsp;
956
Ben Lindstromb3921512001-04-11 15:57:50 +0000957 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000958
959 have = buffer_len(&c->input);
960 len = sizeof(s4_req);
961 if (have < len)
962 return 0;
963 p = buffer_ptr(&c->input);
964 for (found = 0, i = len; i < have; i++) {
965 if (p[i] == '\0') {
966 found = 1;
967 break;
968 }
969 if (i > 1024) {
970 /* the peer is probably sending garbage */
971 debug("channel %d: decode socks4: too long",
972 c->self);
973 return -1;
974 }
975 }
976 if (!found)
977 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000978 buffer_get(&c->input, (char *)&s4_req.version, 1);
979 buffer_get(&c->input, (char *)&s4_req.command, 1);
980 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000981 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000982 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000983 p = buffer_ptr(&c->input);
984 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000985 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000986 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000987 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000988 c->self, len, have);
989 strlcpy(username, p, sizeof(username));
990 buffer_consume(&c->input, len);
991 buffer_consume(&c->input, 1); /* trailing '\0' */
992
Ben Lindstromb3921512001-04-11 15:57:50 +0000993 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000994 strlcpy(c->path, host, sizeof(c->path));
995 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100996
Damien Millerfbdeece2003-09-02 22:52:31 +1000997 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000998 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000999
Ben Lindstromb3921512001-04-11 15:57:50 +00001000 if (s4_req.command != 1) {
1001 debug("channel %d: cannot handle: socks4 cn %d",
1002 c->self, s4_req.command);
1003 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001004 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001005 s4_rsp.version = 0; /* vn: 0 for reply */
1006 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001007 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001008 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Damien Millera0fdce92006-03-26 14:28:50 +11001009 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +00001010 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001011}
1012
Darren Tucker46471c92003-07-03 13:55:19 +10001013/* try to decode a socks5 header */
1014#define SSH_SOCKS5_AUTHDONE 0x1000
1015#define SSH_SOCKS5_NOAUTH 0x00
1016#define SSH_SOCKS5_IPV4 0x01
1017#define SSH_SOCKS5_DOMAIN 0x03
1018#define SSH_SOCKS5_IPV6 0x04
1019#define SSH_SOCKS5_CONNECT 0x01
1020#define SSH_SOCKS5_SUCCESS 0x00
1021
Damien Miller8473dd82006-07-24 14:08:32 +10001022/* ARGSUSED */
Darren Tucker46471c92003-07-03 13:55:19 +10001023static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001024channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001025{
1026 struct {
1027 u_int8_t version;
1028 u_int8_t command;
1029 u_int8_t reserved;
1030 u_int8_t atyp;
1031 } s5_req, s5_rsp;
1032 u_int16_t dest_port;
1033 u_char *p, dest_addr[255+1];
Damien Miller0f077072006-07-10 22:21:02 +10001034 u_int have, need, i, found, nmethods, addrlen, af;
Darren Tucker46471c92003-07-03 13:55:19 +10001035
1036 debug2("channel %d: decode socks5", c->self);
1037 p = buffer_ptr(&c->input);
1038 if (p[0] != 0x05)
1039 return -1;
1040 have = buffer_len(&c->input);
1041 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1042 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001043 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001044 return 0;
1045 nmethods = p[1];
1046 if (have < nmethods + 2)
1047 return 0;
1048 /* look for method: "NO AUTHENTICATION REQUIRED" */
1049 for (found = 0, i = 2 ; i < nmethods + 2; i++) {
1050 if (p[i] == SSH_SOCKS5_NOAUTH ) {
1051 found = 1;
1052 break;
1053 }
1054 }
1055 if (!found) {
1056 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1057 c->self);
1058 return -1;
1059 }
1060 buffer_consume(&c->input, nmethods + 2);
1061 buffer_put_char(&c->output, 0x05); /* version */
1062 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1063 FD_SET(c->sock, writeset);
1064 c->flags |= SSH_SOCKS5_AUTHDONE;
1065 debug2("channel %d: socks5 auth done", c->self);
1066 return 0; /* need more */
1067 }
1068 debug2("channel %d: socks5 post auth", c->self);
1069 if (have < sizeof(s5_req)+1)
1070 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001071 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001072 if (s5_req.version != 0x05 ||
1073 s5_req.command != SSH_SOCKS5_CONNECT ||
1074 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001075 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001076 return -1;
1077 }
Darren Tucker47eede72005-03-14 23:08:12 +11001078 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001079 case SSH_SOCKS5_IPV4:
1080 addrlen = 4;
1081 af = AF_INET;
1082 break;
1083 case SSH_SOCKS5_DOMAIN:
1084 addrlen = p[sizeof(s5_req)];
1085 af = -1;
1086 break;
1087 case SSH_SOCKS5_IPV6:
1088 addrlen = 16;
1089 af = AF_INET6;
1090 break;
1091 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001092 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001093 return -1;
1094 }
Damien Miller0f077072006-07-10 22:21:02 +10001095 need = sizeof(s5_req) + addrlen + 2;
1096 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1097 need++;
1098 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001099 return 0;
1100 buffer_consume(&c->input, sizeof(s5_req));
1101 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1102 buffer_consume(&c->input, 1); /* host string length */
1103 buffer_get(&c->input, (char *)&dest_addr, addrlen);
1104 buffer_get(&c->input, (char *)&dest_port, 2);
1105 dest_addr[addrlen] = '\0';
1106 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
Darren Tucker1f8311c2004-05-13 16:39:33 +10001107 strlcpy(c->path, (char *)dest_addr, sizeof(c->path));
Darren Tucker46471c92003-07-03 13:55:19 +10001108 else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL)
1109 return -1;
1110 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001111
Damien Millerfbdeece2003-09-02 22:52:31 +10001112 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001113 c->self, c->path, c->host_port, s5_req.command);
1114
1115 s5_rsp.version = 0x05;
1116 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1117 s5_rsp.reserved = 0; /* ignored */
1118 s5_rsp.atyp = SSH_SOCKS5_IPV4;
1119 ((struct in_addr *)&dest_addr)->s_addr = INADDR_ANY;
1120 dest_port = 0; /* ignored */
1121
Damien Millera0fdce92006-03-26 14:28:50 +11001122 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
1123 buffer_append(&c->output, &dest_addr, sizeof(struct in_addr));
1124 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001125 return 1;
1126}
1127
Ben Lindstromb3921512001-04-11 15:57:50 +00001128/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001129static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001130channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001131{
1132 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001133 u_int have;
1134 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001135
1136 have = buffer_len(&c->input);
Damien Miller4623a752001-10-10 15:03:58 +10001137 c->delayed = 0;
Ben Lindstromb3921512001-04-11 15:57:50 +00001138 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001139 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001140 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001141 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001142 /* need more */
1143 FD_SET(c->sock, readset);
1144 return;
1145 }
1146 /* try to guess the protocol */
1147 p = buffer_ptr(&c->input);
1148 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001149 case 0x04:
1150 ret = channel_decode_socks4(c, readset, writeset);
1151 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001152 case 0x05:
1153 ret = channel_decode_socks5(c, readset, writeset);
1154 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001155 default:
1156 ret = -1;
1157 break;
1158 }
1159 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001160 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001161 } else if (ret == 0) {
1162 debug2("channel %d: pre_dynamic: need more", c->self);
1163 /* need more */
1164 FD_SET(c->sock, readset);
1165 } else {
1166 /* switch to the next state */
1167 c->type = SSH_CHANNEL_OPENING;
1168 port_open_helper(c, "direct-tcpip");
1169 }
1170}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001171
Damien Millerb38eff82000-04-01 11:09:21 +10001172/* This is our fake X11 server socket. */
Damien Miller8473dd82006-07-24 14:08:32 +10001173/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001174static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001175channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001176{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001177 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001178 struct sockaddr addr;
Damien Miller398e1cf2002-02-05 11:52:13 +11001179 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001180 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001181 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001182 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001183
1184 if (FD_ISSET(c->sock, readset)) {
1185 debug("X11 connection requested.");
1186 addrlen = sizeof(addr);
1187 newsock = accept(c->sock, &addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001188 if (c->single_connection) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001189 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001190 channel_close_fd(&c->sock);
1191 chan_mark_dead(c);
1192 }
Damien Millerb38eff82000-04-01 11:09:21 +10001193 if (newsock < 0) {
1194 error("accept: %.100s", strerror(errno));
1195 return;
1196 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001197 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001198 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001199 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001200 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001201 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001202
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001203 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001204 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001205 c->local_window_max, c->local_maxpacket, 0, buf, 1);
Damien Millerbd483e72000-04-30 10:00:53 +10001206 if (compat20) {
1207 packet_start(SSH2_MSG_CHANNEL_OPEN);
1208 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001209 packet_put_int(nc->self);
Damien Millere7378562001-12-21 14:58:35 +11001210 packet_put_int(nc->local_window_max);
1211 packet_put_int(nc->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +11001212 /* originator ipaddr and port */
1213 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +10001214 if (datafellows & SSH_BUG_X11FWD) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001215 debug2("ssh2 x11 bug compat mode");
Damien Miller30c3d422000-05-09 11:02:59 +10001216 } else {
1217 packet_put_int(remote_port);
1218 }
Damien Millerbd483e72000-04-30 10:00:53 +10001219 packet_send();
1220 } else {
1221 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001222 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001223 if (packet_get_protocol_flags() &
1224 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +00001225 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +10001226 packet_send();
1227 }
Damien Millerd83ff352001-01-30 09:19:34 +11001228 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001229 }
1230}
1231
Ben Lindstrombba81212001-06-25 05:01:22 +00001232static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001233port_open_helper(Channel *c, char *rtype)
1234{
1235 int direct;
1236 char buf[1024];
1237 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001238 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001239
1240 direct = (strcmp(rtype, "direct-tcpip") == 0);
1241
1242 snprintf(buf, sizeof buf,
1243 "%s: listening port %d for %.100s port %d, "
1244 "connect from %.200s port %d",
1245 rtype, c->listening_port, c->path, c->host_port,
1246 remote_ipaddr, remote_port);
1247
1248 xfree(c->remote_name);
1249 c->remote_name = xstrdup(buf);
1250
1251 if (compat20) {
1252 packet_start(SSH2_MSG_CHANNEL_OPEN);
1253 packet_put_cstring(rtype);
1254 packet_put_int(c->self);
1255 packet_put_int(c->local_window_max);
1256 packet_put_int(c->local_maxpacket);
1257 if (direct) {
1258 /* target host, port */
1259 packet_put_cstring(c->path);
1260 packet_put_int(c->host_port);
1261 } else {
1262 /* listen address, port */
1263 packet_put_cstring(c->path);
1264 packet_put_int(c->listening_port);
1265 }
1266 /* originator host and port */
1267 packet_put_cstring(remote_ipaddr);
Damien Miller677257f2005-06-17 12:55:03 +10001268 packet_put_int((u_int)remote_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001269 packet_send();
1270 } else {
1271 packet_start(SSH_MSG_PORT_OPEN);
1272 packet_put_int(c->self);
1273 packet_put_cstring(c->path);
1274 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001275 if (packet_get_protocol_flags() &
1276 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001277 packet_put_cstring(c->remote_name);
1278 packet_send();
1279 }
1280 xfree(remote_ipaddr);
1281}
1282
Damien Miller5e7fd072005-11-05 14:53:39 +11001283static void
1284channel_set_reuseaddr(int fd)
1285{
1286 int on = 1;
1287
1288 /*
1289 * Set socket options.
1290 * Allow local port reuse in TIME_WAIT.
1291 */
1292 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1293 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1294}
1295
Damien Millerb38eff82000-04-01 11:09:21 +10001296/*
1297 * This socket is listening for connections to a forwarded TCP/IP port.
1298 */
Damien Miller8473dd82006-07-24 14:08:32 +10001299/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001300static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001301channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001302{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001303 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001304 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001305 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001306 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001307 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001308
Damien Millerb38eff82000-04-01 11:09:21 +10001309 if (FD_ISSET(c->sock, readset)) {
1310 debug("Connection to port %d forwarding "
1311 "to %.100s port %d requested.",
1312 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001313
Damien Miller4623a752001-10-10 15:03:58 +10001314 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1315 nextstate = SSH_CHANNEL_OPENING;
1316 rtype = "forwarded-tcpip";
1317 } else {
1318 if (c->host_port == 0) {
1319 nextstate = SSH_CHANNEL_DYNAMIC;
Damien Millerd3c04b92001-10-10 15:04:20 +10001320 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001321 } else {
1322 nextstate = SSH_CHANNEL_OPENING;
1323 rtype = "direct-tcpip";
1324 }
1325 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001326
Damien Millerb38eff82000-04-01 11:09:21 +10001327 addrlen = sizeof(addr);
1328 newsock = accept(c->sock, &addr, &addrlen);
1329 if (newsock < 0) {
1330 error("accept: %.100s", strerror(errno));
1331 return;
1332 }
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00001333 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001334 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1335 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001336 nc->listening_port = c->listening_port;
1337 nc->host_port = c->host_port;
1338 strlcpy(nc->path, c->path, sizeof(nc->path));
1339
Damien Miller4623a752001-10-10 15:03:58 +10001340 if (nextstate == SSH_CHANNEL_DYNAMIC) {
1341 /*
1342 * do not call the channel_post handler until
1343 * this flag has been reset by a pre-handler.
1344 * otherwise the FD_ISSET calls might overflow
1345 */
1346 nc->delayed = 1;
1347 } else {
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001348 port_open_helper(nc, rtype);
Damien Miller4623a752001-10-10 15:03:58 +10001349 }
Damien Millerb38eff82000-04-01 11:09:21 +10001350 }
1351}
1352
1353/*
1354 * This is the authentication agent socket listening for connections from
1355 * clients.
1356 */
Damien Miller8473dd82006-07-24 14:08:32 +10001357/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001358static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001359channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001360{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001361 Channel *nc;
1362 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001363 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001364 socklen_t addrlen;
1365
1366 if (FD_ISSET(c->sock, readset)) {
1367 addrlen = sizeof(addr);
1368 newsock = accept(c->sock, &addr, &addrlen);
1369 if (newsock < 0) {
1370 error("accept from auth socket: %.100s", strerror(errno));
1371 return;
1372 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001373 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001374 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1375 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001376 0, "accepted auth socket", 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001377 if (compat20) {
1378 packet_start(SSH2_MSG_CHANNEL_OPEN);
1379 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001380 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001381 packet_put_int(c->local_window_max);
1382 packet_put_int(c->local_maxpacket);
1383 } else {
1384 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001385 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001386 }
Damien Millerb38eff82000-04-01 11:09:21 +10001387 packet_send();
1388 }
1389}
1390
Damien Miller8473dd82006-07-24 14:08:32 +10001391/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001392static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001393channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001394{
Ben Lindstrom69128662001-05-08 20:07:39 +00001395 int err = 0;
Ben Lindstrom11180952001-07-04 05:13:35 +00001396 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001397
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001398 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001399 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001400 err = errno;
1401 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001402 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001403 if (err == 0) {
1404 debug("channel %d: connected", c->self);
1405 c->type = SSH_CHANNEL_OPEN;
1406 if (compat20) {
1407 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1408 packet_put_int(c->remote_id);
1409 packet_put_int(c->self);
1410 packet_put_int(c->local_window);
1411 packet_put_int(c->local_maxpacket);
1412 } else {
1413 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1414 packet_put_int(c->remote_id);
1415 packet_put_int(c->self);
1416 }
1417 } else {
1418 debug("channel %d: not connected: %s",
1419 c->self, strerror(err));
1420 if (compat20) {
1421 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1422 packet_put_int(c->remote_id);
1423 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1424 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1425 packet_put_cstring(strerror(err));
1426 packet_put_cstring("");
1427 }
1428 } else {
1429 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1430 packet_put_int(c->remote_id);
1431 }
1432 chan_mark_dead(c);
1433 }
1434 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001435 }
1436}
1437
Damien Miller8473dd82006-07-24 14:08:32 +10001438/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001439static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001440channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001441{
Darren Tucker11327cc2005-03-14 23:22:25 +11001442 char buf[CHAN_RBUF];
Damien Millerb38eff82000-04-01 11:09:21 +10001443 int len;
1444
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001445 if (c->rfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001446 FD_ISSET(c->rfd, readset)) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001447 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001448 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +10001449 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1450 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001451#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001452 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001453#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001454 if ((!c->isatty && len <= 0) ||
1455 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001456#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001457 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001458 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001459 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001460 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001461 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001462 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001463 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001464 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001465 c->type = SSH_CHANNEL_INPUT_DRAINING;
Damien Millerfbdeece2003-09-02 22:52:31 +10001466 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001467 } else {
1468 chan_read_failed(c);
1469 }
1470 return -1;
1471 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001472 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001473 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001474 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001475 chan_read_failed(c);
1476 }
Damien Millerd27b9472005-12-13 19:29:02 +11001477 } else if (c->datagram) {
1478 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001479 } else {
1480 buffer_append(&c->input, buf, len);
1481 }
Damien Millerb38eff82000-04-01 11:09:21 +10001482 }
1483 return 1;
1484}
Damien Miller4f7becb2006-03-26 14:10:14 +11001485
Damien Miller8473dd82006-07-24 14:08:32 +10001486/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001487static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001488channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001489{
Ben Lindstrome229b252001-03-05 06:28:06 +00001490 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001491 u_char *data = NULL, *buf;
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001492 u_int dlen;
Damien Millerb38eff82000-04-01 11:09:21 +10001493 int len;
1494
1495 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001496 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001497 FD_ISSET(c->wfd, writeset) &&
1498 buffer_len(&c->output) > 0) {
Damien Miller077b2382005-12-31 16:22:32 +11001499 if (c->output_filter != NULL) {
1500 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1501 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001502 if (c->type != SSH_CHANNEL_OPEN)
1503 chan_mark_dead(c);
1504 else
1505 chan_write_failed(c);
1506 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001507 }
1508 } else if (c->datagram) {
1509 buf = data = buffer_get_string(&c->output, &dlen);
1510 } else {
1511 buf = data = buffer_ptr(&c->output);
1512 dlen = buffer_len(&c->output);
1513 }
1514
Damien Millerd27b9472005-12-13 19:29:02 +11001515 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001516 /* ignore truncated writes, datagrams might get lost */
1517 c->local_consumed += dlen + 4;
Damien Miller077b2382005-12-31 16:22:32 +11001518 len = write(c->wfd, buf, dlen);
Damien Millerd27b9472005-12-13 19:29:02 +11001519 xfree(data);
1520 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1521 return 1;
1522 if (len <= 0) {
1523 if (c->type != SSH_CHANNEL_OPEN)
1524 chan_mark_dead(c);
1525 else
1526 chan_write_failed(c);
1527 return -1;
1528 }
1529 return 1;
1530 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001531#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001532 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker240fdfa2003-11-22 14:10:02 +11001533 if (compat20 && c->wfd_isatty)
1534 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001535#endif
Damien Miller077b2382005-12-31 16:22:32 +11001536
1537 len = write(c->wfd, buf, dlen);
Damien Miller6f83b8e2000-05-02 09:23:45 +10001538 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1539 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001540 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001541 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001542 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001543 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001544 return -1;
1545 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001546 buffer_clear(&c->output);
Damien Millerfbdeece2003-09-02 22:52:31 +10001547 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001548 c->type = SSH_CHANNEL_INPUT_DRAINING;
1549 } else {
1550 chan_write_failed(c);
1551 }
1552 return -1;
1553 }
Damien Miller077b2382005-12-31 16:22:32 +11001554 if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001555 if (tcgetattr(c->wfd, &tio) == 0 &&
1556 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1557 /*
1558 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001559 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001560 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001561 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001562 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001563 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001564 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001565 }
1566 }
Damien Millerb38eff82000-04-01 11:09:21 +10001567 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001568 if (compat20 && len > 0) {
1569 c->local_consumed += len;
1570 }
1571 }
1572 return 1;
1573}
Damien Miller4f7becb2006-03-26 14:10:14 +11001574
Ben Lindstrombba81212001-06-25 05:01:22 +00001575static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001576channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001577{
Darren Tucker11327cc2005-03-14 23:22:25 +11001578 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001579 int len;
1580
Damien Miller1383bd82000-04-06 12:32:37 +10001581/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001582 if (c->efd != -1) {
1583 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1584 FD_ISSET(c->efd, writeset) &&
1585 buffer_len(&c->extended) > 0) {
1586 len = write(c->efd, buffer_ptr(&c->extended),
1587 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001588 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001589 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001590 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1591 return 1;
1592 if (len <= 0) {
1593 debug2("channel %d: closing write-efd %d",
1594 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001595 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001596 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001597 buffer_consume(&c->extended, len);
1598 c->local_consumed += len;
1599 }
1600 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1601 FD_ISSET(c->efd, readset)) {
1602 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001603 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001604 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001605 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1606 return 1;
1607 if (len <= 0) {
1608 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001609 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001610 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001611 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001612 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001613 }
Damien Miller33b13562000-04-04 14:38:59 +10001614 }
1615 }
1616 return 1;
1617}
Damien Miller4f7becb2006-03-26 14:10:14 +11001618
Damien Miller8473dd82006-07-24 14:08:32 +10001619/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001620static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001621channel_handle_ctl(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller0e220db2004-06-15 10:34:08 +10001622{
1623 char buf[16];
1624 int len;
1625
1626 /* Monitor control fd to detect if the slave client exits */
1627 if (c->ctl_fd != -1 && FD_ISSET(c->ctl_fd, readset)) {
1628 len = read(c->ctl_fd, buf, sizeof(buf));
1629 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1630 return 1;
1631 if (len <= 0) {
1632 debug2("channel %d: ctl read<=0", c->self);
1633 if (c->type != SSH_CHANNEL_OPEN) {
1634 debug2("channel %d: not open", c->self);
1635 chan_mark_dead(c);
1636 return -1;
1637 } else {
1638 chan_read_failed(c);
1639 chan_write_failed(c);
1640 }
1641 return -1;
1642 } else
1643 fatal("%s: unexpected data on ctl fd", __func__);
1644 }
1645 return 1;
1646}
Damien Miller4f7becb2006-03-26 14:10:14 +11001647
Damien Miller0e220db2004-06-15 10:34:08 +10001648static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001649channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001650{
Ben Lindstromb3921512001-04-11 15:57:50 +00001651 if (c->type == SSH_CHANNEL_OPEN &&
1652 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001653 c->local_window < c->local_window_max/2 &&
1654 c->local_consumed > 0) {
1655 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1656 packet_put_int(c->remote_id);
1657 packet_put_int(c->local_consumed);
1658 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001659 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001660 c->self, c->local_window,
1661 c->local_consumed);
1662 c->local_window += c->local_consumed;
1663 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001664 }
1665 return 1;
1666}
1667
Ben Lindstrombba81212001-06-25 05:01:22 +00001668static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001669channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001670{
Damien Miller4623a752001-10-10 15:03:58 +10001671 if (c->delayed)
1672 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001673 channel_handle_rfd(c, readset, writeset);
1674 channel_handle_wfd(c, readset, writeset);
Damien Millerde6987c2002-01-22 23:20:40 +11001675 if (!compat20)
Damien Miller4623a752001-10-10 15:03:58 +10001676 return;
Damien Miller33b13562000-04-04 14:38:59 +10001677 channel_handle_efd(c, readset, writeset);
Damien Miller0e220db2004-06-15 10:34:08 +10001678 channel_handle_ctl(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001679 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001680}
1681
Damien Miller8473dd82006-07-24 14:08:32 +10001682/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001683static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001684channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001685{
1686 int len;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001687
Damien Millerb38eff82000-04-01 11:09:21 +10001688 /* Send buffered output data to the socket. */
1689 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1690 len = write(c->sock, buffer_ptr(&c->output),
1691 buffer_len(&c->output));
1692 if (len <= 0)
Damien Miller76765c02002-01-22 23:21:15 +11001693 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001694 else
1695 buffer_consume(&c->output, len);
1696 }
1697}
1698
Ben Lindstrombba81212001-06-25 05:01:22 +00001699static void
Damien Miller33b13562000-04-04 14:38:59 +10001700channel_handler_init_20(void)
1701{
Damien Millerde6987c2002-01-22 23:20:40 +11001702 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001703 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001704 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001705 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001706 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001707 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001708 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001709 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001710
Damien Millerde6987c2002-01-22 23:20:40 +11001711 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001712 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001713 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001714 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001715 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001716 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001717 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001718}
1719
Ben Lindstrombba81212001-06-25 05:01:22 +00001720static void
Damien Millerb38eff82000-04-01 11:09:21 +10001721channel_handler_init_13(void)
1722{
1723 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1724 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1725 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1726 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1727 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1728 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1729 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001730 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001731 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001732
Damien Millerde6987c2002-01-22 23:20:40 +11001733 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001734 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1735 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1736 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1737 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001738 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001739 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001740}
1741
Ben Lindstrombba81212001-06-25 05:01:22 +00001742static void
Damien Millerb38eff82000-04-01 11:09:21 +10001743channel_handler_init_15(void)
1744{
Damien Millerde6987c2002-01-22 23:20:40 +11001745 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001746 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001747 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1748 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1749 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001750 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001751 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001752
1753 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1754 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1755 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Damien Millerde6987c2002-01-22 23:20:40 +11001756 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001757 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001758 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001759}
1760
Ben Lindstrombba81212001-06-25 05:01:22 +00001761static void
Damien Millerb38eff82000-04-01 11:09:21 +10001762channel_handler_init(void)
1763{
1764 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001765
Damien Miller9f0f5c62001-12-21 14:45:46 +11001766 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001767 channel_pre[i] = NULL;
1768 channel_post[i] = NULL;
1769 }
Damien Miller33b13562000-04-04 14:38:59 +10001770 if (compat20)
1771 channel_handler_init_20();
1772 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001773 channel_handler_init_13();
1774 else
1775 channel_handler_init_15();
1776}
1777
Damien Miller3ec27592001-10-12 11:35:04 +10001778/* gc dead channels */
1779static void
1780channel_garbage_collect(Channel *c)
1781{
1782 if (c == NULL)
1783 return;
1784 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11001785 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10001786 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001787 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001788 c->detach_user(c->self, NULL);
1789 /* if we still have a callback */
1790 if (c->detach_user != NULL)
1791 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001792 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001793 }
1794 if (!chan_is_dead(c, 1))
1795 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001796 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001797 channel_free(c);
1798}
1799
Ben Lindstrombba81212001-06-25 05:01:22 +00001800static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001801channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001802{
1803 static int did_init = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001804 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001805 Channel *c;
1806
1807 if (!did_init) {
1808 channel_handler_init();
1809 did_init = 1;
1810 }
1811 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001812 c = channels[i];
1813 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001814 continue;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001815 if (ftab[c->type] != NULL)
1816 (*ftab[c->type])(c, readset, writeset);
Damien Miller3ec27592001-10-12 11:35:04 +10001817 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001818 }
1819}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001820
Ben Lindstrome9c99912001-06-09 00:41:05 +00001821/*
1822 * Allocate/update select bitmasks and add any bits relevant to channels in
1823 * select bitmasks.
1824 */
Damien Miller4af51302000-04-16 11:18:38 +10001825void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001826channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001827 u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001828{
Damien Miller36812092006-03-26 14:22:47 +11001829 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11001830
1831 n = MAX(*maxfdp, channel_max_fd);
1832
Damien Miller36812092006-03-26 14:22:47 +11001833 nfdset = howmany(n+1, NFDBITS);
1834 /* Explicitly test here, because xrealloc isn't always called */
1835 if (nfdset && SIZE_T_MAX / nfdset < sizeof(fd_mask))
1836 fatal("channel_prepare_select: max_fd (%d) is too large", n);
1837 sz = nfdset * sizeof(fd_mask);
1838
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001839 /* perhaps check sz < nalloc/2 and shrink? */
1840 if (*readsetp == NULL || sz > *nallocp) {
Damien Miller36812092006-03-26 14:22:47 +11001841 *readsetp = xrealloc(*readsetp, nfdset, sizeof(fd_mask));
1842 *writesetp = xrealloc(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001843 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11001844 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001845 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11001846 memset(*readsetp, 0, sz);
1847 memset(*writesetp, 0, sz);
1848
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001849 if (!rekeying)
1850 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001851}
1852
Ben Lindstrome9c99912001-06-09 00:41:05 +00001853/*
1854 * After select, perform any appropriate operations for channels which have
1855 * events pending.
1856 */
Damien Miller4af51302000-04-16 11:18:38 +10001857void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001858channel_after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001859{
Damien Millerb38eff82000-04-01 11:09:21 +10001860 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001861}
1862
Ben Lindstrome9c99912001-06-09 00:41:05 +00001863
Damien Miller5e953212001-01-30 09:14:00 +11001864/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10001865void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001866channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001867{
Damien Millerb38eff82000-04-01 11:09:21 +10001868 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001869 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001870
Damien Miller95def091999-11-25 00:26:21 +11001871 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001872 c = channels[i];
1873 if (c == NULL)
1874 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001875
Ben Lindstrome9c99912001-06-09 00:41:05 +00001876 /*
1877 * We are only interested in channels that can have buffered
1878 * incoming data.
1879 */
Damien Miller34132e52000-01-14 15:45:46 +11001880 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001881 if (c->type != SSH_CHANNEL_OPEN &&
1882 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001883 continue;
1884 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001885 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001886 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001887 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001888 if (compat20 &&
1889 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001890 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10001891 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001892 continue;
1893 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001894
Damien Miller95def091999-11-25 00:26:21 +11001895 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001896 if ((c->istate == CHAN_INPUT_OPEN ||
1897 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1898 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001899 if (c->datagram) {
1900 if (len > 0) {
1901 u_char *data;
1902 u_int dlen;
1903
1904 data = buffer_get_string(&c->input,
1905 &dlen);
1906 packet_start(SSH2_MSG_CHANNEL_DATA);
1907 packet_put_int(c->remote_id);
1908 packet_put_string(data, dlen);
1909 packet_send();
1910 c->remote_window -= dlen + 4;
1911 xfree(data);
1912 }
1913 continue;
1914 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001915 /*
1916 * Send some data for the other side over the secure
1917 * connection.
1918 */
Damien Miller33b13562000-04-04 14:38:59 +10001919 if (compat20) {
1920 if (len > c->remote_window)
1921 len = c->remote_window;
1922 if (len > c->remote_maxpacket)
1923 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001924 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001925 if (packet_is_interactive()) {
1926 if (len > 1024)
1927 len = 512;
1928 } else {
1929 /* Keep the packets at reasonable size. */
1930 if (len > packet_get_maxsize()/2)
1931 len = packet_get_maxsize()/2;
1932 }
Damien Miller95def091999-11-25 00:26:21 +11001933 }
Damien Millerb38eff82000-04-01 11:09:21 +10001934 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001935 packet_start(compat20 ?
1936 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001937 packet_put_int(c->remote_id);
1938 packet_put_string(buffer_ptr(&c->input), len);
1939 packet_send();
1940 buffer_consume(&c->input, len);
1941 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001942 }
1943 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001944 if (compat13)
1945 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001946 /*
1947 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00001948 * tell peer, that we will not send more data: send IEOF.
1949 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11001950 */
Ben Lindstromcf159442002-03-26 03:26:24 +00001951 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10001952 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
1953 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00001954 else
1955 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001956 }
Damien Miller33b13562000-04-04 14:38:59 +10001957 /* Send extended data, i.e. stderr */
1958 if (compat20 &&
Ben Lindstromcf159442002-03-26 03:26:24 +00001959 !(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10001960 c->remote_window > 0 &&
1961 (len = buffer_len(&c->extended)) > 0 &&
1962 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001963 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001964 c->self, c->remote_window, buffer_len(&c->extended),
1965 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001966 if (len > c->remote_window)
1967 len = c->remote_window;
1968 if (len > c->remote_maxpacket)
1969 len = c->remote_maxpacket;
1970 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1971 packet_put_int(c->remote_id);
1972 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1973 packet_put_string(buffer_ptr(&c->extended), len);
1974 packet_send();
1975 buffer_consume(&c->extended, len);
1976 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001977 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001978 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001979 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001980}
1981
Ben Lindstrome9c99912001-06-09 00:41:05 +00001982
1983/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11001984
1985/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10001986void
Damien Miller630d6f42002-01-22 23:17:30 +11001987channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001988{
Damien Miller34132e52000-01-14 15:45:46 +11001989 int id;
Damien Miller95def091999-11-25 00:26:21 +11001990 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001991 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001992 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001993
Damien Miller95def091999-11-25 00:26:21 +11001994 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001995 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001996 c = channel_lookup(id);
1997 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001998 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001999
Damien Miller95def091999-11-25 00:26:21 +11002000 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002001 if (c->type != SSH_CHANNEL_OPEN &&
2002 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11002003 return;
2004
Damien Miller95def091999-11-25 00:26:21 +11002005 /* Get the data. */
2006 data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10002007
Damien Millera04ad492004-01-21 11:02:09 +11002008 /*
2009 * Ignore data for protocol > 1.3 if output end is no longer open.
2010 * For protocol 2 the sending side is reducing its window as it sends
2011 * data, so we must 'fake' consumption of the data in order to ensure
2012 * that window updates are sent back. Otherwise the connection might
2013 * deadlock.
2014 */
2015 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
2016 if (compat20) {
2017 c->local_window -= data_len;
2018 c->local_consumed += data_len;
2019 }
2020 xfree(data);
2021 return;
2022 }
2023
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002024 if (compat20) {
Damien Miller33b13562000-04-04 14:38:59 +10002025 if (data_len > c->local_maxpacket) {
Damien Miller996acd22003-04-09 20:59:48 +10002026 logit("channel %d: rcvd big packet %d, maxpack %d",
Damien Miller33b13562000-04-04 14:38:59 +10002027 c->self, data_len, c->local_maxpacket);
2028 }
2029 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002030 logit("channel %d: rcvd too much data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002031 c->self, data_len, c->local_window);
2032 xfree(data);
2033 return;
2034 }
2035 c->local_window -= data_len;
Damien Miller33b13562000-04-04 14:38:59 +10002036 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002037 packet_check_eom();
Damien Millerd27b9472005-12-13 19:29:02 +11002038 if (c->datagram)
2039 buffer_put_string(&c->output, data, data_len);
2040 else
2041 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11002042 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002043}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002044
Damien Millerd79b4242006-03-31 23:11:44 +11002045/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002046void
Damien Miller630d6f42002-01-22 23:17:30 +11002047channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002048{
2049 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002050 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002051 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002052 Channel *c;
2053
2054 /* Get the channel number and verify it. */
2055 id = packet_get_int();
2056 c = channel_lookup(id);
2057
2058 if (c == NULL)
2059 packet_disconnect("Received extended_data for bad channel %d.", id);
2060 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002061 logit("channel %d: ext data for non open", id);
Damien Miller33b13562000-04-04 14:38:59 +10002062 return;
2063 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002064 if (c->flags & CHAN_EOF_RCVD) {
2065 if (datafellows & SSH_BUG_EXTEOF)
2066 debug("channel %d: accepting ext data after eof", id);
2067 else
2068 packet_disconnect("Received extended_data after EOF "
2069 "on channel %d.", id);
2070 }
Damien Miller33b13562000-04-04 14:38:59 +10002071 tcode = packet_get_int();
2072 if (c->efd == -1 ||
2073 c->extended_usage != CHAN_EXTENDED_WRITE ||
2074 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002075 logit("channel %d: bad ext data", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002076 return;
2077 }
2078 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002079 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002080 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002081 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002082 c->self, data_len, c->local_window);
2083 xfree(data);
2084 return;
2085 }
Damien Millerd3444942000-09-30 14:20:03 +11002086 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002087 c->local_window -= data_len;
2088 buffer_append(&c->extended, data, data_len);
2089 xfree(data);
2090}
2091
Damien Millerd79b4242006-03-31 23:11:44 +11002092/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002093void
Damien Miller630d6f42002-01-22 23:17:30 +11002094channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002095{
2096 int id;
2097 Channel *c;
2098
Damien Millerb38eff82000-04-01 11:09:21 +10002099 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002100 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002101 c = channel_lookup(id);
2102 if (c == NULL)
2103 packet_disconnect("Received ieof for nonexistent channel %d.", id);
2104 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002105
2106 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002107 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002108 debug("channel %d: FORCE input drain", c->self);
2109 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002110 if (buffer_len(&c->input) == 0)
2111 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002112 }
2113
Damien Millerb38eff82000-04-01 11:09:21 +10002114}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002115
Damien Millerd79b4242006-03-31 23:11:44 +11002116/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002117void
Damien Miller630d6f42002-01-22 23:17:30 +11002118channel_input_close(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002119{
Damien Millerb38eff82000-04-01 11:09:21 +10002120 int id;
2121 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002122
Damien Millerb38eff82000-04-01 11:09:21 +10002123 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002124 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002125 c = channel_lookup(id);
2126 if (c == NULL)
2127 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11002128
2129 /*
2130 * Send a confirmation that we have closed the channel and no more
2131 * data is coming for it.
2132 */
Damien Miller95def091999-11-25 00:26:21 +11002133 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10002134 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11002135 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002136
Damien Miller5428f641999-11-25 11:54:57 +11002137 /*
2138 * If the channel is in closed state, we have sent a close request,
2139 * and the other side will eventually respond with a confirmation.
2140 * Thus, we cannot free the channel here, because then there would be
2141 * no-one to receive the confirmation. The channel gets freed when
2142 * the confirmation arrives.
2143 */
Damien Millerb38eff82000-04-01 11:09:21 +10002144 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11002145 /*
2146 * Not a closed channel - mark it as draining, which will
2147 * cause it to be freed later.
2148 */
Damien Miller76765c02002-01-22 23:21:15 +11002149 buffer_clear(&c->input);
Damien Millerb38eff82000-04-01 11:09:21 +10002150 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11002151 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002152}
2153
Damien Millerb38eff82000-04-01 11:09:21 +10002154/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Millerd79b4242006-03-31 23:11:44 +11002155/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002156void
Damien Miller630d6f42002-01-22 23:17:30 +11002157channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002158{
Damien Millerb38eff82000-04-01 11:09:21 +10002159 int id = packet_get_int();
2160 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002161
Damien Miller48b03fc2002-01-22 23:11:40 +11002162 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002163 if (c == NULL)
2164 packet_disconnect("Received oclose for nonexistent channel %d.", id);
2165 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002166}
2167
Damien Millerd79b4242006-03-31 23:11:44 +11002168/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002169void
Damien Miller630d6f42002-01-22 23:17:30 +11002170channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002171{
2172 int id = packet_get_int();
2173 Channel *c = channel_lookup(id);
2174
Damien Miller48b03fc2002-01-22 23:11:40 +11002175 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002176 if (c == NULL)
2177 packet_disconnect("Received close confirmation for "
2178 "out-of-range channel %d.", id);
2179 if (c->type != SSH_CHANNEL_CLOSED)
2180 packet_disconnect("Received close confirmation for "
2181 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002182 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002183}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002184
Damien Millerd79b4242006-03-31 23:11:44 +11002185/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002186void
Damien Miller630d6f42002-01-22 23:17:30 +11002187channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002188{
Damien Millerb38eff82000-04-01 11:09:21 +10002189 int id, remote_id;
2190 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002191
Damien Millerb38eff82000-04-01 11:09:21 +10002192 id = packet_get_int();
2193 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002194
Damien Millerb38eff82000-04-01 11:09:21 +10002195 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2196 packet_disconnect("Received open confirmation for "
2197 "non-opening channel %d.", id);
2198 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002199 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002200 c->remote_id = remote_id;
2201 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002202
2203 if (compat20) {
2204 c->remote_window = packet_get_int();
2205 c->remote_maxpacket = packet_get_int();
Damien Miller67f0bc02002-02-05 12:23:08 +11002206 if (c->confirm) {
Damien Millerd3444942000-09-30 14:20:03 +11002207 debug2("callback start");
Damien Miller0e220db2004-06-15 10:34:08 +10002208 c->confirm(c->self, c->confirm_ctx);
Damien Millerd3444942000-09-30 14:20:03 +11002209 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002210 }
Damien Millerfbdeece2003-09-02 22:52:31 +10002211 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
Damien Miller33b13562000-04-04 14:38:59 +10002212 c->remote_window, c->remote_maxpacket);
2213 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002214 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002215}
2216
Ben Lindstrombba81212001-06-25 05:01:22 +00002217static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002218reason2txt(int reason)
2219{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002220 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002221 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2222 return "administratively prohibited";
2223 case SSH2_OPEN_CONNECT_FAILED:
2224 return "connect failed";
2225 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2226 return "unknown channel type";
2227 case SSH2_OPEN_RESOURCE_SHORTAGE:
2228 return "resource shortage";
2229 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002230 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002231}
2232
Damien Millerd79b4242006-03-31 23:11:44 +11002233/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002234void
Damien Miller630d6f42002-01-22 23:17:30 +11002235channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002236{
Kevin Steves12057502001-02-05 14:54:34 +00002237 int id, reason;
2238 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002239 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002240
Damien Millerb38eff82000-04-01 11:09:21 +10002241 id = packet_get_int();
2242 c = channel_lookup(id);
2243
2244 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2245 packet_disconnect("Received open failure for "
2246 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002247 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00002248 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00002249 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00002250 msg = packet_get_string(NULL);
2251 lang = packet_get_string(NULL);
2252 }
Damien Miller996acd22003-04-09 20:59:48 +10002253 logit("channel %d: open failed: %s%s%s", id,
Ben Lindstrom69128662001-05-08 20:07:39 +00002254 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00002255 if (msg != NULL)
2256 xfree(msg);
2257 if (lang != NULL)
2258 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10002259 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002260 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11002261 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002262 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002263}
2264
Damien Millerd79b4242006-03-31 23:11:44 +11002265/* ARGSUSED */
Damien Miller33b13562000-04-04 14:38:59 +10002266void
Damien Miller630d6f42002-01-22 23:17:30 +11002267channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002268{
2269 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002270 int id;
2271 u_int adjust;
Damien Miller33b13562000-04-04 14:38:59 +10002272
2273 if (!compat20)
2274 return;
2275
2276 /* Get the channel number and verify it. */
2277 id = packet_get_int();
2278 c = channel_lookup(id);
2279
Damien Millerd47c62a2005-12-13 19:33:57 +11002280 if (c == NULL) {
2281 logit("Received window adjust for non-open channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002282 return;
2283 }
2284 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002285 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002286 debug2("channel %d: rcvd adjust %u", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10002287 c->remote_window += adjust;
2288}
2289
Damien Millerd79b4242006-03-31 23:11:44 +11002290/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002291void
Damien Miller630d6f42002-01-22 23:17:30 +11002292channel_input_port_open(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002293{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002294 Channel *c = NULL;
2295 u_short host_port;
2296 char *host, *originator_string;
2297 int remote_id, sock = -1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002298
Ben Lindstrome9c99912001-06-09 00:41:05 +00002299 remote_id = packet_get_int();
2300 host = packet_get_string(NULL);
2301 host_port = packet_get_int();
2302
2303 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2304 originator_string = packet_get_string(NULL);
2305 } else {
2306 originator_string = xstrdup("unknown (remote did not supply name)");
2307 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002308 packet_check_eom();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002309 sock = channel_connect_to(host, host_port);
2310 if (sock != -1) {
2311 c = channel_new("connected socket",
2312 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2313 originator_string, 1);
Damien Miller699d0032002-02-08 22:07:16 +11002314 c->remote_id = remote_id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002315 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002316 xfree(originator_string);
Ben Lindstrome9c99912001-06-09 00:41:05 +00002317 if (c == NULL) {
2318 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2319 packet_put_int(remote_id);
2320 packet_send();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002321 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002322 xfree(host);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002323}
2324
2325
Ben Lindstrome9c99912001-06-09 00:41:05 +00002326/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002327
Ben Lindstrom908afed2001-10-03 17:34:59 +00002328void
2329channel_set_af(int af)
2330{
2331 IPv4or6 = af;
2332}
2333
Damien Millerb16461c2002-01-22 23:29:22 +11002334static int
2335channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
2336 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002337{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002338 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11002339 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11002340 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002341 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11002342 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002343
Damien Millerb16461c2002-01-22 23:29:22 +11002344 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2345 listen_addr : host_to_connect;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002346 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00002347
Damien Millerb16461c2002-01-22 23:29:22 +11002348 if (host == NULL) {
2349 error("No forward host name.");
Damien Millera7270302005-07-06 09:36:05 +10002350 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002351 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002352 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00002353 error("Forward host name too long.");
Damien Millera7270302005-07-06 09:36:05 +10002354 return 0;
Kevin Steves12057502001-02-05 14:54:34 +00002355 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002356
Damien Miller5428f641999-11-25 11:54:57 +11002357 /*
Damien Millerf91ee4c2005-03-01 21:24:33 +11002358 * Determine whether or not a port forward listens to loopback,
Darren Tucker47eede72005-03-14 23:08:12 +11002359 * specified address or wildcard. On the client, a specified bind
2360 * address will always override gateway_ports. On the server, a
2361 * gateway_ports of 1 (``yes'') will override the client's
2362 * specification and force a wildcard bind, whereas a value of 2
2363 * (``clientspecified'') will bind to whatever address the client
Damien Millerf91ee4c2005-03-01 21:24:33 +11002364 * asked for.
2365 *
2366 * Special-case listen_addrs are:
2367 *
2368 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2369 * "" (empty string), "*" -> wildcard v4/v6
2370 * "localhost" -> loopback v4/v6
2371 */
2372 addr = NULL;
2373 if (listen_addr == NULL) {
2374 /* No address specified: default to gateway_ports setting */
2375 if (gateway_ports)
2376 wildcard = 1;
2377 } else if (gateway_ports || is_client) {
2378 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2379 strcmp(listen_addr, "0.0.0.0") == 0) ||
2380 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
2381 (!is_client && gateway_ports == 1))
2382 wildcard = 1;
2383 else if (strcmp(listen_addr, "localhost") != 0)
2384 addr = listen_addr;
2385 }
2386
2387 debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s",
2388 type, wildcard, (addr == NULL) ? "NULL" : addr);
2389
2390 /*
Damien Miller34132e52000-01-14 15:45:46 +11002391 * getaddrinfo returns a loopback address if the hostname is
2392 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002393 */
Damien Miller34132e52000-01-14 15:45:46 +11002394 memset(&hints, 0, sizeof(hints));
2395 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002396 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002397 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002398 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002399 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2400 if (addr == NULL) {
2401 /* This really shouldn't happen */
2402 packet_disconnect("getaddrinfo: fatal error: %s",
2403 gai_strerror(r));
2404 } else {
Damien Millera7270302005-07-06 09:36:05 +10002405 error("channel_setup_fwd_listener: "
Damien Millerf91ee4c2005-03-01 21:24:33 +11002406 "getaddrinfo(%.64s): %s", addr, gai_strerror(r));
2407 }
Damien Millera7270302005-07-06 09:36:05 +10002408 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002409 }
Damien Miller5428f641999-11-25 11:54:57 +11002410
Damien Miller34132e52000-01-14 15:45:46 +11002411 for (ai = aitop; ai; ai = ai->ai_next) {
2412 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2413 continue;
2414 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2415 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Millerb16461c2002-01-22 23:29:22 +11002416 error("channel_setup_fwd_listener: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002417 continue;
2418 }
2419 /* Create a port to listen for the host. */
Damien Miller2372ace2003-05-14 13:42:23 +10002420 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002421 if (sock < 0) {
2422 /* this is no error since kernel may not support ipv6 */
2423 verbose("socket: %.100s", strerror(errno));
2424 continue;
2425 }
Damien Miller5e7fd072005-11-05 14:53:39 +11002426
2427 channel_set_reuseaddr(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10002428
Damien Miller34132e52000-01-14 15:45:46 +11002429 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002430
Damien Miller34132e52000-01-14 15:45:46 +11002431 /* Bind the socket to the address. */
2432 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2433 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002434 if (!ai->ai_next)
2435 error("bind: %.100s", strerror(errno));
2436 else
2437 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002438
Damien Miller34132e52000-01-14 15:45:46 +11002439 close(sock);
2440 continue;
2441 }
2442 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11002443 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002444 error("listen: %.100s", strerror(errno));
2445 close(sock);
2446 continue;
2447 }
2448 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002449 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002450 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002451 0, "port listener", 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002452 strlcpy(c->path, host, sizeof(c->path));
2453 c->host_port = port_to_connect;
2454 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002455 success = 1;
2456 }
2457 if (success == 0)
Damien Millerb16461c2002-01-22 23:29:22 +11002458 error("channel_setup_fwd_listener: cannot listen to port: %d",
Kevin Steves12057502001-02-05 14:54:34 +00002459 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002460 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002461 return success;
Damien Miller95def091999-11-25 00:26:21 +11002462}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002463
Darren Tuckere7066df2004-05-24 10:18:05 +10002464int
2465channel_cancel_rport_listener(const char *host, u_short port)
2466{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002467 u_int i;
2468 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10002469
Darren Tucker47eede72005-03-14 23:08:12 +11002470 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10002471 Channel *c = channels[i];
2472
2473 if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
2474 strncmp(c->path, host, sizeof(c->path)) == 0 &&
Darren Tuckerfc959702004-07-17 16:12:08 +10002475 c->listening_port == port) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10002476 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10002477 channel_free(c);
2478 found = 1;
2479 }
2480 }
2481
2482 return (found);
2483}
2484
Damien Millerb16461c2002-01-22 23:29:22 +11002485/* protocol local port fwd, used by ssh (and sshd in v1) */
2486int
Damien Millerf91ee4c2005-03-01 21:24:33 +11002487channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
Damien Millerb16461c2002-01-22 23:29:22 +11002488 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2489{
2490 return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002491 listen_host, listen_port, host_to_connect, port_to_connect,
2492 gateway_ports);
Damien Millerb16461c2002-01-22 23:29:22 +11002493}
2494
2495/* protocol v2 remote port fwd, used by sshd */
2496int
2497channel_setup_remote_fwd_listener(const char *listen_address,
2498 u_short listen_port, int gateway_ports)
2499{
2500 return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
2501 listen_address, listen_port, NULL, 0, gateway_ports);
2502}
2503
Damien Miller5428f641999-11-25 11:54:57 +11002504/*
2505 * Initiate forwarding of connections to port "port" on remote host through
2506 * the secure channel to host:port from local side.
2507 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002508
Darren Tuckere7d4b192006-07-12 22:17:10 +10002509int
Damien Millerf91ee4c2005-03-01 21:24:33 +11002510channel_request_remote_forwarding(const char *listen_host, u_short listen_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +11002511 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002512{
Damien Millerdff50992002-01-22 23:16:32 +11002513 int type, success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002514
Damien Miller95def091999-11-25 00:26:21 +11002515 /* Record locally that connection to this host/port is permitted. */
2516 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2517 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002518
Damien Miller95def091999-11-25 00:26:21 +11002519 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002520 if (compat20) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002521 const char *address_to_bind;
2522 if (listen_host == NULL)
2523 address_to_bind = "localhost";
2524 else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0)
2525 address_to_bind = "";
2526 else
2527 address_to_bind = listen_host;
2528
Damien Miller33b13562000-04-04 14:38:59 +10002529 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2530 packet_put_cstring("tcpip-forward");
Damien Miller2797f7f2002-04-23 21:09:44 +10002531 packet_put_char(1); /* boolean: want reply */
Damien Miller33b13562000-04-04 14:38:59 +10002532 packet_put_cstring(address_to_bind);
2533 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002534 packet_send();
2535 packet_write_wait();
2536 /* Assume that server accepts the request */
2537 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002538 } else {
2539 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002540 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002541 packet_put_cstring(host_to_connect);
2542 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002543 packet_send();
2544 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002545
2546 /* Wait for response from the remote side. */
Damien Millerdff50992002-01-22 23:16:32 +11002547 type = packet_read();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002548 switch (type) {
2549 case SSH_SMSG_SUCCESS:
2550 success = 1;
2551 break;
2552 case SSH_SMSG_FAILURE:
Damien Miller0bc1bd82000-11-13 22:57:25 +11002553 break;
2554 default:
2555 /* Unknown packet */
2556 packet_disconnect("Protocol error for port forward request:"
2557 "received packet type %d.", type);
2558 }
2559 }
2560 if (success) {
2561 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2562 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2563 permitted_opens[num_permitted_opens].listen_port = listen_port;
2564 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002565 }
Darren Tuckere7d4b192006-07-12 22:17:10 +10002566 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002567}
2568
Damien Miller5428f641999-11-25 11:54:57 +11002569/*
Darren Tuckerfc959702004-07-17 16:12:08 +10002570 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10002571 * local side.
2572 */
Darren Tuckere7066df2004-05-24 10:18:05 +10002573void
Damien Millerf91ee4c2005-03-01 21:24:33 +11002574channel_request_rforward_cancel(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10002575{
2576 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10002577
2578 if (!compat20)
2579 return;
2580
2581 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckerfc959702004-07-17 16:12:08 +10002582 if (permitted_opens[i].host_to_connect != NULL &&
Darren Tuckere7066df2004-05-24 10:18:05 +10002583 permitted_opens[i].listen_port == port)
2584 break;
2585 }
2586 if (i >= num_permitted_opens) {
2587 debug("%s: requested forward not found", __func__);
2588 return;
2589 }
2590 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2591 packet_put_cstring("cancel-tcpip-forward");
2592 packet_put_char(0);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002593 packet_put_cstring(host == NULL ? "" : host);
Darren Tuckere7066df2004-05-24 10:18:05 +10002594 packet_put_int(port);
2595 packet_send();
2596
2597 permitted_opens[i].listen_port = 0;
2598 permitted_opens[i].port_to_connect = 0;
Damien Miller0a0176e2005-11-05 15:07:59 +11002599 xfree(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10002600 permitted_opens[i].host_to_connect = NULL;
2601}
2602
2603/*
Damien Miller5428f641999-11-25 11:54:57 +11002604 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2605 * listening for the port, and sends back a success reply (or disconnect
Darren Tuckere7d4b192006-07-12 22:17:10 +10002606 * message if there was an error).
Damien Miller5428f641999-11-25 11:54:57 +11002607 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002608int
Damien Millere247cc42000-05-07 12:03:14 +10002609channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002610{
Damien Milleraae6c611999-12-06 11:47:28 +11002611 u_short port, host_port;
Darren Tuckere7d4b192006-07-12 22:17:10 +10002612 int success = 0;
Damien Miller95def091999-11-25 00:26:21 +11002613 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002614
Damien Miller95def091999-11-25 00:26:21 +11002615 /* Get arguments from the packet. */
2616 port = packet_get_int();
2617 hostname = packet_get_string(NULL);
2618 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002619
Damien Millerbac2d8a2000-09-05 16:13:06 +11002620#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002621 /*
2622 * Check that an unprivileged user is not trying to forward a
2623 * privileged port.
2624 */
Damien Miller95def091999-11-25 00:26:21 +11002625 if (port < IPPORT_RESERVED && !is_root)
Darren Tucker9189ff82003-07-03 13:52:04 +10002626 packet_disconnect(
2627 "Requested forwarding of port %d but user is not root.",
2628 port);
2629 if (host_port == 0)
2630 packet_disconnect("Dynamic forwarding denied.");
Damien Millerbac2d8a2000-09-05 16:13:06 +11002631#endif
Darren Tucker9189ff82003-07-03 13:52:04 +10002632
Damien Miller0bc1bd82000-11-13 22:57:25 +11002633 /* Initiate forwarding */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002634 success = channel_setup_local_fwd_listener(NULL, port, hostname,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002635 host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002636
2637 /* Free the argument string. */
2638 xfree(hostname);
Darren Tuckere7d4b192006-07-12 22:17:10 +10002639
2640 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002641}
2642
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002643/*
2644 * Permits opening to any host/port if permitted_opens[] is empty. This is
2645 * usually called by the server, because the user could connect to any port
2646 * anyway, and the server has no way to know but to trust the client anyway.
2647 */
2648void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002649channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002650{
2651 if (num_permitted_opens == 0)
2652 all_opens_permitted = 1;
2653}
2654
Ben Lindstroma3700052001-04-05 23:26:32 +00002655void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002656channel_add_permitted_opens(char *host, int port)
2657{
2658 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
Darren Tuckere7d4b192006-07-12 22:17:10 +10002659 fatal("channel_add_permitted_opens: too many forwards");
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002660 debug("allow port forwarding to host %s port %d", host, port);
2661
2662 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2663 permitted_opens[num_permitted_opens].port_to_connect = port;
2664 num_permitted_opens++;
2665
2666 all_opens_permitted = 0;
2667}
2668
Damien Millera765cf42006-07-24 14:08:13 +10002669int
Damien Miller9b439df2006-07-24 14:04:00 +10002670channel_add_adm_permitted_opens(char *host, int port)
2671{
2672 if (num_adm_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2673 fatal("channel_add_adm_permitted_opens: too many forwards");
Damien Millera765cf42006-07-24 14:08:13 +10002674 debug("config allows port forwarding to host %s port %d", host, port);
Damien Miller9b439df2006-07-24 14:04:00 +10002675
2676 permitted_adm_opens[num_adm_permitted_opens].host_to_connect
2677 = xstrdup(host);
2678 permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
Damien Millera765cf42006-07-24 14:08:13 +10002679 return ++num_adm_permitted_opens;
Damien Miller9b439df2006-07-24 14:04:00 +10002680}
2681
2682void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002683channel_clear_permitted_opens(void)
2684{
2685 int i;
2686
2687 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002688 if (permitted_opens[i].host_to_connect != NULL)
2689 xfree(permitted_opens[i].host_to_connect);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002690 num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10002691}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002692
Damien Miller9b439df2006-07-24 14:04:00 +10002693void
2694channel_clear_adm_permitted_opens(void)
2695{
2696 int i;
2697
2698 for (i = 0; i < num_adm_permitted_opens; i++)
2699 if (permitted_adm_opens[i].host_to_connect != NULL)
2700 xfree(permitted_adm_opens[i].host_to_connect);
2701 num_adm_permitted_opens = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002702}
2703
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002704/* return socket to remote host, port */
Ben Lindstrombba81212001-06-25 05:01:22 +00002705static int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002706connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002707{
Damien Miller34132e52000-01-14 15:45:46 +11002708 struct addrinfo hints, *ai, *aitop;
2709 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2710 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002711 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002712
Damien Miller34132e52000-01-14 15:45:46 +11002713 memset(&hints, 0, sizeof(hints));
2714 hints.ai_family = IPv4or6;
2715 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002716 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002717 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002718 error("connect_to %.100s: unknown host (%s)", host,
2719 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002720 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002721 }
Damien Miller34132e52000-01-14 15:45:46 +11002722 for (ai = aitop; ai; ai = ai->ai_next) {
2723 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2724 continue;
2725 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2726 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002727 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002728 continue;
2729 }
Damien Miller2372ace2003-05-14 13:42:23 +10002730 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002731 if (sock < 0) {
Damien Millerb46b9f32003-01-10 21:45:12 +11002732 if (ai->ai_next == NULL)
2733 error("socket: %.100s", strerror(errno));
2734 else
2735 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002736 continue;
2737 }
Damien Miller232711f2004-06-15 10:35:30 +10002738 if (set_nonblock(sock) == -1)
2739 fatal("%s: set_nonblock(%d)", __func__, sock);
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002740 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2741 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002742 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002743 strerror(errno));
2744 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002745 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002746 }
2747 break; /* success */
2748
2749 }
2750 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002751 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002752 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002753 return -1;
2754 }
2755 /* success */
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00002756 set_nodelay(sock);
Damien Millerb38eff82000-04-01 11:09:21 +10002757 return sock;
2758}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002759
Damien Miller0bc1bd82000-11-13 22:57:25 +11002760int
Ben Lindstrom173e6462001-07-04 05:15:15 +00002761channel_connect_by_listen_address(u_short listen_port)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002762{
2763 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002764
Damien Miller0bc1bd82000-11-13 22:57:25 +11002765 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002766 if (permitted_opens[i].host_to_connect != NULL &&
2767 permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002768 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002769 permitted_opens[i].host_to_connect,
2770 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002771 error("WARNING: Server requests forwarding for unknown listen_port %d",
2772 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002773 return -1;
2774}
Damien Millerb38eff82000-04-01 11:09:21 +10002775
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002776/* Check if connecting to that port is permitted and connect. */
2777int
2778channel_connect_to(const char *host, u_short port)
2779{
Damien Miller9b439df2006-07-24 14:04:00 +10002780 int i, permit, permit_adm = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002781
2782 permit = all_opens_permitted;
2783 if (!permit) {
2784 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002785 if (permitted_opens[i].host_to_connect != NULL &&
2786 permitted_opens[i].port_to_connect == port &&
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002787 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2788 permit = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002789 }
Damien Miller9b439df2006-07-24 14:04:00 +10002790
2791 if (num_adm_permitted_opens > 0) {
2792 permit_adm = 0;
2793 for (i = 0; i < num_adm_permitted_opens; i++)
2794 if (permitted_adm_opens[i].host_to_connect != NULL &&
2795 permitted_adm_opens[i].port_to_connect == port &&
2796 strcmp(permitted_adm_opens[i].host_to_connect, host)
2797 == 0)
2798 permit_adm = 1;
2799 }
2800
2801 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10002802 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002803 "but the request was denied.", host, port);
2804 return -1;
2805 }
2806 return connect_to(host, port);
2807}
2808
Damien Miller0e220db2004-06-15 10:34:08 +10002809void
2810channel_send_window_changes(void)
2811{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002812 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10002813 struct winsize ws;
2814
2815 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11002816 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10002817 channels[i]->type != SSH_CHANNEL_OPEN)
2818 continue;
2819 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
2820 continue;
2821 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11002822 packet_put_int((u_int)ws.ws_col);
2823 packet_put_int((u_int)ws.ws_row);
2824 packet_put_int((u_int)ws.ws_xpixel);
2825 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10002826 packet_send();
2827 }
2828}
2829
Ben Lindstrome9c99912001-06-09 00:41:05 +00002830/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002831
Damien Miller5428f641999-11-25 11:54:57 +11002832/*
2833 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002834 * Returns 0 and a suitable display number for the DISPLAY variable
2835 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11002836 */
Kevin Steves366298c2001-12-19 17:58:01 +00002837int
Damien Miller95c249f2002-02-05 12:11:34 +11002838x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10002839 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002840{
Damien Millere7378562001-12-21 14:58:35 +11002841 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11002842 int display_number, sock;
2843 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002844 struct addrinfo hints, *ai, *aitop;
2845 char strport[NI_MAXSERV];
2846 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002847
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002848 if (chanids == NULL)
2849 return -1;
2850
Damien Millera34a28b1999-12-14 10:47:15 +11002851 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002852 display_number < MAX_DISPLAYS;
2853 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11002854 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002855 memset(&hints, 0, sizeof(hints));
2856 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11002857 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11002858 hints.ai_socktype = SOCK_STREAM;
2859 snprintf(strport, sizeof strport, "%d", port);
2860 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2861 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00002862 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002863 }
Damien Miller34132e52000-01-14 15:45:46 +11002864 for (ai = aitop; ai; ai = ai->ai_next) {
2865 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2866 continue;
Damien Miller2372ace2003-05-14 13:42:23 +10002867 sock = socket(ai->ai_family, ai->ai_socktype,
2868 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002869 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002870 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002871 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10002872 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00002873 return -1;
Damien Millere2192732000-01-17 13:22:55 +11002874 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002875 debug("x11_create_display_inet: Socket family %d not supported",
2876 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002877 continue;
2878 }
Damien Miller34132e52000-01-14 15:45:46 +11002879 }
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002880#ifdef IPV6_V6ONLY
2881 if (ai->ai_family == AF_INET6) {
2882 int on = 1;
2883 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
2884 error("setsockopt IPV6_V6ONLY: %.100s", strerror(errno));
2885 }
2886#endif
Damien Miller5e7fd072005-11-05 14:53:39 +11002887 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11002888 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002889 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002890 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002891
2892 if (ai->ai_next)
2893 continue;
2894
Damien Miller34132e52000-01-14 15:45:46 +11002895 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11002896 close(socks[n]);
2897 }
2898 num_socks = 0;
2899 break;
2900 }
2901 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002902#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002903 if (num_socks == NUM_SOCKS)
2904 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002905#else
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002906 if (x11_use_localhost) {
2907 if (num_socks == NUM_SOCKS)
2908 break;
2909 } else {
2910 break;
2911 }
Damien Miller7bcb0892000-03-11 20:45:40 +11002912#endif
Damien Miller95def091999-11-25 00:26:21 +11002913 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002914 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002915 if (num_socks > 0)
2916 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002917 }
Damien Miller95def091999-11-25 00:26:21 +11002918 if (display_number >= MAX_DISPLAYS) {
2919 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00002920 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002921 }
Damien Miller95def091999-11-25 00:26:21 +11002922 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002923 for (n = 0; n < num_socks; n++) {
2924 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11002925 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002926 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002927 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00002928 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11002929 }
Damien Miller95def091999-11-25 00:26:21 +11002930 }
Damien Miller34132e52000-01-14 15:45:46 +11002931
Damien Miller34132e52000-01-14 15:45:46 +11002932 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11002933 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11002934 for (n = 0; n < num_socks; n++) {
2935 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11002936 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10002937 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2938 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002939 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11002940 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002941 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11002942 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002943 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002944
Kevin Steves366298c2001-12-19 17:58:01 +00002945 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002946 *display_numberp = display_number;
2947 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002948}
2949
Ben Lindstrombba81212001-06-25 05:01:22 +00002950static int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002951connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002952{
Damien Miller95def091999-11-25 00:26:21 +11002953 int sock;
2954 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002955
Damien Miller3afe3752001-12-21 12:39:51 +11002956 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2957 if (sock < 0)
2958 error("socket: %.100s", strerror(errno));
2959 memset(&addr, 0, sizeof(addr));
2960 addr.sun_family = AF_UNIX;
2961 snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
Damien Miller90967402006-03-26 14:07:26 +11002962 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11002963 return sock;
2964 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11002965 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2966 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002967}
2968
Damien Millerbd483e72000-04-30 10:00:53 +10002969int
2970x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002971{
Damien Miller57c4e872006-03-31 23:11:07 +11002972 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11002973 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002974 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002975 struct addrinfo hints, *ai, *aitop;
2976 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11002977 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002978
Damien Miller95def091999-11-25 00:26:21 +11002979 /* Try to open a socket for the local X server. */
2980 display = getenv("DISPLAY");
2981 if (!display) {
2982 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002983 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002984 }
Damien Miller5428f641999-11-25 11:54:57 +11002985 /*
2986 * Now we decode the value of the DISPLAY variable and make a
2987 * connection to the real X server.
2988 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002989
Damien Miller5428f641999-11-25 11:54:57 +11002990 /*
2991 * Check if it is a unix domain socket. Unix domain displays are in
2992 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2993 */
Damien Miller95def091999-11-25 00:26:21 +11002994 if (strncmp(display, "unix:", 5) == 0 ||
2995 display[0] == ':') {
2996 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11002997 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11002998 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11002999 display);
Damien Millerbd483e72000-04-30 10:00:53 +10003000 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003001 }
3002 /* Create a socket. */
3003 sock = connect_local_xsocket(display_number);
3004 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10003005 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003006
3007 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10003008 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003009 }
Damien Miller5428f641999-11-25 11:54:57 +11003010 /*
3011 * Connect to an inet socket. The DISPLAY value is supposedly
3012 * hostname:d[.s], where hostname may also be numeric IP address.
3013 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00003014 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11003015 cp = strchr(buf, ':');
3016 if (!cp) {
3017 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10003018 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003019 }
Damien Miller95def091999-11-25 00:26:21 +11003020 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11003021 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11003022 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11003023 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11003024 display);
Damien Millerbd483e72000-04-30 10:00:53 +10003025 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003026 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003027
Damien Miller34132e52000-01-14 15:45:46 +11003028 /* Look up the host address */
3029 memset(&hints, 0, sizeof(hints));
3030 hints.ai_family = IPv4or6;
3031 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11003032 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11003033 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
3034 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10003035 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003036 }
Damien Miller34132e52000-01-14 15:45:46 +11003037 for (ai = aitop; ai; ai = ai->ai_next) {
3038 /* Create a socket. */
Damien Miller2372ace2003-05-14 13:42:23 +10003039 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003040 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10003041 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10003042 continue;
3043 }
3044 /* Connect it to the display. */
3045 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11003046 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10003047 6000 + display_number, strerror(errno));
3048 close(sock);
3049 continue;
3050 }
3051 /* Success */
3052 break;
Damien Miller34132e52000-01-14 15:45:46 +11003053 }
Damien Miller34132e52000-01-14 15:45:46 +11003054 freeaddrinfo(aitop);
3055 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11003056 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11003057 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10003058 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003059 }
Damien Miller398e1cf2002-02-05 11:52:13 +11003060 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10003061 return sock;
3062}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003063
Damien Millerbd483e72000-04-30 10:00:53 +10003064/*
3065 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
3066 * the remote channel number. We should do whatever we want, and respond
3067 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
3068 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003069
Damien Miller8473dd82006-07-24 14:08:32 +10003070/* ARGSUSED */
Damien Millerbd483e72000-04-30 10:00:53 +10003071void
Damien Miller630d6f42002-01-22 23:17:30 +11003072x11_input_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10003073{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003074 Channel *c = NULL;
3075 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10003076 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10003077
3078 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003079
3080 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00003081
3082 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003083 remote_host = packet_get_string(NULL);
3084 } else {
3085 remote_host = xstrdup("unknown (remote did not supply name)");
3086 }
Damien Miller48b03fc2002-01-22 23:11:40 +11003087 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10003088
3089 /* Obtain a connection to the real X display. */
3090 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003091 if (sock != -1) {
3092 /* Allocate a channel for this connection. */
3093 c = channel_new("connected x11 socket",
3094 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
3095 remote_host, 1);
Damien Miller699d0032002-02-08 22:07:16 +11003096 c->remote_id = remote_id;
3097 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003098 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003099 xfree(remote_host);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003100 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10003101 /* Send refusal to the remote host. */
3102 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003103 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10003104 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10003105 /* Send a confirmation to the remote host. */
3106 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003107 packet_put_int(remote_id);
3108 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10003109 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003110 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003111}
3112
Damien Miller69b69aa2000-10-28 14:19:58 +11003113/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
Damien Miller8473dd82006-07-24 14:08:32 +10003114/* ARGSUSED */
Damien Miller69b69aa2000-10-28 14:19:58 +11003115void
Damien Miller630d6f42002-01-22 23:17:30 +11003116deny_input_open(int type, u_int32_t seq, void *ctxt)
Damien Miller69b69aa2000-10-28 14:19:58 +11003117{
3118 int rchan = packet_get_int();
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00003119
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00003120 switch (type) {
Damien Miller69b69aa2000-10-28 14:19:58 +11003121 case SSH_SMSG_AGENT_OPEN:
3122 error("Warning: ssh server tried agent forwarding.");
3123 break;
3124 case SSH_SMSG_X11_OPEN:
3125 error("Warning: ssh server tried X11 forwarding.");
3126 break;
3127 default:
Damien Miller630d6f42002-01-22 23:17:30 +11003128 error("deny_input_open: type %d", type);
Damien Miller69b69aa2000-10-28 14:19:58 +11003129 break;
3130 }
Damien Miller5eb137c2005-12-31 16:19:53 +11003131 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller69b69aa2000-10-28 14:19:58 +11003132 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
3133 packet_put_int(rchan);
3134 packet_send();
3135}
3136
Damien Miller5428f641999-11-25 11:54:57 +11003137/*
3138 * Requests forwarding of X11 connections, generates fake authentication
3139 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00003140 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11003141 */
Damien Miller4af51302000-04-16 11:18:38 +10003142void
Damien Miller17e7ed02005-06-17 12:54:33 +10003143x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Millerbd483e72000-04-30 10:00:53 +10003144 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003145{
Ben Lindstrom46c16222000-12-22 01:43:59 +00003146 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10003147 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11003148 char *new_data;
3149 int screen_number;
3150 const char *cp;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10003151 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003152
Damien Millerf92c0792005-07-06 09:45:26 +10003153 if (x11_saved_display == NULL)
3154 x11_saved_display = xstrdup(disp);
3155 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10003156 error("x11_request_forwarding_with_spoofing: different "
3157 "$DISPLAY already forwarded");
3158 return;
3159 }
3160
Damien Miller17e7ed02005-06-17 12:54:33 +10003161 cp = disp;
3162 if (disp)
3163 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11003164 if (cp)
3165 cp = strchr(cp, '.');
3166 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11003167 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11003168 else
3169 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003170
Damien Miller13390022005-07-06 09:44:19 +10003171 if (x11_saved_proto == NULL) {
3172 /* Save protocol name. */
3173 x11_saved_proto = xstrdup(proto);
3174 /*
Damien Miller46d38de2005-07-17 17:02:09 +10003175 * Extract real authentication data and generate fake data
Damien Miller13390022005-07-06 09:44:19 +10003176 * of the same length.
3177 */
3178 x11_saved_data = xmalloc(data_len);
3179 x11_fake_data = xmalloc(data_len);
3180 for (i = 0; i < data_len; i++) {
3181 if (sscanf(data + 2 * i, "%2x", &value) != 1)
3182 fatal("x11_request_forwarding: bad "
3183 "authentication data: %.100s", data);
3184 if (i % 4 == 0)
3185 rnd = arc4random();
3186 x11_saved_data[i] = value;
3187 x11_fake_data[i] = rnd & 0xff;
3188 rnd >>= 8;
3189 }
3190 x11_saved_data_len = data_len;
3191 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11003192 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003193
Damien Miller95def091999-11-25 00:26:21 +11003194 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10003195 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003196
Damien Miller95def091999-11-25 00:26:21 +11003197 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10003198 if (compat20) {
3199 channel_request_start(client_session_id, "x11-req", 0);
3200 packet_put_char(0); /* XXX bool single connection */
3201 } else {
3202 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
3203 }
3204 packet_put_cstring(proto);
3205 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11003206 packet_put_int(screen_number);
3207 packet_send();
3208 packet_write_wait();
3209 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003210}
3211
Ben Lindstrome9c99912001-06-09 00:41:05 +00003212
3213/* -- agent forwarding */
3214
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003215/* Sends a message to the server to request authentication fd forwarding. */
3216
Damien Miller4af51302000-04-16 11:18:38 +10003217void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003218auth_request_forwarding(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003219{
Damien Miller95def091999-11-25 00:26:21 +11003220 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
3221 packet_send();
3222 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003223}