blob: 503a814500934a5caba286e4bf822d324b5ee233 [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 Lindstromc8cb8c02001-05-04 22:40:28 +000043RCSID("$OpenBSD: channels.c,v 1.112 2001/05/04 14:34:34 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"
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +000054#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "log.h"
57#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058#include "channels.h"
59#include "nchan.h"
60#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000061#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100062#include "key.h"
63#include "authfd.h"
64
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065/* Maximum number of fake X11 displays to try. */
66#define MAX_DISPLAYS 1000
67
68/* Max len of agent socket */
69#define MAX_SOCKET_NAME 100
70
Damien Miller5428f641999-11-25 11:54:57 +110071/*
72 * Pointer to an array containing all allocated channels. The array is
73 * dynamically extended as needed.
74 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075static Channel *channels = NULL;
76
Damien Miller5428f641999-11-25 11:54:57 +110077/*
78 * Size of the channel array. All slots of the array must always be
79 * initialized (at least the type field); unused slots are marked with type
80 * SSH_CHANNEL_FREE.
81 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082static int channels_alloc = 0;
83
Damien Miller5428f641999-11-25 11:54:57 +110084/*
85 * Maximum file descriptor value used in any of the channels. This is
86 * updated in channel_allocate.
87 */
Damien Miller5e953212001-01-30 09:14:00 +110088static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
90/* Name and directory of socket for authentication agent forwarding. */
91static char *channel_forwarded_auth_socket_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +110092static char *channel_forwarded_auth_socket_dir = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
94/* Saved X11 authentication protocol name. */
95char *x11_saved_proto = NULL;
96
97/* Saved X11 authentication data. This is the real data. */
98char *x11_saved_data = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +000099u_int x11_saved_data_len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
Damien Miller5428f641999-11-25 11:54:57 +1100101/*
102 * Fake X11 authentication data. This is what the server will be sending us;
103 * we should replace any occurrences of this by the real data.
104 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105char *x11_fake_data = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000106u_int x11_fake_data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
Damien Miller5428f641999-11-25 11:54:57 +1100108/*
109 * Data structure for storing which hosts are permitted for forward requests.
110 * The local sides of any remote forwards are stored in this array to prevent
111 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
112 * network (which might be behind a firewall).
113 */
Damien Miller95def091999-11-25 00:26:21 +1100114typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000115 char *host_to_connect; /* Connect to 'host'. */
116 u_short port_to_connect; /* Connect to 'port'. */
117 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118} ForwardPermission;
119
120/* List of all permitted host/port pairs to connect. */
121static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
122/* Number of permitted host/port pairs in the array. */
123static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +1100124/*
125 * If this is true, all opens are permitted. This is the case on the server
126 * on which we have to trust the client anyway, and the user could do
127 * anything after logging in anyway.
128 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129static int all_opens_permitted = 0;
130
131/* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */
132static int have_hostname_in_open = 0;
133
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000134/* AF_UNSPEC or AF_INET or AF_INET6 */
135extern int IPv4or6;
136
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000137void port_open_helper(Channel *c, char *rtype);
138
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139/* Sets specific protocol options. */
140
Damien Miller4af51302000-04-16 11:18:38 +1000141void
Damien Miller95def091999-11-25 00:26:21 +1100142channel_set_options(int hostname_in_open)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143{
Damien Miller95def091999-11-25 00:26:21 +1100144 have_hostname_in_open = hostname_in_open;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145}
146
Damien Millerb38eff82000-04-01 11:09:21 +1000147/* lookup channel by id */
148
149Channel *
150channel_lookup(int id)
151{
152 Channel *c;
Damien Millerc0fd17f2000-06-26 10:22:53 +1000153 if (id < 0 || id > channels_alloc) {
Damien Millerb38eff82000-04-01 11:09:21 +1000154 log("channel_lookup: %d: bad id", id);
155 return NULL;
156 }
157 c = &channels[id];
158 if (c->type == SSH_CHANNEL_FREE) {
159 log("channel_lookup: %d: bad id: channel free", id);
160 return NULL;
161 }
162 return c;
163}
164
Damien Miller5428f641999-11-25 11:54:57 +1100165/*
Damien Millere247cc42000-05-07 12:03:14 +1000166 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000167 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100168 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169
Damien Miller6f83b8e2000-05-02 09:23:45 +1000170void
Damien Miller69b69aa2000-10-28 14:19:58 +1100171channel_register_fds(Channel *c, int rfd, int wfd, int efd,
172 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173{
Damien Miller95def091999-11-25 00:26:21 +1100174 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100175 channel_max_fd = MAX(channel_max_fd, rfd);
176 channel_max_fd = MAX(channel_max_fd, wfd);
177 channel_max_fd = MAX(channel_max_fd, efd);
178
Damien Miller5428f641999-11-25 11:54:57 +1100179 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000180
Damien Miller6f83b8e2000-05-02 09:23:45 +1000181 c->rfd = rfd;
182 c->wfd = wfd;
183 c->sock = (rfd == wfd) ? rfd : -1;
184 c->efd = efd;
185 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100186
Damien Miller79438cc2001-02-16 12:34:57 +1100187 /* XXX ugly hack: nonblock is only set by the server */
188 if (nonblock && isatty(c->rfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000189 debug("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100190 c->isatty = 1;
191 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000192 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100193 c->self, c->wfd);
194 }
195 } else {
196 c->isatty = 0;
197 }
198
Damien Miller69b69aa2000-10-28 14:19:58 +1100199 /* enable nonblocking mode */
200 if (nonblock) {
201 if (rfd != -1)
202 set_nonblock(rfd);
203 if (wfd != -1)
204 set_nonblock(wfd);
205 if (efd != -1)
206 set_nonblock(efd);
207 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000208}
209
210/*
211 * Allocate a new channel object and set its type and socket. This will cause
212 * remote_name to be freed.
213 */
214
215int
216channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Damien Miller69b69aa2000-10-28 14:19:58 +1100217 int window, int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000218{
219 int i, found;
220 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221
Damien Miller95def091999-11-25 00:26:21 +1100222 /* Do initial allocation if this is the first call. */
223 if (channels_alloc == 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000224 chan_init();
Damien Miller95def091999-11-25 00:26:21 +1100225 channels_alloc = 10;
226 channels = xmalloc(channels_alloc * sizeof(Channel));
227 for (i = 0; i < channels_alloc; i++)
228 channels[i].type = SSH_CHANNEL_FREE;
Damien Miller5428f641999-11-25 11:54:57 +1100229 /*
230 * Kludge: arrange a call to channel_stop_listening if we
231 * terminate with fatal().
232 */
Damien Miller95def091999-11-25 00:26:21 +1100233 fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL);
234 }
235 /* Try to find a free slot where to put the new channel. */
236 for (found = -1, i = 0; i < channels_alloc; i++)
237 if (channels[i].type == SSH_CHANNEL_FREE) {
238 /* Found a free slot. */
239 found = i;
240 break;
241 }
242 if (found == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100243 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100244 found = channels_alloc;
245 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100246 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100247 channels = xrealloc(channels, channels_alloc * sizeof(Channel));
248 for (i = found; i < channels_alloc; i++)
249 channels[i].type = SSH_CHANNEL_FREE;
250 }
251 /* Initialize and return new channel number. */
252 c = &channels[found];
253 buffer_init(&c->input);
254 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000255 buffer_init(&c->extended);
Damien Miller95def091999-11-25 00:26:21 +1100256 chan_init_iostates(c);
Damien Miller69b69aa2000-10-28 14:19:58 +1100257 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100258 c->self = found;
259 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000260 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000261 c->local_window = window;
262 c->local_window_max = window;
263 c->local_consumed = 0;
264 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100265 c->remote_id = -1;
266 c->remote_name = remote_name;
Damien Millerb38eff82000-04-01 11:09:21 +1000267 c->remote_window = 0;
268 c->remote_maxpacket = 0;
269 c->cb_fn = NULL;
270 c->cb_arg = NULL;
271 c->cb_event = 0;
272 c->dettach_user = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000273 c->input_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100274 debug("channel %d: new [%s]", found, remote_name);
275 return found;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000277/* old interface XXX */
Damien Miller4af51302000-04-16 11:18:38 +1000278int
Damien Millerb38eff82000-04-01 11:09:21 +1000279channel_allocate(int type, int sock, char *remote_name)
280{
Damien Miller69b69aa2000-10-28 14:19:58 +1100281 return channel_new("", type, sock, sock, -1, 0, 0, 0, remote_name, 1);
Damien Millerb38eff82000-04-01 11:09:21 +1000282}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283
Damien Miller6f83b8e2000-05-02 09:23:45 +1000284
285/* Close all channel fd/socket. */
286
287void
288channel_close_fds(Channel *c)
289{
290 if (c->sock != -1) {
291 close(c->sock);
292 c->sock = -1;
293 }
294 if (c->rfd != -1) {
295 close(c->rfd);
296 c->rfd = -1;
297 }
298 if (c->wfd != -1) {
299 close(c->wfd);
300 c->wfd = -1;
301 }
302 if (c->efd != -1) {
303 close(c->efd);
304 c->efd = -1;
305 }
306}
307
308/* Free the channel and close its fd/socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309
Damien Miller4af51302000-04-16 11:18:38 +1000310void
Damien Millerb38eff82000-04-01 11:09:21 +1000311channel_free(int id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312{
Damien Millerb38eff82000-04-01 11:09:21 +1000313 Channel *c = channel_lookup(id);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000314 char *s = channel_open_message();
315
Damien Millerb38eff82000-04-01 11:09:21 +1000316 if (c == NULL)
317 packet_disconnect("channel free: bad local channel %d", id);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000318 debug("channel_free: channel %d: status: %s", id, s);
319 xfree(s);
320
Damien Miller33b13562000-04-04 14:38:59 +1000321 if (c->dettach_user != NULL) {
322 debug("channel_free: channel %d: dettaching channel user", id);
323 c->dettach_user(c->self, NULL);
324 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000325 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000326 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000327 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000328 buffer_free(&c->input);
329 buffer_free(&c->output);
330 buffer_free(&c->extended);
331 c->type = SSH_CHANNEL_FREE;
332 if (c->remote_name) {
333 xfree(c->remote_name);
334 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100335 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336}
337
Damien Miller5428f641999-11-25 11:54:57 +1100338/*
Damien Millerb38eff82000-04-01 11:09:21 +1000339 * 'channel_pre*' are called just before select() to add any bits relevant to
340 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100341 */
Damien Millerb38eff82000-04-01 11:09:21 +1000342/*
343 * 'channel_post*': perform any appropriate operations for channels which
344 * have events pending.
345 */
346typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
347chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
348chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
349
350void
351channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
352{
353 FD_SET(c->sock, readset);
354}
355
356void
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000357channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
358{
359 debug3("channel %d: waiting for connection", c->self);
360 FD_SET(c->sock, writeset);
361}
362
363void
Damien Millerb38eff82000-04-01 11:09:21 +1000364channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
365{
366 if (buffer_len(&c->input) < packet_get_maxsize())
367 FD_SET(c->sock, readset);
368 if (buffer_len(&c->output) > 0)
369 FD_SET(c->sock, writeset);
370}
371
372void
373channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
374{
375 /* test whether sockets are 'alive' for read/write */
376 if (c->istate == CHAN_INPUT_OPEN)
377 if (buffer_len(&c->input) < packet_get_maxsize())
378 FD_SET(c->sock, readset);
379 if (c->ostate == CHAN_OUTPUT_OPEN ||
380 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
381 if (buffer_len(&c->output) > 0) {
382 FD_SET(c->sock, writeset);
383 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
384 chan_obuf_empty(c);
385 }
386 }
387}
388
389void
Damien Miller33b13562000-04-04 14:38:59 +1000390channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
391{
392 if (c->istate == CHAN_INPUT_OPEN &&
393 c->remote_window > 0 &&
394 buffer_len(&c->input) < c->remote_window)
395 FD_SET(c->rfd, readset);
396 if (c->ostate == CHAN_OUTPUT_OPEN ||
397 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
398 if (buffer_len(&c->output) > 0) {
399 FD_SET(c->wfd, writeset);
400 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
401 chan_obuf_empty(c);
402 }
403 }
404 /** XXX check close conditions, too */
405 if (c->efd != -1) {
406 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
407 buffer_len(&c->extended) > 0)
408 FD_SET(c->efd, writeset);
409 else if (c->extended_usage == CHAN_EXTENDED_READ &&
410 buffer_len(&c->extended) < c->remote_window)
411 FD_SET(c->efd, readset);
412 }
413}
414
415void
Damien Millerb38eff82000-04-01 11:09:21 +1000416channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
417{
418 if (buffer_len(&c->input) == 0) {
419 packet_start(SSH_MSG_CHANNEL_CLOSE);
420 packet_put_int(c->remote_id);
421 packet_send();
422 c->type = SSH_CHANNEL_CLOSED;
Ben Lindstromc486d882001-04-11 16:08:34 +0000423 debug("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000424 }
425}
426
427void
428channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
429{
430 if (buffer_len(&c->output) == 0)
431 channel_free(c->self);
Damien Miller4af51302000-04-16 11:18:38 +1000432 else
Damien Millerb38eff82000-04-01 11:09:21 +1000433 FD_SET(c->sock, writeset);
434}
435
436/*
437 * This is a special state for X11 authentication spoofing. An opened X11
438 * connection (when authentication spoofing is being done) remains in this
439 * state until the first packet has been completely read. The authentication
440 * data in that packet is then substituted by the real data if it matches the
441 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000442 * XXX All this happens at the client side.
Damien Millerb38eff82000-04-01 11:09:21 +1000443 */
444int
445x11_open_helper(Channel *c)
446{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000447 u_char *ucp;
448 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000449
450 /* Check if the fixed size part of the packet is in buffer. */
451 if (buffer_len(&c->output) < 12)
452 return 0;
453
454 /* Parse the lengths of variable-length fields. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000455 ucp = (u_char *) buffer_ptr(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000456 if (ucp[0] == 0x42) { /* Byte order MSB first. */
457 proto_len = 256 * ucp[6] + ucp[7];
458 data_len = 256 * ucp[8] + ucp[9];
459 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
460 proto_len = ucp[6] + 256 * ucp[7];
461 data_len = ucp[8] + 256 * ucp[9];
462 } else {
463 debug("Initial X11 packet contains bad byte order byte: 0x%x",
464 ucp[0]);
465 return -1;
466 }
467
468 /* Check if the whole packet is in buffer. */
469 if (buffer_len(&c->output) <
470 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
471 return 0;
472
473 /* Check if authentication protocol matches. */
474 if (proto_len != strlen(x11_saved_proto) ||
475 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
476 debug("X11 connection uses different authentication protocol.");
477 return -1;
478 }
479 /* Check if authentication data matches our fake data. */
480 if (data_len != x11_fake_data_len ||
481 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
482 x11_fake_data, x11_fake_data_len) != 0) {
483 debug("X11 auth data does not match fake data.");
484 return -1;
485 }
486 /* Check fake data length */
487 if (x11_fake_data_len != x11_saved_data_len) {
488 error("X11 fake_data_len %d != saved_data_len %d",
489 x11_fake_data_len, x11_saved_data_len);
490 return -1;
491 }
492 /*
493 * Received authentication protocol and data match
494 * our fake data. Substitute the fake data with real
495 * data.
496 */
497 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
498 x11_saved_data, x11_saved_data_len);
499 return 1;
500}
501
502void
503channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
504{
505 int ret = x11_open_helper(c);
506 if (ret == 1) {
507 /* Start normal processing for the channel. */
508 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000509 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000510 } else if (ret == -1) {
511 /*
512 * We have received an X11 connection that has bad
513 * authentication information.
514 */
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000515 log("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000516 buffer_clear(&c->input);
517 buffer_clear(&c->output);
518 close(c->sock);
519 c->sock = -1;
520 c->type = SSH_CHANNEL_CLOSED;
521 packet_start(SSH_MSG_CHANNEL_CLOSE);
522 packet_put_int(c->remote_id);
523 packet_send();
524 }
525}
526
527void
Damien Millerbd483e72000-04-30 10:00:53 +1000528channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000529{
530 int ret = x11_open_helper(c);
531 if (ret == 1) {
532 c->type = SSH_CHANNEL_OPEN;
Damien Miller30c3d422000-05-09 11:02:59 +1000533 if (compat20)
534 channel_pre_open_20(c, readset, writeset);
535 else
536 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000537 } else if (ret == -1) {
538 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerbd483e72000-04-30 10:00:53 +1000539 chan_read_failed(c); /** force close? */
Damien Millerb38eff82000-04-01 11:09:21 +1000540 chan_write_failed(c);
541 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
542 }
543}
544
Ben Lindstromb3921512001-04-11 15:57:50 +0000545/* try to decode a socks4 header */
546int
547channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000548{
549 u_char *p, *host;
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000550 int len, have, i, found;
Ben Lindstromb3921512001-04-11 15:57:50 +0000551 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000552 struct {
553 u_int8_t version;
554 u_int8_t command;
555 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000556 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000557 } s4_req, s4_rsp;
558
Ben Lindstromb3921512001-04-11 15:57:50 +0000559 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000560
561 have = buffer_len(&c->input);
562 len = sizeof(s4_req);
563 if (have < len)
564 return 0;
565 p = buffer_ptr(&c->input);
566 for (found = 0, i = len; i < have; i++) {
567 if (p[i] == '\0') {
568 found = 1;
569 break;
570 }
571 if (i > 1024) {
572 /* the peer is probably sending garbage */
573 debug("channel %d: decode socks4: too long",
574 c->self);
575 return -1;
576 }
577 }
578 if (!found)
579 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000580 buffer_get(&c->input, (char *)&s4_req.version, 1);
581 buffer_get(&c->input, (char *)&s4_req.command, 1);
582 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000583 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000584 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000585 p = buffer_ptr(&c->input);
586 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000587 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000588 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000589 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000590 c->self, len, have);
591 strlcpy(username, p, sizeof(username));
592 buffer_consume(&c->input, len);
593 buffer_consume(&c->input, 1); /* trailing '\0' */
594
Ben Lindstromb3921512001-04-11 15:57:50 +0000595 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000596 strlcpy(c->path, host, sizeof(c->path));
597 c->host_port = ntohs(s4_req.dest_port);
598
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000599 debug("channel %d: dynamic request: socks4 host %s port %u command %u",
600 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000601
Ben Lindstromb3921512001-04-11 15:57:50 +0000602 if (s4_req.command != 1) {
603 debug("channel %d: cannot handle: socks4 cn %d",
604 c->self, s4_req.command);
605 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000606 }
Ben Lindstromb3921512001-04-11 15:57:50 +0000607 s4_rsp.version = 0; /* vn: 0 for reply */
608 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000609 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +0000610 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000611 buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +0000612 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000613}
614
Ben Lindstromb3921512001-04-11 15:57:50 +0000615/* dynamic port forwarding */
616void
617channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
618{
619 u_char *p;
620 int have, ret;
621
622 have = buffer_len(&c->input);
623
624 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +0000625 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +0000626 /* check if the fixed size part of the packet is in buffer. */
627 if (have < 4) {
628 /* need more */
629 FD_SET(c->sock, readset);
630 return;
631 }
632 /* try to guess the protocol */
633 p = buffer_ptr(&c->input);
634 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000635 case 0x04:
636 ret = channel_decode_socks4(c, readset, writeset);
637 break;
Ben Lindstromb3921512001-04-11 15:57:50 +0000638 default:
639 ret = -1;
640 break;
641 }
642 if (ret < 0) {
643 channel_free(c->self);
644 } else if (ret == 0) {
645 debug2("channel %d: pre_dynamic: need more", c->self);
646 /* need more */
647 FD_SET(c->sock, readset);
648 } else {
649 /* switch to the next state */
650 c->type = SSH_CHANNEL_OPENING;
651 port_open_helper(c, "direct-tcpip");
652 }
653}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000654
Damien Millerb38eff82000-04-01 11:09:21 +1000655/* This is our fake X11 server socket. */
656void
657channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
658{
659 struct sockaddr addr;
Ben Lindstromc8cb8c02001-05-04 22:40:28 +0000660 int newsock, newch, oldch;
Damien Millerb38eff82000-04-01 11:09:21 +1000661 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +1100662 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +1000663 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +1000664
665 if (FD_ISSET(c->sock, readset)) {
666 debug("X11 connection requested.");
667 addrlen = sizeof(addr);
668 newsock = accept(c->sock, &addr, &addrlen);
669 if (newsock < 0) {
670 error("accept: %.100s", strerror(errno));
671 return;
672 }
Damien Millerd83ff352001-01-30 09:19:34 +1100673 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +1000674 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +1000675 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +1100676 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +1000677
Ben Lindstromc8cb8c02001-05-04 22:40:28 +0000678 oldch = c->self;
Damien Millerbd483e72000-04-30 10:00:53 +1000679 newch = channel_new("x11",
680 SSH_CHANNEL_OPENING, newsock, newsock, -1,
681 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100682 0, xstrdup(buf), 1);
Ben Lindstromc8cb8c02001-05-04 22:40:28 +0000683 c = channel_lookup(oldch);
Damien Millerbd483e72000-04-30 10:00:53 +1000684 if (compat20) {
685 packet_start(SSH2_MSG_CHANNEL_OPEN);
686 packet_put_cstring("x11");
687 packet_put_int(newch);
688 packet_put_int(c->local_window_max);
689 packet_put_int(c->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +1100690 /* originator ipaddr and port */
691 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +1000692 if (datafellows & SSH_BUG_X11FWD) {
693 debug("ssh2 x11 bug compat mode");
694 } else {
695 packet_put_int(remote_port);
696 }
Damien Millerbd483e72000-04-30 10:00:53 +1000697 packet_send();
698 } else {
699 packet_start(SSH_SMSG_X11_OPEN);
700 packet_put_int(newch);
701 if (have_hostname_in_open)
702 packet_put_string(buf, strlen(buf));
703 packet_send();
704 }
Damien Millerd83ff352001-01-30 09:19:34 +1100705 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +1000706 }
707}
708
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000709void
710port_open_helper(Channel *c, char *rtype)
711{
712 int direct;
713 char buf[1024];
714 char *remote_ipaddr = get_peer_ipaddr(c->sock);
715 u_short remote_port = get_peer_port(c->sock);
716
717 direct = (strcmp(rtype, "direct-tcpip") == 0);
718
719 snprintf(buf, sizeof buf,
720 "%s: listening port %d for %.100s port %d, "
721 "connect from %.200s port %d",
722 rtype, c->listening_port, c->path, c->host_port,
723 remote_ipaddr, remote_port);
724
725 xfree(c->remote_name);
726 c->remote_name = xstrdup(buf);
727
728 if (compat20) {
729 packet_start(SSH2_MSG_CHANNEL_OPEN);
730 packet_put_cstring(rtype);
731 packet_put_int(c->self);
732 packet_put_int(c->local_window_max);
733 packet_put_int(c->local_maxpacket);
734 if (direct) {
735 /* target host, port */
736 packet_put_cstring(c->path);
737 packet_put_int(c->host_port);
738 } else {
739 /* listen address, port */
740 packet_put_cstring(c->path);
741 packet_put_int(c->listening_port);
742 }
743 /* originator host and port */
744 packet_put_cstring(remote_ipaddr);
745 packet_put_int(remote_port);
746 packet_send();
747 } else {
748 packet_start(SSH_MSG_PORT_OPEN);
749 packet_put_int(c->self);
750 packet_put_cstring(c->path);
751 packet_put_int(c->host_port);
752 if (have_hostname_in_open)
753 packet_put_cstring(c->remote_name);
754 packet_send();
755 }
756 xfree(remote_ipaddr);
757}
758
Damien Millerb38eff82000-04-01 11:09:21 +1000759/*
760 * This socket is listening for connections to a forwarded TCP/IP port.
761 */
762void
763channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
764{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000765 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +1000766 struct sockaddr addr;
Ben Lindstromc8cb8c02001-05-04 22:40:28 +0000767 int newsock, newch, nextstate, oldch;
Damien Millerb38eff82000-04-01 11:09:21 +1000768 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000769 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100770
Damien Millerb38eff82000-04-01 11:09:21 +1000771 if (FD_ISSET(c->sock, readset)) {
772 debug("Connection to port %d forwarding "
773 "to %.100s port %d requested.",
774 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000775
776 rtype = (c->type == SSH_CHANNEL_RPORT_LISTENER) ?
777 "forwarded-tcpip" : "direct-tcpip";
778 nextstate = (c->host_port == 0) ? SSH_CHANNEL_DYNAMIC :
779 SSH_CHANNEL_OPENING;
780
Damien Millerb38eff82000-04-01 11:09:21 +1000781 addrlen = sizeof(addr);
782 newsock = accept(c->sock, &addr, &addrlen);
783 if (newsock < 0) {
784 error("accept: %.100s", strerror(errno));
785 return;
786 }
Ben Lindstromc8cb8c02001-05-04 22:40:28 +0000787 oldch = c->self;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100788 newch = channel_new(rtype,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000789 nextstate, newsock, newsock, -1,
Damien Millerb38eff82000-04-01 11:09:21 +1000790 c->local_window_max, c->local_maxpacket,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000791 0, xstrdup(rtype), 1);
792
Ben Lindstromc8cb8c02001-05-04 22:40:28 +0000793 c = channel_lookup(oldch);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000794 nc = channel_lookup(newch);
795 if (nc == NULL) {
796 error("xxx: no new channel:");
797 return;
Damien Millerb38eff82000-04-01 11:09:21 +1000798 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000799 nc->listening_port = c->listening_port;
800 nc->host_port = c->host_port;
801 strlcpy(nc->path, c->path, sizeof(nc->path));
802
803 if (nextstate != SSH_CHANNEL_DYNAMIC)
804 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +1000805 }
806}
807
808/*
809 * This is the authentication agent socket listening for connections from
810 * clients.
811 */
812void
813channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
814{
815 struct sockaddr addr;
Ben Lindstromc8cb8c02001-05-04 22:40:28 +0000816 int newsock, newch, oldch;
Damien Millerb38eff82000-04-01 11:09:21 +1000817 socklen_t addrlen;
818
819 if (FD_ISSET(c->sock, readset)) {
820 addrlen = sizeof(addr);
821 newsock = accept(c->sock, &addr, &addrlen);
822 if (newsock < 0) {
823 error("accept from auth socket: %.100s", strerror(errno));
824 return;
825 }
Ben Lindstromc8cb8c02001-05-04 22:40:28 +0000826 oldch = c->self;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100827 newch = channel_new("accepted auth socket",
828 SSH_CHANNEL_OPENING, newsock, newsock, -1,
829 c->local_window_max, c->local_maxpacket,
830 0, xstrdup("accepted auth socket"), 1);
Ben Lindstromc8cb8c02001-05-04 22:40:28 +0000831 c = channel_lookup(oldch);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100832 if (compat20) {
833 packet_start(SSH2_MSG_CHANNEL_OPEN);
834 packet_put_cstring("auth-agent@openssh.com");
835 packet_put_int(newch);
836 packet_put_int(c->local_window_max);
837 packet_put_int(c->local_maxpacket);
838 } else {
839 packet_start(SSH_SMSG_AGENT_OPEN);
840 packet_put_int(newch);
841 }
Damien Millerb38eff82000-04-01 11:09:21 +1000842 packet_send();
843 }
844}
845
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000846void
847channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
848{
849 if (FD_ISSET(c->sock, writeset)) {
850 int err = 0;
851 int sz = sizeof(err);
852 c->type = SSH_CHANNEL_OPEN;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000853 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err, &sz) < 0) {
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000854 debug("getsockopt SO_ERROR failed");
855 } else {
856 if (err == 0) {
Ben Lindstrom2b451802001-05-03 22:35:32 +0000857 debug("channel %d: connected", c->self);
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000858 } else {
859 debug("channel %d: not connected: %s",
860 c->self, strerror(err));
861 chan_read_failed(c);
862 chan_write_failed(c);
863 }
864 }
865 }
866}
867
Damien Millerb38eff82000-04-01 11:09:21 +1000868int
869channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
870{
871 char buf[16*1024];
872 int len;
873
874 if (c->rfd != -1 &&
875 FD_ISSET(c->rfd, readset)) {
876 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +1000877 if (len < 0 && (errno == EINTR || errno == EAGAIN))
878 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000879 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +1000880 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +1000881 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +0000882 if (c->type != SSH_CHANNEL_OPEN) {
883 debug("channel %d: not open", c->self);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000884 channel_free(c->self);
885 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +0000886 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000887 buffer_consume(&c->output, buffer_len(&c->output));
888 c->type = SSH_CHANNEL_INPUT_DRAINING;
Ben Lindstromc486d882001-04-11 16:08:34 +0000889 debug("channel %d: status set to input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000890 } else {
891 chan_read_failed(c);
892 }
893 return -1;
894 }
Damien Millerad833b32000-08-23 10:46:23 +1000895 if(c->input_filter != NULL) {
896 if (c->input_filter(c, buf, len) == -1) {
Ben Lindstromc486d882001-04-11 16:08:34 +0000897 debug("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +1000898 chan_read_failed(c);
899 }
900 } else {
901 buffer_append(&c->input, buf, len);
902 }
Damien Millerb38eff82000-04-01 11:09:21 +1000903 }
904 return 1;
905}
906int
907channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
908{
Ben Lindstrome229b252001-03-05 06:28:06 +0000909 struct termios tio;
Damien Millerb38eff82000-04-01 11:09:21 +1000910 int len;
911
912 /* Send buffered output data to the socket. */
913 if (c->wfd != -1 &&
914 FD_ISSET(c->wfd, writeset) &&
915 buffer_len(&c->output) > 0) {
916 len = write(c->wfd, buffer_ptr(&c->output),
Damien Miller6f83b8e2000-05-02 09:23:45 +1000917 buffer_len(&c->output));
918 if (len < 0 && (errno == EINTR || errno == EAGAIN))
919 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000920 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +0000921 if (c->type != SSH_CHANNEL_OPEN) {
922 debug("channel %d: not open", c->self);
923 channel_free(c->self);
924 return -1;
925 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000926 buffer_consume(&c->output, buffer_len(&c->output));
Ben Lindstromc486d882001-04-11 16:08:34 +0000927 debug("channel %d: status set to input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000928 c->type = SSH_CHANNEL_INPUT_DRAINING;
929 } else {
930 chan_write_failed(c);
931 }
932 return -1;
933 }
Damien Miller79438cc2001-02-16 12:34:57 +1100934 if (compat20 && c->isatty) {
Damien Miller79438cc2001-02-16 12:34:57 +1100935 if (tcgetattr(c->wfd, &tio) == 0 &&
936 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
937 /*
938 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +0000939 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +0000940 * size of a SSH2_MSG_CHANNEL_DATA message
941 * (4 byte channel id + data)
Damien Miller79438cc2001-02-16 12:34:57 +1100942 */
Ben Lindstrome229b252001-03-05 06:28:06 +0000943 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +1100944 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +1100945 }
946 }
Damien Millerb38eff82000-04-01 11:09:21 +1000947 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +1000948 if (compat20 && len > 0) {
949 c->local_consumed += len;
950 }
951 }
952 return 1;
953}
954int
955channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
956{
957 char buf[16*1024];
958 int len;
959
Damien Miller1383bd82000-04-06 12:32:37 +1000960/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +1000961 if (c->efd != -1) {
962 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
963 FD_ISSET(c->efd, writeset) &&
964 buffer_len(&c->extended) > 0) {
965 len = write(c->efd, buffer_ptr(&c->extended),
966 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +1100967 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000968 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000969 if (len < 0 && (errno == EINTR || errno == EAGAIN))
970 return 1;
971 if (len <= 0) {
972 debug2("channel %d: closing write-efd %d",
973 c->self, c->efd);
974 close(c->efd);
975 c->efd = -1;
976 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000977 buffer_consume(&c->extended, len);
978 c->local_consumed += len;
979 }
980 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
981 FD_ISSET(c->efd, readset)) {
982 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +1100983 debug2("channel %d: read %d from efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000984 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000985 if (len < 0 && (errno == EINTR || errno == EAGAIN))
986 return 1;
987 if (len <= 0) {
988 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +1000989 c->self, c->efd);
990 close(c->efd);
991 c->efd = -1;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000992 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000993 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000994 }
Damien Miller33b13562000-04-04 14:38:59 +1000995 }
996 }
997 return 1;
998}
999int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001000channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001001{
Ben Lindstromb3921512001-04-11 15:57:50 +00001002 if (c->type == SSH_CHANNEL_OPEN &&
1003 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001004 c->local_window < c->local_window_max/2 &&
1005 c->local_consumed > 0) {
1006 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1007 packet_put_int(c->remote_id);
1008 packet_put_int(c->local_consumed);
1009 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001010 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001011 c->self, c->local_window,
1012 c->local_consumed);
1013 c->local_window += c->local_consumed;
1014 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001015 }
1016 return 1;
1017}
1018
1019void
1020channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
1021{
1022 channel_handle_rfd(c, readset, writeset);
1023 channel_handle_wfd(c, readset, writeset);
1024}
1025
1026void
Damien Miller33b13562000-04-04 14:38:59 +10001027channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
1028{
1029 channel_handle_rfd(c, readset, writeset);
1030 channel_handle_wfd(c, readset, writeset);
1031 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001032
1033 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001034}
1035
1036void
Damien Millerb38eff82000-04-01 11:09:21 +10001037channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1038{
1039 int len;
1040 /* Send buffered output data to the socket. */
1041 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1042 len = write(c->sock, buffer_ptr(&c->output),
1043 buffer_len(&c->output));
1044 if (len <= 0)
1045 buffer_consume(&c->output, buffer_len(&c->output));
1046 else
1047 buffer_consume(&c->output, len);
1048 }
1049}
1050
1051void
Damien Miller33b13562000-04-04 14:38:59 +10001052channel_handler_init_20(void)
1053{
1054 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +10001055 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001056 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001057 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001058 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001059 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001060 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001061 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001062
1063 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
1064 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001065 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001066 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001067 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001068 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001069 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_2;
Damien Miller33b13562000-04-04 14:38:59 +10001070}
1071
1072void
Damien Millerb38eff82000-04-01 11:09:21 +10001073channel_handler_init_13(void)
1074{
1075 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1076 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1077 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1078 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1079 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1080 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1081 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001082 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001083 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001084
1085 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
1086 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1087 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1088 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1089 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001090 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001091 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001092}
1093
1094void
1095channel_handler_init_15(void)
1096{
1097 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +10001098 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001099 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1100 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1101 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001102 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001103 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001104
1105 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1106 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1107 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1108 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001109 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001110 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001111}
1112
1113void
1114channel_handler_init(void)
1115{
1116 int i;
1117 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1118 channel_pre[i] = NULL;
1119 channel_post[i] = NULL;
1120 }
Damien Miller33b13562000-04-04 14:38:59 +10001121 if (compat20)
1122 channel_handler_init_20();
1123 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001124 channel_handler_init_13();
1125 else
1126 channel_handler_init_15();
1127}
1128
Damien Miller4af51302000-04-16 11:18:38 +10001129void
Damien Millerb38eff82000-04-01 11:09:21 +10001130channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1131{
1132 static int did_init = 0;
1133 int i;
1134 Channel *c;
1135
1136 if (!did_init) {
1137 channel_handler_init();
1138 did_init = 1;
1139 }
1140 for (i = 0; i < channels_alloc; i++) {
1141 c = &channels[i];
1142 if (c->type == SSH_CHANNEL_FREE)
1143 continue;
1144 if (ftab[c->type] == NULL)
1145 continue;
1146 (*ftab[c->type])(c, readset, writeset);
Ben Lindstromc8cb8c02001-05-04 22:40:28 +00001147 c = &channels[i]; /* XXX realloc */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001148 if (chan_is_dead(c)) {
1149 /*
1150 * we have to remove the fd's from the select mask
1151 * before the channels are free'd and the fd's are
1152 * closed
1153 */
1154 if (c->wfd != -1)
1155 FD_CLR(c->wfd, writeset);
1156 if (c->rfd != -1)
1157 FD_CLR(c->rfd, readset);
1158 if (c->efd != -1) {
1159 if (c->extended_usage == CHAN_EXTENDED_READ)
1160 FD_CLR(c->efd, readset);
1161 if (c->extended_usage == CHAN_EXTENDED_WRITE)
1162 FD_CLR(c->efd, writeset);
1163 }
1164 channel_free(c->self);
1165 }
Damien Millerb38eff82000-04-01 11:09:21 +10001166 }
1167}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001168
Damien Miller4af51302000-04-16 11:18:38 +10001169void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001170channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1171 int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001172{
Damien Miller5e953212001-01-30 09:14:00 +11001173 int n;
1174 u_int sz;
1175
1176 n = MAX(*maxfdp, channel_max_fd);
1177
1178 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1179 if (*readsetp == NULL || n > *maxfdp) {
1180 if (*readsetp)
1181 xfree(*readsetp);
1182 if (*writesetp)
1183 xfree(*writesetp);
1184 *readsetp = xmalloc(sz);
1185 *writesetp = xmalloc(sz);
1186 *maxfdp = n;
1187 }
1188 memset(*readsetp, 0, sz);
1189 memset(*writesetp, 0, sz);
1190
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001191 if (!rekeying)
1192 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193}
1194
Damien Miller4af51302000-04-16 11:18:38 +10001195void
Damien Miller95def091999-11-25 00:26:21 +11001196channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001197{
Damien Millerb38eff82000-04-01 11:09:21 +10001198 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001199}
1200
Damien Miller5e953212001-01-30 09:14:00 +11001201/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001202
Damien Miller4af51302000-04-16 11:18:38 +10001203void
Damien Miller95def091999-11-25 00:26:21 +11001204channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001205{
Damien Miller95def091999-11-25 00:26:21 +11001206 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001207 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001208
Damien Miller95def091999-11-25 00:26:21 +11001209 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001210 c = &channels[i];
Damien Miller34132e52000-01-14 15:45:46 +11001211
Damien Miller5428f641999-11-25 11:54:57 +11001212 /* We are only interested in channels that can have buffered incoming data. */
Damien Miller34132e52000-01-14 15:45:46 +11001213 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001214 if (c->type != SSH_CHANNEL_OPEN &&
1215 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001216 continue;
1217 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001218 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001219 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001220 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001221 if (compat20 &&
1222 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001223 /* XXX is this true? */
Ben Lindstromb1131e92001-03-05 07:27:13 +00001224 debug2("channel %d: no data after CLOSE", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001225 continue;
1226 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001227
Damien Miller95def091999-11-25 00:26:21 +11001228 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001229 if ((c->istate == CHAN_INPUT_OPEN ||
1230 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1231 (len = buffer_len(&c->input)) > 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001232 /* Send some data for the other side over the secure connection. */
Damien Miller33b13562000-04-04 14:38:59 +10001233 if (compat20) {
1234 if (len > c->remote_window)
1235 len = c->remote_window;
1236 if (len > c->remote_maxpacket)
1237 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001238 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001239 if (packet_is_interactive()) {
1240 if (len > 1024)
1241 len = 512;
1242 } else {
1243 /* Keep the packets at reasonable size. */
1244 if (len > packet_get_maxsize()/2)
1245 len = packet_get_maxsize()/2;
1246 }
Damien Miller95def091999-11-25 00:26:21 +11001247 }
Damien Millerb38eff82000-04-01 11:09:21 +10001248 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001249 packet_start(compat20 ?
1250 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001251 packet_put_int(c->remote_id);
1252 packet_put_string(buffer_ptr(&c->input), len);
1253 packet_send();
1254 buffer_consume(&c->input, len);
1255 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001256 }
1257 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001258 if (compat13)
1259 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001260 /*
1261 * input-buffer is empty and read-socket shutdown:
1262 * tell peer, that we will not send more data: send IEOF
1263 */
Damien Millerb38eff82000-04-01 11:09:21 +10001264 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001265 }
Damien Miller33b13562000-04-04 14:38:59 +10001266 /* Send extended data, i.e. stderr */
1267 if (compat20 &&
1268 c->remote_window > 0 &&
1269 (len = buffer_len(&c->extended)) > 0 &&
1270 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001271 debug2("channel %d: rwin %d elen %d euse %d",
1272 c->self, c->remote_window, buffer_len(&c->extended),
1273 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001274 if (len > c->remote_window)
1275 len = c->remote_window;
1276 if (len > c->remote_maxpacket)
1277 len = c->remote_maxpacket;
1278 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1279 packet_put_int(c->remote_id);
1280 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1281 packet_put_string(buffer_ptr(&c->extended), len);
1282 packet_send();
1283 buffer_consume(&c->extended, len);
1284 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001285 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001286 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001287 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001288}
1289
Damien Miller5428f641999-11-25 11:54:57 +11001290/*
1291 * This is called when a packet of type CHANNEL_DATA has just been received.
1292 * The message type has already been consumed, but channel number and data is
1293 * still there.
1294 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001295
Damien Miller4af51302000-04-16 11:18:38 +10001296void
Damien Miller62cee002000-09-23 17:15:56 +11001297channel_input_data(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001298{
Damien Miller34132e52000-01-14 15:45:46 +11001299 int id;
Damien Miller95def091999-11-25 00:26:21 +11001300 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001301 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001302 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001303
Damien Miller95def091999-11-25 00:26:21 +11001304 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001305 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001306 c = channel_lookup(id);
1307 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001308 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001309
Damien Miller95def091999-11-25 00:26:21 +11001310 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001311 if (c->type != SSH_CHANNEL_OPEN &&
1312 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001313 return;
1314
1315 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001316 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001317 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001318
Damien Miller95def091999-11-25 00:26:21 +11001319 /* Get the data. */
1320 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001321 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001322
Damien Miller33b13562000-04-04 14:38:59 +10001323 if (compat20){
1324 if (data_len > c->local_maxpacket) {
1325 log("channel %d: rcvd big packet %d, maxpack %d",
1326 c->self, data_len, c->local_maxpacket);
1327 }
1328 if (data_len > c->local_window) {
1329 log("channel %d: rcvd too much data %d, win %d",
1330 c->self, data_len, c->local_window);
1331 xfree(data);
1332 return;
1333 }
1334 c->local_window -= data_len;
1335 }else{
1336 packet_integrity_check(plen, 4 + 4 + data_len, type);
1337 }
Damien Millerb38eff82000-04-01 11:09:21 +10001338 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001339 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001340}
Damien Miller4af51302000-04-16 11:18:38 +10001341void
Damien Miller62cee002000-09-23 17:15:56 +11001342channel_input_extended_data(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001343{
1344 int id;
1345 int tcode;
1346 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001347 u_int data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001348 Channel *c;
1349
1350 /* Get the channel number and verify it. */
1351 id = packet_get_int();
1352 c = channel_lookup(id);
1353
1354 if (c == NULL)
1355 packet_disconnect("Received extended_data for bad channel %d.", id);
1356 if (c->type != SSH_CHANNEL_OPEN) {
1357 log("channel %d: ext data for non open", id);
1358 return;
1359 }
1360 tcode = packet_get_int();
1361 if (c->efd == -1 ||
1362 c->extended_usage != CHAN_EXTENDED_WRITE ||
1363 tcode != SSH2_EXTENDED_DATA_STDERR) {
1364 log("channel %d: bad ext data", c->self);
1365 return;
1366 }
1367 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001368 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001369 if (data_len > c->local_window) {
1370 log("channel %d: rcvd too much extended_data %d, win %d",
1371 c->self, data_len, c->local_window);
1372 xfree(data);
1373 return;
1374 }
Damien Millerd3444942000-09-30 14:20:03 +11001375 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001376 c->local_window -= data_len;
1377 buffer_append(&c->extended, data, data_len);
1378 xfree(data);
1379}
1380
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001381
Damien Miller5428f641999-11-25 11:54:57 +11001382/*
1383 * Returns true if no channel has too much buffered data, and false if one or
1384 * more channel is overfull.
1385 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001386
Damien Miller4af51302000-04-16 11:18:38 +10001387int
Damien Miller95def091999-11-25 00:26:21 +11001388channel_not_very_much_buffered_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001389{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001390 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001391 Channel *c;
Damien Miller95def091999-11-25 00:26:21 +11001392
1393 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001394 c = &channels[i];
1395 if (c->type == SSH_CHANNEL_OPEN) {
Damien Miller33b13562000-04-04 14:38:59 +10001396 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
Damien Millerb38eff82000-04-01 11:09:21 +10001397 debug("channel %d: big input buffer %d",
1398 c->self, buffer_len(&c->input));
Damien Miller95def091999-11-25 00:26:21 +11001399 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001400 }
1401 if (buffer_len(&c->output) > packet_get_maxsize()) {
1402 debug("channel %d: big output buffer %d",
1403 c->self, buffer_len(&c->output));
Damien Miller95def091999-11-25 00:26:21 +11001404 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001405 }
Damien Miller95def091999-11-25 00:26:21 +11001406 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001407 }
Damien Miller95def091999-11-25 00:26:21 +11001408 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001409}
1410
Damien Miller4af51302000-04-16 11:18:38 +10001411void
Damien Miller62cee002000-09-23 17:15:56 +11001412channel_input_ieof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001413{
1414 int id;
1415 Channel *c;
1416
1417 packet_integrity_check(plen, 4, type);
1418
1419 id = packet_get_int();
1420 c = channel_lookup(id);
1421 if (c == NULL)
1422 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1423 chan_rcvd_ieof(c);
1424}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001425
Damien Miller4af51302000-04-16 11:18:38 +10001426void
Damien Miller62cee002000-09-23 17:15:56 +11001427channel_input_close(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001428{
Damien Millerb38eff82000-04-01 11:09:21 +10001429 int id;
1430 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001431
Damien Millerb38eff82000-04-01 11:09:21 +10001432 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001433
Damien Millerb38eff82000-04-01 11:09:21 +10001434 id = packet_get_int();
1435 c = channel_lookup(id);
1436 if (c == NULL)
1437 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001438
1439 /*
1440 * Send a confirmation that we have closed the channel and no more
1441 * data is coming for it.
1442 */
Damien Miller95def091999-11-25 00:26:21 +11001443 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001444 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001445 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001446
Damien Miller5428f641999-11-25 11:54:57 +11001447 /*
1448 * If the channel is in closed state, we have sent a close request,
1449 * and the other side will eventually respond with a confirmation.
1450 * Thus, we cannot free the channel here, because then there would be
1451 * no-one to receive the confirmation. The channel gets freed when
1452 * the confirmation arrives.
1453 */
Damien Millerb38eff82000-04-01 11:09:21 +10001454 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001455 /*
1456 * Not a closed channel - mark it as draining, which will
1457 * cause it to be freed later.
1458 */
Damien Millerb38eff82000-04-01 11:09:21 +10001459 buffer_consume(&c->input, buffer_len(&c->input));
1460 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001461 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001462}
1463
Damien Millerb38eff82000-04-01 11:09:21 +10001464/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001465void
Damien Miller62cee002000-09-23 17:15:56 +11001466channel_input_oclose(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001467{
Damien Millerb38eff82000-04-01 11:09:21 +10001468 int id = packet_get_int();
1469 Channel *c = channel_lookup(id);
1470 packet_integrity_check(plen, 4, type);
1471 if (c == NULL)
1472 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1473 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001474}
1475
Damien Miller4af51302000-04-16 11:18:38 +10001476void
Damien Miller62cee002000-09-23 17:15:56 +11001477channel_input_close_confirmation(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001478{
1479 int id = packet_get_int();
1480 Channel *c = channel_lookup(id);
1481
Damien Miller4af51302000-04-16 11:18:38 +10001482 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001483 if (c == NULL)
1484 packet_disconnect("Received close confirmation for "
1485 "out-of-range channel %d.", id);
1486 if (c->type != SSH_CHANNEL_CLOSED)
1487 packet_disconnect("Received close confirmation for "
1488 "non-closed channel %d (type %d).", id, c->type);
1489 channel_free(c->self);
1490}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001491
Damien Miller4af51302000-04-16 11:18:38 +10001492void
Damien Miller62cee002000-09-23 17:15:56 +11001493channel_input_open_confirmation(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001494{
Damien Millerb38eff82000-04-01 11:09:21 +10001495 int id, remote_id;
1496 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001497
Damien Miller33b13562000-04-04 14:38:59 +10001498 if (!compat20)
1499 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001500
Damien Millerb38eff82000-04-01 11:09:21 +10001501 id = packet_get_int();
1502 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001503
Damien Millerb38eff82000-04-01 11:09:21 +10001504 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1505 packet_disconnect("Received open confirmation for "
1506 "non-opening channel %d.", id);
1507 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001508 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001509 c->remote_id = remote_id;
1510 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001511
1512 if (compat20) {
1513 c->remote_window = packet_get_int();
1514 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001515 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001516 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001517 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001518 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001519 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001520 }
1521 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1522 c->remote_window, c->remote_maxpacket);
1523 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001524}
1525
Damien Miller4af51302000-04-16 11:18:38 +10001526void
Damien Miller62cee002000-09-23 17:15:56 +11001527channel_input_open_failure(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001528{
Kevin Steves12057502001-02-05 14:54:34 +00001529 int id, reason;
1530 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001531 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001532
Damien Miller33b13562000-04-04 14:38:59 +10001533 if (!compat20)
1534 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001535
1536 id = packet_get_int();
1537 c = channel_lookup(id);
1538
1539 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1540 packet_disconnect("Received open failure for "
1541 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001542 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00001543 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00001544 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00001545 msg = packet_get_string(NULL);
1546 lang = packet_get_string(NULL);
1547 }
Damien Miller4af51302000-04-16 11:18:38 +10001548 packet_done();
Kevin Steves12057502001-02-05 14:54:34 +00001549 log("channel_open_failure: %d: reason %d %s", id,
1550 reason, msg ? msg : "<no additional info>");
1551 if (msg != NULL)
1552 xfree(msg);
1553 if (lang != NULL)
1554 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001555 }
Damien Miller95def091999-11-25 00:26:21 +11001556 /* Free the channel. This will also close the socket. */
Damien Millerb38eff82000-04-01 11:09:21 +10001557 channel_free(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001558}
1559
Damien Miller33b13562000-04-04 14:38:59 +10001560void
Damien Miller62cee002000-09-23 17:15:56 +11001561channel_input_channel_request(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001562{
1563 int id;
1564 Channel *c;
1565
1566 id = packet_get_int();
1567 c = channel_lookup(id);
1568
1569 if (c == NULL ||
1570 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1571 packet_disconnect("Received request for "
1572 "non-open channel %d.", id);
1573 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001574 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001575 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001576 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001577 } else {
1578 char *service = packet_get_string(NULL);
Ben Lindstromcc74df72001-03-05 06:20:14 +00001579 debug("channel %d: rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001580 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001581 xfree(service);
1582 }
1583}
1584
Damien Miller4af51302000-04-16 11:18:38 +10001585void
Damien Miller62cee002000-09-23 17:15:56 +11001586channel_input_window_adjust(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001587{
1588 Channel *c;
1589 int id, adjust;
1590
1591 if (!compat20)
1592 return;
1593
1594 /* Get the channel number and verify it. */
1595 id = packet_get_int();
1596 c = channel_lookup(id);
1597
1598 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1599 log("Received window adjust for "
1600 "non-open channel %d.", id);
1601 return;
1602 }
1603 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001604 packet_done();
Damien Millerd3444942000-09-30 14:20:03 +11001605 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10001606 c->remote_window += adjust;
1607}
1608
Damien Miller5428f641999-11-25 11:54:57 +11001609/*
1610 * Stops listening for channels, and removes any unix domain sockets that we
1611 * might have.
1612 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001613
Damien Miller4af51302000-04-16 11:18:38 +10001614void
Damien Miller95def091999-11-25 00:26:21 +11001615channel_stop_listening()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001616{
Damien Miller95def091999-11-25 00:26:21 +11001617 int i;
1618 for (i = 0; i < channels_alloc; i++) {
1619 switch (channels[i].type) {
1620 case SSH_CHANNEL_AUTH_SOCKET:
1621 close(channels[i].sock);
Damien Miller78315eb2000-09-29 23:01:36 +11001622 unlink(channels[i].path);
Damien Miller95def091999-11-25 00:26:21 +11001623 channel_free(i);
1624 break;
1625 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001626 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001627 case SSH_CHANNEL_X11_LISTENER:
1628 close(channels[i].sock);
1629 channel_free(i);
1630 break;
1631 default:
1632 break;
1633 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001634 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001635}
1636
Damien Miller5428f641999-11-25 11:54:57 +11001637/*
Damien Miller6f83b8e2000-05-02 09:23:45 +10001638 * Closes the sockets/fds of all channels. This is used to close extra file
Damien Miller5428f641999-11-25 11:54:57 +11001639 * descriptors after a fork.
1640 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001641
Damien Miller4af51302000-04-16 11:18:38 +10001642void
Damien Miller95def091999-11-25 00:26:21 +11001643channel_close_all()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001644{
Damien Miller95def091999-11-25 00:26:21 +11001645 int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +10001646 for (i = 0; i < channels_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001647 if (channels[i].type != SSH_CHANNEL_FREE)
Damien Miller6f83b8e2000-05-02 09:23:45 +10001648 channel_close_fds(&channels[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001649}
1650
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001651/* Returns true if any channel is still open. */
1652
Damien Miller4af51302000-04-16 11:18:38 +10001653int
Damien Miller95def091999-11-25 00:26:21 +11001654channel_still_open()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001655{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001656 u_int i;
Damien Miller95def091999-11-25 00:26:21 +11001657 for (i = 0; i < channels_alloc; i++)
1658 switch (channels[i].type) {
1659 case SSH_CHANNEL_FREE:
1660 case SSH_CHANNEL_X11_LISTENER:
1661 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001662 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001663 case SSH_CHANNEL_CLOSED:
1664 case SSH_CHANNEL_AUTH_SOCKET:
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001665 case SSH_CHANNEL_DYNAMIC:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001666 case SSH_CHANNEL_CONNECTING: /* XXX ??? */
Damien Miller95def091999-11-25 00:26:21 +11001667 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001668 case SSH_CHANNEL_LARVAL:
1669 if (!compat20)
1670 fatal("cannot happen: SSH_CHANNEL_LARVAL");
1671 continue;
Damien Miller95def091999-11-25 00:26:21 +11001672 case SSH_CHANNEL_OPENING:
1673 case SSH_CHANNEL_OPEN:
1674 case SSH_CHANNEL_X11_OPEN:
1675 return 1;
1676 case SSH_CHANNEL_INPUT_DRAINING:
1677 case SSH_CHANNEL_OUTPUT_DRAINING:
1678 if (!compat13)
1679 fatal("cannot happen: OUT_DRAIN");
1680 return 1;
1681 default:
1682 fatal("channel_still_open: bad channel type %d", channels[i].type);
1683 /* NOTREACHED */
1684 }
1685 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001686}
1687
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001688/* Returns the id of an open channel suitable for keepaliving */
1689
1690int
1691channel_find_open()
1692{
1693 u_int i;
1694 for (i = 0; i < channels_alloc; i++)
1695 switch (channels[i].type) {
1696 case SSH_CHANNEL_CLOSED:
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001697 case SSH_CHANNEL_DYNAMIC:
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001698 case SSH_CHANNEL_FREE:
1699 case SSH_CHANNEL_X11_LISTENER:
1700 case SSH_CHANNEL_PORT_LISTENER:
1701 case SSH_CHANNEL_RPORT_LISTENER:
1702 case SSH_CHANNEL_OPENING:
Ben Lindstromd334b272001-04-14 23:08:36 +00001703 continue;
1704 case SSH_CHANNEL_LARVAL:
1705 case SSH_CHANNEL_AUTH_SOCKET:
1706 case SSH_CHANNEL_CONNECTING: /* XXX ??? */
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001707 case SSH_CHANNEL_OPEN:
1708 case SSH_CHANNEL_X11_OPEN:
1709 return i;
1710 case SSH_CHANNEL_INPUT_DRAINING:
1711 case SSH_CHANNEL_OUTPUT_DRAINING:
1712 if (!compat13)
1713 fatal("cannot happen: OUT_DRAIN");
1714 return i;
1715 default:
1716 fatal("channel_find_open: bad channel type %d", channels[i].type);
1717 /* NOTREACHED */
1718 }
1719 return -1;
1720}
1721
1722
Damien Miller5428f641999-11-25 11:54:57 +11001723/*
1724 * Returns a message describing the currently open forwarded connections,
1725 * suitable for sending to the client. The message contains crlf pairs for
1726 * newlines.
1727 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001728
Damien Miller95def091999-11-25 00:26:21 +11001729char *
1730channel_open_message()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001731{
Damien Miller95def091999-11-25 00:26:21 +11001732 Buffer buffer;
1733 int i;
1734 char buf[512], *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001735
Damien Miller95def091999-11-25 00:26:21 +11001736 buffer_init(&buffer);
1737 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001738 buffer_append(&buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001739 for (i = 0; i < channels_alloc; i++) {
1740 Channel *c = &channels[i];
1741 switch (c->type) {
1742 case SSH_CHANNEL_FREE:
1743 case SSH_CHANNEL_X11_LISTENER:
1744 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001745 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001746 case SSH_CHANNEL_CLOSED:
1747 case SSH_CHANNEL_AUTH_SOCKET:
1748 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001749 case SSH_CHANNEL_LARVAL:
Damien Miller95def091999-11-25 00:26:21 +11001750 case SSH_CHANNEL_OPENING:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001751 case SSH_CHANNEL_CONNECTING:
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001752 case SSH_CHANNEL_DYNAMIC:
Damien Miller95def091999-11-25 00:26:21 +11001753 case SSH_CHANNEL_OPEN:
1754 case SSH_CHANNEL_X11_OPEN:
1755 case SSH_CHANNEL_INPUT_DRAINING:
1756 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Millerb38eff82000-04-01 11:09:21 +10001757 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 +11001758 c->self, c->remote_name,
1759 c->type, c->remote_id,
1760 c->istate, buffer_len(&c->input),
Damien Millerb38eff82000-04-01 11:09:21 +10001761 c->ostate, buffer_len(&c->output),
1762 c->rfd, c->wfd);
Damien Miller95def091999-11-25 00:26:21 +11001763 buffer_append(&buffer, buf, strlen(buf));
1764 continue;
1765 default:
Damien Millerb38eff82000-04-01 11:09:21 +10001766 fatal("channel_open_message: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001767 /* NOTREACHED */
1768 }
1769 }
1770 buffer_append(&buffer, "\0", 1);
1771 cp = xstrdup(buffer_ptr(&buffer));
1772 buffer_free(&buffer);
1773 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001774}
1775
Damien Miller5428f641999-11-25 11:54:57 +11001776/*
1777 * Initiate forwarding of connections to local port "port" through the secure
1778 * channel to host:port from remote side.
1779 */
Kevin Steves12057502001-02-05 14:54:34 +00001780int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001781channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
1782 u_short port_to_connect, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001783{
Kevin Steves12057502001-02-05 14:54:34 +00001784 return channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +11001785 NULL, listen_port,
1786 host_to_connect, port_to_connect,
1787 gateway_ports, /*remote_fwd*/ 0);
1788}
1789
1790/*
1791 * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
1792 * (SSH_CHANNEL_RPORT_LISTENER).
1793 */
Kevin Steves12057502001-02-05 14:54:34 +00001794int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001795channel_request_forwarding(
1796 const char *listen_address, u_short listen_port,
1797 const char *host_to_connect, u_short port_to_connect,
1798 int gateway_ports, int remote_fwd)
1799{
1800 int success, ch, sock, on = 1, ctype;
Damien Miller34132e52000-01-14 15:45:46 +11001801 struct addrinfo hints, *ai, *aitop;
1802 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0bc1bd82000-11-13 22:57:25 +11001803 const char *host;
Damien Miller5428f641999-11-25 11:54:57 +11001804 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001805
Kevin Steves12057502001-02-05 14:54:34 +00001806 success = 0;
1807
Damien Miller0bc1bd82000-11-13 22:57:25 +11001808 if (remote_fwd) {
1809 host = listen_address;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001810 ctype = SSH_CHANNEL_RPORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001811 } else {
1812 host = host_to_connect;
1813 ctype =SSH_CHANNEL_PORT_LISTENER;
1814 }
1815
Kevin Steves12057502001-02-05 14:54:34 +00001816 if (strlen(host) > sizeof(channels[0].path) - 1) {
1817 error("Forward host name too long.");
1818 return success;
1819 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001820
Damien Miller0bc1bd82000-11-13 22:57:25 +11001821 /* XXX listen_address is currently ignored */
Damien Miller5428f641999-11-25 11:54:57 +11001822 /*
Damien Miller34132e52000-01-14 15:45:46 +11001823 * getaddrinfo returns a loopback address if the hostname is
1824 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11001825 */
Damien Miller34132e52000-01-14 15:45:46 +11001826 memset(&hints, 0, sizeof(hints));
1827 hints.ai_family = IPv4or6;
1828 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
1829 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001830 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001831 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
1832 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11001833
Damien Miller34132e52000-01-14 15:45:46 +11001834 for (ai = aitop; ai; ai = ai->ai_next) {
1835 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1836 continue;
1837 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1838 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001839 error("channel_request_forwarding: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001840 continue;
1841 }
1842 /* Create a port to listen for the host. */
1843 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1844 if (sock < 0) {
1845 /* this is no error since kernel may not support ipv6 */
1846 verbose("socket: %.100s", strerror(errno));
1847 continue;
1848 }
1849 /*
1850 * Set socket options. We would like the socket to disappear
1851 * as soon as it has been closed for whatever reason.
1852 */
1853 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
1854 linger.l_onoff = 1;
1855 linger.l_linger = 5;
1856 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
1857 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001858
Damien Miller34132e52000-01-14 15:45:46 +11001859 /* Bind the socket to the address. */
1860 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1861 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11001862 if (!ai->ai_next)
1863 error("bind: %.100s", strerror(errno));
1864 else
1865 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001866
Damien Miller34132e52000-01-14 15:45:46 +11001867 close(sock);
1868 continue;
1869 }
1870 /* Start listening for connections on the socket. */
1871 if (listen(sock, 5) < 0) {
1872 error("listen: %.100s", strerror(errno));
1873 close(sock);
1874 continue;
1875 }
1876 /* Allocate a channel number for the socket. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001877 ch = channel_new("port listener", ctype, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10001878 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11001879 0, xstrdup("port listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11001880 strlcpy(channels[ch].path, host, sizeof(channels[ch].path));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001881 channels[ch].host_port = port_to_connect;
1882 channels[ch].listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11001883 success = 1;
1884 }
1885 if (success == 0)
Kevin Steves12057502001-02-05 14:54:34 +00001886 error("channel_request_forwarding: cannot listen to port: %d",
1887 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001888 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00001889 return success;
Damien Miller95def091999-11-25 00:26:21 +11001890}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001891
Damien Miller5428f641999-11-25 11:54:57 +11001892/*
1893 * Initiate forwarding of connections to port "port" on remote host through
1894 * the secure channel to host:port from local side.
1895 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001896
Damien Miller4af51302000-04-16 11:18:38 +10001897void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001898channel_request_remote_forwarding(u_short listen_port,
1899 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001900{
Damien Miller0bc1bd82000-11-13 22:57:25 +11001901 int payload_len, type, success = 0;
1902
Damien Miller95def091999-11-25 00:26:21 +11001903 /* Record locally that connection to this host/port is permitted. */
1904 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1905 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001906
Damien Miller95def091999-11-25 00:26:21 +11001907 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10001908 if (compat20) {
1909 const char *address_to_bind = "0.0.0.0";
1910 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1911 packet_put_cstring("tcpip-forward");
1912 packet_put_char(0); /* boolean: want reply */
1913 packet_put_cstring(address_to_bind);
1914 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001915 packet_send();
1916 packet_write_wait();
1917 /* Assume that server accepts the request */
1918 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10001919 } else {
1920 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10001921 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10001922 packet_put_cstring(host_to_connect);
1923 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10001924 packet_send();
1925 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001926
1927 /* Wait for response from the remote side. */
1928 type = packet_read(&payload_len);
1929 switch (type) {
1930 case SSH_SMSG_SUCCESS:
1931 success = 1;
1932 break;
1933 case SSH_SMSG_FAILURE:
1934 log("Warning: Server denied remote port forwarding.");
1935 break;
1936 default:
1937 /* Unknown packet */
1938 packet_disconnect("Protocol error for port forward request:"
1939 "received packet type %d.", type);
1940 }
1941 }
1942 if (success) {
1943 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
1944 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
1945 permitted_opens[num_permitted_opens].listen_port = listen_port;
1946 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10001947 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001948}
1949
Damien Miller5428f641999-11-25 11:54:57 +11001950/*
1951 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
1952 * listening for the port, and sends back a success reply (or disconnect
1953 * message if there was an error). This never returns if there was an error.
1954 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001955
Damien Miller4af51302000-04-16 11:18:38 +10001956void
Damien Millere247cc42000-05-07 12:03:14 +10001957channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001958{
Damien Milleraae6c611999-12-06 11:47:28 +11001959 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11001960 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001961
Damien Miller95def091999-11-25 00:26:21 +11001962 /* Get arguments from the packet. */
1963 port = packet_get_int();
1964 hostname = packet_get_string(NULL);
1965 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001966
Damien Millerbac2d8a2000-09-05 16:13:06 +11001967#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11001968 /*
1969 * Check that an unprivileged user is not trying to forward a
1970 * privileged port.
1971 */
Damien Miller95def091999-11-25 00:26:21 +11001972 if (port < IPPORT_RESERVED && !is_root)
1973 packet_disconnect("Requested forwarding of port %d but user is not root.",
1974 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001975#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001976 /* Initiate forwarding */
Damien Millere247cc42000-05-07 12:03:14 +10001977 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11001978
1979 /* Free the argument string. */
1980 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001981}
1982
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001983/*
1984 * Permits opening to any host/port if permitted_opens[] is empty. This is
1985 * usually called by the server, because the user could connect to any port
1986 * anyway, and the server has no way to know but to trust the client anyway.
1987 */
1988void
1989channel_permit_all_opens()
1990{
1991 if (num_permitted_opens == 0)
1992 all_opens_permitted = 1;
1993}
1994
Ben Lindstroma3700052001-04-05 23:26:32 +00001995void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001996channel_add_permitted_opens(char *host, int port)
1997{
1998 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1999 fatal("channel_request_remote_forwarding: too many forwards");
2000 debug("allow port forwarding to host %s port %d", host, port);
2001
2002 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2003 permitted_opens[num_permitted_opens].port_to_connect = port;
2004 num_permitted_opens++;
2005
2006 all_opens_permitted = 0;
2007}
2008
Ben Lindstroma3700052001-04-05 23:26:32 +00002009void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002010channel_clear_permitted_opens(void)
2011{
2012 int i;
2013
2014 for (i = 0; i < num_permitted_opens; i++)
2015 xfree(permitted_opens[i].host_to_connect);
2016 num_permitted_opens = 0;
2017
2018}
2019
2020
2021/* return socket to remote host, port */
Damien Millerb38eff82000-04-01 11:09:21 +10002022int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002023connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002024{
Damien Miller34132e52000-01-14 15:45:46 +11002025 struct addrinfo hints, *ai, *aitop;
2026 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2027 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002028 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002029
Damien Miller34132e52000-01-14 15:45:46 +11002030 memset(&hints, 0, sizeof(hints));
2031 hints.ai_family = IPv4or6;
2032 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002033 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002034 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002035 error("connect_to %.100s: unknown host (%s)", host,
2036 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002037 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002038 }
Damien Miller34132e52000-01-14 15:45:46 +11002039 for (ai = aitop; ai; ai = ai->ai_next) {
2040 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2041 continue;
2042 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2043 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002044 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002045 continue;
2046 }
Damien Miller34132e52000-01-14 15:45:46 +11002047 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2048 if (sock < 0) {
2049 error("socket: %.100s", strerror(errno));
2050 continue;
2051 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +00002052 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002053 fatal("connect_to: F_SETFL: %s", strerror(errno));
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002054 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2055 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002056 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002057 strerror(errno));
2058 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002059 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002060 }
2061 break; /* success */
2062
2063 }
2064 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002065 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002066 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002067 return -1;
2068 }
2069 /* success */
2070 return sock;
2071}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002072
Damien Miller0bc1bd82000-11-13 22:57:25 +11002073int
2074channel_connect_by_listen_adress(u_short listen_port)
2075{
2076 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002077
Damien Miller0bc1bd82000-11-13 22:57:25 +11002078 for (i = 0; i < num_permitted_opens; i++)
2079 if (permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002080 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002081 permitted_opens[i].host_to_connect,
2082 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002083 error("WARNING: Server requests forwarding for unknown listen_port %d",
2084 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002085 return -1;
2086}
Damien Millerb38eff82000-04-01 11:09:21 +10002087
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002088/* Check if connecting to that port is permitted and connect. */
2089int
2090channel_connect_to(const char *host, u_short port)
2091{
2092 int i, permit;
2093
2094 permit = all_opens_permitted;
2095 if (!permit) {
2096 for (i = 0; i < num_permitted_opens; i++)
2097 if (permitted_opens[i].port_to_connect == port &&
2098 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2099 permit = 1;
2100
2101 }
2102 if (!permit) {
2103 log("Received request to connect to host %.100s port %d, "
2104 "but the request was denied.", host, port);
2105 return -1;
2106 }
2107 return connect_to(host, port);
2108}
2109
Damien Millerb38eff82000-04-01 11:09:21 +10002110/*
2111 * This is called after receiving PORT_OPEN message. This attempts to
2112 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
2113 * or CHANNEL_OPEN_FAILURE.
2114 */
2115
Damien Miller4af51302000-04-16 11:18:38 +10002116void
Damien Miller62cee002000-09-23 17:15:56 +11002117channel_input_port_open(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002118{
2119 u_short host_port;
2120 char *host, *originator_string;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002121 int remote_channel, sock = -1, newch;
Damien Millerb38eff82000-04-01 11:09:21 +10002122
Damien Millerb38eff82000-04-01 11:09:21 +10002123 remote_channel = packet_get_int();
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002124 host = packet_get_string(NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10002125 host_port = packet_get_int();
2126
Damien Millerb38eff82000-04-01 11:09:21 +10002127 if (have_hostname_in_open) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002128 originator_string = packet_get_string(NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10002129 } else {
2130 originator_string = xstrdup("unknown (remote did not supply name)");
Damien Miller95def091999-11-25 00:26:21 +11002131 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002132 packet_done();
2133 sock = channel_connect_to(host, host_port);
2134 if (sock != -1) {
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002135 newch = channel_allocate(SSH_CHANNEL_CONNECTING,
2136 sock, originator_string);
Damien Millerb38eff82000-04-01 11:09:21 +10002137 channels[newch].remote_id = remote_channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002138
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002139 /*XXX delay answer? */
Damien Millerb38eff82000-04-01 11:09:21 +10002140 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2141 packet_put_int(remote_channel);
2142 packet_put_int(newch);
2143 packet_send();
2144 } else {
2145 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2146 packet_put_int(remote_channel);
2147 packet_send();
2148 }
Damien Miller95def091999-11-25 00:26:21 +11002149 xfree(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002150}
2151
Damien Miller5428f641999-11-25 11:54:57 +11002152/*
2153 * Creates an internet domain socket for listening for X11 connections.
2154 * Returns a suitable value for the DISPLAY variable, or NULL if an error
2155 * occurs.
2156 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002157
Damien Miller34132e52000-01-14 15:45:46 +11002158#define NUM_SOCKS 10
2159
Damien Miller95def091999-11-25 00:26:21 +11002160char *
Damien Millera34a28b1999-12-14 10:47:15 +11002161x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002162{
Damien Milleraae6c611999-12-06 11:47:28 +11002163 int display_number, sock;
2164 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002165 struct addrinfo hints, *ai, *aitop;
2166 char strport[NI_MAXSERV];
2167 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
2168 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11002169 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002170
Damien Millera34a28b1999-12-14 10:47:15 +11002171 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11002172 display_number < MAX_DISPLAYS;
2173 display_number++) {
2174 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002175 memset(&hints, 0, sizeof(hints));
2176 hints.ai_family = IPv4or6;
2177 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
2178 hints.ai_socktype = SOCK_STREAM;
2179 snprintf(strport, sizeof strport, "%d", port);
2180 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2181 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11002182 return NULL;
2183 }
Damien Miller34132e52000-01-14 15:45:46 +11002184 for (ai = aitop; ai; ai = ai->ai_next) {
2185 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2186 continue;
2187 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2188 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002189 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002190 error("socket: %.100s", strerror(errno));
2191 return NULL;
2192 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002193 debug("x11_create_display_inet: Socket family %d not supported",
2194 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002195 continue;
2196 }
Damien Miller34132e52000-01-14 15:45:46 +11002197 }
2198 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2199 debug("bind port %d: %.100s", port, strerror(errno));
2200 shutdown(sock, SHUT_RDWR);
2201 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002202
2203 if (ai->ai_next)
2204 continue;
2205
Damien Miller34132e52000-01-14 15:45:46 +11002206 for (n = 0; n < num_socks; n++) {
2207 shutdown(socks[n], SHUT_RDWR);
2208 close(socks[n]);
2209 }
2210 num_socks = 0;
2211 break;
2212 }
2213 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002214#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002215 if (num_socks == NUM_SOCKS)
2216 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002217#else
2218 break;
2219#endif
Damien Miller95def091999-11-25 00:26:21 +11002220 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002221 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002222 if (num_socks > 0)
2223 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002224 }
Damien Miller95def091999-11-25 00:26:21 +11002225 if (display_number >= MAX_DISPLAYS) {
2226 error("Failed to allocate internet-domain X11 display socket.");
2227 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002228 }
Damien Miller95def091999-11-25 00:26:21 +11002229 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002230 for (n = 0; n < num_socks; n++) {
2231 sock = socks[n];
2232 if (listen(sock, 5) < 0) {
2233 error("listen: %.100s", strerror(errno));
2234 shutdown(sock, SHUT_RDWR);
2235 close(sock);
2236 return NULL;
2237 }
Damien Miller95def091999-11-25 00:26:21 +11002238 }
Damien Miller34132e52000-01-14 15:45:46 +11002239
Damien Miller95def091999-11-25 00:26:21 +11002240 /* Set up a suitable value for the DISPLAY variable. */
2241 if (gethostname(hostname, sizeof(hostname)) < 0)
2242 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11002243
2244#ifdef IPADDR_IN_DISPLAY
Kevin Stevesef4eea92001-02-05 12:42:17 +00002245 /*
Damien Miller76112de1999-12-21 11:18:08 +11002246 * HPUX detects the local hostname in the DISPLAY variable and tries
2247 * to set up a shared memory connection to the server, which it
2248 * incorrectly supposes to be local.
2249 *
2250 * The workaround - as used in later $$H and other programs - is
2251 * is to set display to the host's IP address.
2252 */
2253 {
2254 struct hostent *he;
2255 struct in_addr my_addr;
2256
2257 he = gethostbyname(hostname);
2258 if (he == NULL) {
2259 error("[X11-broken-fwd-hostname-workaround] Could not get "
2260 "IP address for hostname %s.", hostname);
2261
2262 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
2263 "Could not get IP address for hostname %s.", hostname);
2264
2265 shutdown(sock, SHUT_RDWR);
2266 close(sock);
2267
2268 return NULL;
2269 }
2270
2271 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2272
2273 /* Set DISPLAY to <ip address>:screen.display */
Kevin Stevesef4eea92001-02-05 12:42:17 +00002274 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Kevin Stevesda6cd592000-12-29 18:41:21 +00002275 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002276 }
2277#else /* IPADDR_IN_DISPLAY */
2278 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11002279 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Kevin Stevesda6cd592000-12-29 18:41:21 +00002280 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002281#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002282
Damien Miller34132e52000-01-14 15:45:46 +11002283 /* Allocate a channel for each socket. */
2284 for (n = 0; n < num_socks; n++) {
2285 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10002286 (void) channel_new("x11 listener",
2287 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2288 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002289 0, xstrdup("X11 inet listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11002290 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002291
Damien Miller95def091999-11-25 00:26:21 +11002292 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11002293 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002294}
2295
2296#ifndef X_UNIX_PATH
2297#define X_UNIX_PATH "/tmp/.X11-unix/X"
2298#endif
2299
2300static
2301int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002302connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002303{
Damien Miller95def091999-11-25 00:26:21 +11002304 static const char *const x_sockets[] = {
2305 X_UNIX_PATH "%u",
2306 "/var/X/.X11-unix/X" "%u",
2307 "/usr/spool/sockets/X11/" "%u",
2308 NULL
2309 };
2310 int sock;
2311 struct sockaddr_un addr;
2312 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002313
Damien Miller95def091999-11-25 00:26:21 +11002314 for (path = x_sockets; *path; ++path) {
2315 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2316 if (sock < 0)
2317 error("socket: %.100s", strerror(errno));
2318 memset(&addr, 0, sizeof(addr));
2319 addr.sun_family = AF_UNIX;
2320 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
2321 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2322 return sock;
2323 close(sock);
2324 }
2325 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2326 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002327}
2328
Damien Millerbd483e72000-04-30 10:00:53 +10002329int
2330x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002331{
Damien Millerbd483e72000-04-30 10:00:53 +10002332 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002333 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002334 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002335 struct addrinfo hints, *ai, *aitop;
2336 char strport[NI_MAXSERV];
2337 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002338
Damien Miller95def091999-11-25 00:26:21 +11002339 /* Try to open a socket for the local X server. */
2340 display = getenv("DISPLAY");
2341 if (!display) {
2342 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002343 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002344 }
Damien Miller5428f641999-11-25 11:54:57 +11002345 /*
2346 * Now we decode the value of the DISPLAY variable and make a
2347 * connection to the real X server.
2348 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002349
Damien Miller5428f641999-11-25 11:54:57 +11002350 /*
2351 * Check if it is a unix domain socket. Unix domain displays are in
2352 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2353 */
Damien Miller95def091999-11-25 00:26:21 +11002354 if (strncmp(display, "unix:", 5) == 0 ||
2355 display[0] == ':') {
2356 /* Connect to the unix domain socket. */
2357 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2358 error("Could not parse display number from DISPLAY: %.100s",
2359 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002360 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002361 }
2362 /* Create a socket. */
2363 sock = connect_local_xsocket(display_number);
2364 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002365 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002366
2367 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002368 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002369 }
Damien Miller5428f641999-11-25 11:54:57 +11002370 /*
2371 * Connect to an inet socket. The DISPLAY value is supposedly
2372 * hostname:d[.s], where hostname may also be numeric IP address.
2373 */
Damien Miller95def091999-11-25 00:26:21 +11002374 strncpy(buf, display, sizeof(buf));
2375 buf[sizeof(buf) - 1] = 0;
2376 cp = strchr(buf, ':');
2377 if (!cp) {
2378 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002379 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002380 }
Damien Miller95def091999-11-25 00:26:21 +11002381 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002382 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002383 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2384 error("Could not parse display number from DISPLAY: %.100s",
2385 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002386 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002387 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002388
Damien Miller34132e52000-01-14 15:45:46 +11002389 /* Look up the host address */
2390 memset(&hints, 0, sizeof(hints));
2391 hints.ai_family = IPv4or6;
2392 hints.ai_socktype = SOCK_STREAM;
2393 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2394 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2395 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002396 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002397 }
Damien Miller34132e52000-01-14 15:45:46 +11002398 for (ai = aitop; ai; ai = ai->ai_next) {
2399 /* Create a socket. */
2400 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2401 if (sock < 0) {
2402 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002403 continue;
2404 }
2405 /* Connect it to the display. */
2406 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2407 debug("connect %.100s port %d: %.100s", buf,
2408 6000 + display_number, strerror(errno));
2409 close(sock);
2410 continue;
2411 }
2412 /* Success */
2413 break;
Damien Miller34132e52000-01-14 15:45:46 +11002414 }
Damien Miller34132e52000-01-14 15:45:46 +11002415 freeaddrinfo(aitop);
2416 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002417 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002418 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002419 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002420 }
Damien Millerbd483e72000-04-30 10:00:53 +10002421 return sock;
2422}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002423
Damien Millerbd483e72000-04-30 10:00:53 +10002424/*
2425 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2426 * the remote channel number. We should do whatever we want, and respond
2427 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2428 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002429
Damien Millerbd483e72000-04-30 10:00:53 +10002430void
Damien Miller62cee002000-09-23 17:15:56 +11002431x11_input_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002432{
2433 int remote_channel, sock = 0, newch;
2434 char *remote_host;
Ben Lindstrom46c16222000-12-22 01:43:59 +00002435 u_int remote_len;
Damien Miller95def091999-11-25 00:26:21 +11002436
Damien Millerbd483e72000-04-30 10:00:53 +10002437 /* Get remote channel number. */
2438 remote_channel = packet_get_int();
Damien Miller95def091999-11-25 00:26:21 +11002439
Damien Millerbd483e72000-04-30 10:00:53 +10002440 /* Get remote originator name. */
2441 if (have_hostname_in_open) {
2442 remote_host = packet_get_string(&remote_len);
2443 remote_len += 4;
2444 } else {
2445 remote_host = xstrdup("unknown (remote did not supply name)");
2446 remote_len = 0;
2447 }
2448
2449 debug("Received X11 open request.");
2450 packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN);
2451
2452 /* Obtain a connection to the real X display. */
2453 sock = x11_connect_display();
2454 if (sock == -1) {
2455 /* Send refusal to the remote host. */
2456 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2457 packet_put_int(remote_channel);
2458 packet_send();
2459 } else {
2460 /* Allocate a channel for this connection. */
2461 newch = channel_allocate(
2462 (x11_saved_proto == NULL) ?
2463 SSH_CHANNEL_OPEN : SSH_CHANNEL_X11_OPEN,
2464 sock, remote_host);
2465 channels[newch].remote_id = remote_channel;
2466
2467 /* Send a confirmation to the remote host. */
2468 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2469 packet_put_int(remote_channel);
2470 packet_put_int(newch);
2471 packet_send();
2472 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002473}
2474
Damien Miller69b69aa2000-10-28 14:19:58 +11002475/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2476void
2477deny_input_open(int type, int plen, void *ctxt)
2478{
2479 int rchan = packet_get_int();
2480 switch(type){
2481 case SSH_SMSG_AGENT_OPEN:
2482 error("Warning: ssh server tried agent forwarding.");
2483 break;
2484 case SSH_SMSG_X11_OPEN:
2485 error("Warning: ssh server tried X11 forwarding.");
2486 break;
2487 default:
2488 error("deny_input_open: type %d plen %d", type, plen);
2489 break;
2490 }
2491 error("Warning: this is probably a break in attempt by a malicious server.");
2492 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2493 packet_put_int(rchan);
2494 packet_send();
2495}
2496
Damien Miller5428f641999-11-25 11:54:57 +11002497/*
2498 * Requests forwarding of X11 connections, generates fake authentication
2499 * data, and enables authentication spoofing.
2500 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002501
Damien Miller4af51302000-04-16 11:18:38 +10002502void
Damien Millerbd483e72000-04-30 10:00:53 +10002503x11_request_forwarding_with_spoofing(int client_session_id,
2504 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002505{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002506 u_int data_len = (u_int) strlen(data) / 2;
Ben Lindstromb3211a82001-02-10 22:33:19 +00002507 u_int i, value, len;
Damien Miller95def091999-11-25 00:26:21 +11002508 char *new_data;
2509 int screen_number;
2510 const char *cp;
2511 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002512
Damien Miller95def091999-11-25 00:26:21 +11002513 cp = getenv("DISPLAY");
2514 if (cp)
2515 cp = strchr(cp, ':');
2516 if (cp)
2517 cp = strchr(cp, '.');
2518 if (cp)
2519 screen_number = atoi(cp + 1);
2520 else
2521 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002522
Damien Miller95def091999-11-25 00:26:21 +11002523 /* Save protocol name. */
2524 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002525
Damien Miller5428f641999-11-25 11:54:57 +11002526 /*
2527 * Extract real authentication data and generate fake data of the
2528 * same length.
2529 */
Damien Miller95def091999-11-25 00:26:21 +11002530 x11_saved_data = xmalloc(data_len);
2531 x11_fake_data = xmalloc(data_len);
2532 for (i = 0; i < data_len; i++) {
2533 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2534 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2535 if (i % 4 == 0)
2536 rand = arc4random();
2537 x11_saved_data[i] = value;
2538 x11_fake_data[i] = rand & 0xff;
2539 rand >>= 8;
2540 }
2541 x11_saved_data_len = data_len;
2542 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002543
Damien Miller95def091999-11-25 00:26:21 +11002544 /* Convert the fake data into hex. */
Ben Lindstromb3211a82001-02-10 22:33:19 +00002545 len = 2 * data_len + 1;
2546 new_data = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +11002547 for (i = 0; i < data_len; i++)
Ben Lindstromb3211a82001-02-10 22:33:19 +00002548 snprintf(new_data + 2 * i, len - 2 * i,
2549 "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002550
Damien Miller95def091999-11-25 00:26:21 +11002551 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002552 if (compat20) {
2553 channel_request_start(client_session_id, "x11-req", 0);
2554 packet_put_char(0); /* XXX bool single connection */
2555 } else {
2556 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2557 }
2558 packet_put_cstring(proto);
2559 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002560 packet_put_int(screen_number);
2561 packet_send();
2562 packet_write_wait();
2563 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002564}
2565
2566/* Sends a message to the server to request authentication fd forwarding. */
2567
Damien Miller4af51302000-04-16 11:18:38 +10002568void
Damien Miller95def091999-11-25 00:26:21 +11002569auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002570{
Damien Miller95def091999-11-25 00:26:21 +11002571 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2572 packet_send();
2573 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002574}
2575
Damien Miller5428f641999-11-25 11:54:57 +11002576/*
2577 * Returns the name of the forwarded authentication socket. Returns NULL if
2578 * there is no forwarded authentication socket. The returned value points to
2579 * a static buffer.
2580 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002581
Damien Miller95def091999-11-25 00:26:21 +11002582char *
2583auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002584{
Damien Miller95def091999-11-25 00:26:21 +11002585 return channel_forwarded_auth_socket_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002586}
2587
2588/* removes the agent forwarding socket */
2589
Damien Miller4af51302000-04-16 11:18:38 +10002590void
Damien Miller95def091999-11-25 00:26:21 +11002591cleanup_socket(void)
2592{
Damien Miller78315eb2000-09-29 23:01:36 +11002593 unlink(channel_forwarded_auth_socket_name);
Damien Miller95def091999-11-25 00:26:21 +11002594 rmdir(channel_forwarded_auth_socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002595}
2596
Damien Miller5428f641999-11-25 11:54:57 +11002597/*
Damien Millerd3a18572000-06-07 19:55:44 +10002598 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002599 * This starts forwarding authentication requests.
2600 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002601
Damien Millerd3a18572000-06-07 19:55:44 +10002602int
Damien Miller95def091999-11-25 00:26:21 +11002603auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002604{
Damien Miller95def091999-11-25 00:26:21 +11002605 int sock, newch;
2606 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002607
Damien Miller95def091999-11-25 00:26:21 +11002608 if (auth_get_socket_name() != NULL)
2609 fatal("Protocol error: authentication forwarding requested twice.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002610
Damien Miller95def091999-11-25 00:26:21 +11002611 /* Temporarily drop privileged uid for mkdir/bind. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00002612 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002613
Damien Miller95def091999-11-25 00:26:21 +11002614 /* Allocate a buffer for the socket name, and format the name. */
2615 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
2616 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
2617 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002618
Damien Miller95def091999-11-25 00:26:21 +11002619 /* Create private directory for socket */
Damien Millerd3a18572000-06-07 19:55:44 +10002620 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL) {
2621 packet_send_debug("Agent forwarding disabled: mkdtemp() failed: %.100s",
2622 strerror(errno));
2623 restore_uid();
2624 xfree(channel_forwarded_auth_socket_name);
2625 xfree(channel_forwarded_auth_socket_dir);
2626 channel_forwarded_auth_socket_name = NULL;
2627 channel_forwarded_auth_socket_dir = NULL;
2628 return 0;
2629 }
Damien Miller95def091999-11-25 00:26:21 +11002630 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
2631 channel_forwarded_auth_socket_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002632
Damien Miller95def091999-11-25 00:26:21 +11002633 if (atexit(cleanup_socket) < 0) {
2634 int saved = errno;
2635 cleanup_socket();
2636 packet_disconnect("socket: %.100s", strerror(saved));
2637 }
2638 /* Create the socket. */
2639 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2640 if (sock < 0)
2641 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002642
Damien Miller95def091999-11-25 00:26:21 +11002643 /* Bind it to the name. */
2644 memset(&sunaddr, 0, sizeof(sunaddr));
2645 sunaddr.sun_family = AF_UNIX;
2646 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
2647 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002648
Damien Miller95def091999-11-25 00:26:21 +11002649 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2650 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002651
Damien Miller95def091999-11-25 00:26:21 +11002652 /* Restore the privileged uid. */
2653 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002654
Damien Miller95def091999-11-25 00:26:21 +11002655 /* Start listening on the socket. */
2656 if (listen(sock, 5) < 0)
2657 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002658
Damien Miller95def091999-11-25 00:26:21 +11002659 /* Allocate a channel for the authentication agent socket. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002660 newch = channel_new("auth socket",
2661 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2662 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2663 0, xstrdup("auth socket"), 1);
2664
Damien Miller037a0dc1999-12-07 15:38:31 +11002665 strlcpy(channels[newch].path, channel_forwarded_auth_socket_name,
2666 sizeof(channels[newch].path));
Damien Millerd3a18572000-06-07 19:55:44 +10002667 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002668}
2669
2670/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2671
Damien Miller4af51302000-04-16 11:18:38 +10002672void
Damien Miller62cee002000-09-23 17:15:56 +11002673auth_input_open_request(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002674{
Damien Miller95def091999-11-25 00:26:21 +11002675 int remch, sock, newch;
2676 char *dummyname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002677
Damien Millerb38eff82000-04-01 11:09:21 +10002678 packet_integrity_check(plen, 4, type);
2679
Damien Miller95def091999-11-25 00:26:21 +11002680 /* Read the remote channel number from the message. */
2681 remch = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002682
Damien Miller5428f641999-11-25 11:54:57 +11002683 /*
2684 * Get a connection to the local authentication agent (this may again
2685 * get forwarded).
2686 */
Damien Miller95def091999-11-25 00:26:21 +11002687 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002688
Damien Miller5428f641999-11-25 11:54:57 +11002689 /*
2690 * If we could not connect the agent, send an error message back to
2691 * the server. This should never happen unless the agent dies,
2692 * because authentication forwarding is only enabled if we have an
2693 * agent.
2694 */
Damien Miller95def091999-11-25 00:26:21 +11002695 if (sock < 0) {
2696 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2697 packet_put_int(remch);
2698 packet_send();
2699 return;
2700 }
2701 debug("Forwarding authentication connection.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002702
Damien Miller5428f641999-11-25 11:54:57 +11002703 /*
2704 * Dummy host name. This will be freed when the channel is freed; it
2705 * will still be valid in the packet_put_string below since the
2706 * channel cannot yet be freed at that point.
2707 */
Damien Miller95def091999-11-25 00:26:21 +11002708 dummyname = xstrdup("authentication agent connection");
2709
2710 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);
2711 channels[newch].remote_id = remch;
2712
2713 /* Send a confirmation to the remote host. */
2714 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2715 packet_put_int(remch);
2716 packet_put_int(newch);
2717 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002718}
Damien Miller33b13562000-04-04 14:38:59 +10002719
2720void
Damien Millerbd483e72000-04-30 10:00:53 +10002721channel_start_open(int id)
Damien Miller33b13562000-04-04 14:38:59 +10002722{
2723 Channel *c = channel_lookup(id);
2724 if (c == NULL) {
2725 log("channel_open: %d: bad id", id);
2726 return;
2727 }
Damien Millerbd483e72000-04-30 10:00:53 +10002728 debug("send channel open %d", id);
Damien Miller33b13562000-04-04 14:38:59 +10002729 packet_start(SSH2_MSG_CHANNEL_OPEN);
2730 packet_put_cstring(c->ctype);
2731 packet_put_int(c->self);
2732 packet_put_int(c->local_window);
2733 packet_put_int(c->local_maxpacket);
Damien Millerbd483e72000-04-30 10:00:53 +10002734}
2735void
2736channel_open(int id)
2737{
2738 /* XXX REMOVE ME */
2739 channel_start_open(id);
Damien Miller33b13562000-04-04 14:38:59 +10002740 packet_send();
Damien Miller33b13562000-04-04 14:38:59 +10002741}
2742void
2743channel_request(int id, char *service, int wantconfirm)
2744{
2745 channel_request_start(id, service, wantconfirm);
2746 packet_send();
2747 debug("channel request %d: %s", id, service) ;
2748}
2749void
2750channel_request_start(int id, char *service, int wantconfirm)
2751{
2752 Channel *c = channel_lookup(id);
2753 if (c == NULL) {
2754 log("channel_request: %d: bad id", id);
2755 return;
2756 }
2757 packet_start(SSH2_MSG_CHANNEL_REQUEST);
2758 packet_put_int(c->remote_id);
2759 packet_put_cstring(service);
2760 packet_put_char(wantconfirm);
2761}
2762void
2763channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
2764{
2765 Channel *c = channel_lookup(id);
2766 if (c == NULL) {
2767 log("channel_register_callback: %d: bad id", id);
2768 return;
2769 }
2770 c->cb_event = mtype;
2771 c->cb_fn = fn;
2772 c->cb_arg = arg;
2773}
2774void
2775channel_register_cleanup(int id, channel_callback_fn *fn)
2776{
2777 Channel *c = channel_lookup(id);
2778 if (c == NULL) {
2779 log("channel_register_cleanup: %d: bad id", id);
2780 return;
2781 }
2782 c->dettach_user = fn;
2783}
2784void
2785channel_cancel_cleanup(int id)
2786{
2787 Channel *c = channel_lookup(id);
2788 if (c == NULL) {
2789 log("channel_cancel_cleanup: %d: bad id", id);
2790 return;
2791 }
2792 c->dettach_user = NULL;
2793}
Kevin Stevesef4eea92001-02-05 12:42:17 +00002794void
Damien Millerad833b32000-08-23 10:46:23 +10002795channel_register_filter(int id, channel_filter_fn *fn)
2796{
2797 Channel *c = channel_lookup(id);
2798 if (c == NULL) {
2799 log("channel_register_filter: %d: bad id", id);
2800 return;
2801 }
2802 c->input_filter = fn;
2803}
Damien Miller33b13562000-04-04 14:38:59 +10002804
2805void
Damien Miller69b69aa2000-10-28 14:19:58 +11002806channel_set_fds(int id, int rfd, int wfd, int efd,
2807 int extusage, int nonblock)
Damien Miller33b13562000-04-04 14:38:59 +10002808{
2809 Channel *c = channel_lookup(id);
2810 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
2811 fatal("channel_activate for non-larval channel %d.", id);
Damien Miller69b69aa2000-10-28 14:19:58 +11002812 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller33b13562000-04-04 14:38:59 +10002813 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002814 /* XXX window size? */
Damien Millere4340be2000-09-16 13:29:08 +11002815 c->local_window = c->local_window_max = c->local_maxpacket * 2;
Damien Miller33b13562000-04-04 14:38:59 +10002816 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2817 packet_put_int(c->remote_id);
2818 packet_put_int(c->local_window);
2819 packet_send();
2820}