blob: 1252f344673b8e4efb39dc25b7aa4638e8d1633b [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * This file contains functions for generic socket connection forwarding.
6 * There is also code for initiating connection forwarding for X11 connections,
7 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 *
Damien Miller33b13562000-04-04 14:38:59 +100015 * SSH2 support added by Markus Friedl.
Damien Millera90fc082002-01-22 23:19:38 +110016 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110017 * Copyright (c) 1999 Dug Song. All rights reserved.
18 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110039 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040
41#include "includes.h"
Damien Millere204f6a2006-01-31 21:47:15 +110042RCSID("$OpenBSD: channels.c,v 1.232 2006/01/30 12:22:22 reyk Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
44#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "ssh1.h"
46#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047#include "packet.h"
48#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000049#include "log.h"
50#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100054#include "key.h"
55#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110056#include "pathnames.h"
Darren Tucker46471c92003-07-03 13:55:19 +100057#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
Ben Lindstrome9c99912001-06-09 00:41:05 +000059/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
Damien Miller5428f641999-11-25 11:54:57 +110061/*
62 * Pointer to an array containing all allocated channels. The array is
63 * dynamically extended as needed.
64 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000065static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
Damien Miller5428f641999-11-25 11:54:57 +110067/*
68 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000069 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110070 */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +100071static u_int channels_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072
Damien Miller5428f641999-11-25 11:54:57 +110073/*
74 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +000075 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +110076 */
Damien Miller5e953212001-01-30 09:14:00 +110077static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Ben Lindstrome9c99912001-06-09 00:41:05 +000080/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
Damien Miller5428f641999-11-25 11:54:57 +110082/*
83 * Data structure for storing which hosts are permitted for forward requests.
84 * The local sides of any remote forwards are stored in this array to prevent
85 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
86 * network (which might be behind a firewall).
87 */
Damien Miller95def091999-11-25 00:26:21 +110088typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +100089 char *host_to_connect; /* Connect to 'host'. */
90 u_short port_to_connect; /* Connect to 'port'. */
91 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092} ForwardPermission;
93
94/* List of all permitted host/port pairs to connect. */
95static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
Ben Lindstrome9c99912001-06-09 00:41:05 +000096
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097/* Number of permitted host/port pairs in the array. */
98static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +110099/*
100 * If this is true, all opens are permitted. This is the case on the server
101 * on which we have to trust the client anyway, and the user could do
102 * anything after logging in anyway.
103 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104static int all_opens_permitted = 0;
105
Ben Lindstrome9c99912001-06-09 00:41:05 +0000106
107/* -- X11 forwarding */
108
109/* Maximum number of fake X11 displays to try. */
110#define MAX_DISPLAYS 1000
111
Damien Miller13390022005-07-06 09:44:19 +1000112/* Saved X11 local (client) display. */
113static char *x11_saved_display = NULL;
114
Ben Lindstrome9c99912001-06-09 00:41:05 +0000115/* Saved X11 authentication protocol name. */
116static char *x11_saved_proto = NULL;
117
118/* Saved X11 authentication data. This is the real data. */
119static char *x11_saved_data = NULL;
120static u_int x11_saved_data_len = 0;
121
122/*
123 * Fake X11 authentication data. This is what the server will be sending us;
124 * we should replace any occurrences of this by the real data.
125 */
126static char *x11_fake_data = NULL;
127static u_int x11_fake_data_len;
128
129
130/* -- agent forwarding */
131
132#define NUM_SOCKS 10
133
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000134/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000135static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000136
Ben Lindstrome9c99912001-06-09 00:41:05 +0000137/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000138static void port_open_helper(Channel *c, char *rtype);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000139
Ben Lindstrome9c99912001-06-09 00:41:05 +0000140/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000141
142Channel *
Damien Millerd47c62a2005-12-13 19:33:57 +1100143channel_by_id(int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000144{
145 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000146
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000147 if (id < 0 || (u_int)id >= channels_alloc) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100148 logit("channel_by_id: %d: bad id", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000149 return NULL;
150 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000151 c = channels[id];
152 if (c == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100153 logit("channel_by_id: %d: bad id: channel free", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000154 return NULL;
155 }
156 return c;
157}
158
Damien Miller5428f641999-11-25 11:54:57 +1100159/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100160 * Returns the channel if it is allowed to receive protocol messages.
161 * Private channels, like listening sockets, may not receive messages.
162 */
163Channel *
164channel_lookup(int id)
165{
166 Channel *c;
167
168 if ((c = channel_by_id(id)) == NULL)
169 return (NULL);
170
171 switch(c->type) {
172 case SSH_CHANNEL_X11_OPEN:
173 case SSH_CHANNEL_LARVAL:
174 case SSH_CHANNEL_CONNECTING:
175 case SSH_CHANNEL_DYNAMIC:
176 case SSH_CHANNEL_OPENING:
177 case SSH_CHANNEL_OPEN:
178 case SSH_CHANNEL_INPUT_DRAINING:
179 case SSH_CHANNEL_OUTPUT_DRAINING:
180 return (c);
181 break;
182 }
183 logit("Non-public channel %d, type %d.", id, c->type);
184 return (NULL);
185}
186
187/*
Damien Millere247cc42000-05-07 12:03:14 +1000188 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000189 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100190 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191
Ben Lindstrombba81212001-06-25 05:01:22 +0000192static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100193channel_register_fds(Channel *c, int rfd, int wfd, int efd,
194 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195{
Damien Miller95def091999-11-25 00:26:21 +1100196 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100197 channel_max_fd = MAX(channel_max_fd, rfd);
198 channel_max_fd = MAX(channel_max_fd, wfd);
199 channel_max_fd = MAX(channel_max_fd, efd);
200
Damien Miller5428f641999-11-25 11:54:57 +1100201 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000202
Damien Miller6f83b8e2000-05-02 09:23:45 +1000203 c->rfd = rfd;
204 c->wfd = wfd;
205 c->sock = (rfd == wfd) ? rfd : -1;
Damien Miller0e220db2004-06-15 10:34:08 +1000206 c->ctl_fd = -1; /* XXX: set elsewhere */
Damien Miller6f83b8e2000-05-02 09:23:45 +1000207 c->efd = efd;
208 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100209
Damien Miller79438cc2001-02-16 12:34:57 +1100210 /* XXX ugly hack: nonblock is only set by the server */
211 if (nonblock && isatty(c->rfd)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000212 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100213 c->isatty = 1;
214 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000215 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100216 c->self, c->wfd);
217 }
218 } else {
219 c->isatty = 0;
220 }
Ben Lindstrombeb5f332002-07-22 15:28:53 +0000221 c->wfd_isatty = isatty(c->wfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100222
Damien Miller69b69aa2000-10-28 14:19:58 +1100223 /* enable nonblocking mode */
224 if (nonblock) {
225 if (rfd != -1)
226 set_nonblock(rfd);
227 if (wfd != -1)
228 set_nonblock(wfd);
229 if (efd != -1)
230 set_nonblock(efd);
231 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000232}
233
234/*
235 * Allocate a new channel object and set its type and socket. This will cause
236 * remote_name to be freed.
237 */
238
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000239Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000240channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000241 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000242{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000243 int found;
244 u_int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000245 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246
Damien Miller95def091999-11-25 00:26:21 +1100247 /* Do initial allocation if this is the first call. */
248 if (channels_alloc == 0) {
249 channels_alloc = 10;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000250 channels = xmalloc(channels_alloc * sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100251 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000252 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100253 }
254 /* Try to find a free slot where to put the new channel. */
255 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000256 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100257 /* Found a free slot. */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000258 found = (int)i;
Damien Miller95def091999-11-25 00:26:21 +1100259 break;
260 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000261 if (found < 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100262 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100263 found = channels_alloc;
Damien Miller9403aa22002-06-26 19:14:43 +1000264 if (channels_alloc > 10000)
265 fatal("channel_new: internal error: channels_alloc %d "
266 "too big.", channels_alloc);
Damien Miller5efcecc2003-09-17 07:31:14 +1000267 channels = xrealloc(channels,
268 (channels_alloc + 10) * sizeof(Channel *));
269 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100270 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100271 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000272 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100273 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000274 /* Initialize and return new channel. */
275 c = channels[found] = xmalloc(sizeof(Channel));
Damien Miller4623a752001-10-10 15:03:58 +1000276 memset(c, 0, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100277 buffer_init(&c->input);
278 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000279 buffer_init(&c->extended);
Damien Miller5144df92002-01-22 23:28:45 +1100280 c->ostate = CHAN_OUTPUT_OPEN;
281 c->istate = CHAN_INPUT_OPEN;
282 c->flags = 0;
Damien Miller69b69aa2000-10-28 14:19:58 +1100283 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100284 c->self = found;
285 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000286 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000287 c->local_window = window;
288 c->local_window_max = window;
289 c->local_consumed = 0;
290 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100291 c->remote_id = -1;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000292 c->remote_name = xstrdup(remote_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000293 c->remote_window = 0;
294 c->remote_maxpacket = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000295 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100296 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000297 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100298 c->detach_close = 0;
Damien Miller67f0bc02002-02-05 12:23:08 +1100299 c->confirm = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +1000300 c->confirm_ctx = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000301 c->input_filter = NULL;
Damien Miller077b2382005-12-31 16:22:32 +1100302 c->output_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100303 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000304 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000306
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000307static int
308channel_find_maxfd(void)
309{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000310 u_int i;
311 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000312 Channel *c;
313
314 for (i = 0; i < channels_alloc; i++) {
315 c = channels[i];
316 if (c != NULL) {
317 max = MAX(max, c->rfd);
318 max = MAX(max, c->wfd);
319 max = MAX(max, c->efd);
320 }
321 }
322 return max;
323}
324
325int
326channel_close_fd(int *fdp)
327{
328 int ret = 0, fd = *fdp;
329
330 if (fd != -1) {
331 ret = close(fd);
332 *fdp = -1;
333 if (fd == channel_max_fd)
334 channel_max_fd = channel_find_maxfd();
335 }
336 return ret;
337}
338
Damien Miller6f83b8e2000-05-02 09:23:45 +1000339/* Close all channel fd/socket. */
340
Ben Lindstrombba81212001-06-25 05:01:22 +0000341static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000342channel_close_fds(Channel *c)
343{
Damien Miller0e220db2004-06-15 10:34:08 +1000344 debug3("channel %d: close_fds r %d w %d e %d c %d",
345 c->self, c->rfd, c->wfd, c->efd, c->ctl_fd);
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000346
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000347 channel_close_fd(&c->sock);
Damien Miller0e220db2004-06-15 10:34:08 +1000348 channel_close_fd(&c->ctl_fd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000349 channel_close_fd(&c->rfd);
350 channel_close_fd(&c->wfd);
351 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000352}
353
354/* Free the channel and close its fd/socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000355
Damien Miller4af51302000-04-16 11:18:38 +1000356void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000357channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000358{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000359 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000360 u_int i, n;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000361
362 for (n = 0, i = 0; i < channels_alloc; i++)
363 if (channels[i])
364 n++;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000365 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000366 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000367
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000368 s = channel_open_message();
Damien Millerfbdeece2003-09-02 22:52:31 +1000369 debug3("channel %d: status: %s", c->self, s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000370 xfree(s);
371
Damien Miller6f83b8e2000-05-02 09:23:45 +1000372 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000373 shutdown(c->sock, SHUT_RDWR);
Damien Miller0e220db2004-06-15 10:34:08 +1000374 if (c->ctl_fd != -1)
375 shutdown(c->ctl_fd, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000376 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000377 buffer_free(&c->input);
378 buffer_free(&c->output);
379 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000380 if (c->remote_name) {
381 xfree(c->remote_name);
382 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100383 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000384 channels[c->self] = NULL;
385 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000386}
387
Ben Lindstrome9c99912001-06-09 00:41:05 +0000388void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000389channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000390{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000391 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000392
Ben Lindstrom601e4362001-06-21 03:19:23 +0000393 for (i = 0; i < channels_alloc; i++)
394 if (channels[i] != NULL)
395 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000396}
397
398/*
399 * Closes the sockets/fds of all channels. This is used to close extra file
400 * descriptors after a fork.
401 */
402
403void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000404channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000405{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000406 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000407
408 for (i = 0; i < channels_alloc; i++)
409 if (channels[i] != NULL)
410 channel_close_fds(channels[i]);
411}
412
413/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000414 * Stop listening to channels.
415 */
416
417void
418channel_stop_listening(void)
419{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000420 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000421 Channel *c;
422
423 for (i = 0; i < channels_alloc; i++) {
424 c = channels[i];
425 if (c != NULL) {
426 switch (c->type) {
427 case SSH_CHANNEL_AUTH_SOCKET:
428 case SSH_CHANNEL_PORT_LISTENER:
429 case SSH_CHANNEL_RPORT_LISTENER:
430 case SSH_CHANNEL_X11_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000431 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000432 channel_free(c);
433 break;
434 }
435 }
436 }
437}
438
439/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000440 * Returns true if no channel has too much buffered data, and false if one or
441 * more channel is overfull.
442 */
443
444int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000445channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000446{
447 u_int i;
448 Channel *c;
449
450 for (i = 0; i < channels_alloc; i++) {
451 c = channels[i];
452 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000453#if 0
454 if (!compat20 &&
455 buffer_len(&c->input) > packet_get_maxsize()) {
Damien Miller275295e2003-01-08 14:04:09 +1100456 debug2("channel %d: big input buffer %d",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000457 c->self, buffer_len(&c->input));
458 return 0;
459 }
Damien Milleraf5f2e62001-10-10 15:01:16 +1000460#endif
Ben Lindstrome9c99912001-06-09 00:41:05 +0000461 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000462 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000463 c->self, buffer_len(&c->output),
464 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000465 return 0;
466 }
467 }
468 }
469 return 1;
470}
471
472/* Returns true if any channel is still open. */
473
474int
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 */
516
517int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000518channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000519{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000520 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000521 Channel *c;
522
523 for (i = 0; i < channels_alloc; i++) {
524 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000525 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000526 continue;
527 switch (c->type) {
528 case SSH_CHANNEL_CLOSED:
529 case SSH_CHANNEL_DYNAMIC:
530 case SSH_CHANNEL_X11_LISTENER:
531 case SSH_CHANNEL_PORT_LISTENER:
532 case SSH_CHANNEL_RPORT_LISTENER:
533 case SSH_CHANNEL_OPENING:
534 case SSH_CHANNEL_CONNECTING:
535 case SSH_CHANNEL_ZOMBIE:
536 continue;
537 case SSH_CHANNEL_LARVAL:
538 case SSH_CHANNEL_AUTH_SOCKET:
539 case SSH_CHANNEL_OPEN:
540 case SSH_CHANNEL_X11_OPEN:
541 return i;
542 case SSH_CHANNEL_INPUT_DRAINING:
543 case SSH_CHANNEL_OUTPUT_DRAINING:
544 if (!compat13)
545 fatal("cannot happen: OUT_DRAIN");
546 return i;
547 default:
548 fatal("channel_find_open: bad channel type %d", c->type);
549 /* NOTREACHED */
550 }
551 }
552 return -1;
553}
554
555
556/*
557 * Returns a message describing the currently open forwarded connections,
558 * suitable for sending to the client. The message contains crlf pairs for
559 * newlines.
560 */
561
562char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000563channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000564{
565 Buffer buffer;
566 Channel *c;
567 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000568 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000569
570 buffer_init(&buffer);
571 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
572 buffer_append(&buffer, buf, strlen(buf));
573 for (i = 0; i < channels_alloc; i++) {
574 c = channels[i];
575 if (c == NULL)
576 continue;
577 switch (c->type) {
578 case SSH_CHANNEL_X11_LISTENER:
579 case SSH_CHANNEL_PORT_LISTENER:
580 case SSH_CHANNEL_RPORT_LISTENER:
581 case SSH_CHANNEL_CLOSED:
582 case SSH_CHANNEL_AUTH_SOCKET:
583 case SSH_CHANNEL_ZOMBIE:
584 continue;
585 case SSH_CHANNEL_LARVAL:
586 case SSH_CHANNEL_OPENING:
587 case SSH_CHANNEL_CONNECTING:
588 case SSH_CHANNEL_DYNAMIC:
589 case SSH_CHANNEL_OPEN:
590 case SSH_CHANNEL_X11_OPEN:
591 case SSH_CHANNEL_INPUT_DRAINING:
592 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Miller0e220db2004-06-15 10:34:08 +1000593 snprintf(buf, sizeof buf,
594 " #%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 +0000595 c->self, c->remote_name,
596 c->type, c->remote_id,
597 c->istate, buffer_len(&c->input),
598 c->ostate, buffer_len(&c->output),
Damien Miller0e220db2004-06-15 10:34:08 +1000599 c->rfd, c->wfd, c->ctl_fd);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000600 buffer_append(&buffer, buf, strlen(buf));
601 continue;
602 default:
603 fatal("channel_open_message: bad channel type %d", c->type);
604 /* NOTREACHED */
605 }
606 }
607 buffer_append(&buffer, "\0", 1);
608 cp = xstrdup(buffer_ptr(&buffer));
609 buffer_free(&buffer);
610 return cp;
611}
612
613void
614channel_send_open(int id)
615{
616 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000617
Ben Lindstrome9c99912001-06-09 00:41:05 +0000618 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000619 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000620 return;
621 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000622 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000623 packet_start(SSH2_MSG_CHANNEL_OPEN);
624 packet_put_cstring(c->ctype);
625 packet_put_int(c->self);
626 packet_put_int(c->local_window);
627 packet_put_int(c->local_maxpacket);
628 packet_send();
629}
630
631void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000632channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000633{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000634 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000635
Ben Lindstrome9c99912001-06-09 00:41:05 +0000636 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000637 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000638 return;
639 }
Damien Miller0e220db2004-06-15 10:34:08 +1000640 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000641 packet_start(SSH2_MSG_CHANNEL_REQUEST);
642 packet_put_int(c->remote_id);
643 packet_put_cstring(service);
644 packet_put_char(wantconfirm);
645}
646void
Damien Miller0e220db2004-06-15 10:34:08 +1000647channel_register_confirm(int id, channel_callback_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000648{
649 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000650
Ben Lindstrome9c99912001-06-09 00:41:05 +0000651 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000652 logit("channel_register_comfirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000653 return;
654 }
Damien Miller67f0bc02002-02-05 12:23:08 +1100655 c->confirm = fn;
Damien Miller0e220db2004-06-15 10:34:08 +1000656 c->confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000657}
658void
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}
670void
671channel_cancel_cleanup(int id)
672{
Damien Millerd47c62a2005-12-13 19:33:57 +1100673 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000674
Ben Lindstrome9c99912001-06-09 00:41:05 +0000675 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000676 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000677 return;
678 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000679 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100680 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000681}
682void
Damien Miller077b2382005-12-31 16:22:32 +1100683channel_register_filter(int id, channel_infilter_fn *ifn,
684 channel_outfilter_fn *ofn)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000685{
686 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000687
Ben Lindstrome9c99912001-06-09 00:41:05 +0000688 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000689 logit("channel_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000690 return;
691 }
Damien Miller077b2382005-12-31 16:22:32 +1100692 c->input_filter = ifn;
693 c->output_filter = ofn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000694}
695
696void
697channel_set_fds(int id, int rfd, int wfd, int efd,
Damien Miller2aa0c192002-02-19 15:20:08 +1100698 int extusage, int nonblock, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000699{
700 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000701
Ben Lindstrome9c99912001-06-09 00:41:05 +0000702 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
703 fatal("channel_activate for non-larval channel %d.", id);
704 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
705 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100706 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000707 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
708 packet_put_int(c->remote_id);
709 packet_put_int(c->local_window);
710 packet_send();
711}
712
Damien Miller5428f641999-11-25 11:54:57 +1100713/*
Damien Millerb38eff82000-04-01 11:09:21 +1000714 * 'channel_pre*' are called just before select() to add any bits relevant to
715 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100716 */
Damien Millerb38eff82000-04-01 11:09:21 +1000717/*
718 * 'channel_post*': perform any appropriate operations for channels which
719 * have events pending.
720 */
721typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
722chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
723chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
724
Ben Lindstrombba81212001-06-25 05:01:22 +0000725static void
Damien Millerb38eff82000-04-01 11:09:21 +1000726channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
727{
728 FD_SET(c->sock, readset);
729}
730
Ben Lindstrombba81212001-06-25 05:01:22 +0000731static void
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000732channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
733{
734 debug3("channel %d: waiting for connection", c->self);
735 FD_SET(c->sock, writeset);
736}
737
Ben Lindstrombba81212001-06-25 05:01:22 +0000738static void
Damien Millerb38eff82000-04-01 11:09:21 +1000739channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
740{
741 if (buffer_len(&c->input) < packet_get_maxsize())
742 FD_SET(c->sock, readset);
743 if (buffer_len(&c->output) > 0)
744 FD_SET(c->sock, writeset);
745}
746
Ben Lindstrombba81212001-06-25 05:01:22 +0000747static void
Damien Millerde6987c2002-01-22 23:20:40 +1100748channel_pre_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000749{
Damien Millerde6987c2002-01-22 23:20:40 +1100750 u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
Damien Millerb38eff82000-04-01 11:09:21 +1000751
Darren Tucker11327cc2005-03-14 23:22:25 +1100752 /* check buffer limits */
753 limit = MIN(limit, (BUFFER_MAX_LEN - BUFFER_MAX_CHUNK - CHAN_RBUF));
754
Damien Miller33b13562000-04-04 14:38:59 +1000755 if (c->istate == CHAN_INPUT_OPEN &&
Damien Millerde6987c2002-01-22 23:20:40 +1100756 limit > 0 &&
757 buffer_len(&c->input) < limit)
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 Millerb38eff82000-04-01 11:09:21 +1000788channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
789{
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 Millerb38eff82000-04-01 11:09:21 +1000800channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
801{
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 Millerb38eff82000-04-01 11:09:21 +1000876channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
877{
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 Millerbd483e72000-04-30 10:00:53 +1000902channel_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
Ben Lindstromb3921512001-04-11 15:57:50 +0000929channel_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 */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000993 buffer_append(&c->output, (char *)&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
1007channel_decode_socks5(Channel *c, fd_set * readset, fd_set * writeset)
1008{
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 */
1054 memcpy((char *)&s5_req, p, sizeof(s5_req));
1055 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
1102 buffer_append(&c->output, (char *)&s5_rsp, sizeof(s5_rsp));
1103 buffer_append(&c->output, (char *)&dest_addr, sizeof(struct in_addr));
1104 buffer_append(&c->output, (char *)&dest_port, sizeof(dest_port));
1105 return 1;
1106}
1107
Ben Lindstromb3921512001-04-11 15:57:50 +00001108/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001109static void
Ben Lindstromb3921512001-04-11 15:57:50 +00001110channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
1111{
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 Millerb38eff82000-04-01 11:09:21 +10001154channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
1155{
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 Millerb38eff82000-04-01 11:09:21 +10001279channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
1280{
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 Millerb38eff82000-04-01 11:09:21 +10001336channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
1337{
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
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001369channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
1370{
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 Millerb38eff82000-04-01 11:09:21 +10001415channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
1416{
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)) {
1422 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +10001423 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1424 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001425 if (len <= 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001426 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001427 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001428 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001429 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001430 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001431 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001432 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001433 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001434 c->type = SSH_CHANNEL_INPUT_DRAINING;
Damien Millerfbdeece2003-09-02 22:52:31 +10001435 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001436 } else {
1437 chan_read_failed(c);
1438 }
1439 return -1;
1440 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001441 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001442 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001443 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001444 chan_read_failed(c);
1445 }
Damien Millerd27b9472005-12-13 19:29:02 +11001446 } else if (c->datagram) {
1447 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001448 } else {
1449 buffer_append(&c->input, buf, len);
1450 }
Damien Millerb38eff82000-04-01 11:09:21 +10001451 }
1452 return 1;
1453}
Ben Lindstrombba81212001-06-25 05:01:22 +00001454static int
Damien Millerb38eff82000-04-01 11:09:21 +10001455channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
1456{
Ben Lindstrome229b252001-03-05 06:28:06 +00001457 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001458 u_char *data = NULL, *buf;
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001459 u_int dlen;
Damien Millerb38eff82000-04-01 11:09:21 +10001460 int len;
1461
1462 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001463 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001464 FD_ISSET(c->wfd, writeset) &&
1465 buffer_len(&c->output) > 0) {
Damien Miller077b2382005-12-31 16:22:32 +11001466 if (c->output_filter != NULL) {
1467 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1468 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001469 if (c->type != SSH_CHANNEL_OPEN)
1470 chan_mark_dead(c);
1471 else
1472 chan_write_failed(c);
1473 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001474 }
1475 } else if (c->datagram) {
1476 buf = data = buffer_get_string(&c->output, &dlen);
1477 } else {
1478 buf = data = buffer_ptr(&c->output);
1479 dlen = buffer_len(&c->output);
1480 }
1481
Damien Millerd27b9472005-12-13 19:29:02 +11001482 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001483 /* ignore truncated writes, datagrams might get lost */
1484 c->local_consumed += dlen + 4;
Damien Miller077b2382005-12-31 16:22:32 +11001485 len = write(c->wfd, buf, dlen);
Damien Millerd27b9472005-12-13 19:29:02 +11001486 xfree(data);
1487 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1488 return 1;
1489 if (len <= 0) {
1490 if (c->type != SSH_CHANNEL_OPEN)
1491 chan_mark_dead(c);
1492 else
1493 chan_write_failed(c);
1494 return -1;
1495 }
1496 return 1;
1497 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001498#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001499 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker240fdfa2003-11-22 14:10:02 +11001500 if (compat20 && c->wfd_isatty)
1501 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001502#endif
Damien Miller077b2382005-12-31 16:22:32 +11001503
1504 len = write(c->wfd, buf, dlen);
Damien Miller6f83b8e2000-05-02 09:23:45 +10001505 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1506 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001507 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001508 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001509 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001510 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001511 return -1;
1512 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001513 buffer_clear(&c->output);
Damien Millerfbdeece2003-09-02 22:52:31 +10001514 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001515 c->type = SSH_CHANNEL_INPUT_DRAINING;
1516 } else {
1517 chan_write_failed(c);
1518 }
1519 return -1;
1520 }
Damien Miller077b2382005-12-31 16:22:32 +11001521 if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001522 if (tcgetattr(c->wfd, &tio) == 0 &&
1523 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1524 /*
1525 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001526 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001527 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001528 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001529 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001530 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001531 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001532 }
1533 }
Damien Millerb38eff82000-04-01 11:09:21 +10001534 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001535 if (compat20 && len > 0) {
1536 c->local_consumed += len;
1537 }
1538 }
1539 return 1;
1540}
Ben Lindstrombba81212001-06-25 05:01:22 +00001541static int
Damien Miller33b13562000-04-04 14:38:59 +10001542channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
1543{
Darren Tucker11327cc2005-03-14 23:22:25 +11001544 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001545 int len;
1546
Damien Miller1383bd82000-04-06 12:32:37 +10001547/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001548 if (c->efd != -1) {
1549 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1550 FD_ISSET(c->efd, writeset) &&
1551 buffer_len(&c->extended) > 0) {
1552 len = write(c->efd, buffer_ptr(&c->extended),
1553 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001554 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001555 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001556 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1557 return 1;
1558 if (len <= 0) {
1559 debug2("channel %d: closing write-efd %d",
1560 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001561 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001562 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001563 buffer_consume(&c->extended, len);
1564 c->local_consumed += len;
1565 }
1566 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1567 FD_ISSET(c->efd, readset)) {
1568 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001569 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001570 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001571 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1572 return 1;
1573 if (len <= 0) {
1574 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001575 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001576 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001577 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001578 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001579 }
Damien Miller33b13562000-04-04 14:38:59 +10001580 }
1581 }
1582 return 1;
1583}
Ben Lindstrombba81212001-06-25 05:01:22 +00001584static int
Damien Miller0e220db2004-06-15 10:34:08 +10001585channel_handle_ctl(Channel *c, fd_set * readset, fd_set * writeset)
1586{
1587 char buf[16];
1588 int len;
1589
1590 /* Monitor control fd to detect if the slave client exits */
1591 if (c->ctl_fd != -1 && FD_ISSET(c->ctl_fd, readset)) {
1592 len = read(c->ctl_fd, buf, sizeof(buf));
1593 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1594 return 1;
1595 if (len <= 0) {
1596 debug2("channel %d: ctl read<=0", c->self);
1597 if (c->type != SSH_CHANNEL_OPEN) {
1598 debug2("channel %d: not open", c->self);
1599 chan_mark_dead(c);
1600 return -1;
1601 } else {
1602 chan_read_failed(c);
1603 chan_write_failed(c);
1604 }
1605 return -1;
1606 } else
1607 fatal("%s: unexpected data on ctl fd", __func__);
1608 }
1609 return 1;
1610}
1611static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001612channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001613{
Ben Lindstromb3921512001-04-11 15:57:50 +00001614 if (c->type == SSH_CHANNEL_OPEN &&
1615 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001616 c->local_window < c->local_window_max/2 &&
1617 c->local_consumed > 0) {
1618 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1619 packet_put_int(c->remote_id);
1620 packet_put_int(c->local_consumed);
1621 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001622 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001623 c->self, c->local_window,
1624 c->local_consumed);
1625 c->local_window += c->local_consumed;
1626 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001627 }
1628 return 1;
1629}
1630
Ben Lindstrombba81212001-06-25 05:01:22 +00001631static void
Damien Millerde6987c2002-01-22 23:20:40 +11001632channel_post_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001633{
Damien Miller4623a752001-10-10 15:03:58 +10001634 if (c->delayed)
1635 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001636 channel_handle_rfd(c, readset, writeset);
1637 channel_handle_wfd(c, readset, writeset);
Damien Millerde6987c2002-01-22 23:20:40 +11001638 if (!compat20)
Damien Miller4623a752001-10-10 15:03:58 +10001639 return;
Damien Miller33b13562000-04-04 14:38:59 +10001640 channel_handle_efd(c, readset, writeset);
Damien Miller0e220db2004-06-15 10:34:08 +10001641 channel_handle_ctl(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001642 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001643}
1644
Ben Lindstrombba81212001-06-25 05:01:22 +00001645static void
Damien Millerb38eff82000-04-01 11:09:21 +10001646channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1647{
1648 int len;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001649
Damien Millerb38eff82000-04-01 11:09:21 +10001650 /* Send buffered output data to the socket. */
1651 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1652 len = write(c->sock, buffer_ptr(&c->output),
1653 buffer_len(&c->output));
1654 if (len <= 0)
Damien Miller76765c02002-01-22 23:21:15 +11001655 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001656 else
1657 buffer_consume(&c->output, len);
1658 }
1659}
1660
Ben Lindstrombba81212001-06-25 05:01:22 +00001661static void
Damien Miller33b13562000-04-04 14:38:59 +10001662channel_handler_init_20(void)
1663{
Damien Millerde6987c2002-01-22 23:20:40 +11001664 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001665 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001666 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001667 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001668 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001669 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001670 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001671 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001672
Damien Millerde6987c2002-01-22 23:20:40 +11001673 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001674 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001675 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001676 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001677 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001678 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001679 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001680}
1681
Ben Lindstrombba81212001-06-25 05:01:22 +00001682static void
Damien Millerb38eff82000-04-01 11:09:21 +10001683channel_handler_init_13(void)
1684{
1685 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1686 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1687 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1688 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1689 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1690 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1691 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001692 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001693 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001694
Damien Millerde6987c2002-01-22 23:20:40 +11001695 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001696 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1697 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1698 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1699 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001700 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001701 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001702}
1703
Ben Lindstrombba81212001-06-25 05:01:22 +00001704static void
Damien Millerb38eff82000-04-01 11:09:21 +10001705channel_handler_init_15(void)
1706{
Damien Millerde6987c2002-01-22 23:20:40 +11001707 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001708 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001709 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1710 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1711 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001712 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001713 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001714
1715 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1716 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1717 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Damien Millerde6987c2002-01-22 23:20:40 +11001718 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001719 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001720 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001721}
1722
Ben Lindstrombba81212001-06-25 05:01:22 +00001723static void
Damien Millerb38eff82000-04-01 11:09:21 +10001724channel_handler_init(void)
1725{
1726 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001727
Damien Miller9f0f5c62001-12-21 14:45:46 +11001728 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001729 channel_pre[i] = NULL;
1730 channel_post[i] = NULL;
1731 }
Damien Miller33b13562000-04-04 14:38:59 +10001732 if (compat20)
1733 channel_handler_init_20();
1734 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001735 channel_handler_init_13();
1736 else
1737 channel_handler_init_15();
1738}
1739
Damien Miller3ec27592001-10-12 11:35:04 +10001740/* gc dead channels */
1741static void
1742channel_garbage_collect(Channel *c)
1743{
1744 if (c == NULL)
1745 return;
1746 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11001747 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10001748 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001749 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001750 c->detach_user(c->self, NULL);
1751 /* if we still have a callback */
1752 if (c->detach_user != NULL)
1753 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001754 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001755 }
1756 if (!chan_is_dead(c, 1))
1757 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001758 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001759 channel_free(c);
1760}
1761
Ben Lindstrombba81212001-06-25 05:01:22 +00001762static void
Damien Millerb38eff82000-04-01 11:09:21 +10001763channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1764{
1765 static int did_init = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001766 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001767 Channel *c;
1768
1769 if (!did_init) {
1770 channel_handler_init();
1771 did_init = 1;
1772 }
1773 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001774 c = channels[i];
1775 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001776 continue;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001777 if (ftab[c->type] != NULL)
1778 (*ftab[c->type])(c, readset, writeset);
Damien Miller3ec27592001-10-12 11:35:04 +10001779 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001780 }
1781}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001782
Ben Lindstrome9c99912001-06-09 00:41:05 +00001783/*
1784 * Allocate/update select bitmasks and add any bits relevant to channels in
1785 * select bitmasks.
1786 */
Damien Miller4af51302000-04-16 11:18:38 +10001787void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001788channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001789 u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001790{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001791 u_int n, sz;
Damien Miller5e953212001-01-30 09:14:00 +11001792
1793 n = MAX(*maxfdp, channel_max_fd);
1794
1795 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001796 /* perhaps check sz < nalloc/2 and shrink? */
1797 if (*readsetp == NULL || sz > *nallocp) {
1798 *readsetp = xrealloc(*readsetp, sz);
1799 *writesetp = xrealloc(*writesetp, sz);
1800 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11001801 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001802 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11001803 memset(*readsetp, 0, sz);
1804 memset(*writesetp, 0, sz);
1805
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001806 if (!rekeying)
1807 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001808}
1809
Ben Lindstrome9c99912001-06-09 00:41:05 +00001810/*
1811 * After select, perform any appropriate operations for channels which have
1812 * events pending.
1813 */
Damien Miller4af51302000-04-16 11:18:38 +10001814void
Damien Miller95def091999-11-25 00:26:21 +11001815channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001816{
Damien Millerb38eff82000-04-01 11:09:21 +10001817 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001818}
1819
Ben Lindstrome9c99912001-06-09 00:41:05 +00001820
Damien Miller5e953212001-01-30 09:14:00 +11001821/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001822
Damien Miller4af51302000-04-16 11:18:38 +10001823void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001824channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001825{
Damien Millerb38eff82000-04-01 11:09:21 +10001826 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001827 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001828
Damien Miller95def091999-11-25 00:26:21 +11001829 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001830 c = channels[i];
1831 if (c == NULL)
1832 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001833
Ben Lindstrome9c99912001-06-09 00:41:05 +00001834 /*
1835 * We are only interested in channels that can have buffered
1836 * incoming data.
1837 */
Damien Miller34132e52000-01-14 15:45:46 +11001838 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001839 if (c->type != SSH_CHANNEL_OPEN &&
1840 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001841 continue;
1842 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001843 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001844 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001845 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001846 if (compat20 &&
1847 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001848 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10001849 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001850 continue;
1851 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001852
Damien Miller95def091999-11-25 00:26:21 +11001853 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001854 if ((c->istate == CHAN_INPUT_OPEN ||
1855 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1856 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001857 if (c->datagram) {
1858 if (len > 0) {
1859 u_char *data;
1860 u_int dlen;
1861
1862 data = buffer_get_string(&c->input,
1863 &dlen);
1864 packet_start(SSH2_MSG_CHANNEL_DATA);
1865 packet_put_int(c->remote_id);
1866 packet_put_string(data, dlen);
1867 packet_send();
1868 c->remote_window -= dlen + 4;
1869 xfree(data);
1870 }
1871 continue;
1872 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001873 /*
1874 * Send some data for the other side over the secure
1875 * connection.
1876 */
Damien Miller33b13562000-04-04 14:38:59 +10001877 if (compat20) {
1878 if (len > c->remote_window)
1879 len = c->remote_window;
1880 if (len > c->remote_maxpacket)
1881 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001882 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001883 if (packet_is_interactive()) {
1884 if (len > 1024)
1885 len = 512;
1886 } else {
1887 /* Keep the packets at reasonable size. */
1888 if (len > packet_get_maxsize()/2)
1889 len = packet_get_maxsize()/2;
1890 }
Damien Miller95def091999-11-25 00:26:21 +11001891 }
Damien Millerb38eff82000-04-01 11:09:21 +10001892 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001893 packet_start(compat20 ?
1894 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001895 packet_put_int(c->remote_id);
1896 packet_put_string(buffer_ptr(&c->input), len);
1897 packet_send();
1898 buffer_consume(&c->input, len);
1899 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001900 }
1901 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001902 if (compat13)
1903 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001904 /*
1905 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00001906 * tell peer, that we will not send more data: send IEOF.
1907 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11001908 */
Ben Lindstromcf159442002-03-26 03:26:24 +00001909 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10001910 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
1911 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00001912 else
1913 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001914 }
Damien Miller33b13562000-04-04 14:38:59 +10001915 /* Send extended data, i.e. stderr */
1916 if (compat20 &&
Ben Lindstromcf159442002-03-26 03:26:24 +00001917 !(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10001918 c->remote_window > 0 &&
1919 (len = buffer_len(&c->extended)) > 0 &&
1920 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001921 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001922 c->self, c->remote_window, buffer_len(&c->extended),
1923 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001924 if (len > c->remote_window)
1925 len = c->remote_window;
1926 if (len > c->remote_maxpacket)
1927 len = c->remote_maxpacket;
1928 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1929 packet_put_int(c->remote_id);
1930 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1931 packet_put_string(buffer_ptr(&c->extended), len);
1932 packet_send();
1933 buffer_consume(&c->extended, len);
1934 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001935 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001936 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001937 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001938}
1939
Ben Lindstrome9c99912001-06-09 00:41:05 +00001940
1941/* -- protocol input */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001942
Damien Miller4af51302000-04-16 11:18:38 +10001943void
Damien Miller630d6f42002-01-22 23:17:30 +11001944channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001945{
Damien Miller34132e52000-01-14 15:45:46 +11001946 int id;
Damien Miller95def091999-11-25 00:26:21 +11001947 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001948 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001949 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001950
Damien Miller95def091999-11-25 00:26:21 +11001951 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001952 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001953 c = channel_lookup(id);
1954 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001955 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001956
Damien Miller95def091999-11-25 00:26:21 +11001957 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001958 if (c->type != SSH_CHANNEL_OPEN &&
1959 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001960 return;
1961
Damien Miller95def091999-11-25 00:26:21 +11001962 /* Get the data. */
1963 data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001964
Damien Millera04ad492004-01-21 11:02:09 +11001965 /*
1966 * Ignore data for protocol > 1.3 if output end is no longer open.
1967 * For protocol 2 the sending side is reducing its window as it sends
1968 * data, so we must 'fake' consumption of the data in order to ensure
1969 * that window updates are sent back. Otherwise the connection might
1970 * deadlock.
1971 */
1972 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
1973 if (compat20) {
1974 c->local_window -= data_len;
1975 c->local_consumed += data_len;
1976 }
1977 xfree(data);
1978 return;
1979 }
1980
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001981 if (compat20) {
Damien Miller33b13562000-04-04 14:38:59 +10001982 if (data_len > c->local_maxpacket) {
Damien Miller996acd22003-04-09 20:59:48 +10001983 logit("channel %d: rcvd big packet %d, maxpack %d",
Damien Miller33b13562000-04-04 14:38:59 +10001984 c->self, data_len, c->local_maxpacket);
1985 }
1986 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10001987 logit("channel %d: rcvd too much data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10001988 c->self, data_len, c->local_window);
1989 xfree(data);
1990 return;
1991 }
1992 c->local_window -= data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001993 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001994 packet_check_eom();
Damien Millerd27b9472005-12-13 19:29:02 +11001995 if (c->datagram)
1996 buffer_put_string(&c->output, data, data_len);
1997 else
1998 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001999 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002000}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002001
Damien Miller4af51302000-04-16 11:18:38 +10002002void
Damien Miller630d6f42002-01-22 23:17:30 +11002003channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002004{
2005 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002006 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002007 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002008 Channel *c;
2009
2010 /* Get the channel number and verify it. */
2011 id = packet_get_int();
2012 c = channel_lookup(id);
2013
2014 if (c == NULL)
2015 packet_disconnect("Received extended_data for bad channel %d.", id);
2016 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002017 logit("channel %d: ext data for non open", id);
Damien Miller33b13562000-04-04 14:38:59 +10002018 return;
2019 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002020 if (c->flags & CHAN_EOF_RCVD) {
2021 if (datafellows & SSH_BUG_EXTEOF)
2022 debug("channel %d: accepting ext data after eof", id);
2023 else
2024 packet_disconnect("Received extended_data after EOF "
2025 "on channel %d.", id);
2026 }
Damien Miller33b13562000-04-04 14:38:59 +10002027 tcode = packet_get_int();
2028 if (c->efd == -1 ||
2029 c->extended_usage != CHAN_EXTENDED_WRITE ||
2030 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002031 logit("channel %d: bad ext data", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002032 return;
2033 }
2034 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002035 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002036 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002037 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002038 c->self, data_len, c->local_window);
2039 xfree(data);
2040 return;
2041 }
Damien Millerd3444942000-09-30 14:20:03 +11002042 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002043 c->local_window -= data_len;
2044 buffer_append(&c->extended, data, data_len);
2045 xfree(data);
2046}
2047
Damien Miller4af51302000-04-16 11:18:38 +10002048void
Damien Miller630d6f42002-01-22 23:17:30 +11002049channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002050{
2051 int id;
2052 Channel *c;
2053
Damien Millerb38eff82000-04-01 11:09:21 +10002054 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002055 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002056 c = channel_lookup(id);
2057 if (c == NULL)
2058 packet_disconnect("Received ieof for nonexistent channel %d.", id);
2059 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002060
2061 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002062 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002063 debug("channel %d: FORCE input drain", c->self);
2064 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002065 if (buffer_len(&c->input) == 0)
2066 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002067 }
2068
Damien Millerb38eff82000-04-01 11:09:21 +10002069}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002070
Damien Miller4af51302000-04-16 11:18:38 +10002071void
Damien Miller630d6f42002-01-22 23:17:30 +11002072channel_input_close(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002073{
Damien Millerb38eff82000-04-01 11:09:21 +10002074 int id;
2075 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002076
Damien Millerb38eff82000-04-01 11:09:21 +10002077 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002078 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002079 c = channel_lookup(id);
2080 if (c == NULL)
2081 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11002082
2083 /*
2084 * Send a confirmation that we have closed the channel and no more
2085 * data is coming for it.
2086 */
Damien Miller95def091999-11-25 00:26:21 +11002087 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10002088 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11002089 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002090
Damien Miller5428f641999-11-25 11:54:57 +11002091 /*
2092 * If the channel is in closed state, we have sent a close request,
2093 * and the other side will eventually respond with a confirmation.
2094 * Thus, we cannot free the channel here, because then there would be
2095 * no-one to receive the confirmation. The channel gets freed when
2096 * the confirmation arrives.
2097 */
Damien Millerb38eff82000-04-01 11:09:21 +10002098 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11002099 /*
2100 * Not a closed channel - mark it as draining, which will
2101 * cause it to be freed later.
2102 */
Damien Miller76765c02002-01-22 23:21:15 +11002103 buffer_clear(&c->input);
Damien Millerb38eff82000-04-01 11:09:21 +10002104 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11002105 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002106}
2107
Damien Millerb38eff82000-04-01 11:09:21 +10002108/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10002109void
Damien Miller630d6f42002-01-22 23:17:30 +11002110channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002111{
Damien Millerb38eff82000-04-01 11:09:21 +10002112 int id = packet_get_int();
2113 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002114
Damien Miller48b03fc2002-01-22 23:11:40 +11002115 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002116 if (c == NULL)
2117 packet_disconnect("Received oclose for nonexistent channel %d.", id);
2118 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002119}
2120
Damien Miller4af51302000-04-16 11:18:38 +10002121void
Damien Miller630d6f42002-01-22 23:17:30 +11002122channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002123{
2124 int id = packet_get_int();
2125 Channel *c = channel_lookup(id);
2126
Damien Miller48b03fc2002-01-22 23:11:40 +11002127 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002128 if (c == NULL)
2129 packet_disconnect("Received close confirmation for "
2130 "out-of-range channel %d.", id);
2131 if (c->type != SSH_CHANNEL_CLOSED)
2132 packet_disconnect("Received close confirmation for "
2133 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002134 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002135}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002136
Damien Miller4af51302000-04-16 11:18:38 +10002137void
Damien Miller630d6f42002-01-22 23:17:30 +11002138channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002139{
Damien Millerb38eff82000-04-01 11:09:21 +10002140 int id, remote_id;
2141 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002142
Damien Millerb38eff82000-04-01 11:09:21 +10002143 id = packet_get_int();
2144 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002145
Damien Millerb38eff82000-04-01 11:09:21 +10002146 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2147 packet_disconnect("Received open confirmation for "
2148 "non-opening channel %d.", id);
2149 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002150 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002151 c->remote_id = remote_id;
2152 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002153
2154 if (compat20) {
2155 c->remote_window = packet_get_int();
2156 c->remote_maxpacket = packet_get_int();
Damien Miller67f0bc02002-02-05 12:23:08 +11002157 if (c->confirm) {
Damien Millerd3444942000-09-30 14:20:03 +11002158 debug2("callback start");
Damien Miller0e220db2004-06-15 10:34:08 +10002159 c->confirm(c->self, c->confirm_ctx);
Damien Millerd3444942000-09-30 14:20:03 +11002160 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002161 }
Damien Millerfbdeece2003-09-02 22:52:31 +10002162 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
Damien Miller33b13562000-04-04 14:38:59 +10002163 c->remote_window, c->remote_maxpacket);
2164 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002165 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002166}
2167
Ben Lindstrombba81212001-06-25 05:01:22 +00002168static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002169reason2txt(int reason)
2170{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002171 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002172 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2173 return "administratively prohibited";
2174 case SSH2_OPEN_CONNECT_FAILED:
2175 return "connect failed";
2176 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2177 return "unknown channel type";
2178 case SSH2_OPEN_RESOURCE_SHORTAGE:
2179 return "resource shortage";
2180 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002181 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002182}
2183
Damien Miller4af51302000-04-16 11:18:38 +10002184void
Damien Miller630d6f42002-01-22 23:17:30 +11002185channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002186{
Kevin Steves12057502001-02-05 14:54:34 +00002187 int id, reason;
2188 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002189 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002190
Damien Millerb38eff82000-04-01 11:09:21 +10002191 id = packet_get_int();
2192 c = channel_lookup(id);
2193
2194 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2195 packet_disconnect("Received open failure for "
2196 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002197 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00002198 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00002199 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00002200 msg = packet_get_string(NULL);
2201 lang = packet_get_string(NULL);
2202 }
Damien Miller996acd22003-04-09 20:59:48 +10002203 logit("channel %d: open failed: %s%s%s", id,
Ben Lindstrom69128662001-05-08 20:07:39 +00002204 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00002205 if (msg != NULL)
2206 xfree(msg);
2207 if (lang != NULL)
2208 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10002209 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002210 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11002211 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002212 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002213}
2214
Damien Miller33b13562000-04-04 14:38:59 +10002215void
Damien Miller630d6f42002-01-22 23:17:30 +11002216channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002217{
2218 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002219 int id;
2220 u_int adjust;
Damien Miller33b13562000-04-04 14:38:59 +10002221
2222 if (!compat20)
2223 return;
2224
2225 /* Get the channel number and verify it. */
2226 id = packet_get_int();
2227 c = channel_lookup(id);
2228
Damien Millerd47c62a2005-12-13 19:33:57 +11002229 if (c == NULL) {
2230 logit("Received window adjust for non-open channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002231 return;
2232 }
2233 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002234 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002235 debug2("channel %d: rcvd adjust %u", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10002236 c->remote_window += adjust;
2237}
2238
Damien Miller4af51302000-04-16 11:18:38 +10002239void
Damien Miller630d6f42002-01-22 23:17:30 +11002240channel_input_port_open(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002241{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002242 Channel *c = NULL;
2243 u_short host_port;
2244 char *host, *originator_string;
2245 int remote_id, sock = -1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002246
Ben Lindstrome9c99912001-06-09 00:41:05 +00002247 remote_id = packet_get_int();
2248 host = packet_get_string(NULL);
2249 host_port = packet_get_int();
2250
2251 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2252 originator_string = packet_get_string(NULL);
2253 } else {
2254 originator_string = xstrdup("unknown (remote did not supply name)");
2255 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002256 packet_check_eom();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002257 sock = channel_connect_to(host, host_port);
2258 if (sock != -1) {
2259 c = channel_new("connected socket",
2260 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2261 originator_string, 1);
Damien Miller699d0032002-02-08 22:07:16 +11002262 c->remote_id = remote_id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002263 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002264 xfree(originator_string);
Ben Lindstrome9c99912001-06-09 00:41:05 +00002265 if (c == NULL) {
2266 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2267 packet_put_int(remote_id);
2268 packet_send();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002269 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002270 xfree(host);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002271}
2272
2273
Ben Lindstrome9c99912001-06-09 00:41:05 +00002274/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002275
Ben Lindstrom908afed2001-10-03 17:34:59 +00002276void
2277channel_set_af(int af)
2278{
2279 IPv4or6 = af;
2280}
2281
Damien Millerb16461c2002-01-22 23:29:22 +11002282static int
2283channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
2284 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002285{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002286 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11002287 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11002288 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002289 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11002290 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002291
Damien Millerb16461c2002-01-22 23:29:22 +11002292 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2293 listen_addr : host_to_connect;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002294 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00002295
Damien Millerb16461c2002-01-22 23:29:22 +11002296 if (host == NULL) {
2297 error("No forward host name.");
Damien Millera7270302005-07-06 09:36:05 +10002298 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002299 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002300 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00002301 error("Forward host name too long.");
Damien Millera7270302005-07-06 09:36:05 +10002302 return 0;
Kevin Steves12057502001-02-05 14:54:34 +00002303 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002304
Damien Miller5428f641999-11-25 11:54:57 +11002305 /*
Damien Millerf91ee4c2005-03-01 21:24:33 +11002306 * Determine whether or not a port forward listens to loopback,
Darren Tucker47eede72005-03-14 23:08:12 +11002307 * specified address or wildcard. On the client, a specified bind
2308 * address will always override gateway_ports. On the server, a
2309 * gateway_ports of 1 (``yes'') will override the client's
2310 * specification and force a wildcard bind, whereas a value of 2
2311 * (``clientspecified'') will bind to whatever address the client
Damien Millerf91ee4c2005-03-01 21:24:33 +11002312 * asked for.
2313 *
2314 * Special-case listen_addrs are:
2315 *
2316 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2317 * "" (empty string), "*" -> wildcard v4/v6
2318 * "localhost" -> loopback v4/v6
2319 */
2320 addr = NULL;
2321 if (listen_addr == NULL) {
2322 /* No address specified: default to gateway_ports setting */
2323 if (gateway_ports)
2324 wildcard = 1;
2325 } else if (gateway_ports || is_client) {
2326 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2327 strcmp(listen_addr, "0.0.0.0") == 0) ||
2328 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
2329 (!is_client && gateway_ports == 1))
2330 wildcard = 1;
2331 else if (strcmp(listen_addr, "localhost") != 0)
2332 addr = listen_addr;
2333 }
2334
2335 debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s",
2336 type, wildcard, (addr == NULL) ? "NULL" : addr);
2337
2338 /*
Damien Miller34132e52000-01-14 15:45:46 +11002339 * getaddrinfo returns a loopback address if the hostname is
2340 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002341 */
Damien Miller34132e52000-01-14 15:45:46 +11002342 memset(&hints, 0, sizeof(hints));
2343 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002344 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002345 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002346 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002347 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2348 if (addr == NULL) {
2349 /* This really shouldn't happen */
2350 packet_disconnect("getaddrinfo: fatal error: %s",
2351 gai_strerror(r));
2352 } else {
Damien Millera7270302005-07-06 09:36:05 +10002353 error("channel_setup_fwd_listener: "
Damien Millerf91ee4c2005-03-01 21:24:33 +11002354 "getaddrinfo(%.64s): %s", addr, gai_strerror(r));
2355 }
Damien Millera7270302005-07-06 09:36:05 +10002356 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002357 }
Damien Miller5428f641999-11-25 11:54:57 +11002358
Damien Miller34132e52000-01-14 15:45:46 +11002359 for (ai = aitop; ai; ai = ai->ai_next) {
2360 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2361 continue;
2362 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2363 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Millerb16461c2002-01-22 23:29:22 +11002364 error("channel_setup_fwd_listener: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002365 continue;
2366 }
2367 /* Create a port to listen for the host. */
Damien Miller2372ace2003-05-14 13:42:23 +10002368 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002369 if (sock < 0) {
2370 /* this is no error since kernel may not support ipv6 */
2371 verbose("socket: %.100s", strerror(errno));
2372 continue;
2373 }
Damien Miller5e7fd072005-11-05 14:53:39 +11002374
2375 channel_set_reuseaddr(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10002376
Damien Miller34132e52000-01-14 15:45:46 +11002377 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002378
Damien Miller34132e52000-01-14 15:45:46 +11002379 /* Bind the socket to the address. */
2380 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2381 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002382 if (!ai->ai_next)
2383 error("bind: %.100s", strerror(errno));
2384 else
2385 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002386
Damien Miller34132e52000-01-14 15:45:46 +11002387 close(sock);
2388 continue;
2389 }
2390 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11002391 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002392 error("listen: %.100s", strerror(errno));
2393 close(sock);
2394 continue;
2395 }
2396 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002397 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002398 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002399 0, "port listener", 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002400 strlcpy(c->path, host, sizeof(c->path));
2401 c->host_port = port_to_connect;
2402 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002403 success = 1;
2404 }
2405 if (success == 0)
Damien Millerb16461c2002-01-22 23:29:22 +11002406 error("channel_setup_fwd_listener: cannot listen to port: %d",
Kevin Steves12057502001-02-05 14:54:34 +00002407 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002408 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002409 return success;
Damien Miller95def091999-11-25 00:26:21 +11002410}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002411
Darren Tuckere7066df2004-05-24 10:18:05 +10002412int
2413channel_cancel_rport_listener(const char *host, u_short port)
2414{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002415 u_int i;
2416 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10002417
Darren Tucker47eede72005-03-14 23:08:12 +11002418 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10002419 Channel *c = channels[i];
2420
2421 if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
2422 strncmp(c->path, host, sizeof(c->path)) == 0 &&
Darren Tuckerfc959702004-07-17 16:12:08 +10002423 c->listening_port == port) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10002424 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10002425 channel_free(c);
2426 found = 1;
2427 }
2428 }
2429
2430 return (found);
2431}
2432
Damien Millerb16461c2002-01-22 23:29:22 +11002433/* protocol local port fwd, used by ssh (and sshd in v1) */
2434int
Damien Millerf91ee4c2005-03-01 21:24:33 +11002435channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
Damien Millerb16461c2002-01-22 23:29:22 +11002436 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2437{
2438 return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002439 listen_host, listen_port, host_to_connect, port_to_connect,
2440 gateway_ports);
Damien Millerb16461c2002-01-22 23:29:22 +11002441}
2442
2443/* protocol v2 remote port fwd, used by sshd */
2444int
2445channel_setup_remote_fwd_listener(const char *listen_address,
2446 u_short listen_port, int gateway_ports)
2447{
2448 return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
2449 listen_address, listen_port, NULL, 0, gateway_ports);
2450}
2451
Damien Miller5428f641999-11-25 11:54:57 +11002452/*
2453 * Initiate forwarding of connections to port "port" on remote host through
2454 * the secure channel to host:port from local side.
2455 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002456
Damien Miller4af51302000-04-16 11:18:38 +10002457void
Damien Millerf91ee4c2005-03-01 21:24:33 +11002458channel_request_remote_forwarding(const char *listen_host, u_short listen_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +11002459 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002460{
Damien Millerdff50992002-01-22 23:16:32 +11002461 int type, success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002462
Damien Miller95def091999-11-25 00:26:21 +11002463 /* Record locally that connection to this host/port is permitted. */
2464 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2465 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002466
Damien Miller95def091999-11-25 00:26:21 +11002467 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002468 if (compat20) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002469 const char *address_to_bind;
2470 if (listen_host == NULL)
2471 address_to_bind = "localhost";
2472 else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0)
2473 address_to_bind = "";
2474 else
2475 address_to_bind = listen_host;
2476
Damien Miller33b13562000-04-04 14:38:59 +10002477 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2478 packet_put_cstring("tcpip-forward");
Damien Miller2797f7f2002-04-23 21:09:44 +10002479 packet_put_char(1); /* boolean: want reply */
Damien Miller33b13562000-04-04 14:38:59 +10002480 packet_put_cstring(address_to_bind);
2481 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002482 packet_send();
2483 packet_write_wait();
2484 /* Assume that server accepts the request */
2485 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002486 } else {
2487 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002488 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002489 packet_put_cstring(host_to_connect);
2490 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002491 packet_send();
2492 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002493
2494 /* Wait for response from the remote side. */
Damien Millerdff50992002-01-22 23:16:32 +11002495 type = packet_read();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002496 switch (type) {
2497 case SSH_SMSG_SUCCESS:
2498 success = 1;
2499 break;
2500 case SSH_SMSG_FAILURE:
Damien Miller996acd22003-04-09 20:59:48 +10002501 logit("Warning: Server denied remote port forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11002502 break;
2503 default:
2504 /* Unknown packet */
2505 packet_disconnect("Protocol error for port forward request:"
2506 "received packet type %d.", type);
2507 }
2508 }
2509 if (success) {
2510 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2511 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2512 permitted_opens[num_permitted_opens].listen_port = listen_port;
2513 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002514 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002515}
2516
Damien Miller5428f641999-11-25 11:54:57 +11002517/*
Darren Tuckerfc959702004-07-17 16:12:08 +10002518 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10002519 * local side.
2520 */
Darren Tuckere7066df2004-05-24 10:18:05 +10002521void
Damien Millerf91ee4c2005-03-01 21:24:33 +11002522channel_request_rforward_cancel(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10002523{
2524 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10002525
2526 if (!compat20)
2527 return;
2528
2529 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckerfc959702004-07-17 16:12:08 +10002530 if (permitted_opens[i].host_to_connect != NULL &&
Darren Tuckere7066df2004-05-24 10:18:05 +10002531 permitted_opens[i].listen_port == port)
2532 break;
2533 }
2534 if (i >= num_permitted_opens) {
2535 debug("%s: requested forward not found", __func__);
2536 return;
2537 }
2538 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2539 packet_put_cstring("cancel-tcpip-forward");
2540 packet_put_char(0);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002541 packet_put_cstring(host == NULL ? "" : host);
Darren Tuckere7066df2004-05-24 10:18:05 +10002542 packet_put_int(port);
2543 packet_send();
2544
2545 permitted_opens[i].listen_port = 0;
2546 permitted_opens[i].port_to_connect = 0;
Damien Miller0a0176e2005-11-05 15:07:59 +11002547 xfree(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10002548 permitted_opens[i].host_to_connect = NULL;
2549}
2550
2551/*
Damien Miller5428f641999-11-25 11:54:57 +11002552 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2553 * listening for the port, and sends back a success reply (or disconnect
2554 * message if there was an error). This never returns if there was an error.
2555 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002556
Damien Miller4af51302000-04-16 11:18:38 +10002557void
Damien Millere247cc42000-05-07 12:03:14 +10002558channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002559{
Damien Milleraae6c611999-12-06 11:47:28 +11002560 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11002561 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002562
Damien Miller95def091999-11-25 00:26:21 +11002563 /* Get arguments from the packet. */
2564 port = packet_get_int();
2565 hostname = packet_get_string(NULL);
2566 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002567
Damien Millerbac2d8a2000-09-05 16:13:06 +11002568#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002569 /*
2570 * Check that an unprivileged user is not trying to forward a
2571 * privileged port.
2572 */
Damien Miller95def091999-11-25 00:26:21 +11002573 if (port < IPPORT_RESERVED && !is_root)
Darren Tucker9189ff82003-07-03 13:52:04 +10002574 packet_disconnect(
2575 "Requested forwarding of port %d but user is not root.",
2576 port);
2577 if (host_port == 0)
2578 packet_disconnect("Dynamic forwarding denied.");
Damien Millerbac2d8a2000-09-05 16:13:06 +11002579#endif
Darren Tucker9189ff82003-07-03 13:52:04 +10002580
Damien Miller0bc1bd82000-11-13 22:57:25 +11002581 /* Initiate forwarding */
Damien Millerf91ee4c2005-03-01 21:24:33 +11002582 channel_setup_local_fwd_listener(NULL, port, hostname,
2583 host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002584
2585 /* Free the argument string. */
2586 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002587}
2588
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002589/*
2590 * Permits opening to any host/port if permitted_opens[] is empty. This is
2591 * usually called by the server, because the user could connect to any port
2592 * anyway, and the server has no way to know but to trust the client anyway.
2593 */
2594void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002595channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002596{
2597 if (num_permitted_opens == 0)
2598 all_opens_permitted = 1;
2599}
2600
Ben Lindstroma3700052001-04-05 23:26:32 +00002601void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002602channel_add_permitted_opens(char *host, int port)
2603{
2604 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2605 fatal("channel_request_remote_forwarding: too many forwards");
2606 debug("allow port forwarding to host %s port %d", host, port);
2607
2608 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2609 permitted_opens[num_permitted_opens].port_to_connect = port;
2610 num_permitted_opens++;
2611
2612 all_opens_permitted = 0;
2613}
2614
Ben Lindstroma3700052001-04-05 23:26:32 +00002615void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002616channel_clear_permitted_opens(void)
2617{
2618 int i;
2619
2620 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002621 if (permitted_opens[i].host_to_connect != NULL)
2622 xfree(permitted_opens[i].host_to_connect);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002623 num_permitted_opens = 0;
2624
2625}
2626
2627
2628/* return socket to remote host, port */
Ben Lindstrombba81212001-06-25 05:01:22 +00002629static int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002630connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002631{
Damien Miller34132e52000-01-14 15:45:46 +11002632 struct addrinfo hints, *ai, *aitop;
2633 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2634 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002635 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002636
Damien Miller34132e52000-01-14 15:45:46 +11002637 memset(&hints, 0, sizeof(hints));
2638 hints.ai_family = IPv4or6;
2639 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002640 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002641 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002642 error("connect_to %.100s: unknown host (%s)", host,
2643 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002644 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002645 }
Damien Miller34132e52000-01-14 15:45:46 +11002646 for (ai = aitop; ai; ai = ai->ai_next) {
2647 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2648 continue;
2649 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2650 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002651 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002652 continue;
2653 }
Damien Miller2372ace2003-05-14 13:42:23 +10002654 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002655 if (sock < 0) {
Damien Millerb46b9f32003-01-10 21:45:12 +11002656 if (ai->ai_next == NULL)
2657 error("socket: %.100s", strerror(errno));
2658 else
2659 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002660 continue;
2661 }
Damien Miller232711f2004-06-15 10:35:30 +10002662 if (set_nonblock(sock) == -1)
2663 fatal("%s: set_nonblock(%d)", __func__, sock);
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002664 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2665 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002666 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002667 strerror(errno));
2668 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002669 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002670 }
2671 break; /* success */
2672
2673 }
2674 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002675 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002676 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002677 return -1;
2678 }
2679 /* success */
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00002680 set_nodelay(sock);
Damien Millerb38eff82000-04-01 11:09:21 +10002681 return sock;
2682}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002683
Damien Miller0bc1bd82000-11-13 22:57:25 +11002684int
Ben Lindstrom173e6462001-07-04 05:15:15 +00002685channel_connect_by_listen_address(u_short listen_port)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002686{
2687 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002688
Damien Miller0bc1bd82000-11-13 22:57:25 +11002689 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002690 if (permitted_opens[i].host_to_connect != NULL &&
2691 permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002692 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002693 permitted_opens[i].host_to_connect,
2694 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002695 error("WARNING: Server requests forwarding for unknown listen_port %d",
2696 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002697 return -1;
2698}
Damien Millerb38eff82000-04-01 11:09:21 +10002699
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002700/* Check if connecting to that port is permitted and connect. */
2701int
2702channel_connect_to(const char *host, u_short port)
2703{
2704 int i, permit;
2705
2706 permit = all_opens_permitted;
2707 if (!permit) {
2708 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002709 if (permitted_opens[i].host_to_connect != NULL &&
2710 permitted_opens[i].port_to_connect == port &&
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002711 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2712 permit = 1;
2713
2714 }
2715 if (!permit) {
Damien Miller996acd22003-04-09 20:59:48 +10002716 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002717 "but the request was denied.", host, port);
2718 return -1;
2719 }
2720 return connect_to(host, port);
2721}
2722
Damien Miller0e220db2004-06-15 10:34:08 +10002723void
2724channel_send_window_changes(void)
2725{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002726 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10002727 struct winsize ws;
2728
2729 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11002730 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10002731 channels[i]->type != SSH_CHANNEL_OPEN)
2732 continue;
2733 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
2734 continue;
2735 channel_request_start(i, "window-change", 0);
2736 packet_put_int(ws.ws_col);
2737 packet_put_int(ws.ws_row);
2738 packet_put_int(ws.ws_xpixel);
2739 packet_put_int(ws.ws_ypixel);
2740 packet_send();
2741 }
2742}
2743
Ben Lindstrome9c99912001-06-09 00:41:05 +00002744/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002745
Damien Miller5428f641999-11-25 11:54:57 +11002746/*
2747 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002748 * Returns 0 and a suitable display number for the DISPLAY variable
2749 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11002750 */
Kevin Steves366298c2001-12-19 17:58:01 +00002751int
Damien Miller95c249f2002-02-05 12:11:34 +11002752x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10002753 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002754{
Damien Millere7378562001-12-21 14:58:35 +11002755 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11002756 int display_number, sock;
2757 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002758 struct addrinfo hints, *ai, *aitop;
2759 char strport[NI_MAXSERV];
2760 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002761
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002762 if (chanids == NULL)
2763 return -1;
2764
Damien Millera34a28b1999-12-14 10:47:15 +11002765 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002766 display_number < MAX_DISPLAYS;
2767 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11002768 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002769 memset(&hints, 0, sizeof(hints));
2770 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11002771 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11002772 hints.ai_socktype = SOCK_STREAM;
2773 snprintf(strport, sizeof strport, "%d", port);
2774 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2775 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00002776 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002777 }
Damien Miller34132e52000-01-14 15:45:46 +11002778 for (ai = aitop; ai; ai = ai->ai_next) {
2779 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2780 continue;
Damien Miller2372ace2003-05-14 13:42:23 +10002781 sock = socket(ai->ai_family, ai->ai_socktype,
2782 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002783 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002784 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002785 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10002786 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00002787 return -1;
Damien Millere2192732000-01-17 13:22:55 +11002788 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002789 debug("x11_create_display_inet: Socket family %d not supported",
2790 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002791 continue;
2792 }
Damien Miller34132e52000-01-14 15:45:46 +11002793 }
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002794#ifdef IPV6_V6ONLY
2795 if (ai->ai_family == AF_INET6) {
2796 int on = 1;
2797 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
2798 error("setsockopt IPV6_V6ONLY: %.100s", strerror(errno));
2799 }
2800#endif
Damien Miller5e7fd072005-11-05 14:53:39 +11002801 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11002802 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002803 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002804 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002805
2806 if (ai->ai_next)
2807 continue;
2808
Damien Miller34132e52000-01-14 15:45:46 +11002809 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11002810 close(socks[n]);
2811 }
2812 num_socks = 0;
2813 break;
2814 }
2815 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002816#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002817 if (num_socks == NUM_SOCKS)
2818 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002819#else
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002820 if (x11_use_localhost) {
2821 if (num_socks == NUM_SOCKS)
2822 break;
2823 } else {
2824 break;
2825 }
Damien Miller7bcb0892000-03-11 20:45:40 +11002826#endif
Damien Miller95def091999-11-25 00:26:21 +11002827 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002828 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002829 if (num_socks > 0)
2830 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002831 }
Damien Miller95def091999-11-25 00:26:21 +11002832 if (display_number >= MAX_DISPLAYS) {
2833 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00002834 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002835 }
Damien Miller95def091999-11-25 00:26:21 +11002836 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002837 for (n = 0; n < num_socks; n++) {
2838 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11002839 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002840 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002841 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00002842 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11002843 }
Damien Miller95def091999-11-25 00:26:21 +11002844 }
Damien Miller34132e52000-01-14 15:45:46 +11002845
Damien Miller34132e52000-01-14 15:45:46 +11002846 /* Allocate a channel for each socket. */
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002847 *chanids = xmalloc(sizeof(**chanids) * (num_socks + 1));
Damien Miller34132e52000-01-14 15:45:46 +11002848 for (n = 0; n < num_socks; n++) {
2849 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11002850 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10002851 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2852 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002853 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11002854 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002855 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11002856 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002857 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002858
Kevin Steves366298c2001-12-19 17:58:01 +00002859 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002860 *display_numberp = display_number;
2861 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002862}
2863
Ben Lindstrombba81212001-06-25 05:01:22 +00002864static int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002865connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002866{
Damien Miller95def091999-11-25 00:26:21 +11002867 int sock;
2868 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002869
Damien Miller3afe3752001-12-21 12:39:51 +11002870 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2871 if (sock < 0)
2872 error("socket: %.100s", strerror(errno));
2873 memset(&addr, 0, sizeof(addr));
2874 addr.sun_family = AF_UNIX;
2875 snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
2876 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2877 return sock;
2878 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11002879 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2880 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002881}
2882
Damien Millerbd483e72000-04-30 10:00:53 +10002883int
2884x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002885{
Damien Miller398e1cf2002-02-05 11:52:13 +11002886 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002887 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002888 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002889 struct addrinfo hints, *ai, *aitop;
2890 char strport[NI_MAXSERV];
2891 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002892
Damien Miller95def091999-11-25 00:26:21 +11002893 /* Try to open a socket for the local X server. */
2894 display = getenv("DISPLAY");
2895 if (!display) {
2896 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002897 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002898 }
Damien Miller5428f641999-11-25 11:54:57 +11002899 /*
2900 * Now we decode the value of the DISPLAY variable and make a
2901 * connection to the real X server.
2902 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002903
Damien Miller5428f641999-11-25 11:54:57 +11002904 /*
2905 * Check if it is a unix domain socket. Unix domain displays are in
2906 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2907 */
Damien Miller95def091999-11-25 00:26:21 +11002908 if (strncmp(display, "unix:", 5) == 0 ||
2909 display[0] == ':') {
2910 /* Connect to the unix domain socket. */
2911 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2912 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11002913 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002914 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002915 }
2916 /* Create a socket. */
2917 sock = connect_local_xsocket(display_number);
2918 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002919 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002920
2921 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002922 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002923 }
Damien Miller5428f641999-11-25 11:54:57 +11002924 /*
2925 * Connect to an inet socket. The DISPLAY value is supposedly
2926 * hostname:d[.s], where hostname may also be numeric IP address.
2927 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00002928 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11002929 cp = strchr(buf, ':');
2930 if (!cp) {
2931 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002932 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002933 }
Damien Miller95def091999-11-25 00:26:21 +11002934 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002935 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002936 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2937 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11002938 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002939 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002940 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002941
Damien Miller34132e52000-01-14 15:45:46 +11002942 /* Look up the host address */
2943 memset(&hints, 0, sizeof(hints));
2944 hints.ai_family = IPv4or6;
2945 hints.ai_socktype = SOCK_STREAM;
2946 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2947 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2948 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002949 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002950 }
Damien Miller34132e52000-01-14 15:45:46 +11002951 for (ai = aitop; ai; ai = ai->ai_next) {
2952 /* Create a socket. */
Damien Miller2372ace2003-05-14 13:42:23 +10002953 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002954 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002955 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002956 continue;
2957 }
2958 /* Connect it to the display. */
2959 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002960 debug2("connect %.100s port %d: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10002961 6000 + display_number, strerror(errno));
2962 close(sock);
2963 continue;
2964 }
2965 /* Success */
2966 break;
Damien Miller34132e52000-01-14 15:45:46 +11002967 }
Damien Miller34132e52000-01-14 15:45:46 +11002968 freeaddrinfo(aitop);
2969 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002970 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002971 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002972 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002973 }
Damien Miller398e1cf2002-02-05 11:52:13 +11002974 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10002975 return sock;
2976}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002977
Damien Millerbd483e72000-04-30 10:00:53 +10002978/*
2979 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2980 * the remote channel number. We should do whatever we want, and respond
2981 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2982 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002983
Damien Millerbd483e72000-04-30 10:00:53 +10002984void
Damien Miller630d6f42002-01-22 23:17:30 +11002985x11_input_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002986{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002987 Channel *c = NULL;
2988 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002989 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10002990
2991 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002992
2993 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002994
2995 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002996 remote_host = packet_get_string(NULL);
2997 } else {
2998 remote_host = xstrdup("unknown (remote did not supply name)");
2999 }
Damien Miller48b03fc2002-01-22 23:11:40 +11003000 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10003001
3002 /* Obtain a connection to the real X display. */
3003 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003004 if (sock != -1) {
3005 /* Allocate a channel for this connection. */
3006 c = channel_new("connected x11 socket",
3007 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
3008 remote_host, 1);
Damien Miller699d0032002-02-08 22:07:16 +11003009 c->remote_id = remote_id;
3010 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003011 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003012 xfree(remote_host);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003013 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10003014 /* Send refusal to the remote host. */
3015 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003016 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10003017 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10003018 /* Send a confirmation to the remote host. */
3019 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003020 packet_put_int(remote_id);
3021 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10003022 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003023 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003024}
3025
Damien Miller69b69aa2000-10-28 14:19:58 +11003026/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
3027void
Damien Miller630d6f42002-01-22 23:17:30 +11003028deny_input_open(int type, u_int32_t seq, void *ctxt)
Damien Miller69b69aa2000-10-28 14:19:58 +11003029{
3030 int rchan = packet_get_int();
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00003031
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00003032 switch (type) {
Damien Miller69b69aa2000-10-28 14:19:58 +11003033 case SSH_SMSG_AGENT_OPEN:
3034 error("Warning: ssh server tried agent forwarding.");
3035 break;
3036 case SSH_SMSG_X11_OPEN:
3037 error("Warning: ssh server tried X11 forwarding.");
3038 break;
3039 default:
Damien Miller630d6f42002-01-22 23:17:30 +11003040 error("deny_input_open: type %d", type);
Damien Miller69b69aa2000-10-28 14:19:58 +11003041 break;
3042 }
Damien Miller5eb137c2005-12-31 16:19:53 +11003043 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller69b69aa2000-10-28 14:19:58 +11003044 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
3045 packet_put_int(rchan);
3046 packet_send();
3047}
3048
Damien Miller5428f641999-11-25 11:54:57 +11003049/*
3050 * Requests forwarding of X11 connections, generates fake authentication
3051 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00003052 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11003053 */
Damien Miller4af51302000-04-16 11:18:38 +10003054void
Damien Miller17e7ed02005-06-17 12:54:33 +10003055x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Millerbd483e72000-04-30 10:00:53 +10003056 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003057{
Ben Lindstrom46c16222000-12-22 01:43:59 +00003058 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10003059 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11003060 char *new_data;
3061 int screen_number;
3062 const char *cp;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10003063 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003064
Damien Millerf92c0792005-07-06 09:45:26 +10003065 if (x11_saved_display == NULL)
3066 x11_saved_display = xstrdup(disp);
3067 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10003068 error("x11_request_forwarding_with_spoofing: different "
3069 "$DISPLAY already forwarded");
3070 return;
3071 }
3072
Damien Miller17e7ed02005-06-17 12:54:33 +10003073 cp = disp;
3074 if (disp)
3075 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11003076 if (cp)
3077 cp = strchr(cp, '.');
3078 if (cp)
3079 screen_number = atoi(cp + 1);
3080 else
3081 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003082
Damien Miller13390022005-07-06 09:44:19 +10003083 if (x11_saved_proto == NULL) {
3084 /* Save protocol name. */
3085 x11_saved_proto = xstrdup(proto);
3086 /*
Damien Miller46d38de2005-07-17 17:02:09 +10003087 * Extract real authentication data and generate fake data
Damien Miller13390022005-07-06 09:44:19 +10003088 * of the same length.
3089 */
3090 x11_saved_data = xmalloc(data_len);
3091 x11_fake_data = xmalloc(data_len);
3092 for (i = 0; i < data_len; i++) {
3093 if (sscanf(data + 2 * i, "%2x", &value) != 1)
3094 fatal("x11_request_forwarding: bad "
3095 "authentication data: %.100s", data);
3096 if (i % 4 == 0)
3097 rnd = arc4random();
3098 x11_saved_data[i] = value;
3099 x11_fake_data[i] = rnd & 0xff;
3100 rnd >>= 8;
3101 }
3102 x11_saved_data_len = data_len;
3103 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11003104 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003105
Damien Miller95def091999-11-25 00:26:21 +11003106 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10003107 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003108
Damien Miller95def091999-11-25 00:26:21 +11003109 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10003110 if (compat20) {
3111 channel_request_start(client_session_id, "x11-req", 0);
3112 packet_put_char(0); /* XXX bool single connection */
3113 } else {
3114 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
3115 }
3116 packet_put_cstring(proto);
3117 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11003118 packet_put_int(screen_number);
3119 packet_send();
3120 packet_write_wait();
3121 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003122}
3123
Ben Lindstrome9c99912001-06-09 00:41:05 +00003124
3125/* -- agent forwarding */
3126
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003127/* Sends a message to the server to request authentication fd forwarding. */
3128
Damien Miller4af51302000-04-16 11:18:38 +10003129void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003130auth_request_forwarding(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003131{
Damien Miller95def091999-11-25 00:26:21 +11003132 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
3133 packet_send();
3134 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003135}