blob: d86cb1f08d6f89b54588f2eff5894f226e9d9508 [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 Lindstromd334b272001-04-14 23:08:36 +000043RCSID("$OpenBSD: channels.c,v 1.108 2001/04/14 16:17:14 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 Lindstrom3bb4f9d2001-04-08 18:30:26 +0000545
Ben Lindstromb3921512001-04-11 15:57:50 +0000546int
547channel_decode_helper(Channel *c, int start, int lookfor)
548{
549 u_char *p;
550 int i, have;
551
552 p = buffer_ptr(&c->input);
553 have = buffer_len(&c->input);
554 debug2("channel %d: decode_helper: start %d have %d lookfor %d",
555 c->self, start, have, lookfor);
556 if (have < start)
557 return 0;
558 for (i = start; i < have; i++) {
559 if (p[i] == lookfor) {
560 debug2("channel %d: decode_helper: matched at %d",
561 c->self, i);
562 if (lookfor == '\0' ||
563 (i+3 < have &&
564 p[i+1] == '\n' &&
565 p[i+2] == '\r' &&
566 p[i+3] == '\n'))
567 return i;
568 }
569 if (i > 4096) {
570 /* the peer is probably sending garbage */
571 debug("channel %d: decode_helper: too long",
572 c->self);
573 return -1;
574 }
575 }
576 return 0; /* need more */
577}
578
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000579/* try to decode a http connect header */
580int
581channel_decode_https(Channel *c, fd_set * readset, fd_set * writeset)
582{
583 u_char *p, *host, *buf;
584 int port, ret;
585 char httpok[] = "HTTP/1.0 200\r\n\r\n";
586
587 debug2("channel %d: decode https connect", c->self);
588 ret = channel_decode_helper(c, strlen("connect "), '\r');
589 if (ret <= 0)
590 return ret;
591 p = buffer_ptr(&c->input);
592 buf = xmalloc(ret+1);
593 host = xmalloc(ret);
594 memcpy(buf, p, ret);
595 buf[ret] = '\0';
596 if (sscanf(buf, "CONNECT %[^:]:%u HTTP/", host, &port) != 2) {
597 debug("channel %d: cannot parse http header", c->self);
598 return -1;
599 }
600 debug("channel %d: dynamic request: https host %s port %u",
601 c->self, host, port);
602 strlcpy(c->path, host, sizeof(c->path));
603 c->host_port = port;
604 xfree(host);
605 xfree(buf);
606 buffer_consume(&c->input, ret+4);
607 buffer_append(&c->output, httpok, strlen(httpok));
608
609 return 1;
610}
611
Ben Lindstromb3921512001-04-11 15:57:50 +0000612/* try to decode a socks4 header */
613int
614channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000615{
616 u_char *p, *host;
Ben Lindstromb3921512001-04-11 15:57:50 +0000617 int len, have, ret;
618 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000619 struct {
620 u_int8_t version;
621 u_int8_t command;
622 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000623 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000624 } s4_req, s4_rsp;
625
Ben Lindstromb3921512001-04-11 15:57:50 +0000626 debug2("channel %d: decode socks4", c->self);
627 ret = channel_decode_helper(c, sizeof(s4_req), '\0');
628 if (ret <= 0)
629 return ret;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000630 buffer_get(&c->input, (char *)&s4_req.version, 1);
631 buffer_get(&c->input, (char *)&s4_req.command, 1);
632 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000633 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000634 p = buffer_ptr(&c->input);
635 len = strlen(p);
636 have = buffer_len(&c->input);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000637 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000638 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000639 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000640 c->self, len, have);
641 strlcpy(username, p, sizeof(username));
642 buffer_consume(&c->input, len);
643 buffer_consume(&c->input, 1); /* trailing '\0' */
644
Ben Lindstromb3921512001-04-11 15:57:50 +0000645 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000646 strlcpy(c->path, host, sizeof(c->path));
647 c->host_port = ntohs(s4_req.dest_port);
648
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000649 debug("channel %d: dynamic request: socks4 host %s port %u command %u",
650 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000651
Ben Lindstromb3921512001-04-11 15:57:50 +0000652 if (s4_req.command != 1) {
653 debug("channel %d: cannot handle: socks4 cn %d",
654 c->self, s4_req.command);
655 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000656 }
Ben Lindstromb3921512001-04-11 15:57:50 +0000657 s4_rsp.version = 0; /* vn: 0 for reply */
658 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000659 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +0000660 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000661 buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +0000662 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000663}
664
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000665/* try to decode a socks5 header */
666#define SSH_SOCKS5_AUTHDONE 0x1000
667#define SSH_SOCKS5_NOAUTH 0x00
668#define SSH_SOCKS5_IPV4 0x01
669#define SSH_SOCKS5_DOMAIN 0x03
670#define SSH_SOCKS5_IPV6 0x04
671#define SSH_SOCKS5_CONNECT 0x01
672#define SSH_SOCKS5_SUCCESS 0x00
673
674int
675channel_decode_socks5(Channel *c, fd_set * readset, fd_set * writeset)
676{
677 struct {
678 u_int8_t version;
679 u_int8_t command;
680 u_int8_t reserved;
681 u_int8_t atyp;
682 } s5_req, s5_rsp;
683 u_int16_t dest_port;
684 u_char *p, dest_addr[255+1];
685 int i, have, found, nmethods, addrlen, af;
686
687 debug2("channel %d: decode socks5", c->self);
688 p = buffer_ptr(&c->input);
689 if (p[0] != 0x05)
690 return -1;
691 have = buffer_len(&c->input);
692 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
693 /* format: ver | nmethods | methods */
694 if (have < 2)
695 return 0;
696 nmethods = p[1];
697 if (have < nmethods + 2)
698 return 0;
699 /* look for method: "NO AUTHENTICATION REQUIRED" */
700 for (found = 0, i = 2 ; i < nmethods + 2; i++) {
701 if (p[i] == SSH_SOCKS5_NOAUTH ) {
702 found = 1;
703 break;
704 }
705 }
706 if (!found) {
707 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
708 c->self);
709 return -1;
710 }
711 buffer_consume(&c->input, nmethods + 2);
712 buffer_put_char(&c->output, 0x05); /* version */
713 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
714 FD_SET(c->sock, writeset);
715 c->flags |= SSH_SOCKS5_AUTHDONE;
716 debug2("channel %d: socks5 auth done", c->self);
717 return 0; /* need more */
718 }
719 debug2("channel %d: socks5 post auth", c->self);
720 if (have < sizeof(s5_req)+1)
721 return 0; /* need more */
722 memcpy((char *)&s5_req, p, sizeof(s5_req));
723 if (s5_req.version != 0x05 ||
724 s5_req.command != SSH_SOCKS5_CONNECT ||
725 s5_req.reserved != 0x00) {
726 debug("channel %d: only socks5 connect supported", c->self);
727 return -1;
728 }
729 switch(s5_req.atyp){
730 case SSH_SOCKS5_IPV4:
731 addrlen = 4;
732 af = AF_INET;
733 break;
734 case SSH_SOCKS5_DOMAIN:
735 addrlen = p[sizeof(s5_req)];
736 af = -1;
737 break;
738 case SSH_SOCKS5_IPV6:
739 addrlen = 16;
740 af = AF_INET6;
741 break;
742 default:
743 debug("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
744 return -1;
745 }
746 if (have < 4 + addrlen + 2)
747 return 0;
748 buffer_consume(&c->input, sizeof(s5_req));
749 buffer_get(&c->input, (char *)&dest_addr, addrlen);
750 buffer_get(&c->input, (char *)&dest_port, 2);
751 dest_addr[addrlen] = '\0';
752 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
753 strlcpy(c->path, dest_addr, sizeof(c->path));
754 else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL)
755 return -1;
756 c->host_port = ntohs(dest_port);
757
758 debug("channel %d: dynamic request: socks5 host %s port %u command %u",
759 c->self, c->path, c->host_port, s5_req.command);
760
761 s5_rsp.version = 0x05;
762 s5_rsp.command = SSH_SOCKS5_SUCCESS;
763 s5_rsp.reserved = 0; /* ignored */
764 s5_rsp.atyp = SSH_SOCKS5_IPV4;
765 ((struct in_addr *)&dest_addr)->s_addr = INADDR_ANY;
766 dest_port = 0; /* ignored */
767
768 buffer_append(&c->output, (char *)&s5_rsp, sizeof(s5_rsp));
769 buffer_append(&c->output, (char *)&dest_addr, sizeof(struct in_addr));
770 buffer_append(&c->output, (char *)&dest_port, sizeof(dest_port));
771 return 1;
772}
773
Ben Lindstromb3921512001-04-11 15:57:50 +0000774/* dynamic port forwarding */
775void
776channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
777{
778 u_char *p;
779 int have, ret;
780
781 have = buffer_len(&c->input);
782
783 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +0000784 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +0000785 /* check if the fixed size part of the packet is in buffer. */
786 if (have < 4) {
787 /* need more */
788 FD_SET(c->sock, readset);
789 return;
790 }
791 /* try to guess the protocol */
792 p = buffer_ptr(&c->input);
793 switch (p[0]) {
Ben Lindstromb3921512001-04-11 15:57:50 +0000794 case 'C':
795 ret = channel_decode_https(c, readset, writeset);
796 break;
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000797 case 0x04:
798 ret = channel_decode_socks4(c, readset, writeset);
799 break;
Ben Lindstromb3921512001-04-11 15:57:50 +0000800 case 0x05:
801 ret = channel_decode_socks5(c, readset, writeset);
802 break;
Ben Lindstromb3921512001-04-11 15:57:50 +0000803 default:
804 ret = -1;
805 break;
806 }
807 if (ret < 0) {
808 channel_free(c->self);
809 } else if (ret == 0) {
810 debug2("channel %d: pre_dynamic: need more", c->self);
811 /* need more */
812 FD_SET(c->sock, readset);
813 } else {
814 /* switch to the next state */
815 c->type = SSH_CHANNEL_OPENING;
816 port_open_helper(c, "direct-tcpip");
817 }
818}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000819
Damien Millerb38eff82000-04-01 11:09:21 +1000820/* This is our fake X11 server socket. */
821void
822channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
823{
824 struct sockaddr addr;
825 int newsock, newch;
826 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +1100827 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +1000828 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +1000829
830 if (FD_ISSET(c->sock, readset)) {
831 debug("X11 connection requested.");
832 addrlen = sizeof(addr);
833 newsock = accept(c->sock, &addr, &addrlen);
834 if (newsock < 0) {
835 error("accept: %.100s", strerror(errno));
836 return;
837 }
Damien Millerd83ff352001-01-30 09:19:34 +1100838 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +1000839 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +1000840 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +1100841 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +1000842
843 newch = channel_new("x11",
844 SSH_CHANNEL_OPENING, newsock, newsock, -1,
845 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100846 0, xstrdup(buf), 1);
Damien Millerbd483e72000-04-30 10:00:53 +1000847 if (compat20) {
848 packet_start(SSH2_MSG_CHANNEL_OPEN);
849 packet_put_cstring("x11");
850 packet_put_int(newch);
851 packet_put_int(c->local_window_max);
852 packet_put_int(c->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +1100853 /* originator ipaddr and port */
854 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +1000855 if (datafellows & SSH_BUG_X11FWD) {
856 debug("ssh2 x11 bug compat mode");
857 } else {
858 packet_put_int(remote_port);
859 }
Damien Millerbd483e72000-04-30 10:00:53 +1000860 packet_send();
861 } else {
862 packet_start(SSH_SMSG_X11_OPEN);
863 packet_put_int(newch);
864 if (have_hostname_in_open)
865 packet_put_string(buf, strlen(buf));
866 packet_send();
867 }
Damien Millerd83ff352001-01-30 09:19:34 +1100868 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +1000869 }
870}
871
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000872void
873port_open_helper(Channel *c, char *rtype)
874{
875 int direct;
876 char buf[1024];
877 char *remote_ipaddr = get_peer_ipaddr(c->sock);
878 u_short remote_port = get_peer_port(c->sock);
879
880 direct = (strcmp(rtype, "direct-tcpip") == 0);
881
882 snprintf(buf, sizeof buf,
883 "%s: listening port %d for %.100s port %d, "
884 "connect from %.200s port %d",
885 rtype, c->listening_port, c->path, c->host_port,
886 remote_ipaddr, remote_port);
887
888 xfree(c->remote_name);
889 c->remote_name = xstrdup(buf);
890
891 if (compat20) {
892 packet_start(SSH2_MSG_CHANNEL_OPEN);
893 packet_put_cstring(rtype);
894 packet_put_int(c->self);
895 packet_put_int(c->local_window_max);
896 packet_put_int(c->local_maxpacket);
897 if (direct) {
898 /* target host, port */
899 packet_put_cstring(c->path);
900 packet_put_int(c->host_port);
901 } else {
902 /* listen address, port */
903 packet_put_cstring(c->path);
904 packet_put_int(c->listening_port);
905 }
906 /* originator host and port */
907 packet_put_cstring(remote_ipaddr);
908 packet_put_int(remote_port);
909 packet_send();
910 } else {
911 packet_start(SSH_MSG_PORT_OPEN);
912 packet_put_int(c->self);
913 packet_put_cstring(c->path);
914 packet_put_int(c->host_port);
915 if (have_hostname_in_open)
916 packet_put_cstring(c->remote_name);
917 packet_send();
918 }
919 xfree(remote_ipaddr);
920}
921
Damien Millerb38eff82000-04-01 11:09:21 +1000922/*
923 * This socket is listening for connections to a forwarded TCP/IP port.
924 */
925void
926channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
927{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000928 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +1000929 struct sockaddr addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000930 int newsock, newch, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +1000931 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000932 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100933
Damien Millerb38eff82000-04-01 11:09:21 +1000934 if (FD_ISSET(c->sock, readset)) {
935 debug("Connection to port %d forwarding "
936 "to %.100s port %d requested.",
937 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000938
939 rtype = (c->type == SSH_CHANNEL_RPORT_LISTENER) ?
940 "forwarded-tcpip" : "direct-tcpip";
941 nextstate = (c->host_port == 0) ? SSH_CHANNEL_DYNAMIC :
942 SSH_CHANNEL_OPENING;
943
Damien Millerb38eff82000-04-01 11:09:21 +1000944 addrlen = sizeof(addr);
945 newsock = accept(c->sock, &addr, &addrlen);
946 if (newsock < 0) {
947 error("accept: %.100s", strerror(errno));
948 return;
949 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100950 newch = channel_new(rtype,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000951 nextstate, newsock, newsock, -1,
Damien Millerb38eff82000-04-01 11:09:21 +1000952 c->local_window_max, c->local_maxpacket,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000953 0, xstrdup(rtype), 1);
954
955 nc = channel_lookup(newch);
956 if (nc == NULL) {
957 error("xxx: no new channel:");
958 return;
Damien Millerb38eff82000-04-01 11:09:21 +1000959 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000960 nc->listening_port = c->listening_port;
961 nc->host_port = c->host_port;
962 strlcpy(nc->path, c->path, sizeof(nc->path));
963
964 if (nextstate != SSH_CHANNEL_DYNAMIC)
965 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +1000966 }
967}
968
969/*
970 * This is the authentication agent socket listening for connections from
971 * clients.
972 */
973void
974channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
975{
976 struct sockaddr addr;
977 int newsock, newch;
978 socklen_t addrlen;
979
980 if (FD_ISSET(c->sock, readset)) {
981 addrlen = sizeof(addr);
982 newsock = accept(c->sock, &addr, &addrlen);
983 if (newsock < 0) {
984 error("accept from auth socket: %.100s", strerror(errno));
985 return;
986 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100987 newch = channel_new("accepted auth socket",
988 SSH_CHANNEL_OPENING, newsock, newsock, -1,
989 c->local_window_max, c->local_maxpacket,
990 0, xstrdup("accepted auth socket"), 1);
991 if (compat20) {
992 packet_start(SSH2_MSG_CHANNEL_OPEN);
993 packet_put_cstring("auth-agent@openssh.com");
994 packet_put_int(newch);
995 packet_put_int(c->local_window_max);
996 packet_put_int(c->local_maxpacket);
997 } else {
998 packet_start(SSH_SMSG_AGENT_OPEN);
999 packet_put_int(newch);
1000 }
Damien Millerb38eff82000-04-01 11:09:21 +10001001 packet_send();
1002 }
1003}
1004
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001005void
1006channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
1007{
1008 if (FD_ISSET(c->sock, writeset)) {
1009 int err = 0;
1010 int sz = sizeof(err);
1011 c->type = SSH_CHANNEL_OPEN;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001012 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err, &sz) < 0) {
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001013 debug("getsockopt SO_ERROR failed");
1014 } else {
1015 if (err == 0) {
1016 debug("channel %d: connected)", c->self);
1017 } else {
1018 debug("channel %d: not connected: %s",
1019 c->self, strerror(err));
1020 chan_read_failed(c);
1021 chan_write_failed(c);
1022 }
1023 }
1024 }
1025}
1026
Damien Millerb38eff82000-04-01 11:09:21 +10001027int
1028channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
1029{
1030 char buf[16*1024];
1031 int len;
1032
1033 if (c->rfd != -1 &&
1034 FD_ISSET(c->rfd, readset)) {
1035 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +10001036 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1037 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001038 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +10001039 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001040 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001041 if (c->type != SSH_CHANNEL_OPEN) {
1042 debug("channel %d: not open", c->self);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001043 channel_free(c->self);
1044 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001045 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001046 buffer_consume(&c->output, buffer_len(&c->output));
1047 c->type = SSH_CHANNEL_INPUT_DRAINING;
Ben Lindstromc486d882001-04-11 16:08:34 +00001048 debug("channel %d: status set to input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001049 } else {
1050 chan_read_failed(c);
1051 }
1052 return -1;
1053 }
Damien Millerad833b32000-08-23 10:46:23 +10001054 if(c->input_filter != NULL) {
1055 if (c->input_filter(c, buf, len) == -1) {
Ben Lindstromc486d882001-04-11 16:08:34 +00001056 debug("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001057 chan_read_failed(c);
1058 }
1059 } else {
1060 buffer_append(&c->input, buf, len);
1061 }
Damien Millerb38eff82000-04-01 11:09:21 +10001062 }
1063 return 1;
1064}
1065int
1066channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
1067{
Ben Lindstrome229b252001-03-05 06:28:06 +00001068 struct termios tio;
Damien Millerb38eff82000-04-01 11:09:21 +10001069 int len;
1070
1071 /* Send buffered output data to the socket. */
1072 if (c->wfd != -1 &&
1073 FD_ISSET(c->wfd, writeset) &&
1074 buffer_len(&c->output) > 0) {
1075 len = write(c->wfd, buffer_ptr(&c->output),
Damien Miller6f83b8e2000-05-02 09:23:45 +10001076 buffer_len(&c->output));
1077 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1078 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001079 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001080 if (c->type != SSH_CHANNEL_OPEN) {
1081 debug("channel %d: not open", c->self);
1082 channel_free(c->self);
1083 return -1;
1084 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001085 buffer_consume(&c->output, buffer_len(&c->output));
Ben Lindstromc486d882001-04-11 16:08:34 +00001086 debug("channel %d: status set to input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001087 c->type = SSH_CHANNEL_INPUT_DRAINING;
1088 } else {
1089 chan_write_failed(c);
1090 }
1091 return -1;
1092 }
Damien Miller79438cc2001-02-16 12:34:57 +11001093 if (compat20 && c->isatty) {
Damien Miller79438cc2001-02-16 12:34:57 +11001094 if (tcgetattr(c->wfd, &tio) == 0 &&
1095 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1096 /*
1097 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001098 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001099 * size of a SSH2_MSG_CHANNEL_DATA message
1100 * (4 byte channel id + data)
Damien Miller79438cc2001-02-16 12:34:57 +11001101 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001102 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001103 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001104 }
1105 }
Damien Millerb38eff82000-04-01 11:09:21 +10001106 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001107 if (compat20 && len > 0) {
1108 c->local_consumed += len;
1109 }
1110 }
1111 return 1;
1112}
1113int
1114channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
1115{
1116 char buf[16*1024];
1117 int len;
1118
Damien Miller1383bd82000-04-06 12:32:37 +10001119/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001120 if (c->efd != -1) {
1121 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1122 FD_ISSET(c->efd, writeset) &&
1123 buffer_len(&c->extended) > 0) {
1124 len = write(c->efd, buffer_ptr(&c->extended),
1125 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001126 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001127 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001128 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1129 return 1;
1130 if (len <= 0) {
1131 debug2("channel %d: closing write-efd %d",
1132 c->self, c->efd);
1133 close(c->efd);
1134 c->efd = -1;
1135 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001136 buffer_consume(&c->extended, len);
1137 c->local_consumed += len;
1138 }
1139 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1140 FD_ISSET(c->efd, readset)) {
1141 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001142 debug2("channel %d: read %d from efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001143 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001144 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1145 return 1;
1146 if (len <= 0) {
1147 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001148 c->self, c->efd);
1149 close(c->efd);
1150 c->efd = -1;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001151 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001152 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001153 }
Damien Miller33b13562000-04-04 14:38:59 +10001154 }
1155 }
1156 return 1;
1157}
1158int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001159channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001160{
Ben Lindstromb3921512001-04-11 15:57:50 +00001161 if (c->type == SSH_CHANNEL_OPEN &&
1162 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001163 c->local_window < c->local_window_max/2 &&
1164 c->local_consumed > 0) {
1165 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1166 packet_put_int(c->remote_id);
1167 packet_put_int(c->local_consumed);
1168 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001169 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001170 c->self, c->local_window,
1171 c->local_consumed);
1172 c->local_window += c->local_consumed;
1173 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001174 }
1175 return 1;
1176}
1177
1178void
1179channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
1180{
1181 channel_handle_rfd(c, readset, writeset);
1182 channel_handle_wfd(c, readset, writeset);
1183}
1184
1185void
Damien Miller33b13562000-04-04 14:38:59 +10001186channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
1187{
1188 channel_handle_rfd(c, readset, writeset);
1189 channel_handle_wfd(c, readset, writeset);
1190 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001191
1192 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001193}
1194
1195void
Damien Millerb38eff82000-04-01 11:09:21 +10001196channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1197{
1198 int len;
1199 /* Send buffered output data to the socket. */
1200 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1201 len = write(c->sock, buffer_ptr(&c->output),
1202 buffer_len(&c->output));
1203 if (len <= 0)
1204 buffer_consume(&c->output, buffer_len(&c->output));
1205 else
1206 buffer_consume(&c->output, len);
1207 }
1208}
1209
1210void
Damien Miller33b13562000-04-04 14:38:59 +10001211channel_handler_init_20(void)
1212{
1213 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +10001214 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001215 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001216 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001217 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001218 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001219 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001220 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001221
1222 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
1223 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001224 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001225 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001226 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001227 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001228 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_2;
Damien Miller33b13562000-04-04 14:38:59 +10001229}
1230
1231void
Damien Millerb38eff82000-04-01 11:09:21 +10001232channel_handler_init_13(void)
1233{
1234 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1235 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1236 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1237 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1238 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1239 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1240 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001241 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001242 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001243
1244 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
1245 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1246 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1247 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1248 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001249 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001250 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001251}
1252
1253void
1254channel_handler_init_15(void)
1255{
1256 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +10001257 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001258 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1259 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1260 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001261 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001262 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001263
1264 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1265 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1266 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1267 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001268 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001269 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001270}
1271
1272void
1273channel_handler_init(void)
1274{
1275 int i;
1276 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1277 channel_pre[i] = NULL;
1278 channel_post[i] = NULL;
1279 }
Damien Miller33b13562000-04-04 14:38:59 +10001280 if (compat20)
1281 channel_handler_init_20();
1282 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001283 channel_handler_init_13();
1284 else
1285 channel_handler_init_15();
1286}
1287
Damien Miller4af51302000-04-16 11:18:38 +10001288void
Damien Millerb38eff82000-04-01 11:09:21 +10001289channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1290{
1291 static int did_init = 0;
1292 int i;
1293 Channel *c;
1294
1295 if (!did_init) {
1296 channel_handler_init();
1297 did_init = 1;
1298 }
1299 for (i = 0; i < channels_alloc; i++) {
1300 c = &channels[i];
1301 if (c->type == SSH_CHANNEL_FREE)
1302 continue;
1303 if (ftab[c->type] == NULL)
1304 continue;
1305 (*ftab[c->type])(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001306 if (chan_is_dead(c)) {
1307 /*
1308 * we have to remove the fd's from the select mask
1309 * before the channels are free'd and the fd's are
1310 * closed
1311 */
1312 if (c->wfd != -1)
1313 FD_CLR(c->wfd, writeset);
1314 if (c->rfd != -1)
1315 FD_CLR(c->rfd, readset);
1316 if (c->efd != -1) {
1317 if (c->extended_usage == CHAN_EXTENDED_READ)
1318 FD_CLR(c->efd, readset);
1319 if (c->extended_usage == CHAN_EXTENDED_WRITE)
1320 FD_CLR(c->efd, writeset);
1321 }
1322 channel_free(c->self);
1323 }
Damien Millerb38eff82000-04-01 11:09:21 +10001324 }
1325}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001326
Damien Miller4af51302000-04-16 11:18:38 +10001327void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001328channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1329 int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001330{
Damien Miller5e953212001-01-30 09:14:00 +11001331 int n;
1332 u_int sz;
1333
1334 n = MAX(*maxfdp, channel_max_fd);
1335
1336 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1337 if (*readsetp == NULL || n > *maxfdp) {
1338 if (*readsetp)
1339 xfree(*readsetp);
1340 if (*writesetp)
1341 xfree(*writesetp);
1342 *readsetp = xmalloc(sz);
1343 *writesetp = xmalloc(sz);
1344 *maxfdp = n;
1345 }
1346 memset(*readsetp, 0, sz);
1347 memset(*writesetp, 0, sz);
1348
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001349 if (!rekeying)
1350 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001351}
1352
Damien Miller4af51302000-04-16 11:18:38 +10001353void
Damien Miller95def091999-11-25 00:26:21 +11001354channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001355{
Damien Millerb38eff82000-04-01 11:09:21 +10001356 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001357}
1358
Damien Miller5e953212001-01-30 09:14:00 +11001359/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001360
Damien Miller4af51302000-04-16 11:18:38 +10001361void
Damien Miller95def091999-11-25 00:26:21 +11001362channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001363{
Damien Miller95def091999-11-25 00:26:21 +11001364 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001365 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001366
Damien Miller95def091999-11-25 00:26:21 +11001367 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001368 c = &channels[i];
Damien Miller34132e52000-01-14 15:45:46 +11001369
Damien Miller5428f641999-11-25 11:54:57 +11001370 /* We are only interested in channels that can have buffered incoming data. */
Damien Miller34132e52000-01-14 15:45:46 +11001371 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001372 if (c->type != SSH_CHANNEL_OPEN &&
1373 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001374 continue;
1375 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001376 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001377 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001378 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001379 if (compat20 &&
1380 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001381 /* XXX is this true? */
Ben Lindstromb1131e92001-03-05 07:27:13 +00001382 debug2("channel %d: no data after CLOSE", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001383 continue;
1384 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001385
Damien Miller95def091999-11-25 00:26:21 +11001386 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001387 if ((c->istate == CHAN_INPUT_OPEN ||
1388 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1389 (len = buffer_len(&c->input)) > 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001390 /* Send some data for the other side over the secure connection. */
Damien Miller33b13562000-04-04 14:38:59 +10001391 if (compat20) {
1392 if (len > c->remote_window)
1393 len = c->remote_window;
1394 if (len > c->remote_maxpacket)
1395 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001396 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001397 if (packet_is_interactive()) {
1398 if (len > 1024)
1399 len = 512;
1400 } else {
1401 /* Keep the packets at reasonable size. */
1402 if (len > packet_get_maxsize()/2)
1403 len = packet_get_maxsize()/2;
1404 }
Damien Miller95def091999-11-25 00:26:21 +11001405 }
Damien Millerb38eff82000-04-01 11:09:21 +10001406 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001407 packet_start(compat20 ?
1408 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001409 packet_put_int(c->remote_id);
1410 packet_put_string(buffer_ptr(&c->input), len);
1411 packet_send();
1412 buffer_consume(&c->input, len);
1413 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001414 }
1415 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001416 if (compat13)
1417 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001418 /*
1419 * input-buffer is empty and read-socket shutdown:
1420 * tell peer, that we will not send more data: send IEOF
1421 */
Damien Millerb38eff82000-04-01 11:09:21 +10001422 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001423 }
Damien Miller33b13562000-04-04 14:38:59 +10001424 /* Send extended data, i.e. stderr */
1425 if (compat20 &&
1426 c->remote_window > 0 &&
1427 (len = buffer_len(&c->extended)) > 0 &&
1428 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001429 debug2("channel %d: rwin %d elen %d euse %d",
1430 c->self, c->remote_window, buffer_len(&c->extended),
1431 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001432 if (len > c->remote_window)
1433 len = c->remote_window;
1434 if (len > c->remote_maxpacket)
1435 len = c->remote_maxpacket;
1436 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1437 packet_put_int(c->remote_id);
1438 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1439 packet_put_string(buffer_ptr(&c->extended), len);
1440 packet_send();
1441 buffer_consume(&c->extended, len);
1442 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001443 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001444 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001445 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001446}
1447
Damien Miller5428f641999-11-25 11:54:57 +11001448/*
1449 * This is called when a packet of type CHANNEL_DATA has just been received.
1450 * The message type has already been consumed, but channel number and data is
1451 * still there.
1452 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001453
Damien Miller4af51302000-04-16 11:18:38 +10001454void
Damien Miller62cee002000-09-23 17:15:56 +11001455channel_input_data(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001456{
Damien Miller34132e52000-01-14 15:45:46 +11001457 int id;
Damien Miller95def091999-11-25 00:26:21 +11001458 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001459 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001460 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001461
Damien Miller95def091999-11-25 00:26:21 +11001462 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001463 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001464 c = channel_lookup(id);
1465 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001466 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001467
Damien Miller95def091999-11-25 00:26:21 +11001468 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001469 if (c->type != SSH_CHANNEL_OPEN &&
1470 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001471 return;
1472
1473 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001474 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001475 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001476
Damien Miller95def091999-11-25 00:26:21 +11001477 /* Get the data. */
1478 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001479 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001480
Damien Miller33b13562000-04-04 14:38:59 +10001481 if (compat20){
1482 if (data_len > c->local_maxpacket) {
1483 log("channel %d: rcvd big packet %d, maxpack %d",
1484 c->self, data_len, c->local_maxpacket);
1485 }
1486 if (data_len > c->local_window) {
1487 log("channel %d: rcvd too much data %d, win %d",
1488 c->self, data_len, c->local_window);
1489 xfree(data);
1490 return;
1491 }
1492 c->local_window -= data_len;
1493 }else{
1494 packet_integrity_check(plen, 4 + 4 + data_len, type);
1495 }
Damien Millerb38eff82000-04-01 11:09:21 +10001496 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001497 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001498}
Damien Miller4af51302000-04-16 11:18:38 +10001499void
Damien Miller62cee002000-09-23 17:15:56 +11001500channel_input_extended_data(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001501{
1502 int id;
1503 int tcode;
1504 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001505 u_int data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001506 Channel *c;
1507
1508 /* Get the channel number and verify it. */
1509 id = packet_get_int();
1510 c = channel_lookup(id);
1511
1512 if (c == NULL)
1513 packet_disconnect("Received extended_data for bad channel %d.", id);
1514 if (c->type != SSH_CHANNEL_OPEN) {
1515 log("channel %d: ext data for non open", id);
1516 return;
1517 }
1518 tcode = packet_get_int();
1519 if (c->efd == -1 ||
1520 c->extended_usage != CHAN_EXTENDED_WRITE ||
1521 tcode != SSH2_EXTENDED_DATA_STDERR) {
1522 log("channel %d: bad ext data", c->self);
1523 return;
1524 }
1525 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001526 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001527 if (data_len > c->local_window) {
1528 log("channel %d: rcvd too much extended_data %d, win %d",
1529 c->self, data_len, c->local_window);
1530 xfree(data);
1531 return;
1532 }
Damien Millerd3444942000-09-30 14:20:03 +11001533 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001534 c->local_window -= data_len;
1535 buffer_append(&c->extended, data, data_len);
1536 xfree(data);
1537}
1538
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001539
Damien Miller5428f641999-11-25 11:54:57 +11001540/*
1541 * Returns true if no channel has too much buffered data, and false if one or
1542 * more channel is overfull.
1543 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001544
Damien Miller4af51302000-04-16 11:18:38 +10001545int
Damien Miller95def091999-11-25 00:26:21 +11001546channel_not_very_much_buffered_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001547{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001548 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001549 Channel *c;
Damien Miller95def091999-11-25 00:26:21 +11001550
1551 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001552 c = &channels[i];
1553 if (c->type == SSH_CHANNEL_OPEN) {
Damien Miller33b13562000-04-04 14:38:59 +10001554 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
Damien Millerb38eff82000-04-01 11:09:21 +10001555 debug("channel %d: big input buffer %d",
1556 c->self, buffer_len(&c->input));
Damien Miller95def091999-11-25 00:26:21 +11001557 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001558 }
1559 if (buffer_len(&c->output) > packet_get_maxsize()) {
1560 debug("channel %d: big output buffer %d",
1561 c->self, buffer_len(&c->output));
Damien Miller95def091999-11-25 00:26:21 +11001562 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001563 }
Damien Miller95def091999-11-25 00:26:21 +11001564 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001565 }
Damien Miller95def091999-11-25 00:26:21 +11001566 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001567}
1568
Damien Miller4af51302000-04-16 11:18:38 +10001569void
Damien Miller62cee002000-09-23 17:15:56 +11001570channel_input_ieof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001571{
1572 int id;
1573 Channel *c;
1574
1575 packet_integrity_check(plen, 4, type);
1576
1577 id = packet_get_int();
1578 c = channel_lookup(id);
1579 if (c == NULL)
1580 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1581 chan_rcvd_ieof(c);
1582}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001583
Damien Miller4af51302000-04-16 11:18:38 +10001584void
Damien Miller62cee002000-09-23 17:15:56 +11001585channel_input_close(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001586{
Damien Millerb38eff82000-04-01 11:09:21 +10001587 int id;
1588 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001589
Damien Millerb38eff82000-04-01 11:09:21 +10001590 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001591
Damien Millerb38eff82000-04-01 11:09:21 +10001592 id = packet_get_int();
1593 c = channel_lookup(id);
1594 if (c == NULL)
1595 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001596
1597 /*
1598 * Send a confirmation that we have closed the channel and no more
1599 * data is coming for it.
1600 */
Damien Miller95def091999-11-25 00:26:21 +11001601 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001602 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001603 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001604
Damien Miller5428f641999-11-25 11:54:57 +11001605 /*
1606 * If the channel is in closed state, we have sent a close request,
1607 * and the other side will eventually respond with a confirmation.
1608 * Thus, we cannot free the channel here, because then there would be
1609 * no-one to receive the confirmation. The channel gets freed when
1610 * the confirmation arrives.
1611 */
Damien Millerb38eff82000-04-01 11:09:21 +10001612 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001613 /*
1614 * Not a closed channel - mark it as draining, which will
1615 * cause it to be freed later.
1616 */
Damien Millerb38eff82000-04-01 11:09:21 +10001617 buffer_consume(&c->input, buffer_len(&c->input));
1618 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001619 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001620}
1621
Damien Millerb38eff82000-04-01 11:09:21 +10001622/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001623void
Damien Miller62cee002000-09-23 17:15:56 +11001624channel_input_oclose(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001625{
Damien Millerb38eff82000-04-01 11:09:21 +10001626 int id = packet_get_int();
1627 Channel *c = channel_lookup(id);
1628 packet_integrity_check(plen, 4, type);
1629 if (c == NULL)
1630 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1631 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001632}
1633
Damien Miller4af51302000-04-16 11:18:38 +10001634void
Damien Miller62cee002000-09-23 17:15:56 +11001635channel_input_close_confirmation(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001636{
1637 int id = packet_get_int();
1638 Channel *c = channel_lookup(id);
1639
Damien Miller4af51302000-04-16 11:18:38 +10001640 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001641 if (c == NULL)
1642 packet_disconnect("Received close confirmation for "
1643 "out-of-range channel %d.", id);
1644 if (c->type != SSH_CHANNEL_CLOSED)
1645 packet_disconnect("Received close confirmation for "
1646 "non-closed channel %d (type %d).", id, c->type);
1647 channel_free(c->self);
1648}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001649
Damien Miller4af51302000-04-16 11:18:38 +10001650void
Damien Miller62cee002000-09-23 17:15:56 +11001651channel_input_open_confirmation(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001652{
Damien Millerb38eff82000-04-01 11:09:21 +10001653 int id, remote_id;
1654 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001655
Damien Miller33b13562000-04-04 14:38:59 +10001656 if (!compat20)
1657 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001658
Damien Millerb38eff82000-04-01 11:09:21 +10001659 id = packet_get_int();
1660 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001661
Damien Millerb38eff82000-04-01 11:09:21 +10001662 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1663 packet_disconnect("Received open confirmation for "
1664 "non-opening channel %d.", id);
1665 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001666 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001667 c->remote_id = remote_id;
1668 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001669
1670 if (compat20) {
1671 c->remote_window = packet_get_int();
1672 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001673 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001674 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001675 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001676 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001677 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001678 }
1679 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1680 c->remote_window, c->remote_maxpacket);
1681 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001682}
1683
Damien Miller4af51302000-04-16 11:18:38 +10001684void
Damien Miller62cee002000-09-23 17:15:56 +11001685channel_input_open_failure(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001686{
Kevin Steves12057502001-02-05 14:54:34 +00001687 int id, reason;
1688 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001689 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001690
Damien Miller33b13562000-04-04 14:38:59 +10001691 if (!compat20)
1692 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001693
1694 id = packet_get_int();
1695 c = channel_lookup(id);
1696
1697 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1698 packet_disconnect("Received open failure for "
1699 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001700 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00001701 reason = packet_get_int();
1702 if (packet_remaining() > 0) {
1703 msg = packet_get_string(NULL);
1704 lang = packet_get_string(NULL);
1705 }
Damien Miller4af51302000-04-16 11:18:38 +10001706 packet_done();
Kevin Steves12057502001-02-05 14:54:34 +00001707 log("channel_open_failure: %d: reason %d %s", id,
1708 reason, msg ? msg : "<no additional info>");
1709 if (msg != NULL)
1710 xfree(msg);
1711 if (lang != NULL)
1712 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001713 }
Damien Miller95def091999-11-25 00:26:21 +11001714 /* Free the channel. This will also close the socket. */
Damien Millerb38eff82000-04-01 11:09:21 +10001715 channel_free(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001716}
1717
Damien Miller33b13562000-04-04 14:38:59 +10001718void
Damien Miller62cee002000-09-23 17:15:56 +11001719channel_input_channel_request(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001720{
1721 int id;
1722 Channel *c;
1723
1724 id = packet_get_int();
1725 c = channel_lookup(id);
1726
1727 if (c == NULL ||
1728 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1729 packet_disconnect("Received request for "
1730 "non-open channel %d.", id);
1731 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001732 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001733 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001734 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001735 } else {
1736 char *service = packet_get_string(NULL);
Ben Lindstromcc74df72001-03-05 06:20:14 +00001737 debug("channel %d: rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001738 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001739 xfree(service);
1740 }
1741}
1742
Damien Miller4af51302000-04-16 11:18:38 +10001743void
Damien Miller62cee002000-09-23 17:15:56 +11001744channel_input_window_adjust(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001745{
1746 Channel *c;
1747 int id, adjust;
1748
1749 if (!compat20)
1750 return;
1751
1752 /* Get the channel number and verify it. */
1753 id = packet_get_int();
1754 c = channel_lookup(id);
1755
1756 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1757 log("Received window adjust for "
1758 "non-open channel %d.", id);
1759 return;
1760 }
1761 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001762 packet_done();
Damien Millerd3444942000-09-30 14:20:03 +11001763 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10001764 c->remote_window += adjust;
1765}
1766
Damien Miller5428f641999-11-25 11:54:57 +11001767/*
1768 * Stops listening for channels, and removes any unix domain sockets that we
1769 * might have.
1770 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001771
Damien Miller4af51302000-04-16 11:18:38 +10001772void
Damien Miller95def091999-11-25 00:26:21 +11001773channel_stop_listening()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001774{
Damien Miller95def091999-11-25 00:26:21 +11001775 int i;
1776 for (i = 0; i < channels_alloc; i++) {
1777 switch (channels[i].type) {
1778 case SSH_CHANNEL_AUTH_SOCKET:
1779 close(channels[i].sock);
Damien Miller78315eb2000-09-29 23:01:36 +11001780 unlink(channels[i].path);
Damien Miller95def091999-11-25 00:26:21 +11001781 channel_free(i);
1782 break;
1783 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001784 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001785 case SSH_CHANNEL_X11_LISTENER:
1786 close(channels[i].sock);
1787 channel_free(i);
1788 break;
1789 default:
1790 break;
1791 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001792 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001793}
1794
Damien Miller5428f641999-11-25 11:54:57 +11001795/*
Damien Miller6f83b8e2000-05-02 09:23:45 +10001796 * Closes the sockets/fds of all channels. This is used to close extra file
Damien Miller5428f641999-11-25 11:54:57 +11001797 * descriptors after a fork.
1798 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001799
Damien Miller4af51302000-04-16 11:18:38 +10001800void
Damien Miller95def091999-11-25 00:26:21 +11001801channel_close_all()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001802{
Damien Miller95def091999-11-25 00:26:21 +11001803 int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +10001804 for (i = 0; i < channels_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001805 if (channels[i].type != SSH_CHANNEL_FREE)
Damien Miller6f83b8e2000-05-02 09:23:45 +10001806 channel_close_fds(&channels[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001807}
1808
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001809/* Returns true if any channel is still open. */
1810
Damien Miller4af51302000-04-16 11:18:38 +10001811int
Damien Miller95def091999-11-25 00:26:21 +11001812channel_still_open()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001813{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001814 u_int i;
Damien Miller95def091999-11-25 00:26:21 +11001815 for (i = 0; i < channels_alloc; i++)
1816 switch (channels[i].type) {
1817 case SSH_CHANNEL_FREE:
1818 case SSH_CHANNEL_X11_LISTENER:
1819 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001820 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001821 case SSH_CHANNEL_CLOSED:
1822 case SSH_CHANNEL_AUTH_SOCKET:
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001823 case SSH_CHANNEL_DYNAMIC:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001824 case SSH_CHANNEL_CONNECTING: /* XXX ??? */
Damien Miller95def091999-11-25 00:26:21 +11001825 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001826 case SSH_CHANNEL_LARVAL:
1827 if (!compat20)
1828 fatal("cannot happen: SSH_CHANNEL_LARVAL");
1829 continue;
Damien Miller95def091999-11-25 00:26:21 +11001830 case SSH_CHANNEL_OPENING:
1831 case SSH_CHANNEL_OPEN:
1832 case SSH_CHANNEL_X11_OPEN:
1833 return 1;
1834 case SSH_CHANNEL_INPUT_DRAINING:
1835 case SSH_CHANNEL_OUTPUT_DRAINING:
1836 if (!compat13)
1837 fatal("cannot happen: OUT_DRAIN");
1838 return 1;
1839 default:
1840 fatal("channel_still_open: bad channel type %d", channels[i].type);
1841 /* NOTREACHED */
1842 }
1843 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001844}
1845
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001846/* Returns the id of an open channel suitable for keepaliving */
1847
1848int
1849channel_find_open()
1850{
1851 u_int i;
1852 for (i = 0; i < channels_alloc; i++)
1853 switch (channels[i].type) {
1854 case SSH_CHANNEL_CLOSED:
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001855 case SSH_CHANNEL_DYNAMIC:
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001856 case SSH_CHANNEL_FREE:
1857 case SSH_CHANNEL_X11_LISTENER:
1858 case SSH_CHANNEL_PORT_LISTENER:
1859 case SSH_CHANNEL_RPORT_LISTENER:
1860 case SSH_CHANNEL_OPENING:
Ben Lindstromd334b272001-04-14 23:08:36 +00001861 continue;
1862 case SSH_CHANNEL_LARVAL:
1863 case SSH_CHANNEL_AUTH_SOCKET:
1864 case SSH_CHANNEL_CONNECTING: /* XXX ??? */
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001865 case SSH_CHANNEL_OPEN:
1866 case SSH_CHANNEL_X11_OPEN:
1867 return i;
1868 case SSH_CHANNEL_INPUT_DRAINING:
1869 case SSH_CHANNEL_OUTPUT_DRAINING:
1870 if (!compat13)
1871 fatal("cannot happen: OUT_DRAIN");
1872 return i;
1873 default:
1874 fatal("channel_find_open: bad channel type %d", channels[i].type);
1875 /* NOTREACHED */
1876 }
1877 return -1;
1878}
1879
1880
Damien Miller5428f641999-11-25 11:54:57 +11001881/*
1882 * Returns a message describing the currently open forwarded connections,
1883 * suitable for sending to the client. The message contains crlf pairs for
1884 * newlines.
1885 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001886
Damien Miller95def091999-11-25 00:26:21 +11001887char *
1888channel_open_message()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001889{
Damien Miller95def091999-11-25 00:26:21 +11001890 Buffer buffer;
1891 int i;
1892 char buf[512], *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001893
Damien Miller95def091999-11-25 00:26:21 +11001894 buffer_init(&buffer);
1895 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001896 buffer_append(&buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001897 for (i = 0; i < channels_alloc; i++) {
1898 Channel *c = &channels[i];
1899 switch (c->type) {
1900 case SSH_CHANNEL_FREE:
1901 case SSH_CHANNEL_X11_LISTENER:
1902 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001903 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001904 case SSH_CHANNEL_CLOSED:
1905 case SSH_CHANNEL_AUTH_SOCKET:
1906 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001907 case SSH_CHANNEL_LARVAL:
Damien Miller95def091999-11-25 00:26:21 +11001908 case SSH_CHANNEL_OPENING:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001909 case SSH_CHANNEL_CONNECTING:
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001910 case SSH_CHANNEL_DYNAMIC:
Damien Miller95def091999-11-25 00:26:21 +11001911 case SSH_CHANNEL_OPEN:
1912 case SSH_CHANNEL_X11_OPEN:
1913 case SSH_CHANNEL_INPUT_DRAINING:
1914 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Millerb38eff82000-04-01 11:09:21 +10001915 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 +11001916 c->self, c->remote_name,
1917 c->type, c->remote_id,
1918 c->istate, buffer_len(&c->input),
Damien Millerb38eff82000-04-01 11:09:21 +10001919 c->ostate, buffer_len(&c->output),
1920 c->rfd, c->wfd);
Damien Miller95def091999-11-25 00:26:21 +11001921 buffer_append(&buffer, buf, strlen(buf));
1922 continue;
1923 default:
Damien Millerb38eff82000-04-01 11:09:21 +10001924 fatal("channel_open_message: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001925 /* NOTREACHED */
1926 }
1927 }
1928 buffer_append(&buffer, "\0", 1);
1929 cp = xstrdup(buffer_ptr(&buffer));
1930 buffer_free(&buffer);
1931 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001932}
1933
Damien Miller5428f641999-11-25 11:54:57 +11001934/*
1935 * Initiate forwarding of connections to local port "port" through the secure
1936 * channel to host:port from remote side.
1937 */
Kevin Steves12057502001-02-05 14:54:34 +00001938int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001939channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
1940 u_short port_to_connect, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001941{
Kevin Steves12057502001-02-05 14:54:34 +00001942 return channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +11001943 NULL, listen_port,
1944 host_to_connect, port_to_connect,
1945 gateway_ports, /*remote_fwd*/ 0);
1946}
1947
1948/*
1949 * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
1950 * (SSH_CHANNEL_RPORT_LISTENER).
1951 */
Kevin Steves12057502001-02-05 14:54:34 +00001952int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001953channel_request_forwarding(
1954 const char *listen_address, u_short listen_port,
1955 const char *host_to_connect, u_short port_to_connect,
1956 int gateway_ports, int remote_fwd)
1957{
1958 int success, ch, sock, on = 1, ctype;
Damien Miller34132e52000-01-14 15:45:46 +11001959 struct addrinfo hints, *ai, *aitop;
1960 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0bc1bd82000-11-13 22:57:25 +11001961 const char *host;
Damien Miller5428f641999-11-25 11:54:57 +11001962 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001963
Kevin Steves12057502001-02-05 14:54:34 +00001964 success = 0;
1965
Damien Miller0bc1bd82000-11-13 22:57:25 +11001966 if (remote_fwd) {
1967 host = listen_address;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001968 ctype = SSH_CHANNEL_RPORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001969 } else {
1970 host = host_to_connect;
1971 ctype =SSH_CHANNEL_PORT_LISTENER;
1972 }
1973
Kevin Steves12057502001-02-05 14:54:34 +00001974 if (strlen(host) > sizeof(channels[0].path) - 1) {
1975 error("Forward host name too long.");
1976 return success;
1977 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001978
Damien Miller0bc1bd82000-11-13 22:57:25 +11001979 /* XXX listen_address is currently ignored */
Damien Miller5428f641999-11-25 11:54:57 +11001980 /*
Damien Miller34132e52000-01-14 15:45:46 +11001981 * getaddrinfo returns a loopback address if the hostname is
1982 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11001983 */
Damien Miller34132e52000-01-14 15:45:46 +11001984 memset(&hints, 0, sizeof(hints));
1985 hints.ai_family = IPv4or6;
1986 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
1987 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001988 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001989 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
1990 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11001991
Damien Miller34132e52000-01-14 15:45:46 +11001992 for (ai = aitop; ai; ai = ai->ai_next) {
1993 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1994 continue;
1995 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1996 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001997 error("channel_request_forwarding: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001998 continue;
1999 }
2000 /* Create a port to listen for the host. */
2001 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2002 if (sock < 0) {
2003 /* this is no error since kernel may not support ipv6 */
2004 verbose("socket: %.100s", strerror(errno));
2005 continue;
2006 }
2007 /*
2008 * Set socket options. We would like the socket to disappear
2009 * as soon as it has been closed for whatever reason.
2010 */
2011 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
2012 linger.l_onoff = 1;
2013 linger.l_linger = 5;
2014 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
2015 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002016
Damien Miller34132e52000-01-14 15:45:46 +11002017 /* Bind the socket to the address. */
2018 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2019 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002020 if (!ai->ai_next)
2021 error("bind: %.100s", strerror(errno));
2022 else
2023 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002024
Damien Miller34132e52000-01-14 15:45:46 +11002025 close(sock);
2026 continue;
2027 }
2028 /* Start listening for connections on the socket. */
2029 if (listen(sock, 5) < 0) {
2030 error("listen: %.100s", strerror(errno));
2031 close(sock);
2032 continue;
2033 }
2034 /* Allocate a channel number for the socket. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002035 ch = channel_new("port listener", ctype, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002036 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002037 0, xstrdup("port listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11002038 strlcpy(channels[ch].path, host, sizeof(channels[ch].path));
Damien Miller0bc1bd82000-11-13 22:57:25 +11002039 channels[ch].host_port = port_to_connect;
2040 channels[ch].listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002041 success = 1;
2042 }
2043 if (success == 0)
Kevin Steves12057502001-02-05 14:54:34 +00002044 error("channel_request_forwarding: cannot listen to port: %d",
2045 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002046 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002047 return success;
Damien Miller95def091999-11-25 00:26:21 +11002048}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002049
Damien Miller5428f641999-11-25 11:54:57 +11002050/*
2051 * Initiate forwarding of connections to port "port" on remote host through
2052 * the secure channel to host:port from local side.
2053 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002054
Damien Miller4af51302000-04-16 11:18:38 +10002055void
Damien Miller0bc1bd82000-11-13 22:57:25 +11002056channel_request_remote_forwarding(u_short listen_port,
2057 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002058{
Damien Miller0bc1bd82000-11-13 22:57:25 +11002059 int payload_len, type, success = 0;
2060
Damien Miller95def091999-11-25 00:26:21 +11002061 /* Record locally that connection to this host/port is permitted. */
2062 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2063 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002064
Damien Miller95def091999-11-25 00:26:21 +11002065 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002066 if (compat20) {
2067 const char *address_to_bind = "0.0.0.0";
2068 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2069 packet_put_cstring("tcpip-forward");
2070 packet_put_char(0); /* boolean: want reply */
2071 packet_put_cstring(address_to_bind);
2072 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002073 packet_send();
2074 packet_write_wait();
2075 /* Assume that server accepts the request */
2076 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002077 } else {
2078 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002079 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002080 packet_put_cstring(host_to_connect);
2081 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002082 packet_send();
2083 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002084
2085 /* Wait for response from the remote side. */
2086 type = packet_read(&payload_len);
2087 switch (type) {
2088 case SSH_SMSG_SUCCESS:
2089 success = 1;
2090 break;
2091 case SSH_SMSG_FAILURE:
2092 log("Warning: Server denied remote port forwarding.");
2093 break;
2094 default:
2095 /* Unknown packet */
2096 packet_disconnect("Protocol error for port forward request:"
2097 "received packet type %d.", type);
2098 }
2099 }
2100 if (success) {
2101 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2102 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2103 permitted_opens[num_permitted_opens].listen_port = listen_port;
2104 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002105 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002106}
2107
Damien Miller5428f641999-11-25 11:54:57 +11002108/*
2109 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2110 * listening for the port, and sends back a success reply (or disconnect
2111 * message if there was an error). This never returns if there was an error.
2112 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002113
Damien Miller4af51302000-04-16 11:18:38 +10002114void
Damien Millere247cc42000-05-07 12:03:14 +10002115channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002116{
Damien Milleraae6c611999-12-06 11:47:28 +11002117 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11002118 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002119
Damien Miller95def091999-11-25 00:26:21 +11002120 /* Get arguments from the packet. */
2121 port = packet_get_int();
2122 hostname = packet_get_string(NULL);
2123 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002124
Damien Millerbac2d8a2000-09-05 16:13:06 +11002125#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002126 /*
2127 * Check that an unprivileged user is not trying to forward a
2128 * privileged port.
2129 */
Damien Miller95def091999-11-25 00:26:21 +11002130 if (port < IPPORT_RESERVED && !is_root)
2131 packet_disconnect("Requested forwarding of port %d but user is not root.",
2132 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11002133#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11002134 /* Initiate forwarding */
Damien Millere247cc42000-05-07 12:03:14 +10002135 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002136
2137 /* Free the argument string. */
2138 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002139}
2140
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002141/*
2142 * Permits opening to any host/port if permitted_opens[] is empty. This is
2143 * usually called by the server, because the user could connect to any port
2144 * anyway, and the server has no way to know but to trust the client anyway.
2145 */
2146void
2147channel_permit_all_opens()
2148{
2149 if (num_permitted_opens == 0)
2150 all_opens_permitted = 1;
2151}
2152
Ben Lindstroma3700052001-04-05 23:26:32 +00002153void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002154channel_add_permitted_opens(char *host, int port)
2155{
2156 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2157 fatal("channel_request_remote_forwarding: too many forwards");
2158 debug("allow port forwarding to host %s port %d", host, port);
2159
2160 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2161 permitted_opens[num_permitted_opens].port_to_connect = port;
2162 num_permitted_opens++;
2163
2164 all_opens_permitted = 0;
2165}
2166
Ben Lindstroma3700052001-04-05 23:26:32 +00002167void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002168channel_clear_permitted_opens(void)
2169{
2170 int i;
2171
2172 for (i = 0; i < num_permitted_opens; i++)
2173 xfree(permitted_opens[i].host_to_connect);
2174 num_permitted_opens = 0;
2175
2176}
2177
2178
2179/* return socket to remote host, port */
Damien Millerb38eff82000-04-01 11:09:21 +10002180int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002181connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002182{
Damien Miller34132e52000-01-14 15:45:46 +11002183 struct addrinfo hints, *ai, *aitop;
2184 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2185 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002186 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002187
Damien Miller34132e52000-01-14 15:45:46 +11002188 memset(&hints, 0, sizeof(hints));
2189 hints.ai_family = IPv4or6;
2190 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002191 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002192 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002193 error("connect_to %.100s: unknown host (%s)", host,
2194 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002195 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002196 }
Damien Miller34132e52000-01-14 15:45:46 +11002197 for (ai = aitop; ai; ai = ai->ai_next) {
2198 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2199 continue;
2200 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2201 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002202 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002203 continue;
2204 }
Damien Miller34132e52000-01-14 15:45:46 +11002205 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2206 if (sock < 0) {
2207 error("socket: %.100s", strerror(errno));
2208 continue;
2209 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +00002210 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002211 fatal("connect_to: F_SETFL: %s", strerror(errno));
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002212 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2213 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002214 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002215 strerror(errno));
2216 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002217 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002218 }
2219 break; /* success */
2220
2221 }
2222 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002223 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002224 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002225 return -1;
2226 }
2227 /* success */
2228 return sock;
2229}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002230
Damien Miller0bc1bd82000-11-13 22:57:25 +11002231int
2232channel_connect_by_listen_adress(u_short listen_port)
2233{
2234 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002235
Damien Miller0bc1bd82000-11-13 22:57:25 +11002236 for (i = 0; i < num_permitted_opens; i++)
2237 if (permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002238 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002239 permitted_opens[i].host_to_connect,
2240 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002241 error("WARNING: Server requests forwarding for unknown listen_port %d",
2242 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002243 return -1;
2244}
Damien Millerb38eff82000-04-01 11:09:21 +10002245
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002246/* Check if connecting to that port is permitted and connect. */
2247int
2248channel_connect_to(const char *host, u_short port)
2249{
2250 int i, permit;
2251
2252 permit = all_opens_permitted;
2253 if (!permit) {
2254 for (i = 0; i < num_permitted_opens; i++)
2255 if (permitted_opens[i].port_to_connect == port &&
2256 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2257 permit = 1;
2258
2259 }
2260 if (!permit) {
2261 log("Received request to connect to host %.100s port %d, "
2262 "but the request was denied.", host, port);
2263 return -1;
2264 }
2265 return connect_to(host, port);
2266}
2267
Damien Millerb38eff82000-04-01 11:09:21 +10002268/*
2269 * This is called after receiving PORT_OPEN message. This attempts to
2270 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
2271 * or CHANNEL_OPEN_FAILURE.
2272 */
2273
Damien Miller4af51302000-04-16 11:18:38 +10002274void
Damien Miller62cee002000-09-23 17:15:56 +11002275channel_input_port_open(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002276{
2277 u_short host_port;
2278 char *host, *originator_string;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002279 int remote_channel, sock = -1, newch;
Damien Millerb38eff82000-04-01 11:09:21 +10002280
Damien Millerb38eff82000-04-01 11:09:21 +10002281 remote_channel = packet_get_int();
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002282 host = packet_get_string(NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10002283 host_port = packet_get_int();
2284
Damien Millerb38eff82000-04-01 11:09:21 +10002285 if (have_hostname_in_open) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002286 originator_string = packet_get_string(NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10002287 } else {
2288 originator_string = xstrdup("unknown (remote did not supply name)");
Damien Miller95def091999-11-25 00:26:21 +11002289 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002290 packet_done();
2291 sock = channel_connect_to(host, host_port);
2292 if (sock != -1) {
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002293 newch = channel_allocate(SSH_CHANNEL_CONNECTING,
2294 sock, originator_string);
Damien Millerb38eff82000-04-01 11:09:21 +10002295 channels[newch].remote_id = remote_channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002296
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002297 /*XXX delay answer? */
Damien Millerb38eff82000-04-01 11:09:21 +10002298 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2299 packet_put_int(remote_channel);
2300 packet_put_int(newch);
2301 packet_send();
2302 } else {
2303 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2304 packet_put_int(remote_channel);
2305 packet_send();
2306 }
Damien Miller95def091999-11-25 00:26:21 +11002307 xfree(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002308}
2309
Damien Miller5428f641999-11-25 11:54:57 +11002310/*
2311 * Creates an internet domain socket for listening for X11 connections.
2312 * Returns a suitable value for the DISPLAY variable, or NULL if an error
2313 * occurs.
2314 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002315
Damien Miller34132e52000-01-14 15:45:46 +11002316#define NUM_SOCKS 10
2317
Damien Miller95def091999-11-25 00:26:21 +11002318char *
Damien Millera34a28b1999-12-14 10:47:15 +11002319x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002320{
Damien Milleraae6c611999-12-06 11:47:28 +11002321 int display_number, sock;
2322 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002323 struct addrinfo hints, *ai, *aitop;
2324 char strport[NI_MAXSERV];
2325 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
2326 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11002327 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002328
Damien Millera34a28b1999-12-14 10:47:15 +11002329 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11002330 display_number < MAX_DISPLAYS;
2331 display_number++) {
2332 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002333 memset(&hints, 0, sizeof(hints));
2334 hints.ai_family = IPv4or6;
2335 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
2336 hints.ai_socktype = SOCK_STREAM;
2337 snprintf(strport, sizeof strport, "%d", port);
2338 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2339 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11002340 return NULL;
2341 }
Damien Miller34132e52000-01-14 15:45:46 +11002342 for (ai = aitop; ai; ai = ai->ai_next) {
2343 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2344 continue;
2345 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2346 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002347 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002348 error("socket: %.100s", strerror(errno));
2349 return NULL;
2350 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002351 debug("x11_create_display_inet: Socket family %d not supported",
2352 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002353 continue;
2354 }
Damien Miller34132e52000-01-14 15:45:46 +11002355 }
2356 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2357 debug("bind port %d: %.100s", port, strerror(errno));
2358 shutdown(sock, SHUT_RDWR);
2359 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002360
2361 if (ai->ai_next)
2362 continue;
2363
Damien Miller34132e52000-01-14 15:45:46 +11002364 for (n = 0; n < num_socks; n++) {
2365 shutdown(socks[n], SHUT_RDWR);
2366 close(socks[n]);
2367 }
2368 num_socks = 0;
2369 break;
2370 }
2371 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002372#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002373 if (num_socks == NUM_SOCKS)
2374 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002375#else
2376 break;
2377#endif
Damien Miller95def091999-11-25 00:26:21 +11002378 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002379 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002380 if (num_socks > 0)
2381 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002382 }
Damien Miller95def091999-11-25 00:26:21 +11002383 if (display_number >= MAX_DISPLAYS) {
2384 error("Failed to allocate internet-domain X11 display socket.");
2385 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002386 }
Damien Miller95def091999-11-25 00:26:21 +11002387 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002388 for (n = 0; n < num_socks; n++) {
2389 sock = socks[n];
2390 if (listen(sock, 5) < 0) {
2391 error("listen: %.100s", strerror(errno));
2392 shutdown(sock, SHUT_RDWR);
2393 close(sock);
2394 return NULL;
2395 }
Damien Miller95def091999-11-25 00:26:21 +11002396 }
Damien Miller34132e52000-01-14 15:45:46 +11002397
Damien Miller95def091999-11-25 00:26:21 +11002398 /* Set up a suitable value for the DISPLAY variable. */
2399 if (gethostname(hostname, sizeof(hostname)) < 0)
2400 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11002401
2402#ifdef IPADDR_IN_DISPLAY
Kevin Stevesef4eea92001-02-05 12:42:17 +00002403 /*
Damien Miller76112de1999-12-21 11:18:08 +11002404 * HPUX detects the local hostname in the DISPLAY variable and tries
2405 * to set up a shared memory connection to the server, which it
2406 * incorrectly supposes to be local.
2407 *
2408 * The workaround - as used in later $$H and other programs - is
2409 * is to set display to the host's IP address.
2410 */
2411 {
2412 struct hostent *he;
2413 struct in_addr my_addr;
2414
2415 he = gethostbyname(hostname);
2416 if (he == NULL) {
2417 error("[X11-broken-fwd-hostname-workaround] Could not get "
2418 "IP address for hostname %s.", hostname);
2419
2420 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
2421 "Could not get IP address for hostname %s.", hostname);
2422
2423 shutdown(sock, SHUT_RDWR);
2424 close(sock);
2425
2426 return NULL;
2427 }
2428
2429 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2430
2431 /* Set DISPLAY to <ip address>:screen.display */
Kevin Stevesef4eea92001-02-05 12:42:17 +00002432 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Kevin Stevesda6cd592000-12-29 18:41:21 +00002433 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002434 }
2435#else /* IPADDR_IN_DISPLAY */
2436 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11002437 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Kevin Stevesda6cd592000-12-29 18:41:21 +00002438 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002439#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002440
Damien Miller34132e52000-01-14 15:45:46 +11002441 /* Allocate a channel for each socket. */
2442 for (n = 0; n < num_socks; n++) {
2443 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10002444 (void) channel_new("x11 listener",
2445 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2446 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002447 0, xstrdup("X11 inet listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11002448 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002449
Damien Miller95def091999-11-25 00:26:21 +11002450 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11002451 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002452}
2453
2454#ifndef X_UNIX_PATH
2455#define X_UNIX_PATH "/tmp/.X11-unix/X"
2456#endif
2457
2458static
2459int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002460connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002461{
Damien Miller95def091999-11-25 00:26:21 +11002462 static const char *const x_sockets[] = {
2463 X_UNIX_PATH "%u",
2464 "/var/X/.X11-unix/X" "%u",
2465 "/usr/spool/sockets/X11/" "%u",
2466 NULL
2467 };
2468 int sock;
2469 struct sockaddr_un addr;
2470 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002471
Damien Miller95def091999-11-25 00:26:21 +11002472 for (path = x_sockets; *path; ++path) {
2473 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2474 if (sock < 0)
2475 error("socket: %.100s", strerror(errno));
2476 memset(&addr, 0, sizeof(addr));
2477 addr.sun_family = AF_UNIX;
2478 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
2479 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2480 return sock;
2481 close(sock);
2482 }
2483 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2484 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002485}
2486
Damien Millerbd483e72000-04-30 10:00:53 +10002487int
2488x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002489{
Damien Millerbd483e72000-04-30 10:00:53 +10002490 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002491 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002492 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002493 struct addrinfo hints, *ai, *aitop;
2494 char strport[NI_MAXSERV];
2495 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002496
Damien Miller95def091999-11-25 00:26:21 +11002497 /* Try to open a socket for the local X server. */
2498 display = getenv("DISPLAY");
2499 if (!display) {
2500 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002501 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002502 }
Damien Miller5428f641999-11-25 11:54:57 +11002503 /*
2504 * Now we decode the value of the DISPLAY variable and make a
2505 * connection to the real X server.
2506 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002507
Damien Miller5428f641999-11-25 11:54:57 +11002508 /*
2509 * Check if it is a unix domain socket. Unix domain displays are in
2510 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2511 */
Damien Miller95def091999-11-25 00:26:21 +11002512 if (strncmp(display, "unix:", 5) == 0 ||
2513 display[0] == ':') {
2514 /* Connect to the unix domain socket. */
2515 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2516 error("Could not parse display number from DISPLAY: %.100s",
2517 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002518 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002519 }
2520 /* Create a socket. */
2521 sock = connect_local_xsocket(display_number);
2522 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002523 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002524
2525 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002526 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002527 }
Damien Miller5428f641999-11-25 11:54:57 +11002528 /*
2529 * Connect to an inet socket. The DISPLAY value is supposedly
2530 * hostname:d[.s], where hostname may also be numeric IP address.
2531 */
Damien Miller95def091999-11-25 00:26:21 +11002532 strncpy(buf, display, sizeof(buf));
2533 buf[sizeof(buf) - 1] = 0;
2534 cp = strchr(buf, ':');
2535 if (!cp) {
2536 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002537 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002538 }
Damien Miller95def091999-11-25 00:26:21 +11002539 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002540 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002541 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2542 error("Could not parse display number from DISPLAY: %.100s",
2543 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002544 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002545 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002546
Damien Miller34132e52000-01-14 15:45:46 +11002547 /* Look up the host address */
2548 memset(&hints, 0, sizeof(hints));
2549 hints.ai_family = IPv4or6;
2550 hints.ai_socktype = SOCK_STREAM;
2551 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2552 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2553 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002554 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002555 }
Damien Miller34132e52000-01-14 15:45:46 +11002556 for (ai = aitop; ai; ai = ai->ai_next) {
2557 /* Create a socket. */
2558 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2559 if (sock < 0) {
2560 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002561 continue;
2562 }
2563 /* Connect it to the display. */
2564 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2565 debug("connect %.100s port %d: %.100s", buf,
2566 6000 + display_number, strerror(errno));
2567 close(sock);
2568 continue;
2569 }
2570 /* Success */
2571 break;
Damien Miller34132e52000-01-14 15:45:46 +11002572 }
Damien Miller34132e52000-01-14 15:45:46 +11002573 freeaddrinfo(aitop);
2574 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002575 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002576 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002577 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002578 }
Damien Millerbd483e72000-04-30 10:00:53 +10002579 return sock;
2580}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002581
Damien Millerbd483e72000-04-30 10:00:53 +10002582/*
2583 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2584 * the remote channel number. We should do whatever we want, and respond
2585 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2586 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002587
Damien Millerbd483e72000-04-30 10:00:53 +10002588void
Damien Miller62cee002000-09-23 17:15:56 +11002589x11_input_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002590{
2591 int remote_channel, sock = 0, newch;
2592 char *remote_host;
Ben Lindstrom46c16222000-12-22 01:43:59 +00002593 u_int remote_len;
Damien Miller95def091999-11-25 00:26:21 +11002594
Damien Millerbd483e72000-04-30 10:00:53 +10002595 /* Get remote channel number. */
2596 remote_channel = packet_get_int();
Damien Miller95def091999-11-25 00:26:21 +11002597
Damien Millerbd483e72000-04-30 10:00:53 +10002598 /* Get remote originator name. */
2599 if (have_hostname_in_open) {
2600 remote_host = packet_get_string(&remote_len);
2601 remote_len += 4;
2602 } else {
2603 remote_host = xstrdup("unknown (remote did not supply name)");
2604 remote_len = 0;
2605 }
2606
2607 debug("Received X11 open request.");
2608 packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN);
2609
2610 /* Obtain a connection to the real X display. */
2611 sock = x11_connect_display();
2612 if (sock == -1) {
2613 /* Send refusal to the remote host. */
2614 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2615 packet_put_int(remote_channel);
2616 packet_send();
2617 } else {
2618 /* Allocate a channel for this connection. */
2619 newch = channel_allocate(
2620 (x11_saved_proto == NULL) ?
2621 SSH_CHANNEL_OPEN : SSH_CHANNEL_X11_OPEN,
2622 sock, remote_host);
2623 channels[newch].remote_id = remote_channel;
2624
2625 /* Send a confirmation to the remote host. */
2626 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2627 packet_put_int(remote_channel);
2628 packet_put_int(newch);
2629 packet_send();
2630 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002631}
2632
Damien Miller69b69aa2000-10-28 14:19:58 +11002633/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2634void
2635deny_input_open(int type, int plen, void *ctxt)
2636{
2637 int rchan = packet_get_int();
2638 switch(type){
2639 case SSH_SMSG_AGENT_OPEN:
2640 error("Warning: ssh server tried agent forwarding.");
2641 break;
2642 case SSH_SMSG_X11_OPEN:
2643 error("Warning: ssh server tried X11 forwarding.");
2644 break;
2645 default:
2646 error("deny_input_open: type %d plen %d", type, plen);
2647 break;
2648 }
2649 error("Warning: this is probably a break in attempt by a malicious server.");
2650 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2651 packet_put_int(rchan);
2652 packet_send();
2653}
2654
Damien Miller5428f641999-11-25 11:54:57 +11002655/*
2656 * Requests forwarding of X11 connections, generates fake authentication
2657 * data, and enables authentication spoofing.
2658 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002659
Damien Miller4af51302000-04-16 11:18:38 +10002660void
Damien Millerbd483e72000-04-30 10:00:53 +10002661x11_request_forwarding_with_spoofing(int client_session_id,
2662 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002663{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002664 u_int data_len = (u_int) strlen(data) / 2;
Ben Lindstromb3211a82001-02-10 22:33:19 +00002665 u_int i, value, len;
Damien Miller95def091999-11-25 00:26:21 +11002666 char *new_data;
2667 int screen_number;
2668 const char *cp;
2669 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002670
Damien Miller95def091999-11-25 00:26:21 +11002671 cp = getenv("DISPLAY");
2672 if (cp)
2673 cp = strchr(cp, ':');
2674 if (cp)
2675 cp = strchr(cp, '.');
2676 if (cp)
2677 screen_number = atoi(cp + 1);
2678 else
2679 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002680
Damien Miller95def091999-11-25 00:26:21 +11002681 /* Save protocol name. */
2682 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002683
Damien Miller5428f641999-11-25 11:54:57 +11002684 /*
2685 * Extract real authentication data and generate fake data of the
2686 * same length.
2687 */
Damien Miller95def091999-11-25 00:26:21 +11002688 x11_saved_data = xmalloc(data_len);
2689 x11_fake_data = xmalloc(data_len);
2690 for (i = 0; i < data_len; i++) {
2691 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2692 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2693 if (i % 4 == 0)
2694 rand = arc4random();
2695 x11_saved_data[i] = value;
2696 x11_fake_data[i] = rand & 0xff;
2697 rand >>= 8;
2698 }
2699 x11_saved_data_len = data_len;
2700 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002701
Damien Miller95def091999-11-25 00:26:21 +11002702 /* Convert the fake data into hex. */
Ben Lindstromb3211a82001-02-10 22:33:19 +00002703 len = 2 * data_len + 1;
2704 new_data = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +11002705 for (i = 0; i < data_len; i++)
Ben Lindstromb3211a82001-02-10 22:33:19 +00002706 snprintf(new_data + 2 * i, len - 2 * i,
2707 "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002708
Damien Miller95def091999-11-25 00:26:21 +11002709 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002710 if (compat20) {
2711 channel_request_start(client_session_id, "x11-req", 0);
2712 packet_put_char(0); /* XXX bool single connection */
2713 } else {
2714 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2715 }
2716 packet_put_cstring(proto);
2717 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002718 packet_put_int(screen_number);
2719 packet_send();
2720 packet_write_wait();
2721 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002722}
2723
2724/* Sends a message to the server to request authentication fd forwarding. */
2725
Damien Miller4af51302000-04-16 11:18:38 +10002726void
Damien Miller95def091999-11-25 00:26:21 +11002727auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002728{
Damien Miller95def091999-11-25 00:26:21 +11002729 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2730 packet_send();
2731 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002732}
2733
Damien Miller5428f641999-11-25 11:54:57 +11002734/*
2735 * Returns the name of the forwarded authentication socket. Returns NULL if
2736 * there is no forwarded authentication socket. The returned value points to
2737 * a static buffer.
2738 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002739
Damien Miller95def091999-11-25 00:26:21 +11002740char *
2741auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002742{
Damien Miller95def091999-11-25 00:26:21 +11002743 return channel_forwarded_auth_socket_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002744}
2745
2746/* removes the agent forwarding socket */
2747
Damien Miller4af51302000-04-16 11:18:38 +10002748void
Damien Miller95def091999-11-25 00:26:21 +11002749cleanup_socket(void)
2750{
Damien Miller78315eb2000-09-29 23:01:36 +11002751 unlink(channel_forwarded_auth_socket_name);
Damien Miller95def091999-11-25 00:26:21 +11002752 rmdir(channel_forwarded_auth_socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002753}
2754
Damien Miller5428f641999-11-25 11:54:57 +11002755/*
Damien Millerd3a18572000-06-07 19:55:44 +10002756 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002757 * This starts forwarding authentication requests.
2758 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002759
Damien Millerd3a18572000-06-07 19:55:44 +10002760int
Damien Miller95def091999-11-25 00:26:21 +11002761auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002762{
Damien Miller95def091999-11-25 00:26:21 +11002763 int sock, newch;
2764 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002765
Damien Miller95def091999-11-25 00:26:21 +11002766 if (auth_get_socket_name() != NULL)
2767 fatal("Protocol error: authentication forwarding requested twice.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002768
Damien Miller95def091999-11-25 00:26:21 +11002769 /* Temporarily drop privileged uid for mkdir/bind. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00002770 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002771
Damien Miller95def091999-11-25 00:26:21 +11002772 /* Allocate a buffer for the socket name, and format the name. */
2773 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
2774 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
2775 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002776
Damien Miller95def091999-11-25 00:26:21 +11002777 /* Create private directory for socket */
Damien Millerd3a18572000-06-07 19:55:44 +10002778 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL) {
2779 packet_send_debug("Agent forwarding disabled: mkdtemp() failed: %.100s",
2780 strerror(errno));
2781 restore_uid();
2782 xfree(channel_forwarded_auth_socket_name);
2783 xfree(channel_forwarded_auth_socket_dir);
2784 channel_forwarded_auth_socket_name = NULL;
2785 channel_forwarded_auth_socket_dir = NULL;
2786 return 0;
2787 }
Damien Miller95def091999-11-25 00:26:21 +11002788 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
2789 channel_forwarded_auth_socket_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002790
Damien Miller95def091999-11-25 00:26:21 +11002791 if (atexit(cleanup_socket) < 0) {
2792 int saved = errno;
2793 cleanup_socket();
2794 packet_disconnect("socket: %.100s", strerror(saved));
2795 }
2796 /* Create the socket. */
2797 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2798 if (sock < 0)
2799 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002800
Damien Miller95def091999-11-25 00:26:21 +11002801 /* Bind it to the name. */
2802 memset(&sunaddr, 0, sizeof(sunaddr));
2803 sunaddr.sun_family = AF_UNIX;
2804 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
2805 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002806
Damien Miller95def091999-11-25 00:26:21 +11002807 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2808 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002809
Damien Miller95def091999-11-25 00:26:21 +11002810 /* Restore the privileged uid. */
2811 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002812
Damien Miller95def091999-11-25 00:26:21 +11002813 /* Start listening on the socket. */
2814 if (listen(sock, 5) < 0)
2815 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002816
Damien Miller95def091999-11-25 00:26:21 +11002817 /* Allocate a channel for the authentication agent socket. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002818 newch = channel_new("auth socket",
2819 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2820 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2821 0, xstrdup("auth socket"), 1);
2822
Damien Miller037a0dc1999-12-07 15:38:31 +11002823 strlcpy(channels[newch].path, channel_forwarded_auth_socket_name,
2824 sizeof(channels[newch].path));
Damien Millerd3a18572000-06-07 19:55:44 +10002825 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002826}
2827
2828/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2829
Damien Miller4af51302000-04-16 11:18:38 +10002830void
Damien Miller62cee002000-09-23 17:15:56 +11002831auth_input_open_request(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002832{
Damien Miller95def091999-11-25 00:26:21 +11002833 int remch, sock, newch;
2834 char *dummyname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002835
Damien Millerb38eff82000-04-01 11:09:21 +10002836 packet_integrity_check(plen, 4, type);
2837
Damien Miller95def091999-11-25 00:26:21 +11002838 /* Read the remote channel number from the message. */
2839 remch = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002840
Damien Miller5428f641999-11-25 11:54:57 +11002841 /*
2842 * Get a connection to the local authentication agent (this may again
2843 * get forwarded).
2844 */
Damien Miller95def091999-11-25 00:26:21 +11002845 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002846
Damien Miller5428f641999-11-25 11:54:57 +11002847 /*
2848 * If we could not connect the agent, send an error message back to
2849 * the server. This should never happen unless the agent dies,
2850 * because authentication forwarding is only enabled if we have an
2851 * agent.
2852 */
Damien Miller95def091999-11-25 00:26:21 +11002853 if (sock < 0) {
2854 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2855 packet_put_int(remch);
2856 packet_send();
2857 return;
2858 }
2859 debug("Forwarding authentication connection.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002860
Damien Miller5428f641999-11-25 11:54:57 +11002861 /*
2862 * Dummy host name. This will be freed when the channel is freed; it
2863 * will still be valid in the packet_put_string below since the
2864 * channel cannot yet be freed at that point.
2865 */
Damien Miller95def091999-11-25 00:26:21 +11002866 dummyname = xstrdup("authentication agent connection");
2867
2868 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);
2869 channels[newch].remote_id = remch;
2870
2871 /* Send a confirmation to the remote host. */
2872 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2873 packet_put_int(remch);
2874 packet_put_int(newch);
2875 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002876}
Damien Miller33b13562000-04-04 14:38:59 +10002877
2878void
Damien Millerbd483e72000-04-30 10:00:53 +10002879channel_start_open(int id)
Damien Miller33b13562000-04-04 14:38:59 +10002880{
2881 Channel *c = channel_lookup(id);
2882 if (c == NULL) {
2883 log("channel_open: %d: bad id", id);
2884 return;
2885 }
Damien Millerbd483e72000-04-30 10:00:53 +10002886 debug("send channel open %d", id);
Damien Miller33b13562000-04-04 14:38:59 +10002887 packet_start(SSH2_MSG_CHANNEL_OPEN);
2888 packet_put_cstring(c->ctype);
2889 packet_put_int(c->self);
2890 packet_put_int(c->local_window);
2891 packet_put_int(c->local_maxpacket);
Damien Millerbd483e72000-04-30 10:00:53 +10002892}
2893void
2894channel_open(int id)
2895{
2896 /* XXX REMOVE ME */
2897 channel_start_open(id);
Damien Miller33b13562000-04-04 14:38:59 +10002898 packet_send();
Damien Miller33b13562000-04-04 14:38:59 +10002899}
2900void
2901channel_request(int id, char *service, int wantconfirm)
2902{
2903 channel_request_start(id, service, wantconfirm);
2904 packet_send();
2905 debug("channel request %d: %s", id, service) ;
2906}
2907void
2908channel_request_start(int id, char *service, int wantconfirm)
2909{
2910 Channel *c = channel_lookup(id);
2911 if (c == NULL) {
2912 log("channel_request: %d: bad id", id);
2913 return;
2914 }
2915 packet_start(SSH2_MSG_CHANNEL_REQUEST);
2916 packet_put_int(c->remote_id);
2917 packet_put_cstring(service);
2918 packet_put_char(wantconfirm);
2919}
2920void
2921channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
2922{
2923 Channel *c = channel_lookup(id);
2924 if (c == NULL) {
2925 log("channel_register_callback: %d: bad id", id);
2926 return;
2927 }
2928 c->cb_event = mtype;
2929 c->cb_fn = fn;
2930 c->cb_arg = arg;
2931}
2932void
2933channel_register_cleanup(int id, channel_callback_fn *fn)
2934{
2935 Channel *c = channel_lookup(id);
2936 if (c == NULL) {
2937 log("channel_register_cleanup: %d: bad id", id);
2938 return;
2939 }
2940 c->dettach_user = fn;
2941}
2942void
2943channel_cancel_cleanup(int id)
2944{
2945 Channel *c = channel_lookup(id);
2946 if (c == NULL) {
2947 log("channel_cancel_cleanup: %d: bad id", id);
2948 return;
2949 }
2950 c->dettach_user = NULL;
2951}
Kevin Stevesef4eea92001-02-05 12:42:17 +00002952void
Damien Millerad833b32000-08-23 10:46:23 +10002953channel_register_filter(int id, channel_filter_fn *fn)
2954{
2955 Channel *c = channel_lookup(id);
2956 if (c == NULL) {
2957 log("channel_register_filter: %d: bad id", id);
2958 return;
2959 }
2960 c->input_filter = fn;
2961}
Damien Miller33b13562000-04-04 14:38:59 +10002962
2963void
Damien Miller69b69aa2000-10-28 14:19:58 +11002964channel_set_fds(int id, int rfd, int wfd, int efd,
2965 int extusage, int nonblock)
Damien Miller33b13562000-04-04 14:38:59 +10002966{
2967 Channel *c = channel_lookup(id);
2968 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
2969 fatal("channel_activate for non-larval channel %d.", id);
Damien Miller69b69aa2000-10-28 14:19:58 +11002970 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller33b13562000-04-04 14:38:59 +10002971 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002972 /* XXX window size? */
Damien Millere4340be2000-09-16 13:29:08 +11002973 c->local_window = c->local_window_max = c->local_maxpacket * 2;
Damien Miller33b13562000-04-04 14:38:59 +10002974 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2975 packet_put_int(c->remote_id);
2976 packet_put_int(c->local_window);
2977 packet_send();
2978}