blob: d9513fada522667086088884bd5da5a821ae82e4 [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 *
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Millere4340be2000-09-16 13:29:08 +110017 * Copyright (c) 1999,2000 Markus Friedl. All rights reserved.
18 * Copyright (c) 1999 Dug Song. All rights reserved.
19 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Ben Lindstrom601e4362001-06-21 03:19:23 +000043RCSID("$OpenBSD: channels.c,v 1.126 2001/06/20 13:56:39 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "ssh1.h"
47#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048#include "packet.h"
49#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "log.h"
52#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100056#include "key.h"
57#include "authfd.h"
58
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Ben Lindstrome9c99912001-06-09 00:41:05 +000060/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
Damien Miller5428f641999-11-25 11:54:57 +110062/*
63 * Pointer to an array containing all allocated channels. The array is
64 * dynamically extended as needed.
65 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000066static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067
Damien Miller5428f641999-11-25 11:54:57 +110068/*
69 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000070 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110071 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072static int channels_alloc = 0;
73
Damien Miller5428f641999-11-25 11:54:57 +110074/*
75 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +000076 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +110077 */
Damien Miller5e953212001-01-30 09:14:00 +110078static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
Ben Lindstrome9c99912001-06-09 00:41:05 +000081/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Miller5428f641999-11-25 11:54:57 +110083/*
84 * Data structure for storing which hosts are permitted for forward requests.
85 * The local sides of any remote forwards are stored in this array to prevent
86 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
87 * network (which might be behind a firewall).
88 */
Damien Miller95def091999-11-25 00:26:21 +110089typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +100090 char *host_to_connect; /* Connect to 'host'. */
91 u_short port_to_connect; /* Connect to 'port'. */
92 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093} ForwardPermission;
94
95/* List of all permitted host/port pairs to connect. */
96static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
Ben Lindstrome9c99912001-06-09 00:41:05 +000097
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098/* Number of permitted host/port pairs in the array. */
99static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * If this is true, all opens are permitted. This is the case on the server
102 * on which we have to trust the client anyway, and the user could do
103 * anything after logging in anyway.
104 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105static int all_opens_permitted = 0;
106
Ben Lindstrome9c99912001-06-09 00:41:05 +0000107
108/* -- X11 forwarding */
109
110/* Maximum number of fake X11 displays to try. */
111#define MAX_DISPLAYS 1000
112
113/* Saved X11 authentication protocol name. */
114static char *x11_saved_proto = NULL;
115
116/* Saved X11 authentication data. This is the real data. */
117static char *x11_saved_data = NULL;
118static u_int x11_saved_data_len = 0;
119
120/*
121 * Fake X11 authentication data. This is what the server will be sending us;
122 * we should replace any occurrences of this by the real data.
123 */
124static char *x11_fake_data = NULL;
125static u_int x11_fake_data_len;
126
127
128/* -- agent forwarding */
129
130#define NUM_SOCKS 10
131
132/* Name and directory of socket for authentication agent forwarding. */
133static char *auth_sock_name = NULL;
134static char *auth_sock_dir = NULL;
135
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000137/* AF_UNSPEC or AF_INET or AF_INET6 */
138extern int IPv4or6;
139
Ben Lindstrome9c99912001-06-09 00:41:05 +0000140/* helper */
141void port_open_helper(Channel *c, char *rtype);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000142
Ben Lindstrome9c99912001-06-09 00:41:05 +0000143/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000144
145Channel *
146channel_lookup(int id)
147{
148 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000149
Damien Millerc0fd17f2000-06-26 10:22:53 +1000150 if (id < 0 || id > channels_alloc) {
Damien Millerb38eff82000-04-01 11:09:21 +1000151 log("channel_lookup: %d: bad id", id);
152 return NULL;
153 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000154 c = channels[id];
155 if (c == NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +1000156 log("channel_lookup: %d: bad id: channel free", id);
157 return NULL;
158 }
159 return c;
160}
161
Damien Miller5428f641999-11-25 11:54:57 +1100162/*
Damien Millere247cc42000-05-07 12:03:14 +1000163 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000164 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100165 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166
Damien Miller6f83b8e2000-05-02 09:23:45 +1000167void
Damien Miller69b69aa2000-10-28 14:19:58 +1100168channel_register_fds(Channel *c, int rfd, int wfd, int efd,
169 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170{
Damien Miller95def091999-11-25 00:26:21 +1100171 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100172 channel_max_fd = MAX(channel_max_fd, rfd);
173 channel_max_fd = MAX(channel_max_fd, wfd);
174 channel_max_fd = MAX(channel_max_fd, efd);
175
Damien Miller5428f641999-11-25 11:54:57 +1100176 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000177
Damien Miller6f83b8e2000-05-02 09:23:45 +1000178 c->rfd = rfd;
179 c->wfd = wfd;
180 c->sock = (rfd == wfd) ? rfd : -1;
181 c->efd = efd;
182 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100183
Damien Miller79438cc2001-02-16 12:34:57 +1100184 /* XXX ugly hack: nonblock is only set by the server */
185 if (nonblock && isatty(c->rfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000186 debug("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100187 c->isatty = 1;
188 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000189 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100190 c->self, c->wfd);
191 }
192 } else {
193 c->isatty = 0;
194 }
195
Damien Miller69b69aa2000-10-28 14:19:58 +1100196 /* enable nonblocking mode */
197 if (nonblock) {
198 if (rfd != -1)
199 set_nonblock(rfd);
200 if (wfd != -1)
201 set_nonblock(wfd);
202 if (efd != -1)
203 set_nonblock(efd);
204 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000205}
206
207/*
208 * Allocate a new channel object and set its type and socket. This will cause
209 * remote_name to be freed.
210 */
211
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000212Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000213channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Damien Miller69b69aa2000-10-28 14:19:58 +1100214 int window, int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000215{
216 int i, found;
217 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218
Damien Miller95def091999-11-25 00:26:21 +1100219 /* Do initial allocation if this is the first call. */
220 if (channels_alloc == 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000221 chan_init();
Damien Miller95def091999-11-25 00:26:21 +1100222 channels_alloc = 10;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000223 channels = xmalloc(channels_alloc * sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100224 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000225 channels[i] = NULL;
Ben Lindstrom601e4362001-06-21 03:19:23 +0000226 fatal_add_cleanup((void (*) (void *)) channel_free_all, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100227 }
228 /* Try to find a free slot where to put the new channel. */
229 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000230 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100231 /* Found a free slot. */
232 found = i;
233 break;
234 }
235 if (found == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100236 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100237 found = channels_alloc;
238 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100239 debug2("channel: expanding %d", channels_alloc);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000240 channels = xrealloc(channels, channels_alloc * sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100241 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000242 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100243 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000244 /* Initialize and return new channel. */
245 c = channels[found] = xmalloc(sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100246 buffer_init(&c->input);
247 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000248 buffer_init(&c->extended);
Damien Miller95def091999-11-25 00:26:21 +1100249 chan_init_iostates(c);
Damien Miller69b69aa2000-10-28 14:19:58 +1100250 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100251 c->self = found;
252 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000253 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000254 c->local_window = window;
255 c->local_window_max = window;
256 c->local_consumed = 0;
257 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100258 c->remote_id = -1;
259 c->remote_name = remote_name;
Damien Millerb38eff82000-04-01 11:09:21 +1000260 c->remote_window = 0;
261 c->remote_maxpacket = 0;
262 c->cb_fn = NULL;
263 c->cb_arg = NULL;
264 c->cb_event = 0;
265 c->dettach_user = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000266 c->input_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100267 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000268 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000270
271/* Close all channel fd/socket. */
272
273void
274channel_close_fds(Channel *c)
275{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000276 debug3("channel_close_fds: channel %d: r %d w %d e %d",
277 c->self, c->rfd, c->wfd, c->efd);
278
Damien Miller6f83b8e2000-05-02 09:23:45 +1000279 if (c->sock != -1) {
280 close(c->sock);
281 c->sock = -1;
282 }
283 if (c->rfd != -1) {
284 close(c->rfd);
285 c->rfd = -1;
286 }
287 if (c->wfd != -1) {
288 close(c->wfd);
289 c->wfd = -1;
290 }
291 if (c->efd != -1) {
292 close(c->efd);
293 c->efd = -1;
294 }
295}
296
297/* Free the channel and close its fd/socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298
Damien Miller4af51302000-04-16 11:18:38 +1000299void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000300channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000301{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000302 char *s;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000303 int i, n;
304
305 for (n = 0, i = 0; i < channels_alloc; i++)
306 if (channels[i])
307 n++;
Ben Lindstrom4c247552001-06-05 20:56:47 +0000308 debug("channel_free: channel %d: %s, nchannels %d", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000309 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000310
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000311 s = channel_open_message();
Ben Lindstrom4c247552001-06-05 20:56:47 +0000312 debug3("channel_free: status: %s", s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000313 xfree(s);
314
Damien Miller33b13562000-04-04 14:38:59 +1000315 if (c->dettach_user != NULL) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000316 debug("channel_free: channel %d: dettaching channel user", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000317 c->dettach_user(c->self, NULL);
318 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000319 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000320 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000321 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000322 buffer_free(&c->input);
323 buffer_free(&c->output);
324 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000325 if (c->remote_name) {
326 xfree(c->remote_name);
327 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100328 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000329 channels[c->self] = NULL;
330 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000331}
332
Ben Lindstrome9c99912001-06-09 00:41:05 +0000333void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000334channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000335{
336 int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000337
Ben Lindstrom601e4362001-06-21 03:19:23 +0000338 for (i = 0; i < channels_alloc; i++)
339 if (channels[i] != NULL)
340 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000341}
342
343/*
344 * Closes the sockets/fds of all channels. This is used to close extra file
345 * descriptors after a fork.
346 */
347
348void
349channel_close_all()
350{
351 int i;
352
353 for (i = 0; i < channels_alloc; i++)
354 if (channels[i] != NULL)
355 channel_close_fds(channels[i]);
356}
357
358/*
359 * Returns true if no channel has too much buffered data, and false if one or
360 * more channel is overfull.
361 */
362
363int
364channel_not_very_much_buffered_data()
365{
366 u_int i;
367 Channel *c;
368
369 for (i = 0; i < channels_alloc; i++) {
370 c = channels[i];
371 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
372 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
373 debug("channel %d: big input buffer %d",
374 c->self, buffer_len(&c->input));
375 return 0;
376 }
377 if (buffer_len(&c->output) > packet_get_maxsize()) {
378 debug("channel %d: big output buffer %d",
379 c->self, buffer_len(&c->output));
380 return 0;
381 }
382 }
383 }
384 return 1;
385}
386
387/* Returns true if any channel is still open. */
388
389int
390channel_still_open()
391{
392 int i;
393 Channel *c;
394
395 for (i = 0; i < channels_alloc; i++) {
396 c = channels[i];
397 if (c == NULL)
398 continue;
399 switch (c->type) {
400 case SSH_CHANNEL_X11_LISTENER:
401 case SSH_CHANNEL_PORT_LISTENER:
402 case SSH_CHANNEL_RPORT_LISTENER:
403 case SSH_CHANNEL_CLOSED:
404 case SSH_CHANNEL_AUTH_SOCKET:
405 case SSH_CHANNEL_DYNAMIC:
406 case SSH_CHANNEL_CONNECTING:
407 case SSH_CHANNEL_ZOMBIE:
408 continue;
409 case SSH_CHANNEL_LARVAL:
410 if (!compat20)
411 fatal("cannot happen: SSH_CHANNEL_LARVAL");
412 continue;
413 case SSH_CHANNEL_OPENING:
414 case SSH_CHANNEL_OPEN:
415 case SSH_CHANNEL_X11_OPEN:
416 return 1;
417 case SSH_CHANNEL_INPUT_DRAINING:
418 case SSH_CHANNEL_OUTPUT_DRAINING:
419 if (!compat13)
420 fatal("cannot happen: OUT_DRAIN");
421 return 1;
422 default:
423 fatal("channel_still_open: bad channel type %d", c->type);
424 /* NOTREACHED */
425 }
426 }
427 return 0;
428}
429
430/* Returns the id of an open channel suitable for keepaliving */
431
432int
433channel_find_open()
434{
435 int i;
436 Channel *c;
437
438 for (i = 0; i < channels_alloc; i++) {
439 c = channels[i];
440 if (c == NULL)
441 continue;
442 switch (c->type) {
443 case SSH_CHANNEL_CLOSED:
444 case SSH_CHANNEL_DYNAMIC:
445 case SSH_CHANNEL_X11_LISTENER:
446 case SSH_CHANNEL_PORT_LISTENER:
447 case SSH_CHANNEL_RPORT_LISTENER:
448 case SSH_CHANNEL_OPENING:
449 case SSH_CHANNEL_CONNECTING:
450 case SSH_CHANNEL_ZOMBIE:
451 continue;
452 case SSH_CHANNEL_LARVAL:
453 case SSH_CHANNEL_AUTH_SOCKET:
454 case SSH_CHANNEL_OPEN:
455 case SSH_CHANNEL_X11_OPEN:
456 return i;
457 case SSH_CHANNEL_INPUT_DRAINING:
458 case SSH_CHANNEL_OUTPUT_DRAINING:
459 if (!compat13)
460 fatal("cannot happen: OUT_DRAIN");
461 return i;
462 default:
463 fatal("channel_find_open: bad channel type %d", c->type);
464 /* NOTREACHED */
465 }
466 }
467 return -1;
468}
469
470
471/*
472 * Returns a message describing the currently open forwarded connections,
473 * suitable for sending to the client. The message contains crlf pairs for
474 * newlines.
475 */
476
477char *
478channel_open_message()
479{
480 Buffer buffer;
481 Channel *c;
482 char buf[1024], *cp;
483 int i;
484
485 buffer_init(&buffer);
486 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
487 buffer_append(&buffer, buf, strlen(buf));
488 for (i = 0; i < channels_alloc; i++) {
489 c = channels[i];
490 if (c == NULL)
491 continue;
492 switch (c->type) {
493 case SSH_CHANNEL_X11_LISTENER:
494 case SSH_CHANNEL_PORT_LISTENER:
495 case SSH_CHANNEL_RPORT_LISTENER:
496 case SSH_CHANNEL_CLOSED:
497 case SSH_CHANNEL_AUTH_SOCKET:
498 case SSH_CHANNEL_ZOMBIE:
499 continue;
500 case SSH_CHANNEL_LARVAL:
501 case SSH_CHANNEL_OPENING:
502 case SSH_CHANNEL_CONNECTING:
503 case SSH_CHANNEL_DYNAMIC:
504 case SSH_CHANNEL_OPEN:
505 case SSH_CHANNEL_X11_OPEN:
506 case SSH_CHANNEL_INPUT_DRAINING:
507 case SSH_CHANNEL_OUTPUT_DRAINING:
508 snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
509 c->self, c->remote_name,
510 c->type, c->remote_id,
511 c->istate, buffer_len(&c->input),
512 c->ostate, buffer_len(&c->output),
513 c->rfd, c->wfd);
514 buffer_append(&buffer, buf, strlen(buf));
515 continue;
516 default:
517 fatal("channel_open_message: bad channel type %d", c->type);
518 /* NOTREACHED */
519 }
520 }
521 buffer_append(&buffer, "\0", 1);
522 cp = xstrdup(buffer_ptr(&buffer));
523 buffer_free(&buffer);
524 return cp;
525}
526
527void
528channel_send_open(int id)
529{
530 Channel *c = channel_lookup(id);
531 if (c == NULL) {
532 log("channel_send_open: %d: bad id", id);
533 return;
534 }
535 debug("send channel open %d", id);
536 packet_start(SSH2_MSG_CHANNEL_OPEN);
537 packet_put_cstring(c->ctype);
538 packet_put_int(c->self);
539 packet_put_int(c->local_window);
540 packet_put_int(c->local_maxpacket);
541 packet_send();
542}
543
544void
545channel_request(int id, char *service, int wantconfirm)
546{
547 channel_request_start(id, service, wantconfirm);
548 packet_send();
549 debug("channel request %d: %s", id, service) ;
550}
551void
552channel_request_start(int id, char *service, int wantconfirm)
553{
554 Channel *c = channel_lookup(id);
555 if (c == NULL) {
556 log("channel_request: %d: bad id", id);
557 return;
558 }
559 packet_start(SSH2_MSG_CHANNEL_REQUEST);
560 packet_put_int(c->remote_id);
561 packet_put_cstring(service);
562 packet_put_char(wantconfirm);
563}
564void
565channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
566{
567 Channel *c = channel_lookup(id);
568 if (c == NULL) {
569 log("channel_register_callback: %d: bad id", id);
570 return;
571 }
572 c->cb_event = mtype;
573 c->cb_fn = fn;
574 c->cb_arg = arg;
575}
576void
577channel_register_cleanup(int id, channel_callback_fn *fn)
578{
579 Channel *c = channel_lookup(id);
580 if (c == NULL) {
581 log("channel_register_cleanup: %d: bad id", id);
582 return;
583 }
584 c->dettach_user = fn;
585}
586void
587channel_cancel_cleanup(int id)
588{
589 Channel *c = channel_lookup(id);
590 if (c == NULL) {
591 log("channel_cancel_cleanup: %d: bad id", id);
592 return;
593 }
594 c->dettach_user = NULL;
595}
596void
597channel_register_filter(int id, channel_filter_fn *fn)
598{
599 Channel *c = channel_lookup(id);
600 if (c == NULL) {
601 log("channel_register_filter: %d: bad id", id);
602 return;
603 }
604 c->input_filter = fn;
605}
606
607void
608channel_set_fds(int id, int rfd, int wfd, int efd,
609 int extusage, int nonblock)
610{
611 Channel *c = channel_lookup(id);
612 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
613 fatal("channel_activate for non-larval channel %d.", id);
614 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
615 c->type = SSH_CHANNEL_OPEN;
616 /* XXX window size? */
617 c->local_window = c->local_window_max = c->local_maxpacket * 2;
618 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
619 packet_put_int(c->remote_id);
620 packet_put_int(c->local_window);
621 packet_send();
622}
623
Damien Miller5428f641999-11-25 11:54:57 +1100624/*
Damien Millerb38eff82000-04-01 11:09:21 +1000625 * 'channel_pre*' are called just before select() to add any bits relevant to
626 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100627 */
Damien Millerb38eff82000-04-01 11:09:21 +1000628/*
629 * 'channel_post*': perform any appropriate operations for channels which
630 * have events pending.
631 */
632typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
633chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
634chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
635
636void
637channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
638{
639 FD_SET(c->sock, readset);
640}
641
642void
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000643channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
644{
645 debug3("channel %d: waiting for connection", c->self);
646 FD_SET(c->sock, writeset);
647}
648
649void
Damien Millerb38eff82000-04-01 11:09:21 +1000650channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
651{
652 if (buffer_len(&c->input) < packet_get_maxsize())
653 FD_SET(c->sock, readset);
654 if (buffer_len(&c->output) > 0)
655 FD_SET(c->sock, writeset);
656}
657
658void
659channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
660{
661 /* test whether sockets are 'alive' for read/write */
662 if (c->istate == CHAN_INPUT_OPEN)
663 if (buffer_len(&c->input) < packet_get_maxsize())
664 FD_SET(c->sock, readset);
665 if (c->ostate == CHAN_OUTPUT_OPEN ||
666 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
667 if (buffer_len(&c->output) > 0) {
668 FD_SET(c->sock, writeset);
669 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
670 chan_obuf_empty(c);
671 }
672 }
673}
674
675void
Damien Miller33b13562000-04-04 14:38:59 +1000676channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
677{
678 if (c->istate == CHAN_INPUT_OPEN &&
679 c->remote_window > 0 &&
680 buffer_len(&c->input) < c->remote_window)
681 FD_SET(c->rfd, readset);
682 if (c->ostate == CHAN_OUTPUT_OPEN ||
683 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
684 if (buffer_len(&c->output) > 0) {
685 FD_SET(c->wfd, writeset);
686 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
687 chan_obuf_empty(c);
688 }
689 }
690 /** XXX check close conditions, too */
691 if (c->efd != -1) {
692 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
693 buffer_len(&c->extended) > 0)
694 FD_SET(c->efd, writeset);
695 else if (c->extended_usage == CHAN_EXTENDED_READ &&
696 buffer_len(&c->extended) < c->remote_window)
697 FD_SET(c->efd, readset);
698 }
699}
700
701void
Damien Millerb38eff82000-04-01 11:09:21 +1000702channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
703{
704 if (buffer_len(&c->input) == 0) {
705 packet_start(SSH_MSG_CHANNEL_CLOSE);
706 packet_put_int(c->remote_id);
707 packet_send();
708 c->type = SSH_CHANNEL_CLOSED;
Ben Lindstromc486d882001-04-11 16:08:34 +0000709 debug("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000710 }
711}
712
713void
714channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
715{
716 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000717 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000718 else
Damien Millerb38eff82000-04-01 11:09:21 +1000719 FD_SET(c->sock, writeset);
720}
721
722/*
723 * This is a special state for X11 authentication spoofing. An opened X11
724 * connection (when authentication spoofing is being done) remains in this
725 * state until the first packet has been completely read. The authentication
726 * data in that packet is then substituted by the real data if it matches the
727 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000728 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000729 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000730 */
731int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000732x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000733{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000734 u_char *ucp;
735 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000736
737 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000738 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000739 return 0;
740
741 /* Parse the lengths of variable-length fields. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000742 ucp = (u_char *) buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000743 if (ucp[0] == 0x42) { /* Byte order MSB first. */
744 proto_len = 256 * ucp[6] + ucp[7];
745 data_len = 256 * ucp[8] + ucp[9];
746 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
747 proto_len = ucp[6] + 256 * ucp[7];
748 data_len = ucp[8] + 256 * ucp[9];
749 } else {
750 debug("Initial X11 packet contains bad byte order byte: 0x%x",
751 ucp[0]);
752 return -1;
753 }
754
755 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000756 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000757 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
758 return 0;
759
760 /* Check if authentication protocol matches. */
761 if (proto_len != strlen(x11_saved_proto) ||
762 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
763 debug("X11 connection uses different authentication protocol.");
764 return -1;
765 }
766 /* Check if authentication data matches our fake data. */
767 if (data_len != x11_fake_data_len ||
768 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
769 x11_fake_data, x11_fake_data_len) != 0) {
770 debug("X11 auth data does not match fake data.");
771 return -1;
772 }
773 /* Check fake data length */
774 if (x11_fake_data_len != x11_saved_data_len) {
775 error("X11 fake_data_len %d != saved_data_len %d",
776 x11_fake_data_len, x11_saved_data_len);
777 return -1;
778 }
779 /*
780 * Received authentication protocol and data match
781 * our fake data. Substitute the fake data with real
782 * data.
783 */
784 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
785 x11_saved_data, x11_saved_data_len);
786 return 1;
787}
788
789void
790channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
791{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000792 int ret = x11_open_helper(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000793 if (ret == 1) {
794 /* Start normal processing for the channel. */
795 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000796 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000797 } else if (ret == -1) {
798 /*
799 * We have received an X11 connection that has bad
800 * authentication information.
801 */
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000802 log("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000803 buffer_clear(&c->input);
804 buffer_clear(&c->output);
805 close(c->sock);
806 c->sock = -1;
807 c->type = SSH_CHANNEL_CLOSED;
808 packet_start(SSH_MSG_CHANNEL_CLOSE);
809 packet_put_int(c->remote_id);
810 packet_send();
811 }
812}
813
814void
Damien Millerbd483e72000-04-30 10:00:53 +1000815channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000816{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000817 int ret = x11_open_helper(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000818 if (ret == 1) {
819 c->type = SSH_CHANNEL_OPEN;
Damien Miller30c3d422000-05-09 11:02:59 +1000820 if (compat20)
821 channel_pre_open_20(c, readset, writeset);
822 else
823 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000824 } else if (ret == -1) {
825 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerbd483e72000-04-30 10:00:53 +1000826 chan_read_failed(c); /** force close? */
Damien Millerb38eff82000-04-01 11:09:21 +1000827 chan_write_failed(c);
828 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
829 }
830}
831
Ben Lindstromb3921512001-04-11 15:57:50 +0000832/* try to decode a socks4 header */
833int
834channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000835{
836 u_char *p, *host;
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000837 int len, have, i, found;
Ben Lindstromb3921512001-04-11 15:57:50 +0000838 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000839 struct {
840 u_int8_t version;
841 u_int8_t command;
842 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000843 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000844 } s4_req, s4_rsp;
845
Ben Lindstromb3921512001-04-11 15:57:50 +0000846 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000847
848 have = buffer_len(&c->input);
849 len = sizeof(s4_req);
850 if (have < len)
851 return 0;
852 p = buffer_ptr(&c->input);
853 for (found = 0, i = len; i < have; i++) {
854 if (p[i] == '\0') {
855 found = 1;
856 break;
857 }
858 if (i > 1024) {
859 /* the peer is probably sending garbage */
860 debug("channel %d: decode socks4: too long",
861 c->self);
862 return -1;
863 }
864 }
865 if (!found)
866 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000867 buffer_get(&c->input, (char *)&s4_req.version, 1);
868 buffer_get(&c->input, (char *)&s4_req.command, 1);
869 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000870 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000871 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000872 p = buffer_ptr(&c->input);
873 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000874 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000875 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000876 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000877 c->self, len, have);
878 strlcpy(username, p, sizeof(username));
879 buffer_consume(&c->input, len);
880 buffer_consume(&c->input, 1); /* trailing '\0' */
881
Ben Lindstromb3921512001-04-11 15:57:50 +0000882 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000883 strlcpy(c->path, host, sizeof(c->path));
884 c->host_port = ntohs(s4_req.dest_port);
885
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000886 debug("channel %d: dynamic request: socks4 host %s port %u command %u",
887 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000888
Ben Lindstromb3921512001-04-11 15:57:50 +0000889 if (s4_req.command != 1) {
890 debug("channel %d: cannot handle: socks4 cn %d",
891 c->self, s4_req.command);
892 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000893 }
Ben Lindstromb3921512001-04-11 15:57:50 +0000894 s4_rsp.version = 0; /* vn: 0 for reply */
895 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000896 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +0000897 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000898 buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +0000899 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000900}
901
Ben Lindstromb3921512001-04-11 15:57:50 +0000902/* dynamic port forwarding */
903void
904channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
905{
906 u_char *p;
907 int have, ret;
908
909 have = buffer_len(&c->input);
910
911 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +0000912 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +0000913 /* check if the fixed size part of the packet is in buffer. */
914 if (have < 4) {
915 /* need more */
916 FD_SET(c->sock, readset);
917 return;
918 }
919 /* try to guess the protocol */
920 p = buffer_ptr(&c->input);
921 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000922 case 0x04:
923 ret = channel_decode_socks4(c, readset, writeset);
924 break;
Ben Lindstromb3921512001-04-11 15:57:50 +0000925 default:
926 ret = -1;
927 break;
928 }
929 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000930 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +0000931 } else if (ret == 0) {
932 debug2("channel %d: pre_dynamic: need more", c->self);
933 /* need more */
934 FD_SET(c->sock, readset);
935 } else {
936 /* switch to the next state */
937 c->type = SSH_CHANNEL_OPENING;
938 port_open_helper(c, "direct-tcpip");
939 }
940}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000941
Damien Millerb38eff82000-04-01 11:09:21 +1000942/* This is our fake X11 server socket. */
943void
944channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
945{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000946 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +1000947 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000948 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +1000949 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +1100950 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +1000951 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +1000952
953 if (FD_ISSET(c->sock, readset)) {
954 debug("X11 connection requested.");
955 addrlen = sizeof(addr);
956 newsock = accept(c->sock, &addr, &addrlen);
957 if (newsock < 0) {
958 error("accept: %.100s", strerror(errno));
959 return;
960 }
Damien Millerd83ff352001-01-30 09:19:34 +1100961 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +1000962 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +1000963 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +1100964 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +1000965
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000966 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +1000967 SSH_CHANNEL_OPENING, newsock, newsock, -1,
968 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100969 0, xstrdup(buf), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000970 if (nc == NULL) {
971 close(newsock);
972 xfree(remote_ipaddr);
973 return;
974 }
Damien Millerbd483e72000-04-30 10:00:53 +1000975 if (compat20) {
976 packet_start(SSH2_MSG_CHANNEL_OPEN);
977 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000978 packet_put_int(nc->self);
Damien Millerbd483e72000-04-30 10:00:53 +1000979 packet_put_int(c->local_window_max);
980 packet_put_int(c->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +1100981 /* originator ipaddr and port */
982 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +1000983 if (datafellows & SSH_BUG_X11FWD) {
984 debug("ssh2 x11 bug compat mode");
985 } else {
986 packet_put_int(remote_port);
987 }
Damien Millerbd483e72000-04-30 10:00:53 +1000988 packet_send();
989 } else {
990 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000991 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000992 if (packet_get_protocol_flags() &
993 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +0000994 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +1000995 packet_send();
996 }
Damien Millerd83ff352001-01-30 09:19:34 +1100997 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +1000998 }
999}
1000
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001001void
1002port_open_helper(Channel *c, char *rtype)
1003{
1004 int direct;
1005 char buf[1024];
1006 char *remote_ipaddr = get_peer_ipaddr(c->sock);
1007 u_short remote_port = get_peer_port(c->sock);
1008
1009 direct = (strcmp(rtype, "direct-tcpip") == 0);
1010
1011 snprintf(buf, sizeof buf,
1012 "%s: listening port %d for %.100s port %d, "
1013 "connect from %.200s port %d",
1014 rtype, c->listening_port, c->path, c->host_port,
1015 remote_ipaddr, remote_port);
1016
1017 xfree(c->remote_name);
1018 c->remote_name = xstrdup(buf);
1019
1020 if (compat20) {
1021 packet_start(SSH2_MSG_CHANNEL_OPEN);
1022 packet_put_cstring(rtype);
1023 packet_put_int(c->self);
1024 packet_put_int(c->local_window_max);
1025 packet_put_int(c->local_maxpacket);
1026 if (direct) {
1027 /* target host, port */
1028 packet_put_cstring(c->path);
1029 packet_put_int(c->host_port);
1030 } else {
1031 /* listen address, port */
1032 packet_put_cstring(c->path);
1033 packet_put_int(c->listening_port);
1034 }
1035 /* originator host and port */
1036 packet_put_cstring(remote_ipaddr);
1037 packet_put_int(remote_port);
1038 packet_send();
1039 } else {
1040 packet_start(SSH_MSG_PORT_OPEN);
1041 packet_put_int(c->self);
1042 packet_put_cstring(c->path);
1043 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001044 if (packet_get_protocol_flags() &
1045 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001046 packet_put_cstring(c->remote_name);
1047 packet_send();
1048 }
1049 xfree(remote_ipaddr);
1050}
1051
Damien Millerb38eff82000-04-01 11:09:21 +10001052/*
1053 * This socket is listening for connections to a forwarded TCP/IP port.
1054 */
1055void
1056channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
1057{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001058 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001059 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001060 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001061 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001062 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001063
Damien Millerb38eff82000-04-01 11:09:21 +10001064 if (FD_ISSET(c->sock, readset)) {
1065 debug("Connection to port %d forwarding "
1066 "to %.100s port %d requested.",
1067 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001068
1069 rtype = (c->type == SSH_CHANNEL_RPORT_LISTENER) ?
1070 "forwarded-tcpip" : "direct-tcpip";
Ben Lindstrom6d618462001-05-10 23:24:49 +00001071 nextstate = (c->host_port == 0 &&
1072 c->type != SSH_CHANNEL_RPORT_LISTENER) ?
1073 SSH_CHANNEL_DYNAMIC : SSH_CHANNEL_OPENING;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001074
Damien Millerb38eff82000-04-01 11:09:21 +10001075 addrlen = sizeof(addr);
1076 newsock = accept(c->sock, &addr, &addrlen);
1077 if (newsock < 0) {
1078 error("accept: %.100s", strerror(errno));
1079 return;
1080 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001081 nc = channel_new(rtype,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001082 nextstate, newsock, newsock, -1,
Damien Millerb38eff82000-04-01 11:09:21 +10001083 c->local_window_max, c->local_maxpacket,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001084 0, xstrdup(rtype), 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001085 if (nc == NULL) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001086 error("channel_post_port_listener: no new channel:");
1087 close(newsock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001088 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001089 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001090 nc->listening_port = c->listening_port;
1091 nc->host_port = c->host_port;
1092 strlcpy(nc->path, c->path, sizeof(nc->path));
1093
1094 if (nextstate != SSH_CHANNEL_DYNAMIC)
1095 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001096 }
1097}
1098
1099/*
1100 * This is the authentication agent socket listening for connections from
1101 * clients.
1102 */
1103void
1104channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
1105{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001106 Channel *nc;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001107 char *name;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001108 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001109 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001110 socklen_t addrlen;
1111
1112 if (FD_ISSET(c->sock, readset)) {
1113 addrlen = sizeof(addr);
1114 newsock = accept(c->sock, &addr, &addrlen);
1115 if (newsock < 0) {
1116 error("accept from auth socket: %.100s", strerror(errno));
1117 return;
1118 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001119 name = xstrdup("accepted auth socket");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001120 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001121 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1122 c->local_window_max, c->local_maxpacket,
Ben Lindstrome9c99912001-06-09 00:41:05 +00001123 0, name, 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001124 if (nc == NULL) {
1125 error("channel_post_auth_listener: channel_new failed");
Ben Lindstrome9c99912001-06-09 00:41:05 +00001126 xfree(name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001127 close(newsock);
1128 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001129 if (compat20) {
1130 packet_start(SSH2_MSG_CHANNEL_OPEN);
1131 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001132 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001133 packet_put_int(c->local_window_max);
1134 packet_put_int(c->local_maxpacket);
1135 } else {
1136 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001137 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001138 }
Damien Millerb38eff82000-04-01 11:09:21 +10001139 packet_send();
1140 }
1141}
1142
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001143void
1144channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
1145{
Ben Lindstrom69128662001-05-08 20:07:39 +00001146 int err = 0;
1147 int sz = sizeof(err);
1148
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001149 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001150 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err,
1151 &sz) < 0) {
1152 err = errno;
1153 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001154 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001155 if (err == 0) {
1156 debug("channel %d: connected", c->self);
1157 c->type = SSH_CHANNEL_OPEN;
1158 if (compat20) {
1159 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1160 packet_put_int(c->remote_id);
1161 packet_put_int(c->self);
1162 packet_put_int(c->local_window);
1163 packet_put_int(c->local_maxpacket);
1164 } else {
1165 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1166 packet_put_int(c->remote_id);
1167 packet_put_int(c->self);
1168 }
1169 } else {
1170 debug("channel %d: not connected: %s",
1171 c->self, strerror(err));
1172 if (compat20) {
1173 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1174 packet_put_int(c->remote_id);
1175 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1176 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1177 packet_put_cstring(strerror(err));
1178 packet_put_cstring("");
1179 }
1180 } else {
1181 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1182 packet_put_int(c->remote_id);
1183 }
1184 chan_mark_dead(c);
1185 }
1186 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001187 }
1188}
1189
Damien Millerb38eff82000-04-01 11:09:21 +10001190int
1191channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
1192{
1193 char buf[16*1024];
1194 int len;
1195
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001196 if (c->rfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001197 FD_ISSET(c->rfd, readset)) {
1198 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +10001199 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1200 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001201 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +10001202 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001203 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001204 if (c->type != SSH_CHANNEL_OPEN) {
1205 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001206 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001207 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001208 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001209 buffer_consume(&c->output, buffer_len(&c->output));
1210 c->type = SSH_CHANNEL_INPUT_DRAINING;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001211 debug("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001212 } else {
1213 chan_read_failed(c);
1214 }
1215 return -1;
1216 }
Damien Millerad833b32000-08-23 10:46:23 +10001217 if(c->input_filter != NULL) {
1218 if (c->input_filter(c, buf, len) == -1) {
Ben Lindstromc486d882001-04-11 16:08:34 +00001219 debug("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001220 chan_read_failed(c);
1221 }
1222 } else {
1223 buffer_append(&c->input, buf, len);
1224 }
Damien Millerb38eff82000-04-01 11:09:21 +10001225 }
1226 return 1;
1227}
1228int
1229channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
1230{
Ben Lindstrome229b252001-03-05 06:28:06 +00001231 struct termios tio;
Damien Millerb38eff82000-04-01 11:09:21 +10001232 int len;
1233
1234 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001235 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001236 FD_ISSET(c->wfd, writeset) &&
1237 buffer_len(&c->output) > 0) {
1238 len = write(c->wfd, buffer_ptr(&c->output),
Damien Miller6f83b8e2000-05-02 09:23:45 +10001239 buffer_len(&c->output));
1240 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1241 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001242 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001243 if (c->type != SSH_CHANNEL_OPEN) {
1244 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001245 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001246 return -1;
1247 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001248 buffer_consume(&c->output, buffer_len(&c->output));
Ben Lindstrome9c99912001-06-09 00:41:05 +00001249 debug("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001250 c->type = SSH_CHANNEL_INPUT_DRAINING;
1251 } else {
1252 chan_write_failed(c);
1253 }
1254 return -1;
1255 }
Damien Miller79438cc2001-02-16 12:34:57 +11001256 if (compat20 && c->isatty) {
Damien Miller79438cc2001-02-16 12:34:57 +11001257 if (tcgetattr(c->wfd, &tio) == 0 &&
1258 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1259 /*
1260 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001261 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001262 * size of a SSH2_MSG_CHANNEL_DATA message
1263 * (4 byte channel id + data)
Damien Miller79438cc2001-02-16 12:34:57 +11001264 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001265 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001266 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001267 }
1268 }
Damien Millerb38eff82000-04-01 11:09:21 +10001269 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001270 if (compat20 && len > 0) {
1271 c->local_consumed += len;
1272 }
1273 }
1274 return 1;
1275}
1276int
1277channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
1278{
1279 char buf[16*1024];
1280 int len;
1281
Damien Miller1383bd82000-04-06 12:32:37 +10001282/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001283 if (c->efd != -1) {
1284 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1285 FD_ISSET(c->efd, writeset) &&
1286 buffer_len(&c->extended) > 0) {
1287 len = write(c->efd, buffer_ptr(&c->extended),
1288 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001289 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001290 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001291 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1292 return 1;
1293 if (len <= 0) {
1294 debug2("channel %d: closing write-efd %d",
1295 c->self, c->efd);
1296 close(c->efd);
1297 c->efd = -1;
1298 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001299 buffer_consume(&c->extended, len);
1300 c->local_consumed += len;
1301 }
1302 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1303 FD_ISSET(c->efd, readset)) {
1304 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001305 debug2("channel %d: read %d from efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001306 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001307 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1308 return 1;
1309 if (len <= 0) {
1310 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001311 c->self, c->efd);
1312 close(c->efd);
1313 c->efd = -1;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001314 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001315 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001316 }
Damien Miller33b13562000-04-04 14:38:59 +10001317 }
1318 }
1319 return 1;
1320}
1321int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001322channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001323{
Ben Lindstromb3921512001-04-11 15:57:50 +00001324 if (c->type == SSH_CHANNEL_OPEN &&
1325 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001326 c->local_window < c->local_window_max/2 &&
1327 c->local_consumed > 0) {
1328 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1329 packet_put_int(c->remote_id);
1330 packet_put_int(c->local_consumed);
1331 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001332 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001333 c->self, c->local_window,
1334 c->local_consumed);
1335 c->local_window += c->local_consumed;
1336 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001337 }
1338 return 1;
1339}
1340
1341void
1342channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
1343{
1344 channel_handle_rfd(c, readset, writeset);
1345 channel_handle_wfd(c, readset, writeset);
1346}
1347
1348void
Damien Miller33b13562000-04-04 14:38:59 +10001349channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
1350{
1351 channel_handle_rfd(c, readset, writeset);
1352 channel_handle_wfd(c, readset, writeset);
1353 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001354
1355 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001356}
1357
1358void
Damien Millerb38eff82000-04-01 11:09:21 +10001359channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1360{
1361 int len;
1362 /* Send buffered output data to the socket. */
1363 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1364 len = write(c->sock, buffer_ptr(&c->output),
1365 buffer_len(&c->output));
1366 if (len <= 0)
1367 buffer_consume(&c->output, buffer_len(&c->output));
1368 else
1369 buffer_consume(&c->output, len);
1370 }
1371}
1372
1373void
Damien Miller33b13562000-04-04 14:38:59 +10001374channel_handler_init_20(void)
1375{
1376 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +10001377 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001378 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001379 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001380 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001381 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001382 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001383 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001384
1385 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
1386 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001387 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001388 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001389 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001390 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001391 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_2;
Damien Miller33b13562000-04-04 14:38:59 +10001392}
1393
1394void
Damien Millerb38eff82000-04-01 11:09:21 +10001395channel_handler_init_13(void)
1396{
1397 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1398 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1399 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1400 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1401 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1402 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1403 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001404 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001405 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001406
1407 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
1408 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1409 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1410 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1411 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001412 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001413 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001414}
1415
1416void
1417channel_handler_init_15(void)
1418{
1419 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +10001420 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001421 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1422 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1423 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001424 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001425 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001426
1427 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1428 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1429 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1430 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001431 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001432 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001433}
1434
1435void
1436channel_handler_init(void)
1437{
1438 int i;
1439 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1440 channel_pre[i] = NULL;
1441 channel_post[i] = NULL;
1442 }
Damien Miller33b13562000-04-04 14:38:59 +10001443 if (compat20)
1444 channel_handler_init_20();
1445 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001446 channel_handler_init_13();
1447 else
1448 channel_handler_init_15();
1449}
1450
Damien Miller4af51302000-04-16 11:18:38 +10001451void
Damien Millerb38eff82000-04-01 11:09:21 +10001452channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1453{
1454 static int did_init = 0;
1455 int i;
1456 Channel *c;
1457
1458 if (!did_init) {
1459 channel_handler_init();
1460 did_init = 1;
1461 }
1462 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001463 c = channels[i];
1464 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001465 continue;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001466 if (ftab[c->type] != NULL)
1467 (*ftab[c->type])(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001468 if (chan_is_dead(c)) {
1469 /*
1470 * we have to remove the fd's from the select mask
1471 * before the channels are free'd and the fd's are
1472 * closed
1473 */
1474 if (c->wfd != -1)
1475 FD_CLR(c->wfd, writeset);
1476 if (c->rfd != -1)
1477 FD_CLR(c->rfd, readset);
1478 if (c->efd != -1) {
1479 if (c->extended_usage == CHAN_EXTENDED_READ)
1480 FD_CLR(c->efd, readset);
1481 if (c->extended_usage == CHAN_EXTENDED_WRITE)
1482 FD_CLR(c->efd, writeset);
1483 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001484 channel_free(c);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001485 }
Damien Millerb38eff82000-04-01 11:09:21 +10001486 }
1487}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001488
Ben Lindstrome9c99912001-06-09 00:41:05 +00001489/*
1490 * Allocate/update select bitmasks and add any bits relevant to channels in
1491 * select bitmasks.
1492 */
Damien Miller4af51302000-04-16 11:18:38 +10001493void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001494channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1495 int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001496{
Damien Miller5e953212001-01-30 09:14:00 +11001497 int n;
1498 u_int sz;
1499
1500 n = MAX(*maxfdp, channel_max_fd);
1501
1502 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1503 if (*readsetp == NULL || n > *maxfdp) {
1504 if (*readsetp)
1505 xfree(*readsetp);
1506 if (*writesetp)
1507 xfree(*writesetp);
1508 *readsetp = xmalloc(sz);
1509 *writesetp = xmalloc(sz);
1510 *maxfdp = n;
1511 }
1512 memset(*readsetp, 0, sz);
1513 memset(*writesetp, 0, sz);
1514
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001515 if (!rekeying)
1516 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001517}
1518
Ben Lindstrome9c99912001-06-09 00:41:05 +00001519/*
1520 * After select, perform any appropriate operations for channels which have
1521 * events pending.
1522 */
Damien Miller4af51302000-04-16 11:18:38 +10001523void
Damien Miller95def091999-11-25 00:26:21 +11001524channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001525{
Damien Millerb38eff82000-04-01 11:09:21 +10001526 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001527}
1528
Ben Lindstrome9c99912001-06-09 00:41:05 +00001529
Damien Miller5e953212001-01-30 09:14:00 +11001530/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001531
Damien Miller4af51302000-04-16 11:18:38 +10001532void
Damien Miller95def091999-11-25 00:26:21 +11001533channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001534{
Damien Miller95def091999-11-25 00:26:21 +11001535 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001536 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001537
Damien Miller95def091999-11-25 00:26:21 +11001538 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001539 c = channels[i];
1540 if (c == NULL)
1541 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001542
Ben Lindstrome9c99912001-06-09 00:41:05 +00001543 /*
1544 * We are only interested in channels that can have buffered
1545 * incoming data.
1546 */
Damien Miller34132e52000-01-14 15:45:46 +11001547 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001548 if (c->type != SSH_CHANNEL_OPEN &&
1549 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001550 continue;
1551 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001552 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001553 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001554 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001555 if (compat20 &&
1556 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001557 /* XXX is this true? */
Ben Lindstromb1131e92001-03-05 07:27:13 +00001558 debug2("channel %d: no data after CLOSE", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001559 continue;
1560 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001561
Damien Miller95def091999-11-25 00:26:21 +11001562 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001563 if ((c->istate == CHAN_INPUT_OPEN ||
1564 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1565 (len = buffer_len(&c->input)) > 0) {
Ben Lindstrome9c99912001-06-09 00:41:05 +00001566 /*
1567 * Send some data for the other side over the secure
1568 * connection.
1569 */
Damien Miller33b13562000-04-04 14:38:59 +10001570 if (compat20) {
1571 if (len > c->remote_window)
1572 len = c->remote_window;
1573 if (len > c->remote_maxpacket)
1574 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001575 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001576 if (packet_is_interactive()) {
1577 if (len > 1024)
1578 len = 512;
1579 } else {
1580 /* Keep the packets at reasonable size. */
1581 if (len > packet_get_maxsize()/2)
1582 len = packet_get_maxsize()/2;
1583 }
Damien Miller95def091999-11-25 00:26:21 +11001584 }
Damien Millerb38eff82000-04-01 11:09:21 +10001585 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001586 packet_start(compat20 ?
1587 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001588 packet_put_int(c->remote_id);
1589 packet_put_string(buffer_ptr(&c->input), len);
1590 packet_send();
1591 buffer_consume(&c->input, len);
1592 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001593 }
1594 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001595 if (compat13)
1596 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001597 /*
1598 * input-buffer is empty and read-socket shutdown:
1599 * tell peer, that we will not send more data: send IEOF
1600 */
Damien Millerb38eff82000-04-01 11:09:21 +10001601 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001602 }
Damien Miller33b13562000-04-04 14:38:59 +10001603 /* Send extended data, i.e. stderr */
1604 if (compat20 &&
1605 c->remote_window > 0 &&
1606 (len = buffer_len(&c->extended)) > 0 &&
1607 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001608 debug2("channel %d: rwin %d elen %d euse %d",
1609 c->self, c->remote_window, buffer_len(&c->extended),
1610 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001611 if (len > c->remote_window)
1612 len = c->remote_window;
1613 if (len > c->remote_maxpacket)
1614 len = c->remote_maxpacket;
1615 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1616 packet_put_int(c->remote_id);
1617 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1618 packet_put_string(buffer_ptr(&c->extended), len);
1619 packet_send();
1620 buffer_consume(&c->extended, len);
1621 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001622 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001623 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001624 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001625}
1626
Ben Lindstrome9c99912001-06-09 00:41:05 +00001627
1628/* -- protocol input */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001629
Damien Miller4af51302000-04-16 11:18:38 +10001630void
Damien Miller62cee002000-09-23 17:15:56 +11001631channel_input_data(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001632{
Damien Miller34132e52000-01-14 15:45:46 +11001633 int id;
Damien Miller95def091999-11-25 00:26:21 +11001634 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001635 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001636 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001637
Damien Miller95def091999-11-25 00:26:21 +11001638 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001639 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001640 c = channel_lookup(id);
1641 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001642 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001643
Damien Miller95def091999-11-25 00:26:21 +11001644 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001645 if (c->type != SSH_CHANNEL_OPEN &&
1646 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001647 return;
1648
1649 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001650 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001651 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001652
Damien Miller95def091999-11-25 00:26:21 +11001653 /* Get the data. */
1654 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001655 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001656
Damien Miller33b13562000-04-04 14:38:59 +10001657 if (compat20){
1658 if (data_len > c->local_maxpacket) {
1659 log("channel %d: rcvd big packet %d, maxpack %d",
1660 c->self, data_len, c->local_maxpacket);
1661 }
1662 if (data_len > c->local_window) {
1663 log("channel %d: rcvd too much data %d, win %d",
1664 c->self, data_len, c->local_window);
1665 xfree(data);
1666 return;
1667 }
1668 c->local_window -= data_len;
1669 }else{
1670 packet_integrity_check(plen, 4 + 4 + data_len, type);
1671 }
Damien Millerb38eff82000-04-01 11:09:21 +10001672 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001673 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001674}
Ben Lindstrome9c99912001-06-09 00:41:05 +00001675
Damien Miller4af51302000-04-16 11:18:38 +10001676void
Damien Miller62cee002000-09-23 17:15:56 +11001677channel_input_extended_data(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001678{
1679 int id;
1680 int tcode;
1681 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001682 u_int data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001683 Channel *c;
1684
1685 /* Get the channel number and verify it. */
1686 id = packet_get_int();
1687 c = channel_lookup(id);
1688
1689 if (c == NULL)
1690 packet_disconnect("Received extended_data for bad channel %d.", id);
1691 if (c->type != SSH_CHANNEL_OPEN) {
1692 log("channel %d: ext data for non open", id);
1693 return;
1694 }
1695 tcode = packet_get_int();
1696 if (c->efd == -1 ||
1697 c->extended_usage != CHAN_EXTENDED_WRITE ||
1698 tcode != SSH2_EXTENDED_DATA_STDERR) {
1699 log("channel %d: bad ext data", c->self);
1700 return;
1701 }
1702 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001703 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001704 if (data_len > c->local_window) {
1705 log("channel %d: rcvd too much extended_data %d, win %d",
1706 c->self, data_len, c->local_window);
1707 xfree(data);
1708 return;
1709 }
Damien Millerd3444942000-09-30 14:20:03 +11001710 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001711 c->local_window -= data_len;
1712 buffer_append(&c->extended, data, data_len);
1713 xfree(data);
1714}
1715
Damien Miller4af51302000-04-16 11:18:38 +10001716void
Damien Miller62cee002000-09-23 17:15:56 +11001717channel_input_ieof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001718{
1719 int id;
1720 Channel *c;
1721
1722 packet_integrity_check(plen, 4, type);
1723
1724 id = packet_get_int();
1725 c = channel_lookup(id);
1726 if (c == NULL)
1727 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1728 chan_rcvd_ieof(c);
1729}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001730
Damien Miller4af51302000-04-16 11:18:38 +10001731void
Damien Miller62cee002000-09-23 17:15:56 +11001732channel_input_close(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001733{
Damien Millerb38eff82000-04-01 11:09:21 +10001734 int id;
1735 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001736
Damien Millerb38eff82000-04-01 11:09:21 +10001737 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001738
Damien Millerb38eff82000-04-01 11:09:21 +10001739 id = packet_get_int();
1740 c = channel_lookup(id);
1741 if (c == NULL)
1742 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001743
1744 /*
1745 * Send a confirmation that we have closed the channel and no more
1746 * data is coming for it.
1747 */
Damien Miller95def091999-11-25 00:26:21 +11001748 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001749 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001750 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001751
Damien Miller5428f641999-11-25 11:54:57 +11001752 /*
1753 * If the channel is in closed state, we have sent a close request,
1754 * and the other side will eventually respond with a confirmation.
1755 * Thus, we cannot free the channel here, because then there would be
1756 * no-one to receive the confirmation. The channel gets freed when
1757 * the confirmation arrives.
1758 */
Damien Millerb38eff82000-04-01 11:09:21 +10001759 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001760 /*
1761 * Not a closed channel - mark it as draining, which will
1762 * cause it to be freed later.
1763 */
Damien Millerb38eff82000-04-01 11:09:21 +10001764 buffer_consume(&c->input, buffer_len(&c->input));
1765 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001766 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001767}
1768
Damien Millerb38eff82000-04-01 11:09:21 +10001769/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001770void
Damien Miller62cee002000-09-23 17:15:56 +11001771channel_input_oclose(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001772{
Damien Millerb38eff82000-04-01 11:09:21 +10001773 int id = packet_get_int();
1774 Channel *c = channel_lookup(id);
1775 packet_integrity_check(plen, 4, type);
1776 if (c == NULL)
1777 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1778 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001779}
1780
Damien Miller4af51302000-04-16 11:18:38 +10001781void
Damien Miller62cee002000-09-23 17:15:56 +11001782channel_input_close_confirmation(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001783{
1784 int id = packet_get_int();
1785 Channel *c = channel_lookup(id);
1786
Damien Miller4af51302000-04-16 11:18:38 +10001787 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001788 if (c == NULL)
1789 packet_disconnect("Received close confirmation for "
1790 "out-of-range channel %d.", id);
1791 if (c->type != SSH_CHANNEL_CLOSED)
1792 packet_disconnect("Received close confirmation for "
1793 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001794 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001795}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001796
Damien Miller4af51302000-04-16 11:18:38 +10001797void
Damien Miller62cee002000-09-23 17:15:56 +11001798channel_input_open_confirmation(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001799{
Damien Millerb38eff82000-04-01 11:09:21 +10001800 int id, remote_id;
1801 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001802
Damien Miller33b13562000-04-04 14:38:59 +10001803 if (!compat20)
1804 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001805
Damien Millerb38eff82000-04-01 11:09:21 +10001806 id = packet_get_int();
1807 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001808
Damien Millerb38eff82000-04-01 11:09:21 +10001809 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1810 packet_disconnect("Received open confirmation for "
1811 "non-opening channel %d.", id);
1812 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001813 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001814 c->remote_id = remote_id;
1815 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001816
1817 if (compat20) {
1818 c->remote_window = packet_get_int();
1819 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001820 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001821 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001822 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001823 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001824 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001825 }
1826 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1827 c->remote_window, c->remote_maxpacket);
1828 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001829}
1830
Ben Lindstrom69128662001-05-08 20:07:39 +00001831char *
1832reason2txt(int reason)
1833{
1834 switch(reason) {
1835 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
1836 return "administratively prohibited";
1837 case SSH2_OPEN_CONNECT_FAILED:
1838 return "connect failed";
1839 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
1840 return "unknown channel type";
1841 case SSH2_OPEN_RESOURCE_SHORTAGE:
1842 return "resource shortage";
1843 }
Ben Lindstrome2595442001-06-05 20:01:39 +00001844 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00001845}
1846
Damien Miller4af51302000-04-16 11:18:38 +10001847void
Damien Miller62cee002000-09-23 17:15:56 +11001848channel_input_open_failure(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001849{
Kevin Steves12057502001-02-05 14:54:34 +00001850 int id, reason;
1851 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001852 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001853
Damien Miller33b13562000-04-04 14:38:59 +10001854 if (!compat20)
1855 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001856
1857 id = packet_get_int();
1858 c = channel_lookup(id);
1859
1860 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1861 packet_disconnect("Received open failure for "
1862 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001863 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00001864 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00001865 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00001866 msg = packet_get_string(NULL);
1867 lang = packet_get_string(NULL);
1868 }
Damien Miller4af51302000-04-16 11:18:38 +10001869 packet_done();
Ben Lindstrom69128662001-05-08 20:07:39 +00001870 log("channel %d: open failed: %s%s%s", id,
1871 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00001872 if (msg != NULL)
1873 xfree(msg);
1874 if (lang != NULL)
1875 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001876 }
Damien Miller95def091999-11-25 00:26:21 +11001877 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001878 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001879}
1880
Damien Miller33b13562000-04-04 14:38:59 +10001881void
Damien Miller62cee002000-09-23 17:15:56 +11001882channel_input_channel_request(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001883{
1884 int id;
1885 Channel *c;
1886
1887 id = packet_get_int();
1888 c = channel_lookup(id);
1889
1890 if (c == NULL ||
1891 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1892 packet_disconnect("Received request for "
1893 "non-open channel %d.", id);
1894 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001895 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001896 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001897 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001898 } else {
1899 char *service = packet_get_string(NULL);
Ben Lindstromcc74df72001-03-05 06:20:14 +00001900 debug("channel %d: rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001901 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001902 xfree(service);
1903 }
1904}
1905
Damien Miller4af51302000-04-16 11:18:38 +10001906void
Damien Miller62cee002000-09-23 17:15:56 +11001907channel_input_window_adjust(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001908{
1909 Channel *c;
1910 int id, adjust;
1911
1912 if (!compat20)
1913 return;
1914
1915 /* Get the channel number and verify it. */
1916 id = packet_get_int();
1917 c = channel_lookup(id);
1918
1919 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1920 log("Received window adjust for "
1921 "non-open channel %d.", id);
1922 return;
1923 }
1924 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001925 packet_done();
Damien Millerd3444942000-09-30 14:20:03 +11001926 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10001927 c->remote_window += adjust;
1928}
1929
Damien Miller4af51302000-04-16 11:18:38 +10001930void
Ben Lindstrome9c99912001-06-09 00:41:05 +00001931channel_input_port_open(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001932{
Ben Lindstrome9c99912001-06-09 00:41:05 +00001933 Channel *c = NULL;
1934 u_short host_port;
1935 char *host, *originator_string;
1936 int remote_id, sock = -1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001937
Ben Lindstrome9c99912001-06-09 00:41:05 +00001938 remote_id = packet_get_int();
1939 host = packet_get_string(NULL);
1940 host_port = packet_get_int();
1941
1942 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
1943 originator_string = packet_get_string(NULL);
1944 } else {
1945 originator_string = xstrdup("unknown (remote did not supply name)");
1946 }
1947 packet_done();
1948 sock = channel_connect_to(host, host_port);
1949 if (sock != -1) {
1950 c = channel_new("connected socket",
1951 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
1952 originator_string, 1);
1953 if (c == NULL) {
1954 error("channel_input_port_open: channel_new failed");
1955 close(sock);
1956 } else {
1957 c->remote_id = remote_id;
Damien Miller95def091999-11-25 00:26:21 +11001958 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001959 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001960 if (c == NULL) {
1961 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1962 packet_put_int(remote_id);
1963 packet_send();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001964 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001965 xfree(host);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001966}
1967
1968
Ben Lindstrome9c99912001-06-09 00:41:05 +00001969/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001970
Damien Miller5428f641999-11-25 11:54:57 +11001971/*
1972 * Initiate forwarding of connections to local port "port" through the secure
1973 * channel to host:port from remote side.
1974 */
Kevin Steves12057502001-02-05 14:54:34 +00001975int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001976channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
1977 u_short port_to_connect, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001978{
Kevin Steves12057502001-02-05 14:54:34 +00001979 return channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +11001980 NULL, listen_port,
1981 host_to_connect, port_to_connect,
1982 gateway_ports, /*remote_fwd*/ 0);
1983}
1984
1985/*
1986 * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
1987 * (SSH_CHANNEL_RPORT_LISTENER).
1988 */
Kevin Steves12057502001-02-05 14:54:34 +00001989int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001990channel_request_forwarding(
1991 const char *listen_address, u_short listen_port,
1992 const char *host_to_connect, u_short port_to_connect,
1993 int gateway_ports, int remote_fwd)
1994{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001995 Channel *c;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001996 int success, sock, on = 1, type;
Damien Miller34132e52000-01-14 15:45:46 +11001997 struct addrinfo hints, *ai, *aitop;
1998 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0bc1bd82000-11-13 22:57:25 +11001999 const char *host;
Damien Miller5428f641999-11-25 11:54:57 +11002000 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002001
Kevin Steves12057502001-02-05 14:54:34 +00002002 success = 0;
2003
Damien Miller0bc1bd82000-11-13 22:57:25 +11002004 if (remote_fwd) {
2005 host = listen_address;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002006 type = SSH_CHANNEL_RPORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002007 } else {
2008 host = host_to_connect;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002009 type = SSH_CHANNEL_PORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002010 }
2011
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002012 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00002013 error("Forward host name too long.");
2014 return success;
2015 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002016
Damien Miller0bc1bd82000-11-13 22:57:25 +11002017 /* XXX listen_address is currently ignored */
Damien Miller5428f641999-11-25 11:54:57 +11002018 /*
Damien Miller34132e52000-01-14 15:45:46 +11002019 * getaddrinfo returns a loopback address if the hostname is
2020 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002021 */
Damien Miller34132e52000-01-14 15:45:46 +11002022 memset(&hints, 0, sizeof(hints));
2023 hints.ai_family = IPv4or6;
2024 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
2025 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002026 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002027 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
2028 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11002029
Damien Miller34132e52000-01-14 15:45:46 +11002030 for (ai = aitop; ai; ai = ai->ai_next) {
2031 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2032 continue;
2033 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2034 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11002035 error("channel_request_forwarding: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002036 continue;
2037 }
2038 /* Create a port to listen for the host. */
2039 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2040 if (sock < 0) {
2041 /* this is no error since kernel may not support ipv6 */
2042 verbose("socket: %.100s", strerror(errno));
2043 continue;
2044 }
2045 /*
2046 * Set socket options. We would like the socket to disappear
2047 * as soon as it has been closed for whatever reason.
2048 */
2049 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
2050 linger.l_onoff = 1;
2051 linger.l_linger = 5;
2052 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
2053 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002054
Damien Miller34132e52000-01-14 15:45:46 +11002055 /* Bind the socket to the address. */
2056 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2057 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002058 if (!ai->ai_next)
2059 error("bind: %.100s", strerror(errno));
2060 else
2061 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002062
Damien Miller34132e52000-01-14 15:45:46 +11002063 close(sock);
2064 continue;
2065 }
2066 /* Start listening for connections on the socket. */
2067 if (listen(sock, 5) < 0) {
2068 error("listen: %.100s", strerror(errno));
2069 close(sock);
2070 continue;
2071 }
2072 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002073 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002074 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002075 0, xstrdup("port listener"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002076 if (c == NULL) {
2077 error("channel_request_forwarding: channel_new failed");
2078 close(sock);
2079 continue;
2080 }
2081 strlcpy(c->path, host, sizeof(c->path));
2082 c->host_port = port_to_connect;
2083 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002084 success = 1;
2085 }
2086 if (success == 0)
Kevin Steves12057502001-02-05 14:54:34 +00002087 error("channel_request_forwarding: cannot listen to port: %d",
2088 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002089 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002090 return success;
Damien Miller95def091999-11-25 00:26:21 +11002091}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002092
Damien Miller5428f641999-11-25 11:54:57 +11002093/*
2094 * Initiate forwarding of connections to port "port" on remote host through
2095 * the secure channel to host:port from local side.
2096 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002097
Damien Miller4af51302000-04-16 11:18:38 +10002098void
Damien Miller0bc1bd82000-11-13 22:57:25 +11002099channel_request_remote_forwarding(u_short listen_port,
2100 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002101{
Damien Miller0bc1bd82000-11-13 22:57:25 +11002102 int payload_len, type, success = 0;
2103
Damien Miller95def091999-11-25 00:26:21 +11002104 /* Record locally that connection to this host/port is permitted. */
2105 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2106 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002107
Damien Miller95def091999-11-25 00:26:21 +11002108 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002109 if (compat20) {
2110 const char *address_to_bind = "0.0.0.0";
2111 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2112 packet_put_cstring("tcpip-forward");
2113 packet_put_char(0); /* boolean: want reply */
2114 packet_put_cstring(address_to_bind);
2115 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002116 packet_send();
2117 packet_write_wait();
2118 /* Assume that server accepts the request */
2119 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002120 } else {
2121 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002122 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002123 packet_put_cstring(host_to_connect);
2124 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002125 packet_send();
2126 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002127
2128 /* Wait for response from the remote side. */
2129 type = packet_read(&payload_len);
2130 switch (type) {
2131 case SSH_SMSG_SUCCESS:
2132 success = 1;
2133 break;
2134 case SSH_SMSG_FAILURE:
2135 log("Warning: Server denied remote port forwarding.");
2136 break;
2137 default:
2138 /* Unknown packet */
2139 packet_disconnect("Protocol error for port forward request:"
2140 "received packet type %d.", type);
2141 }
2142 }
2143 if (success) {
2144 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2145 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2146 permitted_opens[num_permitted_opens].listen_port = listen_port;
2147 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002148 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002149}
2150
Damien Miller5428f641999-11-25 11:54:57 +11002151/*
2152 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2153 * listening for the port, and sends back a success reply (or disconnect
2154 * message if there was an error). This never returns if there was an error.
2155 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002156
Damien Miller4af51302000-04-16 11:18:38 +10002157void
Damien Millere247cc42000-05-07 12:03:14 +10002158channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002159{
Damien Milleraae6c611999-12-06 11:47:28 +11002160 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11002161 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002162
Damien Miller95def091999-11-25 00:26:21 +11002163 /* Get arguments from the packet. */
2164 port = packet_get_int();
2165 hostname = packet_get_string(NULL);
2166 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002167
Damien Millerbac2d8a2000-09-05 16:13:06 +11002168#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002169 /*
2170 * Check that an unprivileged user is not trying to forward a
2171 * privileged port.
2172 */
Damien Miller95def091999-11-25 00:26:21 +11002173 if (port < IPPORT_RESERVED && !is_root)
2174 packet_disconnect("Requested forwarding of port %d but user is not root.",
2175 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11002176#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11002177 /* Initiate forwarding */
Damien Millere247cc42000-05-07 12:03:14 +10002178 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002179
2180 /* Free the argument string. */
2181 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002182}
2183
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002184/*
2185 * Permits opening to any host/port if permitted_opens[] is empty. This is
2186 * usually called by the server, because the user could connect to any port
2187 * anyway, and the server has no way to know but to trust the client anyway.
2188 */
2189void
2190channel_permit_all_opens()
2191{
2192 if (num_permitted_opens == 0)
2193 all_opens_permitted = 1;
2194}
2195
Ben Lindstroma3700052001-04-05 23:26:32 +00002196void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002197channel_add_permitted_opens(char *host, int port)
2198{
2199 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2200 fatal("channel_request_remote_forwarding: too many forwards");
2201 debug("allow port forwarding to host %s port %d", host, port);
2202
2203 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2204 permitted_opens[num_permitted_opens].port_to_connect = port;
2205 num_permitted_opens++;
2206
2207 all_opens_permitted = 0;
2208}
2209
Ben Lindstroma3700052001-04-05 23:26:32 +00002210void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002211channel_clear_permitted_opens(void)
2212{
2213 int i;
2214
2215 for (i = 0; i < num_permitted_opens; i++)
2216 xfree(permitted_opens[i].host_to_connect);
2217 num_permitted_opens = 0;
2218
2219}
2220
2221
2222/* return socket to remote host, port */
Damien Millerb38eff82000-04-01 11:09:21 +10002223int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002224connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002225{
Damien Miller34132e52000-01-14 15:45:46 +11002226 struct addrinfo hints, *ai, *aitop;
2227 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2228 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002229 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002230
Damien Miller34132e52000-01-14 15:45:46 +11002231 memset(&hints, 0, sizeof(hints));
2232 hints.ai_family = IPv4or6;
2233 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002234 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002235 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002236 error("connect_to %.100s: unknown host (%s)", host,
2237 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002238 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002239 }
Damien Miller34132e52000-01-14 15:45:46 +11002240 for (ai = aitop; ai; ai = ai->ai_next) {
2241 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2242 continue;
2243 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2244 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002245 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002246 continue;
2247 }
Damien Miller34132e52000-01-14 15:45:46 +11002248 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2249 if (sock < 0) {
2250 error("socket: %.100s", strerror(errno));
2251 continue;
2252 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +00002253 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002254 fatal("connect_to: F_SETFL: %s", strerror(errno));
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002255 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2256 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002257 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002258 strerror(errno));
2259 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002260 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002261 }
2262 break; /* success */
2263
2264 }
2265 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002266 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002267 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002268 return -1;
2269 }
2270 /* success */
2271 return sock;
2272}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002273
Damien Miller0bc1bd82000-11-13 22:57:25 +11002274int
2275channel_connect_by_listen_adress(u_short listen_port)
2276{
2277 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002278
Damien Miller0bc1bd82000-11-13 22:57:25 +11002279 for (i = 0; i < num_permitted_opens; i++)
2280 if (permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002281 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002282 permitted_opens[i].host_to_connect,
2283 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002284 error("WARNING: Server requests forwarding for unknown listen_port %d",
2285 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002286 return -1;
2287}
Damien Millerb38eff82000-04-01 11:09:21 +10002288
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002289/* Check if connecting to that port is permitted and connect. */
2290int
2291channel_connect_to(const char *host, u_short port)
2292{
2293 int i, permit;
2294
2295 permit = all_opens_permitted;
2296 if (!permit) {
2297 for (i = 0; i < num_permitted_opens; i++)
2298 if (permitted_opens[i].port_to_connect == port &&
2299 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2300 permit = 1;
2301
2302 }
2303 if (!permit) {
2304 log("Received request to connect to host %.100s port %d, "
2305 "but the request was denied.", host, port);
2306 return -1;
2307 }
2308 return connect_to(host, port);
2309}
2310
Ben Lindstrome9c99912001-06-09 00:41:05 +00002311/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002312
Damien Miller5428f641999-11-25 11:54:57 +11002313/*
2314 * Creates an internet domain socket for listening for X11 connections.
2315 * Returns a suitable value for the DISPLAY variable, or NULL if an error
2316 * occurs.
2317 */
Damien Miller95def091999-11-25 00:26:21 +11002318char *
Damien Millera34a28b1999-12-14 10:47:15 +11002319x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002320{
Damien Milleraae6c611999-12-06 11:47:28 +11002321 int display_number, sock;
2322 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002323 struct addrinfo hints, *ai, *aitop;
2324 char strport[NI_MAXSERV];
2325 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
2326 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11002327 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002328
Damien Millera34a28b1999-12-14 10:47:15 +11002329 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11002330 display_number < MAX_DISPLAYS;
2331 display_number++) {
2332 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002333 memset(&hints, 0, sizeof(hints));
2334 hints.ai_family = IPv4or6;
2335 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
2336 hints.ai_socktype = SOCK_STREAM;
2337 snprintf(strport, sizeof strport, "%d", port);
2338 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2339 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11002340 return NULL;
2341 }
Damien Miller34132e52000-01-14 15:45:46 +11002342 for (ai = aitop; ai; ai = ai->ai_next) {
2343 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2344 continue;
2345 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2346 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002347 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002348 error("socket: %.100s", strerror(errno));
2349 return NULL;
2350 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002351 debug("x11_create_display_inet: Socket family %d not supported",
2352 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002353 continue;
2354 }
Damien Miller34132e52000-01-14 15:45:46 +11002355 }
2356 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2357 debug("bind port %d: %.100s", port, strerror(errno));
2358 shutdown(sock, SHUT_RDWR);
2359 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002360
2361 if (ai->ai_next)
2362 continue;
2363
Damien Miller34132e52000-01-14 15:45:46 +11002364 for (n = 0; n < num_socks; n++) {
2365 shutdown(socks[n], SHUT_RDWR);
2366 close(socks[n]);
2367 }
2368 num_socks = 0;
2369 break;
2370 }
2371 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002372#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002373 if (num_socks == NUM_SOCKS)
2374 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002375#else
2376 break;
2377#endif
Damien Miller95def091999-11-25 00:26:21 +11002378 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002379 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002380 if (num_socks > 0)
2381 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002382 }
Damien Miller95def091999-11-25 00:26:21 +11002383 if (display_number >= MAX_DISPLAYS) {
2384 error("Failed to allocate internet-domain X11 display socket.");
2385 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002386 }
Damien Miller95def091999-11-25 00:26:21 +11002387 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002388 for (n = 0; n < num_socks; n++) {
2389 sock = socks[n];
2390 if (listen(sock, 5) < 0) {
2391 error("listen: %.100s", strerror(errno));
2392 shutdown(sock, SHUT_RDWR);
2393 close(sock);
2394 return NULL;
2395 }
Damien Miller95def091999-11-25 00:26:21 +11002396 }
Damien Miller34132e52000-01-14 15:45:46 +11002397
Damien Miller95def091999-11-25 00:26:21 +11002398 /* Set up a suitable value for the DISPLAY variable. */
2399 if (gethostname(hostname, sizeof(hostname)) < 0)
2400 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11002401
2402#ifdef IPADDR_IN_DISPLAY
Kevin Stevesef4eea92001-02-05 12:42:17 +00002403 /*
Damien Miller76112de1999-12-21 11:18:08 +11002404 * HPUX detects the local hostname in the DISPLAY variable and tries
2405 * to set up a shared memory connection to the server, which it
2406 * incorrectly supposes to be local.
2407 *
2408 * The workaround - as used in later $$H and other programs - is
2409 * is to set display to the host's IP address.
2410 */
2411 {
2412 struct hostent *he;
2413 struct in_addr my_addr;
2414
2415 he = gethostbyname(hostname);
2416 if (he == NULL) {
2417 error("[X11-broken-fwd-hostname-workaround] Could not get "
2418 "IP address for hostname %s.", hostname);
2419
2420 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
2421 "Could not get IP address for hostname %s.", hostname);
2422
2423 shutdown(sock, SHUT_RDWR);
2424 close(sock);
2425
2426 return NULL;
2427 }
2428
2429 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2430
2431 /* Set DISPLAY to <ip address>:screen.display */
Kevin Stevesef4eea92001-02-05 12:42:17 +00002432 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Kevin Stevesda6cd592000-12-29 18:41:21 +00002433 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002434 }
2435#else /* IPADDR_IN_DISPLAY */
2436 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11002437 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Kevin Stevesda6cd592000-12-29 18:41:21 +00002438 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002439#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002440
Damien Miller34132e52000-01-14 15:45:46 +11002441 /* Allocate a channel for each socket. */
2442 for (n = 0; n < num_socks; n++) {
2443 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10002444 (void) channel_new("x11 listener",
2445 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2446 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002447 0, xstrdup("X11 inet listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11002448 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002449
Damien Miller95def091999-11-25 00:26:21 +11002450 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11002451 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002452}
2453
2454#ifndef X_UNIX_PATH
2455#define X_UNIX_PATH "/tmp/.X11-unix/X"
2456#endif
2457
2458static
2459int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002460connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002461{
Damien Miller95def091999-11-25 00:26:21 +11002462 static const char *const x_sockets[] = {
2463 X_UNIX_PATH "%u",
2464 "/var/X/.X11-unix/X" "%u",
2465 "/usr/spool/sockets/X11/" "%u",
2466 NULL
2467 };
2468 int sock;
2469 struct sockaddr_un addr;
2470 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002471
Damien Miller95def091999-11-25 00:26:21 +11002472 for (path = x_sockets; *path; ++path) {
2473 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2474 if (sock < 0)
2475 error("socket: %.100s", strerror(errno));
2476 memset(&addr, 0, sizeof(addr));
2477 addr.sun_family = AF_UNIX;
2478 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
2479 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2480 return sock;
2481 close(sock);
2482 }
2483 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2484 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002485}
2486
Damien Millerbd483e72000-04-30 10:00:53 +10002487int
2488x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002489{
Damien Millerbd483e72000-04-30 10:00:53 +10002490 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002491 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002492 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002493 struct addrinfo hints, *ai, *aitop;
2494 char strport[NI_MAXSERV];
2495 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002496
Damien Miller95def091999-11-25 00:26:21 +11002497 /* Try to open a socket for the local X server. */
2498 display = getenv("DISPLAY");
2499 if (!display) {
2500 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002501 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002502 }
Damien Miller5428f641999-11-25 11:54:57 +11002503 /*
2504 * Now we decode the value of the DISPLAY variable and make a
2505 * connection to the real X server.
2506 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002507
Damien Miller5428f641999-11-25 11:54:57 +11002508 /*
2509 * Check if it is a unix domain socket. Unix domain displays are in
2510 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2511 */
Damien Miller95def091999-11-25 00:26:21 +11002512 if (strncmp(display, "unix:", 5) == 0 ||
2513 display[0] == ':') {
2514 /* Connect to the unix domain socket. */
2515 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2516 error("Could not parse display number from DISPLAY: %.100s",
2517 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002518 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002519 }
2520 /* Create a socket. */
2521 sock = connect_local_xsocket(display_number);
2522 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002523 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002524
2525 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002526 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002527 }
Damien Miller5428f641999-11-25 11:54:57 +11002528 /*
2529 * Connect to an inet socket. The DISPLAY value is supposedly
2530 * hostname:d[.s], where hostname may also be numeric IP address.
2531 */
Damien Miller95def091999-11-25 00:26:21 +11002532 strncpy(buf, display, sizeof(buf));
2533 buf[sizeof(buf) - 1] = 0;
2534 cp = strchr(buf, ':');
2535 if (!cp) {
2536 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002537 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002538 }
Damien Miller95def091999-11-25 00:26:21 +11002539 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002540 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002541 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2542 error("Could not parse display number from DISPLAY: %.100s",
2543 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002544 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002545 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002546
Damien Miller34132e52000-01-14 15:45:46 +11002547 /* Look up the host address */
2548 memset(&hints, 0, sizeof(hints));
2549 hints.ai_family = IPv4or6;
2550 hints.ai_socktype = SOCK_STREAM;
2551 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2552 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2553 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002554 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002555 }
Damien Miller34132e52000-01-14 15:45:46 +11002556 for (ai = aitop; ai; ai = ai->ai_next) {
2557 /* Create a socket. */
2558 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2559 if (sock < 0) {
2560 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002561 continue;
2562 }
2563 /* Connect it to the display. */
2564 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2565 debug("connect %.100s port %d: %.100s", buf,
2566 6000 + display_number, strerror(errno));
2567 close(sock);
2568 continue;
2569 }
2570 /* Success */
2571 break;
Damien Miller34132e52000-01-14 15:45:46 +11002572 }
Damien Miller34132e52000-01-14 15:45:46 +11002573 freeaddrinfo(aitop);
2574 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002575 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002576 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002577 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002578 }
Damien Millerbd483e72000-04-30 10:00:53 +10002579 return sock;
2580}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002581
Damien Millerbd483e72000-04-30 10:00:53 +10002582/*
2583 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2584 * the remote channel number. We should do whatever we want, and respond
2585 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2586 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002587
Damien Millerbd483e72000-04-30 10:00:53 +10002588void
Damien Miller62cee002000-09-23 17:15:56 +11002589x11_input_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002590{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002591 Channel *c = NULL;
2592 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002593 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10002594
2595 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002596
2597 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002598
2599 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002600 remote_host = packet_get_string(NULL);
2601 } else {
2602 remote_host = xstrdup("unknown (remote did not supply name)");
2603 }
2604 packet_done();
Damien Millerbd483e72000-04-30 10:00:53 +10002605
2606 /* Obtain a connection to the real X display. */
2607 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002608 if (sock != -1) {
2609 /* Allocate a channel for this connection. */
2610 c = channel_new("connected x11 socket",
2611 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
2612 remote_host, 1);
2613 if (c == NULL) {
2614 error("x11_input_open: channel_new failed");
2615 close(sock);
2616 } else {
2617 c->remote_id = remote_id;
2618 }
2619 }
2620 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10002621 /* Send refusal to the remote host. */
2622 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002623 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10002624 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10002625 /* Send a confirmation to the remote host. */
2626 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002627 packet_put_int(remote_id);
2628 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10002629 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002630 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002631}
2632
Damien Miller69b69aa2000-10-28 14:19:58 +11002633/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2634void
2635deny_input_open(int type, int plen, void *ctxt)
2636{
2637 int rchan = packet_get_int();
2638 switch(type){
2639 case SSH_SMSG_AGENT_OPEN:
2640 error("Warning: ssh server tried agent forwarding.");
2641 break;
2642 case SSH_SMSG_X11_OPEN:
2643 error("Warning: ssh server tried X11 forwarding.");
2644 break;
2645 default:
2646 error("deny_input_open: type %d plen %d", type, plen);
2647 break;
2648 }
2649 error("Warning: this is probably a break in attempt by a malicious server.");
2650 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2651 packet_put_int(rchan);
2652 packet_send();
2653}
2654
Damien Miller5428f641999-11-25 11:54:57 +11002655/*
2656 * Requests forwarding of X11 connections, generates fake authentication
2657 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00002658 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11002659 */
Damien Miller4af51302000-04-16 11:18:38 +10002660void
Damien Millerbd483e72000-04-30 10:00:53 +10002661x11_request_forwarding_with_spoofing(int client_session_id,
2662 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002663{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002664 u_int data_len = (u_int) strlen(data) / 2;
Ben Lindstromb3211a82001-02-10 22:33:19 +00002665 u_int i, value, len;
Damien Miller95def091999-11-25 00:26:21 +11002666 char *new_data;
2667 int screen_number;
2668 const char *cp;
2669 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002670
Damien Miller95def091999-11-25 00:26:21 +11002671 cp = getenv("DISPLAY");
2672 if (cp)
2673 cp = strchr(cp, ':');
2674 if (cp)
2675 cp = strchr(cp, '.');
2676 if (cp)
2677 screen_number = atoi(cp + 1);
2678 else
2679 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002680
Damien Miller95def091999-11-25 00:26:21 +11002681 /* Save protocol name. */
2682 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002683
Damien Miller5428f641999-11-25 11:54:57 +11002684 /*
2685 * Extract real authentication data and generate fake data of the
2686 * same length.
2687 */
Damien Miller95def091999-11-25 00:26:21 +11002688 x11_saved_data = xmalloc(data_len);
2689 x11_fake_data = xmalloc(data_len);
2690 for (i = 0; i < data_len; i++) {
2691 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2692 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2693 if (i % 4 == 0)
2694 rand = arc4random();
2695 x11_saved_data[i] = value;
2696 x11_fake_data[i] = rand & 0xff;
2697 rand >>= 8;
2698 }
2699 x11_saved_data_len = data_len;
2700 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002701
Damien Miller95def091999-11-25 00:26:21 +11002702 /* Convert the fake data into hex. */
Ben Lindstromb3211a82001-02-10 22:33:19 +00002703 len = 2 * data_len + 1;
2704 new_data = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +11002705 for (i = 0; i < data_len; i++)
Ben Lindstromb3211a82001-02-10 22:33:19 +00002706 snprintf(new_data + 2 * i, len - 2 * i,
2707 "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002708
Damien Miller95def091999-11-25 00:26:21 +11002709 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002710 if (compat20) {
2711 channel_request_start(client_session_id, "x11-req", 0);
2712 packet_put_char(0); /* XXX bool single connection */
2713 } else {
2714 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2715 }
2716 packet_put_cstring(proto);
2717 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002718 packet_put_int(screen_number);
2719 packet_send();
2720 packet_write_wait();
2721 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002722}
2723
Ben Lindstrome9c99912001-06-09 00:41:05 +00002724
2725/* -- agent forwarding */
2726
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002727/* Sends a message to the server to request authentication fd forwarding. */
2728
Damien Miller4af51302000-04-16 11:18:38 +10002729void
Damien Miller95def091999-11-25 00:26:21 +11002730auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002731{
Damien Miller95def091999-11-25 00:26:21 +11002732 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2733 packet_send();
2734 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002735}
2736
Damien Miller5428f641999-11-25 11:54:57 +11002737/*
2738 * Returns the name of the forwarded authentication socket. Returns NULL if
2739 * there is no forwarded authentication socket. The returned value points to
2740 * a static buffer.
2741 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002742
Damien Miller95def091999-11-25 00:26:21 +11002743char *
2744auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002745{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002746 return auth_sock_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002747}
2748
2749/* removes the agent forwarding socket */
2750
Damien Miller4af51302000-04-16 11:18:38 +10002751void
Ben Lindstrom983c0982001-06-09 01:20:06 +00002752auth_sock_cleanup_proc(void *_pw)
Damien Miller95def091999-11-25 00:26:21 +11002753{
Ben Lindstrom983c0982001-06-09 01:20:06 +00002754 struct passwd *pw = _pw;
2755
Ben Lindstrom838394c2001-06-09 01:11:59 +00002756 if (auth_sock_name) {
Ben Lindstrom983c0982001-06-09 01:20:06 +00002757 temporarily_use_uid(pw);
Ben Lindstrom838394c2001-06-09 01:11:59 +00002758 unlink(auth_sock_name);
2759 rmdir(auth_sock_dir);
2760 auth_sock_name = NULL;
Ben Lindstrom983c0982001-06-09 01:20:06 +00002761 restore_uid();
Ben Lindstrom838394c2001-06-09 01:11:59 +00002762 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002763}
2764
Damien Miller5428f641999-11-25 11:54:57 +11002765/*
Damien Millerd3a18572000-06-07 19:55:44 +10002766 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002767 * This starts forwarding authentication requests.
2768 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002769
Damien Millerd3a18572000-06-07 19:55:44 +10002770int
Damien Miller95def091999-11-25 00:26:21 +11002771auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002772{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002773 Channel *nc;
2774 int sock;
Damien Miller95def091999-11-25 00:26:21 +11002775 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002776
Ben Lindstrome9c99912001-06-09 00:41:05 +00002777 if (auth_get_socket_name() != NULL) {
2778 error("authentication forwarding requested twice.");
2779 return 0;
2780 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002781
Damien Miller95def091999-11-25 00:26:21 +11002782 /* Temporarily drop privileged uid for mkdir/bind. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00002783 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002784
Damien Miller95def091999-11-25 00:26:21 +11002785 /* Allocate a buffer for the socket name, and format the name. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002786 auth_sock_name = xmalloc(MAXPATHLEN);
2787 auth_sock_dir = xmalloc(MAXPATHLEN);
2788 strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002789
Damien Miller95def091999-11-25 00:26:21 +11002790 /* Create private directory for socket */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002791 if (mkdtemp(auth_sock_dir) == NULL) {
2792 packet_send_debug("Agent forwarding disabled: "
2793 "mkdtemp() failed: %.100s", strerror(errno));
Damien Millerd3a18572000-06-07 19:55:44 +10002794 restore_uid();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002795 xfree(auth_sock_name);
2796 xfree(auth_sock_dir);
2797 auth_sock_name = NULL;
2798 auth_sock_dir = NULL;
Damien Millerd3a18572000-06-07 19:55:44 +10002799 return 0;
2800 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002801 snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%d",
2802 auth_sock_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002803
Ben Lindstrom838394c2001-06-09 01:11:59 +00002804 /* delete agent socket on fatal() */
Ben Lindstrom983c0982001-06-09 01:20:06 +00002805 fatal_add_cleanup(auth_sock_cleanup_proc, pw);
Ben Lindstrom838394c2001-06-09 01:11:59 +00002806
Damien Miller95def091999-11-25 00:26:21 +11002807 /* Create the socket. */
2808 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2809 if (sock < 0)
2810 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002811
Damien Miller95def091999-11-25 00:26:21 +11002812 /* Bind it to the name. */
2813 memset(&sunaddr, 0, sizeof(sunaddr));
2814 sunaddr.sun_family = AF_UNIX;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002815 strncpy(sunaddr.sun_path, auth_sock_name,
Damien Miller95def091999-11-25 00:26:21 +11002816 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002817
Damien Miller95def091999-11-25 00:26:21 +11002818 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2819 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002820
Damien Miller95def091999-11-25 00:26:21 +11002821 /* Restore the privileged uid. */
2822 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002823
Damien Miller95def091999-11-25 00:26:21 +11002824 /* Start listening on the socket. */
2825 if (listen(sock, 5) < 0)
2826 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002827
Damien Miller95def091999-11-25 00:26:21 +11002828 /* Allocate a channel for the authentication agent socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002829 nc = channel_new("auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11002830 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2831 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2832 0, xstrdup("auth socket"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002833 if (nc == NULL) {
2834 error("auth_input_request_forwarding: channel_new failed");
Ben Lindstrom983c0982001-06-09 01:20:06 +00002835 auth_sock_cleanup_proc(pw);
Ben Lindstromdf4981b2001-06-09 01:32:29 +00002836 fatal_remove_cleanup(auth_sock_cleanup_proc, pw);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002837 close(sock);
2838 return 0;
2839 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002840 strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
Damien Millerd3a18572000-06-07 19:55:44 +10002841 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002842}
2843
2844/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2845
Damien Miller4af51302000-04-16 11:18:38 +10002846void
Damien Miller62cee002000-09-23 17:15:56 +11002847auth_input_open_request(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002848{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002849 Channel *c = NULL;
2850 int remote_id, sock;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002851 char *name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002852
Damien Millerb38eff82000-04-01 11:09:21 +10002853 packet_integrity_check(plen, 4, type);
2854
Damien Miller95def091999-11-25 00:26:21 +11002855 /* Read the remote channel number from the message. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002856 remote_id = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002857
Damien Miller5428f641999-11-25 11:54:57 +11002858 /*
2859 * Get a connection to the local authentication agent (this may again
2860 * get forwarded).
2861 */
Damien Miller95def091999-11-25 00:26:21 +11002862 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002863
Damien Miller5428f641999-11-25 11:54:57 +11002864 /*
2865 * If we could not connect the agent, send an error message back to
2866 * the server. This should never happen unless the agent dies,
2867 * because authentication forwarding is only enabled if we have an
2868 * agent.
2869 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002870 if (sock >= 0) {
Ben Lindstrome9c99912001-06-09 00:41:05 +00002871 name = xstrdup("authentication agent connection");
2872 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
2873 -1, 0, 0, 0, name, 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002874 if (c == NULL) {
2875 error("auth_input_open_request: channel_new failed");
Ben Lindstrome9c99912001-06-09 00:41:05 +00002876 xfree(name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002877 close(sock);
2878 } else {
2879 c->remote_id = remote_id;
2880 }
Damien Miller95def091999-11-25 00:26:21 +11002881 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002882 if (c == NULL) {
2883 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2884 packet_put_int(remote_id);
2885 } else {
2886 /* Send a confirmation to the remote host. */
2887 debug("Forwarding authentication connection.");
2888 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2889 packet_put_int(remote_id);
2890 packet_put_int(c->self);
2891 }
Damien Miller95def091999-11-25 00:26:21 +11002892 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002893}