blob: d5526fb8374ac9d10cef91d31113862768a4cf19 [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 Lindstrom3fcf1a22001-04-08 18:26:59 +000043RCSID("$OpenBSD: channels.c,v 1.102 2001/04/06 21:00:10 markus Exp $");
Ben Lindstrom226cfa02001-01-22 05:34:40 +000044
45#include <openssl/rsa.h>
46#include <openssl/dsa.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047
48#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000049#include "ssh1.h"
50#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051#include "packet.h"
52#include "xmalloc.h"
53#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "log.h"
56#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057#include "channels.h"
58#include "nchan.h"
59#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000060#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100061#include "key.h"
62#include "authfd.h"
63
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064/* Maximum number of fake X11 displays to try. */
65#define MAX_DISPLAYS 1000
66
67/* Max len of agent socket */
68#define MAX_SOCKET_NAME 100
69
Damien Miller5428f641999-11-25 11:54:57 +110070/*
71 * Pointer to an array containing all allocated channels. The array is
72 * dynamically extended as needed.
73 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074static Channel *channels = NULL;
75
Damien Miller5428f641999-11-25 11:54:57 +110076/*
77 * Size of the channel array. All slots of the array must always be
78 * initialized (at least the type field); unused slots are marked with type
79 * SSH_CHANNEL_FREE.
80 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081static int channels_alloc = 0;
82
Damien Miller5428f641999-11-25 11:54:57 +110083/*
84 * Maximum file descriptor value used in any of the channels. This is
85 * updated in channel_allocate.
86 */
Damien Miller5e953212001-01-30 09:14:00 +110087static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
89/* Name and directory of socket for authentication agent forwarding. */
90static char *channel_forwarded_auth_socket_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +110091static char *channel_forwarded_auth_socket_dir = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
93/* Saved X11 authentication protocol name. */
94char *x11_saved_proto = NULL;
95
96/* Saved X11 authentication data. This is the real data. */
97char *x11_saved_data = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +000098u_int x11_saved_data_len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * Fake X11 authentication data. This is what the server will be sending us;
102 * we should replace any occurrences of this by the real data.
103 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104char *x11_fake_data = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000105u_int x11_fake_data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Miller5428f641999-11-25 11:54:57 +1100107/*
108 * Data structure for storing which hosts are permitted for forward requests.
109 * The local sides of any remote forwards are stored in this array to prevent
110 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
111 * network (which might be behind a firewall).
112 */
Damien Miller95def091999-11-25 00:26:21 +1100113typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000114 char *host_to_connect; /* Connect to 'host'. */
115 u_short port_to_connect; /* Connect to 'port'. */
116 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117} ForwardPermission;
118
119/* List of all permitted host/port pairs to connect. */
120static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
121/* Number of permitted host/port pairs in the array. */
122static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +1100123/*
124 * If this is true, all opens are permitted. This is the case on the server
125 * on which we have to trust the client anyway, and the user could do
126 * anything after logging in anyway.
127 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128static int all_opens_permitted = 0;
129
130/* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */
131static int have_hostname_in_open = 0;
132
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000133/* AF_UNSPEC or AF_INET or AF_INET6 */
134extern int IPv4or6;
135
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136/* Sets specific protocol options. */
137
Damien Miller4af51302000-04-16 11:18:38 +1000138void
Damien Miller95def091999-11-25 00:26:21 +1100139channel_set_options(int hostname_in_open)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140{
Damien Miller95def091999-11-25 00:26:21 +1100141 have_hostname_in_open = hostname_in_open;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142}
143
Damien Millerb38eff82000-04-01 11:09:21 +1000144/* lookup channel by id */
145
146Channel *
147channel_lookup(int id)
148{
149 Channel *c;
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 }
154 c = &channels[id];
155 if (c->type == SSH_CHANNEL_FREE) {
156 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
212int
213channel_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;
223 channels = xmalloc(channels_alloc * sizeof(Channel));
224 for (i = 0; i < channels_alloc; i++)
225 channels[i].type = SSH_CHANNEL_FREE;
Damien Miller5428f641999-11-25 11:54:57 +1100226 /*
227 * Kludge: arrange a call to channel_stop_listening if we
228 * terminate with fatal().
229 */
Damien Miller95def091999-11-25 00:26:21 +1100230 fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL);
231 }
232 /* Try to find a free slot where to put the new channel. */
233 for (found = -1, i = 0; i < channels_alloc; i++)
234 if (channels[i].type == SSH_CHANNEL_FREE) {
235 /* Found a free slot. */
236 found = i;
237 break;
238 }
239 if (found == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100240 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100241 found = channels_alloc;
242 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100243 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100244 channels = xrealloc(channels, channels_alloc * sizeof(Channel));
245 for (i = found; i < channels_alloc; i++)
246 channels[i].type = SSH_CHANNEL_FREE;
247 }
248 /* Initialize and return new channel number. */
249 c = &channels[found];
250 buffer_init(&c->input);
251 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000252 buffer_init(&c->extended);
Damien Miller95def091999-11-25 00:26:21 +1100253 chan_init_iostates(c);
Damien Miller69b69aa2000-10-28 14:19:58 +1100254 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100255 c->self = found;
256 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000257 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000258 c->local_window = window;
259 c->local_window_max = window;
260 c->local_consumed = 0;
261 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100262 c->remote_id = -1;
263 c->remote_name = remote_name;
Damien Millerb38eff82000-04-01 11:09:21 +1000264 c->remote_window = 0;
265 c->remote_maxpacket = 0;
266 c->cb_fn = NULL;
267 c->cb_arg = NULL;
268 c->cb_event = 0;
269 c->dettach_user = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000270 c->input_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100271 debug("channel %d: new [%s]", found, remote_name);
272 return found;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000274/* old interface XXX */
Damien Miller4af51302000-04-16 11:18:38 +1000275int
Damien Millerb38eff82000-04-01 11:09:21 +1000276channel_allocate(int type, int sock, char *remote_name)
277{
Damien Miller69b69aa2000-10-28 14:19:58 +1100278 return channel_new("", type, sock, sock, -1, 0, 0, 0, remote_name, 1);
Damien Millerb38eff82000-04-01 11:09:21 +1000279}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280
Damien Miller6f83b8e2000-05-02 09:23:45 +1000281
282/* Close all channel fd/socket. */
283
284void
285channel_close_fds(Channel *c)
286{
287 if (c->sock != -1) {
288 close(c->sock);
289 c->sock = -1;
290 }
291 if (c->rfd != -1) {
292 close(c->rfd);
293 c->rfd = -1;
294 }
295 if (c->wfd != -1) {
296 close(c->wfd);
297 c->wfd = -1;
298 }
299 if (c->efd != -1) {
300 close(c->efd);
301 c->efd = -1;
302 }
303}
304
305/* Free the channel and close its fd/socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000306
Damien Miller4af51302000-04-16 11:18:38 +1000307void
Damien Millerb38eff82000-04-01 11:09:21 +1000308channel_free(int id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309{
Damien Millerb38eff82000-04-01 11:09:21 +1000310 Channel *c = channel_lookup(id);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000311 char *s = channel_open_message();
312
Damien Millerb38eff82000-04-01 11:09:21 +1000313 if (c == NULL)
314 packet_disconnect("channel free: bad local channel %d", id);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000315 debug("channel_free: channel %d: status: %s", id, s);
316 xfree(s);
317
Damien Miller33b13562000-04-04 14:38:59 +1000318 if (c->dettach_user != NULL) {
319 debug("channel_free: channel %d: dettaching channel user", id);
320 c->dettach_user(c->self, NULL);
321 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000322 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000323 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000324 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000325 buffer_free(&c->input);
326 buffer_free(&c->output);
327 buffer_free(&c->extended);
328 c->type = SSH_CHANNEL_FREE;
329 if (c->remote_name) {
330 xfree(c->remote_name);
331 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100332 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000333}
334
Damien Miller5428f641999-11-25 11:54:57 +1100335/*
Damien Millerb38eff82000-04-01 11:09:21 +1000336 * 'channel_pre*' are called just before select() to add any bits relevant to
337 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100338 */
Damien Millerb38eff82000-04-01 11:09:21 +1000339/*
340 * 'channel_post*': perform any appropriate operations for channels which
341 * have events pending.
342 */
343typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
344chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
345chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
346
347void
348channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
349{
350 FD_SET(c->sock, readset);
351}
352
353void
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000354channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
355{
356 debug3("channel %d: waiting for connection", c->self);
357 FD_SET(c->sock, writeset);
358}
359
360void
Damien Millerb38eff82000-04-01 11:09:21 +1000361channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
362{
363 if (buffer_len(&c->input) < packet_get_maxsize())
364 FD_SET(c->sock, readset);
365 if (buffer_len(&c->output) > 0)
366 FD_SET(c->sock, writeset);
367}
368
369void
370channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
371{
372 /* test whether sockets are 'alive' for read/write */
373 if (c->istate == CHAN_INPUT_OPEN)
374 if (buffer_len(&c->input) < packet_get_maxsize())
375 FD_SET(c->sock, readset);
376 if (c->ostate == CHAN_OUTPUT_OPEN ||
377 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
378 if (buffer_len(&c->output) > 0) {
379 FD_SET(c->sock, writeset);
380 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
381 chan_obuf_empty(c);
382 }
383 }
384}
385
386void
Damien Miller33b13562000-04-04 14:38:59 +1000387channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
388{
389 if (c->istate == CHAN_INPUT_OPEN &&
390 c->remote_window > 0 &&
391 buffer_len(&c->input) < c->remote_window)
392 FD_SET(c->rfd, readset);
393 if (c->ostate == CHAN_OUTPUT_OPEN ||
394 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
395 if (buffer_len(&c->output) > 0) {
396 FD_SET(c->wfd, writeset);
397 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
398 chan_obuf_empty(c);
399 }
400 }
401 /** XXX check close conditions, too */
402 if (c->efd != -1) {
403 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
404 buffer_len(&c->extended) > 0)
405 FD_SET(c->efd, writeset);
406 else if (c->extended_usage == CHAN_EXTENDED_READ &&
407 buffer_len(&c->extended) < c->remote_window)
408 FD_SET(c->efd, readset);
409 }
410}
411
412void
Damien Millerb38eff82000-04-01 11:09:21 +1000413channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
414{
415 if (buffer_len(&c->input) == 0) {
416 packet_start(SSH_MSG_CHANNEL_CLOSE);
417 packet_put_int(c->remote_id);
418 packet_send();
419 c->type = SSH_CHANNEL_CLOSED;
420 debug("Closing channel %d after input drain.", c->self);
421 }
422}
423
424void
425channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
426{
427 if (buffer_len(&c->output) == 0)
428 channel_free(c->self);
Damien Miller4af51302000-04-16 11:18:38 +1000429 else
Damien Millerb38eff82000-04-01 11:09:21 +1000430 FD_SET(c->sock, writeset);
431}
432
433/*
434 * This is a special state for X11 authentication spoofing. An opened X11
435 * connection (when authentication spoofing is being done) remains in this
436 * state until the first packet has been completely read. The authentication
437 * data in that packet is then substituted by the real data if it matches the
438 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000439 * XXX All this happens at the client side.
Damien Millerb38eff82000-04-01 11:09:21 +1000440 */
441int
442x11_open_helper(Channel *c)
443{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000444 u_char *ucp;
445 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000446
447 /* Check if the fixed size part of the packet is in buffer. */
448 if (buffer_len(&c->output) < 12)
449 return 0;
450
451 /* Parse the lengths of variable-length fields. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000452 ucp = (u_char *) buffer_ptr(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000453 if (ucp[0] == 0x42) { /* Byte order MSB first. */
454 proto_len = 256 * ucp[6] + ucp[7];
455 data_len = 256 * ucp[8] + ucp[9];
456 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
457 proto_len = ucp[6] + 256 * ucp[7];
458 data_len = ucp[8] + 256 * ucp[9];
459 } else {
460 debug("Initial X11 packet contains bad byte order byte: 0x%x",
461 ucp[0]);
462 return -1;
463 }
464
465 /* Check if the whole packet is in buffer. */
466 if (buffer_len(&c->output) <
467 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
468 return 0;
469
470 /* Check if authentication protocol matches. */
471 if (proto_len != strlen(x11_saved_proto) ||
472 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
473 debug("X11 connection uses different authentication protocol.");
474 return -1;
475 }
476 /* Check if authentication data matches our fake data. */
477 if (data_len != x11_fake_data_len ||
478 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
479 x11_fake_data, x11_fake_data_len) != 0) {
480 debug("X11 auth data does not match fake data.");
481 return -1;
482 }
483 /* Check fake data length */
484 if (x11_fake_data_len != x11_saved_data_len) {
485 error("X11 fake_data_len %d != saved_data_len %d",
486 x11_fake_data_len, x11_saved_data_len);
487 return -1;
488 }
489 /*
490 * Received authentication protocol and data match
491 * our fake data. Substitute the fake data with real
492 * data.
493 */
494 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
495 x11_saved_data, x11_saved_data_len);
496 return 1;
497}
498
499void
500channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
501{
502 int ret = x11_open_helper(c);
503 if (ret == 1) {
504 /* Start normal processing for the channel. */
505 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000506 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000507 } else if (ret == -1) {
508 /*
509 * We have received an X11 connection that has bad
510 * authentication information.
511 */
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000512 log("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000513 buffer_clear(&c->input);
514 buffer_clear(&c->output);
515 close(c->sock);
516 c->sock = -1;
517 c->type = SSH_CHANNEL_CLOSED;
518 packet_start(SSH_MSG_CHANNEL_CLOSE);
519 packet_put_int(c->remote_id);
520 packet_send();
521 }
522}
523
524void
Damien Millerbd483e72000-04-30 10:00:53 +1000525channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000526{
527 int ret = x11_open_helper(c);
528 if (ret == 1) {
529 c->type = SSH_CHANNEL_OPEN;
Damien Miller30c3d422000-05-09 11:02:59 +1000530 if (compat20)
531 channel_pre_open_20(c, readset, writeset);
532 else
533 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000534 } else if (ret == -1) {
535 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerbd483e72000-04-30 10:00:53 +1000536 chan_read_failed(c); /** force close? */
Damien Millerb38eff82000-04-01 11:09:21 +1000537 chan_write_failed(c);
538 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
539 }
540}
541
542/* This is our fake X11 server socket. */
543void
544channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
545{
546 struct sockaddr addr;
547 int newsock, newch;
548 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +1100549 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +1000550 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +1000551
552 if (FD_ISSET(c->sock, readset)) {
553 debug("X11 connection requested.");
554 addrlen = sizeof(addr);
555 newsock = accept(c->sock, &addr, &addrlen);
556 if (newsock < 0) {
557 error("accept: %.100s", strerror(errno));
558 return;
559 }
Damien Millerd83ff352001-01-30 09:19:34 +1100560 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +1000561 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +1000562 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +1100563 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +1000564
565 newch = channel_new("x11",
566 SSH_CHANNEL_OPENING, newsock, newsock, -1,
567 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100568 0, xstrdup(buf), 1);
Damien Millerbd483e72000-04-30 10:00:53 +1000569 if (compat20) {
570 packet_start(SSH2_MSG_CHANNEL_OPEN);
571 packet_put_cstring("x11");
572 packet_put_int(newch);
573 packet_put_int(c->local_window_max);
574 packet_put_int(c->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +1100575 /* originator ipaddr and port */
576 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +1000577 if (datafellows & SSH_BUG_X11FWD) {
578 debug("ssh2 x11 bug compat mode");
579 } else {
580 packet_put_int(remote_port);
581 }
Damien Millerbd483e72000-04-30 10:00:53 +1000582 packet_send();
583 } else {
584 packet_start(SSH_SMSG_X11_OPEN);
585 packet_put_int(newch);
586 if (have_hostname_in_open)
587 packet_put_string(buf, strlen(buf));
588 packet_send();
589 }
Damien Millerd83ff352001-01-30 09:19:34 +1100590 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +1000591 }
592}
593
594/*
595 * This socket is listening for connections to a forwarded TCP/IP port.
596 */
597void
598channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
599{
600 struct sockaddr addr;
601 int newsock, newch;
602 socklen_t addrlen;
Damien Miller33804262001-02-04 23:20:18 +1100603 char buf[1024], *remote_ipaddr, *rtype;
Damien Millerb38eff82000-04-01 11:09:21 +1000604 int remote_port;
605
Damien Miller0bc1bd82000-11-13 22:57:25 +1100606 rtype = (c->type == SSH_CHANNEL_RPORT_LISTENER) ?
607 "forwarded-tcpip" : "direct-tcpip";
608
Damien Millerb38eff82000-04-01 11:09:21 +1000609 if (FD_ISSET(c->sock, readset)) {
610 debug("Connection to port %d forwarding "
611 "to %.100s port %d requested.",
612 c->listening_port, c->path, c->host_port);
613 addrlen = sizeof(addr);
614 newsock = accept(c->sock, &addr, &addrlen);
615 if (newsock < 0) {
616 error("accept: %.100s", strerror(errno));
617 return;
618 }
Damien Miller33804262001-02-04 23:20:18 +1100619 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +1000620 remote_port = get_peer_port(newsock);
621 snprintf(buf, sizeof buf,
622 "listen port %d for %.100s port %d, "
623 "connect from %.200s port %d",
624 c->listening_port, c->path, c->host_port,
Damien Miller33804262001-02-04 23:20:18 +1100625 remote_ipaddr, remote_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100626
627 newch = channel_new(rtype,
Damien Millerb38eff82000-04-01 11:09:21 +1000628 SSH_CHANNEL_OPENING, newsock, newsock, -1,
629 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100630 0, xstrdup(buf), 1);
Damien Miller33b13562000-04-04 14:38:59 +1000631 if (compat20) {
632 packet_start(SSH2_MSG_CHANNEL_OPEN);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100633 packet_put_cstring(rtype);
Damien Miller33b13562000-04-04 14:38:59 +1000634 packet_put_int(newch);
635 packet_put_int(c->local_window_max);
636 packet_put_int(c->local_maxpacket);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100637 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
638 /* listen address, port */
639 packet_put_string(c->path, strlen(c->path));
640 packet_put_int(c->listening_port);
641 } else {
642 /* target host, port */
643 packet_put_string(c->path, strlen(c->path));
644 packet_put_int(c->host_port);
645 }
Damien Miller4af51302000-04-16 11:18:38 +1000646 /* originator host and port */
Damien Miller33804262001-02-04 23:20:18 +1100647 packet_put_cstring(remote_ipaddr);
Damien Miller33b13562000-04-04 14:38:59 +1000648 packet_put_int(remote_port);
649 packet_send();
650 } else {
651 packet_start(SSH_MSG_PORT_OPEN);
652 packet_put_int(newch);
653 packet_put_string(c->path, strlen(c->path));
654 packet_put_int(c->host_port);
655 if (have_hostname_in_open) {
656 packet_put_string(buf, strlen(buf));
657 }
658 packet_send();
Damien Millerb38eff82000-04-01 11:09:21 +1000659 }
Damien Miller33804262001-02-04 23:20:18 +1100660 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +1000661 }
662}
663
664/*
665 * This is the authentication agent socket listening for connections from
666 * clients.
667 */
668void
669channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
670{
671 struct sockaddr addr;
672 int newsock, newch;
673 socklen_t addrlen;
674
675 if (FD_ISSET(c->sock, readset)) {
676 addrlen = sizeof(addr);
677 newsock = accept(c->sock, &addr, &addrlen);
678 if (newsock < 0) {
679 error("accept from auth socket: %.100s", strerror(errno));
680 return;
681 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100682 newch = channel_new("accepted auth socket",
683 SSH_CHANNEL_OPENING, newsock, newsock, -1,
684 c->local_window_max, c->local_maxpacket,
685 0, xstrdup("accepted auth socket"), 1);
686 if (compat20) {
687 packet_start(SSH2_MSG_CHANNEL_OPEN);
688 packet_put_cstring("auth-agent@openssh.com");
689 packet_put_int(newch);
690 packet_put_int(c->local_window_max);
691 packet_put_int(c->local_maxpacket);
692 } else {
693 packet_start(SSH_SMSG_AGENT_OPEN);
694 packet_put_int(newch);
695 }
Damien Millerb38eff82000-04-01 11:09:21 +1000696 packet_send();
697 }
698}
699
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000700void
701channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
702{
703 if (FD_ISSET(c->sock, writeset)) {
704 int err = 0;
705 int sz = sizeof(err);
706 c->type = SSH_CHANNEL_OPEN;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000707 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err, &sz) < 0) {
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000708 debug("getsockopt SO_ERROR failed");
709 } else {
710 if (err == 0) {
711 debug("channel %d: connected)", c->self);
712 } else {
713 debug("channel %d: not connected: %s",
714 c->self, strerror(err));
715 chan_read_failed(c);
716 chan_write_failed(c);
717 }
718 }
719 }
720}
721
Damien Millerb38eff82000-04-01 11:09:21 +1000722int
723channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
724{
725 char buf[16*1024];
726 int len;
727
728 if (c->rfd != -1 &&
729 FD_ISSET(c->rfd, readset)) {
730 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +1000731 if (len < 0 && (errno == EINTR || errno == EAGAIN))
732 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000733 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +1000734 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +1000735 c->self, c->rfd, len);
736 if (compat13) {
737 buffer_consume(&c->output, buffer_len(&c->output));
738 c->type = SSH_CHANNEL_INPUT_DRAINING;
739 debug("Channel %d status set to input draining.", c->self);
740 } else {
741 chan_read_failed(c);
742 }
743 return -1;
744 }
Damien Millerad833b32000-08-23 10:46:23 +1000745 if(c->input_filter != NULL) {
746 if (c->input_filter(c, buf, len) == -1) {
747 debug("filter stops channel %d", c->self);
748 chan_read_failed(c);
749 }
750 } else {
751 buffer_append(&c->input, buf, len);
752 }
Damien Millerb38eff82000-04-01 11:09:21 +1000753 }
754 return 1;
755}
756int
757channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
758{
Ben Lindstrome229b252001-03-05 06:28:06 +0000759 struct termios tio;
Damien Millerb38eff82000-04-01 11:09:21 +1000760 int len;
761
762 /* Send buffered output data to the socket. */
763 if (c->wfd != -1 &&
764 FD_ISSET(c->wfd, writeset) &&
765 buffer_len(&c->output) > 0) {
766 len = write(c->wfd, buffer_ptr(&c->output),
Damien Miller6f83b8e2000-05-02 09:23:45 +1000767 buffer_len(&c->output));
768 if (len < 0 && (errno == EINTR || errno == EAGAIN))
769 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000770 if (len <= 0) {
771 if (compat13) {
772 buffer_consume(&c->output, buffer_len(&c->output));
773 debug("Channel %d status set to input draining.", c->self);
774 c->type = SSH_CHANNEL_INPUT_DRAINING;
775 } else {
776 chan_write_failed(c);
777 }
778 return -1;
779 }
Damien Miller79438cc2001-02-16 12:34:57 +1100780 if (compat20 && c->isatty) {
Damien Miller79438cc2001-02-16 12:34:57 +1100781 if (tcgetattr(c->wfd, &tio) == 0 &&
782 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
783 /*
784 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +0000785 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +0000786 * size of a SSH2_MSG_CHANNEL_DATA message
787 * (4 byte channel id + data)
Damien Miller79438cc2001-02-16 12:34:57 +1100788 */
Ben Lindstrome229b252001-03-05 06:28:06 +0000789 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +1100790 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +1100791 }
792 }
Damien Millerb38eff82000-04-01 11:09:21 +1000793 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +1000794 if (compat20 && len > 0) {
795 c->local_consumed += len;
796 }
797 }
798 return 1;
799}
800int
801channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
802{
803 char buf[16*1024];
804 int len;
805
Damien Miller1383bd82000-04-06 12:32:37 +1000806/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +1000807 if (c->efd != -1) {
808 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
809 FD_ISSET(c->efd, writeset) &&
810 buffer_len(&c->extended) > 0) {
811 len = write(c->efd, buffer_ptr(&c->extended),
812 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +1100813 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000814 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000815 if (len < 0 && (errno == EINTR || errno == EAGAIN))
816 return 1;
817 if (len <= 0) {
818 debug2("channel %d: closing write-efd %d",
819 c->self, c->efd);
820 close(c->efd);
821 c->efd = -1;
822 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000823 buffer_consume(&c->extended, len);
824 c->local_consumed += len;
825 }
826 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
827 FD_ISSET(c->efd, readset)) {
828 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +1100829 debug2("channel %d: read %d from efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000830 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000831 if (len < 0 && (errno == EINTR || errno == EAGAIN))
832 return 1;
833 if (len <= 0) {
834 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +1000835 c->self, c->efd);
836 close(c->efd);
837 c->efd = -1;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000838 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000839 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000840 }
Damien Miller33b13562000-04-04 14:38:59 +1000841 }
842 }
843 return 1;
844}
845int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000846channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000847{
Damien Millerefb4afe2000-04-12 18:45:05 +1000848 if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +1000849 c->local_window < c->local_window_max/2 &&
850 c->local_consumed > 0) {
851 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
852 packet_put_int(c->remote_id);
853 packet_put_int(c->local_consumed);
854 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +1100855 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +1000856 c->self, c->local_window,
857 c->local_consumed);
858 c->local_window += c->local_consumed;
859 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000860 }
861 return 1;
862}
863
864void
865channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
866{
867 channel_handle_rfd(c, readset, writeset);
868 channel_handle_wfd(c, readset, writeset);
869}
870
871void
Damien Miller33b13562000-04-04 14:38:59 +1000872channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
873{
874 channel_handle_rfd(c, readset, writeset);
875 channel_handle_wfd(c, readset, writeset);
876 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000877
878 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +1000879}
880
881void
Damien Millerb38eff82000-04-01 11:09:21 +1000882channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
883{
884 int len;
885 /* Send buffered output data to the socket. */
886 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
887 len = write(c->sock, buffer_ptr(&c->output),
888 buffer_len(&c->output));
889 if (len <= 0)
890 buffer_consume(&c->output, buffer_len(&c->output));
891 else
892 buffer_consume(&c->output, len);
893 }
894}
895
896void
Damien Miller33b13562000-04-04 14:38:59 +1000897channel_handler_init_20(void)
898{
899 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +1000900 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +1000901 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100902 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +1000903 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100904 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000905 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Damien Miller33b13562000-04-04 14:38:59 +1000906
907 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
908 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100909 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +1000910 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100911 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000912 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Miller33b13562000-04-04 14:38:59 +1000913}
914
915void
Damien Millerb38eff82000-04-01 11:09:21 +1000916channel_handler_init_13(void)
917{
918 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
919 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
920 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
921 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
922 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
923 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
924 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000925 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Damien Millerb38eff82000-04-01 11:09:21 +1000926
927 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
928 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
929 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
930 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
931 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000932 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerb38eff82000-04-01 11:09:21 +1000933}
934
935void
936channel_handler_init_15(void)
937{
938 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +1000939 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +1000940 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
941 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
942 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000943 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Damien Millerb38eff82000-04-01 11:09:21 +1000944
945 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
946 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
947 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
948 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000949 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerb38eff82000-04-01 11:09:21 +1000950}
951
952void
953channel_handler_init(void)
954{
955 int i;
956 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
957 channel_pre[i] = NULL;
958 channel_post[i] = NULL;
959 }
Damien Miller33b13562000-04-04 14:38:59 +1000960 if (compat20)
961 channel_handler_init_20();
962 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +1000963 channel_handler_init_13();
964 else
965 channel_handler_init_15();
966}
967
Damien Miller4af51302000-04-16 11:18:38 +1000968void
Damien Millerb38eff82000-04-01 11:09:21 +1000969channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
970{
971 static int did_init = 0;
972 int i;
973 Channel *c;
974
975 if (!did_init) {
976 channel_handler_init();
977 did_init = 1;
978 }
979 for (i = 0; i < channels_alloc; i++) {
980 c = &channels[i];
981 if (c->type == SSH_CHANNEL_FREE)
982 continue;
983 if (ftab[c->type] == NULL)
984 continue;
985 (*ftab[c->type])(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000986 if (chan_is_dead(c)) {
987 /*
988 * we have to remove the fd's from the select mask
989 * before the channels are free'd and the fd's are
990 * closed
991 */
992 if (c->wfd != -1)
993 FD_CLR(c->wfd, writeset);
994 if (c->rfd != -1)
995 FD_CLR(c->rfd, readset);
996 if (c->efd != -1) {
997 if (c->extended_usage == CHAN_EXTENDED_READ)
998 FD_CLR(c->efd, readset);
999 if (c->extended_usage == CHAN_EXTENDED_WRITE)
1000 FD_CLR(c->efd, writeset);
1001 }
1002 channel_free(c->self);
1003 }
Damien Millerb38eff82000-04-01 11:09:21 +10001004 }
1005}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001006
Damien Miller4af51302000-04-16 11:18:38 +10001007void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001008channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1009 int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001010{
Damien Miller5e953212001-01-30 09:14:00 +11001011 int n;
1012 u_int sz;
1013
1014 n = MAX(*maxfdp, channel_max_fd);
1015
1016 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1017 if (*readsetp == NULL || n > *maxfdp) {
1018 if (*readsetp)
1019 xfree(*readsetp);
1020 if (*writesetp)
1021 xfree(*writesetp);
1022 *readsetp = xmalloc(sz);
1023 *writesetp = xmalloc(sz);
1024 *maxfdp = n;
1025 }
1026 memset(*readsetp, 0, sz);
1027 memset(*writesetp, 0, sz);
1028
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001029 if (!rekeying)
1030 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001031}
1032
Damien Miller4af51302000-04-16 11:18:38 +10001033void
Damien Miller95def091999-11-25 00:26:21 +11001034channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001035{
Damien Millerb38eff82000-04-01 11:09:21 +10001036 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001037}
1038
Damien Miller5e953212001-01-30 09:14:00 +11001039/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001040
Damien Miller4af51302000-04-16 11:18:38 +10001041void
Damien Miller95def091999-11-25 00:26:21 +11001042channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001043{
Damien Miller95def091999-11-25 00:26:21 +11001044 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001045 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001046
Damien Miller95def091999-11-25 00:26:21 +11001047 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001048 c = &channels[i];
Damien Miller34132e52000-01-14 15:45:46 +11001049
Damien Miller5428f641999-11-25 11:54:57 +11001050 /* We are only interested in channels that can have buffered incoming data. */
Damien Miller34132e52000-01-14 15:45:46 +11001051 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001052 if (c->type != SSH_CHANNEL_OPEN &&
1053 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001054 continue;
1055 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001056 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001057 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001058 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001059 if (compat20 &&
1060 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001061 /* XXX is this true? */
Ben Lindstromb1131e92001-03-05 07:27:13 +00001062 debug2("channel %d: no data after CLOSE", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001063 continue;
1064 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001065
Damien Miller95def091999-11-25 00:26:21 +11001066 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001067 if ((c->istate == CHAN_INPUT_OPEN ||
1068 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1069 (len = buffer_len(&c->input)) > 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001070 /* Send some data for the other side over the secure connection. */
Damien Miller33b13562000-04-04 14:38:59 +10001071 if (compat20) {
1072 if (len > c->remote_window)
1073 len = c->remote_window;
1074 if (len > c->remote_maxpacket)
1075 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001076 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001077 if (packet_is_interactive()) {
1078 if (len > 1024)
1079 len = 512;
1080 } else {
1081 /* Keep the packets at reasonable size. */
1082 if (len > packet_get_maxsize()/2)
1083 len = packet_get_maxsize()/2;
1084 }
Damien Miller95def091999-11-25 00:26:21 +11001085 }
Damien Millerb38eff82000-04-01 11:09:21 +10001086 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001087 packet_start(compat20 ?
1088 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001089 packet_put_int(c->remote_id);
1090 packet_put_string(buffer_ptr(&c->input), len);
1091 packet_send();
1092 buffer_consume(&c->input, len);
1093 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001094 }
1095 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001096 if (compat13)
1097 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001098 /*
1099 * input-buffer is empty and read-socket shutdown:
1100 * tell peer, that we will not send more data: send IEOF
1101 */
Damien Millerb38eff82000-04-01 11:09:21 +10001102 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001103 }
Damien Miller33b13562000-04-04 14:38:59 +10001104 /* Send extended data, i.e. stderr */
1105 if (compat20 &&
1106 c->remote_window > 0 &&
1107 (len = buffer_len(&c->extended)) > 0 &&
1108 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001109 debug2("channel %d: rwin %d elen %d euse %d",
1110 c->self, c->remote_window, buffer_len(&c->extended),
1111 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001112 if (len > c->remote_window)
1113 len = c->remote_window;
1114 if (len > c->remote_maxpacket)
1115 len = c->remote_maxpacket;
1116 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1117 packet_put_int(c->remote_id);
1118 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1119 packet_put_string(buffer_ptr(&c->extended), len);
1120 packet_send();
1121 buffer_consume(&c->extended, len);
1122 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001123 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001124 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001125 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001126}
1127
Damien Miller5428f641999-11-25 11:54:57 +11001128/*
1129 * This is called when a packet of type CHANNEL_DATA has just been received.
1130 * The message type has already been consumed, but channel number and data is
1131 * still there.
1132 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001133
Damien Miller4af51302000-04-16 11:18:38 +10001134void
Damien Miller62cee002000-09-23 17:15:56 +11001135channel_input_data(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136{
Damien Miller34132e52000-01-14 15:45:46 +11001137 int id;
Damien Miller95def091999-11-25 00:26:21 +11001138 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001139 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001140 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001141
Damien Miller95def091999-11-25 00:26:21 +11001142 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001143 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001144 c = channel_lookup(id);
1145 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001146 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001147
Damien Miller95def091999-11-25 00:26:21 +11001148 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001149 if (c->type != SSH_CHANNEL_OPEN &&
1150 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001151 return;
1152
1153 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001154 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001155 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001156
Damien Miller95def091999-11-25 00:26:21 +11001157 /* Get the data. */
1158 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001159 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001160
Damien Miller33b13562000-04-04 14:38:59 +10001161 if (compat20){
1162 if (data_len > c->local_maxpacket) {
1163 log("channel %d: rcvd big packet %d, maxpack %d",
1164 c->self, data_len, c->local_maxpacket);
1165 }
1166 if (data_len > c->local_window) {
1167 log("channel %d: rcvd too much data %d, win %d",
1168 c->self, data_len, c->local_window);
1169 xfree(data);
1170 return;
1171 }
1172 c->local_window -= data_len;
1173 }else{
1174 packet_integrity_check(plen, 4 + 4 + data_len, type);
1175 }
Damien Millerb38eff82000-04-01 11:09:21 +10001176 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001177 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001178}
Damien Miller4af51302000-04-16 11:18:38 +10001179void
Damien Miller62cee002000-09-23 17:15:56 +11001180channel_input_extended_data(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001181{
1182 int id;
1183 int tcode;
1184 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001185 u_int data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001186 Channel *c;
1187
1188 /* Get the channel number and verify it. */
1189 id = packet_get_int();
1190 c = channel_lookup(id);
1191
1192 if (c == NULL)
1193 packet_disconnect("Received extended_data for bad channel %d.", id);
1194 if (c->type != SSH_CHANNEL_OPEN) {
1195 log("channel %d: ext data for non open", id);
1196 return;
1197 }
1198 tcode = packet_get_int();
1199 if (c->efd == -1 ||
1200 c->extended_usage != CHAN_EXTENDED_WRITE ||
1201 tcode != SSH2_EXTENDED_DATA_STDERR) {
1202 log("channel %d: bad ext data", c->self);
1203 return;
1204 }
1205 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001206 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001207 if (data_len > c->local_window) {
1208 log("channel %d: rcvd too much extended_data %d, win %d",
1209 c->self, data_len, c->local_window);
1210 xfree(data);
1211 return;
1212 }
Damien Millerd3444942000-09-30 14:20:03 +11001213 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001214 c->local_window -= data_len;
1215 buffer_append(&c->extended, data, data_len);
1216 xfree(data);
1217}
1218
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001219
Damien Miller5428f641999-11-25 11:54:57 +11001220/*
1221 * Returns true if no channel has too much buffered data, and false if one or
1222 * more channel is overfull.
1223 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001224
Damien Miller4af51302000-04-16 11:18:38 +10001225int
Damien Miller95def091999-11-25 00:26:21 +11001226channel_not_very_much_buffered_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001227{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001228 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001229 Channel *c;
Damien Miller95def091999-11-25 00:26:21 +11001230
1231 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001232 c = &channels[i];
1233 if (c->type == SSH_CHANNEL_OPEN) {
Damien Miller33b13562000-04-04 14:38:59 +10001234 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
Damien Millerb38eff82000-04-01 11:09:21 +10001235 debug("channel %d: big input buffer %d",
1236 c->self, buffer_len(&c->input));
Damien Miller95def091999-11-25 00:26:21 +11001237 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001238 }
1239 if (buffer_len(&c->output) > packet_get_maxsize()) {
1240 debug("channel %d: big output buffer %d",
1241 c->self, buffer_len(&c->output));
Damien Miller95def091999-11-25 00:26:21 +11001242 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001243 }
Damien Miller95def091999-11-25 00:26:21 +11001244 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001245 }
Damien Miller95def091999-11-25 00:26:21 +11001246 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001247}
1248
Damien Miller4af51302000-04-16 11:18:38 +10001249void
Damien Miller62cee002000-09-23 17:15:56 +11001250channel_input_ieof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001251{
1252 int id;
1253 Channel *c;
1254
1255 packet_integrity_check(plen, 4, type);
1256
1257 id = packet_get_int();
1258 c = channel_lookup(id);
1259 if (c == NULL)
1260 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1261 chan_rcvd_ieof(c);
1262}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001263
Damien Miller4af51302000-04-16 11:18:38 +10001264void
Damien Miller62cee002000-09-23 17:15:56 +11001265channel_input_close(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001266{
Damien Millerb38eff82000-04-01 11:09:21 +10001267 int id;
1268 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001269
Damien Millerb38eff82000-04-01 11:09:21 +10001270 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001271
Damien Millerb38eff82000-04-01 11:09:21 +10001272 id = packet_get_int();
1273 c = channel_lookup(id);
1274 if (c == NULL)
1275 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001276
1277 /*
1278 * Send a confirmation that we have closed the channel and no more
1279 * data is coming for it.
1280 */
Damien Miller95def091999-11-25 00:26:21 +11001281 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001282 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001283 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001284
Damien Miller5428f641999-11-25 11:54:57 +11001285 /*
1286 * If the channel is in closed state, we have sent a close request,
1287 * and the other side will eventually respond with a confirmation.
1288 * Thus, we cannot free the channel here, because then there would be
1289 * no-one to receive the confirmation. The channel gets freed when
1290 * the confirmation arrives.
1291 */
Damien Millerb38eff82000-04-01 11:09:21 +10001292 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001293 /*
1294 * Not a closed channel - mark it as draining, which will
1295 * cause it to be freed later.
1296 */
Damien Millerb38eff82000-04-01 11:09:21 +10001297 buffer_consume(&c->input, buffer_len(&c->input));
1298 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001299 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001300}
1301
Damien Millerb38eff82000-04-01 11:09:21 +10001302/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001303void
Damien Miller62cee002000-09-23 17:15:56 +11001304channel_input_oclose(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001305{
Damien Millerb38eff82000-04-01 11:09:21 +10001306 int id = packet_get_int();
1307 Channel *c = channel_lookup(id);
1308 packet_integrity_check(plen, 4, type);
1309 if (c == NULL)
1310 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1311 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001312}
1313
Damien Miller4af51302000-04-16 11:18:38 +10001314void
Damien Miller62cee002000-09-23 17:15:56 +11001315channel_input_close_confirmation(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001316{
1317 int id = packet_get_int();
1318 Channel *c = channel_lookup(id);
1319
Damien Miller4af51302000-04-16 11:18:38 +10001320 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001321 if (c == NULL)
1322 packet_disconnect("Received close confirmation for "
1323 "out-of-range channel %d.", id);
1324 if (c->type != SSH_CHANNEL_CLOSED)
1325 packet_disconnect("Received close confirmation for "
1326 "non-closed channel %d (type %d).", id, c->type);
1327 channel_free(c->self);
1328}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001329
Damien Miller4af51302000-04-16 11:18:38 +10001330void
Damien Miller62cee002000-09-23 17:15:56 +11001331channel_input_open_confirmation(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001332{
Damien Millerb38eff82000-04-01 11:09:21 +10001333 int id, remote_id;
1334 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001335
Damien Miller33b13562000-04-04 14:38:59 +10001336 if (!compat20)
1337 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001338
Damien Millerb38eff82000-04-01 11:09:21 +10001339 id = packet_get_int();
1340 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001341
Damien Millerb38eff82000-04-01 11:09:21 +10001342 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1343 packet_disconnect("Received open confirmation for "
1344 "non-opening channel %d.", id);
1345 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001346 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001347 c->remote_id = remote_id;
1348 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001349
1350 if (compat20) {
1351 c->remote_window = packet_get_int();
1352 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001353 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001354 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001355 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001356 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001357 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001358 }
1359 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1360 c->remote_window, c->remote_maxpacket);
1361 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001362}
1363
Damien Miller4af51302000-04-16 11:18:38 +10001364void
Damien Miller62cee002000-09-23 17:15:56 +11001365channel_input_open_failure(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001366{
Kevin Steves12057502001-02-05 14:54:34 +00001367 int id, reason;
1368 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001369 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001370
Damien Miller33b13562000-04-04 14:38:59 +10001371 if (!compat20)
1372 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001373
1374 id = packet_get_int();
1375 c = channel_lookup(id);
1376
1377 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1378 packet_disconnect("Received open failure for "
1379 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001380 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00001381 reason = packet_get_int();
1382 if (packet_remaining() > 0) {
1383 msg = packet_get_string(NULL);
1384 lang = packet_get_string(NULL);
1385 }
Damien Miller4af51302000-04-16 11:18:38 +10001386 packet_done();
Kevin Steves12057502001-02-05 14:54:34 +00001387 log("channel_open_failure: %d: reason %d %s", id,
1388 reason, msg ? msg : "<no additional info>");
1389 if (msg != NULL)
1390 xfree(msg);
1391 if (lang != NULL)
1392 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001393 }
Damien Miller95def091999-11-25 00:26:21 +11001394 /* Free the channel. This will also close the socket. */
Damien Millerb38eff82000-04-01 11:09:21 +10001395 channel_free(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001396}
1397
Damien Miller33b13562000-04-04 14:38:59 +10001398void
Damien Miller62cee002000-09-23 17:15:56 +11001399channel_input_channel_request(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001400{
1401 int id;
1402 Channel *c;
1403
1404 id = packet_get_int();
1405 c = channel_lookup(id);
1406
1407 if (c == NULL ||
1408 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1409 packet_disconnect("Received request for "
1410 "non-open channel %d.", id);
1411 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001412 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001413 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001414 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001415 } else {
1416 char *service = packet_get_string(NULL);
Ben Lindstromcc74df72001-03-05 06:20:14 +00001417 debug("channel %d: rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001418 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001419 xfree(service);
1420 }
1421}
1422
Damien Miller4af51302000-04-16 11:18:38 +10001423void
Damien Miller62cee002000-09-23 17:15:56 +11001424channel_input_window_adjust(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001425{
1426 Channel *c;
1427 int id, adjust;
1428
1429 if (!compat20)
1430 return;
1431
1432 /* Get the channel number and verify it. */
1433 id = packet_get_int();
1434 c = channel_lookup(id);
1435
1436 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1437 log("Received window adjust for "
1438 "non-open channel %d.", id);
1439 return;
1440 }
1441 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001442 packet_done();
Damien Millerd3444942000-09-30 14:20:03 +11001443 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10001444 c->remote_window += adjust;
1445}
1446
Damien Miller5428f641999-11-25 11:54:57 +11001447/*
1448 * Stops listening for channels, and removes any unix domain sockets that we
1449 * might have.
1450 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001451
Damien Miller4af51302000-04-16 11:18:38 +10001452void
Damien Miller95def091999-11-25 00:26:21 +11001453channel_stop_listening()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001454{
Damien Miller95def091999-11-25 00:26:21 +11001455 int i;
1456 for (i = 0; i < channels_alloc; i++) {
1457 switch (channels[i].type) {
1458 case SSH_CHANNEL_AUTH_SOCKET:
1459 close(channels[i].sock);
Damien Miller78315eb2000-09-29 23:01:36 +11001460 unlink(channels[i].path);
Damien Miller95def091999-11-25 00:26:21 +11001461 channel_free(i);
1462 break;
1463 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001464 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001465 case SSH_CHANNEL_X11_LISTENER:
1466 close(channels[i].sock);
1467 channel_free(i);
1468 break;
1469 default:
1470 break;
1471 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001473}
1474
Damien Miller5428f641999-11-25 11:54:57 +11001475/*
Damien Miller6f83b8e2000-05-02 09:23:45 +10001476 * Closes the sockets/fds of all channels. This is used to close extra file
Damien Miller5428f641999-11-25 11:54:57 +11001477 * descriptors after a fork.
1478 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001479
Damien Miller4af51302000-04-16 11:18:38 +10001480void
Damien Miller95def091999-11-25 00:26:21 +11001481channel_close_all()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001482{
Damien Miller95def091999-11-25 00:26:21 +11001483 int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +10001484 for (i = 0; i < channels_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001485 if (channels[i].type != SSH_CHANNEL_FREE)
Damien Miller6f83b8e2000-05-02 09:23:45 +10001486 channel_close_fds(&channels[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001487}
1488
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001489/* Returns true if any channel is still open. */
1490
Damien Miller4af51302000-04-16 11:18:38 +10001491int
Damien Miller95def091999-11-25 00:26:21 +11001492channel_still_open()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001493{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001494 u_int i;
Damien Miller95def091999-11-25 00:26:21 +11001495 for (i = 0; i < channels_alloc; i++)
1496 switch (channels[i].type) {
1497 case SSH_CHANNEL_FREE:
1498 case SSH_CHANNEL_X11_LISTENER:
1499 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001500 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001501 case SSH_CHANNEL_CLOSED:
1502 case SSH_CHANNEL_AUTH_SOCKET:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001503 case SSH_CHANNEL_CONNECTING: /* XXX ??? */
Damien Miller95def091999-11-25 00:26:21 +11001504 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001505 case SSH_CHANNEL_LARVAL:
1506 if (!compat20)
1507 fatal("cannot happen: SSH_CHANNEL_LARVAL");
1508 continue;
Damien Miller95def091999-11-25 00:26:21 +11001509 case SSH_CHANNEL_OPENING:
1510 case SSH_CHANNEL_OPEN:
1511 case SSH_CHANNEL_X11_OPEN:
1512 return 1;
1513 case SSH_CHANNEL_INPUT_DRAINING:
1514 case SSH_CHANNEL_OUTPUT_DRAINING:
1515 if (!compat13)
1516 fatal("cannot happen: OUT_DRAIN");
1517 return 1;
1518 default:
1519 fatal("channel_still_open: bad channel type %d", channels[i].type);
1520 /* NOTREACHED */
1521 }
1522 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001523}
1524
Damien Miller5428f641999-11-25 11:54:57 +11001525/*
1526 * Returns a message describing the currently open forwarded connections,
1527 * suitable for sending to the client. The message contains crlf pairs for
1528 * newlines.
1529 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001530
Damien Miller95def091999-11-25 00:26:21 +11001531char *
1532channel_open_message()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001533{
Damien Miller95def091999-11-25 00:26:21 +11001534 Buffer buffer;
1535 int i;
1536 char buf[512], *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001537
Damien Miller95def091999-11-25 00:26:21 +11001538 buffer_init(&buffer);
1539 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001540 buffer_append(&buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001541 for (i = 0; i < channels_alloc; i++) {
1542 Channel *c = &channels[i];
1543 switch (c->type) {
1544 case SSH_CHANNEL_FREE:
1545 case SSH_CHANNEL_X11_LISTENER:
1546 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001547 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001548 case SSH_CHANNEL_CLOSED:
1549 case SSH_CHANNEL_AUTH_SOCKET:
1550 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001551 case SSH_CHANNEL_LARVAL:
Damien Miller95def091999-11-25 00:26:21 +11001552 case SSH_CHANNEL_OPENING:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001553 case SSH_CHANNEL_CONNECTING:
Damien Miller95def091999-11-25 00:26:21 +11001554 case SSH_CHANNEL_OPEN:
1555 case SSH_CHANNEL_X11_OPEN:
1556 case SSH_CHANNEL_INPUT_DRAINING:
1557 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Millerb38eff82000-04-01 11:09:21 +10001558 snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
Damien Miller34132e52000-01-14 15:45:46 +11001559 c->self, c->remote_name,
1560 c->type, c->remote_id,
1561 c->istate, buffer_len(&c->input),
Damien Millerb38eff82000-04-01 11:09:21 +10001562 c->ostate, buffer_len(&c->output),
1563 c->rfd, c->wfd);
Damien Miller95def091999-11-25 00:26:21 +11001564 buffer_append(&buffer, buf, strlen(buf));
1565 continue;
1566 default:
Damien Millerb38eff82000-04-01 11:09:21 +10001567 fatal("channel_open_message: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001568 /* NOTREACHED */
1569 }
1570 }
1571 buffer_append(&buffer, "\0", 1);
1572 cp = xstrdup(buffer_ptr(&buffer));
1573 buffer_free(&buffer);
1574 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001575}
1576
Damien Miller5428f641999-11-25 11:54:57 +11001577/*
1578 * Initiate forwarding of connections to local port "port" through the secure
1579 * channel to host:port from remote side.
1580 */
Kevin Steves12057502001-02-05 14:54:34 +00001581int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001582channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
1583 u_short port_to_connect, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001584{
Kevin Steves12057502001-02-05 14:54:34 +00001585 return channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +11001586 NULL, listen_port,
1587 host_to_connect, port_to_connect,
1588 gateway_ports, /*remote_fwd*/ 0);
1589}
1590
1591/*
1592 * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
1593 * (SSH_CHANNEL_RPORT_LISTENER).
1594 */
Kevin Steves12057502001-02-05 14:54:34 +00001595int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001596channel_request_forwarding(
1597 const char *listen_address, u_short listen_port,
1598 const char *host_to_connect, u_short port_to_connect,
1599 int gateway_ports, int remote_fwd)
1600{
1601 int success, ch, sock, on = 1, ctype;
Damien Miller34132e52000-01-14 15:45:46 +11001602 struct addrinfo hints, *ai, *aitop;
1603 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0bc1bd82000-11-13 22:57:25 +11001604 const char *host;
Damien Miller5428f641999-11-25 11:54:57 +11001605 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001606
Kevin Steves12057502001-02-05 14:54:34 +00001607 success = 0;
1608
Damien Miller0bc1bd82000-11-13 22:57:25 +11001609 if (remote_fwd) {
1610 host = listen_address;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001611 ctype = SSH_CHANNEL_RPORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001612 } else {
1613 host = host_to_connect;
1614 ctype =SSH_CHANNEL_PORT_LISTENER;
1615 }
1616
Kevin Steves12057502001-02-05 14:54:34 +00001617 if (strlen(host) > sizeof(channels[0].path) - 1) {
1618 error("Forward host name too long.");
1619 return success;
1620 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001621
Damien Miller0bc1bd82000-11-13 22:57:25 +11001622 /* XXX listen_address is currently ignored */
Damien Miller5428f641999-11-25 11:54:57 +11001623 /*
Damien Miller34132e52000-01-14 15:45:46 +11001624 * getaddrinfo returns a loopback address if the hostname is
1625 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11001626 */
Damien Miller34132e52000-01-14 15:45:46 +11001627 memset(&hints, 0, sizeof(hints));
1628 hints.ai_family = IPv4or6;
1629 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
1630 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001631 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001632 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
1633 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11001634
Damien Miller34132e52000-01-14 15:45:46 +11001635 for (ai = aitop; ai; ai = ai->ai_next) {
1636 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1637 continue;
1638 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1639 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001640 error("channel_request_forwarding: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001641 continue;
1642 }
1643 /* Create a port to listen for the host. */
1644 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1645 if (sock < 0) {
1646 /* this is no error since kernel may not support ipv6 */
1647 verbose("socket: %.100s", strerror(errno));
1648 continue;
1649 }
1650 /*
1651 * Set socket options. We would like the socket to disappear
1652 * as soon as it has been closed for whatever reason.
1653 */
1654 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
1655 linger.l_onoff = 1;
1656 linger.l_linger = 5;
1657 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
1658 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001659
Damien Miller34132e52000-01-14 15:45:46 +11001660 /* Bind the socket to the address. */
1661 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1662 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11001663 if (!ai->ai_next)
1664 error("bind: %.100s", strerror(errno));
1665 else
1666 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001667
Damien Miller34132e52000-01-14 15:45:46 +11001668 close(sock);
1669 continue;
1670 }
1671 /* Start listening for connections on the socket. */
1672 if (listen(sock, 5) < 0) {
1673 error("listen: %.100s", strerror(errno));
1674 close(sock);
1675 continue;
1676 }
1677 /* Allocate a channel number for the socket. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001678 ch = channel_new("port listener", ctype, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10001679 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11001680 0, xstrdup("port listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11001681 strlcpy(channels[ch].path, host, sizeof(channels[ch].path));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001682 channels[ch].host_port = port_to_connect;
1683 channels[ch].listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11001684 success = 1;
1685 }
1686 if (success == 0)
Kevin Steves12057502001-02-05 14:54:34 +00001687 error("channel_request_forwarding: cannot listen to port: %d",
1688 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001689 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00001690 return success;
Damien Miller95def091999-11-25 00:26:21 +11001691}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001692
Damien Miller5428f641999-11-25 11:54:57 +11001693/*
1694 * Initiate forwarding of connections to port "port" on remote host through
1695 * the secure channel to host:port from local side.
1696 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001697
Damien Miller4af51302000-04-16 11:18:38 +10001698void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001699channel_request_remote_forwarding(u_short listen_port,
1700 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001701{
Damien Miller0bc1bd82000-11-13 22:57:25 +11001702 int payload_len, type, success = 0;
1703
Damien Miller95def091999-11-25 00:26:21 +11001704 /* Record locally that connection to this host/port is permitted. */
1705 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1706 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001707
Damien Miller95def091999-11-25 00:26:21 +11001708 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10001709 if (compat20) {
1710 const char *address_to_bind = "0.0.0.0";
1711 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1712 packet_put_cstring("tcpip-forward");
1713 packet_put_char(0); /* boolean: want reply */
1714 packet_put_cstring(address_to_bind);
1715 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001716 packet_send();
1717 packet_write_wait();
1718 /* Assume that server accepts the request */
1719 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10001720 } else {
1721 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10001722 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10001723 packet_put_cstring(host_to_connect);
1724 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10001725 packet_send();
1726 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001727
1728 /* Wait for response from the remote side. */
1729 type = packet_read(&payload_len);
1730 switch (type) {
1731 case SSH_SMSG_SUCCESS:
1732 success = 1;
1733 break;
1734 case SSH_SMSG_FAILURE:
1735 log("Warning: Server denied remote port forwarding.");
1736 break;
1737 default:
1738 /* Unknown packet */
1739 packet_disconnect("Protocol error for port forward request:"
1740 "received packet type %d.", type);
1741 }
1742 }
1743 if (success) {
1744 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
1745 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
1746 permitted_opens[num_permitted_opens].listen_port = listen_port;
1747 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10001748 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001749}
1750
Damien Miller5428f641999-11-25 11:54:57 +11001751/*
1752 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
1753 * listening for the port, and sends back a success reply (or disconnect
1754 * message if there was an error). This never returns if there was an error.
1755 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001756
Damien Miller4af51302000-04-16 11:18:38 +10001757void
Damien Millere247cc42000-05-07 12:03:14 +10001758channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001759{
Damien Milleraae6c611999-12-06 11:47:28 +11001760 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11001761 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001762
Damien Miller95def091999-11-25 00:26:21 +11001763 /* Get arguments from the packet. */
1764 port = packet_get_int();
1765 hostname = packet_get_string(NULL);
1766 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001767
Damien Millerbac2d8a2000-09-05 16:13:06 +11001768#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11001769 /*
1770 * Check that an unprivileged user is not trying to forward a
1771 * privileged port.
1772 */
Damien Miller95def091999-11-25 00:26:21 +11001773 if (port < IPPORT_RESERVED && !is_root)
1774 packet_disconnect("Requested forwarding of port %d but user is not root.",
1775 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001776#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001777 /* Initiate forwarding */
Damien Millere247cc42000-05-07 12:03:14 +10001778 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11001779
1780 /* Free the argument string. */
1781 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001782}
1783
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001784/*
1785 * Permits opening to any host/port if permitted_opens[] is empty. This is
1786 * usually called by the server, because the user could connect to any port
1787 * anyway, and the server has no way to know but to trust the client anyway.
1788 */
1789void
1790channel_permit_all_opens()
1791{
1792 if (num_permitted_opens == 0)
1793 all_opens_permitted = 1;
1794}
1795
Ben Lindstroma3700052001-04-05 23:26:32 +00001796void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001797channel_add_permitted_opens(char *host, int port)
1798{
1799 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1800 fatal("channel_request_remote_forwarding: too many forwards");
1801 debug("allow port forwarding to host %s port %d", host, port);
1802
1803 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
1804 permitted_opens[num_permitted_opens].port_to_connect = port;
1805 num_permitted_opens++;
1806
1807 all_opens_permitted = 0;
1808}
1809
Ben Lindstroma3700052001-04-05 23:26:32 +00001810void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001811channel_clear_permitted_opens(void)
1812{
1813 int i;
1814
1815 for (i = 0; i < num_permitted_opens; i++)
1816 xfree(permitted_opens[i].host_to_connect);
1817 num_permitted_opens = 0;
1818
1819}
1820
1821
1822/* return socket to remote host, port */
Damien Millerb38eff82000-04-01 11:09:21 +10001823int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001824connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001825{
Damien Miller34132e52000-01-14 15:45:46 +11001826 struct addrinfo hints, *ai, *aitop;
1827 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1828 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10001829 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11001830
Damien Miller34132e52000-01-14 15:45:46 +11001831 memset(&hints, 0, sizeof(hints));
1832 hints.ai_family = IPv4or6;
1833 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001834 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11001835 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001836 error("connect_to %.100s: unknown host (%s)", host,
1837 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10001838 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001839 }
Damien Miller34132e52000-01-14 15:45:46 +11001840 for (ai = aitop; ai; ai = ai->ai_next) {
1841 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1842 continue;
1843 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1844 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001845 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001846 continue;
1847 }
Damien Miller34132e52000-01-14 15:45:46 +11001848 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1849 if (sock < 0) {
1850 error("socket: %.100s", strerror(errno));
1851 continue;
1852 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +00001853 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001854 fatal("connect_to: F_SETFL: %s", strerror(errno));
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001855 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
1856 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001857 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11001858 strerror(errno));
1859 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001860 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11001861 }
1862 break; /* success */
1863
1864 }
1865 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11001866 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001867 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10001868 return -1;
1869 }
1870 /* success */
1871 return sock;
1872}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001873
Damien Miller0bc1bd82000-11-13 22:57:25 +11001874int
1875channel_connect_by_listen_adress(u_short listen_port)
1876{
1877 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001878
Damien Miller0bc1bd82000-11-13 22:57:25 +11001879 for (i = 0; i < num_permitted_opens; i++)
1880 if (permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001881 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11001882 permitted_opens[i].host_to_connect,
1883 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00001884 error("WARNING: Server requests forwarding for unknown listen_port %d",
1885 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001886 return -1;
1887}
Damien Millerb38eff82000-04-01 11:09:21 +10001888
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001889/* Check if connecting to that port is permitted and connect. */
1890int
1891channel_connect_to(const char *host, u_short port)
1892{
1893 int i, permit;
1894
1895 permit = all_opens_permitted;
1896 if (!permit) {
1897 for (i = 0; i < num_permitted_opens; i++)
1898 if (permitted_opens[i].port_to_connect == port &&
1899 strcmp(permitted_opens[i].host_to_connect, host) == 0)
1900 permit = 1;
1901
1902 }
1903 if (!permit) {
1904 log("Received request to connect to host %.100s port %d, "
1905 "but the request was denied.", host, port);
1906 return -1;
1907 }
1908 return connect_to(host, port);
1909}
1910
Damien Millerb38eff82000-04-01 11:09:21 +10001911/*
1912 * This is called after receiving PORT_OPEN message. This attempts to
1913 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
1914 * or CHANNEL_OPEN_FAILURE.
1915 */
1916
Damien Miller4af51302000-04-16 11:18:38 +10001917void
Damien Miller62cee002000-09-23 17:15:56 +11001918channel_input_port_open(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001919{
1920 u_short host_port;
1921 char *host, *originator_string;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001922 int remote_channel, sock = -1, newch;
Damien Millerb38eff82000-04-01 11:09:21 +10001923
Damien Millerb38eff82000-04-01 11:09:21 +10001924 remote_channel = packet_get_int();
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001925 host = packet_get_string(NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001926 host_port = packet_get_int();
1927
Damien Millerb38eff82000-04-01 11:09:21 +10001928 if (have_hostname_in_open) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001929 originator_string = packet_get_string(NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10001930 } else {
1931 originator_string = xstrdup("unknown (remote did not supply name)");
Damien Miller95def091999-11-25 00:26:21 +11001932 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001933 packet_done();
1934 sock = channel_connect_to(host, host_port);
1935 if (sock != -1) {
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001936 newch = channel_allocate(SSH_CHANNEL_CONNECTING,
1937 sock, originator_string);
Damien Millerb38eff82000-04-01 11:09:21 +10001938 channels[newch].remote_id = remote_channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001939
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001940 /*XXX delay answer? */
Damien Millerb38eff82000-04-01 11:09:21 +10001941 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1942 packet_put_int(remote_channel);
1943 packet_put_int(newch);
1944 packet_send();
1945 } else {
1946 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1947 packet_put_int(remote_channel);
1948 packet_send();
1949 }
Damien Miller95def091999-11-25 00:26:21 +11001950 xfree(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001951}
1952
Damien Miller5428f641999-11-25 11:54:57 +11001953/*
1954 * Creates an internet domain socket for listening for X11 connections.
1955 * Returns a suitable value for the DISPLAY variable, or NULL if an error
1956 * occurs.
1957 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001958
Damien Miller34132e52000-01-14 15:45:46 +11001959#define NUM_SOCKS 10
1960
Damien Miller95def091999-11-25 00:26:21 +11001961char *
Damien Millera34a28b1999-12-14 10:47:15 +11001962x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001963{
Damien Milleraae6c611999-12-06 11:47:28 +11001964 int display_number, sock;
1965 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11001966 struct addrinfo hints, *ai, *aitop;
1967 char strport[NI_MAXSERV];
1968 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1969 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11001970 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001971
Damien Millera34a28b1999-12-14 10:47:15 +11001972 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11001973 display_number < MAX_DISPLAYS;
1974 display_number++) {
1975 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11001976 memset(&hints, 0, sizeof(hints));
1977 hints.ai_family = IPv4or6;
1978 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
1979 hints.ai_socktype = SOCK_STREAM;
1980 snprintf(strport, sizeof strport, "%d", port);
1981 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
1982 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11001983 return NULL;
1984 }
Damien Miller34132e52000-01-14 15:45:46 +11001985 for (ai = aitop; ai; ai = ai->ai_next) {
1986 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1987 continue;
1988 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1989 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11001990 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11001991 error("socket: %.100s", strerror(errno));
1992 return NULL;
1993 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11001994 debug("x11_create_display_inet: Socket family %d not supported",
1995 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11001996 continue;
1997 }
Damien Miller34132e52000-01-14 15:45:46 +11001998 }
1999 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2000 debug("bind port %d: %.100s", port, strerror(errno));
2001 shutdown(sock, SHUT_RDWR);
2002 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002003
2004 if (ai->ai_next)
2005 continue;
2006
Damien Miller34132e52000-01-14 15:45:46 +11002007 for (n = 0; n < num_socks; n++) {
2008 shutdown(socks[n], SHUT_RDWR);
2009 close(socks[n]);
2010 }
2011 num_socks = 0;
2012 break;
2013 }
2014 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002015#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002016 if (num_socks == NUM_SOCKS)
2017 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002018#else
2019 break;
2020#endif
Damien Miller95def091999-11-25 00:26:21 +11002021 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002022 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002023 if (num_socks > 0)
2024 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002025 }
Damien Miller95def091999-11-25 00:26:21 +11002026 if (display_number >= MAX_DISPLAYS) {
2027 error("Failed to allocate internet-domain X11 display socket.");
2028 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002029 }
Damien Miller95def091999-11-25 00:26:21 +11002030 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002031 for (n = 0; n < num_socks; n++) {
2032 sock = socks[n];
2033 if (listen(sock, 5) < 0) {
2034 error("listen: %.100s", strerror(errno));
2035 shutdown(sock, SHUT_RDWR);
2036 close(sock);
2037 return NULL;
2038 }
Damien Miller95def091999-11-25 00:26:21 +11002039 }
Damien Miller34132e52000-01-14 15:45:46 +11002040
Damien Miller95def091999-11-25 00:26:21 +11002041 /* Set up a suitable value for the DISPLAY variable. */
2042 if (gethostname(hostname, sizeof(hostname)) < 0)
2043 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11002044
2045#ifdef IPADDR_IN_DISPLAY
Kevin Stevesef4eea92001-02-05 12:42:17 +00002046 /*
Damien Miller76112de1999-12-21 11:18:08 +11002047 * HPUX detects the local hostname in the DISPLAY variable and tries
2048 * to set up a shared memory connection to the server, which it
2049 * incorrectly supposes to be local.
2050 *
2051 * The workaround - as used in later $$H and other programs - is
2052 * is to set display to the host's IP address.
2053 */
2054 {
2055 struct hostent *he;
2056 struct in_addr my_addr;
2057
2058 he = gethostbyname(hostname);
2059 if (he == NULL) {
2060 error("[X11-broken-fwd-hostname-workaround] Could not get "
2061 "IP address for hostname %s.", hostname);
2062
2063 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
2064 "Could not get IP address for hostname %s.", hostname);
2065
2066 shutdown(sock, SHUT_RDWR);
2067 close(sock);
2068
2069 return NULL;
2070 }
2071
2072 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2073
2074 /* Set DISPLAY to <ip address>:screen.display */
Kevin Stevesef4eea92001-02-05 12:42:17 +00002075 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Kevin Stevesda6cd592000-12-29 18:41:21 +00002076 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002077 }
2078#else /* IPADDR_IN_DISPLAY */
2079 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11002080 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Kevin Stevesda6cd592000-12-29 18:41:21 +00002081 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002082#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002083
Damien Miller34132e52000-01-14 15:45:46 +11002084 /* Allocate a channel for each socket. */
2085 for (n = 0; n < num_socks; n++) {
2086 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10002087 (void) channel_new("x11 listener",
2088 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2089 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002090 0, xstrdup("X11 inet listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11002091 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002092
Damien Miller95def091999-11-25 00:26:21 +11002093 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11002094 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002095}
2096
2097#ifndef X_UNIX_PATH
2098#define X_UNIX_PATH "/tmp/.X11-unix/X"
2099#endif
2100
2101static
2102int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002103connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002104{
Damien Miller95def091999-11-25 00:26:21 +11002105 static const char *const x_sockets[] = {
2106 X_UNIX_PATH "%u",
2107 "/var/X/.X11-unix/X" "%u",
2108 "/usr/spool/sockets/X11/" "%u",
2109 NULL
2110 };
2111 int sock;
2112 struct sockaddr_un addr;
2113 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002114
Damien Miller95def091999-11-25 00:26:21 +11002115 for (path = x_sockets; *path; ++path) {
2116 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2117 if (sock < 0)
2118 error("socket: %.100s", strerror(errno));
2119 memset(&addr, 0, sizeof(addr));
2120 addr.sun_family = AF_UNIX;
2121 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
2122 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2123 return sock;
2124 close(sock);
2125 }
2126 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2127 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002128}
2129
Damien Millerbd483e72000-04-30 10:00:53 +10002130int
2131x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002132{
Damien Millerbd483e72000-04-30 10:00:53 +10002133 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002134 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002135 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002136 struct addrinfo hints, *ai, *aitop;
2137 char strport[NI_MAXSERV];
2138 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002139
Damien Miller95def091999-11-25 00:26:21 +11002140 /* Try to open a socket for the local X server. */
2141 display = getenv("DISPLAY");
2142 if (!display) {
2143 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002144 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002145 }
Damien Miller5428f641999-11-25 11:54:57 +11002146 /*
2147 * Now we decode the value of the DISPLAY variable and make a
2148 * connection to the real X server.
2149 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002150
Damien Miller5428f641999-11-25 11:54:57 +11002151 /*
2152 * Check if it is a unix domain socket. Unix domain displays are in
2153 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2154 */
Damien Miller95def091999-11-25 00:26:21 +11002155 if (strncmp(display, "unix:", 5) == 0 ||
2156 display[0] == ':') {
2157 /* Connect to the unix domain socket. */
2158 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2159 error("Could not parse display number from DISPLAY: %.100s",
2160 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002161 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002162 }
2163 /* Create a socket. */
2164 sock = connect_local_xsocket(display_number);
2165 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002166 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002167
2168 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002169 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002170 }
Damien Miller5428f641999-11-25 11:54:57 +11002171 /*
2172 * Connect to an inet socket. The DISPLAY value is supposedly
2173 * hostname:d[.s], where hostname may also be numeric IP address.
2174 */
Damien Miller95def091999-11-25 00:26:21 +11002175 strncpy(buf, display, sizeof(buf));
2176 buf[sizeof(buf) - 1] = 0;
2177 cp = strchr(buf, ':');
2178 if (!cp) {
2179 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002180 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002181 }
Damien Miller95def091999-11-25 00:26:21 +11002182 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002183 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002184 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2185 error("Could not parse display number from DISPLAY: %.100s",
2186 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002187 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002188 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002189
Damien Miller34132e52000-01-14 15:45:46 +11002190 /* Look up the host address */
2191 memset(&hints, 0, sizeof(hints));
2192 hints.ai_family = IPv4or6;
2193 hints.ai_socktype = SOCK_STREAM;
2194 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2195 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2196 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002197 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002198 }
Damien Miller34132e52000-01-14 15:45:46 +11002199 for (ai = aitop; ai; ai = ai->ai_next) {
2200 /* Create a socket. */
2201 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2202 if (sock < 0) {
2203 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002204 continue;
2205 }
2206 /* Connect it to the display. */
2207 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2208 debug("connect %.100s port %d: %.100s", buf,
2209 6000 + display_number, strerror(errno));
2210 close(sock);
2211 continue;
2212 }
2213 /* Success */
2214 break;
Damien Miller34132e52000-01-14 15:45:46 +11002215 }
Damien Miller34132e52000-01-14 15:45:46 +11002216 freeaddrinfo(aitop);
2217 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002218 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002219 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002220 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002221 }
Damien Millerbd483e72000-04-30 10:00:53 +10002222 return sock;
2223}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002224
Damien Millerbd483e72000-04-30 10:00:53 +10002225/*
2226 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2227 * the remote channel number. We should do whatever we want, and respond
2228 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2229 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002230
Damien Millerbd483e72000-04-30 10:00:53 +10002231void
Damien Miller62cee002000-09-23 17:15:56 +11002232x11_input_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002233{
2234 int remote_channel, sock = 0, newch;
2235 char *remote_host;
Ben Lindstrom46c16222000-12-22 01:43:59 +00002236 u_int remote_len;
Damien Miller95def091999-11-25 00:26:21 +11002237
Damien Millerbd483e72000-04-30 10:00:53 +10002238 /* Get remote channel number. */
2239 remote_channel = packet_get_int();
Damien Miller95def091999-11-25 00:26:21 +11002240
Damien Millerbd483e72000-04-30 10:00:53 +10002241 /* Get remote originator name. */
2242 if (have_hostname_in_open) {
2243 remote_host = packet_get_string(&remote_len);
2244 remote_len += 4;
2245 } else {
2246 remote_host = xstrdup("unknown (remote did not supply name)");
2247 remote_len = 0;
2248 }
2249
2250 debug("Received X11 open request.");
2251 packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN);
2252
2253 /* Obtain a connection to the real X display. */
2254 sock = x11_connect_display();
2255 if (sock == -1) {
2256 /* Send refusal to the remote host. */
2257 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2258 packet_put_int(remote_channel);
2259 packet_send();
2260 } else {
2261 /* Allocate a channel for this connection. */
2262 newch = channel_allocate(
2263 (x11_saved_proto == NULL) ?
2264 SSH_CHANNEL_OPEN : SSH_CHANNEL_X11_OPEN,
2265 sock, remote_host);
2266 channels[newch].remote_id = remote_channel;
2267
2268 /* Send a confirmation to the remote host. */
2269 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2270 packet_put_int(remote_channel);
2271 packet_put_int(newch);
2272 packet_send();
2273 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002274}
2275
Damien Miller69b69aa2000-10-28 14:19:58 +11002276/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2277void
2278deny_input_open(int type, int plen, void *ctxt)
2279{
2280 int rchan = packet_get_int();
2281 switch(type){
2282 case SSH_SMSG_AGENT_OPEN:
2283 error("Warning: ssh server tried agent forwarding.");
2284 break;
2285 case SSH_SMSG_X11_OPEN:
2286 error("Warning: ssh server tried X11 forwarding.");
2287 break;
2288 default:
2289 error("deny_input_open: type %d plen %d", type, plen);
2290 break;
2291 }
2292 error("Warning: this is probably a break in attempt by a malicious server.");
2293 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2294 packet_put_int(rchan);
2295 packet_send();
2296}
2297
Damien Miller5428f641999-11-25 11:54:57 +11002298/*
2299 * Requests forwarding of X11 connections, generates fake authentication
2300 * data, and enables authentication spoofing.
2301 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002302
Damien Miller4af51302000-04-16 11:18:38 +10002303void
Damien Millerbd483e72000-04-30 10:00:53 +10002304x11_request_forwarding_with_spoofing(int client_session_id,
2305 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002306{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002307 u_int data_len = (u_int) strlen(data) / 2;
Ben Lindstromb3211a82001-02-10 22:33:19 +00002308 u_int i, value, len;
Damien Miller95def091999-11-25 00:26:21 +11002309 char *new_data;
2310 int screen_number;
2311 const char *cp;
2312 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002313
Damien Miller95def091999-11-25 00:26:21 +11002314 cp = getenv("DISPLAY");
2315 if (cp)
2316 cp = strchr(cp, ':');
2317 if (cp)
2318 cp = strchr(cp, '.');
2319 if (cp)
2320 screen_number = atoi(cp + 1);
2321 else
2322 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002323
Damien Miller95def091999-11-25 00:26:21 +11002324 /* Save protocol name. */
2325 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002326
Damien Miller5428f641999-11-25 11:54:57 +11002327 /*
2328 * Extract real authentication data and generate fake data of the
2329 * same length.
2330 */
Damien Miller95def091999-11-25 00:26:21 +11002331 x11_saved_data = xmalloc(data_len);
2332 x11_fake_data = xmalloc(data_len);
2333 for (i = 0; i < data_len; i++) {
2334 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2335 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2336 if (i % 4 == 0)
2337 rand = arc4random();
2338 x11_saved_data[i] = value;
2339 x11_fake_data[i] = rand & 0xff;
2340 rand >>= 8;
2341 }
2342 x11_saved_data_len = data_len;
2343 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002344
Damien Miller95def091999-11-25 00:26:21 +11002345 /* Convert the fake data into hex. */
Ben Lindstromb3211a82001-02-10 22:33:19 +00002346 len = 2 * data_len + 1;
2347 new_data = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +11002348 for (i = 0; i < data_len; i++)
Ben Lindstromb3211a82001-02-10 22:33:19 +00002349 snprintf(new_data + 2 * i, len - 2 * i,
2350 "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002351
Damien Miller95def091999-11-25 00:26:21 +11002352 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002353 if (compat20) {
2354 channel_request_start(client_session_id, "x11-req", 0);
2355 packet_put_char(0); /* XXX bool single connection */
2356 } else {
2357 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2358 }
2359 packet_put_cstring(proto);
2360 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002361 packet_put_int(screen_number);
2362 packet_send();
2363 packet_write_wait();
2364 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002365}
2366
2367/* Sends a message to the server to request authentication fd forwarding. */
2368
Damien Miller4af51302000-04-16 11:18:38 +10002369void
Damien Miller95def091999-11-25 00:26:21 +11002370auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002371{
Damien Miller95def091999-11-25 00:26:21 +11002372 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2373 packet_send();
2374 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002375}
2376
Damien Miller5428f641999-11-25 11:54:57 +11002377/*
2378 * Returns the name of the forwarded authentication socket. Returns NULL if
2379 * there is no forwarded authentication socket. The returned value points to
2380 * a static buffer.
2381 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002382
Damien Miller95def091999-11-25 00:26:21 +11002383char *
2384auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002385{
Damien Miller95def091999-11-25 00:26:21 +11002386 return channel_forwarded_auth_socket_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002387}
2388
2389/* removes the agent forwarding socket */
2390
Damien Miller4af51302000-04-16 11:18:38 +10002391void
Damien Miller95def091999-11-25 00:26:21 +11002392cleanup_socket(void)
2393{
Damien Miller78315eb2000-09-29 23:01:36 +11002394 unlink(channel_forwarded_auth_socket_name);
Damien Miller95def091999-11-25 00:26:21 +11002395 rmdir(channel_forwarded_auth_socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002396}
2397
Damien Miller5428f641999-11-25 11:54:57 +11002398/*
Damien Millerd3a18572000-06-07 19:55:44 +10002399 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002400 * This starts forwarding authentication requests.
2401 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002402
Damien Millerd3a18572000-06-07 19:55:44 +10002403int
Damien Miller95def091999-11-25 00:26:21 +11002404auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002405{
Damien Miller95def091999-11-25 00:26:21 +11002406 int sock, newch;
2407 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002408
Damien Miller95def091999-11-25 00:26:21 +11002409 if (auth_get_socket_name() != NULL)
2410 fatal("Protocol error: authentication forwarding requested twice.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002411
Damien Miller95def091999-11-25 00:26:21 +11002412 /* Temporarily drop privileged uid for mkdir/bind. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00002413 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002414
Damien Miller95def091999-11-25 00:26:21 +11002415 /* Allocate a buffer for the socket name, and format the name. */
2416 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
2417 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
2418 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002419
Damien Miller95def091999-11-25 00:26:21 +11002420 /* Create private directory for socket */
Damien Millerd3a18572000-06-07 19:55:44 +10002421 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL) {
2422 packet_send_debug("Agent forwarding disabled: mkdtemp() failed: %.100s",
2423 strerror(errno));
2424 restore_uid();
2425 xfree(channel_forwarded_auth_socket_name);
2426 xfree(channel_forwarded_auth_socket_dir);
2427 channel_forwarded_auth_socket_name = NULL;
2428 channel_forwarded_auth_socket_dir = NULL;
2429 return 0;
2430 }
Damien Miller95def091999-11-25 00:26:21 +11002431 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
2432 channel_forwarded_auth_socket_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002433
Damien Miller95def091999-11-25 00:26:21 +11002434 if (atexit(cleanup_socket) < 0) {
2435 int saved = errno;
2436 cleanup_socket();
2437 packet_disconnect("socket: %.100s", strerror(saved));
2438 }
2439 /* Create the socket. */
2440 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2441 if (sock < 0)
2442 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002443
Damien Miller95def091999-11-25 00:26:21 +11002444 /* Bind it to the name. */
2445 memset(&sunaddr, 0, sizeof(sunaddr));
2446 sunaddr.sun_family = AF_UNIX;
2447 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
2448 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002449
Damien Miller95def091999-11-25 00:26:21 +11002450 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2451 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002452
Damien Miller95def091999-11-25 00:26:21 +11002453 /* Restore the privileged uid. */
2454 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002455
Damien Miller95def091999-11-25 00:26:21 +11002456 /* Start listening on the socket. */
2457 if (listen(sock, 5) < 0)
2458 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002459
Damien Miller95def091999-11-25 00:26:21 +11002460 /* Allocate a channel for the authentication agent socket. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002461 newch = channel_new("auth socket",
2462 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2463 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2464 0, xstrdup("auth socket"), 1);
2465
Damien Miller037a0dc1999-12-07 15:38:31 +11002466 strlcpy(channels[newch].path, channel_forwarded_auth_socket_name,
2467 sizeof(channels[newch].path));
Damien Millerd3a18572000-06-07 19:55:44 +10002468 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002469}
2470
2471/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2472
Damien Miller4af51302000-04-16 11:18:38 +10002473void
Damien Miller62cee002000-09-23 17:15:56 +11002474auth_input_open_request(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002475{
Damien Miller95def091999-11-25 00:26:21 +11002476 int remch, sock, newch;
2477 char *dummyname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002478
Damien Millerb38eff82000-04-01 11:09:21 +10002479 packet_integrity_check(plen, 4, type);
2480
Damien Miller95def091999-11-25 00:26:21 +11002481 /* Read the remote channel number from the message. */
2482 remch = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002483
Damien Miller5428f641999-11-25 11:54:57 +11002484 /*
2485 * Get a connection to the local authentication agent (this may again
2486 * get forwarded).
2487 */
Damien Miller95def091999-11-25 00:26:21 +11002488 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002489
Damien Miller5428f641999-11-25 11:54:57 +11002490 /*
2491 * If we could not connect the agent, send an error message back to
2492 * the server. This should never happen unless the agent dies,
2493 * because authentication forwarding is only enabled if we have an
2494 * agent.
2495 */
Damien Miller95def091999-11-25 00:26:21 +11002496 if (sock < 0) {
2497 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2498 packet_put_int(remch);
2499 packet_send();
2500 return;
2501 }
2502 debug("Forwarding authentication connection.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002503
Damien Miller5428f641999-11-25 11:54:57 +11002504 /*
2505 * Dummy host name. This will be freed when the channel is freed; it
2506 * will still be valid in the packet_put_string below since the
2507 * channel cannot yet be freed at that point.
2508 */
Damien Miller95def091999-11-25 00:26:21 +11002509 dummyname = xstrdup("authentication agent connection");
2510
2511 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);
2512 channels[newch].remote_id = remch;
2513
2514 /* Send a confirmation to the remote host. */
2515 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2516 packet_put_int(remch);
2517 packet_put_int(newch);
2518 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002519}
Damien Miller33b13562000-04-04 14:38:59 +10002520
2521void
Damien Millerbd483e72000-04-30 10:00:53 +10002522channel_start_open(int id)
Damien Miller33b13562000-04-04 14:38:59 +10002523{
2524 Channel *c = channel_lookup(id);
2525 if (c == NULL) {
2526 log("channel_open: %d: bad id", id);
2527 return;
2528 }
Damien Millerbd483e72000-04-30 10:00:53 +10002529 debug("send channel open %d", id);
Damien Miller33b13562000-04-04 14:38:59 +10002530 packet_start(SSH2_MSG_CHANNEL_OPEN);
2531 packet_put_cstring(c->ctype);
2532 packet_put_int(c->self);
2533 packet_put_int(c->local_window);
2534 packet_put_int(c->local_maxpacket);
Damien Millerbd483e72000-04-30 10:00:53 +10002535}
2536void
2537channel_open(int id)
2538{
2539 /* XXX REMOVE ME */
2540 channel_start_open(id);
Damien Miller33b13562000-04-04 14:38:59 +10002541 packet_send();
Damien Miller33b13562000-04-04 14:38:59 +10002542}
2543void
2544channel_request(int id, char *service, int wantconfirm)
2545{
2546 channel_request_start(id, service, wantconfirm);
2547 packet_send();
2548 debug("channel request %d: %s", id, service) ;
2549}
2550void
2551channel_request_start(int id, char *service, int wantconfirm)
2552{
2553 Channel *c = channel_lookup(id);
2554 if (c == NULL) {
2555 log("channel_request: %d: bad id", id);
2556 return;
2557 }
2558 packet_start(SSH2_MSG_CHANNEL_REQUEST);
2559 packet_put_int(c->remote_id);
2560 packet_put_cstring(service);
2561 packet_put_char(wantconfirm);
2562}
2563void
2564channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
2565{
2566 Channel *c = channel_lookup(id);
2567 if (c == NULL) {
2568 log("channel_register_callback: %d: bad id", id);
2569 return;
2570 }
2571 c->cb_event = mtype;
2572 c->cb_fn = fn;
2573 c->cb_arg = arg;
2574}
2575void
2576channel_register_cleanup(int id, channel_callback_fn *fn)
2577{
2578 Channel *c = channel_lookup(id);
2579 if (c == NULL) {
2580 log("channel_register_cleanup: %d: bad id", id);
2581 return;
2582 }
2583 c->dettach_user = fn;
2584}
2585void
2586channel_cancel_cleanup(int id)
2587{
2588 Channel *c = channel_lookup(id);
2589 if (c == NULL) {
2590 log("channel_cancel_cleanup: %d: bad id", id);
2591 return;
2592 }
2593 c->dettach_user = NULL;
2594}
Kevin Stevesef4eea92001-02-05 12:42:17 +00002595void
Damien Millerad833b32000-08-23 10:46:23 +10002596channel_register_filter(int id, channel_filter_fn *fn)
2597{
2598 Channel *c = channel_lookup(id);
2599 if (c == NULL) {
2600 log("channel_register_filter: %d: bad id", id);
2601 return;
2602 }
2603 c->input_filter = fn;
2604}
Damien Miller33b13562000-04-04 14:38:59 +10002605
2606void
Damien Miller69b69aa2000-10-28 14:19:58 +11002607channel_set_fds(int id, int rfd, int wfd, int efd,
2608 int extusage, int nonblock)
Damien Miller33b13562000-04-04 14:38:59 +10002609{
2610 Channel *c = channel_lookup(id);
2611 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
2612 fatal("channel_activate for non-larval channel %d.", id);
Damien Miller69b69aa2000-10-28 14:19:58 +11002613 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller33b13562000-04-04 14:38:59 +10002614 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002615 /* XXX window size? */
Damien Millere4340be2000-09-16 13:29:08 +11002616 c->local_window = c->local_window_max = c->local_maxpacket * 2;
Damien Miller33b13562000-04-04 14:38:59 +10002617 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2618 packet_put_int(c->remote_id);
2619 packet_put_int(c->local_window);
2620 packet_send();
2621}