blob: 51718578b2992fd30aeda9755863c69998dfcc6b [file] [log] [blame]
Darren Tuckere7d4b192006-07-12 22:17:10 +10001/* $OpenBSD: channels.c,v 1.253 2006/07/11 18:50:47 markus 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
52#include <termios.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
54#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "ssh1.h"
56#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057#include "packet.h"
58#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000059#include "log.h"
60#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000063#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100064#include "key.h"
65#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110066#include "pathnames.h"
Darren Tucker46471c92003-07-03 13:55:19 +100067#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068
Ben Lindstrome9c99912001-06-09 00:41:05 +000069/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
Damien Miller5428f641999-11-25 11:54:57 +110071/*
72 * Pointer to an array containing all allocated channels. The array is
73 * dynamically extended as needed.
74 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000075static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076
Damien Miller5428f641999-11-25 11:54:57 +110077/*
78 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000079 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110080 */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +100081static u_int channels_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Miller5428f641999-11-25 11:54:57 +110083/*
84 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +000085 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +110086 */
Damien Miller5e953212001-01-30 09:14:00 +110087static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Ben Lindstrome9c99912001-06-09 00:41:05 +000090/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091
Damien Miller5428f641999-11-25 11:54:57 +110092/*
93 * Data structure for storing which hosts are permitted for forward requests.
94 * The local sides of any remote forwards are stored in this array to prevent
95 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
96 * network (which might be behind a firewall).
97 */
Damien Miller95def091999-11-25 00:26:21 +110098typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +100099 char *host_to_connect; /* Connect to 'host'. */
100 u_short port_to_connect; /* Connect to 'port'. */
101 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102} ForwardPermission;
103
104/* List of all permitted host/port pairs to connect. */
105static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000106
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107/* Number of permitted host/port pairs in the array. */
108static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +1100109/*
110 * If this is true, all opens are permitted. This is the case on the server
111 * on which we have to trust the client anyway, and the user could do
112 * anything after logging in anyway.
113 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114static int all_opens_permitted = 0;
115
Ben Lindstrome9c99912001-06-09 00:41:05 +0000116
117/* -- X11 forwarding */
118
119/* Maximum number of fake X11 displays to try. */
120#define MAX_DISPLAYS 1000
121
Damien Miller13390022005-07-06 09:44:19 +1000122/* Saved X11 local (client) display. */
123static char *x11_saved_display = NULL;
124
Ben Lindstrome9c99912001-06-09 00:41:05 +0000125/* Saved X11 authentication protocol name. */
126static char *x11_saved_proto = NULL;
127
128/* Saved X11 authentication data. This is the real data. */
129static char *x11_saved_data = NULL;
130static u_int x11_saved_data_len = 0;
131
132/*
133 * Fake X11 authentication data. This is what the server will be sending us;
134 * we should replace any occurrences of this by the real data.
135 */
Damien Miller4ae97f12006-03-26 14:08:10 +1100136static u_char *x11_fake_data = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000137static u_int x11_fake_data_len;
138
139
140/* -- agent forwarding */
141
142#define NUM_SOCKS 10
143
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000144/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000145static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000146
Ben Lindstrome9c99912001-06-09 00:41:05 +0000147/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000148static void port_open_helper(Channel *c, char *rtype);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000149
Ben Lindstrome9c99912001-06-09 00:41:05 +0000150/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000151
152Channel *
Damien Millerd47c62a2005-12-13 19:33:57 +1100153channel_by_id(int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000154{
155 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000156
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000157 if (id < 0 || (u_int)id >= channels_alloc) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100158 logit("channel_by_id: %d: bad id", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000159 return NULL;
160 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000161 c = channels[id];
162 if (c == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100163 logit("channel_by_id: %d: bad id: channel free", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000164 return NULL;
165 }
166 return c;
167}
168
Damien Miller5428f641999-11-25 11:54:57 +1100169/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100170 * Returns the channel if it is allowed to receive protocol messages.
171 * Private channels, like listening sockets, may not receive messages.
172 */
173Channel *
174channel_lookup(int id)
175{
176 Channel *c;
177
178 if ((c = channel_by_id(id)) == NULL)
179 return (NULL);
180
Damien Millerd62f2ca2006-03-26 13:57:41 +1100181 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100182 case SSH_CHANNEL_X11_OPEN:
183 case SSH_CHANNEL_LARVAL:
184 case SSH_CHANNEL_CONNECTING:
185 case SSH_CHANNEL_DYNAMIC:
186 case SSH_CHANNEL_OPENING:
187 case SSH_CHANNEL_OPEN:
188 case SSH_CHANNEL_INPUT_DRAINING:
189 case SSH_CHANNEL_OUTPUT_DRAINING:
190 return (c);
Damien Millerd47c62a2005-12-13 19:33:57 +1100191 }
192 logit("Non-public channel %d, type %d.", id, c->type);
193 return (NULL);
194}
195
196/*
Damien Millere247cc42000-05-07 12:03:14 +1000197 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000198 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100199 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000200static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100201channel_register_fds(Channel *c, int rfd, int wfd, int efd,
202 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203{
Damien Miller95def091999-11-25 00:26:21 +1100204 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100205 channel_max_fd = MAX(channel_max_fd, rfd);
206 channel_max_fd = MAX(channel_max_fd, wfd);
207 channel_max_fd = MAX(channel_max_fd, efd);
208
Damien Miller5428f641999-11-25 11:54:57 +1100209 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000210
Damien Miller6f83b8e2000-05-02 09:23:45 +1000211 c->rfd = rfd;
212 c->wfd = wfd;
213 c->sock = (rfd == wfd) ? rfd : -1;
Damien Miller0e220db2004-06-15 10:34:08 +1000214 c->ctl_fd = -1; /* XXX: set elsewhere */
Damien Miller6f83b8e2000-05-02 09:23:45 +1000215 c->efd = efd;
216 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100217
Damien Miller79438cc2001-02-16 12:34:57 +1100218 /* XXX ugly hack: nonblock is only set by the server */
219 if (nonblock && isatty(c->rfd)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000220 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100221 c->isatty = 1;
222 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000223 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100224 c->self, c->wfd);
225 }
226 } else {
227 c->isatty = 0;
228 }
Ben Lindstrombeb5f332002-07-22 15:28:53 +0000229 c->wfd_isatty = isatty(c->wfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100230
Damien Miller69b69aa2000-10-28 14:19:58 +1100231 /* enable nonblocking mode */
232 if (nonblock) {
233 if (rfd != -1)
234 set_nonblock(rfd);
235 if (wfd != -1)
236 set_nonblock(wfd);
237 if (efd != -1)
238 set_nonblock(efd);
239 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000240}
241
242/*
243 * Allocate a new channel object and set its type and socket. This will cause
244 * remote_name to be freed.
245 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000246Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000247channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000248 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000249{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000250 int found;
251 u_int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000252 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253
Damien Miller95def091999-11-25 00:26:21 +1100254 /* Do initial allocation if this is the first call. */
255 if (channels_alloc == 0) {
256 channels_alloc = 10;
Damien Miller07d86be2006-03-26 14:19:21 +1100257 channels = xcalloc(channels_alloc, sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100258 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000259 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100260 }
261 /* Try to find a free slot where to put the new channel. */
262 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000263 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100264 /* Found a free slot. */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000265 found = (int)i;
Damien Miller95def091999-11-25 00:26:21 +1100266 break;
267 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000268 if (found < 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100269 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100270 found = channels_alloc;
Damien Miller9403aa22002-06-26 19:14:43 +1000271 if (channels_alloc > 10000)
272 fatal("channel_new: internal error: channels_alloc %d "
273 "too big.", channels_alloc);
Damien Miller36812092006-03-26 14:22:47 +1100274 channels = xrealloc(channels, channels_alloc + 10,
275 sizeof(Channel *));
Damien Miller5efcecc2003-09-17 07:31:14 +1000276 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100277 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100278 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000279 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100280 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000281 /* Initialize and return new channel. */
Damien Miller07d86be2006-03-26 14:19:21 +1100282 c = channels[found] = xcalloc(1, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100283 buffer_init(&c->input);
284 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000285 buffer_init(&c->extended);
Damien Miller5144df92002-01-22 23:28:45 +1100286 c->ostate = CHAN_OUTPUT_OPEN;
287 c->istate = CHAN_INPUT_OPEN;
288 c->flags = 0;
Damien Miller69b69aa2000-10-28 14:19:58 +1100289 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100290 c->self = found;
291 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000292 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000293 c->local_window = window;
294 c->local_window_max = window;
295 c->local_consumed = 0;
296 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100297 c->remote_id = -1;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000298 c->remote_name = xstrdup(remote_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000299 c->remote_window = 0;
300 c->remote_maxpacket = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000301 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100302 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000303 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100304 c->detach_close = 0;
Damien Miller67f0bc02002-02-05 12:23:08 +1100305 c->confirm = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +1000306 c->confirm_ctx = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000307 c->input_filter = NULL;
Damien Miller077b2382005-12-31 16:22:32 +1100308 c->output_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100309 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000310 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000312
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000313static int
314channel_find_maxfd(void)
315{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000316 u_int i;
317 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000318 Channel *c;
319
320 for (i = 0; i < channels_alloc; i++) {
321 c = channels[i];
322 if (c != NULL) {
323 max = MAX(max, c->rfd);
324 max = MAX(max, c->wfd);
325 max = MAX(max, c->efd);
326 }
327 }
328 return max;
329}
330
331int
332channel_close_fd(int *fdp)
333{
334 int ret = 0, fd = *fdp;
335
336 if (fd != -1) {
337 ret = close(fd);
338 *fdp = -1;
339 if (fd == channel_max_fd)
340 channel_max_fd = channel_find_maxfd();
341 }
342 return ret;
343}
344
Damien Miller6f83b8e2000-05-02 09:23:45 +1000345/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000346static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000347channel_close_fds(Channel *c)
348{
Damien Miller0e220db2004-06-15 10:34:08 +1000349 debug3("channel %d: close_fds r %d w %d e %d c %d",
350 c->self, c->rfd, c->wfd, c->efd, c->ctl_fd);
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000351
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000352 channel_close_fd(&c->sock);
Damien Miller0e220db2004-06-15 10:34:08 +1000353 channel_close_fd(&c->ctl_fd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000354 channel_close_fd(&c->rfd);
355 channel_close_fd(&c->wfd);
356 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000357}
358
359/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000360void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000361channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000363 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000364 u_int i, n;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000365
366 for (n = 0, i = 0; i < channels_alloc; i++)
367 if (channels[i])
368 n++;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000369 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000370 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000371
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000372 s = channel_open_message();
Damien Millerfbdeece2003-09-02 22:52:31 +1000373 debug3("channel %d: status: %s", c->self, s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000374 xfree(s);
375
Damien Miller6f83b8e2000-05-02 09:23:45 +1000376 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000377 shutdown(c->sock, SHUT_RDWR);
Damien Miller0e220db2004-06-15 10:34:08 +1000378 if (c->ctl_fd != -1)
379 shutdown(c->ctl_fd, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000380 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000381 buffer_free(&c->input);
382 buffer_free(&c->output);
383 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000384 if (c->remote_name) {
385 xfree(c->remote_name);
386 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100387 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000388 channels[c->self] = NULL;
389 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000390}
391
Ben Lindstrome9c99912001-06-09 00:41:05 +0000392void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000393channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000394{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000395 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000396
Ben Lindstrom601e4362001-06-21 03:19:23 +0000397 for (i = 0; i < channels_alloc; i++)
398 if (channels[i] != NULL)
399 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000400}
401
402/*
403 * Closes the sockets/fds of all channels. This is used to close extra file
404 * descriptors after a fork.
405 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000406void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000407channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000408{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000409 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000410
411 for (i = 0; i < channels_alloc; i++)
412 if (channels[i] != NULL)
413 channel_close_fds(channels[i]);
414}
415
416/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000417 * Stop listening to channels.
418 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000419void
420channel_stop_listening(void)
421{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000422 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000423 Channel *c;
424
425 for (i = 0; i < channels_alloc; i++) {
426 c = channels[i];
427 if (c != NULL) {
428 switch (c->type) {
429 case SSH_CHANNEL_AUTH_SOCKET:
430 case SSH_CHANNEL_PORT_LISTENER:
431 case SSH_CHANNEL_RPORT_LISTENER:
432 case SSH_CHANNEL_X11_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000433 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000434 channel_free(c);
435 break;
436 }
437 }
438 }
439}
440
441/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000442 * Returns true if no channel has too much buffered data, and false if one or
443 * more channel is overfull.
444 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000445int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000446channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000447{
448 u_int i;
449 Channel *c;
450
451 for (i = 0; i < channels_alloc; i++) {
452 c = channels[i];
453 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000454#if 0
455 if (!compat20 &&
456 buffer_len(&c->input) > packet_get_maxsize()) {
Damien Miller275295e2003-01-08 14:04:09 +1100457 debug2("channel %d: big input buffer %d",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000458 c->self, buffer_len(&c->input));
459 return 0;
460 }
Damien Milleraf5f2e62001-10-10 15:01:16 +1000461#endif
Ben Lindstrome9c99912001-06-09 00:41:05 +0000462 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000463 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000464 c->self, buffer_len(&c->output),
465 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000466 return 0;
467 }
468 }
469 }
470 return 1;
471}
472
473/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000474int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000475channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000476{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000477 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000478 Channel *c;
479
480 for (i = 0; i < channels_alloc; i++) {
481 c = channels[i];
482 if (c == NULL)
483 continue;
484 switch (c->type) {
485 case SSH_CHANNEL_X11_LISTENER:
486 case SSH_CHANNEL_PORT_LISTENER:
487 case SSH_CHANNEL_RPORT_LISTENER:
488 case SSH_CHANNEL_CLOSED:
489 case SSH_CHANNEL_AUTH_SOCKET:
490 case SSH_CHANNEL_DYNAMIC:
491 case SSH_CHANNEL_CONNECTING:
492 case SSH_CHANNEL_ZOMBIE:
493 continue;
494 case SSH_CHANNEL_LARVAL:
495 if (!compat20)
496 fatal("cannot happen: SSH_CHANNEL_LARVAL");
497 continue;
498 case SSH_CHANNEL_OPENING:
499 case SSH_CHANNEL_OPEN:
500 case SSH_CHANNEL_X11_OPEN:
501 return 1;
502 case SSH_CHANNEL_INPUT_DRAINING:
503 case SSH_CHANNEL_OUTPUT_DRAINING:
504 if (!compat13)
505 fatal("cannot happen: OUT_DRAIN");
506 return 1;
507 default:
508 fatal("channel_still_open: bad channel type %d", c->type);
509 /* NOTREACHED */
510 }
511 }
512 return 0;
513}
514
515/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000516int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000517channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000518{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000519 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000520 Channel *c;
521
522 for (i = 0; i < channels_alloc; i++) {
523 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000524 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000525 continue;
526 switch (c->type) {
527 case SSH_CHANNEL_CLOSED:
528 case SSH_CHANNEL_DYNAMIC:
529 case SSH_CHANNEL_X11_LISTENER:
530 case SSH_CHANNEL_PORT_LISTENER:
531 case SSH_CHANNEL_RPORT_LISTENER:
532 case SSH_CHANNEL_OPENING:
533 case SSH_CHANNEL_CONNECTING:
534 case SSH_CHANNEL_ZOMBIE:
535 continue;
536 case SSH_CHANNEL_LARVAL:
537 case SSH_CHANNEL_AUTH_SOCKET:
538 case SSH_CHANNEL_OPEN:
539 case SSH_CHANNEL_X11_OPEN:
540 return i;
541 case SSH_CHANNEL_INPUT_DRAINING:
542 case SSH_CHANNEL_OUTPUT_DRAINING:
543 if (!compat13)
544 fatal("cannot happen: OUT_DRAIN");
545 return i;
546 default:
547 fatal("channel_find_open: bad channel type %d", c->type);
548 /* NOTREACHED */
549 }
550 }
551 return -1;
552}
553
554
555/*
556 * Returns a message describing the currently open forwarded connections,
557 * suitable for sending to the client. The message contains crlf pairs for
558 * newlines.
559 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000560char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000561channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000562{
563 Buffer buffer;
564 Channel *c;
565 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000566 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000567
568 buffer_init(&buffer);
569 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
570 buffer_append(&buffer, buf, strlen(buf));
571 for (i = 0; i < channels_alloc; i++) {
572 c = channels[i];
573 if (c == NULL)
574 continue;
575 switch (c->type) {
576 case SSH_CHANNEL_X11_LISTENER:
577 case SSH_CHANNEL_PORT_LISTENER:
578 case SSH_CHANNEL_RPORT_LISTENER:
579 case SSH_CHANNEL_CLOSED:
580 case SSH_CHANNEL_AUTH_SOCKET:
581 case SSH_CHANNEL_ZOMBIE:
582 continue;
583 case SSH_CHANNEL_LARVAL:
584 case SSH_CHANNEL_OPENING:
585 case SSH_CHANNEL_CONNECTING:
586 case SSH_CHANNEL_DYNAMIC:
587 case SSH_CHANNEL_OPEN:
588 case SSH_CHANNEL_X11_OPEN:
589 case SSH_CHANNEL_INPUT_DRAINING:
590 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Miller0e220db2004-06-15 10:34:08 +1000591 snprintf(buf, sizeof buf,
592 " #%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 +0000593 c->self, c->remote_name,
594 c->type, c->remote_id,
595 c->istate, buffer_len(&c->input),
596 c->ostate, buffer_len(&c->output),
Damien Miller0e220db2004-06-15 10:34:08 +1000597 c->rfd, c->wfd, c->ctl_fd);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000598 buffer_append(&buffer, buf, strlen(buf));
599 continue;
600 default:
601 fatal("channel_open_message: bad channel type %d", c->type);
602 /* NOTREACHED */
603 }
604 }
605 buffer_append(&buffer, "\0", 1);
606 cp = xstrdup(buffer_ptr(&buffer));
607 buffer_free(&buffer);
608 return cp;
609}
610
611void
612channel_send_open(int id)
613{
614 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000615
Ben Lindstrome9c99912001-06-09 00:41:05 +0000616 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000617 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000618 return;
619 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000620 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000621 packet_start(SSH2_MSG_CHANNEL_OPEN);
622 packet_put_cstring(c->ctype);
623 packet_put_int(c->self);
624 packet_put_int(c->local_window);
625 packet_put_int(c->local_maxpacket);
626 packet_send();
627}
628
629void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000630channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000631{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000632 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000633
Ben Lindstrome9c99912001-06-09 00:41:05 +0000634 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000635 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000636 return;
637 }
Damien Miller0e220db2004-06-15 10:34:08 +1000638 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000639 packet_start(SSH2_MSG_CHANNEL_REQUEST);
640 packet_put_int(c->remote_id);
641 packet_put_cstring(service);
642 packet_put_char(wantconfirm);
643}
Damien Miller4f7becb2006-03-26 14:10:14 +1100644
Ben Lindstrome9c99912001-06-09 00:41:05 +0000645void
Damien Miller0e220db2004-06-15 10:34:08 +1000646channel_register_confirm(int id, channel_callback_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000647{
648 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000649
Ben Lindstrome9c99912001-06-09 00:41:05 +0000650 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000651 logit("channel_register_comfirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000652 return;
653 }
Damien Miller67f0bc02002-02-05 12:23:08 +1100654 c->confirm = fn;
Damien Miller0e220db2004-06-15 10:34:08 +1000655 c->confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000656}
Damien Miller4f7becb2006-03-26 14:10:14 +1100657
Ben Lindstrome9c99912001-06-09 00:41:05 +0000658void
Damien Miller39eda6e2005-11-05 14:52:50 +1100659channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000660{
Damien Millerd47c62a2005-12-13 19:33:57 +1100661 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000662
Ben Lindstrome9c99912001-06-09 00:41:05 +0000663 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000664 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000665 return;
666 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000667 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100668 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000669}
Damien Miller4f7becb2006-03-26 14:10:14 +1100670
Ben Lindstrome9c99912001-06-09 00:41:05 +0000671void
672channel_cancel_cleanup(int id)
673{
Damien Millerd47c62a2005-12-13 19:33:57 +1100674 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000675
Ben Lindstrome9c99912001-06-09 00:41:05 +0000676 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000677 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000678 return;
679 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000680 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100681 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000682}
Damien Miller4f7becb2006-03-26 14:10:14 +1100683
Ben Lindstrome9c99912001-06-09 00:41:05 +0000684void
Damien Miller077b2382005-12-31 16:22:32 +1100685channel_register_filter(int id, channel_infilter_fn *ifn,
686 channel_outfilter_fn *ofn)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000687{
688 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000689
Ben Lindstrome9c99912001-06-09 00:41:05 +0000690 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000691 logit("channel_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000692 return;
693 }
Damien Miller077b2382005-12-31 16:22:32 +1100694 c->input_filter = ifn;
695 c->output_filter = ofn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000696}
697
698void
699channel_set_fds(int id, int rfd, int wfd, int efd,
Damien Miller2aa0c192002-02-19 15:20:08 +1100700 int extusage, int nonblock, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000701{
702 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000703
Ben Lindstrome9c99912001-06-09 00:41:05 +0000704 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
705 fatal("channel_activate for non-larval channel %d.", id);
706 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
707 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100708 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000709 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
710 packet_put_int(c->remote_id);
711 packet_put_int(c->local_window);
712 packet_send();
713}
714
Damien Miller5428f641999-11-25 11:54:57 +1100715/*
Damien Millerb38eff82000-04-01 11:09:21 +1000716 * 'channel_pre*' are called just before select() to add any bits relevant to
717 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100718 */
Damien Millerb38eff82000-04-01 11:09:21 +1000719/*
720 * 'channel_post*': perform any appropriate operations for channels which
721 * have events pending.
722 */
Damien Millerd62f2ca2006-03-26 13:57:41 +1100723typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000724chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
725chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
726
Ben Lindstrombba81212001-06-25 05:01:22 +0000727static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100728channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000729{
730 FD_SET(c->sock, readset);
731}
732
Ben Lindstrombba81212001-06-25 05:01:22 +0000733static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100734channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000735{
736 debug3("channel %d: waiting for connection", c->self);
737 FD_SET(c->sock, writeset);
738}
739
Ben Lindstrombba81212001-06-25 05:01:22 +0000740static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100741channel_pre_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000742{
743 if (buffer_len(&c->input) < packet_get_maxsize())
744 FD_SET(c->sock, readset);
745 if (buffer_len(&c->output) > 0)
746 FD_SET(c->sock, writeset);
747}
748
Ben Lindstrombba81212001-06-25 05:01:22 +0000749static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100750channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000751{
Damien Millerde6987c2002-01-22 23:20:40 +1100752 u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
Damien Millerb38eff82000-04-01 11:09:21 +1000753
Damien Miller33b13562000-04-04 14:38:59 +1000754 if (c->istate == CHAN_INPUT_OPEN &&
Damien Millerde6987c2002-01-22 23:20:40 +1100755 limit > 0 &&
Damien Miller499a0d52006-04-23 12:06:03 +1000756 buffer_len(&c->input) < limit &&
757 buffer_check_alloc(&c->input, CHAN_RBUF))
Damien Miller33b13562000-04-04 14:38:59 +1000758 FD_SET(c->rfd, readset);
759 if (c->ostate == CHAN_OUTPUT_OPEN ||
760 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
761 if (buffer_len(&c->output) > 0) {
762 FD_SET(c->wfd, writeset);
763 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000764 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +1000765 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
766 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000767 else
768 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000769 }
770 }
771 /** XXX check close conditions, too */
Damien Millerde6987c2002-01-22 23:20:40 +1100772 if (compat20 && c->efd != -1) {
Damien Miller33b13562000-04-04 14:38:59 +1000773 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
774 buffer_len(&c->extended) > 0)
775 FD_SET(c->efd, writeset);
Ben Lindstromcf159442002-03-26 03:26:24 +0000776 else if (!(c->flags & CHAN_EOF_SENT) &&
777 c->extended_usage == CHAN_EXTENDED_READ &&
Damien Miller33b13562000-04-04 14:38:59 +1000778 buffer_len(&c->extended) < c->remote_window)
779 FD_SET(c->efd, readset);
780 }
Damien Miller0e220db2004-06-15 10:34:08 +1000781 /* XXX: What about efd? races? */
Darren Tuckerfc959702004-07-17 16:12:08 +1000782 if (compat20 && c->ctl_fd != -1 &&
Damien Miller0e220db2004-06-15 10:34:08 +1000783 c->istate == CHAN_INPUT_OPEN && c->ostate == CHAN_OUTPUT_OPEN)
784 FD_SET(c->ctl_fd, readset);
Damien Miller33b13562000-04-04 14:38:59 +1000785}
786
Ben Lindstrombba81212001-06-25 05:01:22 +0000787static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100788channel_pre_input_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000789{
790 if (buffer_len(&c->input) == 0) {
791 packet_start(SSH_MSG_CHANNEL_CLOSE);
792 packet_put_int(c->remote_id);
793 packet_send();
794 c->type = SSH_CHANNEL_CLOSED;
Damien Millerfbdeece2003-09-02 22:52:31 +1000795 debug2("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000796 }
797}
798
Ben Lindstrombba81212001-06-25 05:01:22 +0000799static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100800channel_pre_output_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000801{
802 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000803 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000804 else
Damien Millerb38eff82000-04-01 11:09:21 +1000805 FD_SET(c->sock, writeset);
806}
807
808/*
809 * This is a special state for X11 authentication spoofing. An opened X11
810 * connection (when authentication spoofing is being done) remains in this
811 * state until the first packet has been completely read. The authentication
812 * data in that packet is then substituted by the real data if it matches the
813 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000814 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000815 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000816 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000817static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000818x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000819{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000820 u_char *ucp;
821 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000822
823 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000824 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000825 return 0;
826
827 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100828 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000829 if (ucp[0] == 0x42) { /* Byte order MSB first. */
830 proto_len = 256 * ucp[6] + ucp[7];
831 data_len = 256 * ucp[8] + ucp[9];
832 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
833 proto_len = ucp[6] + 256 * ucp[7];
834 data_len = ucp[8] + 256 * ucp[9];
835 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +1000836 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100837 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000838 return -1;
839 }
840
841 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000842 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000843 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
844 return 0;
845
846 /* Check if authentication protocol matches. */
847 if (proto_len != strlen(x11_saved_proto) ||
848 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000849 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +1000850 return -1;
851 }
852 /* Check if authentication data matches our fake data. */
853 if (data_len != x11_fake_data_len ||
854 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
855 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000856 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +1000857 return -1;
858 }
859 /* Check fake data length */
860 if (x11_fake_data_len != x11_saved_data_len) {
861 error("X11 fake_data_len %d != saved_data_len %d",
862 x11_fake_data_len, x11_saved_data_len);
863 return -1;
864 }
865 /*
866 * Received authentication protocol and data match
867 * our fake data. Substitute the fake data with real
868 * data.
869 */
870 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
871 x11_saved_data, x11_saved_data_len);
872 return 1;
873}
874
Ben Lindstrombba81212001-06-25 05:01:22 +0000875static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100876channel_pre_x11_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000877{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000878 int ret = x11_open_helper(&c->output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000879
Damien Millerb38eff82000-04-01 11:09:21 +1000880 if (ret == 1) {
881 /* Start normal processing for the channel. */
882 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000883 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000884 } else if (ret == -1) {
885 /*
886 * We have received an X11 connection that has bad
887 * authentication information.
888 */
Damien Miller996acd22003-04-09 20:59:48 +1000889 logit("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000890 buffer_clear(&c->input);
891 buffer_clear(&c->output);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000892 channel_close_fd(&c->sock);
Damien Millerb38eff82000-04-01 11:09:21 +1000893 c->sock = -1;
894 c->type = SSH_CHANNEL_CLOSED;
895 packet_start(SSH_MSG_CHANNEL_CLOSE);
896 packet_put_int(c->remote_id);
897 packet_send();
898 }
899}
900
Ben Lindstrombba81212001-06-25 05:01:22 +0000901static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100902channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000903{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000904 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000905
906 /* c->force_drain = 1; */
907
Damien Millerb38eff82000-04-01 11:09:21 +1000908 if (ret == 1) {
909 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +1100910 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000911 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +1000912 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +1000913 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +1100914 chan_read_failed(c);
915 buffer_clear(&c->input);
916 chan_ibuf_empty(c);
917 buffer_clear(&c->output);
918 /* for proto v1, the peer will send an IEOF */
919 if (compat20)
920 chan_write_failed(c);
921 else
922 c->type = SSH_CHANNEL_OPEN;
Damien Millerfbdeece2003-09-02 22:52:31 +1000923 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +1000924 }
925}
926
Ben Lindstromb3921512001-04-11 15:57:50 +0000927/* try to decode a socks4 header */
Ben Lindstrombba81212001-06-25 05:01:22 +0000928static int
Damien Millerd62f2ca2006-03-26 13:57:41 +1100929channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000930{
Damien Millera10f5612002-09-12 09:49:15 +1000931 char *p, *host;
Damien Millereccb9de2005-06-17 12:59:34 +1000932 u_int len, have, i, found;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100933 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000934 struct {
935 u_int8_t version;
936 u_int8_t command;
937 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000938 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000939 } s4_req, s4_rsp;
940
Ben Lindstromb3921512001-04-11 15:57:50 +0000941 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000942
943 have = buffer_len(&c->input);
944 len = sizeof(s4_req);
945 if (have < len)
946 return 0;
947 p = buffer_ptr(&c->input);
948 for (found = 0, i = len; i < have; i++) {
949 if (p[i] == '\0') {
950 found = 1;
951 break;
952 }
953 if (i > 1024) {
954 /* the peer is probably sending garbage */
955 debug("channel %d: decode socks4: too long",
956 c->self);
957 return -1;
958 }
959 }
960 if (!found)
961 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000962 buffer_get(&c->input, (char *)&s4_req.version, 1);
963 buffer_get(&c->input, (char *)&s4_req.command, 1);
964 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000965 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000966 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000967 p = buffer_ptr(&c->input);
968 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000969 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000970 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000971 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000972 c->self, len, have);
973 strlcpy(username, p, sizeof(username));
974 buffer_consume(&c->input, len);
975 buffer_consume(&c->input, 1); /* trailing '\0' */
976
Ben Lindstromb3921512001-04-11 15:57:50 +0000977 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000978 strlcpy(c->path, host, sizeof(c->path));
979 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100980
Damien Millerfbdeece2003-09-02 22:52:31 +1000981 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000982 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000983
Ben Lindstromb3921512001-04-11 15:57:50 +0000984 if (s4_req.command != 1) {
985 debug("channel %d: cannot handle: socks4 cn %d",
986 c->self, s4_req.command);
987 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000988 }
Ben Lindstromb3921512001-04-11 15:57:50 +0000989 s4_rsp.version = 0; /* vn: 0 for reply */
990 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000991 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +0000992 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Damien Millera0fdce92006-03-26 14:28:50 +1100993 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +0000994 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000995}
996
Darren Tucker46471c92003-07-03 13:55:19 +1000997/* try to decode a socks5 header */
998#define SSH_SOCKS5_AUTHDONE 0x1000
999#define SSH_SOCKS5_NOAUTH 0x00
1000#define SSH_SOCKS5_IPV4 0x01
1001#define SSH_SOCKS5_DOMAIN 0x03
1002#define SSH_SOCKS5_IPV6 0x04
1003#define SSH_SOCKS5_CONNECT 0x01
1004#define SSH_SOCKS5_SUCCESS 0x00
1005
1006static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001007channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001008{
1009 struct {
1010 u_int8_t version;
1011 u_int8_t command;
1012 u_int8_t reserved;
1013 u_int8_t atyp;
1014 } s5_req, s5_rsp;
1015 u_int16_t dest_port;
1016 u_char *p, dest_addr[255+1];
Damien Miller0f077072006-07-10 22:21:02 +10001017 u_int have, need, i, found, nmethods, addrlen, af;
Darren Tucker46471c92003-07-03 13:55:19 +10001018
1019 debug2("channel %d: decode socks5", c->self);
1020 p = buffer_ptr(&c->input);
1021 if (p[0] != 0x05)
1022 return -1;
1023 have = buffer_len(&c->input);
1024 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1025 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001026 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001027 return 0;
1028 nmethods = p[1];
1029 if (have < nmethods + 2)
1030 return 0;
1031 /* look for method: "NO AUTHENTICATION REQUIRED" */
1032 for (found = 0, i = 2 ; i < nmethods + 2; i++) {
1033 if (p[i] == SSH_SOCKS5_NOAUTH ) {
1034 found = 1;
1035 break;
1036 }
1037 }
1038 if (!found) {
1039 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1040 c->self);
1041 return -1;
1042 }
1043 buffer_consume(&c->input, nmethods + 2);
1044 buffer_put_char(&c->output, 0x05); /* version */
1045 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1046 FD_SET(c->sock, writeset);
1047 c->flags |= SSH_SOCKS5_AUTHDONE;
1048 debug2("channel %d: socks5 auth done", c->self);
1049 return 0; /* need more */
1050 }
1051 debug2("channel %d: socks5 post auth", c->self);
1052 if (have < sizeof(s5_req)+1)
1053 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001054 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001055 if (s5_req.version != 0x05 ||
1056 s5_req.command != SSH_SOCKS5_CONNECT ||
1057 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001058 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001059 return -1;
1060 }
Darren Tucker47eede72005-03-14 23:08:12 +11001061 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001062 case SSH_SOCKS5_IPV4:
1063 addrlen = 4;
1064 af = AF_INET;
1065 break;
1066 case SSH_SOCKS5_DOMAIN:
1067 addrlen = p[sizeof(s5_req)];
1068 af = -1;
1069 break;
1070 case SSH_SOCKS5_IPV6:
1071 addrlen = 16;
1072 af = AF_INET6;
1073 break;
1074 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001075 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001076 return -1;
1077 }
Damien Miller0f077072006-07-10 22:21:02 +10001078 need = sizeof(s5_req) + addrlen + 2;
1079 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1080 need++;
1081 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001082 return 0;
1083 buffer_consume(&c->input, sizeof(s5_req));
1084 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1085 buffer_consume(&c->input, 1); /* host string length */
1086 buffer_get(&c->input, (char *)&dest_addr, addrlen);
1087 buffer_get(&c->input, (char *)&dest_port, 2);
1088 dest_addr[addrlen] = '\0';
1089 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
Darren Tucker1f8311c2004-05-13 16:39:33 +10001090 strlcpy(c->path, (char *)dest_addr, sizeof(c->path));
Darren Tucker46471c92003-07-03 13:55:19 +10001091 else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL)
1092 return -1;
1093 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001094
Damien Millerfbdeece2003-09-02 22:52:31 +10001095 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001096 c->self, c->path, c->host_port, s5_req.command);
1097
1098 s5_rsp.version = 0x05;
1099 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1100 s5_rsp.reserved = 0; /* ignored */
1101 s5_rsp.atyp = SSH_SOCKS5_IPV4;
1102 ((struct in_addr *)&dest_addr)->s_addr = INADDR_ANY;
1103 dest_port = 0; /* ignored */
1104
Damien Millera0fdce92006-03-26 14:28:50 +11001105 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
1106 buffer_append(&c->output, &dest_addr, sizeof(struct in_addr));
1107 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001108 return 1;
1109}
1110
Ben Lindstromb3921512001-04-11 15:57:50 +00001111/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001112static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001113channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001114{
1115 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001116 u_int have;
1117 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001118
1119 have = buffer_len(&c->input);
Damien Miller4623a752001-10-10 15:03:58 +10001120 c->delayed = 0;
Ben Lindstromb3921512001-04-11 15:57:50 +00001121 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001122 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001123 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001124 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001125 /* need more */
1126 FD_SET(c->sock, readset);
1127 return;
1128 }
1129 /* try to guess the protocol */
1130 p = buffer_ptr(&c->input);
1131 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001132 case 0x04:
1133 ret = channel_decode_socks4(c, readset, writeset);
1134 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001135 case 0x05:
1136 ret = channel_decode_socks5(c, readset, writeset);
1137 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001138 default:
1139 ret = -1;
1140 break;
1141 }
1142 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001143 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001144 } else if (ret == 0) {
1145 debug2("channel %d: pre_dynamic: need more", c->self);
1146 /* need more */
1147 FD_SET(c->sock, readset);
1148 } else {
1149 /* switch to the next state */
1150 c->type = SSH_CHANNEL_OPENING;
1151 port_open_helper(c, "direct-tcpip");
1152 }
1153}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001154
Damien Millerb38eff82000-04-01 11:09:21 +10001155/* This is our fake X11 server socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +00001156static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001157channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001158{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001159 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001160 struct sockaddr addr;
Damien Miller398e1cf2002-02-05 11:52:13 +11001161 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001162 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001163 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001164 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001165
1166 if (FD_ISSET(c->sock, readset)) {
1167 debug("X11 connection requested.");
1168 addrlen = sizeof(addr);
1169 newsock = accept(c->sock, &addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001170 if (c->single_connection) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001171 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001172 channel_close_fd(&c->sock);
1173 chan_mark_dead(c);
1174 }
Damien Millerb38eff82000-04-01 11:09:21 +10001175 if (newsock < 0) {
1176 error("accept: %.100s", strerror(errno));
1177 return;
1178 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001179 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001180 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001181 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001182 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001183 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001184
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001185 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001186 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001187 c->local_window_max, c->local_maxpacket, 0, buf, 1);
Damien Millerbd483e72000-04-30 10:00:53 +10001188 if (compat20) {
1189 packet_start(SSH2_MSG_CHANNEL_OPEN);
1190 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001191 packet_put_int(nc->self);
Damien Millere7378562001-12-21 14:58:35 +11001192 packet_put_int(nc->local_window_max);
1193 packet_put_int(nc->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +11001194 /* originator ipaddr and port */
1195 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +10001196 if (datafellows & SSH_BUG_X11FWD) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001197 debug2("ssh2 x11 bug compat mode");
Damien Miller30c3d422000-05-09 11:02:59 +10001198 } else {
1199 packet_put_int(remote_port);
1200 }
Damien Millerbd483e72000-04-30 10:00:53 +10001201 packet_send();
1202 } else {
1203 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001204 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001205 if (packet_get_protocol_flags() &
1206 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +00001207 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +10001208 packet_send();
1209 }
Damien Millerd83ff352001-01-30 09:19:34 +11001210 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001211 }
1212}
1213
Ben Lindstrombba81212001-06-25 05:01:22 +00001214static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001215port_open_helper(Channel *c, char *rtype)
1216{
1217 int direct;
1218 char buf[1024];
1219 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001220 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001221
1222 direct = (strcmp(rtype, "direct-tcpip") == 0);
1223
1224 snprintf(buf, sizeof buf,
1225 "%s: listening port %d for %.100s port %d, "
1226 "connect from %.200s port %d",
1227 rtype, c->listening_port, c->path, c->host_port,
1228 remote_ipaddr, remote_port);
1229
1230 xfree(c->remote_name);
1231 c->remote_name = xstrdup(buf);
1232
1233 if (compat20) {
1234 packet_start(SSH2_MSG_CHANNEL_OPEN);
1235 packet_put_cstring(rtype);
1236 packet_put_int(c->self);
1237 packet_put_int(c->local_window_max);
1238 packet_put_int(c->local_maxpacket);
1239 if (direct) {
1240 /* target host, port */
1241 packet_put_cstring(c->path);
1242 packet_put_int(c->host_port);
1243 } else {
1244 /* listen address, port */
1245 packet_put_cstring(c->path);
1246 packet_put_int(c->listening_port);
1247 }
1248 /* originator host and port */
1249 packet_put_cstring(remote_ipaddr);
Damien Miller677257f2005-06-17 12:55:03 +10001250 packet_put_int((u_int)remote_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001251 packet_send();
1252 } else {
1253 packet_start(SSH_MSG_PORT_OPEN);
1254 packet_put_int(c->self);
1255 packet_put_cstring(c->path);
1256 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001257 if (packet_get_protocol_flags() &
1258 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001259 packet_put_cstring(c->remote_name);
1260 packet_send();
1261 }
1262 xfree(remote_ipaddr);
1263}
1264
Damien Miller5e7fd072005-11-05 14:53:39 +11001265static void
1266channel_set_reuseaddr(int fd)
1267{
1268 int on = 1;
1269
1270 /*
1271 * Set socket options.
1272 * Allow local port reuse in TIME_WAIT.
1273 */
1274 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1275 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1276}
1277
Damien Millerb38eff82000-04-01 11:09:21 +10001278/*
1279 * This socket is listening for connections to a forwarded TCP/IP port.
1280 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001281static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001282channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001283{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001284 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001285 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001286 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001287 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001288 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001289
Damien Millerb38eff82000-04-01 11:09:21 +10001290 if (FD_ISSET(c->sock, readset)) {
1291 debug("Connection to port %d forwarding "
1292 "to %.100s port %d requested.",
1293 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001294
Damien Miller4623a752001-10-10 15:03:58 +10001295 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1296 nextstate = SSH_CHANNEL_OPENING;
1297 rtype = "forwarded-tcpip";
1298 } else {
1299 if (c->host_port == 0) {
1300 nextstate = SSH_CHANNEL_DYNAMIC;
Damien Millerd3c04b92001-10-10 15:04:20 +10001301 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001302 } else {
1303 nextstate = SSH_CHANNEL_OPENING;
1304 rtype = "direct-tcpip";
1305 }
1306 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001307
Damien Millerb38eff82000-04-01 11:09:21 +10001308 addrlen = sizeof(addr);
1309 newsock = accept(c->sock, &addr, &addrlen);
1310 if (newsock < 0) {
1311 error("accept: %.100s", strerror(errno));
1312 return;
1313 }
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00001314 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001315 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1316 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001317 nc->listening_port = c->listening_port;
1318 nc->host_port = c->host_port;
1319 strlcpy(nc->path, c->path, sizeof(nc->path));
1320
Damien Miller4623a752001-10-10 15:03:58 +10001321 if (nextstate == SSH_CHANNEL_DYNAMIC) {
1322 /*
1323 * do not call the channel_post handler until
1324 * this flag has been reset by a pre-handler.
1325 * otherwise the FD_ISSET calls might overflow
1326 */
1327 nc->delayed = 1;
1328 } else {
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001329 port_open_helper(nc, rtype);
Damien Miller4623a752001-10-10 15:03:58 +10001330 }
Damien Millerb38eff82000-04-01 11:09:21 +10001331 }
1332}
1333
1334/*
1335 * This is the authentication agent socket listening for connections from
1336 * clients.
1337 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001338static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001339channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001340{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001341 Channel *nc;
1342 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001343 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001344 socklen_t addrlen;
1345
1346 if (FD_ISSET(c->sock, readset)) {
1347 addrlen = sizeof(addr);
1348 newsock = accept(c->sock, &addr, &addrlen);
1349 if (newsock < 0) {
1350 error("accept from auth socket: %.100s", strerror(errno));
1351 return;
1352 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001353 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001354 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1355 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001356 0, "accepted auth socket", 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001357 if (compat20) {
1358 packet_start(SSH2_MSG_CHANNEL_OPEN);
1359 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001360 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001361 packet_put_int(c->local_window_max);
1362 packet_put_int(c->local_maxpacket);
1363 } else {
1364 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001365 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001366 }
Damien Millerb38eff82000-04-01 11:09:21 +10001367 packet_send();
1368 }
1369}
1370
Ben Lindstrombba81212001-06-25 05:01:22 +00001371static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001372channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001373{
Ben Lindstrom69128662001-05-08 20:07:39 +00001374 int err = 0;
Ben Lindstrom11180952001-07-04 05:13:35 +00001375 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001376
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001377 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001378 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001379 err = errno;
1380 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001381 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001382 if (err == 0) {
1383 debug("channel %d: connected", c->self);
1384 c->type = SSH_CHANNEL_OPEN;
1385 if (compat20) {
1386 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1387 packet_put_int(c->remote_id);
1388 packet_put_int(c->self);
1389 packet_put_int(c->local_window);
1390 packet_put_int(c->local_maxpacket);
1391 } else {
1392 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1393 packet_put_int(c->remote_id);
1394 packet_put_int(c->self);
1395 }
1396 } else {
1397 debug("channel %d: not connected: %s",
1398 c->self, strerror(err));
1399 if (compat20) {
1400 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1401 packet_put_int(c->remote_id);
1402 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1403 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1404 packet_put_cstring(strerror(err));
1405 packet_put_cstring("");
1406 }
1407 } else {
1408 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1409 packet_put_int(c->remote_id);
1410 }
1411 chan_mark_dead(c);
1412 }
1413 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001414 }
1415}
1416
Ben Lindstrombba81212001-06-25 05:01:22 +00001417static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001418channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001419{
Darren Tucker11327cc2005-03-14 23:22:25 +11001420 char buf[CHAN_RBUF];
Damien Millerb38eff82000-04-01 11:09:21 +10001421 int len;
1422
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001423 if (c->rfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001424 FD_ISSET(c->rfd, readset)) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001425 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001426 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +10001427 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1428 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001429#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001430 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001431#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001432 if ((!c->isatty && len <= 0) ||
1433 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001434#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001435 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001436 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001437 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001438 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001439 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001440 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001441 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001442 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001443 c->type = SSH_CHANNEL_INPUT_DRAINING;
Damien Millerfbdeece2003-09-02 22:52:31 +10001444 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001445 } else {
1446 chan_read_failed(c);
1447 }
1448 return -1;
1449 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001450 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001451 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001452 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001453 chan_read_failed(c);
1454 }
Damien Millerd27b9472005-12-13 19:29:02 +11001455 } else if (c->datagram) {
1456 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001457 } else {
1458 buffer_append(&c->input, buf, len);
1459 }
Damien Millerb38eff82000-04-01 11:09:21 +10001460 }
1461 return 1;
1462}
Damien Miller4f7becb2006-03-26 14:10:14 +11001463
Ben Lindstrombba81212001-06-25 05:01:22 +00001464static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001465channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001466{
Ben Lindstrome229b252001-03-05 06:28:06 +00001467 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001468 u_char *data = NULL, *buf;
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001469 u_int dlen;
Damien Millerb38eff82000-04-01 11:09:21 +10001470 int len;
1471
1472 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001473 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001474 FD_ISSET(c->wfd, writeset) &&
1475 buffer_len(&c->output) > 0) {
Damien Miller077b2382005-12-31 16:22:32 +11001476 if (c->output_filter != NULL) {
1477 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1478 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001479 if (c->type != SSH_CHANNEL_OPEN)
1480 chan_mark_dead(c);
1481 else
1482 chan_write_failed(c);
1483 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001484 }
1485 } else if (c->datagram) {
1486 buf = data = buffer_get_string(&c->output, &dlen);
1487 } else {
1488 buf = data = buffer_ptr(&c->output);
1489 dlen = buffer_len(&c->output);
1490 }
1491
Damien Millerd27b9472005-12-13 19:29:02 +11001492 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001493 /* ignore truncated writes, datagrams might get lost */
1494 c->local_consumed += dlen + 4;
Damien Miller077b2382005-12-31 16:22:32 +11001495 len = write(c->wfd, buf, dlen);
Damien Millerd27b9472005-12-13 19:29:02 +11001496 xfree(data);
1497 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1498 return 1;
1499 if (len <= 0) {
1500 if (c->type != SSH_CHANNEL_OPEN)
1501 chan_mark_dead(c);
1502 else
1503 chan_write_failed(c);
1504 return -1;
1505 }
1506 return 1;
1507 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001508#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001509 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker240fdfa2003-11-22 14:10:02 +11001510 if (compat20 && c->wfd_isatty)
1511 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001512#endif
Damien Miller077b2382005-12-31 16:22:32 +11001513
1514 len = write(c->wfd, buf, dlen);
Damien Miller6f83b8e2000-05-02 09:23:45 +10001515 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1516 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001517 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001518 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001519 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001520 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001521 return -1;
1522 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001523 buffer_clear(&c->output);
Damien Millerfbdeece2003-09-02 22:52:31 +10001524 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001525 c->type = SSH_CHANNEL_INPUT_DRAINING;
1526 } else {
1527 chan_write_failed(c);
1528 }
1529 return -1;
1530 }
Damien Miller077b2382005-12-31 16:22:32 +11001531 if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001532 if (tcgetattr(c->wfd, &tio) == 0 &&
1533 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1534 /*
1535 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001536 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001537 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001538 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001539 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001540 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001541 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001542 }
1543 }
Damien Millerb38eff82000-04-01 11:09:21 +10001544 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001545 if (compat20 && len > 0) {
1546 c->local_consumed += len;
1547 }
1548 }
1549 return 1;
1550}
Damien Miller4f7becb2006-03-26 14:10:14 +11001551
Ben Lindstrombba81212001-06-25 05:01:22 +00001552static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001553channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001554{
Darren Tucker11327cc2005-03-14 23:22:25 +11001555 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001556 int len;
1557
Damien Miller1383bd82000-04-06 12:32:37 +10001558/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001559 if (c->efd != -1) {
1560 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1561 FD_ISSET(c->efd, writeset) &&
1562 buffer_len(&c->extended) > 0) {
1563 len = write(c->efd, buffer_ptr(&c->extended),
1564 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001565 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001566 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001567 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1568 return 1;
1569 if (len <= 0) {
1570 debug2("channel %d: closing write-efd %d",
1571 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001572 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001573 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001574 buffer_consume(&c->extended, len);
1575 c->local_consumed += len;
1576 }
1577 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1578 FD_ISSET(c->efd, readset)) {
1579 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001580 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001581 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001582 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1583 return 1;
1584 if (len <= 0) {
1585 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001586 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001587 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001588 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001589 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001590 }
Damien Miller33b13562000-04-04 14:38:59 +10001591 }
1592 }
1593 return 1;
1594}
Damien Miller4f7becb2006-03-26 14:10:14 +11001595
Ben Lindstrombba81212001-06-25 05:01:22 +00001596static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001597channel_handle_ctl(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller0e220db2004-06-15 10:34:08 +10001598{
1599 char buf[16];
1600 int len;
1601
1602 /* Monitor control fd to detect if the slave client exits */
1603 if (c->ctl_fd != -1 && FD_ISSET(c->ctl_fd, readset)) {
1604 len = read(c->ctl_fd, buf, sizeof(buf));
1605 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1606 return 1;
1607 if (len <= 0) {
1608 debug2("channel %d: ctl read<=0", c->self);
1609 if (c->type != SSH_CHANNEL_OPEN) {
1610 debug2("channel %d: not open", c->self);
1611 chan_mark_dead(c);
1612 return -1;
1613 } else {
1614 chan_read_failed(c);
1615 chan_write_failed(c);
1616 }
1617 return -1;
1618 } else
1619 fatal("%s: unexpected data on ctl fd", __func__);
1620 }
1621 return 1;
1622}
Damien Miller4f7becb2006-03-26 14:10:14 +11001623
Damien Miller0e220db2004-06-15 10:34:08 +10001624static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001625channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001626{
Ben Lindstromb3921512001-04-11 15:57:50 +00001627 if (c->type == SSH_CHANNEL_OPEN &&
1628 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001629 c->local_window < c->local_window_max/2 &&
1630 c->local_consumed > 0) {
1631 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1632 packet_put_int(c->remote_id);
1633 packet_put_int(c->local_consumed);
1634 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001635 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001636 c->self, c->local_window,
1637 c->local_consumed);
1638 c->local_window += c->local_consumed;
1639 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001640 }
1641 return 1;
1642}
1643
Ben Lindstrombba81212001-06-25 05:01:22 +00001644static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001645channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001646{
Damien Miller4623a752001-10-10 15:03:58 +10001647 if (c->delayed)
1648 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001649 channel_handle_rfd(c, readset, writeset);
1650 channel_handle_wfd(c, readset, writeset);
Damien Millerde6987c2002-01-22 23:20:40 +11001651 if (!compat20)
Damien Miller4623a752001-10-10 15:03:58 +10001652 return;
Damien Miller33b13562000-04-04 14:38:59 +10001653 channel_handle_efd(c, readset, writeset);
Damien Miller0e220db2004-06-15 10:34:08 +10001654 channel_handle_ctl(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001655 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001656}
1657
Ben Lindstrombba81212001-06-25 05:01:22 +00001658static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001659channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001660{
1661 int len;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001662
Damien Millerb38eff82000-04-01 11:09:21 +10001663 /* Send buffered output data to the socket. */
1664 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1665 len = write(c->sock, buffer_ptr(&c->output),
1666 buffer_len(&c->output));
1667 if (len <= 0)
Damien Miller76765c02002-01-22 23:21:15 +11001668 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001669 else
1670 buffer_consume(&c->output, len);
1671 }
1672}
1673
Ben Lindstrombba81212001-06-25 05:01:22 +00001674static void
Damien Miller33b13562000-04-04 14:38:59 +10001675channel_handler_init_20(void)
1676{
Damien Millerde6987c2002-01-22 23:20:40 +11001677 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001678 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001679 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001680 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001681 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001682 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001683 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001684 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001685
Damien Millerde6987c2002-01-22 23:20:40 +11001686 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001687 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001688 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001689 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001690 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001691 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001692 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001693}
1694
Ben Lindstrombba81212001-06-25 05:01:22 +00001695static void
Damien Millerb38eff82000-04-01 11:09:21 +10001696channel_handler_init_13(void)
1697{
1698 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1699 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1700 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1701 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1702 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1703 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1704 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001705 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001706 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001707
Damien Millerde6987c2002-01-22 23:20:40 +11001708 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001709 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1710 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1711 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1712 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001713 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001714 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001715}
1716
Ben Lindstrombba81212001-06-25 05:01:22 +00001717static void
Damien Millerb38eff82000-04-01 11:09:21 +10001718channel_handler_init_15(void)
1719{
Damien Millerde6987c2002-01-22 23:20:40 +11001720 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001721 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001722 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1723 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1724 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001725 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001726 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001727
1728 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1729 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1730 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Damien Millerde6987c2002-01-22 23:20:40 +11001731 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001732 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001733 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001734}
1735
Ben Lindstrombba81212001-06-25 05:01:22 +00001736static void
Damien Millerb38eff82000-04-01 11:09:21 +10001737channel_handler_init(void)
1738{
1739 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001740
Damien Miller9f0f5c62001-12-21 14:45:46 +11001741 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001742 channel_pre[i] = NULL;
1743 channel_post[i] = NULL;
1744 }
Damien Miller33b13562000-04-04 14:38:59 +10001745 if (compat20)
1746 channel_handler_init_20();
1747 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001748 channel_handler_init_13();
1749 else
1750 channel_handler_init_15();
1751}
1752
Damien Miller3ec27592001-10-12 11:35:04 +10001753/* gc dead channels */
1754static void
1755channel_garbage_collect(Channel *c)
1756{
1757 if (c == NULL)
1758 return;
1759 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11001760 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10001761 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001762 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001763 c->detach_user(c->self, NULL);
1764 /* if we still have a callback */
1765 if (c->detach_user != NULL)
1766 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001767 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001768 }
1769 if (!chan_is_dead(c, 1))
1770 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001771 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001772 channel_free(c);
1773}
1774
Ben Lindstrombba81212001-06-25 05:01:22 +00001775static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001776channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001777{
1778 static int did_init = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001779 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001780 Channel *c;
1781
1782 if (!did_init) {
1783 channel_handler_init();
1784 did_init = 1;
1785 }
1786 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001787 c = channels[i];
1788 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001789 continue;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001790 if (ftab[c->type] != NULL)
1791 (*ftab[c->type])(c, readset, writeset);
Damien Miller3ec27592001-10-12 11:35:04 +10001792 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001793 }
1794}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001795
Ben Lindstrome9c99912001-06-09 00:41:05 +00001796/*
1797 * Allocate/update select bitmasks and add any bits relevant to channels in
1798 * select bitmasks.
1799 */
Damien Miller4af51302000-04-16 11:18:38 +10001800void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001801channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001802 u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001803{
Damien Miller36812092006-03-26 14:22:47 +11001804 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11001805
1806 n = MAX(*maxfdp, channel_max_fd);
1807
Damien Miller36812092006-03-26 14:22:47 +11001808 nfdset = howmany(n+1, NFDBITS);
1809 /* Explicitly test here, because xrealloc isn't always called */
1810 if (nfdset && SIZE_T_MAX / nfdset < sizeof(fd_mask))
1811 fatal("channel_prepare_select: max_fd (%d) is too large", n);
1812 sz = nfdset * sizeof(fd_mask);
1813
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001814 /* perhaps check sz < nalloc/2 and shrink? */
1815 if (*readsetp == NULL || sz > *nallocp) {
Damien Miller36812092006-03-26 14:22:47 +11001816 *readsetp = xrealloc(*readsetp, nfdset, sizeof(fd_mask));
1817 *writesetp = xrealloc(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001818 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11001819 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001820 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11001821 memset(*readsetp, 0, sz);
1822 memset(*writesetp, 0, sz);
1823
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001824 if (!rekeying)
1825 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001826}
1827
Ben Lindstrome9c99912001-06-09 00:41:05 +00001828/*
1829 * After select, perform any appropriate operations for channels which have
1830 * events pending.
1831 */
Damien Miller4af51302000-04-16 11:18:38 +10001832void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001833channel_after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001834{
Damien Millerb38eff82000-04-01 11:09:21 +10001835 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001836}
1837
Ben Lindstrome9c99912001-06-09 00:41:05 +00001838
Damien Miller5e953212001-01-30 09:14:00 +11001839/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10001840void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001841channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001842{
Damien Millerb38eff82000-04-01 11:09:21 +10001843 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001844 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001845
Damien Miller95def091999-11-25 00:26:21 +11001846 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001847 c = channels[i];
1848 if (c == NULL)
1849 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001850
Ben Lindstrome9c99912001-06-09 00:41:05 +00001851 /*
1852 * We are only interested in channels that can have buffered
1853 * incoming data.
1854 */
Damien Miller34132e52000-01-14 15:45:46 +11001855 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001856 if (c->type != SSH_CHANNEL_OPEN &&
1857 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001858 continue;
1859 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001860 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001861 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001862 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001863 if (compat20 &&
1864 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001865 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10001866 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001867 continue;
1868 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001869
Damien Miller95def091999-11-25 00:26:21 +11001870 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001871 if ((c->istate == CHAN_INPUT_OPEN ||
1872 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1873 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001874 if (c->datagram) {
1875 if (len > 0) {
1876 u_char *data;
1877 u_int dlen;
1878
1879 data = buffer_get_string(&c->input,
1880 &dlen);
1881 packet_start(SSH2_MSG_CHANNEL_DATA);
1882 packet_put_int(c->remote_id);
1883 packet_put_string(data, dlen);
1884 packet_send();
1885 c->remote_window -= dlen + 4;
1886 xfree(data);
1887 }
1888 continue;
1889 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001890 /*
1891 * Send some data for the other side over the secure
1892 * connection.
1893 */
Damien Miller33b13562000-04-04 14:38:59 +10001894 if (compat20) {
1895 if (len > c->remote_window)
1896 len = c->remote_window;
1897 if (len > c->remote_maxpacket)
1898 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001899 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001900 if (packet_is_interactive()) {
1901 if (len > 1024)
1902 len = 512;
1903 } else {
1904 /* Keep the packets at reasonable size. */
1905 if (len > packet_get_maxsize()/2)
1906 len = packet_get_maxsize()/2;
1907 }
Damien Miller95def091999-11-25 00:26:21 +11001908 }
Damien Millerb38eff82000-04-01 11:09:21 +10001909 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001910 packet_start(compat20 ?
1911 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001912 packet_put_int(c->remote_id);
1913 packet_put_string(buffer_ptr(&c->input), len);
1914 packet_send();
1915 buffer_consume(&c->input, len);
1916 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001917 }
1918 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001919 if (compat13)
1920 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001921 /*
1922 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00001923 * tell peer, that we will not send more data: send IEOF.
1924 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11001925 */
Ben Lindstromcf159442002-03-26 03:26:24 +00001926 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10001927 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
1928 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00001929 else
1930 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001931 }
Damien Miller33b13562000-04-04 14:38:59 +10001932 /* Send extended data, i.e. stderr */
1933 if (compat20 &&
Ben Lindstromcf159442002-03-26 03:26:24 +00001934 !(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10001935 c->remote_window > 0 &&
1936 (len = buffer_len(&c->extended)) > 0 &&
1937 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001938 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001939 c->self, c->remote_window, buffer_len(&c->extended),
1940 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001941 if (len > c->remote_window)
1942 len = c->remote_window;
1943 if (len > c->remote_maxpacket)
1944 len = c->remote_maxpacket;
1945 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1946 packet_put_int(c->remote_id);
1947 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1948 packet_put_string(buffer_ptr(&c->extended), len);
1949 packet_send();
1950 buffer_consume(&c->extended, len);
1951 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001952 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001953 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001954 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001955}
1956
Ben Lindstrome9c99912001-06-09 00:41:05 +00001957
1958/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11001959
1960/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10001961void
Damien Miller630d6f42002-01-22 23:17:30 +11001962channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001963{
Damien Miller34132e52000-01-14 15:45:46 +11001964 int id;
Damien Miller95def091999-11-25 00:26:21 +11001965 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001966 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001967 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001968
Damien Miller95def091999-11-25 00:26:21 +11001969 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001970 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001971 c = channel_lookup(id);
1972 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001973 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001974
Damien Miller95def091999-11-25 00:26:21 +11001975 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001976 if (c->type != SSH_CHANNEL_OPEN &&
1977 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001978 return;
1979
Damien Miller95def091999-11-25 00:26:21 +11001980 /* Get the data. */
1981 data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001982
Damien Millera04ad492004-01-21 11:02:09 +11001983 /*
1984 * Ignore data for protocol > 1.3 if output end is no longer open.
1985 * For protocol 2 the sending side is reducing its window as it sends
1986 * data, so we must 'fake' consumption of the data in order to ensure
1987 * that window updates are sent back. Otherwise the connection might
1988 * deadlock.
1989 */
1990 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
1991 if (compat20) {
1992 c->local_window -= data_len;
1993 c->local_consumed += data_len;
1994 }
1995 xfree(data);
1996 return;
1997 }
1998
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001999 if (compat20) {
Damien Miller33b13562000-04-04 14:38:59 +10002000 if (data_len > c->local_maxpacket) {
Damien Miller996acd22003-04-09 20:59:48 +10002001 logit("channel %d: rcvd big packet %d, maxpack %d",
Damien Miller33b13562000-04-04 14:38:59 +10002002 c->self, data_len, c->local_maxpacket);
2003 }
2004 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002005 logit("channel %d: rcvd too much data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002006 c->self, data_len, c->local_window);
2007 xfree(data);
2008 return;
2009 }
2010 c->local_window -= data_len;
Damien Miller33b13562000-04-04 14:38:59 +10002011 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002012 packet_check_eom();
Damien Millerd27b9472005-12-13 19:29:02 +11002013 if (c->datagram)
2014 buffer_put_string(&c->output, data, data_len);
2015 else
2016 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11002017 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002018}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002019
Damien Millerd79b4242006-03-31 23:11:44 +11002020/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002021void
Damien Miller630d6f42002-01-22 23:17:30 +11002022channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002023{
2024 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002025 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002026 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002027 Channel *c;
2028
2029 /* Get the channel number and verify it. */
2030 id = packet_get_int();
2031 c = channel_lookup(id);
2032
2033 if (c == NULL)
2034 packet_disconnect("Received extended_data for bad channel %d.", id);
2035 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002036 logit("channel %d: ext data for non open", id);
Damien Miller33b13562000-04-04 14:38:59 +10002037 return;
2038 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002039 if (c->flags & CHAN_EOF_RCVD) {
2040 if (datafellows & SSH_BUG_EXTEOF)
2041 debug("channel %d: accepting ext data after eof", id);
2042 else
2043 packet_disconnect("Received extended_data after EOF "
2044 "on channel %d.", id);
2045 }
Damien Miller33b13562000-04-04 14:38:59 +10002046 tcode = packet_get_int();
2047 if (c->efd == -1 ||
2048 c->extended_usage != CHAN_EXTENDED_WRITE ||
2049 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002050 logit("channel %d: bad ext data", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002051 return;
2052 }
2053 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002054 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002055 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002056 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002057 c->self, data_len, c->local_window);
2058 xfree(data);
2059 return;
2060 }
Damien Millerd3444942000-09-30 14:20:03 +11002061 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002062 c->local_window -= data_len;
2063 buffer_append(&c->extended, data, data_len);
2064 xfree(data);
2065}
2066
Damien Millerd79b4242006-03-31 23:11:44 +11002067/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002068void
Damien Miller630d6f42002-01-22 23:17:30 +11002069channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002070{
2071 int id;
2072 Channel *c;
2073
Damien Millerb38eff82000-04-01 11:09:21 +10002074 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002075 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002076 c = channel_lookup(id);
2077 if (c == NULL)
2078 packet_disconnect("Received ieof for nonexistent channel %d.", id);
2079 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002080
2081 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002082 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002083 debug("channel %d: FORCE input drain", c->self);
2084 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002085 if (buffer_len(&c->input) == 0)
2086 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002087 }
2088
Damien Millerb38eff82000-04-01 11:09:21 +10002089}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002090
Damien Millerd79b4242006-03-31 23:11:44 +11002091/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002092void
Damien Miller630d6f42002-01-22 23:17:30 +11002093channel_input_close(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002094{
Damien Millerb38eff82000-04-01 11:09:21 +10002095 int id;
2096 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002097
Damien Millerb38eff82000-04-01 11:09:21 +10002098 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002099 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002100 c = channel_lookup(id);
2101 if (c == NULL)
2102 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11002103
2104 /*
2105 * Send a confirmation that we have closed the channel and no more
2106 * data is coming for it.
2107 */
Damien Miller95def091999-11-25 00:26:21 +11002108 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10002109 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11002110 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002111
Damien Miller5428f641999-11-25 11:54:57 +11002112 /*
2113 * If the channel is in closed state, we have sent a close request,
2114 * and the other side will eventually respond with a confirmation.
2115 * Thus, we cannot free the channel here, because then there would be
2116 * no-one to receive the confirmation. The channel gets freed when
2117 * the confirmation arrives.
2118 */
Damien Millerb38eff82000-04-01 11:09:21 +10002119 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11002120 /*
2121 * Not a closed channel - mark it as draining, which will
2122 * cause it to be freed later.
2123 */
Damien Miller76765c02002-01-22 23:21:15 +11002124 buffer_clear(&c->input);
Damien Millerb38eff82000-04-01 11:09:21 +10002125 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11002126 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002127}
2128
Damien Millerb38eff82000-04-01 11:09:21 +10002129/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Millerd79b4242006-03-31 23:11:44 +11002130/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002131void
Damien Miller630d6f42002-01-22 23:17:30 +11002132channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002133{
Damien Millerb38eff82000-04-01 11:09:21 +10002134 int id = packet_get_int();
2135 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002136
Damien Miller48b03fc2002-01-22 23:11:40 +11002137 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002138 if (c == NULL)
2139 packet_disconnect("Received oclose for nonexistent channel %d.", id);
2140 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002141}
2142
Damien Millerd79b4242006-03-31 23:11:44 +11002143/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002144void
Damien Miller630d6f42002-01-22 23:17:30 +11002145channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002146{
2147 int id = packet_get_int();
2148 Channel *c = channel_lookup(id);
2149
Damien Miller48b03fc2002-01-22 23:11:40 +11002150 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002151 if (c == NULL)
2152 packet_disconnect("Received close confirmation for "
2153 "out-of-range channel %d.", id);
2154 if (c->type != SSH_CHANNEL_CLOSED)
2155 packet_disconnect("Received close confirmation for "
2156 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002157 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002158}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002159
Damien Millerd79b4242006-03-31 23:11:44 +11002160/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002161void
Damien Miller630d6f42002-01-22 23:17:30 +11002162channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002163{
Damien Millerb38eff82000-04-01 11:09:21 +10002164 int id, remote_id;
2165 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002166
Damien Millerb38eff82000-04-01 11:09:21 +10002167 id = packet_get_int();
2168 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002169
Damien Millerb38eff82000-04-01 11:09:21 +10002170 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2171 packet_disconnect("Received open confirmation for "
2172 "non-opening channel %d.", id);
2173 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002174 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002175 c->remote_id = remote_id;
2176 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002177
2178 if (compat20) {
2179 c->remote_window = packet_get_int();
2180 c->remote_maxpacket = packet_get_int();
Damien Miller67f0bc02002-02-05 12:23:08 +11002181 if (c->confirm) {
Damien Millerd3444942000-09-30 14:20:03 +11002182 debug2("callback start");
Damien Miller0e220db2004-06-15 10:34:08 +10002183 c->confirm(c->self, c->confirm_ctx);
Damien Millerd3444942000-09-30 14:20:03 +11002184 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002185 }
Damien Millerfbdeece2003-09-02 22:52:31 +10002186 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
Damien Miller33b13562000-04-04 14:38:59 +10002187 c->remote_window, c->remote_maxpacket);
2188 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002189 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002190}
2191
Ben Lindstrombba81212001-06-25 05:01:22 +00002192static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002193reason2txt(int reason)
2194{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002195 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002196 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2197 return "administratively prohibited";
2198 case SSH2_OPEN_CONNECT_FAILED:
2199 return "connect failed";
2200 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2201 return "unknown channel type";
2202 case SSH2_OPEN_RESOURCE_SHORTAGE:
2203 return "resource shortage";
2204 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002205 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002206}
2207
Damien Millerd79b4242006-03-31 23:11:44 +11002208/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002209void
Damien Miller630d6f42002-01-22 23:17:30 +11002210channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002211{
Kevin Steves12057502001-02-05 14:54:34 +00002212 int id, reason;
2213 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002214 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002215
Damien Millerb38eff82000-04-01 11:09:21 +10002216 id = packet_get_int();
2217 c = channel_lookup(id);
2218
2219 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2220 packet_disconnect("Received open failure for "
2221 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002222 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00002223 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00002224 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00002225 msg = packet_get_string(NULL);
2226 lang = packet_get_string(NULL);
2227 }
Damien Miller996acd22003-04-09 20:59:48 +10002228 logit("channel %d: open failed: %s%s%s", id,
Ben Lindstrom69128662001-05-08 20:07:39 +00002229 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00002230 if (msg != NULL)
2231 xfree(msg);
2232 if (lang != NULL)
2233 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10002234 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002235 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11002236 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002237 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002238}
2239
Damien Millerd79b4242006-03-31 23:11:44 +11002240/* ARGSUSED */
Damien Miller33b13562000-04-04 14:38:59 +10002241void
Damien Miller630d6f42002-01-22 23:17:30 +11002242channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002243{
2244 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002245 int id;
2246 u_int adjust;
Damien Miller33b13562000-04-04 14:38:59 +10002247
2248 if (!compat20)
2249 return;
2250
2251 /* Get the channel number and verify it. */
2252 id = packet_get_int();
2253 c = channel_lookup(id);
2254
Damien Millerd47c62a2005-12-13 19:33:57 +11002255 if (c == NULL) {
2256 logit("Received window adjust for non-open channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002257 return;
2258 }
2259 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002260 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002261 debug2("channel %d: rcvd adjust %u", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10002262 c->remote_window += adjust;
2263}
2264
Damien Millerd79b4242006-03-31 23:11:44 +11002265/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002266void
Damien Miller630d6f42002-01-22 23:17:30 +11002267channel_input_port_open(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002268{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002269 Channel *c = NULL;
2270 u_short host_port;
2271 char *host, *originator_string;
2272 int remote_id, sock = -1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002273
Ben Lindstrome9c99912001-06-09 00:41:05 +00002274 remote_id = packet_get_int();
2275 host = packet_get_string(NULL);
2276 host_port = packet_get_int();
2277
2278 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2279 originator_string = packet_get_string(NULL);
2280 } else {
2281 originator_string = xstrdup("unknown (remote did not supply name)");
2282 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002283 packet_check_eom();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002284 sock = channel_connect_to(host, host_port);
2285 if (sock != -1) {
2286 c = channel_new("connected socket",
2287 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2288 originator_string, 1);
Damien Miller699d0032002-02-08 22:07:16 +11002289 c->remote_id = remote_id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002290 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002291 xfree(originator_string);
Ben Lindstrome9c99912001-06-09 00:41:05 +00002292 if (c == NULL) {
2293 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2294 packet_put_int(remote_id);
2295 packet_send();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002296 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002297 xfree(host);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002298}
2299
2300
Ben Lindstrome9c99912001-06-09 00:41:05 +00002301/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002302
Ben Lindstrom908afed2001-10-03 17:34:59 +00002303void
2304channel_set_af(int af)
2305{
2306 IPv4or6 = af;
2307}
2308
Damien Millerb16461c2002-01-22 23:29:22 +11002309static int
2310channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
2311 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002312{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002313 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11002314 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11002315 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002316 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11002317 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002318
Damien Millerb16461c2002-01-22 23:29:22 +11002319 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2320 listen_addr : host_to_connect;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002321 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00002322
Damien Millerb16461c2002-01-22 23:29:22 +11002323 if (host == NULL) {
2324 error("No forward host name.");
Damien Millera7270302005-07-06 09:36:05 +10002325 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002326 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002327 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00002328 error("Forward host name too long.");
Damien Millera7270302005-07-06 09:36:05 +10002329 return 0;
Kevin Steves12057502001-02-05 14:54:34 +00002330 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002331
Damien Miller5428f641999-11-25 11:54:57 +11002332 /*
Damien Millerf91ee4c2005-03-01 21:24:33 +11002333 * Determine whether or not a port forward listens to loopback,
Darren Tucker47eede72005-03-14 23:08:12 +11002334 * specified address or wildcard. On the client, a specified bind
2335 * address will always override gateway_ports. On the server, a
2336 * gateway_ports of 1 (``yes'') will override the client's
2337 * specification and force a wildcard bind, whereas a value of 2
2338 * (``clientspecified'') will bind to whatever address the client
Damien Millerf91ee4c2005-03-01 21:24:33 +11002339 * asked for.
2340 *
2341 * Special-case listen_addrs are:
2342 *
2343 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2344 * "" (empty string), "*" -> wildcard v4/v6
2345 * "localhost" -> loopback v4/v6
2346 */
2347 addr = NULL;
2348 if (listen_addr == NULL) {
2349 /* No address specified: default to gateway_ports setting */
2350 if (gateway_ports)
2351 wildcard = 1;
2352 } else if (gateway_ports || is_client) {
2353 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2354 strcmp(listen_addr, "0.0.0.0") == 0) ||
2355 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
2356 (!is_client && gateway_ports == 1))
2357 wildcard = 1;
2358 else if (strcmp(listen_addr, "localhost") != 0)
2359 addr = listen_addr;
2360 }
2361
2362 debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s",
2363 type, wildcard, (addr == NULL) ? "NULL" : addr);
2364
2365 /*
Damien Miller34132e52000-01-14 15:45:46 +11002366 * getaddrinfo returns a loopback address if the hostname is
2367 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002368 */
Damien Miller34132e52000-01-14 15:45:46 +11002369 memset(&hints, 0, sizeof(hints));
2370 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002371 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002372 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002373 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002374 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2375 if (addr == NULL) {
2376 /* This really shouldn't happen */
2377 packet_disconnect("getaddrinfo: fatal error: %s",
2378 gai_strerror(r));
2379 } else {
Damien Millera7270302005-07-06 09:36:05 +10002380 error("channel_setup_fwd_listener: "
Damien Millerf91ee4c2005-03-01 21:24:33 +11002381 "getaddrinfo(%.64s): %s", addr, gai_strerror(r));
2382 }
Damien Millera7270302005-07-06 09:36:05 +10002383 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002384 }
Damien Miller5428f641999-11-25 11:54:57 +11002385
Damien Miller34132e52000-01-14 15:45:46 +11002386 for (ai = aitop; ai; ai = ai->ai_next) {
2387 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2388 continue;
2389 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2390 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Millerb16461c2002-01-22 23:29:22 +11002391 error("channel_setup_fwd_listener: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002392 continue;
2393 }
2394 /* Create a port to listen for the host. */
Damien Miller2372ace2003-05-14 13:42:23 +10002395 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002396 if (sock < 0) {
2397 /* this is no error since kernel may not support ipv6 */
2398 verbose("socket: %.100s", strerror(errno));
2399 continue;
2400 }
Damien Miller5e7fd072005-11-05 14:53:39 +11002401
2402 channel_set_reuseaddr(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10002403
Damien Miller34132e52000-01-14 15:45:46 +11002404 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002405
Damien Miller34132e52000-01-14 15:45:46 +11002406 /* Bind the socket to the address. */
2407 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2408 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002409 if (!ai->ai_next)
2410 error("bind: %.100s", strerror(errno));
2411 else
2412 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002413
Damien Miller34132e52000-01-14 15:45:46 +11002414 close(sock);
2415 continue;
2416 }
2417 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11002418 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002419 error("listen: %.100s", strerror(errno));
2420 close(sock);
2421 continue;
2422 }
2423 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002424 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002425 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002426 0, "port listener", 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002427 strlcpy(c->path, host, sizeof(c->path));
2428 c->host_port = port_to_connect;
2429 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002430 success = 1;
2431 }
2432 if (success == 0)
Damien Millerb16461c2002-01-22 23:29:22 +11002433 error("channel_setup_fwd_listener: cannot listen to port: %d",
Kevin Steves12057502001-02-05 14:54:34 +00002434 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002435 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002436 return success;
Damien Miller95def091999-11-25 00:26:21 +11002437}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002438
Darren Tuckere7066df2004-05-24 10:18:05 +10002439int
2440channel_cancel_rport_listener(const char *host, u_short port)
2441{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002442 u_int i;
2443 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10002444
Darren Tucker47eede72005-03-14 23:08:12 +11002445 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10002446 Channel *c = channels[i];
2447
2448 if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
2449 strncmp(c->path, host, sizeof(c->path)) == 0 &&
Darren Tuckerfc959702004-07-17 16:12:08 +10002450 c->listening_port == port) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10002451 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10002452 channel_free(c);
2453 found = 1;
2454 }
2455 }
2456
2457 return (found);
2458}
2459
Damien Millerb16461c2002-01-22 23:29:22 +11002460/* protocol local port fwd, used by ssh (and sshd in v1) */
2461int
Damien Millerf91ee4c2005-03-01 21:24:33 +11002462channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
Damien Millerb16461c2002-01-22 23:29:22 +11002463 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2464{
2465 return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002466 listen_host, listen_port, host_to_connect, port_to_connect,
2467 gateway_ports);
Damien Millerb16461c2002-01-22 23:29:22 +11002468}
2469
2470/* protocol v2 remote port fwd, used by sshd */
2471int
2472channel_setup_remote_fwd_listener(const char *listen_address,
2473 u_short listen_port, int gateway_ports)
2474{
2475 return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
2476 listen_address, listen_port, NULL, 0, gateway_ports);
2477}
2478
Damien Miller5428f641999-11-25 11:54:57 +11002479/*
2480 * Initiate forwarding of connections to port "port" on remote host through
2481 * the secure channel to host:port from local side.
2482 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002483
Darren Tuckere7d4b192006-07-12 22:17:10 +10002484int
Damien Millerf91ee4c2005-03-01 21:24:33 +11002485channel_request_remote_forwarding(const char *listen_host, u_short listen_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +11002486 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002487{
Damien Millerdff50992002-01-22 23:16:32 +11002488 int type, success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002489
Damien Miller95def091999-11-25 00:26:21 +11002490 /* Record locally that connection to this host/port is permitted. */
2491 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2492 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002493
Damien Miller95def091999-11-25 00:26:21 +11002494 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002495 if (compat20) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002496 const char *address_to_bind;
2497 if (listen_host == NULL)
2498 address_to_bind = "localhost";
2499 else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0)
2500 address_to_bind = "";
2501 else
2502 address_to_bind = listen_host;
2503
Damien Miller33b13562000-04-04 14:38:59 +10002504 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2505 packet_put_cstring("tcpip-forward");
Damien Miller2797f7f2002-04-23 21:09:44 +10002506 packet_put_char(1); /* boolean: want reply */
Damien Miller33b13562000-04-04 14:38:59 +10002507 packet_put_cstring(address_to_bind);
2508 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002509 packet_send();
2510 packet_write_wait();
2511 /* Assume that server accepts the request */
2512 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002513 } else {
2514 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002515 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002516 packet_put_cstring(host_to_connect);
2517 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002518 packet_send();
2519 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002520
2521 /* Wait for response from the remote side. */
Damien Millerdff50992002-01-22 23:16:32 +11002522 type = packet_read();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002523 switch (type) {
2524 case SSH_SMSG_SUCCESS:
2525 success = 1;
2526 break;
2527 case SSH_SMSG_FAILURE:
Damien Miller0bc1bd82000-11-13 22:57:25 +11002528 break;
2529 default:
2530 /* Unknown packet */
2531 packet_disconnect("Protocol error for port forward request:"
2532 "received packet type %d.", type);
2533 }
2534 }
2535 if (success) {
2536 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2537 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2538 permitted_opens[num_permitted_opens].listen_port = listen_port;
2539 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002540 }
Darren Tuckere7d4b192006-07-12 22:17:10 +10002541 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002542}
2543
Damien Miller5428f641999-11-25 11:54:57 +11002544/*
Darren Tuckerfc959702004-07-17 16:12:08 +10002545 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10002546 * local side.
2547 */
Darren Tuckere7066df2004-05-24 10:18:05 +10002548void
Damien Millerf91ee4c2005-03-01 21:24:33 +11002549channel_request_rforward_cancel(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10002550{
2551 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10002552
2553 if (!compat20)
2554 return;
2555
2556 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckerfc959702004-07-17 16:12:08 +10002557 if (permitted_opens[i].host_to_connect != NULL &&
Darren Tuckere7066df2004-05-24 10:18:05 +10002558 permitted_opens[i].listen_port == port)
2559 break;
2560 }
2561 if (i >= num_permitted_opens) {
2562 debug("%s: requested forward not found", __func__);
2563 return;
2564 }
2565 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2566 packet_put_cstring("cancel-tcpip-forward");
2567 packet_put_char(0);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002568 packet_put_cstring(host == NULL ? "" : host);
Darren Tuckere7066df2004-05-24 10:18:05 +10002569 packet_put_int(port);
2570 packet_send();
2571
2572 permitted_opens[i].listen_port = 0;
2573 permitted_opens[i].port_to_connect = 0;
Damien Miller0a0176e2005-11-05 15:07:59 +11002574 xfree(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10002575 permitted_opens[i].host_to_connect = NULL;
2576}
2577
2578/*
Damien Miller5428f641999-11-25 11:54:57 +11002579 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2580 * listening for the port, and sends back a success reply (or disconnect
Darren Tuckere7d4b192006-07-12 22:17:10 +10002581 * message if there was an error).
Damien Miller5428f641999-11-25 11:54:57 +11002582 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002583int
Damien Millere247cc42000-05-07 12:03:14 +10002584channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002585{
Damien Milleraae6c611999-12-06 11:47:28 +11002586 u_short port, host_port;
Darren Tuckere7d4b192006-07-12 22:17:10 +10002587 int success = 0;
Damien Miller95def091999-11-25 00:26:21 +11002588 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002589
Damien Miller95def091999-11-25 00:26:21 +11002590 /* Get arguments from the packet. */
2591 port = packet_get_int();
2592 hostname = packet_get_string(NULL);
2593 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002594
Damien Millerbac2d8a2000-09-05 16:13:06 +11002595#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002596 /*
2597 * Check that an unprivileged user is not trying to forward a
2598 * privileged port.
2599 */
Damien Miller95def091999-11-25 00:26:21 +11002600 if (port < IPPORT_RESERVED && !is_root)
Darren Tucker9189ff82003-07-03 13:52:04 +10002601 packet_disconnect(
2602 "Requested forwarding of port %d but user is not root.",
2603 port);
2604 if (host_port == 0)
2605 packet_disconnect("Dynamic forwarding denied.");
Damien Millerbac2d8a2000-09-05 16:13:06 +11002606#endif
Darren Tucker9189ff82003-07-03 13:52:04 +10002607
Damien Miller0bc1bd82000-11-13 22:57:25 +11002608 /* Initiate forwarding */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002609 success = channel_setup_local_fwd_listener(NULL, port, hostname,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002610 host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002611
2612 /* Free the argument string. */
2613 xfree(hostname);
Darren Tuckere7d4b192006-07-12 22:17:10 +10002614
2615 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002616}
2617
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002618/*
2619 * Permits opening to any host/port if permitted_opens[] is empty. This is
2620 * usually called by the server, because the user could connect to any port
2621 * anyway, and the server has no way to know but to trust the client anyway.
2622 */
2623void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002624channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002625{
2626 if (num_permitted_opens == 0)
2627 all_opens_permitted = 1;
2628}
2629
Ben Lindstroma3700052001-04-05 23:26:32 +00002630void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002631channel_add_permitted_opens(char *host, int port)
2632{
2633 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
Darren Tuckere7d4b192006-07-12 22:17:10 +10002634 fatal("channel_add_permitted_opens: too many forwards");
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002635 debug("allow port forwarding to host %s port %d", host, port);
2636
2637 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2638 permitted_opens[num_permitted_opens].port_to_connect = port;
2639 num_permitted_opens++;
2640
2641 all_opens_permitted = 0;
2642}
2643
Ben Lindstroma3700052001-04-05 23:26:32 +00002644void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002645channel_clear_permitted_opens(void)
2646{
2647 int i;
2648
2649 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002650 if (permitted_opens[i].host_to_connect != NULL)
2651 xfree(permitted_opens[i].host_to_connect);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002652 num_permitted_opens = 0;
2653
2654}
2655
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002656/* return socket to remote host, port */
Ben Lindstrombba81212001-06-25 05:01:22 +00002657static int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002658connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002659{
Damien Miller34132e52000-01-14 15:45:46 +11002660 struct addrinfo hints, *ai, *aitop;
2661 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2662 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002663 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002664
Damien Miller34132e52000-01-14 15:45:46 +11002665 memset(&hints, 0, sizeof(hints));
2666 hints.ai_family = IPv4or6;
2667 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002668 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002669 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002670 error("connect_to %.100s: unknown host (%s)", host,
2671 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002672 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002673 }
Damien Miller34132e52000-01-14 15:45:46 +11002674 for (ai = aitop; ai; ai = ai->ai_next) {
2675 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2676 continue;
2677 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2678 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002679 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002680 continue;
2681 }
Damien Miller2372ace2003-05-14 13:42:23 +10002682 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002683 if (sock < 0) {
Damien Millerb46b9f32003-01-10 21:45:12 +11002684 if (ai->ai_next == NULL)
2685 error("socket: %.100s", strerror(errno));
2686 else
2687 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002688 continue;
2689 }
Damien Miller232711f2004-06-15 10:35:30 +10002690 if (set_nonblock(sock) == -1)
2691 fatal("%s: set_nonblock(%d)", __func__, sock);
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002692 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2693 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002694 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002695 strerror(errno));
2696 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002697 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002698 }
2699 break; /* success */
2700
2701 }
2702 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002703 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002704 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002705 return -1;
2706 }
2707 /* success */
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00002708 set_nodelay(sock);
Damien Millerb38eff82000-04-01 11:09:21 +10002709 return sock;
2710}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002711
Damien Miller0bc1bd82000-11-13 22:57:25 +11002712int
Ben Lindstrom173e6462001-07-04 05:15:15 +00002713channel_connect_by_listen_address(u_short listen_port)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002714{
2715 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002716
Damien Miller0bc1bd82000-11-13 22:57:25 +11002717 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002718 if (permitted_opens[i].host_to_connect != NULL &&
2719 permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002720 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002721 permitted_opens[i].host_to_connect,
2722 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002723 error("WARNING: Server requests forwarding for unknown listen_port %d",
2724 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002725 return -1;
2726}
Damien Millerb38eff82000-04-01 11:09:21 +10002727
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002728/* Check if connecting to that port is permitted and connect. */
2729int
2730channel_connect_to(const char *host, u_short port)
2731{
2732 int i, permit;
2733
2734 permit = all_opens_permitted;
2735 if (!permit) {
2736 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002737 if (permitted_opens[i].host_to_connect != NULL &&
2738 permitted_opens[i].port_to_connect == port &&
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002739 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2740 permit = 1;
2741
2742 }
2743 if (!permit) {
Damien Miller996acd22003-04-09 20:59:48 +10002744 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002745 "but the request was denied.", host, port);
2746 return -1;
2747 }
2748 return connect_to(host, port);
2749}
2750
Damien Miller0e220db2004-06-15 10:34:08 +10002751void
2752channel_send_window_changes(void)
2753{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002754 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10002755 struct winsize ws;
2756
2757 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11002758 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10002759 channels[i]->type != SSH_CHANNEL_OPEN)
2760 continue;
2761 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
2762 continue;
2763 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11002764 packet_put_int((u_int)ws.ws_col);
2765 packet_put_int((u_int)ws.ws_row);
2766 packet_put_int((u_int)ws.ws_xpixel);
2767 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10002768 packet_send();
2769 }
2770}
2771
Ben Lindstrome9c99912001-06-09 00:41:05 +00002772/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002773
Damien Miller5428f641999-11-25 11:54:57 +11002774/*
2775 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002776 * Returns 0 and a suitable display number for the DISPLAY variable
2777 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11002778 */
Kevin Steves366298c2001-12-19 17:58:01 +00002779int
Damien Miller95c249f2002-02-05 12:11:34 +11002780x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10002781 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002782{
Damien Millere7378562001-12-21 14:58:35 +11002783 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11002784 int display_number, sock;
2785 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002786 struct addrinfo hints, *ai, *aitop;
2787 char strport[NI_MAXSERV];
2788 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002789
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002790 if (chanids == NULL)
2791 return -1;
2792
Damien Millera34a28b1999-12-14 10:47:15 +11002793 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002794 display_number < MAX_DISPLAYS;
2795 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11002796 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002797 memset(&hints, 0, sizeof(hints));
2798 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11002799 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11002800 hints.ai_socktype = SOCK_STREAM;
2801 snprintf(strport, sizeof strport, "%d", port);
2802 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2803 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00002804 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002805 }
Damien Miller34132e52000-01-14 15:45:46 +11002806 for (ai = aitop; ai; ai = ai->ai_next) {
2807 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2808 continue;
Damien Miller2372ace2003-05-14 13:42:23 +10002809 sock = socket(ai->ai_family, ai->ai_socktype,
2810 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002811 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002812 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002813 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10002814 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00002815 return -1;
Damien Millere2192732000-01-17 13:22:55 +11002816 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002817 debug("x11_create_display_inet: Socket family %d not supported",
2818 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002819 continue;
2820 }
Damien Miller34132e52000-01-14 15:45:46 +11002821 }
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002822#ifdef IPV6_V6ONLY
2823 if (ai->ai_family == AF_INET6) {
2824 int on = 1;
2825 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
2826 error("setsockopt IPV6_V6ONLY: %.100s", strerror(errno));
2827 }
2828#endif
Damien Miller5e7fd072005-11-05 14:53:39 +11002829 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11002830 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002831 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002832 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002833
2834 if (ai->ai_next)
2835 continue;
2836
Damien Miller34132e52000-01-14 15:45:46 +11002837 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11002838 close(socks[n]);
2839 }
2840 num_socks = 0;
2841 break;
2842 }
2843 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002844#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002845 if (num_socks == NUM_SOCKS)
2846 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002847#else
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002848 if (x11_use_localhost) {
2849 if (num_socks == NUM_SOCKS)
2850 break;
2851 } else {
2852 break;
2853 }
Damien Miller7bcb0892000-03-11 20:45:40 +11002854#endif
Damien Miller95def091999-11-25 00:26:21 +11002855 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002856 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002857 if (num_socks > 0)
2858 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002859 }
Damien Miller95def091999-11-25 00:26:21 +11002860 if (display_number >= MAX_DISPLAYS) {
2861 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00002862 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002863 }
Damien Miller95def091999-11-25 00:26:21 +11002864 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002865 for (n = 0; n < num_socks; n++) {
2866 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11002867 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002868 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002869 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00002870 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11002871 }
Damien Miller95def091999-11-25 00:26:21 +11002872 }
Damien Miller34132e52000-01-14 15:45:46 +11002873
Damien Miller34132e52000-01-14 15:45:46 +11002874 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11002875 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11002876 for (n = 0; n < num_socks; n++) {
2877 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11002878 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10002879 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2880 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002881 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11002882 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002883 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11002884 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002885 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002886
Kevin Steves366298c2001-12-19 17:58:01 +00002887 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002888 *display_numberp = display_number;
2889 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002890}
2891
Ben Lindstrombba81212001-06-25 05:01:22 +00002892static int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002893connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002894{
Damien Miller95def091999-11-25 00:26:21 +11002895 int sock;
2896 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002897
Damien Miller3afe3752001-12-21 12:39:51 +11002898 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2899 if (sock < 0)
2900 error("socket: %.100s", strerror(errno));
2901 memset(&addr, 0, sizeof(addr));
2902 addr.sun_family = AF_UNIX;
2903 snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
Damien Miller90967402006-03-26 14:07:26 +11002904 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11002905 return sock;
2906 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11002907 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2908 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002909}
2910
Damien Millerbd483e72000-04-30 10:00:53 +10002911int
2912x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002913{
Damien Miller57c4e872006-03-31 23:11:07 +11002914 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11002915 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002916 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002917 struct addrinfo hints, *ai, *aitop;
2918 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11002919 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002920
Damien Miller95def091999-11-25 00:26:21 +11002921 /* Try to open a socket for the local X server. */
2922 display = getenv("DISPLAY");
2923 if (!display) {
2924 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002925 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002926 }
Damien Miller5428f641999-11-25 11:54:57 +11002927 /*
2928 * Now we decode the value of the DISPLAY variable and make a
2929 * connection to the real X server.
2930 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002931
Damien Miller5428f641999-11-25 11:54:57 +11002932 /*
2933 * Check if it is a unix domain socket. Unix domain displays are in
2934 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2935 */
Damien Miller95def091999-11-25 00:26:21 +11002936 if (strncmp(display, "unix:", 5) == 0 ||
2937 display[0] == ':') {
2938 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11002939 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11002940 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11002941 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002942 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002943 }
2944 /* Create a socket. */
2945 sock = connect_local_xsocket(display_number);
2946 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002947 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002948
2949 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002950 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002951 }
Damien Miller5428f641999-11-25 11:54:57 +11002952 /*
2953 * Connect to an inet socket. The DISPLAY value is supposedly
2954 * hostname:d[.s], where hostname may also be numeric IP address.
2955 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00002956 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11002957 cp = strchr(buf, ':');
2958 if (!cp) {
2959 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002960 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002961 }
Damien Miller95def091999-11-25 00:26:21 +11002962 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002963 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11002964 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11002965 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11002966 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002967 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002968 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002969
Damien Miller34132e52000-01-14 15:45:46 +11002970 /* Look up the host address */
2971 memset(&hints, 0, sizeof(hints));
2972 hints.ai_family = IPv4or6;
2973 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11002974 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11002975 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2976 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002977 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002978 }
Damien Miller34132e52000-01-14 15:45:46 +11002979 for (ai = aitop; ai; ai = ai->ai_next) {
2980 /* Create a socket. */
Damien Miller2372ace2003-05-14 13:42:23 +10002981 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002982 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002983 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002984 continue;
2985 }
2986 /* Connect it to the display. */
2987 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11002988 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10002989 6000 + display_number, strerror(errno));
2990 close(sock);
2991 continue;
2992 }
2993 /* Success */
2994 break;
Damien Miller34132e52000-01-14 15:45:46 +11002995 }
Damien Miller34132e52000-01-14 15:45:46 +11002996 freeaddrinfo(aitop);
2997 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11002998 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002999 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10003000 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003001 }
Damien Miller398e1cf2002-02-05 11:52:13 +11003002 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10003003 return sock;
3004}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003005
Damien Millerbd483e72000-04-30 10:00:53 +10003006/*
3007 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
3008 * the remote channel number. We should do whatever we want, and respond
3009 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
3010 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003011
Damien Millerbd483e72000-04-30 10:00:53 +10003012void
Damien Miller630d6f42002-01-22 23:17:30 +11003013x11_input_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10003014{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003015 Channel *c = NULL;
3016 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10003017 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10003018
3019 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003020
3021 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00003022
3023 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003024 remote_host = packet_get_string(NULL);
3025 } else {
3026 remote_host = xstrdup("unknown (remote did not supply name)");
3027 }
Damien Miller48b03fc2002-01-22 23:11:40 +11003028 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10003029
3030 /* Obtain a connection to the real X display. */
3031 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003032 if (sock != -1) {
3033 /* Allocate a channel for this connection. */
3034 c = channel_new("connected x11 socket",
3035 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
3036 remote_host, 1);
Damien Miller699d0032002-02-08 22:07:16 +11003037 c->remote_id = remote_id;
3038 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003039 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003040 xfree(remote_host);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003041 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10003042 /* Send refusal to the remote host. */
3043 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003044 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10003045 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10003046 /* Send a confirmation to the remote host. */
3047 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003048 packet_put_int(remote_id);
3049 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10003050 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003051 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003052}
3053
Damien Miller69b69aa2000-10-28 14:19:58 +11003054/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
3055void
Damien Miller630d6f42002-01-22 23:17:30 +11003056deny_input_open(int type, u_int32_t seq, void *ctxt)
Damien Miller69b69aa2000-10-28 14:19:58 +11003057{
3058 int rchan = packet_get_int();
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00003059
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00003060 switch (type) {
Damien Miller69b69aa2000-10-28 14:19:58 +11003061 case SSH_SMSG_AGENT_OPEN:
3062 error("Warning: ssh server tried agent forwarding.");
3063 break;
3064 case SSH_SMSG_X11_OPEN:
3065 error("Warning: ssh server tried X11 forwarding.");
3066 break;
3067 default:
Damien Miller630d6f42002-01-22 23:17:30 +11003068 error("deny_input_open: type %d", type);
Damien Miller69b69aa2000-10-28 14:19:58 +11003069 break;
3070 }
Damien Miller5eb137c2005-12-31 16:19:53 +11003071 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller69b69aa2000-10-28 14:19:58 +11003072 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
3073 packet_put_int(rchan);
3074 packet_send();
3075}
3076
Damien Miller5428f641999-11-25 11:54:57 +11003077/*
3078 * Requests forwarding of X11 connections, generates fake authentication
3079 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00003080 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11003081 */
Damien Miller4af51302000-04-16 11:18:38 +10003082void
Damien Miller17e7ed02005-06-17 12:54:33 +10003083x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Millerbd483e72000-04-30 10:00:53 +10003084 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003085{
Ben Lindstrom46c16222000-12-22 01:43:59 +00003086 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10003087 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11003088 char *new_data;
3089 int screen_number;
3090 const char *cp;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10003091 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003092
Damien Millerf92c0792005-07-06 09:45:26 +10003093 if (x11_saved_display == NULL)
3094 x11_saved_display = xstrdup(disp);
3095 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10003096 error("x11_request_forwarding_with_spoofing: different "
3097 "$DISPLAY already forwarded");
3098 return;
3099 }
3100
Damien Miller17e7ed02005-06-17 12:54:33 +10003101 cp = disp;
3102 if (disp)
3103 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11003104 if (cp)
3105 cp = strchr(cp, '.');
3106 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11003107 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11003108 else
3109 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003110
Damien Miller13390022005-07-06 09:44:19 +10003111 if (x11_saved_proto == NULL) {
3112 /* Save protocol name. */
3113 x11_saved_proto = xstrdup(proto);
3114 /*
Damien Miller46d38de2005-07-17 17:02:09 +10003115 * Extract real authentication data and generate fake data
Damien Miller13390022005-07-06 09:44:19 +10003116 * of the same length.
3117 */
3118 x11_saved_data = xmalloc(data_len);
3119 x11_fake_data = xmalloc(data_len);
3120 for (i = 0; i < data_len; i++) {
3121 if (sscanf(data + 2 * i, "%2x", &value) != 1)
3122 fatal("x11_request_forwarding: bad "
3123 "authentication data: %.100s", data);
3124 if (i % 4 == 0)
3125 rnd = arc4random();
3126 x11_saved_data[i] = value;
3127 x11_fake_data[i] = rnd & 0xff;
3128 rnd >>= 8;
3129 }
3130 x11_saved_data_len = data_len;
3131 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11003132 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003133
Damien Miller95def091999-11-25 00:26:21 +11003134 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10003135 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003136
Damien Miller95def091999-11-25 00:26:21 +11003137 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10003138 if (compat20) {
3139 channel_request_start(client_session_id, "x11-req", 0);
3140 packet_put_char(0); /* XXX bool single connection */
3141 } else {
3142 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
3143 }
3144 packet_put_cstring(proto);
3145 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11003146 packet_put_int(screen_number);
3147 packet_send();
3148 packet_write_wait();
3149 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003150}
3151
Ben Lindstrome9c99912001-06-09 00:41:05 +00003152
3153/* -- agent forwarding */
3154
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003155/* Sends a message to the server to request authentication fd forwarding. */
3156
Damien Miller4af51302000-04-16 11:18:38 +10003157void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003158auth_request_forwarding(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003159{
Damien Miller95def091999-11-25 00:26:21 +11003160 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
3161 packet_send();
3162 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003163}