blob: 3d75c8f2b57b7ded89b690f932cc80e3b583ae28 [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 Miller5efcecc2003-09-17 07:31:14 +100042RCSID("$OpenBSD: channels.c,v 1.195 2003/09/16 21:02:40 markus 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 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071static int channels_alloc = 0;
72
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
112/* Saved X11 authentication protocol name. */
113static char *x11_saved_proto = NULL;
114
115/* Saved X11 authentication data. This is the real data. */
116static char *x11_saved_data = NULL;
117static u_int x11_saved_data_len = 0;
118
119/*
120 * Fake X11 authentication data. This is what the server will be sending us;
121 * we should replace any occurrences of this by the real data.
122 */
123static char *x11_fake_data = NULL;
124static u_int x11_fake_data_len;
125
126
127/* -- agent forwarding */
128
129#define NUM_SOCKS 10
130
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000131/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000132static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000133
Ben Lindstrome9c99912001-06-09 00:41:05 +0000134/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000135static void port_open_helper(Channel *c, char *rtype);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000136
Ben Lindstrome9c99912001-06-09 00:41:05 +0000137/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000138
139Channel *
140channel_lookup(int id)
141{
142 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000143
Ben Lindstrom79548872002-03-05 01:57:44 +0000144 if (id < 0 || id >= channels_alloc) {
Damien Miller996acd22003-04-09 20:59:48 +1000145 logit("channel_lookup: %d: bad id", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000146 return NULL;
147 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000148 c = channels[id];
149 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000150 logit("channel_lookup: %d: bad id: channel free", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000151 return NULL;
152 }
153 return c;
154}
155
Damien Miller5428f641999-11-25 11:54:57 +1100156/*
Damien Millere247cc42000-05-07 12:03:14 +1000157 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000158 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100159 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Ben Lindstrombba81212001-06-25 05:01:22 +0000161static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100162channel_register_fds(Channel *c, int rfd, int wfd, int efd,
163 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164{
Damien Miller95def091999-11-25 00:26:21 +1100165 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100166 channel_max_fd = MAX(channel_max_fd, rfd);
167 channel_max_fd = MAX(channel_max_fd, wfd);
168 channel_max_fd = MAX(channel_max_fd, efd);
169
Damien Miller5428f641999-11-25 11:54:57 +1100170 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000171
Damien Miller6f83b8e2000-05-02 09:23:45 +1000172 c->rfd = rfd;
173 c->wfd = wfd;
174 c->sock = (rfd == wfd) ? rfd : -1;
175 c->efd = efd;
176 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100177
Damien Miller79438cc2001-02-16 12:34:57 +1100178 /* XXX ugly hack: nonblock is only set by the server */
179 if (nonblock && isatty(c->rfd)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000180 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100181 c->isatty = 1;
182 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000183 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100184 c->self, c->wfd);
185 }
186 } else {
187 c->isatty = 0;
188 }
Ben Lindstrombeb5f332002-07-22 15:28:53 +0000189 c->wfd_isatty = isatty(c->wfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100190
Damien Miller69b69aa2000-10-28 14:19:58 +1100191 /* enable nonblocking mode */
192 if (nonblock) {
193 if (rfd != -1)
194 set_nonblock(rfd);
195 if (wfd != -1)
196 set_nonblock(wfd);
197 if (efd != -1)
198 set_nonblock(efd);
199 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000200}
201
202/*
203 * Allocate a new channel object and set its type and socket. This will cause
204 * remote_name to be freed.
205 */
206
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000207Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000208channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000209 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000210{
211 int i, found;
212 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213
Damien Miller95def091999-11-25 00:26:21 +1100214 /* Do initial allocation if this is the first call. */
215 if (channels_alloc == 0) {
216 channels_alloc = 10;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000217 channels = xmalloc(channels_alloc * sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100218 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000219 channels[i] = NULL;
Ben Lindstrom601e4362001-06-21 03:19:23 +0000220 fatal_add_cleanup((void (*) (void *)) channel_free_all, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100221 }
222 /* Try to find a free slot where to put the new channel. */
223 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000224 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100225 /* Found a free slot. */
226 found = i;
227 break;
228 }
229 if (found == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100230 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100231 found = channels_alloc;
Damien Miller9403aa22002-06-26 19:14:43 +1000232 if (channels_alloc > 10000)
233 fatal("channel_new: internal error: channels_alloc %d "
234 "too big.", channels_alloc);
Damien Miller5efcecc2003-09-17 07:31:14 +1000235 channels = xrealloc(channels,
236 (channels_alloc + 10) * sizeof(Channel *));
237 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100238 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100239 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000240 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100241 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000242 /* Initialize and return new channel. */
243 c = channels[found] = xmalloc(sizeof(Channel));
Damien Miller4623a752001-10-10 15:03:58 +1000244 memset(c, 0, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100245 buffer_init(&c->input);
246 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000247 buffer_init(&c->extended);
Damien Miller5144df92002-01-22 23:28:45 +1100248 c->ostate = CHAN_OUTPUT_OPEN;
249 c->istate = CHAN_INPUT_OPEN;
250 c->flags = 0;
Damien Miller69b69aa2000-10-28 14:19:58 +1100251 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100252 c->self = found;
253 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000254 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000255 c->local_window = window;
256 c->local_window_max = window;
257 c->local_consumed = 0;
258 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100259 c->remote_id = -1;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000260 c->remote_name = xstrdup(remote_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000261 c->remote_window = 0;
262 c->remote_maxpacket = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000263 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100264 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000265 c->detach_user = NULL;
Damien Miller67f0bc02002-02-05 12:23:08 +1100266 c->confirm = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000267 c->input_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100268 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000269 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000271
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000272static int
273channel_find_maxfd(void)
274{
275 int i, max = 0;
276 Channel *c;
277
278 for (i = 0; i < channels_alloc; i++) {
279 c = channels[i];
280 if (c != NULL) {
281 max = MAX(max, c->rfd);
282 max = MAX(max, c->wfd);
283 max = MAX(max, c->efd);
284 }
285 }
286 return max;
287}
288
289int
290channel_close_fd(int *fdp)
291{
292 int ret = 0, fd = *fdp;
293
294 if (fd != -1) {
295 ret = close(fd);
296 *fdp = -1;
297 if (fd == channel_max_fd)
298 channel_max_fd = channel_find_maxfd();
299 }
300 return ret;
301}
302
Damien Miller6f83b8e2000-05-02 09:23:45 +1000303/* Close all channel fd/socket. */
304
Ben Lindstrombba81212001-06-25 05:01:22 +0000305static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000306channel_close_fds(Channel *c)
307{
Damien Millerfbdeece2003-09-02 22:52:31 +1000308 debug3("channel %d: close_fds r %d w %d e %d",
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000309 c->self, c->rfd, c->wfd, c->efd);
310
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000311 channel_close_fd(&c->sock);
312 channel_close_fd(&c->rfd);
313 channel_close_fd(&c->wfd);
314 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000315}
316
317/* Free the channel and close its fd/socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318
Damien Miller4af51302000-04-16 11:18:38 +1000319void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000320channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000322 char *s;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000323 int i, n;
324
325 for (n = 0, i = 0; i < channels_alloc; i++)
326 if (channels[i])
327 n++;
Damien Millerfbdeece2003-09-02 22:52:31 +1000328 debug("channel %d: free: %s, nchannels %d", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000329 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000330
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000331 s = channel_open_message();
Damien Millerfbdeece2003-09-02 22:52:31 +1000332 debug3("channel %d: status: %s", c->self, s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000333 xfree(s);
334
Damien Miller6f83b8e2000-05-02 09:23:45 +1000335 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000336 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000337 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000338 buffer_free(&c->input);
339 buffer_free(&c->output);
340 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000341 if (c->remote_name) {
342 xfree(c->remote_name);
343 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100344 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000345 channels[c->self] = NULL;
346 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347}
348
Ben Lindstrome9c99912001-06-09 00:41:05 +0000349void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000350channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000351{
352 int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000353
Ben Lindstrom601e4362001-06-21 03:19:23 +0000354 for (i = 0; i < channels_alloc; i++)
355 if (channels[i] != NULL)
356 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000357}
358
359/*
360 * Closes the sockets/fds of all channels. This is used to close extra file
361 * descriptors after a fork.
362 */
363
364void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000365channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000366{
367 int i;
368
369 for (i = 0; i < channels_alloc; i++)
370 if (channels[i] != NULL)
371 channel_close_fds(channels[i]);
372}
373
374/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000375 * Stop listening to channels.
376 */
377
378void
379channel_stop_listening(void)
380{
381 int i;
382 Channel *c;
383
384 for (i = 0; i < channels_alloc; i++) {
385 c = channels[i];
386 if (c != NULL) {
387 switch (c->type) {
388 case SSH_CHANNEL_AUTH_SOCKET:
389 case SSH_CHANNEL_PORT_LISTENER:
390 case SSH_CHANNEL_RPORT_LISTENER:
391 case SSH_CHANNEL_X11_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000392 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000393 channel_free(c);
394 break;
395 }
396 }
397 }
398}
399
400/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000401 * Returns true if no channel has too much buffered data, and false if one or
402 * more channel is overfull.
403 */
404
405int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000406channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000407{
408 u_int i;
409 Channel *c;
410
411 for (i = 0; i < channels_alloc; i++) {
412 c = channels[i];
413 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000414#if 0
415 if (!compat20 &&
416 buffer_len(&c->input) > packet_get_maxsize()) {
Damien Miller275295e2003-01-08 14:04:09 +1100417 debug2("channel %d: big input buffer %d",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000418 c->self, buffer_len(&c->input));
419 return 0;
420 }
Damien Milleraf5f2e62001-10-10 15:01:16 +1000421#endif
Ben Lindstrome9c99912001-06-09 00:41:05 +0000422 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000423 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000424 c->self, buffer_len(&c->output),
425 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000426 return 0;
427 }
428 }
429 }
430 return 1;
431}
432
433/* Returns true if any channel is still open. */
434
435int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000436channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000437{
438 int i;
439 Channel *c;
440
441 for (i = 0; i < channels_alloc; i++) {
442 c = channels[i];
443 if (c == NULL)
444 continue;
445 switch (c->type) {
446 case SSH_CHANNEL_X11_LISTENER:
447 case SSH_CHANNEL_PORT_LISTENER:
448 case SSH_CHANNEL_RPORT_LISTENER:
449 case SSH_CHANNEL_CLOSED:
450 case SSH_CHANNEL_AUTH_SOCKET:
451 case SSH_CHANNEL_DYNAMIC:
452 case SSH_CHANNEL_CONNECTING:
453 case SSH_CHANNEL_ZOMBIE:
454 continue;
455 case SSH_CHANNEL_LARVAL:
456 if (!compat20)
457 fatal("cannot happen: SSH_CHANNEL_LARVAL");
458 continue;
459 case SSH_CHANNEL_OPENING:
460 case SSH_CHANNEL_OPEN:
461 case SSH_CHANNEL_X11_OPEN:
462 return 1;
463 case SSH_CHANNEL_INPUT_DRAINING:
464 case SSH_CHANNEL_OUTPUT_DRAINING:
465 if (!compat13)
466 fatal("cannot happen: OUT_DRAIN");
467 return 1;
468 default:
469 fatal("channel_still_open: bad channel type %d", c->type);
470 /* NOTREACHED */
471 }
472 }
473 return 0;
474}
475
476/* Returns the id of an open channel suitable for keepaliving */
477
478int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000479channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000480{
481 int i;
482 Channel *c;
483
484 for (i = 0; i < channels_alloc; i++) {
485 c = channels[i];
486 if (c == NULL)
487 continue;
488 switch (c->type) {
489 case SSH_CHANNEL_CLOSED:
490 case SSH_CHANNEL_DYNAMIC:
491 case SSH_CHANNEL_X11_LISTENER:
492 case SSH_CHANNEL_PORT_LISTENER:
493 case SSH_CHANNEL_RPORT_LISTENER:
494 case SSH_CHANNEL_OPENING:
495 case SSH_CHANNEL_CONNECTING:
496 case SSH_CHANNEL_ZOMBIE:
497 continue;
498 case SSH_CHANNEL_LARVAL:
499 case SSH_CHANNEL_AUTH_SOCKET:
500 case SSH_CHANNEL_OPEN:
501 case SSH_CHANNEL_X11_OPEN:
502 return i;
503 case SSH_CHANNEL_INPUT_DRAINING:
504 case SSH_CHANNEL_OUTPUT_DRAINING:
505 if (!compat13)
506 fatal("cannot happen: OUT_DRAIN");
507 return i;
508 default:
509 fatal("channel_find_open: bad channel type %d", c->type);
510 /* NOTREACHED */
511 }
512 }
513 return -1;
514}
515
516
517/*
518 * Returns a message describing the currently open forwarded connections,
519 * suitable for sending to the client. The message contains crlf pairs for
520 * newlines.
521 */
522
523char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000524channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000525{
526 Buffer buffer;
527 Channel *c;
528 char buf[1024], *cp;
529 int i;
530
531 buffer_init(&buffer);
532 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
533 buffer_append(&buffer, buf, strlen(buf));
534 for (i = 0; i < channels_alloc; i++) {
535 c = channels[i];
536 if (c == NULL)
537 continue;
538 switch (c->type) {
539 case SSH_CHANNEL_X11_LISTENER:
540 case SSH_CHANNEL_PORT_LISTENER:
541 case SSH_CHANNEL_RPORT_LISTENER:
542 case SSH_CHANNEL_CLOSED:
543 case SSH_CHANNEL_AUTH_SOCKET:
544 case SSH_CHANNEL_ZOMBIE:
545 continue;
546 case SSH_CHANNEL_LARVAL:
547 case SSH_CHANNEL_OPENING:
548 case SSH_CHANNEL_CONNECTING:
549 case SSH_CHANNEL_DYNAMIC:
550 case SSH_CHANNEL_OPEN:
551 case SSH_CHANNEL_X11_OPEN:
552 case SSH_CHANNEL_INPUT_DRAINING:
553 case SSH_CHANNEL_OUTPUT_DRAINING:
554 snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
555 c->self, c->remote_name,
556 c->type, c->remote_id,
557 c->istate, buffer_len(&c->input),
558 c->ostate, buffer_len(&c->output),
559 c->rfd, c->wfd);
560 buffer_append(&buffer, buf, strlen(buf));
561 continue;
562 default:
563 fatal("channel_open_message: bad channel type %d", c->type);
564 /* NOTREACHED */
565 }
566 }
567 buffer_append(&buffer, "\0", 1);
568 cp = xstrdup(buffer_ptr(&buffer));
569 buffer_free(&buffer);
570 return cp;
571}
572
573void
574channel_send_open(int id)
575{
576 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000577
Ben Lindstrome9c99912001-06-09 00:41:05 +0000578 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000579 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000580 return;
581 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000582 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000583 packet_start(SSH2_MSG_CHANNEL_OPEN);
584 packet_put_cstring(c->ctype);
585 packet_put_int(c->self);
586 packet_put_int(c->local_window);
587 packet_put_int(c->local_maxpacket);
588 packet_send();
589}
590
591void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000592channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000593{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000594 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000595
Ben Lindstrome9c99912001-06-09 00:41:05 +0000596 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000597 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000598 return;
599 }
Damien Millerfbdeece2003-09-02 22:52:31 +1000600 debug2("channel %d: request %s", id, service) ;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000601 packet_start(SSH2_MSG_CHANNEL_REQUEST);
602 packet_put_int(c->remote_id);
603 packet_put_cstring(service);
604 packet_put_char(wantconfirm);
605}
606void
Damien Miller67f0bc02002-02-05 12:23:08 +1100607channel_register_confirm(int id, channel_callback_fn *fn)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000608{
609 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000610
Ben Lindstrome9c99912001-06-09 00:41:05 +0000611 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000612 logit("channel_register_comfirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000613 return;
614 }
Damien Miller67f0bc02002-02-05 12:23:08 +1100615 c->confirm = fn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000616}
617void
618channel_register_cleanup(int id, channel_callback_fn *fn)
619{
620 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000621
Ben Lindstrome9c99912001-06-09 00:41:05 +0000622 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000623 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000624 return;
625 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000626 c->detach_user = fn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000627}
628void
629channel_cancel_cleanup(int id)
630{
631 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000632
Ben Lindstrome9c99912001-06-09 00:41:05 +0000633 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000634 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000635 return;
636 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000637 c->detach_user = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000638}
639void
640channel_register_filter(int id, channel_filter_fn *fn)
641{
642 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000643
Ben Lindstrome9c99912001-06-09 00:41:05 +0000644 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000645 logit("channel_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000646 return;
647 }
648 c->input_filter = fn;
649}
650
651void
652channel_set_fds(int id, int rfd, int wfd, int efd,
Damien Miller2aa0c192002-02-19 15:20:08 +1100653 int extusage, int nonblock, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000654{
655 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000656
Ben Lindstrome9c99912001-06-09 00:41:05 +0000657 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
658 fatal("channel_activate for non-larval channel %d.", id);
659 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
660 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100661 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000662 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
663 packet_put_int(c->remote_id);
664 packet_put_int(c->local_window);
665 packet_send();
666}
667
Damien Miller5428f641999-11-25 11:54:57 +1100668/*
Damien Millerb38eff82000-04-01 11:09:21 +1000669 * 'channel_pre*' are called just before select() to add any bits relevant to
670 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100671 */
Damien Millerb38eff82000-04-01 11:09:21 +1000672/*
673 * 'channel_post*': perform any appropriate operations for channels which
674 * have events pending.
675 */
676typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
677chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
678chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
679
Ben Lindstrombba81212001-06-25 05:01:22 +0000680static void
Damien Millerb38eff82000-04-01 11:09:21 +1000681channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
682{
683 FD_SET(c->sock, readset);
684}
685
Ben Lindstrombba81212001-06-25 05:01:22 +0000686static void
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000687channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
688{
689 debug3("channel %d: waiting for connection", c->self);
690 FD_SET(c->sock, writeset);
691}
692
Ben Lindstrombba81212001-06-25 05:01:22 +0000693static void
Damien Millerb38eff82000-04-01 11:09:21 +1000694channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
695{
696 if (buffer_len(&c->input) < packet_get_maxsize())
697 FD_SET(c->sock, readset);
698 if (buffer_len(&c->output) > 0)
699 FD_SET(c->sock, writeset);
700}
701
Ben Lindstrombba81212001-06-25 05:01:22 +0000702static void
Damien Millerde6987c2002-01-22 23:20:40 +1100703channel_pre_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000704{
Damien Millerde6987c2002-01-22 23:20:40 +1100705 u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
Damien Millerb38eff82000-04-01 11:09:21 +1000706
Damien Miller33b13562000-04-04 14:38:59 +1000707 if (c->istate == CHAN_INPUT_OPEN &&
Damien Millerde6987c2002-01-22 23:20:40 +1100708 limit > 0 &&
709 buffer_len(&c->input) < limit)
Damien Miller33b13562000-04-04 14:38:59 +1000710 FD_SET(c->rfd, readset);
711 if (c->ostate == CHAN_OUTPUT_OPEN ||
712 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
713 if (buffer_len(&c->output) > 0) {
714 FD_SET(c->wfd, writeset);
715 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000716 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000717 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
718 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000719 else
720 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000721 }
722 }
723 /** XXX check close conditions, too */
Damien Millerde6987c2002-01-22 23:20:40 +1100724 if (compat20 && c->efd != -1) {
Damien Miller33b13562000-04-04 14:38:59 +1000725 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
726 buffer_len(&c->extended) > 0)
727 FD_SET(c->efd, writeset);
Ben Lindstromcf159442002-03-26 03:26:24 +0000728 else if (!(c->flags & CHAN_EOF_SENT) &&
729 c->extended_usage == CHAN_EXTENDED_READ &&
Damien Miller33b13562000-04-04 14:38:59 +1000730 buffer_len(&c->extended) < c->remote_window)
731 FD_SET(c->efd, readset);
732 }
733}
734
Ben Lindstrombba81212001-06-25 05:01:22 +0000735static void
Damien Millerb38eff82000-04-01 11:09:21 +1000736channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
737{
738 if (buffer_len(&c->input) == 0) {
739 packet_start(SSH_MSG_CHANNEL_CLOSE);
740 packet_put_int(c->remote_id);
741 packet_send();
742 c->type = SSH_CHANNEL_CLOSED;
Damien Millerfbdeece2003-09-02 22:52:31 +1000743 debug2("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000744 }
745}
746
Ben Lindstrombba81212001-06-25 05:01:22 +0000747static void
Damien Millerb38eff82000-04-01 11:09:21 +1000748channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
749{
750 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000751 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000752 else
Damien Millerb38eff82000-04-01 11:09:21 +1000753 FD_SET(c->sock, writeset);
754}
755
756/*
757 * This is a special state for X11 authentication spoofing. An opened X11
758 * connection (when authentication spoofing is being done) remains in this
759 * state until the first packet has been completely read. The authentication
760 * data in that packet is then substituted by the real data if it matches the
761 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000762 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000763 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000764 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000765static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000766x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000767{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000768 u_char *ucp;
769 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000770
771 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000772 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000773 return 0;
774
775 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100776 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000777 if (ucp[0] == 0x42) { /* Byte order MSB first. */
778 proto_len = 256 * ucp[6] + ucp[7];
779 data_len = 256 * ucp[8] + ucp[9];
780 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
781 proto_len = ucp[6] + 256 * ucp[7];
782 data_len = ucp[8] + 256 * ucp[9];
783 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +1000784 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100785 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000786 return -1;
787 }
788
789 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000790 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000791 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
792 return 0;
793
794 /* Check if authentication protocol matches. */
795 if (proto_len != strlen(x11_saved_proto) ||
796 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000797 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +1000798 return -1;
799 }
800 /* Check if authentication data matches our fake data. */
801 if (data_len != x11_fake_data_len ||
802 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
803 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000804 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +1000805 return -1;
806 }
807 /* Check fake data length */
808 if (x11_fake_data_len != x11_saved_data_len) {
809 error("X11 fake_data_len %d != saved_data_len %d",
810 x11_fake_data_len, x11_saved_data_len);
811 return -1;
812 }
813 /*
814 * Received authentication protocol and data match
815 * our fake data. Substitute the fake data with real
816 * data.
817 */
818 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
819 x11_saved_data, x11_saved_data_len);
820 return 1;
821}
822
Ben Lindstrombba81212001-06-25 05:01:22 +0000823static void
Damien Millerb38eff82000-04-01 11:09:21 +1000824channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
825{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000826 int ret = x11_open_helper(&c->output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000827
Damien Millerb38eff82000-04-01 11:09:21 +1000828 if (ret == 1) {
829 /* Start normal processing for the channel. */
830 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000831 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000832 } else if (ret == -1) {
833 /*
834 * We have received an X11 connection that has bad
835 * authentication information.
836 */
Damien Miller996acd22003-04-09 20:59:48 +1000837 logit("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000838 buffer_clear(&c->input);
839 buffer_clear(&c->output);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000840 channel_close_fd(&c->sock);
Damien Millerb38eff82000-04-01 11:09:21 +1000841 c->sock = -1;
842 c->type = SSH_CHANNEL_CLOSED;
843 packet_start(SSH_MSG_CHANNEL_CLOSE);
844 packet_put_int(c->remote_id);
845 packet_send();
846 }
847}
848
Ben Lindstrombba81212001-06-25 05:01:22 +0000849static void
Damien Millerbd483e72000-04-30 10:00:53 +1000850channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000851{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000852 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000853
854 /* c->force_drain = 1; */
855
Damien Millerb38eff82000-04-01 11:09:21 +1000856 if (ret == 1) {
857 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +1100858 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000859 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +1000860 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +1000861 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +1100862 chan_read_failed(c);
863 buffer_clear(&c->input);
864 chan_ibuf_empty(c);
865 buffer_clear(&c->output);
866 /* for proto v1, the peer will send an IEOF */
867 if (compat20)
868 chan_write_failed(c);
869 else
870 c->type = SSH_CHANNEL_OPEN;
Damien Millerfbdeece2003-09-02 22:52:31 +1000871 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +1000872 }
873}
874
Ben Lindstromb3921512001-04-11 15:57:50 +0000875/* try to decode a socks4 header */
Ben Lindstrombba81212001-06-25 05:01:22 +0000876static int
Ben Lindstromb3921512001-04-11 15:57:50 +0000877channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000878{
Damien Millera10f5612002-09-12 09:49:15 +1000879 char *p, *host;
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000880 int len, have, i, found;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100881 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000882 struct {
883 u_int8_t version;
884 u_int8_t command;
885 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000886 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000887 } s4_req, s4_rsp;
888
Ben Lindstromb3921512001-04-11 15:57:50 +0000889 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000890
891 have = buffer_len(&c->input);
892 len = sizeof(s4_req);
893 if (have < len)
894 return 0;
895 p = buffer_ptr(&c->input);
896 for (found = 0, i = len; i < have; i++) {
897 if (p[i] == '\0') {
898 found = 1;
899 break;
900 }
901 if (i > 1024) {
902 /* the peer is probably sending garbage */
903 debug("channel %d: decode socks4: too long",
904 c->self);
905 return -1;
906 }
907 }
908 if (!found)
909 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000910 buffer_get(&c->input, (char *)&s4_req.version, 1);
911 buffer_get(&c->input, (char *)&s4_req.command, 1);
912 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000913 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000914 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000915 p = buffer_ptr(&c->input);
916 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000917 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000918 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000919 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000920 c->self, len, have);
921 strlcpy(username, p, sizeof(username));
922 buffer_consume(&c->input, len);
923 buffer_consume(&c->input, 1); /* trailing '\0' */
924
Ben Lindstromb3921512001-04-11 15:57:50 +0000925 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000926 strlcpy(c->path, host, sizeof(c->path));
927 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100928
Damien Millerfbdeece2003-09-02 22:52:31 +1000929 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000930 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000931
Ben Lindstromb3921512001-04-11 15:57:50 +0000932 if (s4_req.command != 1) {
933 debug("channel %d: cannot handle: socks4 cn %d",
934 c->self, s4_req.command);
935 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000936 }
Ben Lindstromb3921512001-04-11 15:57:50 +0000937 s4_rsp.version = 0; /* vn: 0 for reply */
938 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000939 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +0000940 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000941 buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +0000942 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000943}
944
Darren Tucker46471c92003-07-03 13:55:19 +1000945/* try to decode a socks5 header */
946#define SSH_SOCKS5_AUTHDONE 0x1000
947#define SSH_SOCKS5_NOAUTH 0x00
948#define SSH_SOCKS5_IPV4 0x01
949#define SSH_SOCKS5_DOMAIN 0x03
950#define SSH_SOCKS5_IPV6 0x04
951#define SSH_SOCKS5_CONNECT 0x01
952#define SSH_SOCKS5_SUCCESS 0x00
953
954static int
955channel_decode_socks5(Channel *c, fd_set * readset, fd_set * writeset)
956{
957 struct {
958 u_int8_t version;
959 u_int8_t command;
960 u_int8_t reserved;
961 u_int8_t atyp;
962 } s5_req, s5_rsp;
963 u_int16_t dest_port;
964 u_char *p, dest_addr[255+1];
965 int i, have, found, nmethods, addrlen, af;
966
967 debug2("channel %d: decode socks5", c->self);
968 p = buffer_ptr(&c->input);
969 if (p[0] != 0x05)
970 return -1;
971 have = buffer_len(&c->input);
972 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
973 /* format: ver | nmethods | methods */
974 if (have < 2)
975 return 0;
976 nmethods = p[1];
977 if (have < nmethods + 2)
978 return 0;
979 /* look for method: "NO AUTHENTICATION REQUIRED" */
980 for (found = 0, i = 2 ; i < nmethods + 2; i++) {
981 if (p[i] == SSH_SOCKS5_NOAUTH ) {
982 found = 1;
983 break;
984 }
985 }
986 if (!found) {
987 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
988 c->self);
989 return -1;
990 }
991 buffer_consume(&c->input, nmethods + 2);
992 buffer_put_char(&c->output, 0x05); /* version */
993 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
994 FD_SET(c->sock, writeset);
995 c->flags |= SSH_SOCKS5_AUTHDONE;
996 debug2("channel %d: socks5 auth done", c->self);
997 return 0; /* need more */
998 }
999 debug2("channel %d: socks5 post auth", c->self);
1000 if (have < sizeof(s5_req)+1)
1001 return 0; /* need more */
1002 memcpy((char *)&s5_req, p, sizeof(s5_req));
1003 if (s5_req.version != 0x05 ||
1004 s5_req.command != SSH_SOCKS5_CONNECT ||
1005 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001006 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001007 return -1;
1008 }
1009 switch(s5_req.atyp){
1010 case SSH_SOCKS5_IPV4:
1011 addrlen = 4;
1012 af = AF_INET;
1013 break;
1014 case SSH_SOCKS5_DOMAIN:
1015 addrlen = p[sizeof(s5_req)];
1016 af = -1;
1017 break;
1018 case SSH_SOCKS5_IPV6:
1019 addrlen = 16;
1020 af = AF_INET6;
1021 break;
1022 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001023 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001024 return -1;
1025 }
1026 if (have < 4 + addrlen + 2)
1027 return 0;
1028 buffer_consume(&c->input, sizeof(s5_req));
1029 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1030 buffer_consume(&c->input, 1); /* host string length */
1031 buffer_get(&c->input, (char *)&dest_addr, addrlen);
1032 buffer_get(&c->input, (char *)&dest_port, 2);
1033 dest_addr[addrlen] = '\0';
1034 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1035 strlcpy(c->path, dest_addr, sizeof(c->path));
1036 else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL)
1037 return -1;
1038 c->host_port = ntohs(dest_port);
1039
Damien Millerfbdeece2003-09-02 22:52:31 +10001040 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001041 c->self, c->path, c->host_port, s5_req.command);
1042
1043 s5_rsp.version = 0x05;
1044 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1045 s5_rsp.reserved = 0; /* ignored */
1046 s5_rsp.atyp = SSH_SOCKS5_IPV4;
1047 ((struct in_addr *)&dest_addr)->s_addr = INADDR_ANY;
1048 dest_port = 0; /* ignored */
1049
1050 buffer_append(&c->output, (char *)&s5_rsp, sizeof(s5_rsp));
1051 buffer_append(&c->output, (char *)&dest_addr, sizeof(struct in_addr));
1052 buffer_append(&c->output, (char *)&dest_port, sizeof(dest_port));
1053 return 1;
1054}
1055
Ben Lindstromb3921512001-04-11 15:57:50 +00001056/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001057static void
Ben Lindstromb3921512001-04-11 15:57:50 +00001058channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
1059{
1060 u_char *p;
1061 int have, ret;
1062
1063 have = buffer_len(&c->input);
Damien Miller4623a752001-10-10 15:03:58 +10001064 c->delayed = 0;
Ben Lindstromb3921512001-04-11 15:57:50 +00001065 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001066 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001067 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001068 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001069 /* need more */
1070 FD_SET(c->sock, readset);
1071 return;
1072 }
1073 /* try to guess the protocol */
1074 p = buffer_ptr(&c->input);
1075 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001076 case 0x04:
1077 ret = channel_decode_socks4(c, readset, writeset);
1078 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001079 case 0x05:
1080 ret = channel_decode_socks5(c, readset, writeset);
1081 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001082 default:
1083 ret = -1;
1084 break;
1085 }
1086 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001087 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001088 } else if (ret == 0) {
1089 debug2("channel %d: pre_dynamic: need more", c->self);
1090 /* need more */
1091 FD_SET(c->sock, readset);
1092 } else {
1093 /* switch to the next state */
1094 c->type = SSH_CHANNEL_OPENING;
1095 port_open_helper(c, "direct-tcpip");
1096 }
1097}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001098
Damien Millerb38eff82000-04-01 11:09:21 +10001099/* This is our fake X11 server socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +00001100static void
Damien Millerb38eff82000-04-01 11:09:21 +10001101channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
1102{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001103 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001104 struct sockaddr addr;
Damien Miller398e1cf2002-02-05 11:52:13 +11001105 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001106 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001107 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001108 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001109
1110 if (FD_ISSET(c->sock, readset)) {
1111 debug("X11 connection requested.");
1112 addrlen = sizeof(addr);
1113 newsock = accept(c->sock, &addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001114 if (c->single_connection) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001115 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001116 channel_close_fd(&c->sock);
1117 chan_mark_dead(c);
1118 }
Damien Millerb38eff82000-04-01 11:09:21 +10001119 if (newsock < 0) {
1120 error("accept: %.100s", strerror(errno));
1121 return;
1122 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001123 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001124 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001125 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001126 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001127 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001128
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001129 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001130 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001131 c->local_window_max, c->local_maxpacket, 0, buf, 1);
Damien Millerbd483e72000-04-30 10:00:53 +10001132 if (compat20) {
1133 packet_start(SSH2_MSG_CHANNEL_OPEN);
1134 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001135 packet_put_int(nc->self);
Damien Millere7378562001-12-21 14:58:35 +11001136 packet_put_int(nc->local_window_max);
1137 packet_put_int(nc->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +11001138 /* originator ipaddr and port */
1139 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +10001140 if (datafellows & SSH_BUG_X11FWD) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001141 debug2("ssh2 x11 bug compat mode");
Damien Miller30c3d422000-05-09 11:02:59 +10001142 } else {
1143 packet_put_int(remote_port);
1144 }
Damien Millerbd483e72000-04-30 10:00:53 +10001145 packet_send();
1146 } else {
1147 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001148 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001149 if (packet_get_protocol_flags() &
1150 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +00001151 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +10001152 packet_send();
1153 }
Damien Millerd83ff352001-01-30 09:19:34 +11001154 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001155 }
1156}
1157
Ben Lindstrombba81212001-06-25 05:01:22 +00001158static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001159port_open_helper(Channel *c, char *rtype)
1160{
1161 int direct;
1162 char buf[1024];
1163 char *remote_ipaddr = get_peer_ipaddr(c->sock);
1164 u_short remote_port = get_peer_port(c->sock);
1165
1166 direct = (strcmp(rtype, "direct-tcpip") == 0);
1167
1168 snprintf(buf, sizeof buf,
1169 "%s: listening port %d for %.100s port %d, "
1170 "connect from %.200s port %d",
1171 rtype, c->listening_port, c->path, c->host_port,
1172 remote_ipaddr, remote_port);
1173
1174 xfree(c->remote_name);
1175 c->remote_name = xstrdup(buf);
1176
1177 if (compat20) {
1178 packet_start(SSH2_MSG_CHANNEL_OPEN);
1179 packet_put_cstring(rtype);
1180 packet_put_int(c->self);
1181 packet_put_int(c->local_window_max);
1182 packet_put_int(c->local_maxpacket);
1183 if (direct) {
1184 /* target host, port */
1185 packet_put_cstring(c->path);
1186 packet_put_int(c->host_port);
1187 } else {
1188 /* listen address, port */
1189 packet_put_cstring(c->path);
1190 packet_put_int(c->listening_port);
1191 }
1192 /* originator host and port */
1193 packet_put_cstring(remote_ipaddr);
1194 packet_put_int(remote_port);
1195 packet_send();
1196 } else {
1197 packet_start(SSH_MSG_PORT_OPEN);
1198 packet_put_int(c->self);
1199 packet_put_cstring(c->path);
1200 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001201 if (packet_get_protocol_flags() &
1202 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001203 packet_put_cstring(c->remote_name);
1204 packet_send();
1205 }
1206 xfree(remote_ipaddr);
1207}
1208
Damien Millerb38eff82000-04-01 11:09:21 +10001209/*
1210 * This socket is listening for connections to a forwarded TCP/IP port.
1211 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001212static void
Damien Millerb38eff82000-04-01 11:09:21 +10001213channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
1214{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001215 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001216 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001217 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001218 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001219 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001220
Damien Millerb38eff82000-04-01 11:09:21 +10001221 if (FD_ISSET(c->sock, readset)) {
1222 debug("Connection to port %d forwarding "
1223 "to %.100s port %d requested.",
1224 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001225
Damien Miller4623a752001-10-10 15:03:58 +10001226 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1227 nextstate = SSH_CHANNEL_OPENING;
1228 rtype = "forwarded-tcpip";
1229 } else {
1230 if (c->host_port == 0) {
1231 nextstate = SSH_CHANNEL_DYNAMIC;
Damien Millerd3c04b92001-10-10 15:04:20 +10001232 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001233 } else {
1234 nextstate = SSH_CHANNEL_OPENING;
1235 rtype = "direct-tcpip";
1236 }
1237 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001238
Damien Millerb38eff82000-04-01 11:09:21 +10001239 addrlen = sizeof(addr);
1240 newsock = accept(c->sock, &addr, &addrlen);
1241 if (newsock < 0) {
1242 error("accept: %.100s", strerror(errno));
1243 return;
1244 }
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00001245 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001246 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1247 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001248 nc->listening_port = c->listening_port;
1249 nc->host_port = c->host_port;
1250 strlcpy(nc->path, c->path, sizeof(nc->path));
1251
Damien Miller4623a752001-10-10 15:03:58 +10001252 if (nextstate == SSH_CHANNEL_DYNAMIC) {
1253 /*
1254 * do not call the channel_post handler until
1255 * this flag has been reset by a pre-handler.
1256 * otherwise the FD_ISSET calls might overflow
1257 */
1258 nc->delayed = 1;
1259 } else {
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001260 port_open_helper(nc, rtype);
Damien Miller4623a752001-10-10 15:03:58 +10001261 }
Damien Millerb38eff82000-04-01 11:09:21 +10001262 }
1263}
1264
1265/*
1266 * This is the authentication agent socket listening for connections from
1267 * clients.
1268 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001269static void
Damien Millerb38eff82000-04-01 11:09:21 +10001270channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
1271{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001272 Channel *nc;
1273 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001274 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001275 socklen_t addrlen;
1276
1277 if (FD_ISSET(c->sock, readset)) {
1278 addrlen = sizeof(addr);
1279 newsock = accept(c->sock, &addr, &addrlen);
1280 if (newsock < 0) {
1281 error("accept from auth socket: %.100s", strerror(errno));
1282 return;
1283 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001284 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001285 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1286 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001287 0, "accepted auth socket", 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001288 if (compat20) {
1289 packet_start(SSH2_MSG_CHANNEL_OPEN);
1290 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001291 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001292 packet_put_int(c->local_window_max);
1293 packet_put_int(c->local_maxpacket);
1294 } else {
1295 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001296 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001297 }
Damien Millerb38eff82000-04-01 11:09:21 +10001298 packet_send();
1299 }
1300}
1301
Ben Lindstrombba81212001-06-25 05:01:22 +00001302static void
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001303channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
1304{
Ben Lindstrom69128662001-05-08 20:07:39 +00001305 int err = 0;
Ben Lindstrom11180952001-07-04 05:13:35 +00001306 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001307
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001308 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001309 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001310 err = errno;
1311 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001312 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001313 if (err == 0) {
1314 debug("channel %d: connected", c->self);
1315 c->type = SSH_CHANNEL_OPEN;
1316 if (compat20) {
1317 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1318 packet_put_int(c->remote_id);
1319 packet_put_int(c->self);
1320 packet_put_int(c->local_window);
1321 packet_put_int(c->local_maxpacket);
1322 } else {
1323 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1324 packet_put_int(c->remote_id);
1325 packet_put_int(c->self);
1326 }
1327 } else {
1328 debug("channel %d: not connected: %s",
1329 c->self, strerror(err));
1330 if (compat20) {
1331 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1332 packet_put_int(c->remote_id);
1333 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1334 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1335 packet_put_cstring(strerror(err));
1336 packet_put_cstring("");
1337 }
1338 } else {
1339 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1340 packet_put_int(c->remote_id);
1341 }
1342 chan_mark_dead(c);
1343 }
1344 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001345 }
1346}
1347
Ben Lindstrombba81212001-06-25 05:01:22 +00001348static int
Damien Millerb38eff82000-04-01 11:09:21 +10001349channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
1350{
1351 char buf[16*1024];
1352 int len;
1353
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001354 if (c->rfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001355 FD_ISSET(c->rfd, readset)) {
1356 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +10001357 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1358 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001359 if (len <= 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001360 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001361 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001362 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001363 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001364 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001365 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001366 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001367 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001368 c->type = SSH_CHANNEL_INPUT_DRAINING;
Damien Millerfbdeece2003-09-02 22:52:31 +10001369 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001370 } else {
1371 chan_read_failed(c);
1372 }
1373 return -1;
1374 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001375 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001376 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001377 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001378 chan_read_failed(c);
1379 }
1380 } else {
1381 buffer_append(&c->input, buf, len);
1382 }
Damien Millerb38eff82000-04-01 11:09:21 +10001383 }
1384 return 1;
1385}
Ben Lindstrombba81212001-06-25 05:01:22 +00001386static int
Damien Millerb38eff82000-04-01 11:09:21 +10001387channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
1388{
Ben Lindstrome229b252001-03-05 06:28:06 +00001389 struct termios tio;
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001390 u_char *data;
1391 u_int dlen;
Damien Millerb38eff82000-04-01 11:09:21 +10001392 int len;
1393
1394 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001395 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001396 FD_ISSET(c->wfd, writeset) &&
1397 buffer_len(&c->output) > 0) {
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001398 data = buffer_ptr(&c->output);
1399 dlen = buffer_len(&c->output);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001400#ifdef _AIX
1401 /* XXX: Later AIX versions can't push as much data to tty */
Ben Lindstrombeb5f332002-07-22 15:28:53 +00001402 if (compat20 && c->wfd_isatty && dlen > 8*1024)
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001403 dlen = 8*1024;
1404#endif
Ben Lindstrombeb5f332002-07-22 15:28:53 +00001405 len = write(c->wfd, data, dlen);
Damien Miller6f83b8e2000-05-02 09:23:45 +10001406 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1407 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001408 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001409 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001410 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001411 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001412 return -1;
1413 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001414 buffer_clear(&c->output);
Damien Millerfbdeece2003-09-02 22:52:31 +10001415 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001416 c->type = SSH_CHANNEL_INPUT_DRAINING;
1417 } else {
1418 chan_write_failed(c);
1419 }
1420 return -1;
1421 }
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001422 if (compat20 && c->isatty && dlen >= 1 && data[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001423 if (tcgetattr(c->wfd, &tio) == 0 &&
1424 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1425 /*
1426 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001427 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001428 * size of a SSH2_MSG_CHANNEL_DATA message
1429 * (4 byte channel id + data)
Damien Miller79438cc2001-02-16 12:34:57 +11001430 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001431 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001432 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001433 }
1434 }
Damien Millerb38eff82000-04-01 11:09:21 +10001435 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001436 if (compat20 && len > 0) {
1437 c->local_consumed += len;
1438 }
1439 }
1440 return 1;
1441}
Ben Lindstrombba81212001-06-25 05:01:22 +00001442static int
Damien Miller33b13562000-04-04 14:38:59 +10001443channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
1444{
1445 char buf[16*1024];
1446 int len;
1447
Damien Miller1383bd82000-04-06 12:32:37 +10001448/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001449 if (c->efd != -1) {
1450 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1451 FD_ISSET(c->efd, writeset) &&
1452 buffer_len(&c->extended) > 0) {
1453 len = write(c->efd, buffer_ptr(&c->extended),
1454 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001455 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001456 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001457 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1458 return 1;
1459 if (len <= 0) {
1460 debug2("channel %d: closing write-efd %d",
1461 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001462 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001463 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001464 buffer_consume(&c->extended, len);
1465 c->local_consumed += len;
1466 }
1467 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1468 FD_ISSET(c->efd, readset)) {
1469 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001470 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001471 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001472 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1473 return 1;
1474 if (len <= 0) {
1475 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001476 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001477 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001478 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001479 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001480 }
Damien Miller33b13562000-04-04 14:38:59 +10001481 }
1482 }
1483 return 1;
1484}
Ben Lindstrombba81212001-06-25 05:01:22 +00001485static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001486channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001487{
Ben Lindstromb3921512001-04-11 15:57:50 +00001488 if (c->type == SSH_CHANNEL_OPEN &&
1489 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001490 c->local_window < c->local_window_max/2 &&
1491 c->local_consumed > 0) {
1492 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1493 packet_put_int(c->remote_id);
1494 packet_put_int(c->local_consumed);
1495 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001496 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001497 c->self, c->local_window,
1498 c->local_consumed);
1499 c->local_window += c->local_consumed;
1500 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001501 }
1502 return 1;
1503}
1504
Ben Lindstrombba81212001-06-25 05:01:22 +00001505static void
Damien Millerde6987c2002-01-22 23:20:40 +11001506channel_post_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001507{
Damien Miller4623a752001-10-10 15:03:58 +10001508 if (c->delayed)
1509 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001510 channel_handle_rfd(c, readset, writeset);
1511 channel_handle_wfd(c, readset, writeset);
Damien Millerde6987c2002-01-22 23:20:40 +11001512 if (!compat20)
Damien Miller4623a752001-10-10 15:03:58 +10001513 return;
Damien Miller33b13562000-04-04 14:38:59 +10001514 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001515 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001516}
1517
Ben Lindstrombba81212001-06-25 05:01:22 +00001518static void
Damien Millerb38eff82000-04-01 11:09:21 +10001519channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1520{
1521 int len;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001522
Damien Millerb38eff82000-04-01 11:09:21 +10001523 /* Send buffered output data to the socket. */
1524 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1525 len = write(c->sock, buffer_ptr(&c->output),
1526 buffer_len(&c->output));
1527 if (len <= 0)
Damien Miller76765c02002-01-22 23:21:15 +11001528 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001529 else
1530 buffer_consume(&c->output, len);
1531 }
1532}
1533
Ben Lindstrombba81212001-06-25 05:01:22 +00001534static void
Damien Miller33b13562000-04-04 14:38:59 +10001535channel_handler_init_20(void)
1536{
Damien Millerde6987c2002-01-22 23:20:40 +11001537 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001538 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001539 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001540 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001541 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001542 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001543 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001544 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001545
Damien Millerde6987c2002-01-22 23:20:40 +11001546 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001547 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001548 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001549 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001550 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001551 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001552 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001553}
1554
Ben Lindstrombba81212001-06-25 05:01:22 +00001555static void
Damien Millerb38eff82000-04-01 11:09:21 +10001556channel_handler_init_13(void)
1557{
1558 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1559 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1560 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1561 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1562 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1563 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1564 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001565 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001566 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001567
Damien Millerde6987c2002-01-22 23:20:40 +11001568 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001569 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1570 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1571 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1572 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001573 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001574 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001575}
1576
Ben Lindstrombba81212001-06-25 05:01:22 +00001577static void
Damien Millerb38eff82000-04-01 11:09:21 +10001578channel_handler_init_15(void)
1579{
Damien Millerde6987c2002-01-22 23:20:40 +11001580 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001581 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001582 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1583 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1584 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001585 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001586 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001587
1588 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1589 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1590 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Damien Millerde6987c2002-01-22 23:20:40 +11001591 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001592 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001593 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001594}
1595
Ben Lindstrombba81212001-06-25 05:01:22 +00001596static void
Damien Millerb38eff82000-04-01 11:09:21 +10001597channel_handler_init(void)
1598{
1599 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001600
Damien Miller9f0f5c62001-12-21 14:45:46 +11001601 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001602 channel_pre[i] = NULL;
1603 channel_post[i] = NULL;
1604 }
Damien Miller33b13562000-04-04 14:38:59 +10001605 if (compat20)
1606 channel_handler_init_20();
1607 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001608 channel_handler_init_13();
1609 else
1610 channel_handler_init_15();
1611}
1612
Damien Miller3ec27592001-10-12 11:35:04 +10001613/* gc dead channels */
1614static void
1615channel_garbage_collect(Channel *c)
1616{
1617 if (c == NULL)
1618 return;
1619 if (c->detach_user != NULL) {
1620 if (!chan_is_dead(c, 0))
1621 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001622 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001623 c->detach_user(c->self, NULL);
1624 /* if we still have a callback */
1625 if (c->detach_user != NULL)
1626 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001627 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001628 }
1629 if (!chan_is_dead(c, 1))
1630 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001631 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001632 channel_free(c);
1633}
1634
Ben Lindstrombba81212001-06-25 05:01:22 +00001635static void
Damien Millerb38eff82000-04-01 11:09:21 +10001636channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1637{
1638 static int did_init = 0;
1639 int i;
1640 Channel *c;
1641
1642 if (!did_init) {
1643 channel_handler_init();
1644 did_init = 1;
1645 }
1646 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001647 c = channels[i];
1648 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001649 continue;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001650 if (ftab[c->type] != NULL)
1651 (*ftab[c->type])(c, readset, writeset);
Damien Miller3ec27592001-10-12 11:35:04 +10001652 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001653 }
1654}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001655
Ben Lindstrome9c99912001-06-09 00:41:05 +00001656/*
1657 * Allocate/update select bitmasks and add any bits relevant to channels in
1658 * select bitmasks.
1659 */
Damien Miller4af51302000-04-16 11:18:38 +10001660void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001661channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001662 int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001663{
Damien Miller5e953212001-01-30 09:14:00 +11001664 int n;
1665 u_int sz;
1666
1667 n = MAX(*maxfdp, channel_max_fd);
1668
1669 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001670 /* perhaps check sz < nalloc/2 and shrink? */
1671 if (*readsetp == NULL || sz > *nallocp) {
1672 *readsetp = xrealloc(*readsetp, sz);
1673 *writesetp = xrealloc(*writesetp, sz);
1674 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11001675 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001676 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11001677 memset(*readsetp, 0, sz);
1678 memset(*writesetp, 0, sz);
1679
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001680 if (!rekeying)
1681 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001682}
1683
Ben Lindstrome9c99912001-06-09 00:41:05 +00001684/*
1685 * After select, perform any appropriate operations for channels which have
1686 * events pending.
1687 */
Damien Miller4af51302000-04-16 11:18:38 +10001688void
Damien Miller95def091999-11-25 00:26:21 +11001689channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001690{
Damien Millerb38eff82000-04-01 11:09:21 +10001691 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001692}
1693
Ben Lindstrome9c99912001-06-09 00:41:05 +00001694
Damien Miller5e953212001-01-30 09:14:00 +11001695/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001696
Damien Miller4af51302000-04-16 11:18:38 +10001697void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001698channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001699{
Damien Millerb38eff82000-04-01 11:09:21 +10001700 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001701 int i;
1702 u_int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001703
Damien Miller95def091999-11-25 00:26:21 +11001704 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001705 c = channels[i];
1706 if (c == NULL)
1707 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001708
Ben Lindstrome9c99912001-06-09 00:41:05 +00001709 /*
1710 * We are only interested in channels that can have buffered
1711 * incoming data.
1712 */
Damien Miller34132e52000-01-14 15:45:46 +11001713 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001714 if (c->type != SSH_CHANNEL_OPEN &&
1715 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001716 continue;
1717 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001718 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001719 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001720 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001721 if (compat20 &&
1722 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001723 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10001724 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001725 continue;
1726 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001727
Damien Miller95def091999-11-25 00:26:21 +11001728 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001729 if ((c->istate == CHAN_INPUT_OPEN ||
1730 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1731 (len = buffer_len(&c->input)) > 0) {
Ben Lindstrome9c99912001-06-09 00:41:05 +00001732 /*
1733 * Send some data for the other side over the secure
1734 * connection.
1735 */
Damien Miller33b13562000-04-04 14:38:59 +10001736 if (compat20) {
1737 if (len > c->remote_window)
1738 len = c->remote_window;
1739 if (len > c->remote_maxpacket)
1740 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001741 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001742 if (packet_is_interactive()) {
1743 if (len > 1024)
1744 len = 512;
1745 } else {
1746 /* Keep the packets at reasonable size. */
1747 if (len > packet_get_maxsize()/2)
1748 len = packet_get_maxsize()/2;
1749 }
Damien Miller95def091999-11-25 00:26:21 +11001750 }
Damien Millerb38eff82000-04-01 11:09:21 +10001751 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001752 packet_start(compat20 ?
1753 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001754 packet_put_int(c->remote_id);
1755 packet_put_string(buffer_ptr(&c->input), len);
1756 packet_send();
1757 buffer_consume(&c->input, len);
1758 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001759 }
1760 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001761 if (compat13)
1762 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001763 /*
1764 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00001765 * tell peer, that we will not send more data: send IEOF.
1766 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11001767 */
Ben Lindstromcf159442002-03-26 03:26:24 +00001768 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Ben Lindstrom5a6abda2002-06-09 19:41:48 +00001769 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
1770 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00001771 else
1772 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001773 }
Damien Miller33b13562000-04-04 14:38:59 +10001774 /* Send extended data, i.e. stderr */
1775 if (compat20 &&
Ben Lindstromcf159442002-03-26 03:26:24 +00001776 !(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10001777 c->remote_window > 0 &&
1778 (len = buffer_len(&c->extended)) > 0 &&
1779 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001780 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001781 c->self, c->remote_window, buffer_len(&c->extended),
1782 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001783 if (len > c->remote_window)
1784 len = c->remote_window;
1785 if (len > c->remote_maxpacket)
1786 len = c->remote_maxpacket;
1787 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1788 packet_put_int(c->remote_id);
1789 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1790 packet_put_string(buffer_ptr(&c->extended), len);
1791 packet_send();
1792 buffer_consume(&c->extended, len);
1793 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001794 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001795 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001796 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001797}
1798
Ben Lindstrome9c99912001-06-09 00:41:05 +00001799
1800/* -- protocol input */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001801
Damien Miller4af51302000-04-16 11:18:38 +10001802void
Damien Miller630d6f42002-01-22 23:17:30 +11001803channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001804{
Damien Miller34132e52000-01-14 15:45:46 +11001805 int id;
Damien Miller95def091999-11-25 00:26:21 +11001806 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001807 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001808 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001809
Damien Miller95def091999-11-25 00:26:21 +11001810 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001811 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001812 c = channel_lookup(id);
1813 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001814 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001815
Damien Miller95def091999-11-25 00:26:21 +11001816 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001817 if (c->type != SSH_CHANNEL_OPEN &&
1818 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001819 return;
1820
1821 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001822 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001823 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001824
Damien Miller95def091999-11-25 00:26:21 +11001825 /* Get the data. */
1826 data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001827
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001828 if (compat20) {
Damien Miller33b13562000-04-04 14:38:59 +10001829 if (data_len > c->local_maxpacket) {
Damien Miller996acd22003-04-09 20:59:48 +10001830 logit("channel %d: rcvd big packet %d, maxpack %d",
Damien Miller33b13562000-04-04 14:38:59 +10001831 c->self, data_len, c->local_maxpacket);
1832 }
1833 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10001834 logit("channel %d: rcvd too much data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10001835 c->self, data_len, c->local_window);
1836 xfree(data);
1837 return;
1838 }
1839 c->local_window -= data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001840 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001841 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001842 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001843 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001844}
Ben Lindstrome9c99912001-06-09 00:41:05 +00001845
Damien Miller4af51302000-04-16 11:18:38 +10001846void
Damien Miller630d6f42002-01-22 23:17:30 +11001847channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001848{
1849 int id;
Damien Miller33b13562000-04-04 14:38:59 +10001850 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00001851 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10001852 Channel *c;
1853
1854 /* Get the channel number and verify it. */
1855 id = packet_get_int();
1856 c = channel_lookup(id);
1857
1858 if (c == NULL)
1859 packet_disconnect("Received extended_data for bad channel %d.", id);
1860 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10001861 logit("channel %d: ext data for non open", id);
Damien Miller33b13562000-04-04 14:38:59 +10001862 return;
1863 }
Ben Lindstromcf159442002-03-26 03:26:24 +00001864 if (c->flags & CHAN_EOF_RCVD) {
1865 if (datafellows & SSH_BUG_EXTEOF)
1866 debug("channel %d: accepting ext data after eof", id);
1867 else
1868 packet_disconnect("Received extended_data after EOF "
1869 "on channel %d.", id);
1870 }
Damien Miller33b13562000-04-04 14:38:59 +10001871 tcode = packet_get_int();
1872 if (c->efd == -1 ||
1873 c->extended_usage != CHAN_EXTENDED_WRITE ||
1874 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10001875 logit("channel %d: bad ext data", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001876 return;
1877 }
1878 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001879 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10001880 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10001881 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10001882 c->self, data_len, c->local_window);
1883 xfree(data);
1884 return;
1885 }
Damien Millerd3444942000-09-30 14:20:03 +11001886 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001887 c->local_window -= data_len;
1888 buffer_append(&c->extended, data, data_len);
1889 xfree(data);
1890}
1891
Damien Miller4af51302000-04-16 11:18:38 +10001892void
Damien Miller630d6f42002-01-22 23:17:30 +11001893channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001894{
1895 int id;
1896 Channel *c;
1897
Damien Millerb38eff82000-04-01 11:09:21 +10001898 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001899 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001900 c = channel_lookup(id);
1901 if (c == NULL)
1902 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1903 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001904
1905 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11001906 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001907 debug("channel %d: FORCE input drain", c->self);
1908 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11001909 if (buffer_len(&c->input) == 0)
1910 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001911 }
1912
Damien Millerb38eff82000-04-01 11:09:21 +10001913}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001914
Damien Miller4af51302000-04-16 11:18:38 +10001915void
Damien Miller630d6f42002-01-22 23:17:30 +11001916channel_input_close(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001917{
Damien Millerb38eff82000-04-01 11:09:21 +10001918 int id;
1919 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001920
Damien Millerb38eff82000-04-01 11:09:21 +10001921 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001922 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001923 c = channel_lookup(id);
1924 if (c == NULL)
1925 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001926
1927 /*
1928 * Send a confirmation that we have closed the channel and no more
1929 * data is coming for it.
1930 */
Damien Miller95def091999-11-25 00:26:21 +11001931 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001932 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001933 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001934
Damien Miller5428f641999-11-25 11:54:57 +11001935 /*
1936 * If the channel is in closed state, we have sent a close request,
1937 * and the other side will eventually respond with a confirmation.
1938 * Thus, we cannot free the channel here, because then there would be
1939 * no-one to receive the confirmation. The channel gets freed when
1940 * the confirmation arrives.
1941 */
Damien Millerb38eff82000-04-01 11:09:21 +10001942 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001943 /*
1944 * Not a closed channel - mark it as draining, which will
1945 * cause it to be freed later.
1946 */
Damien Miller76765c02002-01-22 23:21:15 +11001947 buffer_clear(&c->input);
Damien Millerb38eff82000-04-01 11:09:21 +10001948 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001949 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001950}
1951
Damien Millerb38eff82000-04-01 11:09:21 +10001952/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001953void
Damien Miller630d6f42002-01-22 23:17:30 +11001954channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001955{
Damien Millerb38eff82000-04-01 11:09:21 +10001956 int id = packet_get_int();
1957 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11001958
Damien Miller48b03fc2002-01-22 23:11:40 +11001959 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001960 if (c == NULL)
1961 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1962 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001963}
1964
Damien Miller4af51302000-04-16 11:18:38 +10001965void
Damien Miller630d6f42002-01-22 23:17:30 +11001966channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001967{
1968 int id = packet_get_int();
1969 Channel *c = channel_lookup(id);
1970
Damien Miller48b03fc2002-01-22 23:11:40 +11001971 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001972 if (c == NULL)
1973 packet_disconnect("Received close confirmation for "
1974 "out-of-range channel %d.", id);
1975 if (c->type != SSH_CHANNEL_CLOSED)
1976 packet_disconnect("Received close confirmation for "
1977 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001978 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001979}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001980
Damien Miller4af51302000-04-16 11:18:38 +10001981void
Damien Miller630d6f42002-01-22 23:17:30 +11001982channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001983{
Damien Millerb38eff82000-04-01 11:09:21 +10001984 int id, remote_id;
1985 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001986
Damien Millerb38eff82000-04-01 11:09:21 +10001987 id = packet_get_int();
1988 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001989
Damien Millerb38eff82000-04-01 11:09:21 +10001990 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1991 packet_disconnect("Received open confirmation for "
1992 "non-opening channel %d.", id);
1993 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001994 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001995 c->remote_id = remote_id;
1996 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001997
1998 if (compat20) {
1999 c->remote_window = packet_get_int();
2000 c->remote_maxpacket = packet_get_int();
Damien Miller67f0bc02002-02-05 12:23:08 +11002001 if (c->confirm) {
Damien Millerd3444942000-09-30 14:20:03 +11002002 debug2("callback start");
Damien Miller67f0bc02002-02-05 12:23:08 +11002003 c->confirm(c->self, NULL);
Damien Millerd3444942000-09-30 14:20:03 +11002004 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002005 }
Damien Millerfbdeece2003-09-02 22:52:31 +10002006 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
Damien Miller33b13562000-04-04 14:38:59 +10002007 c->remote_window, c->remote_maxpacket);
2008 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002009 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002010}
2011
Ben Lindstrombba81212001-06-25 05:01:22 +00002012static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002013reason2txt(int reason)
2014{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002015 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002016 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2017 return "administratively prohibited";
2018 case SSH2_OPEN_CONNECT_FAILED:
2019 return "connect failed";
2020 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2021 return "unknown channel type";
2022 case SSH2_OPEN_RESOURCE_SHORTAGE:
2023 return "resource shortage";
2024 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002025 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002026}
2027
Damien Miller4af51302000-04-16 11:18:38 +10002028void
Damien Miller630d6f42002-01-22 23:17:30 +11002029channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002030{
Kevin Steves12057502001-02-05 14:54:34 +00002031 int id, reason;
2032 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002033 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002034
Damien Millerb38eff82000-04-01 11:09:21 +10002035 id = packet_get_int();
2036 c = channel_lookup(id);
2037
2038 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2039 packet_disconnect("Received open failure for "
2040 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002041 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00002042 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00002043 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00002044 msg = packet_get_string(NULL);
2045 lang = packet_get_string(NULL);
2046 }
Damien Miller996acd22003-04-09 20:59:48 +10002047 logit("channel %d: open failed: %s%s%s", id,
Ben Lindstrom69128662001-05-08 20:07:39 +00002048 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00002049 if (msg != NULL)
2050 xfree(msg);
2051 if (lang != NULL)
2052 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10002053 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002054 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11002055 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002056 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002057}
2058
Damien Miller33b13562000-04-04 14:38:59 +10002059void
Damien Miller630d6f42002-01-22 23:17:30 +11002060channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002061{
2062 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002063 int id;
2064 u_int adjust;
Damien Miller33b13562000-04-04 14:38:59 +10002065
2066 if (!compat20)
2067 return;
2068
2069 /* Get the channel number and verify it. */
2070 id = packet_get_int();
2071 c = channel_lookup(id);
2072
2073 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002074 logit("Received window adjust for "
Damien Miller33b13562000-04-04 14:38:59 +10002075 "non-open channel %d.", id);
2076 return;
2077 }
2078 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002079 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002080 debug2("channel %d: rcvd adjust %u", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10002081 c->remote_window += adjust;
2082}
2083
Damien Miller4af51302000-04-16 11:18:38 +10002084void
Damien Miller630d6f42002-01-22 23:17:30 +11002085channel_input_port_open(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002086{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002087 Channel *c = NULL;
2088 u_short host_port;
2089 char *host, *originator_string;
2090 int remote_id, sock = -1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002091
Ben Lindstrome9c99912001-06-09 00:41:05 +00002092 remote_id = packet_get_int();
2093 host = packet_get_string(NULL);
2094 host_port = packet_get_int();
2095
2096 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2097 originator_string = packet_get_string(NULL);
2098 } else {
2099 originator_string = xstrdup("unknown (remote did not supply name)");
2100 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002101 packet_check_eom();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002102 sock = channel_connect_to(host, host_port);
2103 if (sock != -1) {
2104 c = channel_new("connected socket",
2105 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2106 originator_string, 1);
Damien Miller699d0032002-02-08 22:07:16 +11002107 c->remote_id = remote_id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002108 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002109 xfree(originator_string);
Ben Lindstrome9c99912001-06-09 00:41:05 +00002110 if (c == NULL) {
2111 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2112 packet_put_int(remote_id);
2113 packet_send();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002114 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002115 xfree(host);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002116}
2117
2118
Ben Lindstrome9c99912001-06-09 00:41:05 +00002119/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002120
Ben Lindstrom908afed2001-10-03 17:34:59 +00002121void
2122channel_set_af(int af)
2123{
2124 IPv4or6 = af;
2125}
2126
Damien Millerb16461c2002-01-22 23:29:22 +11002127static int
2128channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
2129 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002130{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002131 Channel *c;
Damien Millerb16461c2002-01-22 23:29:22 +11002132 int success, sock, on = 1;
Damien Miller34132e52000-01-14 15:45:46 +11002133 struct addrinfo hints, *ai, *aitop;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002134 const char *host;
Damien Millerb16461c2002-01-22 23:29:22 +11002135 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002136
Kevin Steves12057502001-02-05 14:54:34 +00002137 success = 0;
Damien Millerb16461c2002-01-22 23:29:22 +11002138 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2139 listen_addr : host_to_connect;
Kevin Steves12057502001-02-05 14:54:34 +00002140
Damien Millerb16461c2002-01-22 23:29:22 +11002141 if (host == NULL) {
2142 error("No forward host name.");
2143 return success;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002144 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002145 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00002146 error("Forward host name too long.");
2147 return success;
2148 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002149
Damien Miller5428f641999-11-25 11:54:57 +11002150 /*
Damien Miller34132e52000-01-14 15:45:46 +11002151 * getaddrinfo returns a loopback address if the hostname is
2152 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002153 */
Damien Miller34132e52000-01-14 15:45:46 +11002154 memset(&hints, 0, sizeof(hints));
2155 hints.ai_family = IPv4or6;
2156 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
2157 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002158 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002159 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
2160 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11002161
Damien Miller34132e52000-01-14 15:45:46 +11002162 for (ai = aitop; ai; ai = ai->ai_next) {
2163 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2164 continue;
2165 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2166 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Millerb16461c2002-01-22 23:29:22 +11002167 error("channel_setup_fwd_listener: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002168 continue;
2169 }
2170 /* Create a port to listen for the host. */
Damien Miller2372ace2003-05-14 13:42:23 +10002171 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002172 if (sock < 0) {
2173 /* this is no error since kernel may not support ipv6 */
2174 verbose("socket: %.100s", strerror(errno));
2175 continue;
2176 }
2177 /*
Damien Millere1383ce2002-09-19 11:49:37 +10002178 * Set socket options.
2179 * Allow local port reuse in TIME_WAIT.
Damien Miller34132e52000-01-14 15:45:46 +11002180 */
Damien Millere1383ce2002-09-19 11:49:37 +10002181 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on,
2182 sizeof(on)) == -1)
2183 error("setsockopt SO_REUSEADDR: %s", strerror(errno));
2184
Damien Miller34132e52000-01-14 15:45:46 +11002185 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002186
Damien Miller34132e52000-01-14 15:45:46 +11002187 /* Bind the socket to the address. */
2188 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2189 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002190 if (!ai->ai_next)
2191 error("bind: %.100s", strerror(errno));
2192 else
2193 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002194
Damien Miller34132e52000-01-14 15:45:46 +11002195 close(sock);
2196 continue;
2197 }
2198 /* Start listening for connections on the socket. */
2199 if (listen(sock, 5) < 0) {
2200 error("listen: %.100s", strerror(errno));
2201 close(sock);
2202 continue;
2203 }
2204 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002205 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002206 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002207 0, "port listener", 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002208 strlcpy(c->path, host, sizeof(c->path));
2209 c->host_port = port_to_connect;
2210 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002211 success = 1;
2212 }
2213 if (success == 0)
Damien Millerb16461c2002-01-22 23:29:22 +11002214 error("channel_setup_fwd_listener: cannot listen to port: %d",
Kevin Steves12057502001-02-05 14:54:34 +00002215 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002216 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002217 return success;
Damien Miller95def091999-11-25 00:26:21 +11002218}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002219
Damien Millerb16461c2002-01-22 23:29:22 +11002220/* protocol local port fwd, used by ssh (and sshd in v1) */
2221int
2222channel_setup_local_fwd_listener(u_short listen_port,
2223 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2224{
2225 return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
2226 NULL, listen_port, host_to_connect, port_to_connect, gateway_ports);
2227}
2228
2229/* protocol v2 remote port fwd, used by sshd */
2230int
2231channel_setup_remote_fwd_listener(const char *listen_address,
2232 u_short listen_port, int gateway_ports)
2233{
2234 return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
2235 listen_address, listen_port, NULL, 0, gateway_ports);
2236}
2237
Damien Miller5428f641999-11-25 11:54:57 +11002238/*
2239 * Initiate forwarding of connections to port "port" on remote host through
2240 * the secure channel to host:port from local side.
2241 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002242
Damien Miller4af51302000-04-16 11:18:38 +10002243void
Damien Miller0bc1bd82000-11-13 22:57:25 +11002244channel_request_remote_forwarding(u_short listen_port,
2245 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002246{
Damien Millerdff50992002-01-22 23:16:32 +11002247 int type, success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002248
Damien Miller95def091999-11-25 00:26:21 +11002249 /* Record locally that connection to this host/port is permitted. */
2250 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2251 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002252
Damien Miller95def091999-11-25 00:26:21 +11002253 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002254 if (compat20) {
2255 const char *address_to_bind = "0.0.0.0";
2256 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2257 packet_put_cstring("tcpip-forward");
Damien Miller2797f7f2002-04-23 21:09:44 +10002258 packet_put_char(1); /* boolean: want reply */
Damien Miller33b13562000-04-04 14:38:59 +10002259 packet_put_cstring(address_to_bind);
2260 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002261 packet_send();
2262 packet_write_wait();
2263 /* Assume that server accepts the request */
2264 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002265 } else {
2266 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002267 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002268 packet_put_cstring(host_to_connect);
2269 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002270 packet_send();
2271 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002272
2273 /* Wait for response from the remote side. */
Damien Millerdff50992002-01-22 23:16:32 +11002274 type = packet_read();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002275 switch (type) {
2276 case SSH_SMSG_SUCCESS:
2277 success = 1;
2278 break;
2279 case SSH_SMSG_FAILURE:
Damien Miller996acd22003-04-09 20:59:48 +10002280 logit("Warning: Server denied remote port forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11002281 break;
2282 default:
2283 /* Unknown packet */
2284 packet_disconnect("Protocol error for port forward request:"
2285 "received packet type %d.", type);
2286 }
2287 }
2288 if (success) {
2289 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2290 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2291 permitted_opens[num_permitted_opens].listen_port = listen_port;
2292 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002293 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002294}
2295
Damien Miller5428f641999-11-25 11:54:57 +11002296/*
2297 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2298 * listening for the port, and sends back a success reply (or disconnect
2299 * message if there was an error). This never returns if there was an error.
2300 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002301
Damien Miller4af51302000-04-16 11:18:38 +10002302void
Damien Millere247cc42000-05-07 12:03:14 +10002303channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002304{
Damien Milleraae6c611999-12-06 11:47:28 +11002305 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11002306 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002307
Damien Miller95def091999-11-25 00:26:21 +11002308 /* Get arguments from the packet. */
2309 port = packet_get_int();
2310 hostname = packet_get_string(NULL);
2311 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002312
Damien Millerbac2d8a2000-09-05 16:13:06 +11002313#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002314 /*
2315 * Check that an unprivileged user is not trying to forward a
2316 * privileged port.
2317 */
Damien Miller95def091999-11-25 00:26:21 +11002318 if (port < IPPORT_RESERVED && !is_root)
Darren Tucker9189ff82003-07-03 13:52:04 +10002319 packet_disconnect(
2320 "Requested forwarding of port %d but user is not root.",
2321 port);
2322 if (host_port == 0)
2323 packet_disconnect("Dynamic forwarding denied.");
Damien Millerbac2d8a2000-09-05 16:13:06 +11002324#endif
Darren Tucker9189ff82003-07-03 13:52:04 +10002325
Damien Miller0bc1bd82000-11-13 22:57:25 +11002326 /* Initiate forwarding */
Damien Millerb16461c2002-01-22 23:29:22 +11002327 channel_setup_local_fwd_listener(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002328
2329 /* Free the argument string. */
2330 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002331}
2332
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002333/*
2334 * Permits opening to any host/port if permitted_opens[] is empty. This is
2335 * usually called by the server, because the user could connect to any port
2336 * anyway, and the server has no way to know but to trust the client anyway.
2337 */
2338void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002339channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002340{
2341 if (num_permitted_opens == 0)
2342 all_opens_permitted = 1;
2343}
2344
Ben Lindstroma3700052001-04-05 23:26:32 +00002345void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002346channel_add_permitted_opens(char *host, int port)
2347{
2348 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2349 fatal("channel_request_remote_forwarding: too many forwards");
2350 debug("allow port forwarding to host %s port %d", host, port);
2351
2352 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2353 permitted_opens[num_permitted_opens].port_to_connect = port;
2354 num_permitted_opens++;
2355
2356 all_opens_permitted = 0;
2357}
2358
Ben Lindstroma3700052001-04-05 23:26:32 +00002359void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002360channel_clear_permitted_opens(void)
2361{
2362 int i;
2363
2364 for (i = 0; i < num_permitted_opens; i++)
2365 xfree(permitted_opens[i].host_to_connect);
2366 num_permitted_opens = 0;
2367
2368}
2369
2370
2371/* return socket to remote host, port */
Ben Lindstrombba81212001-06-25 05:01:22 +00002372static int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002373connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002374{
Damien Miller34132e52000-01-14 15:45:46 +11002375 struct addrinfo hints, *ai, *aitop;
2376 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2377 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002378 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002379
Damien Miller34132e52000-01-14 15:45:46 +11002380 memset(&hints, 0, sizeof(hints));
2381 hints.ai_family = IPv4or6;
2382 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002383 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002384 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002385 error("connect_to %.100s: unknown host (%s)", host,
2386 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002387 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002388 }
Damien Miller34132e52000-01-14 15:45:46 +11002389 for (ai = aitop; ai; ai = ai->ai_next) {
2390 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2391 continue;
2392 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2393 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002394 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002395 continue;
2396 }
Damien Miller2372ace2003-05-14 13:42:23 +10002397 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002398 if (sock < 0) {
Damien Millerb46b9f32003-01-10 21:45:12 +11002399 if (ai->ai_next == NULL)
2400 error("socket: %.100s", strerror(errno));
2401 else
2402 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002403 continue;
2404 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +00002405 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002406 fatal("connect_to: F_SETFL: %s", strerror(errno));
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002407 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2408 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002409 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002410 strerror(errno));
2411 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002412 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002413 }
2414 break; /* success */
2415
2416 }
2417 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002418 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002419 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002420 return -1;
2421 }
2422 /* success */
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00002423 set_nodelay(sock);
Damien Millerb38eff82000-04-01 11:09:21 +10002424 return sock;
2425}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002426
Damien Miller0bc1bd82000-11-13 22:57:25 +11002427int
Ben Lindstrom173e6462001-07-04 05:15:15 +00002428channel_connect_by_listen_address(u_short listen_port)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002429{
2430 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002431
Damien Miller0bc1bd82000-11-13 22:57:25 +11002432 for (i = 0; i < num_permitted_opens; i++)
2433 if (permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002434 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002435 permitted_opens[i].host_to_connect,
2436 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002437 error("WARNING: Server requests forwarding for unknown listen_port %d",
2438 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002439 return -1;
2440}
Damien Millerb38eff82000-04-01 11:09:21 +10002441
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002442/* Check if connecting to that port is permitted and connect. */
2443int
2444channel_connect_to(const char *host, u_short port)
2445{
2446 int i, permit;
2447
2448 permit = all_opens_permitted;
2449 if (!permit) {
2450 for (i = 0; i < num_permitted_opens; i++)
2451 if (permitted_opens[i].port_to_connect == port &&
2452 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2453 permit = 1;
2454
2455 }
2456 if (!permit) {
Damien Miller996acd22003-04-09 20:59:48 +10002457 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002458 "but the request was denied.", host, port);
2459 return -1;
2460 }
2461 return connect_to(host, port);
2462}
2463
Ben Lindstrome9c99912001-06-09 00:41:05 +00002464/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002465
Damien Miller5428f641999-11-25 11:54:57 +11002466/*
2467 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002468 * Returns 0 and a suitable display number for the DISPLAY variable
2469 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11002470 */
Kevin Steves366298c2001-12-19 17:58:01 +00002471int
Damien Miller95c249f2002-02-05 12:11:34 +11002472x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002473 int single_connection, u_int *display_numberp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002474{
Damien Millere7378562001-12-21 14:58:35 +11002475 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11002476 int display_number, sock;
2477 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002478 struct addrinfo hints, *ai, *aitop;
2479 char strport[NI_MAXSERV];
2480 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002481
Damien Millera34a28b1999-12-14 10:47:15 +11002482 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002483 display_number < MAX_DISPLAYS;
2484 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11002485 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002486 memset(&hints, 0, sizeof(hints));
2487 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11002488 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11002489 hints.ai_socktype = SOCK_STREAM;
2490 snprintf(strport, sizeof strport, "%d", port);
2491 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2492 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00002493 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002494 }
Damien Miller34132e52000-01-14 15:45:46 +11002495 for (ai = aitop; ai; ai = ai->ai_next) {
2496 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2497 continue;
Damien Miller2372ace2003-05-14 13:42:23 +10002498 sock = socket(ai->ai_family, ai->ai_socktype,
2499 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002500 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002501 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002502 error("socket: %.100s", strerror(errno));
Kevin Steves366298c2001-12-19 17:58:01 +00002503 return -1;
Damien Millere2192732000-01-17 13:22:55 +11002504 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002505 debug("x11_create_display_inet: Socket family %d not supported",
2506 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002507 continue;
2508 }
Damien Miller34132e52000-01-14 15:45:46 +11002509 }
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002510#ifdef IPV6_V6ONLY
2511 if (ai->ai_family == AF_INET6) {
2512 int on = 1;
2513 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
2514 error("setsockopt IPV6_V6ONLY: %.100s", strerror(errno));
2515 }
2516#endif
Damien Miller34132e52000-01-14 15:45:46 +11002517 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002518 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002519 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002520
2521 if (ai->ai_next)
2522 continue;
2523
Damien Miller34132e52000-01-14 15:45:46 +11002524 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11002525 close(socks[n]);
2526 }
2527 num_socks = 0;
2528 break;
2529 }
2530 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002531#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002532 if (num_socks == NUM_SOCKS)
2533 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002534#else
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002535 if (x11_use_localhost) {
2536 if (num_socks == NUM_SOCKS)
2537 break;
2538 } else {
2539 break;
2540 }
Damien Miller7bcb0892000-03-11 20:45:40 +11002541#endif
Damien Miller95def091999-11-25 00:26:21 +11002542 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002543 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002544 if (num_socks > 0)
2545 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002546 }
Damien Miller95def091999-11-25 00:26:21 +11002547 if (display_number >= MAX_DISPLAYS) {
2548 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00002549 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002550 }
Damien Miller95def091999-11-25 00:26:21 +11002551 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002552 for (n = 0; n < num_socks; n++) {
2553 sock = socks[n];
2554 if (listen(sock, 5) < 0) {
2555 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002556 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00002557 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11002558 }
Damien Miller95def091999-11-25 00:26:21 +11002559 }
Damien Miller34132e52000-01-14 15:45:46 +11002560
Damien Miller34132e52000-01-14 15:45:46 +11002561 /* Allocate a channel for each socket. */
2562 for (n = 0; n < num_socks; n++) {
2563 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11002564 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10002565 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2566 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002567 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11002568 nc->single_connection = single_connection;
Damien Miller34132e52000-01-14 15:45:46 +11002569 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002570
Kevin Steves366298c2001-12-19 17:58:01 +00002571 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002572 *display_numberp = display_number;
2573 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002574}
2575
Ben Lindstrombba81212001-06-25 05:01:22 +00002576static int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002577connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002578{
Damien Miller95def091999-11-25 00:26:21 +11002579 int sock;
2580 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002581
Damien Miller3afe3752001-12-21 12:39:51 +11002582 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2583 if (sock < 0)
2584 error("socket: %.100s", strerror(errno));
2585 memset(&addr, 0, sizeof(addr));
2586 addr.sun_family = AF_UNIX;
2587 snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
2588 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2589 return sock;
2590 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11002591 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2592 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002593}
2594
Damien Millerbd483e72000-04-30 10:00:53 +10002595int
2596x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002597{
Damien Miller398e1cf2002-02-05 11:52:13 +11002598 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002599 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002600 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002601 struct addrinfo hints, *ai, *aitop;
2602 char strport[NI_MAXSERV];
2603 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002604
Damien Miller95def091999-11-25 00:26:21 +11002605 /* Try to open a socket for the local X server. */
2606 display = getenv("DISPLAY");
2607 if (!display) {
2608 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002609 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002610 }
Damien Miller5428f641999-11-25 11:54:57 +11002611 /*
2612 * Now we decode the value of the DISPLAY variable and make a
2613 * connection to the real X server.
2614 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002615
Damien Miller5428f641999-11-25 11:54:57 +11002616 /*
2617 * Check if it is a unix domain socket. Unix domain displays are in
2618 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2619 */
Damien Miller95def091999-11-25 00:26:21 +11002620 if (strncmp(display, "unix:", 5) == 0 ||
2621 display[0] == ':') {
2622 /* Connect to the unix domain socket. */
2623 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2624 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11002625 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002626 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002627 }
2628 /* Create a socket. */
2629 sock = connect_local_xsocket(display_number);
2630 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002631 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002632
2633 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002634 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002635 }
Damien Miller5428f641999-11-25 11:54:57 +11002636 /*
2637 * Connect to an inet socket. The DISPLAY value is supposedly
2638 * hostname:d[.s], where hostname may also be numeric IP address.
2639 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00002640 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11002641 cp = strchr(buf, ':');
2642 if (!cp) {
2643 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002644 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002645 }
Damien Miller95def091999-11-25 00:26:21 +11002646 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002647 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002648 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2649 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11002650 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002651 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002652 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002653
Damien Miller34132e52000-01-14 15:45:46 +11002654 /* Look up the host address */
2655 memset(&hints, 0, sizeof(hints));
2656 hints.ai_family = IPv4or6;
2657 hints.ai_socktype = SOCK_STREAM;
2658 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2659 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2660 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002661 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002662 }
Damien Miller34132e52000-01-14 15:45:46 +11002663 for (ai = aitop; ai; ai = ai->ai_next) {
2664 /* Create a socket. */
Damien Miller2372ace2003-05-14 13:42:23 +10002665 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002666 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002667 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002668 continue;
2669 }
2670 /* Connect it to the display. */
2671 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002672 debug2("connect %.100s port %d: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10002673 6000 + display_number, strerror(errno));
2674 close(sock);
2675 continue;
2676 }
2677 /* Success */
2678 break;
Damien Miller34132e52000-01-14 15:45:46 +11002679 }
Damien Miller34132e52000-01-14 15:45:46 +11002680 freeaddrinfo(aitop);
2681 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002682 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002683 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002684 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002685 }
Damien Miller398e1cf2002-02-05 11:52:13 +11002686 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10002687 return sock;
2688}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002689
Damien Millerbd483e72000-04-30 10:00:53 +10002690/*
2691 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2692 * the remote channel number. We should do whatever we want, and respond
2693 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2694 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002695
Damien Millerbd483e72000-04-30 10:00:53 +10002696void
Damien Miller630d6f42002-01-22 23:17:30 +11002697x11_input_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002698{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002699 Channel *c = NULL;
2700 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002701 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10002702
2703 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002704
2705 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002706
2707 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002708 remote_host = packet_get_string(NULL);
2709 } else {
2710 remote_host = xstrdup("unknown (remote did not supply name)");
2711 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002712 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10002713
2714 /* Obtain a connection to the real X display. */
2715 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002716 if (sock != -1) {
2717 /* Allocate a channel for this connection. */
2718 c = channel_new("connected x11 socket",
2719 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
2720 remote_host, 1);
Damien Miller699d0032002-02-08 22:07:16 +11002721 c->remote_id = remote_id;
2722 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002723 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002724 xfree(remote_host);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002725 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10002726 /* Send refusal to the remote host. */
2727 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002728 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10002729 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10002730 /* Send a confirmation to the remote host. */
2731 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002732 packet_put_int(remote_id);
2733 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10002734 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002735 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002736}
2737
Damien Miller69b69aa2000-10-28 14:19:58 +11002738/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2739void
Damien Miller630d6f42002-01-22 23:17:30 +11002740deny_input_open(int type, u_int32_t seq, void *ctxt)
Damien Miller69b69aa2000-10-28 14:19:58 +11002741{
2742 int rchan = packet_get_int();
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002743
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002744 switch (type) {
Damien Miller69b69aa2000-10-28 14:19:58 +11002745 case SSH_SMSG_AGENT_OPEN:
2746 error("Warning: ssh server tried agent forwarding.");
2747 break;
2748 case SSH_SMSG_X11_OPEN:
2749 error("Warning: ssh server tried X11 forwarding.");
2750 break;
2751 default:
Damien Miller630d6f42002-01-22 23:17:30 +11002752 error("deny_input_open: type %d", type);
Damien Miller69b69aa2000-10-28 14:19:58 +11002753 break;
2754 }
2755 error("Warning: this is probably a break in attempt by a malicious server.");
2756 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2757 packet_put_int(rchan);
2758 packet_send();
2759}
2760
Damien Miller5428f641999-11-25 11:54:57 +11002761/*
2762 * Requests forwarding of X11 connections, generates fake authentication
2763 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00002764 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11002765 */
Damien Miller4af51302000-04-16 11:18:38 +10002766void
Damien Millerbd483e72000-04-30 10:00:53 +10002767x11_request_forwarding_with_spoofing(int client_session_id,
2768 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002769{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002770 u_int data_len = (u_int) strlen(data) / 2;
Ben Lindstromb3211a82001-02-10 22:33:19 +00002771 u_int i, value, len;
Damien Miller95def091999-11-25 00:26:21 +11002772 char *new_data;
2773 int screen_number;
2774 const char *cp;
2775 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002776
Damien Miller95def091999-11-25 00:26:21 +11002777 cp = getenv("DISPLAY");
2778 if (cp)
2779 cp = strchr(cp, ':');
2780 if (cp)
2781 cp = strchr(cp, '.');
2782 if (cp)
2783 screen_number = atoi(cp + 1);
2784 else
2785 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002786
Damien Miller95def091999-11-25 00:26:21 +11002787 /* Save protocol name. */
2788 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002789
Damien Miller5428f641999-11-25 11:54:57 +11002790 /*
2791 * Extract real authentication data and generate fake data of the
2792 * same length.
2793 */
Damien Miller95def091999-11-25 00:26:21 +11002794 x11_saved_data = xmalloc(data_len);
2795 x11_fake_data = xmalloc(data_len);
2796 for (i = 0; i < data_len; i++) {
2797 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2798 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2799 if (i % 4 == 0)
2800 rand = arc4random();
2801 x11_saved_data[i] = value;
2802 x11_fake_data[i] = rand & 0xff;
2803 rand >>= 8;
2804 }
2805 x11_saved_data_len = data_len;
2806 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002807
Damien Miller95def091999-11-25 00:26:21 +11002808 /* Convert the fake data into hex. */
Ben Lindstromb3211a82001-02-10 22:33:19 +00002809 len = 2 * data_len + 1;
2810 new_data = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +11002811 for (i = 0; i < data_len; i++)
Ben Lindstromb3211a82001-02-10 22:33:19 +00002812 snprintf(new_data + 2 * i, len - 2 * i,
2813 "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002814
Damien Miller95def091999-11-25 00:26:21 +11002815 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002816 if (compat20) {
2817 channel_request_start(client_session_id, "x11-req", 0);
2818 packet_put_char(0); /* XXX bool single connection */
2819 } else {
2820 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2821 }
2822 packet_put_cstring(proto);
2823 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002824 packet_put_int(screen_number);
2825 packet_send();
2826 packet_write_wait();
2827 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002828}
2829
Ben Lindstrome9c99912001-06-09 00:41:05 +00002830
2831/* -- agent forwarding */
2832
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002833/* Sends a message to the server to request authentication fd forwarding. */
2834
Damien Miller4af51302000-04-16 11:18:38 +10002835void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002836auth_request_forwarding(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002837{
Damien Miller95def091999-11-25 00:26:21 +11002838 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2839 packet_send();
2840 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002841}
2842
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002843/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2844
Damien Miller4af51302000-04-16 11:18:38 +10002845void
Damien Miller630d6f42002-01-22 23:17:30 +11002846auth_input_open_request(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002847{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002848 Channel *c = NULL;
2849 int remote_id, sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002850
Damien Miller95def091999-11-25 00:26:21 +11002851 /* Read the remote channel number from the message. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002852 remote_id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002853 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002854
Damien Miller5428f641999-11-25 11:54:57 +11002855 /*
2856 * Get a connection to the local authentication agent (this may again
2857 * get forwarded).
2858 */
Damien Miller95def091999-11-25 00:26:21 +11002859 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002860
Damien Miller5428f641999-11-25 11:54:57 +11002861 /*
2862 * If we could not connect the agent, send an error message back to
2863 * the server. This should never happen unless the agent dies,
2864 * because authentication forwarding is only enabled if we have an
2865 * agent.
2866 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002867 if (sock >= 0) {
Ben Lindstrome9c99912001-06-09 00:41:05 +00002868 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002869 -1, 0, 0, 0, "authentication agent connection", 1);
Damien Miller699d0032002-02-08 22:07:16 +11002870 c->remote_id = remote_id;
2871 c->force_drain = 1;
Damien Miller95def091999-11-25 00:26:21 +11002872 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002873 if (c == NULL) {
2874 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2875 packet_put_int(remote_id);
2876 } else {
2877 /* Send a confirmation to the remote host. */
2878 debug("Forwarding authentication connection.");
2879 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2880 packet_put_int(remote_id);
2881 packet_put_int(c->self);
2882 }
Damien Miller95def091999-11-25 00:26:21 +11002883 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002884}