blob: 5796a8bb92c89b7bdc7938b673d85f4eca2c1000 [file] [log] [blame]
Damien Millerefc04e72006-07-10 20:26:27 +10001/* $OpenBSD: channels.c,v 1.251 2006/07/03 17:59:32 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains functions for generic socket connection forwarding.
7 * There is also code for initiating connection forwarding for X11 connections,
8 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Millera90fc082002-01-22 23:19:38 +110017 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 * Copyright (c) 1999 Dug Song. All rights reserved.
19 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Damien Miller17e91c02006-03-15 11:28:34 +110043
44#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110045#include <sys/types.h>
46#include <sys/un.h>
Damien Millerefc04e72006-07-10 20:26:27 +100047#include <sys/socket.h>
48
49#include <netinet/in.h>
50#include <arpa/inet.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110051
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 Millereccb9de2005-06-17 12:59:34 +10001017 u_int have, 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 }
1078 if (have < 4 + addrlen + 2)
1079 return 0;
1080 buffer_consume(&c->input, sizeof(s5_req));
1081 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1082 buffer_consume(&c->input, 1); /* host string length */
1083 buffer_get(&c->input, (char *)&dest_addr, addrlen);
1084 buffer_get(&c->input, (char *)&dest_port, 2);
1085 dest_addr[addrlen] = '\0';
1086 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
Darren Tucker1f8311c2004-05-13 16:39:33 +10001087 strlcpy(c->path, (char *)dest_addr, sizeof(c->path));
Darren Tucker46471c92003-07-03 13:55:19 +10001088 else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL)
1089 return -1;
1090 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001091
Damien Millerfbdeece2003-09-02 22:52:31 +10001092 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001093 c->self, c->path, c->host_port, s5_req.command);
1094
1095 s5_rsp.version = 0x05;
1096 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1097 s5_rsp.reserved = 0; /* ignored */
1098 s5_rsp.atyp = SSH_SOCKS5_IPV4;
1099 ((struct in_addr *)&dest_addr)->s_addr = INADDR_ANY;
1100 dest_port = 0; /* ignored */
1101
Damien Millera0fdce92006-03-26 14:28:50 +11001102 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
1103 buffer_append(&c->output, &dest_addr, sizeof(struct in_addr));
1104 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001105 return 1;
1106}
1107
Ben Lindstromb3921512001-04-11 15:57:50 +00001108/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001109static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001110channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001111{
1112 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001113 u_int have;
1114 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001115
1116 have = buffer_len(&c->input);
Damien Miller4623a752001-10-10 15:03:58 +10001117 c->delayed = 0;
Ben Lindstromb3921512001-04-11 15:57:50 +00001118 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001119 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001120 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001121 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001122 /* need more */
1123 FD_SET(c->sock, readset);
1124 return;
1125 }
1126 /* try to guess the protocol */
1127 p = buffer_ptr(&c->input);
1128 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001129 case 0x04:
1130 ret = channel_decode_socks4(c, readset, writeset);
1131 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001132 case 0x05:
1133 ret = channel_decode_socks5(c, readset, writeset);
1134 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001135 default:
1136 ret = -1;
1137 break;
1138 }
1139 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001140 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001141 } else if (ret == 0) {
1142 debug2("channel %d: pre_dynamic: need more", c->self);
1143 /* need more */
1144 FD_SET(c->sock, readset);
1145 } else {
1146 /* switch to the next state */
1147 c->type = SSH_CHANNEL_OPENING;
1148 port_open_helper(c, "direct-tcpip");
1149 }
1150}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001151
Damien Millerb38eff82000-04-01 11:09:21 +10001152/* This is our fake X11 server socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +00001153static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001154channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001155{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001156 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001157 struct sockaddr addr;
Damien Miller398e1cf2002-02-05 11:52:13 +11001158 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001159 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001160 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001161 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001162
1163 if (FD_ISSET(c->sock, readset)) {
1164 debug("X11 connection requested.");
1165 addrlen = sizeof(addr);
1166 newsock = accept(c->sock, &addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001167 if (c->single_connection) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001168 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001169 channel_close_fd(&c->sock);
1170 chan_mark_dead(c);
1171 }
Damien Millerb38eff82000-04-01 11:09:21 +10001172 if (newsock < 0) {
1173 error("accept: %.100s", strerror(errno));
1174 return;
1175 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001176 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001177 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001178 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001179 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001180 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001181
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001182 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001183 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001184 c->local_window_max, c->local_maxpacket, 0, buf, 1);
Damien Millerbd483e72000-04-30 10:00:53 +10001185 if (compat20) {
1186 packet_start(SSH2_MSG_CHANNEL_OPEN);
1187 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001188 packet_put_int(nc->self);
Damien Millere7378562001-12-21 14:58:35 +11001189 packet_put_int(nc->local_window_max);
1190 packet_put_int(nc->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +11001191 /* originator ipaddr and port */
1192 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +10001193 if (datafellows & SSH_BUG_X11FWD) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001194 debug2("ssh2 x11 bug compat mode");
Damien Miller30c3d422000-05-09 11:02:59 +10001195 } else {
1196 packet_put_int(remote_port);
1197 }
Damien Millerbd483e72000-04-30 10:00:53 +10001198 packet_send();
1199 } else {
1200 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001201 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001202 if (packet_get_protocol_flags() &
1203 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +00001204 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +10001205 packet_send();
1206 }
Damien Millerd83ff352001-01-30 09:19:34 +11001207 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001208 }
1209}
1210
Ben Lindstrombba81212001-06-25 05:01:22 +00001211static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001212port_open_helper(Channel *c, char *rtype)
1213{
1214 int direct;
1215 char buf[1024];
1216 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001217 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001218
1219 direct = (strcmp(rtype, "direct-tcpip") == 0);
1220
1221 snprintf(buf, sizeof buf,
1222 "%s: listening port %d for %.100s port %d, "
1223 "connect from %.200s port %d",
1224 rtype, c->listening_port, c->path, c->host_port,
1225 remote_ipaddr, remote_port);
1226
1227 xfree(c->remote_name);
1228 c->remote_name = xstrdup(buf);
1229
1230 if (compat20) {
1231 packet_start(SSH2_MSG_CHANNEL_OPEN);
1232 packet_put_cstring(rtype);
1233 packet_put_int(c->self);
1234 packet_put_int(c->local_window_max);
1235 packet_put_int(c->local_maxpacket);
1236 if (direct) {
1237 /* target host, port */
1238 packet_put_cstring(c->path);
1239 packet_put_int(c->host_port);
1240 } else {
1241 /* listen address, port */
1242 packet_put_cstring(c->path);
1243 packet_put_int(c->listening_port);
1244 }
1245 /* originator host and port */
1246 packet_put_cstring(remote_ipaddr);
Damien Miller677257f2005-06-17 12:55:03 +10001247 packet_put_int((u_int)remote_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001248 packet_send();
1249 } else {
1250 packet_start(SSH_MSG_PORT_OPEN);
1251 packet_put_int(c->self);
1252 packet_put_cstring(c->path);
1253 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001254 if (packet_get_protocol_flags() &
1255 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001256 packet_put_cstring(c->remote_name);
1257 packet_send();
1258 }
1259 xfree(remote_ipaddr);
1260}
1261
Damien Miller5e7fd072005-11-05 14:53:39 +11001262static void
1263channel_set_reuseaddr(int fd)
1264{
1265 int on = 1;
1266
1267 /*
1268 * Set socket options.
1269 * Allow local port reuse in TIME_WAIT.
1270 */
1271 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1272 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1273}
1274
Damien Millerb38eff82000-04-01 11:09:21 +10001275/*
1276 * This socket is listening for connections to a forwarded TCP/IP port.
1277 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001278static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001279channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001280{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001281 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001282 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001283 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001284 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001285 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001286
Damien Millerb38eff82000-04-01 11:09:21 +10001287 if (FD_ISSET(c->sock, readset)) {
1288 debug("Connection to port %d forwarding "
1289 "to %.100s port %d requested.",
1290 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001291
Damien Miller4623a752001-10-10 15:03:58 +10001292 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1293 nextstate = SSH_CHANNEL_OPENING;
1294 rtype = "forwarded-tcpip";
1295 } else {
1296 if (c->host_port == 0) {
1297 nextstate = SSH_CHANNEL_DYNAMIC;
Damien Millerd3c04b92001-10-10 15:04:20 +10001298 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001299 } else {
1300 nextstate = SSH_CHANNEL_OPENING;
1301 rtype = "direct-tcpip";
1302 }
1303 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001304
Damien Millerb38eff82000-04-01 11:09:21 +10001305 addrlen = sizeof(addr);
1306 newsock = accept(c->sock, &addr, &addrlen);
1307 if (newsock < 0) {
1308 error("accept: %.100s", strerror(errno));
1309 return;
1310 }
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00001311 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001312 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1313 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001314 nc->listening_port = c->listening_port;
1315 nc->host_port = c->host_port;
1316 strlcpy(nc->path, c->path, sizeof(nc->path));
1317
Damien Miller4623a752001-10-10 15:03:58 +10001318 if (nextstate == SSH_CHANNEL_DYNAMIC) {
1319 /*
1320 * do not call the channel_post handler until
1321 * this flag has been reset by a pre-handler.
1322 * otherwise the FD_ISSET calls might overflow
1323 */
1324 nc->delayed = 1;
1325 } else {
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001326 port_open_helper(nc, rtype);
Damien Miller4623a752001-10-10 15:03:58 +10001327 }
Damien Millerb38eff82000-04-01 11:09:21 +10001328 }
1329}
1330
1331/*
1332 * This is the authentication agent socket listening for connections from
1333 * clients.
1334 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001335static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001336channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001337{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001338 Channel *nc;
1339 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001340 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001341 socklen_t addrlen;
1342
1343 if (FD_ISSET(c->sock, readset)) {
1344 addrlen = sizeof(addr);
1345 newsock = accept(c->sock, &addr, &addrlen);
1346 if (newsock < 0) {
1347 error("accept from auth socket: %.100s", strerror(errno));
1348 return;
1349 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001350 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001351 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1352 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001353 0, "accepted auth socket", 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001354 if (compat20) {
1355 packet_start(SSH2_MSG_CHANNEL_OPEN);
1356 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001357 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001358 packet_put_int(c->local_window_max);
1359 packet_put_int(c->local_maxpacket);
1360 } else {
1361 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001362 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001363 }
Damien Millerb38eff82000-04-01 11:09:21 +10001364 packet_send();
1365 }
1366}
1367
Ben Lindstrombba81212001-06-25 05:01:22 +00001368static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001369channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001370{
Ben Lindstrom69128662001-05-08 20:07:39 +00001371 int err = 0;
Ben Lindstrom11180952001-07-04 05:13:35 +00001372 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001373
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001374 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001375 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001376 err = errno;
1377 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001378 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001379 if (err == 0) {
1380 debug("channel %d: connected", c->self);
1381 c->type = SSH_CHANNEL_OPEN;
1382 if (compat20) {
1383 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1384 packet_put_int(c->remote_id);
1385 packet_put_int(c->self);
1386 packet_put_int(c->local_window);
1387 packet_put_int(c->local_maxpacket);
1388 } else {
1389 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1390 packet_put_int(c->remote_id);
1391 packet_put_int(c->self);
1392 }
1393 } else {
1394 debug("channel %d: not connected: %s",
1395 c->self, strerror(err));
1396 if (compat20) {
1397 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1398 packet_put_int(c->remote_id);
1399 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1400 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1401 packet_put_cstring(strerror(err));
1402 packet_put_cstring("");
1403 }
1404 } else {
1405 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1406 packet_put_int(c->remote_id);
1407 }
1408 chan_mark_dead(c);
1409 }
1410 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001411 }
1412}
1413
Ben Lindstrombba81212001-06-25 05:01:22 +00001414static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001415channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001416{
Darren Tucker11327cc2005-03-14 23:22:25 +11001417 char buf[CHAN_RBUF];
Damien Millerb38eff82000-04-01 11:09:21 +10001418 int len;
1419
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001420 if (c->rfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001421 FD_ISSET(c->rfd, readset)) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001422 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001423 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +10001424 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1425 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001426#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001427 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001428#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001429 if ((!c->isatty && len <= 0) ||
1430 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001431#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001432 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001433 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001434 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001435 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001436 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001437 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001438 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001439 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001440 c->type = SSH_CHANNEL_INPUT_DRAINING;
Damien Millerfbdeece2003-09-02 22:52:31 +10001441 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001442 } else {
1443 chan_read_failed(c);
1444 }
1445 return -1;
1446 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001447 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001448 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001449 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001450 chan_read_failed(c);
1451 }
Damien Millerd27b9472005-12-13 19:29:02 +11001452 } else if (c->datagram) {
1453 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001454 } else {
1455 buffer_append(&c->input, buf, len);
1456 }
Damien Millerb38eff82000-04-01 11:09:21 +10001457 }
1458 return 1;
1459}
Damien Miller4f7becb2006-03-26 14:10:14 +11001460
Ben Lindstrombba81212001-06-25 05:01:22 +00001461static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001462channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001463{
Ben Lindstrome229b252001-03-05 06:28:06 +00001464 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001465 u_char *data = NULL, *buf;
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001466 u_int dlen;
Damien Millerb38eff82000-04-01 11:09:21 +10001467 int len;
1468
1469 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001470 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001471 FD_ISSET(c->wfd, writeset) &&
1472 buffer_len(&c->output) > 0) {
Damien Miller077b2382005-12-31 16:22:32 +11001473 if (c->output_filter != NULL) {
1474 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1475 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001476 if (c->type != SSH_CHANNEL_OPEN)
1477 chan_mark_dead(c);
1478 else
1479 chan_write_failed(c);
1480 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001481 }
1482 } else if (c->datagram) {
1483 buf = data = buffer_get_string(&c->output, &dlen);
1484 } else {
1485 buf = data = buffer_ptr(&c->output);
1486 dlen = buffer_len(&c->output);
1487 }
1488
Damien Millerd27b9472005-12-13 19:29:02 +11001489 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001490 /* ignore truncated writes, datagrams might get lost */
1491 c->local_consumed += dlen + 4;
Damien Miller077b2382005-12-31 16:22:32 +11001492 len = write(c->wfd, buf, dlen);
Damien Millerd27b9472005-12-13 19:29:02 +11001493 xfree(data);
1494 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1495 return 1;
1496 if (len <= 0) {
1497 if (c->type != SSH_CHANNEL_OPEN)
1498 chan_mark_dead(c);
1499 else
1500 chan_write_failed(c);
1501 return -1;
1502 }
1503 return 1;
1504 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001505#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001506 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker240fdfa2003-11-22 14:10:02 +11001507 if (compat20 && c->wfd_isatty)
1508 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001509#endif
Damien Miller077b2382005-12-31 16:22:32 +11001510
1511 len = write(c->wfd, buf, dlen);
Damien Miller6f83b8e2000-05-02 09:23:45 +10001512 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1513 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001514 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001515 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001516 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001517 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001518 return -1;
1519 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001520 buffer_clear(&c->output);
Damien Millerfbdeece2003-09-02 22:52:31 +10001521 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001522 c->type = SSH_CHANNEL_INPUT_DRAINING;
1523 } else {
1524 chan_write_failed(c);
1525 }
1526 return -1;
1527 }
Damien Miller077b2382005-12-31 16:22:32 +11001528 if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001529 if (tcgetattr(c->wfd, &tio) == 0 &&
1530 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1531 /*
1532 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001533 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001534 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001535 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001536 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001537 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001538 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001539 }
1540 }
Damien Millerb38eff82000-04-01 11:09:21 +10001541 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001542 if (compat20 && len > 0) {
1543 c->local_consumed += len;
1544 }
1545 }
1546 return 1;
1547}
Damien Miller4f7becb2006-03-26 14:10:14 +11001548
Ben Lindstrombba81212001-06-25 05:01:22 +00001549static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001550channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001551{
Darren Tucker11327cc2005-03-14 23:22:25 +11001552 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001553 int len;
1554
Damien Miller1383bd82000-04-06 12:32:37 +10001555/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001556 if (c->efd != -1) {
1557 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1558 FD_ISSET(c->efd, writeset) &&
1559 buffer_len(&c->extended) > 0) {
1560 len = write(c->efd, buffer_ptr(&c->extended),
1561 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001562 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001563 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001564 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1565 return 1;
1566 if (len <= 0) {
1567 debug2("channel %d: closing write-efd %d",
1568 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001569 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001570 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001571 buffer_consume(&c->extended, len);
1572 c->local_consumed += len;
1573 }
1574 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1575 FD_ISSET(c->efd, readset)) {
1576 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001577 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001578 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001579 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1580 return 1;
1581 if (len <= 0) {
1582 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001583 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001584 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001585 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001586 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001587 }
Damien Miller33b13562000-04-04 14:38:59 +10001588 }
1589 }
1590 return 1;
1591}
Damien Miller4f7becb2006-03-26 14:10:14 +11001592
Ben Lindstrombba81212001-06-25 05:01:22 +00001593static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001594channel_handle_ctl(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller0e220db2004-06-15 10:34:08 +10001595{
1596 char buf[16];
1597 int len;
1598
1599 /* Monitor control fd to detect if the slave client exits */
1600 if (c->ctl_fd != -1 && FD_ISSET(c->ctl_fd, readset)) {
1601 len = read(c->ctl_fd, buf, sizeof(buf));
1602 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1603 return 1;
1604 if (len <= 0) {
1605 debug2("channel %d: ctl read<=0", c->self);
1606 if (c->type != SSH_CHANNEL_OPEN) {
1607 debug2("channel %d: not open", c->self);
1608 chan_mark_dead(c);
1609 return -1;
1610 } else {
1611 chan_read_failed(c);
1612 chan_write_failed(c);
1613 }
1614 return -1;
1615 } else
1616 fatal("%s: unexpected data on ctl fd", __func__);
1617 }
1618 return 1;
1619}
Damien Miller4f7becb2006-03-26 14:10:14 +11001620
Damien Miller0e220db2004-06-15 10:34:08 +10001621static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001622channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001623{
Ben Lindstromb3921512001-04-11 15:57:50 +00001624 if (c->type == SSH_CHANNEL_OPEN &&
1625 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001626 c->local_window < c->local_window_max/2 &&
1627 c->local_consumed > 0) {
1628 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1629 packet_put_int(c->remote_id);
1630 packet_put_int(c->local_consumed);
1631 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001632 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001633 c->self, c->local_window,
1634 c->local_consumed);
1635 c->local_window += c->local_consumed;
1636 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001637 }
1638 return 1;
1639}
1640
Ben Lindstrombba81212001-06-25 05:01:22 +00001641static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001642channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001643{
Damien Miller4623a752001-10-10 15:03:58 +10001644 if (c->delayed)
1645 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001646 channel_handle_rfd(c, readset, writeset);
1647 channel_handle_wfd(c, readset, writeset);
Damien Millerde6987c2002-01-22 23:20:40 +11001648 if (!compat20)
Damien Miller4623a752001-10-10 15:03:58 +10001649 return;
Damien Miller33b13562000-04-04 14:38:59 +10001650 channel_handle_efd(c, readset, writeset);
Damien Miller0e220db2004-06-15 10:34:08 +10001651 channel_handle_ctl(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001652 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001653}
1654
Ben Lindstrombba81212001-06-25 05:01:22 +00001655static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001656channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001657{
1658 int len;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001659
Damien Millerb38eff82000-04-01 11:09:21 +10001660 /* Send buffered output data to the socket. */
1661 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1662 len = write(c->sock, buffer_ptr(&c->output),
1663 buffer_len(&c->output));
1664 if (len <= 0)
Damien Miller76765c02002-01-22 23:21:15 +11001665 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001666 else
1667 buffer_consume(&c->output, len);
1668 }
1669}
1670
Ben Lindstrombba81212001-06-25 05:01:22 +00001671static void
Damien Miller33b13562000-04-04 14:38:59 +10001672channel_handler_init_20(void)
1673{
Damien Millerde6987c2002-01-22 23:20:40 +11001674 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001675 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001676 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001677 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001678 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001679 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001680 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001681 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001682
Damien Millerde6987c2002-01-22 23:20:40 +11001683 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001684 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001685 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001686 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001687 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001688 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001689 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001690}
1691
Ben Lindstrombba81212001-06-25 05:01:22 +00001692static void
Damien Millerb38eff82000-04-01 11:09:21 +10001693channel_handler_init_13(void)
1694{
1695 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1696 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1697 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1698 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1699 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1700 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1701 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001702 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001703 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001704
Damien Millerde6987c2002-01-22 23:20:40 +11001705 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001706 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1707 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1708 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1709 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001710 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001711 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001712}
1713
Ben Lindstrombba81212001-06-25 05:01:22 +00001714static void
Damien Millerb38eff82000-04-01 11:09:21 +10001715channel_handler_init_15(void)
1716{
Damien Millerde6987c2002-01-22 23:20:40 +11001717 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001718 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001719 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1720 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1721 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001722 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001723 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001724
1725 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1726 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1727 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Damien Millerde6987c2002-01-22 23:20:40 +11001728 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001729 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001730 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001731}
1732
Ben Lindstrombba81212001-06-25 05:01:22 +00001733static void
Damien Millerb38eff82000-04-01 11:09:21 +10001734channel_handler_init(void)
1735{
1736 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001737
Damien Miller9f0f5c62001-12-21 14:45:46 +11001738 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001739 channel_pre[i] = NULL;
1740 channel_post[i] = NULL;
1741 }
Damien Miller33b13562000-04-04 14:38:59 +10001742 if (compat20)
1743 channel_handler_init_20();
1744 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001745 channel_handler_init_13();
1746 else
1747 channel_handler_init_15();
1748}
1749
Damien Miller3ec27592001-10-12 11:35:04 +10001750/* gc dead channels */
1751static void
1752channel_garbage_collect(Channel *c)
1753{
1754 if (c == NULL)
1755 return;
1756 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11001757 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10001758 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001759 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001760 c->detach_user(c->self, NULL);
1761 /* if we still have a callback */
1762 if (c->detach_user != NULL)
1763 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001764 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001765 }
1766 if (!chan_is_dead(c, 1))
1767 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001768 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001769 channel_free(c);
1770}
1771
Ben Lindstrombba81212001-06-25 05:01:22 +00001772static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001773channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001774{
1775 static int did_init = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001776 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001777 Channel *c;
1778
1779 if (!did_init) {
1780 channel_handler_init();
1781 did_init = 1;
1782 }
1783 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001784 c = channels[i];
1785 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001786 continue;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001787 if (ftab[c->type] != NULL)
1788 (*ftab[c->type])(c, readset, writeset);
Damien Miller3ec27592001-10-12 11:35:04 +10001789 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001790 }
1791}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001792
Ben Lindstrome9c99912001-06-09 00:41:05 +00001793/*
1794 * Allocate/update select bitmasks and add any bits relevant to channels in
1795 * select bitmasks.
1796 */
Damien Miller4af51302000-04-16 11:18:38 +10001797void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001798channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001799 u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001800{
Damien Miller36812092006-03-26 14:22:47 +11001801 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11001802
1803 n = MAX(*maxfdp, channel_max_fd);
1804
Damien Miller36812092006-03-26 14:22:47 +11001805 nfdset = howmany(n+1, NFDBITS);
1806 /* Explicitly test here, because xrealloc isn't always called */
1807 if (nfdset && SIZE_T_MAX / nfdset < sizeof(fd_mask))
1808 fatal("channel_prepare_select: max_fd (%d) is too large", n);
1809 sz = nfdset * sizeof(fd_mask);
1810
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001811 /* perhaps check sz < nalloc/2 and shrink? */
1812 if (*readsetp == NULL || sz > *nallocp) {
Damien Miller36812092006-03-26 14:22:47 +11001813 *readsetp = xrealloc(*readsetp, nfdset, sizeof(fd_mask));
1814 *writesetp = xrealloc(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001815 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11001816 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001817 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11001818 memset(*readsetp, 0, sz);
1819 memset(*writesetp, 0, sz);
1820
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001821 if (!rekeying)
1822 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001823}
1824
Ben Lindstrome9c99912001-06-09 00:41:05 +00001825/*
1826 * After select, perform any appropriate operations for channels which have
1827 * events pending.
1828 */
Damien Miller4af51302000-04-16 11:18:38 +10001829void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001830channel_after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001831{
Damien Millerb38eff82000-04-01 11:09:21 +10001832 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001833}
1834
Ben Lindstrome9c99912001-06-09 00:41:05 +00001835
Damien Miller5e953212001-01-30 09:14:00 +11001836/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10001837void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001838channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001839{
Damien Millerb38eff82000-04-01 11:09:21 +10001840 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001841 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001842
Damien Miller95def091999-11-25 00:26:21 +11001843 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001844 c = channels[i];
1845 if (c == NULL)
1846 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001847
Ben Lindstrome9c99912001-06-09 00:41:05 +00001848 /*
1849 * We are only interested in channels that can have buffered
1850 * incoming data.
1851 */
Damien Miller34132e52000-01-14 15:45:46 +11001852 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001853 if (c->type != SSH_CHANNEL_OPEN &&
1854 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001855 continue;
1856 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001857 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001858 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001859 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001860 if (compat20 &&
1861 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001862 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10001863 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001864 continue;
1865 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001866
Damien Miller95def091999-11-25 00:26:21 +11001867 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001868 if ((c->istate == CHAN_INPUT_OPEN ||
1869 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1870 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001871 if (c->datagram) {
1872 if (len > 0) {
1873 u_char *data;
1874 u_int dlen;
1875
1876 data = buffer_get_string(&c->input,
1877 &dlen);
1878 packet_start(SSH2_MSG_CHANNEL_DATA);
1879 packet_put_int(c->remote_id);
1880 packet_put_string(data, dlen);
1881 packet_send();
1882 c->remote_window -= dlen + 4;
1883 xfree(data);
1884 }
1885 continue;
1886 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001887 /*
1888 * Send some data for the other side over the secure
1889 * connection.
1890 */
Damien Miller33b13562000-04-04 14:38:59 +10001891 if (compat20) {
1892 if (len > c->remote_window)
1893 len = c->remote_window;
1894 if (len > c->remote_maxpacket)
1895 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001896 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001897 if (packet_is_interactive()) {
1898 if (len > 1024)
1899 len = 512;
1900 } else {
1901 /* Keep the packets at reasonable size. */
1902 if (len > packet_get_maxsize()/2)
1903 len = packet_get_maxsize()/2;
1904 }
Damien Miller95def091999-11-25 00:26:21 +11001905 }
Damien Millerb38eff82000-04-01 11:09:21 +10001906 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001907 packet_start(compat20 ?
1908 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001909 packet_put_int(c->remote_id);
1910 packet_put_string(buffer_ptr(&c->input), len);
1911 packet_send();
1912 buffer_consume(&c->input, len);
1913 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001914 }
1915 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001916 if (compat13)
1917 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001918 /*
1919 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00001920 * tell peer, that we will not send more data: send IEOF.
1921 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11001922 */
Ben Lindstromcf159442002-03-26 03:26:24 +00001923 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10001924 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
1925 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00001926 else
1927 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001928 }
Damien Miller33b13562000-04-04 14:38:59 +10001929 /* Send extended data, i.e. stderr */
1930 if (compat20 &&
Ben Lindstromcf159442002-03-26 03:26:24 +00001931 !(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10001932 c->remote_window > 0 &&
1933 (len = buffer_len(&c->extended)) > 0 &&
1934 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001935 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001936 c->self, c->remote_window, buffer_len(&c->extended),
1937 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001938 if (len > c->remote_window)
1939 len = c->remote_window;
1940 if (len > c->remote_maxpacket)
1941 len = c->remote_maxpacket;
1942 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1943 packet_put_int(c->remote_id);
1944 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1945 packet_put_string(buffer_ptr(&c->extended), len);
1946 packet_send();
1947 buffer_consume(&c->extended, len);
1948 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001949 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001950 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001951 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001952}
1953
Ben Lindstrome9c99912001-06-09 00:41:05 +00001954
1955/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11001956
1957/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10001958void
Damien Miller630d6f42002-01-22 23:17:30 +11001959channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001960{
Damien Miller34132e52000-01-14 15:45:46 +11001961 int id;
Damien Miller95def091999-11-25 00:26:21 +11001962 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001963 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001964 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001965
Damien Miller95def091999-11-25 00:26:21 +11001966 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001967 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001968 c = channel_lookup(id);
1969 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001970 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001971
Damien Miller95def091999-11-25 00:26:21 +11001972 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001973 if (c->type != SSH_CHANNEL_OPEN &&
1974 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001975 return;
1976
Damien Miller95def091999-11-25 00:26:21 +11001977 /* Get the data. */
1978 data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001979
Damien Millera04ad492004-01-21 11:02:09 +11001980 /*
1981 * Ignore data for protocol > 1.3 if output end is no longer open.
1982 * For protocol 2 the sending side is reducing its window as it sends
1983 * data, so we must 'fake' consumption of the data in order to ensure
1984 * that window updates are sent back. Otherwise the connection might
1985 * deadlock.
1986 */
1987 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
1988 if (compat20) {
1989 c->local_window -= data_len;
1990 c->local_consumed += data_len;
1991 }
1992 xfree(data);
1993 return;
1994 }
1995
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001996 if (compat20) {
Damien Miller33b13562000-04-04 14:38:59 +10001997 if (data_len > c->local_maxpacket) {
Damien Miller996acd22003-04-09 20:59:48 +10001998 logit("channel %d: rcvd big packet %d, maxpack %d",
Damien Miller33b13562000-04-04 14:38:59 +10001999 c->self, data_len, c->local_maxpacket);
2000 }
2001 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002002 logit("channel %d: rcvd too much data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002003 c->self, data_len, c->local_window);
2004 xfree(data);
2005 return;
2006 }
2007 c->local_window -= data_len;
Damien Miller33b13562000-04-04 14:38:59 +10002008 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002009 packet_check_eom();
Damien Millerd27b9472005-12-13 19:29:02 +11002010 if (c->datagram)
2011 buffer_put_string(&c->output, data, data_len);
2012 else
2013 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11002014 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002015}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002016
Damien Millerd79b4242006-03-31 23:11:44 +11002017/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002018void
Damien Miller630d6f42002-01-22 23:17:30 +11002019channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002020{
2021 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002022 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002023 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002024 Channel *c;
2025
2026 /* Get the channel number and verify it. */
2027 id = packet_get_int();
2028 c = channel_lookup(id);
2029
2030 if (c == NULL)
2031 packet_disconnect("Received extended_data for bad channel %d.", id);
2032 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002033 logit("channel %d: ext data for non open", id);
Damien Miller33b13562000-04-04 14:38:59 +10002034 return;
2035 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002036 if (c->flags & CHAN_EOF_RCVD) {
2037 if (datafellows & SSH_BUG_EXTEOF)
2038 debug("channel %d: accepting ext data after eof", id);
2039 else
2040 packet_disconnect("Received extended_data after EOF "
2041 "on channel %d.", id);
2042 }
Damien Miller33b13562000-04-04 14:38:59 +10002043 tcode = packet_get_int();
2044 if (c->efd == -1 ||
2045 c->extended_usage != CHAN_EXTENDED_WRITE ||
2046 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002047 logit("channel %d: bad ext data", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002048 return;
2049 }
2050 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002051 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002052 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002053 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002054 c->self, data_len, c->local_window);
2055 xfree(data);
2056 return;
2057 }
Damien Millerd3444942000-09-30 14:20:03 +11002058 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002059 c->local_window -= data_len;
2060 buffer_append(&c->extended, data, data_len);
2061 xfree(data);
2062}
2063
Damien Millerd79b4242006-03-31 23:11:44 +11002064/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002065void
Damien Miller630d6f42002-01-22 23:17:30 +11002066channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002067{
2068 int id;
2069 Channel *c;
2070
Damien Millerb38eff82000-04-01 11:09:21 +10002071 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002072 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002073 c = channel_lookup(id);
2074 if (c == NULL)
2075 packet_disconnect("Received ieof for nonexistent channel %d.", id);
2076 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002077
2078 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002079 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002080 debug("channel %d: FORCE input drain", c->self);
2081 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002082 if (buffer_len(&c->input) == 0)
2083 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002084 }
2085
Damien Millerb38eff82000-04-01 11:09:21 +10002086}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002087
Damien Millerd79b4242006-03-31 23:11:44 +11002088/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002089void
Damien Miller630d6f42002-01-22 23:17:30 +11002090channel_input_close(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002091{
Damien Millerb38eff82000-04-01 11:09:21 +10002092 int id;
2093 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002094
Damien Millerb38eff82000-04-01 11:09:21 +10002095 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002096 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002097 c = channel_lookup(id);
2098 if (c == NULL)
2099 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11002100
2101 /*
2102 * Send a confirmation that we have closed the channel and no more
2103 * data is coming for it.
2104 */
Damien Miller95def091999-11-25 00:26:21 +11002105 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10002106 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11002107 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002108
Damien Miller5428f641999-11-25 11:54:57 +11002109 /*
2110 * If the channel is in closed state, we have sent a close request,
2111 * and the other side will eventually respond with a confirmation.
2112 * Thus, we cannot free the channel here, because then there would be
2113 * no-one to receive the confirmation. The channel gets freed when
2114 * the confirmation arrives.
2115 */
Damien Millerb38eff82000-04-01 11:09:21 +10002116 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11002117 /*
2118 * Not a closed channel - mark it as draining, which will
2119 * cause it to be freed later.
2120 */
Damien Miller76765c02002-01-22 23:21:15 +11002121 buffer_clear(&c->input);
Damien Millerb38eff82000-04-01 11:09:21 +10002122 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11002123 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002124}
2125
Damien Millerb38eff82000-04-01 11:09:21 +10002126/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Millerd79b4242006-03-31 23:11:44 +11002127/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002128void
Damien Miller630d6f42002-01-22 23:17:30 +11002129channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002130{
Damien Millerb38eff82000-04-01 11:09:21 +10002131 int id = packet_get_int();
2132 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002133
Damien Miller48b03fc2002-01-22 23:11:40 +11002134 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002135 if (c == NULL)
2136 packet_disconnect("Received oclose for nonexistent channel %d.", id);
2137 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002138}
2139
Damien Millerd79b4242006-03-31 23:11:44 +11002140/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002141void
Damien Miller630d6f42002-01-22 23:17:30 +11002142channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002143{
2144 int id = packet_get_int();
2145 Channel *c = channel_lookup(id);
2146
Damien Miller48b03fc2002-01-22 23:11:40 +11002147 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002148 if (c == NULL)
2149 packet_disconnect("Received close confirmation for "
2150 "out-of-range channel %d.", id);
2151 if (c->type != SSH_CHANNEL_CLOSED)
2152 packet_disconnect("Received close confirmation for "
2153 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002154 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002155}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002156
Damien Millerd79b4242006-03-31 23:11:44 +11002157/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002158void
Damien Miller630d6f42002-01-22 23:17:30 +11002159channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002160{
Damien Millerb38eff82000-04-01 11:09:21 +10002161 int id, remote_id;
2162 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002163
Damien Millerb38eff82000-04-01 11:09:21 +10002164 id = packet_get_int();
2165 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002166
Damien Millerb38eff82000-04-01 11:09:21 +10002167 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2168 packet_disconnect("Received open confirmation for "
2169 "non-opening channel %d.", id);
2170 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002171 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002172 c->remote_id = remote_id;
2173 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002174
2175 if (compat20) {
2176 c->remote_window = packet_get_int();
2177 c->remote_maxpacket = packet_get_int();
Damien Miller67f0bc02002-02-05 12:23:08 +11002178 if (c->confirm) {
Damien Millerd3444942000-09-30 14:20:03 +11002179 debug2("callback start");
Damien Miller0e220db2004-06-15 10:34:08 +10002180 c->confirm(c->self, c->confirm_ctx);
Damien Millerd3444942000-09-30 14:20:03 +11002181 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002182 }
Damien Millerfbdeece2003-09-02 22:52:31 +10002183 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
Damien Miller33b13562000-04-04 14:38:59 +10002184 c->remote_window, c->remote_maxpacket);
2185 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002186 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002187}
2188
Ben Lindstrombba81212001-06-25 05:01:22 +00002189static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002190reason2txt(int reason)
2191{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002192 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002193 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2194 return "administratively prohibited";
2195 case SSH2_OPEN_CONNECT_FAILED:
2196 return "connect failed";
2197 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2198 return "unknown channel type";
2199 case SSH2_OPEN_RESOURCE_SHORTAGE:
2200 return "resource shortage";
2201 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002202 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002203}
2204
Damien Millerd79b4242006-03-31 23:11:44 +11002205/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002206void
Damien Miller630d6f42002-01-22 23:17:30 +11002207channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002208{
Kevin Steves12057502001-02-05 14:54:34 +00002209 int id, reason;
2210 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002211 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002212
Damien Millerb38eff82000-04-01 11:09:21 +10002213 id = packet_get_int();
2214 c = channel_lookup(id);
2215
2216 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2217 packet_disconnect("Received open failure for "
2218 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002219 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00002220 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00002221 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00002222 msg = packet_get_string(NULL);
2223 lang = packet_get_string(NULL);
2224 }
Damien Miller996acd22003-04-09 20:59:48 +10002225 logit("channel %d: open failed: %s%s%s", id,
Ben Lindstrom69128662001-05-08 20:07:39 +00002226 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00002227 if (msg != NULL)
2228 xfree(msg);
2229 if (lang != NULL)
2230 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10002231 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002232 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11002233 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002234 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002235}
2236
Damien Millerd79b4242006-03-31 23:11:44 +11002237/* ARGSUSED */
Damien Miller33b13562000-04-04 14:38:59 +10002238void
Damien Miller630d6f42002-01-22 23:17:30 +11002239channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002240{
2241 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002242 int id;
2243 u_int adjust;
Damien Miller33b13562000-04-04 14:38:59 +10002244
2245 if (!compat20)
2246 return;
2247
2248 /* Get the channel number and verify it. */
2249 id = packet_get_int();
2250 c = channel_lookup(id);
2251
Damien Millerd47c62a2005-12-13 19:33:57 +11002252 if (c == NULL) {
2253 logit("Received window adjust for non-open channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002254 return;
2255 }
2256 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002257 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002258 debug2("channel %d: rcvd adjust %u", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10002259 c->remote_window += adjust;
2260}
2261
Damien Millerd79b4242006-03-31 23:11:44 +11002262/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002263void
Damien Miller630d6f42002-01-22 23:17:30 +11002264channel_input_port_open(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002265{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002266 Channel *c = NULL;
2267 u_short host_port;
2268 char *host, *originator_string;
2269 int remote_id, sock = -1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002270
Ben Lindstrome9c99912001-06-09 00:41:05 +00002271 remote_id = packet_get_int();
2272 host = packet_get_string(NULL);
2273 host_port = packet_get_int();
2274
2275 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2276 originator_string = packet_get_string(NULL);
2277 } else {
2278 originator_string = xstrdup("unknown (remote did not supply name)");
2279 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002280 packet_check_eom();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002281 sock = channel_connect_to(host, host_port);
2282 if (sock != -1) {
2283 c = channel_new("connected socket",
2284 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2285 originator_string, 1);
Damien Miller699d0032002-02-08 22:07:16 +11002286 c->remote_id = remote_id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002287 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002288 xfree(originator_string);
Ben Lindstrome9c99912001-06-09 00:41:05 +00002289 if (c == NULL) {
2290 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2291 packet_put_int(remote_id);
2292 packet_send();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002293 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002294 xfree(host);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002295}
2296
2297
Ben Lindstrome9c99912001-06-09 00:41:05 +00002298/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002299
Ben Lindstrom908afed2001-10-03 17:34:59 +00002300void
2301channel_set_af(int af)
2302{
2303 IPv4or6 = af;
2304}
2305
Damien Millerb16461c2002-01-22 23:29:22 +11002306static int
2307channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
2308 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002309{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002310 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11002311 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11002312 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002313 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11002314 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002315
Damien Millerb16461c2002-01-22 23:29:22 +11002316 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2317 listen_addr : host_to_connect;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002318 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00002319
Damien Millerb16461c2002-01-22 23:29:22 +11002320 if (host == NULL) {
2321 error("No forward host name.");
Damien Millera7270302005-07-06 09:36:05 +10002322 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002323 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002324 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00002325 error("Forward host name too long.");
Damien Millera7270302005-07-06 09:36:05 +10002326 return 0;
Kevin Steves12057502001-02-05 14:54:34 +00002327 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002328
Damien Miller5428f641999-11-25 11:54:57 +11002329 /*
Damien Millerf91ee4c2005-03-01 21:24:33 +11002330 * Determine whether or not a port forward listens to loopback,
Darren Tucker47eede72005-03-14 23:08:12 +11002331 * specified address or wildcard. On the client, a specified bind
2332 * address will always override gateway_ports. On the server, a
2333 * gateway_ports of 1 (``yes'') will override the client's
2334 * specification and force a wildcard bind, whereas a value of 2
2335 * (``clientspecified'') will bind to whatever address the client
Damien Millerf91ee4c2005-03-01 21:24:33 +11002336 * asked for.
2337 *
2338 * Special-case listen_addrs are:
2339 *
2340 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2341 * "" (empty string), "*" -> wildcard v4/v6
2342 * "localhost" -> loopback v4/v6
2343 */
2344 addr = NULL;
2345 if (listen_addr == NULL) {
2346 /* No address specified: default to gateway_ports setting */
2347 if (gateway_ports)
2348 wildcard = 1;
2349 } else if (gateway_ports || is_client) {
2350 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2351 strcmp(listen_addr, "0.0.0.0") == 0) ||
2352 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
2353 (!is_client && gateway_ports == 1))
2354 wildcard = 1;
2355 else if (strcmp(listen_addr, "localhost") != 0)
2356 addr = listen_addr;
2357 }
2358
2359 debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s",
2360 type, wildcard, (addr == NULL) ? "NULL" : addr);
2361
2362 /*
Damien Miller34132e52000-01-14 15:45:46 +11002363 * getaddrinfo returns a loopback address if the hostname is
2364 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002365 */
Damien Miller34132e52000-01-14 15:45:46 +11002366 memset(&hints, 0, sizeof(hints));
2367 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002368 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002369 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002370 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002371 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2372 if (addr == NULL) {
2373 /* This really shouldn't happen */
2374 packet_disconnect("getaddrinfo: fatal error: %s",
2375 gai_strerror(r));
2376 } else {
Damien Millera7270302005-07-06 09:36:05 +10002377 error("channel_setup_fwd_listener: "
Damien Millerf91ee4c2005-03-01 21:24:33 +11002378 "getaddrinfo(%.64s): %s", addr, gai_strerror(r));
2379 }
Damien Millera7270302005-07-06 09:36:05 +10002380 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002381 }
Damien Miller5428f641999-11-25 11:54:57 +11002382
Damien Miller34132e52000-01-14 15:45:46 +11002383 for (ai = aitop; ai; ai = ai->ai_next) {
2384 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2385 continue;
2386 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2387 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Millerb16461c2002-01-22 23:29:22 +11002388 error("channel_setup_fwd_listener: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002389 continue;
2390 }
2391 /* Create a port to listen for the host. */
Damien Miller2372ace2003-05-14 13:42:23 +10002392 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002393 if (sock < 0) {
2394 /* this is no error since kernel may not support ipv6 */
2395 verbose("socket: %.100s", strerror(errno));
2396 continue;
2397 }
Damien Miller5e7fd072005-11-05 14:53:39 +11002398
2399 channel_set_reuseaddr(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10002400
Damien Miller34132e52000-01-14 15:45:46 +11002401 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002402
Damien Miller34132e52000-01-14 15:45:46 +11002403 /* Bind the socket to the address. */
2404 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2405 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002406 if (!ai->ai_next)
2407 error("bind: %.100s", strerror(errno));
2408 else
2409 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002410
Damien Miller34132e52000-01-14 15:45:46 +11002411 close(sock);
2412 continue;
2413 }
2414 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11002415 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002416 error("listen: %.100s", strerror(errno));
2417 close(sock);
2418 continue;
2419 }
2420 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002421 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002422 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002423 0, "port listener", 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002424 strlcpy(c->path, host, sizeof(c->path));
2425 c->host_port = port_to_connect;
2426 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002427 success = 1;
2428 }
2429 if (success == 0)
Damien Millerb16461c2002-01-22 23:29:22 +11002430 error("channel_setup_fwd_listener: cannot listen to port: %d",
Kevin Steves12057502001-02-05 14:54:34 +00002431 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002432 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002433 return success;
Damien Miller95def091999-11-25 00:26:21 +11002434}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002435
Darren Tuckere7066df2004-05-24 10:18:05 +10002436int
2437channel_cancel_rport_listener(const char *host, u_short port)
2438{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002439 u_int i;
2440 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10002441
Darren Tucker47eede72005-03-14 23:08:12 +11002442 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10002443 Channel *c = channels[i];
2444
2445 if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
2446 strncmp(c->path, host, sizeof(c->path)) == 0 &&
Darren Tuckerfc959702004-07-17 16:12:08 +10002447 c->listening_port == port) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10002448 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10002449 channel_free(c);
2450 found = 1;
2451 }
2452 }
2453
2454 return (found);
2455}
2456
Damien Millerb16461c2002-01-22 23:29:22 +11002457/* protocol local port fwd, used by ssh (and sshd in v1) */
2458int
Damien Millerf91ee4c2005-03-01 21:24:33 +11002459channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
Damien Millerb16461c2002-01-22 23:29:22 +11002460 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2461{
2462 return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002463 listen_host, listen_port, host_to_connect, port_to_connect,
2464 gateway_ports);
Damien Millerb16461c2002-01-22 23:29:22 +11002465}
2466
2467/* protocol v2 remote port fwd, used by sshd */
2468int
2469channel_setup_remote_fwd_listener(const char *listen_address,
2470 u_short listen_port, int gateway_ports)
2471{
2472 return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
2473 listen_address, listen_port, NULL, 0, gateway_ports);
2474}
2475
Damien Miller5428f641999-11-25 11:54:57 +11002476/*
2477 * Initiate forwarding of connections to port "port" on remote host through
2478 * the secure channel to host:port from local side.
2479 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002480
Damien Miller4af51302000-04-16 11:18:38 +10002481void
Damien Millerf91ee4c2005-03-01 21:24:33 +11002482channel_request_remote_forwarding(const char *listen_host, u_short listen_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +11002483 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002484{
Damien Millerdff50992002-01-22 23:16:32 +11002485 int type, success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002486
Damien Miller95def091999-11-25 00:26:21 +11002487 /* Record locally that connection to this host/port is permitted. */
2488 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2489 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002490
Damien Miller95def091999-11-25 00:26:21 +11002491 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002492 if (compat20) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002493 const char *address_to_bind;
2494 if (listen_host == NULL)
2495 address_to_bind = "localhost";
2496 else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0)
2497 address_to_bind = "";
2498 else
2499 address_to_bind = listen_host;
2500
Damien Miller33b13562000-04-04 14:38:59 +10002501 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2502 packet_put_cstring("tcpip-forward");
Damien Miller2797f7f2002-04-23 21:09:44 +10002503 packet_put_char(1); /* boolean: want reply */
Damien Miller33b13562000-04-04 14:38:59 +10002504 packet_put_cstring(address_to_bind);
2505 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002506 packet_send();
2507 packet_write_wait();
2508 /* Assume that server accepts the request */
2509 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002510 } else {
2511 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002512 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002513 packet_put_cstring(host_to_connect);
2514 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002515 packet_send();
2516 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002517
2518 /* Wait for response from the remote side. */
Damien Millerdff50992002-01-22 23:16:32 +11002519 type = packet_read();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002520 switch (type) {
2521 case SSH_SMSG_SUCCESS:
2522 success = 1;
2523 break;
2524 case SSH_SMSG_FAILURE:
Damien Miller996acd22003-04-09 20:59:48 +10002525 logit("Warning: Server denied remote port forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11002526 break;
2527 default:
2528 /* Unknown packet */
2529 packet_disconnect("Protocol error for port forward request:"
2530 "received packet type %d.", type);
2531 }
2532 }
2533 if (success) {
2534 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2535 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2536 permitted_opens[num_permitted_opens].listen_port = listen_port;
2537 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002538 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002539}
2540
Damien Miller5428f641999-11-25 11:54:57 +11002541/*
Darren Tuckerfc959702004-07-17 16:12:08 +10002542 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10002543 * local side.
2544 */
Darren Tuckere7066df2004-05-24 10:18:05 +10002545void
Damien Millerf91ee4c2005-03-01 21:24:33 +11002546channel_request_rforward_cancel(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10002547{
2548 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10002549
2550 if (!compat20)
2551 return;
2552
2553 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckerfc959702004-07-17 16:12:08 +10002554 if (permitted_opens[i].host_to_connect != NULL &&
Darren Tuckere7066df2004-05-24 10:18:05 +10002555 permitted_opens[i].listen_port == port)
2556 break;
2557 }
2558 if (i >= num_permitted_opens) {
2559 debug("%s: requested forward not found", __func__);
2560 return;
2561 }
2562 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2563 packet_put_cstring("cancel-tcpip-forward");
2564 packet_put_char(0);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002565 packet_put_cstring(host == NULL ? "" : host);
Darren Tuckere7066df2004-05-24 10:18:05 +10002566 packet_put_int(port);
2567 packet_send();
2568
2569 permitted_opens[i].listen_port = 0;
2570 permitted_opens[i].port_to_connect = 0;
Damien Miller0a0176e2005-11-05 15:07:59 +11002571 xfree(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10002572 permitted_opens[i].host_to_connect = NULL;
2573}
2574
2575/*
Damien Miller5428f641999-11-25 11:54:57 +11002576 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2577 * listening for the port, and sends back a success reply (or disconnect
2578 * message if there was an error). This never returns if there was an error.
2579 */
Damien Miller4af51302000-04-16 11:18:38 +10002580void
Damien Millere247cc42000-05-07 12:03:14 +10002581channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002582{
Damien Milleraae6c611999-12-06 11:47:28 +11002583 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11002584 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002585
Damien Miller95def091999-11-25 00:26:21 +11002586 /* Get arguments from the packet. */
2587 port = packet_get_int();
2588 hostname = packet_get_string(NULL);
2589 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002590
Damien Millerbac2d8a2000-09-05 16:13:06 +11002591#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002592 /*
2593 * Check that an unprivileged user is not trying to forward a
2594 * privileged port.
2595 */
Damien Miller95def091999-11-25 00:26:21 +11002596 if (port < IPPORT_RESERVED && !is_root)
Darren Tucker9189ff82003-07-03 13:52:04 +10002597 packet_disconnect(
2598 "Requested forwarding of port %d but user is not root.",
2599 port);
2600 if (host_port == 0)
2601 packet_disconnect("Dynamic forwarding denied.");
Damien Millerbac2d8a2000-09-05 16:13:06 +11002602#endif
Darren Tucker9189ff82003-07-03 13:52:04 +10002603
Damien Miller0bc1bd82000-11-13 22:57:25 +11002604 /* Initiate forwarding */
Damien Millerf91ee4c2005-03-01 21:24:33 +11002605 channel_setup_local_fwd_listener(NULL, port, hostname,
2606 host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002607
2608 /* Free the argument string. */
2609 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002610}
2611
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002612/*
2613 * Permits opening to any host/port if permitted_opens[] is empty. This is
2614 * usually called by the server, because the user could connect to any port
2615 * anyway, and the server has no way to know but to trust the client anyway.
2616 */
2617void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002618channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002619{
2620 if (num_permitted_opens == 0)
2621 all_opens_permitted = 1;
2622}
2623
Ben Lindstroma3700052001-04-05 23:26:32 +00002624void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002625channel_add_permitted_opens(char *host, int port)
2626{
2627 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2628 fatal("channel_request_remote_forwarding: too many forwards");
2629 debug("allow port forwarding to host %s port %d", host, port);
2630
2631 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2632 permitted_opens[num_permitted_opens].port_to_connect = port;
2633 num_permitted_opens++;
2634
2635 all_opens_permitted = 0;
2636}
2637
Ben Lindstroma3700052001-04-05 23:26:32 +00002638void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002639channel_clear_permitted_opens(void)
2640{
2641 int i;
2642
2643 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002644 if (permitted_opens[i].host_to_connect != NULL)
2645 xfree(permitted_opens[i].host_to_connect);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002646 num_permitted_opens = 0;
2647
2648}
2649
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002650/* return socket to remote host, port */
Ben Lindstrombba81212001-06-25 05:01:22 +00002651static int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002652connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002653{
Damien Miller34132e52000-01-14 15:45:46 +11002654 struct addrinfo hints, *ai, *aitop;
2655 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2656 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002657 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002658
Damien Miller34132e52000-01-14 15:45:46 +11002659 memset(&hints, 0, sizeof(hints));
2660 hints.ai_family = IPv4or6;
2661 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002662 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002663 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002664 error("connect_to %.100s: unknown host (%s)", host,
2665 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002666 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002667 }
Damien Miller34132e52000-01-14 15:45:46 +11002668 for (ai = aitop; ai; ai = ai->ai_next) {
2669 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2670 continue;
2671 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2672 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002673 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002674 continue;
2675 }
Damien Miller2372ace2003-05-14 13:42:23 +10002676 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002677 if (sock < 0) {
Damien Millerb46b9f32003-01-10 21:45:12 +11002678 if (ai->ai_next == NULL)
2679 error("socket: %.100s", strerror(errno));
2680 else
2681 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002682 continue;
2683 }
Damien Miller232711f2004-06-15 10:35:30 +10002684 if (set_nonblock(sock) == -1)
2685 fatal("%s: set_nonblock(%d)", __func__, sock);
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002686 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2687 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002688 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002689 strerror(errno));
2690 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002691 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002692 }
2693 break; /* success */
2694
2695 }
2696 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002697 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002698 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002699 return -1;
2700 }
2701 /* success */
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00002702 set_nodelay(sock);
Damien Millerb38eff82000-04-01 11:09:21 +10002703 return sock;
2704}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002705
Damien Miller0bc1bd82000-11-13 22:57:25 +11002706int
Ben Lindstrom173e6462001-07-04 05:15:15 +00002707channel_connect_by_listen_address(u_short listen_port)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002708{
2709 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002710
Damien Miller0bc1bd82000-11-13 22:57:25 +11002711 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002712 if (permitted_opens[i].host_to_connect != NULL &&
2713 permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002714 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002715 permitted_opens[i].host_to_connect,
2716 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002717 error("WARNING: Server requests forwarding for unknown listen_port %d",
2718 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002719 return -1;
2720}
Damien Millerb38eff82000-04-01 11:09:21 +10002721
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002722/* Check if connecting to that port is permitted and connect. */
2723int
2724channel_connect_to(const char *host, u_short port)
2725{
2726 int i, permit;
2727
2728 permit = all_opens_permitted;
2729 if (!permit) {
2730 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002731 if (permitted_opens[i].host_to_connect != NULL &&
2732 permitted_opens[i].port_to_connect == port &&
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002733 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2734 permit = 1;
2735
2736 }
2737 if (!permit) {
Damien Miller996acd22003-04-09 20:59:48 +10002738 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002739 "but the request was denied.", host, port);
2740 return -1;
2741 }
2742 return connect_to(host, port);
2743}
2744
Damien Miller0e220db2004-06-15 10:34:08 +10002745void
2746channel_send_window_changes(void)
2747{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002748 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10002749 struct winsize ws;
2750
2751 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11002752 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10002753 channels[i]->type != SSH_CHANNEL_OPEN)
2754 continue;
2755 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
2756 continue;
2757 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11002758 packet_put_int((u_int)ws.ws_col);
2759 packet_put_int((u_int)ws.ws_row);
2760 packet_put_int((u_int)ws.ws_xpixel);
2761 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10002762 packet_send();
2763 }
2764}
2765
Ben Lindstrome9c99912001-06-09 00:41:05 +00002766/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002767
Damien Miller5428f641999-11-25 11:54:57 +11002768/*
2769 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002770 * Returns 0 and a suitable display number for the DISPLAY variable
2771 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11002772 */
Kevin Steves366298c2001-12-19 17:58:01 +00002773int
Damien Miller95c249f2002-02-05 12:11:34 +11002774x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10002775 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002776{
Damien Millere7378562001-12-21 14:58:35 +11002777 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11002778 int display_number, sock;
2779 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002780 struct addrinfo hints, *ai, *aitop;
2781 char strport[NI_MAXSERV];
2782 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002783
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002784 if (chanids == NULL)
2785 return -1;
2786
Damien Millera34a28b1999-12-14 10:47:15 +11002787 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002788 display_number < MAX_DISPLAYS;
2789 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11002790 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002791 memset(&hints, 0, sizeof(hints));
2792 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11002793 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11002794 hints.ai_socktype = SOCK_STREAM;
2795 snprintf(strport, sizeof strport, "%d", port);
2796 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2797 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00002798 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002799 }
Damien Miller34132e52000-01-14 15:45:46 +11002800 for (ai = aitop; ai; ai = ai->ai_next) {
2801 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2802 continue;
Damien Miller2372ace2003-05-14 13:42:23 +10002803 sock = socket(ai->ai_family, ai->ai_socktype,
2804 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002805 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002806 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002807 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10002808 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00002809 return -1;
Damien Millere2192732000-01-17 13:22:55 +11002810 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002811 debug("x11_create_display_inet: Socket family %d not supported",
2812 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002813 continue;
2814 }
Damien Miller34132e52000-01-14 15:45:46 +11002815 }
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002816#ifdef IPV6_V6ONLY
2817 if (ai->ai_family == AF_INET6) {
2818 int on = 1;
2819 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
2820 error("setsockopt IPV6_V6ONLY: %.100s", strerror(errno));
2821 }
2822#endif
Damien Miller5e7fd072005-11-05 14:53:39 +11002823 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11002824 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002825 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002826 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002827
2828 if (ai->ai_next)
2829 continue;
2830
Damien Miller34132e52000-01-14 15:45:46 +11002831 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11002832 close(socks[n]);
2833 }
2834 num_socks = 0;
2835 break;
2836 }
2837 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002838#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002839 if (num_socks == NUM_SOCKS)
2840 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002841#else
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002842 if (x11_use_localhost) {
2843 if (num_socks == NUM_SOCKS)
2844 break;
2845 } else {
2846 break;
2847 }
Damien Miller7bcb0892000-03-11 20:45:40 +11002848#endif
Damien Miller95def091999-11-25 00:26:21 +11002849 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002850 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002851 if (num_socks > 0)
2852 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002853 }
Damien Miller95def091999-11-25 00:26:21 +11002854 if (display_number >= MAX_DISPLAYS) {
2855 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00002856 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002857 }
Damien Miller95def091999-11-25 00:26:21 +11002858 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002859 for (n = 0; n < num_socks; n++) {
2860 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11002861 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002862 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002863 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00002864 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11002865 }
Damien Miller95def091999-11-25 00:26:21 +11002866 }
Damien Miller34132e52000-01-14 15:45:46 +11002867
Damien Miller34132e52000-01-14 15:45:46 +11002868 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11002869 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11002870 for (n = 0; n < num_socks; n++) {
2871 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11002872 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10002873 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2874 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002875 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11002876 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002877 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11002878 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002879 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002880
Kevin Steves366298c2001-12-19 17:58:01 +00002881 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002882 *display_numberp = display_number;
2883 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002884}
2885
Ben Lindstrombba81212001-06-25 05:01:22 +00002886static int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002887connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002888{
Damien Miller95def091999-11-25 00:26:21 +11002889 int sock;
2890 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002891
Damien Miller3afe3752001-12-21 12:39:51 +11002892 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2893 if (sock < 0)
2894 error("socket: %.100s", strerror(errno));
2895 memset(&addr, 0, sizeof(addr));
2896 addr.sun_family = AF_UNIX;
2897 snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
Damien Miller90967402006-03-26 14:07:26 +11002898 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11002899 return sock;
2900 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11002901 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2902 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002903}
2904
Damien Millerbd483e72000-04-30 10:00:53 +10002905int
2906x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002907{
Damien Miller57c4e872006-03-31 23:11:07 +11002908 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11002909 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002910 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002911 struct addrinfo hints, *ai, *aitop;
2912 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11002913 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002914
Damien Miller95def091999-11-25 00:26:21 +11002915 /* Try to open a socket for the local X server. */
2916 display = getenv("DISPLAY");
2917 if (!display) {
2918 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002919 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002920 }
Damien Miller5428f641999-11-25 11:54:57 +11002921 /*
2922 * Now we decode the value of the DISPLAY variable and make a
2923 * connection to the real X server.
2924 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002925
Damien Miller5428f641999-11-25 11:54:57 +11002926 /*
2927 * Check if it is a unix domain socket. Unix domain displays are in
2928 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2929 */
Damien Miller95def091999-11-25 00:26:21 +11002930 if (strncmp(display, "unix:", 5) == 0 ||
2931 display[0] == ':') {
2932 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11002933 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11002934 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11002935 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002936 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002937 }
2938 /* Create a socket. */
2939 sock = connect_local_xsocket(display_number);
2940 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002941 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002942
2943 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002944 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002945 }
Damien Miller5428f641999-11-25 11:54:57 +11002946 /*
2947 * Connect to an inet socket. The DISPLAY value is supposedly
2948 * hostname:d[.s], where hostname may also be numeric IP address.
2949 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00002950 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11002951 cp = strchr(buf, ':');
2952 if (!cp) {
2953 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002954 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002955 }
Damien Miller95def091999-11-25 00:26:21 +11002956 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002957 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11002958 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11002959 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11002960 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002961 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002962 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002963
Damien Miller34132e52000-01-14 15:45:46 +11002964 /* Look up the host address */
2965 memset(&hints, 0, sizeof(hints));
2966 hints.ai_family = IPv4or6;
2967 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11002968 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11002969 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2970 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002971 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002972 }
Damien Miller34132e52000-01-14 15:45:46 +11002973 for (ai = aitop; ai; ai = ai->ai_next) {
2974 /* Create a socket. */
Damien Miller2372ace2003-05-14 13:42:23 +10002975 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002976 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002977 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002978 continue;
2979 }
2980 /* Connect it to the display. */
2981 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11002982 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10002983 6000 + display_number, strerror(errno));
2984 close(sock);
2985 continue;
2986 }
2987 /* Success */
2988 break;
Damien Miller34132e52000-01-14 15:45:46 +11002989 }
Damien Miller34132e52000-01-14 15:45:46 +11002990 freeaddrinfo(aitop);
2991 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11002992 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002993 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002994 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002995 }
Damien Miller398e1cf2002-02-05 11:52:13 +11002996 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10002997 return sock;
2998}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002999
Damien Millerbd483e72000-04-30 10:00:53 +10003000/*
3001 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
3002 * the remote channel number. We should do whatever we want, and respond
3003 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
3004 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003005
Damien Millerbd483e72000-04-30 10:00:53 +10003006void
Damien Miller630d6f42002-01-22 23:17:30 +11003007x11_input_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10003008{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003009 Channel *c = NULL;
3010 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10003011 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10003012
3013 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003014
3015 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00003016
3017 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003018 remote_host = packet_get_string(NULL);
3019 } else {
3020 remote_host = xstrdup("unknown (remote did not supply name)");
3021 }
Damien Miller48b03fc2002-01-22 23:11:40 +11003022 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10003023
3024 /* Obtain a connection to the real X display. */
3025 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003026 if (sock != -1) {
3027 /* Allocate a channel for this connection. */
3028 c = channel_new("connected x11 socket",
3029 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
3030 remote_host, 1);
Damien Miller699d0032002-02-08 22:07:16 +11003031 c->remote_id = remote_id;
3032 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003033 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003034 xfree(remote_host);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003035 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10003036 /* Send refusal to the remote host. */
3037 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003038 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10003039 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10003040 /* Send a confirmation to the remote host. */
3041 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003042 packet_put_int(remote_id);
3043 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10003044 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003045 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003046}
3047
Damien Miller69b69aa2000-10-28 14:19:58 +11003048/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
3049void
Damien Miller630d6f42002-01-22 23:17:30 +11003050deny_input_open(int type, u_int32_t seq, void *ctxt)
Damien Miller69b69aa2000-10-28 14:19:58 +11003051{
3052 int rchan = packet_get_int();
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00003053
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00003054 switch (type) {
Damien Miller69b69aa2000-10-28 14:19:58 +11003055 case SSH_SMSG_AGENT_OPEN:
3056 error("Warning: ssh server tried agent forwarding.");
3057 break;
3058 case SSH_SMSG_X11_OPEN:
3059 error("Warning: ssh server tried X11 forwarding.");
3060 break;
3061 default:
Damien Miller630d6f42002-01-22 23:17:30 +11003062 error("deny_input_open: type %d", type);
Damien Miller69b69aa2000-10-28 14:19:58 +11003063 break;
3064 }
Damien Miller5eb137c2005-12-31 16:19:53 +11003065 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller69b69aa2000-10-28 14:19:58 +11003066 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
3067 packet_put_int(rchan);
3068 packet_send();
3069}
3070
Damien Miller5428f641999-11-25 11:54:57 +11003071/*
3072 * Requests forwarding of X11 connections, generates fake authentication
3073 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00003074 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11003075 */
Damien Miller4af51302000-04-16 11:18:38 +10003076void
Damien Miller17e7ed02005-06-17 12:54:33 +10003077x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Millerbd483e72000-04-30 10:00:53 +10003078 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003079{
Ben Lindstrom46c16222000-12-22 01:43:59 +00003080 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10003081 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11003082 char *new_data;
3083 int screen_number;
3084 const char *cp;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10003085 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003086
Damien Millerf92c0792005-07-06 09:45:26 +10003087 if (x11_saved_display == NULL)
3088 x11_saved_display = xstrdup(disp);
3089 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10003090 error("x11_request_forwarding_with_spoofing: different "
3091 "$DISPLAY already forwarded");
3092 return;
3093 }
3094
Damien Miller17e7ed02005-06-17 12:54:33 +10003095 cp = disp;
3096 if (disp)
3097 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11003098 if (cp)
3099 cp = strchr(cp, '.');
3100 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11003101 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11003102 else
3103 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003104
Damien Miller13390022005-07-06 09:44:19 +10003105 if (x11_saved_proto == NULL) {
3106 /* Save protocol name. */
3107 x11_saved_proto = xstrdup(proto);
3108 /*
Damien Miller46d38de2005-07-17 17:02:09 +10003109 * Extract real authentication data and generate fake data
Damien Miller13390022005-07-06 09:44:19 +10003110 * of the same length.
3111 */
3112 x11_saved_data = xmalloc(data_len);
3113 x11_fake_data = xmalloc(data_len);
3114 for (i = 0; i < data_len; i++) {
3115 if (sscanf(data + 2 * i, "%2x", &value) != 1)
3116 fatal("x11_request_forwarding: bad "
3117 "authentication data: %.100s", data);
3118 if (i % 4 == 0)
3119 rnd = arc4random();
3120 x11_saved_data[i] = value;
3121 x11_fake_data[i] = rnd & 0xff;
3122 rnd >>= 8;
3123 }
3124 x11_saved_data_len = data_len;
3125 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11003126 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003127
Damien Miller95def091999-11-25 00:26:21 +11003128 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10003129 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003130
Damien Miller95def091999-11-25 00:26:21 +11003131 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10003132 if (compat20) {
3133 channel_request_start(client_session_id, "x11-req", 0);
3134 packet_put_char(0); /* XXX bool single connection */
3135 } else {
3136 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
3137 }
3138 packet_put_cstring(proto);
3139 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11003140 packet_put_int(screen_number);
3141 packet_send();
3142 packet_write_wait();
3143 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003144}
3145
Ben Lindstrome9c99912001-06-09 00:41:05 +00003146
3147/* -- agent forwarding */
3148
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003149/* Sends a message to the server to request authentication fd forwarding. */
3150
Damien Miller4af51302000-04-16 11:18:38 +10003151void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003152auth_request_forwarding(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003153{
Damien Miller95def091999-11-25 00:26:21 +11003154 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
3155 packet_send();
3156 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003157}