blob: b7286940b473fa71120f46bac71e798021418234 [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"
Damien Miller79438cc2001-02-16 12:34:57 +110043RCSID("$OpenBSD: channels.c,v 1.91 2001/02/15 23:19:59 markus Exp $");
Ben Lindstrom226cfa02001-01-22 05:34:40 +000044
45#include <openssl/rsa.h>
46#include <openssl/dsa.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047
48#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000049#include "ssh1.h"
50#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051#include "packet.h"
52#include "xmalloc.h"
53#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "log.h"
56#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057#include "channels.h"
58#include "nchan.h"
59#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000060#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100061#include "key.h"
62#include "authfd.h"
63
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064/* Maximum number of fake X11 displays to try. */
65#define MAX_DISPLAYS 1000
66
67/* Max len of agent socket */
68#define MAX_SOCKET_NAME 100
69
Damien Miller5428f641999-11-25 11:54:57 +110070/*
71 * Pointer to an array containing all allocated channels. The array is
72 * dynamically extended as needed.
73 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074static Channel *channels = NULL;
75
Damien Miller5428f641999-11-25 11:54:57 +110076/*
77 * Size of the channel array. All slots of the array must always be
78 * initialized (at least the type field); unused slots are marked with type
79 * SSH_CHANNEL_FREE.
80 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081static int channels_alloc = 0;
82
Damien Miller5428f641999-11-25 11:54:57 +110083/*
84 * Maximum file descriptor value used in any of the channels. This is
85 * updated in channel_allocate.
86 */
Damien Miller5e953212001-01-30 09:14:00 +110087static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
89/* Name and directory of socket for authentication agent forwarding. */
90static char *channel_forwarded_auth_socket_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +110091static char *channel_forwarded_auth_socket_dir = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
93/* Saved X11 authentication protocol name. */
94char *x11_saved_proto = NULL;
95
96/* Saved X11 authentication data. This is the real data. */
97char *x11_saved_data = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +000098u_int x11_saved_data_len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * Fake X11 authentication data. This is what the server will be sending us;
102 * we should replace any occurrences of this by the real data.
103 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104char *x11_fake_data = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000105u_int x11_fake_data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Miller5428f641999-11-25 11:54:57 +1100107/*
108 * Data structure for storing which hosts are permitted for forward requests.
109 * The local sides of any remote forwards are stored in this array to prevent
110 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
111 * network (which might be behind a firewall).
112 */
Damien Miller95def091999-11-25 00:26:21 +1100113typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000114 char *host_to_connect; /* Connect to 'host'. */
115 u_short port_to_connect; /* Connect to 'port'. */
116 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117} ForwardPermission;
118
119/* List of all permitted host/port pairs to connect. */
120static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
121/* Number of permitted host/port pairs in the array. */
122static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +1100123/*
124 * If this is true, all opens are permitted. This is the case on the server
125 * on which we have to trust the client anyway, and the user could do
126 * anything after logging in anyway.
127 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128static int all_opens_permitted = 0;
129
130/* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */
131static int have_hostname_in_open = 0;
132
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000133/* AF_UNSPEC or AF_INET or AF_INET6 */
134extern int IPv4or6;
135
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136/* Sets specific protocol options. */
137
Damien Miller4af51302000-04-16 11:18:38 +1000138void
Damien Miller95def091999-11-25 00:26:21 +1100139channel_set_options(int hostname_in_open)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140{
Damien Miller95def091999-11-25 00:26:21 +1100141 have_hostname_in_open = hostname_in_open;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142}
143
Damien Miller5428f641999-11-25 11:54:57 +1100144/*
145 * Permits opening to any host/port in SSH_MSG_PORT_OPEN. This is usually
146 * called by the server, because the user could connect to any port anyway,
147 * and the server has no way to know but to trust the client anyway.
148 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149
Damien Miller4af51302000-04-16 11:18:38 +1000150void
Damien Miller95def091999-11-25 00:26:21 +1100151channel_permit_all_opens()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152{
Damien Miller95def091999-11-25 00:26:21 +1100153 all_opens_permitted = 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154}
155
Damien Millerb38eff82000-04-01 11:09:21 +1000156/* lookup channel by id */
157
158Channel *
159channel_lookup(int id)
160{
161 Channel *c;
Damien Millerc0fd17f2000-06-26 10:22:53 +1000162 if (id < 0 || id > channels_alloc) {
Damien Millerb38eff82000-04-01 11:09:21 +1000163 log("channel_lookup: %d: bad id", id);
164 return NULL;
165 }
166 c = &channels[id];
167 if (c->type == SSH_CHANNEL_FREE) {
168 log("channel_lookup: %d: bad id: channel free", id);
169 return NULL;
170 }
171 return c;
172}
173
Damien Miller5428f641999-11-25 11:54:57 +1100174/*
Damien Millere247cc42000-05-07 12:03:14 +1000175 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000176 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100177 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178
Damien Miller6f83b8e2000-05-02 09:23:45 +1000179void
Damien Miller69b69aa2000-10-28 14:19:58 +1100180channel_register_fds(Channel *c, int rfd, int wfd, int efd,
181 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182{
Damien Miller95def091999-11-25 00:26:21 +1100183 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100184 channel_max_fd = MAX(channel_max_fd, rfd);
185 channel_max_fd = MAX(channel_max_fd, wfd);
186 channel_max_fd = MAX(channel_max_fd, efd);
187
Damien Miller5428f641999-11-25 11:54:57 +1100188 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000189
Damien Miller6f83b8e2000-05-02 09:23:45 +1000190 c->rfd = rfd;
191 c->wfd = wfd;
192 c->sock = (rfd == wfd) ? rfd : -1;
193 c->efd = efd;
194 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100195
Damien Miller79438cc2001-02-16 12:34:57 +1100196 /* XXX ugly hack: nonblock is only set by the server */
197 if (nonblock && isatty(c->rfd)) {
198 debug("channel: %d: rfd %d isatty", c->self, c->rfd);
199 c->isatty = 1;
200 if (!isatty(c->wfd)) {
201 error("channel: %d: wfd %d is not a tty?",
202 c->self, c->wfd);
203 }
204 } else {
205 c->isatty = 0;
206 }
207
Damien Miller69b69aa2000-10-28 14:19:58 +1100208 /* enable nonblocking mode */
209 if (nonblock) {
210 if (rfd != -1)
211 set_nonblock(rfd);
212 if (wfd != -1)
213 set_nonblock(wfd);
214 if (efd != -1)
215 set_nonblock(efd);
216 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000217}
218
219/*
220 * Allocate a new channel object and set its type and socket. This will cause
221 * remote_name to be freed.
222 */
223
224int
225channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Damien Miller69b69aa2000-10-28 14:19:58 +1100226 int window, int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000227{
228 int i, found;
229 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230
Damien Miller95def091999-11-25 00:26:21 +1100231 /* Do initial allocation if this is the first call. */
232 if (channels_alloc == 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000233 chan_init();
Damien Miller95def091999-11-25 00:26:21 +1100234 channels_alloc = 10;
235 channels = xmalloc(channels_alloc * sizeof(Channel));
236 for (i = 0; i < channels_alloc; i++)
237 channels[i].type = SSH_CHANNEL_FREE;
Damien Miller5428f641999-11-25 11:54:57 +1100238 /*
239 * Kludge: arrange a call to channel_stop_listening if we
240 * terminate with fatal().
241 */
Damien Miller95def091999-11-25 00:26:21 +1100242 fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL);
243 }
244 /* Try to find a free slot where to put the new channel. */
245 for (found = -1, i = 0; i < channels_alloc; i++)
246 if (channels[i].type == SSH_CHANNEL_FREE) {
247 /* Found a free slot. */
248 found = i;
249 break;
250 }
251 if (found == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100252 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100253 found = channels_alloc;
254 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100255 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100256 channels = xrealloc(channels, channels_alloc * sizeof(Channel));
257 for (i = found; i < channels_alloc; i++)
258 channels[i].type = SSH_CHANNEL_FREE;
259 }
260 /* Initialize and return new channel number. */
261 c = &channels[found];
262 buffer_init(&c->input);
263 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000264 buffer_init(&c->extended);
Damien Miller95def091999-11-25 00:26:21 +1100265 chan_init_iostates(c);
Damien Miller69b69aa2000-10-28 14:19:58 +1100266 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100267 c->self = found;
268 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000269 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000270 c->local_window = window;
271 c->local_window_max = window;
272 c->local_consumed = 0;
273 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100274 c->remote_id = -1;
275 c->remote_name = remote_name;
Damien Millerb38eff82000-04-01 11:09:21 +1000276 c->remote_window = 0;
277 c->remote_maxpacket = 0;
278 c->cb_fn = NULL;
279 c->cb_arg = NULL;
280 c->cb_event = 0;
281 c->dettach_user = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000282 c->input_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100283 debug("channel %d: new [%s]", found, remote_name);
284 return found;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000286/* old interface XXX */
Damien Miller4af51302000-04-16 11:18:38 +1000287int
Damien Millerb38eff82000-04-01 11:09:21 +1000288channel_allocate(int type, int sock, char *remote_name)
289{
Damien Miller69b69aa2000-10-28 14:19:58 +1100290 return channel_new("", type, sock, sock, -1, 0, 0, 0, remote_name, 1);
Damien Millerb38eff82000-04-01 11:09:21 +1000291}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000292
Damien Miller6f83b8e2000-05-02 09:23:45 +1000293
294/* Close all channel fd/socket. */
295
296void
297channel_close_fds(Channel *c)
298{
299 if (c->sock != -1) {
300 close(c->sock);
301 c->sock = -1;
302 }
303 if (c->rfd != -1) {
304 close(c->rfd);
305 c->rfd = -1;
306 }
307 if (c->wfd != -1) {
308 close(c->wfd);
309 c->wfd = -1;
310 }
311 if (c->efd != -1) {
312 close(c->efd);
313 c->efd = -1;
314 }
315}
316
317/* Free the channel and close its fd/socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318
Damien Miller4af51302000-04-16 11:18:38 +1000319void
Damien Millerb38eff82000-04-01 11:09:21 +1000320channel_free(int id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000321{
Damien Millerb38eff82000-04-01 11:09:21 +1000322 Channel *c = channel_lookup(id);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000323 char *s = channel_open_message();
324
Damien Millerb38eff82000-04-01 11:09:21 +1000325 if (c == NULL)
326 packet_disconnect("channel free: bad local channel %d", id);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000327 debug("channel_free: channel %d: status: %s", id, s);
328 xfree(s);
329
Damien Miller33b13562000-04-04 14:38:59 +1000330 if (c->dettach_user != NULL) {
331 debug("channel_free: channel %d: dettaching channel user", id);
332 c->dettach_user(c->self, NULL);
333 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000334 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000335 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000336 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000337 buffer_free(&c->input);
338 buffer_free(&c->output);
339 buffer_free(&c->extended);
340 c->type = SSH_CHANNEL_FREE;
341 if (c->remote_name) {
342 xfree(c->remote_name);
343 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100344 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345}
346
Damien Miller5428f641999-11-25 11:54:57 +1100347/*
Damien Millerb38eff82000-04-01 11:09:21 +1000348 * 'channel_pre*' are called just before select() to add any bits relevant to
349 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100350 */
Damien Millerb38eff82000-04-01 11:09:21 +1000351/*
352 * 'channel_post*': perform any appropriate operations for channels which
353 * have events pending.
354 */
355typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
356chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
357chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
358
359void
360channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
361{
362 FD_SET(c->sock, readset);
363}
364
365void
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000366channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
367{
368 debug3("channel %d: waiting for connection", c->self);
369 FD_SET(c->sock, writeset);
370}
371
372void
Damien Millerb38eff82000-04-01 11:09:21 +1000373channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
374{
375 if (buffer_len(&c->input) < packet_get_maxsize())
376 FD_SET(c->sock, readset);
377 if (buffer_len(&c->output) > 0)
378 FD_SET(c->sock, writeset);
379}
380
381void
382channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
383{
384 /* test whether sockets are 'alive' for read/write */
385 if (c->istate == CHAN_INPUT_OPEN)
386 if (buffer_len(&c->input) < packet_get_maxsize())
387 FD_SET(c->sock, readset);
388 if (c->ostate == CHAN_OUTPUT_OPEN ||
389 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
390 if (buffer_len(&c->output) > 0) {
391 FD_SET(c->sock, writeset);
392 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
393 chan_obuf_empty(c);
394 }
395 }
396}
397
398void
Damien Miller33b13562000-04-04 14:38:59 +1000399channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
400{
401 if (c->istate == CHAN_INPUT_OPEN &&
402 c->remote_window > 0 &&
403 buffer_len(&c->input) < c->remote_window)
404 FD_SET(c->rfd, readset);
405 if (c->ostate == CHAN_OUTPUT_OPEN ||
406 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
407 if (buffer_len(&c->output) > 0) {
408 FD_SET(c->wfd, writeset);
409 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
410 chan_obuf_empty(c);
411 }
412 }
413 /** XXX check close conditions, too */
414 if (c->efd != -1) {
415 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
416 buffer_len(&c->extended) > 0)
417 FD_SET(c->efd, writeset);
418 else if (c->extended_usage == CHAN_EXTENDED_READ &&
419 buffer_len(&c->extended) < c->remote_window)
420 FD_SET(c->efd, readset);
421 }
422}
423
424void
Damien Millerb38eff82000-04-01 11:09:21 +1000425channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
426{
427 if (buffer_len(&c->input) == 0) {
428 packet_start(SSH_MSG_CHANNEL_CLOSE);
429 packet_put_int(c->remote_id);
430 packet_send();
431 c->type = SSH_CHANNEL_CLOSED;
432 debug("Closing channel %d after input drain.", c->self);
433 }
434}
435
436void
437channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
438{
439 if (buffer_len(&c->output) == 0)
440 channel_free(c->self);
Damien Miller4af51302000-04-16 11:18:38 +1000441 else
Damien Millerb38eff82000-04-01 11:09:21 +1000442 FD_SET(c->sock, writeset);
443}
444
445/*
446 * This is a special state for X11 authentication spoofing. An opened X11
447 * connection (when authentication spoofing is being done) remains in this
448 * state until the first packet has been completely read. The authentication
449 * data in that packet is then substituted by the real data if it matches the
450 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000451 * XXX All this happens at the client side.
Damien Millerb38eff82000-04-01 11:09:21 +1000452 */
453int
454x11_open_helper(Channel *c)
455{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000456 u_char *ucp;
457 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000458
459 /* Check if the fixed size part of the packet is in buffer. */
460 if (buffer_len(&c->output) < 12)
461 return 0;
462
463 /* Parse the lengths of variable-length fields. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000464 ucp = (u_char *) buffer_ptr(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000465 if (ucp[0] == 0x42) { /* Byte order MSB first. */
466 proto_len = 256 * ucp[6] + ucp[7];
467 data_len = 256 * ucp[8] + ucp[9];
468 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
469 proto_len = ucp[6] + 256 * ucp[7];
470 data_len = ucp[8] + 256 * ucp[9];
471 } else {
472 debug("Initial X11 packet contains bad byte order byte: 0x%x",
473 ucp[0]);
474 return -1;
475 }
476
477 /* Check if the whole packet is in buffer. */
478 if (buffer_len(&c->output) <
479 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
480 return 0;
481
482 /* Check if authentication protocol matches. */
483 if (proto_len != strlen(x11_saved_proto) ||
484 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
485 debug("X11 connection uses different authentication protocol.");
486 return -1;
487 }
488 /* Check if authentication data matches our fake data. */
489 if (data_len != x11_fake_data_len ||
490 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
491 x11_fake_data, x11_fake_data_len) != 0) {
492 debug("X11 auth data does not match fake data.");
493 return -1;
494 }
495 /* Check fake data length */
496 if (x11_fake_data_len != x11_saved_data_len) {
497 error("X11 fake_data_len %d != saved_data_len %d",
498 x11_fake_data_len, x11_saved_data_len);
499 return -1;
500 }
501 /*
502 * Received authentication protocol and data match
503 * our fake data. Substitute the fake data with real
504 * data.
505 */
506 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
507 x11_saved_data, x11_saved_data_len);
508 return 1;
509}
510
511void
512channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
513{
514 int ret = x11_open_helper(c);
515 if (ret == 1) {
516 /* Start normal processing for the channel. */
517 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000518 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000519 } else if (ret == -1) {
520 /*
521 * We have received an X11 connection that has bad
522 * authentication information.
523 */
524 log("X11 connection rejected because of wrong authentication.\r\n");
525 buffer_clear(&c->input);
526 buffer_clear(&c->output);
527 close(c->sock);
528 c->sock = -1;
529 c->type = SSH_CHANNEL_CLOSED;
530 packet_start(SSH_MSG_CHANNEL_CLOSE);
531 packet_put_int(c->remote_id);
532 packet_send();
533 }
534}
535
536void
Damien Millerbd483e72000-04-30 10:00:53 +1000537channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000538{
539 int ret = x11_open_helper(c);
540 if (ret == 1) {
541 c->type = SSH_CHANNEL_OPEN;
Damien Miller30c3d422000-05-09 11:02:59 +1000542 if (compat20)
543 channel_pre_open_20(c, readset, writeset);
544 else
545 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000546 } else if (ret == -1) {
547 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerbd483e72000-04-30 10:00:53 +1000548 chan_read_failed(c); /** force close? */
Damien Millerb38eff82000-04-01 11:09:21 +1000549 chan_write_failed(c);
550 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
551 }
552}
553
554/* This is our fake X11 server socket. */
555void
556channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
557{
558 struct sockaddr addr;
559 int newsock, newch;
560 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +1100561 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +1000562 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +1000563
564 if (FD_ISSET(c->sock, readset)) {
565 debug("X11 connection requested.");
566 addrlen = sizeof(addr);
567 newsock = accept(c->sock, &addr, &addrlen);
568 if (newsock < 0) {
569 error("accept: %.100s", strerror(errno));
570 return;
571 }
Damien Millerd83ff352001-01-30 09:19:34 +1100572 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +1000573 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +1000574 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +1100575 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +1000576
577 newch = channel_new("x11",
578 SSH_CHANNEL_OPENING, newsock, newsock, -1,
579 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100580 0, xstrdup(buf), 1);
Damien Millerbd483e72000-04-30 10:00:53 +1000581 if (compat20) {
582 packet_start(SSH2_MSG_CHANNEL_OPEN);
583 packet_put_cstring("x11");
584 packet_put_int(newch);
585 packet_put_int(c->local_window_max);
586 packet_put_int(c->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +1100587 /* originator ipaddr and port */
588 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +1000589 if (datafellows & SSH_BUG_X11FWD) {
590 debug("ssh2 x11 bug compat mode");
591 } else {
592 packet_put_int(remote_port);
593 }
Damien Millerbd483e72000-04-30 10:00:53 +1000594 packet_send();
595 } else {
596 packet_start(SSH_SMSG_X11_OPEN);
597 packet_put_int(newch);
598 if (have_hostname_in_open)
599 packet_put_string(buf, strlen(buf));
600 packet_send();
601 }
Damien Millerd83ff352001-01-30 09:19:34 +1100602 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +1000603 }
604}
605
606/*
607 * This socket is listening for connections to a forwarded TCP/IP port.
608 */
609void
610channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
611{
612 struct sockaddr addr;
613 int newsock, newch;
614 socklen_t addrlen;
Damien Miller33804262001-02-04 23:20:18 +1100615 char buf[1024], *remote_ipaddr, *rtype;
Damien Millerb38eff82000-04-01 11:09:21 +1000616 int remote_port;
617
Damien Miller0bc1bd82000-11-13 22:57:25 +1100618 rtype = (c->type == SSH_CHANNEL_RPORT_LISTENER) ?
619 "forwarded-tcpip" : "direct-tcpip";
620
Damien Millerb38eff82000-04-01 11:09:21 +1000621 if (FD_ISSET(c->sock, readset)) {
622 debug("Connection to port %d forwarding "
623 "to %.100s port %d requested.",
624 c->listening_port, c->path, c->host_port);
625 addrlen = sizeof(addr);
626 newsock = accept(c->sock, &addr, &addrlen);
627 if (newsock < 0) {
628 error("accept: %.100s", strerror(errno));
629 return;
630 }
Damien Miller33804262001-02-04 23:20:18 +1100631 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +1000632 remote_port = get_peer_port(newsock);
633 snprintf(buf, sizeof buf,
634 "listen port %d for %.100s port %d, "
635 "connect from %.200s port %d",
636 c->listening_port, c->path, c->host_port,
Damien Miller33804262001-02-04 23:20:18 +1100637 remote_ipaddr, remote_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100638
639 newch = channel_new(rtype,
Damien Millerb38eff82000-04-01 11:09:21 +1000640 SSH_CHANNEL_OPENING, newsock, newsock, -1,
641 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100642 0, xstrdup(buf), 1);
Damien Miller33b13562000-04-04 14:38:59 +1000643 if (compat20) {
644 packet_start(SSH2_MSG_CHANNEL_OPEN);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100645 packet_put_cstring(rtype);
Damien Miller33b13562000-04-04 14:38:59 +1000646 packet_put_int(newch);
647 packet_put_int(c->local_window_max);
648 packet_put_int(c->local_maxpacket);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100649 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
650 /* listen address, port */
651 packet_put_string(c->path, strlen(c->path));
652 packet_put_int(c->listening_port);
653 } else {
654 /* target host, port */
655 packet_put_string(c->path, strlen(c->path));
656 packet_put_int(c->host_port);
657 }
Damien Miller4af51302000-04-16 11:18:38 +1000658 /* originator host and port */
Damien Miller33804262001-02-04 23:20:18 +1100659 packet_put_cstring(remote_ipaddr);
Damien Miller33b13562000-04-04 14:38:59 +1000660 packet_put_int(remote_port);
661 packet_send();
662 } else {
663 packet_start(SSH_MSG_PORT_OPEN);
664 packet_put_int(newch);
665 packet_put_string(c->path, strlen(c->path));
666 packet_put_int(c->host_port);
667 if (have_hostname_in_open) {
668 packet_put_string(buf, strlen(buf));
669 }
670 packet_send();
Damien Millerb38eff82000-04-01 11:09:21 +1000671 }
Damien Miller33804262001-02-04 23:20:18 +1100672 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +1000673 }
674}
675
676/*
677 * This is the authentication agent socket listening for connections from
678 * clients.
679 */
680void
681channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
682{
683 struct sockaddr addr;
684 int newsock, newch;
685 socklen_t addrlen;
686
687 if (FD_ISSET(c->sock, readset)) {
688 addrlen = sizeof(addr);
689 newsock = accept(c->sock, &addr, &addrlen);
690 if (newsock < 0) {
691 error("accept from auth socket: %.100s", strerror(errno));
692 return;
693 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100694 newch = channel_new("accepted auth socket",
695 SSH_CHANNEL_OPENING, newsock, newsock, -1,
696 c->local_window_max, c->local_maxpacket,
697 0, xstrdup("accepted auth socket"), 1);
698 if (compat20) {
699 packet_start(SSH2_MSG_CHANNEL_OPEN);
700 packet_put_cstring("auth-agent@openssh.com");
701 packet_put_int(newch);
702 packet_put_int(c->local_window_max);
703 packet_put_int(c->local_maxpacket);
704 } else {
705 packet_start(SSH_SMSG_AGENT_OPEN);
706 packet_put_int(newch);
707 }
Damien Millerb38eff82000-04-01 11:09:21 +1000708 packet_send();
709 }
710}
711
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000712void
713channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
714{
715 if (FD_ISSET(c->sock, writeset)) {
716 int err = 0;
717 int sz = sizeof(err);
718 c->type = SSH_CHANNEL_OPEN;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000719 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err, &sz) < 0) {
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000720 debug("getsockopt SO_ERROR failed");
721 } else {
722 if (err == 0) {
723 debug("channel %d: connected)", c->self);
724 } else {
725 debug("channel %d: not connected: %s",
726 c->self, strerror(err));
727 chan_read_failed(c);
728 chan_write_failed(c);
729 }
730 }
731 }
732}
733
Damien Millerb38eff82000-04-01 11:09:21 +1000734int
735channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
736{
737 char buf[16*1024];
738 int len;
739
740 if (c->rfd != -1 &&
741 FD_ISSET(c->rfd, readset)) {
742 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +1000743 if (len < 0 && (errno == EINTR || errno == EAGAIN))
744 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000745 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +1000746 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +1000747 c->self, c->rfd, len);
748 if (compat13) {
749 buffer_consume(&c->output, buffer_len(&c->output));
750 c->type = SSH_CHANNEL_INPUT_DRAINING;
751 debug("Channel %d status set to input draining.", c->self);
752 } else {
753 chan_read_failed(c);
754 }
755 return -1;
756 }
Damien Millerad833b32000-08-23 10:46:23 +1000757 if(c->input_filter != NULL) {
758 if (c->input_filter(c, buf, len) == -1) {
759 debug("filter stops channel %d", c->self);
760 chan_read_failed(c);
761 }
762 } else {
763 buffer_append(&c->input, buf, len);
764 }
Damien Millerb38eff82000-04-01 11:09:21 +1000765 }
766 return 1;
767}
768int
769channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
770{
771 int len;
772
773 /* Send buffered output data to the socket. */
774 if (c->wfd != -1 &&
775 FD_ISSET(c->wfd, writeset) &&
776 buffer_len(&c->output) > 0) {
777 len = write(c->wfd, buffer_ptr(&c->output),
Damien Miller6f83b8e2000-05-02 09:23:45 +1000778 buffer_len(&c->output));
779 if (len < 0 && (errno == EINTR || errno == EAGAIN))
780 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000781 if (len <= 0) {
782 if (compat13) {
783 buffer_consume(&c->output, buffer_len(&c->output));
784 debug("Channel %d status set to input draining.", c->self);
785 c->type = SSH_CHANNEL_INPUT_DRAINING;
786 } else {
787 chan_write_failed(c);
788 }
789 return -1;
790 }
Damien Miller79438cc2001-02-16 12:34:57 +1100791 if (compat20 && c->isatty) {
792 struct termios tio;
793 if (tcgetattr(c->wfd, &tio) == 0 &&
794 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
795 /*
796 * Simulate echo to reduce the impact of
797 * traffic analysis.
798 */
799 packet_start(SSH2_MSG_IGNORE);
800 memset(buffer_ptr(&c->output), 0, len);
801 packet_put_string(buffer_ptr(&c->output), len);
802 packet_send();
803 debug("channel: %d simulate echo (%d)", c->self, len);
804 }
805 }
Damien Millerb38eff82000-04-01 11:09:21 +1000806 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +1000807 if (compat20 && len > 0) {
808 c->local_consumed += len;
809 }
810 }
811 return 1;
812}
813int
814channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
815{
816 char buf[16*1024];
817 int len;
818
Damien Miller1383bd82000-04-06 12:32:37 +1000819/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +1000820 if (c->efd != -1) {
821 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
822 FD_ISSET(c->efd, writeset) &&
823 buffer_len(&c->extended) > 0) {
824 len = write(c->efd, buffer_ptr(&c->extended),
825 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +1100826 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000827 c->self, len, c->efd);
828 if (len > 0) {
829 buffer_consume(&c->extended, len);
830 c->local_consumed += len;
831 }
832 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
833 FD_ISSET(c->efd, readset)) {
834 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +1100835 debug2("channel %d: read %d from efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000836 c->self, len, c->efd);
Damien Miller1383bd82000-04-06 12:32:37 +1000837 if (len == 0) {
838 debug("channel %d: closing efd %d",
839 c->self, c->efd);
840 close(c->efd);
841 c->efd = -1;
842 } else if (len > 0)
Damien Miller33b13562000-04-04 14:38:59 +1000843 buffer_append(&c->extended, buf, len);
844 }
845 }
846 return 1;
847}
848int
849channel_check_window(Channel *c, fd_set * readset, fd_set * writeset)
850{
Damien Millerefb4afe2000-04-12 18:45:05 +1000851 if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +1000852 c->local_window < c->local_window_max/2 &&
853 c->local_consumed > 0) {
854 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
855 packet_put_int(c->remote_id);
856 packet_put_int(c->local_consumed);
857 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +1100858 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +1000859 c->self, c->local_window,
860 c->local_consumed);
861 c->local_window += c->local_consumed;
862 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000863 }
864 return 1;
865}
866
867void
868channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
869{
870 channel_handle_rfd(c, readset, writeset);
871 channel_handle_wfd(c, readset, writeset);
872}
873
874void
Damien Miller33b13562000-04-04 14:38:59 +1000875channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
876{
877 channel_handle_rfd(c, readset, writeset);
878 channel_handle_wfd(c, readset, writeset);
879 channel_handle_efd(c, readset, writeset);
880 channel_check_window(c, readset, writeset);
881}
882
883void
Damien Millerb38eff82000-04-01 11:09:21 +1000884channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
885{
886 int len;
887 /* Send buffered output data to the socket. */
888 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
889 len = write(c->sock, buffer_ptr(&c->output),
890 buffer_len(&c->output));
891 if (len <= 0)
892 buffer_consume(&c->output, buffer_len(&c->output));
893 else
894 buffer_consume(&c->output, len);
895 }
896}
897
898void
Damien Miller33b13562000-04-04 14:38:59 +1000899channel_handler_init_20(void)
900{
901 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +1000902 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +1000903 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100904 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +1000905 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100906 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000907 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Damien Miller33b13562000-04-04 14:38:59 +1000908
909 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
910 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100911 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +1000912 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100913 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000914 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Miller33b13562000-04-04 14:38:59 +1000915}
916
917void
Damien Millerb38eff82000-04-01 11:09:21 +1000918channel_handler_init_13(void)
919{
920 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
921 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
922 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
923 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
924 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
925 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
926 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000927 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Damien Millerb38eff82000-04-01 11:09:21 +1000928
929 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
930 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
931 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
932 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
933 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000934 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerb38eff82000-04-01 11:09:21 +1000935}
936
937void
938channel_handler_init_15(void)
939{
940 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +1000941 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +1000942 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
943 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
944 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000945 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Damien Millerb38eff82000-04-01 11:09:21 +1000946
947 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
948 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
949 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
950 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000951 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerb38eff82000-04-01 11:09:21 +1000952}
953
954void
955channel_handler_init(void)
956{
957 int i;
958 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
959 channel_pre[i] = NULL;
960 channel_post[i] = NULL;
961 }
Damien Miller33b13562000-04-04 14:38:59 +1000962 if (compat20)
963 channel_handler_init_20();
964 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +1000965 channel_handler_init_13();
966 else
967 channel_handler_init_15();
968}
969
Damien Miller4af51302000-04-16 11:18:38 +1000970void
Damien Millerb38eff82000-04-01 11:09:21 +1000971channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
972{
973 static int did_init = 0;
974 int i;
975 Channel *c;
976
977 if (!did_init) {
978 channel_handler_init();
979 did_init = 1;
980 }
981 for (i = 0; i < channels_alloc; i++) {
982 c = &channels[i];
983 if (c->type == SSH_CHANNEL_FREE)
984 continue;
985 if (ftab[c->type] == NULL)
986 continue;
987 (*ftab[c->type])(c, readset, writeset);
Damien Miller33b13562000-04-04 14:38:59 +1000988 chan_delete_if_full_closed(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000989 }
990}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991
Damien Miller4af51302000-04-16 11:18:38 +1000992void
Damien Miller5e953212001-01-30 09:14:00 +1100993channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994{
Damien Miller5e953212001-01-30 09:14:00 +1100995 int n;
996 u_int sz;
997
998 n = MAX(*maxfdp, channel_max_fd);
999
1000 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1001 if (*readsetp == NULL || n > *maxfdp) {
1002 if (*readsetp)
1003 xfree(*readsetp);
1004 if (*writesetp)
1005 xfree(*writesetp);
1006 *readsetp = xmalloc(sz);
1007 *writesetp = xmalloc(sz);
1008 *maxfdp = n;
1009 }
1010 memset(*readsetp, 0, sz);
1011 memset(*writesetp, 0, sz);
1012
1013 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001014}
1015
Damien Miller4af51302000-04-16 11:18:38 +10001016void
Damien Miller95def091999-11-25 00:26:21 +11001017channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001018{
Damien Millerb38eff82000-04-01 11:09:21 +10001019 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001020}
1021
Damien Miller5e953212001-01-30 09:14:00 +11001022/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023
Damien Miller4af51302000-04-16 11:18:38 +10001024void
Damien Miller95def091999-11-25 00:26:21 +11001025channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001026{
Damien Miller95def091999-11-25 00:26:21 +11001027 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001028 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001029
Damien Miller95def091999-11-25 00:26:21 +11001030 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001031 c = &channels[i];
Damien Miller34132e52000-01-14 15:45:46 +11001032
Damien Miller5428f641999-11-25 11:54:57 +11001033 /* We are only interested in channels that can have buffered incoming data. */
Damien Miller34132e52000-01-14 15:45:46 +11001034 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001035 if (c->type != SSH_CHANNEL_OPEN &&
1036 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001037 continue;
1038 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001039 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001040 continue;
Damien Millerb38eff82000-04-01 11:09:21 +10001041 if (c->istate != CHAN_INPUT_OPEN &&
1042 c->istate != CHAN_INPUT_WAIT_DRAIN)
Damien Miller34132e52000-01-14 15:45:46 +11001043 continue;
1044 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001045 if (compat20 &&
1046 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Damien Miller33b13562000-04-04 14:38:59 +10001047 debug("channel: %d: no data after CLOSE", c->self);
1048 continue;
1049 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001050
Damien Miller95def091999-11-25 00:26:21 +11001051 /* Get the amount of buffered data for this channel. */
Damien Millerb38eff82000-04-01 11:09:21 +10001052 len = buffer_len(&c->input);
Damien Miller95def091999-11-25 00:26:21 +11001053 if (len > 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001054 /* Send some data for the other side over the secure connection. */
Damien Miller33b13562000-04-04 14:38:59 +10001055 if (compat20) {
1056 if (len > c->remote_window)
1057 len = c->remote_window;
1058 if (len > c->remote_maxpacket)
1059 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001060 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001061 if (packet_is_interactive()) {
1062 if (len > 1024)
1063 len = 512;
1064 } else {
1065 /* Keep the packets at reasonable size. */
1066 if (len > packet_get_maxsize()/2)
1067 len = packet_get_maxsize()/2;
1068 }
Damien Miller95def091999-11-25 00:26:21 +11001069 }
Damien Millerb38eff82000-04-01 11:09:21 +10001070 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001071 packet_start(compat20 ?
1072 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001073 packet_put_int(c->remote_id);
1074 packet_put_string(buffer_ptr(&c->input), len);
1075 packet_send();
1076 buffer_consume(&c->input, len);
1077 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001078 }
1079 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001080 if (compat13)
1081 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001082 /*
1083 * input-buffer is empty and read-socket shutdown:
1084 * tell peer, that we will not send more data: send IEOF
1085 */
Damien Millerb38eff82000-04-01 11:09:21 +10001086 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001087 }
Damien Miller33b13562000-04-04 14:38:59 +10001088 /* Send extended data, i.e. stderr */
1089 if (compat20 &&
1090 c->remote_window > 0 &&
1091 (len = buffer_len(&c->extended)) > 0 &&
1092 c->extended_usage == CHAN_EXTENDED_READ) {
1093 if (len > c->remote_window)
1094 len = c->remote_window;
1095 if (len > c->remote_maxpacket)
1096 len = c->remote_maxpacket;
1097 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1098 packet_put_int(c->remote_id);
1099 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1100 packet_put_string(buffer_ptr(&c->extended), len);
1101 packet_send();
1102 buffer_consume(&c->extended, len);
1103 c->remote_window -= len;
1104 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001105 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001106}
1107
Damien Miller5428f641999-11-25 11:54:57 +11001108/*
1109 * This is called when a packet of type CHANNEL_DATA has just been received.
1110 * The message type has already been consumed, but channel number and data is
1111 * still there.
1112 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001113
Damien Miller4af51302000-04-16 11:18:38 +10001114void
Damien Miller62cee002000-09-23 17:15:56 +11001115channel_input_data(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001116{
Damien Miller34132e52000-01-14 15:45:46 +11001117 int id;
Damien Miller95def091999-11-25 00:26:21 +11001118 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001119 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001120 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001121
Damien Miller95def091999-11-25 00:26:21 +11001122 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001123 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001124 c = channel_lookup(id);
1125 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001126 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001127
Damien Miller95def091999-11-25 00:26:21 +11001128 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001129 if (c->type != SSH_CHANNEL_OPEN &&
1130 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001131 return;
1132
1133 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001134 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001135 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001136
Damien Miller95def091999-11-25 00:26:21 +11001137 /* Get the data. */
1138 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001139 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001140
Damien Miller33b13562000-04-04 14:38:59 +10001141 if (compat20){
1142 if (data_len > c->local_maxpacket) {
1143 log("channel %d: rcvd big packet %d, maxpack %d",
1144 c->self, data_len, c->local_maxpacket);
1145 }
1146 if (data_len > c->local_window) {
1147 log("channel %d: rcvd too much data %d, win %d",
1148 c->self, data_len, c->local_window);
1149 xfree(data);
1150 return;
1151 }
1152 c->local_window -= data_len;
1153 }else{
1154 packet_integrity_check(plen, 4 + 4 + data_len, type);
1155 }
Damien Millerb38eff82000-04-01 11:09:21 +10001156 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001157 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001158}
Damien Miller4af51302000-04-16 11:18:38 +10001159void
Damien Miller62cee002000-09-23 17:15:56 +11001160channel_input_extended_data(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001161{
1162 int id;
1163 int tcode;
1164 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001165 u_int data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001166 Channel *c;
1167
1168 /* Get the channel number and verify it. */
1169 id = packet_get_int();
1170 c = channel_lookup(id);
1171
1172 if (c == NULL)
1173 packet_disconnect("Received extended_data for bad channel %d.", id);
1174 if (c->type != SSH_CHANNEL_OPEN) {
1175 log("channel %d: ext data for non open", id);
1176 return;
1177 }
1178 tcode = packet_get_int();
1179 if (c->efd == -1 ||
1180 c->extended_usage != CHAN_EXTENDED_WRITE ||
1181 tcode != SSH2_EXTENDED_DATA_STDERR) {
1182 log("channel %d: bad ext data", c->self);
1183 return;
1184 }
1185 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001186 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001187 if (data_len > c->local_window) {
1188 log("channel %d: rcvd too much extended_data %d, win %d",
1189 c->self, data_len, c->local_window);
1190 xfree(data);
1191 return;
1192 }
Damien Millerd3444942000-09-30 14:20:03 +11001193 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001194 c->local_window -= data_len;
1195 buffer_append(&c->extended, data, data_len);
1196 xfree(data);
1197}
1198
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001199
Damien Miller5428f641999-11-25 11:54:57 +11001200/*
1201 * Returns true if no channel has too much buffered data, and false if one or
1202 * more channel is overfull.
1203 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001204
Damien Miller4af51302000-04-16 11:18:38 +10001205int
Damien Miller95def091999-11-25 00:26:21 +11001206channel_not_very_much_buffered_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001207{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001208 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001209 Channel *c;
Damien Miller95def091999-11-25 00:26:21 +11001210
1211 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001212 c = &channels[i];
1213 if (c->type == SSH_CHANNEL_OPEN) {
Damien Miller33b13562000-04-04 14:38:59 +10001214 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
Damien Millerb38eff82000-04-01 11:09:21 +10001215 debug("channel %d: big input buffer %d",
1216 c->self, buffer_len(&c->input));
Damien Miller95def091999-11-25 00:26:21 +11001217 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001218 }
1219 if (buffer_len(&c->output) > packet_get_maxsize()) {
1220 debug("channel %d: big output buffer %d",
1221 c->self, buffer_len(&c->output));
Damien Miller95def091999-11-25 00:26:21 +11001222 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001223 }
Damien Miller95def091999-11-25 00:26:21 +11001224 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001225 }
Damien Miller95def091999-11-25 00:26:21 +11001226 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001227}
1228
Damien Miller4af51302000-04-16 11:18:38 +10001229void
Damien Miller62cee002000-09-23 17:15:56 +11001230channel_input_ieof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001231{
1232 int id;
1233 Channel *c;
1234
1235 packet_integrity_check(plen, 4, type);
1236
1237 id = packet_get_int();
1238 c = channel_lookup(id);
1239 if (c == NULL)
1240 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1241 chan_rcvd_ieof(c);
1242}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001243
Damien Miller4af51302000-04-16 11:18:38 +10001244void
Damien Miller62cee002000-09-23 17:15:56 +11001245channel_input_close(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001246{
Damien Millerb38eff82000-04-01 11:09:21 +10001247 int id;
1248 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001249
Damien Millerb38eff82000-04-01 11:09:21 +10001250 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251
Damien Millerb38eff82000-04-01 11:09:21 +10001252 id = packet_get_int();
1253 c = channel_lookup(id);
1254 if (c == NULL)
1255 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001256
1257 /*
1258 * Send a confirmation that we have closed the channel and no more
1259 * data is coming for it.
1260 */
Damien Miller95def091999-11-25 00:26:21 +11001261 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001262 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001263 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001264
Damien Miller5428f641999-11-25 11:54:57 +11001265 /*
1266 * If the channel is in closed state, we have sent a close request,
1267 * and the other side will eventually respond with a confirmation.
1268 * Thus, we cannot free the channel here, because then there would be
1269 * no-one to receive the confirmation. The channel gets freed when
1270 * the confirmation arrives.
1271 */
Damien Millerb38eff82000-04-01 11:09:21 +10001272 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001273 /*
1274 * Not a closed channel - mark it as draining, which will
1275 * cause it to be freed later.
1276 */
Damien Millerb38eff82000-04-01 11:09:21 +10001277 buffer_consume(&c->input, buffer_len(&c->input));
1278 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001279 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001280}
1281
Damien Millerb38eff82000-04-01 11:09:21 +10001282/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001283void
Damien Miller62cee002000-09-23 17:15:56 +11001284channel_input_oclose(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001285{
Damien Millerb38eff82000-04-01 11:09:21 +10001286 int id = packet_get_int();
1287 Channel *c = channel_lookup(id);
1288 packet_integrity_check(plen, 4, type);
1289 if (c == NULL)
1290 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1291 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001292}
1293
Damien Miller4af51302000-04-16 11:18:38 +10001294void
Damien Miller62cee002000-09-23 17:15:56 +11001295channel_input_close_confirmation(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001296{
1297 int id = packet_get_int();
1298 Channel *c = channel_lookup(id);
1299
Damien Miller4af51302000-04-16 11:18:38 +10001300 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001301 if (c == NULL)
1302 packet_disconnect("Received close confirmation for "
1303 "out-of-range channel %d.", id);
1304 if (c->type != SSH_CHANNEL_CLOSED)
1305 packet_disconnect("Received close confirmation for "
1306 "non-closed channel %d (type %d).", id, c->type);
1307 channel_free(c->self);
1308}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001309
Damien Miller4af51302000-04-16 11:18:38 +10001310void
Damien Miller62cee002000-09-23 17:15:56 +11001311channel_input_open_confirmation(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001312{
Damien Millerb38eff82000-04-01 11:09:21 +10001313 int id, remote_id;
1314 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001315
Damien Miller33b13562000-04-04 14:38:59 +10001316 if (!compat20)
1317 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001318
Damien Millerb38eff82000-04-01 11:09:21 +10001319 id = packet_get_int();
1320 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001321
Damien Millerb38eff82000-04-01 11:09:21 +10001322 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1323 packet_disconnect("Received open confirmation for "
1324 "non-opening channel %d.", id);
1325 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001326 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001327 c->remote_id = remote_id;
1328 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001329
1330 if (compat20) {
1331 c->remote_window = packet_get_int();
1332 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001333 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001334 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001335 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001336 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001337 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001338 }
1339 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1340 c->remote_window, c->remote_maxpacket);
1341 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001342}
1343
Damien Miller4af51302000-04-16 11:18:38 +10001344void
Damien Miller62cee002000-09-23 17:15:56 +11001345channel_input_open_failure(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001346{
Kevin Steves12057502001-02-05 14:54:34 +00001347 int id, reason;
1348 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001349 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001350
Damien Miller33b13562000-04-04 14:38:59 +10001351 if (!compat20)
1352 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001353
1354 id = packet_get_int();
1355 c = channel_lookup(id);
1356
1357 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1358 packet_disconnect("Received open failure for "
1359 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001360 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00001361 reason = packet_get_int();
1362 if (packet_remaining() > 0) {
1363 msg = packet_get_string(NULL);
1364 lang = packet_get_string(NULL);
1365 }
Damien Miller4af51302000-04-16 11:18:38 +10001366 packet_done();
Kevin Steves12057502001-02-05 14:54:34 +00001367 log("channel_open_failure: %d: reason %d %s", id,
1368 reason, msg ? msg : "<no additional info>");
1369 if (msg != NULL)
1370 xfree(msg);
1371 if (lang != NULL)
1372 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001373 }
Damien Miller95def091999-11-25 00:26:21 +11001374 /* Free the channel. This will also close the socket. */
Damien Millerb38eff82000-04-01 11:09:21 +10001375 channel_free(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001376}
1377
Damien Miller33b13562000-04-04 14:38:59 +10001378void
Damien Miller62cee002000-09-23 17:15:56 +11001379channel_input_channel_request(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001380{
1381 int id;
1382 Channel *c;
1383
1384 id = packet_get_int();
1385 c = channel_lookup(id);
1386
1387 if (c == NULL ||
1388 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1389 packet_disconnect("Received request for "
1390 "non-open channel %d.", id);
1391 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001392 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001393 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001394 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001395 } else {
1396 char *service = packet_get_string(NULL);
1397 debug("channel: %d rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001398 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001399 xfree(service);
1400 }
1401}
1402
Damien Miller4af51302000-04-16 11:18:38 +10001403void
Damien Miller62cee002000-09-23 17:15:56 +11001404channel_input_window_adjust(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001405{
1406 Channel *c;
1407 int id, adjust;
1408
1409 if (!compat20)
1410 return;
1411
1412 /* Get the channel number and verify it. */
1413 id = packet_get_int();
1414 c = channel_lookup(id);
1415
1416 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1417 log("Received window adjust for "
1418 "non-open channel %d.", id);
1419 return;
1420 }
1421 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001422 packet_done();
Damien Millerd3444942000-09-30 14:20:03 +11001423 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10001424 c->remote_window += adjust;
1425}
1426
Damien Miller5428f641999-11-25 11:54:57 +11001427/*
1428 * Stops listening for channels, and removes any unix domain sockets that we
1429 * might have.
1430 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001431
Damien Miller4af51302000-04-16 11:18:38 +10001432void
Damien Miller95def091999-11-25 00:26:21 +11001433channel_stop_listening()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001434{
Damien Miller95def091999-11-25 00:26:21 +11001435 int i;
1436 for (i = 0; i < channels_alloc; i++) {
1437 switch (channels[i].type) {
1438 case SSH_CHANNEL_AUTH_SOCKET:
1439 close(channels[i].sock);
Damien Miller78315eb2000-09-29 23:01:36 +11001440 unlink(channels[i].path);
Damien Miller95def091999-11-25 00:26:21 +11001441 channel_free(i);
1442 break;
1443 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001444 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001445 case SSH_CHANNEL_X11_LISTENER:
1446 close(channels[i].sock);
1447 channel_free(i);
1448 break;
1449 default:
1450 break;
1451 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001452 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001453}
1454
Damien Miller5428f641999-11-25 11:54:57 +11001455/*
Damien Miller6f83b8e2000-05-02 09:23:45 +10001456 * Closes the sockets/fds of all channels. This is used to close extra file
Damien Miller5428f641999-11-25 11:54:57 +11001457 * descriptors after a fork.
1458 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001459
Damien Miller4af51302000-04-16 11:18:38 +10001460void
Damien Miller95def091999-11-25 00:26:21 +11001461channel_close_all()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001462{
Damien Miller95def091999-11-25 00:26:21 +11001463 int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +10001464 for (i = 0; i < channels_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001465 if (channels[i].type != SSH_CHANNEL_FREE)
Damien Miller6f83b8e2000-05-02 09:23:45 +10001466 channel_close_fds(&channels[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001467}
1468
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001469/* Returns true if any channel is still open. */
1470
Damien Miller4af51302000-04-16 11:18:38 +10001471int
Damien Miller95def091999-11-25 00:26:21 +11001472channel_still_open()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001473{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001474 u_int i;
Damien Miller95def091999-11-25 00:26:21 +11001475 for (i = 0; i < channels_alloc; i++)
1476 switch (channels[i].type) {
1477 case SSH_CHANNEL_FREE:
1478 case SSH_CHANNEL_X11_LISTENER:
1479 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001480 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001481 case SSH_CHANNEL_CLOSED:
1482 case SSH_CHANNEL_AUTH_SOCKET:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001483 case SSH_CHANNEL_CONNECTING: /* XXX ??? */
Damien Miller95def091999-11-25 00:26:21 +11001484 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001485 case SSH_CHANNEL_LARVAL:
1486 if (!compat20)
1487 fatal("cannot happen: SSH_CHANNEL_LARVAL");
1488 continue;
Damien Miller95def091999-11-25 00:26:21 +11001489 case SSH_CHANNEL_OPENING:
1490 case SSH_CHANNEL_OPEN:
1491 case SSH_CHANNEL_X11_OPEN:
1492 return 1;
1493 case SSH_CHANNEL_INPUT_DRAINING:
1494 case SSH_CHANNEL_OUTPUT_DRAINING:
1495 if (!compat13)
1496 fatal("cannot happen: OUT_DRAIN");
1497 return 1;
1498 default:
1499 fatal("channel_still_open: bad channel type %d", channels[i].type);
1500 /* NOTREACHED */
1501 }
1502 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001503}
1504
Damien Miller5428f641999-11-25 11:54:57 +11001505/*
1506 * Returns a message describing the currently open forwarded connections,
1507 * suitable for sending to the client. The message contains crlf pairs for
1508 * newlines.
1509 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001510
Damien Miller95def091999-11-25 00:26:21 +11001511char *
1512channel_open_message()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001513{
Damien Miller95def091999-11-25 00:26:21 +11001514 Buffer buffer;
1515 int i;
1516 char buf[512], *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001517
Damien Miller95def091999-11-25 00:26:21 +11001518 buffer_init(&buffer);
1519 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001520 buffer_append(&buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001521 for (i = 0; i < channels_alloc; i++) {
1522 Channel *c = &channels[i];
1523 switch (c->type) {
1524 case SSH_CHANNEL_FREE:
1525 case SSH_CHANNEL_X11_LISTENER:
1526 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001527 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001528 case SSH_CHANNEL_CLOSED:
1529 case SSH_CHANNEL_AUTH_SOCKET:
1530 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001531 case SSH_CHANNEL_LARVAL:
Damien Miller95def091999-11-25 00:26:21 +11001532 case SSH_CHANNEL_OPENING:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001533 case SSH_CHANNEL_CONNECTING:
Damien Miller95def091999-11-25 00:26:21 +11001534 case SSH_CHANNEL_OPEN:
1535 case SSH_CHANNEL_X11_OPEN:
1536 case SSH_CHANNEL_INPUT_DRAINING:
1537 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Millerb38eff82000-04-01 11:09:21 +10001538 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 +11001539 c->self, c->remote_name,
1540 c->type, c->remote_id,
1541 c->istate, buffer_len(&c->input),
Damien Millerb38eff82000-04-01 11:09:21 +10001542 c->ostate, buffer_len(&c->output),
1543 c->rfd, c->wfd);
Damien Miller95def091999-11-25 00:26:21 +11001544 buffer_append(&buffer, buf, strlen(buf));
1545 continue;
1546 default:
Damien Millerb38eff82000-04-01 11:09:21 +10001547 fatal("channel_open_message: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001548 /* NOTREACHED */
1549 }
1550 }
1551 buffer_append(&buffer, "\0", 1);
1552 cp = xstrdup(buffer_ptr(&buffer));
1553 buffer_free(&buffer);
1554 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001555}
1556
Damien Miller5428f641999-11-25 11:54:57 +11001557/*
1558 * Initiate forwarding of connections to local port "port" through the secure
1559 * channel to host:port from remote side.
1560 */
Kevin Steves12057502001-02-05 14:54:34 +00001561int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001562channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
1563 u_short port_to_connect, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001564{
Kevin Steves12057502001-02-05 14:54:34 +00001565 return channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +11001566 NULL, listen_port,
1567 host_to_connect, port_to_connect,
1568 gateway_ports, /*remote_fwd*/ 0);
1569}
1570
1571/*
1572 * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
1573 * (SSH_CHANNEL_RPORT_LISTENER).
1574 */
Kevin Steves12057502001-02-05 14:54:34 +00001575int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001576channel_request_forwarding(
1577 const char *listen_address, u_short listen_port,
1578 const char *host_to_connect, u_short port_to_connect,
1579 int gateway_ports, int remote_fwd)
1580{
1581 int success, ch, sock, on = 1, ctype;
Damien Miller34132e52000-01-14 15:45:46 +11001582 struct addrinfo hints, *ai, *aitop;
1583 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0bc1bd82000-11-13 22:57:25 +11001584 const char *host;
Damien Miller5428f641999-11-25 11:54:57 +11001585 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001586
Kevin Steves12057502001-02-05 14:54:34 +00001587 success = 0;
1588
Damien Miller0bc1bd82000-11-13 22:57:25 +11001589 if (remote_fwd) {
1590 host = listen_address;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001591 ctype = SSH_CHANNEL_RPORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001592 } else {
1593 host = host_to_connect;
1594 ctype =SSH_CHANNEL_PORT_LISTENER;
1595 }
1596
Kevin Steves12057502001-02-05 14:54:34 +00001597 if (strlen(host) > sizeof(channels[0].path) - 1) {
1598 error("Forward host name too long.");
1599 return success;
1600 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001601
Damien Miller0bc1bd82000-11-13 22:57:25 +11001602 /* XXX listen_address is currently ignored */
Damien Miller5428f641999-11-25 11:54:57 +11001603 /*
Damien Miller34132e52000-01-14 15:45:46 +11001604 * getaddrinfo returns a loopback address if the hostname is
1605 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11001606 */
Damien Miller34132e52000-01-14 15:45:46 +11001607 memset(&hints, 0, sizeof(hints));
1608 hints.ai_family = IPv4or6;
1609 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
1610 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001611 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001612 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
1613 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11001614
Damien Miller34132e52000-01-14 15:45:46 +11001615 for (ai = aitop; ai; ai = ai->ai_next) {
1616 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1617 continue;
1618 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1619 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001620 error("channel_request_forwarding: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001621 continue;
1622 }
1623 /* Create a port to listen for the host. */
1624 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1625 if (sock < 0) {
1626 /* this is no error since kernel may not support ipv6 */
1627 verbose("socket: %.100s", strerror(errno));
1628 continue;
1629 }
1630 /*
1631 * Set socket options. We would like the socket to disappear
1632 * as soon as it has been closed for whatever reason.
1633 */
1634 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
1635 linger.l_onoff = 1;
1636 linger.l_linger = 5;
1637 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
1638 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001639
Damien Miller34132e52000-01-14 15:45:46 +11001640 /* Bind the socket to the address. */
1641 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1642 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11001643 if (!ai->ai_next)
1644 error("bind: %.100s", strerror(errno));
1645 else
1646 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001647
Damien Miller34132e52000-01-14 15:45:46 +11001648 close(sock);
1649 continue;
1650 }
1651 /* Start listening for connections on the socket. */
1652 if (listen(sock, 5) < 0) {
1653 error("listen: %.100s", strerror(errno));
1654 close(sock);
1655 continue;
1656 }
1657 /* Allocate a channel number for the socket. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001658 ch = channel_new("port listener", ctype, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10001659 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11001660 0, xstrdup("port listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11001661 strlcpy(channels[ch].path, host, sizeof(channels[ch].path));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001662 channels[ch].host_port = port_to_connect;
1663 channels[ch].listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11001664 success = 1;
1665 }
1666 if (success == 0)
Kevin Steves12057502001-02-05 14:54:34 +00001667 error("channel_request_forwarding: cannot listen to port: %d",
1668 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001669 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00001670 return success;
Damien Miller95def091999-11-25 00:26:21 +11001671}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001672
Damien Miller5428f641999-11-25 11:54:57 +11001673/*
1674 * Initiate forwarding of connections to port "port" on remote host through
1675 * the secure channel to host:port from local side.
1676 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001677
Damien Miller4af51302000-04-16 11:18:38 +10001678void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001679channel_request_remote_forwarding(u_short listen_port,
1680 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001681{
Damien Miller0bc1bd82000-11-13 22:57:25 +11001682 int payload_len, type, success = 0;
1683
Damien Miller95def091999-11-25 00:26:21 +11001684 /* Record locally that connection to this host/port is permitted. */
1685 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1686 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001687
Damien Miller95def091999-11-25 00:26:21 +11001688 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10001689 if (compat20) {
1690 const char *address_to_bind = "0.0.0.0";
1691 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1692 packet_put_cstring("tcpip-forward");
1693 packet_put_char(0); /* boolean: want reply */
1694 packet_put_cstring(address_to_bind);
1695 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001696 packet_send();
1697 packet_write_wait();
1698 /* Assume that server accepts the request */
1699 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10001700 } else {
1701 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10001702 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10001703 packet_put_cstring(host_to_connect);
1704 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10001705 packet_send();
1706 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001707
1708 /* Wait for response from the remote side. */
1709 type = packet_read(&payload_len);
1710 switch (type) {
1711 case SSH_SMSG_SUCCESS:
1712 success = 1;
1713 break;
1714 case SSH_SMSG_FAILURE:
1715 log("Warning: Server denied remote port forwarding.");
1716 break;
1717 default:
1718 /* Unknown packet */
1719 packet_disconnect("Protocol error for port forward request:"
1720 "received packet type %d.", type);
1721 }
1722 }
1723 if (success) {
1724 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
1725 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
1726 permitted_opens[num_permitted_opens].listen_port = listen_port;
1727 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10001728 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001729}
1730
Damien Miller5428f641999-11-25 11:54:57 +11001731/*
1732 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
1733 * listening for the port, and sends back a success reply (or disconnect
1734 * message if there was an error). This never returns if there was an error.
1735 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001736
Damien Miller4af51302000-04-16 11:18:38 +10001737void
Damien Millere247cc42000-05-07 12:03:14 +10001738channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001739{
Damien Milleraae6c611999-12-06 11:47:28 +11001740 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11001741 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001742
Damien Miller95def091999-11-25 00:26:21 +11001743 /* Get arguments from the packet. */
1744 port = packet_get_int();
1745 hostname = packet_get_string(NULL);
1746 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001747
Damien Millerbac2d8a2000-09-05 16:13:06 +11001748#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11001749 /*
1750 * Check that an unprivileged user is not trying to forward a
1751 * privileged port.
1752 */
Damien Miller95def091999-11-25 00:26:21 +11001753 if (port < IPPORT_RESERVED && !is_root)
1754 packet_disconnect("Requested forwarding of port %d but user is not root.",
1755 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001756#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001757 /* Initiate forwarding */
Damien Millere247cc42000-05-07 12:03:14 +10001758 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11001759
1760 /* Free the argument string. */
1761 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001762}
1763
Damien Millerb38eff82000-04-01 11:09:21 +10001764/* XXX move to aux.c */
1765int
1766channel_connect_to(const char *host, u_short host_port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001767{
Damien Miller34132e52000-01-14 15:45:46 +11001768 struct addrinfo hints, *ai, *aitop;
1769 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1770 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10001771 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11001772
Damien Miller34132e52000-01-14 15:45:46 +11001773 memset(&hints, 0, sizeof(hints));
1774 hints.ai_family = IPv4or6;
1775 hints.ai_socktype = SOCK_STREAM;
1776 snprintf(strport, sizeof strport, "%d", host_port);
1777 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
1778 error("%.100s: unknown host (%s)", host, gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10001779 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001780 }
Damien Miller34132e52000-01-14 15:45:46 +11001781 for (ai = aitop; ai; ai = ai->ai_next) {
1782 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1783 continue;
1784 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1785 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Millerb38eff82000-04-01 11:09:21 +10001786 error("channel_connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001787 continue;
1788 }
1789 /* Create the socket. */
1790 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1791 if (sock < 0) {
1792 error("socket: %.100s", strerror(errno));
1793 continue;
1794 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +00001795 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001796 fatal("connect_to: F_SETFL: %s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11001797 /* Connect to the host/port. */
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001798 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
1799 errno != EINPROGRESS) {
Damien Miller34132e52000-01-14 15:45:46 +11001800 error("connect %.100s port %s: %.100s", ntop, strport,
1801 strerror(errno));
1802 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001803 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11001804 }
1805 break; /* success */
1806
1807 }
1808 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11001809 if (!ai) {
Kevin Stevesef4eea92001-02-05 12:42:17 +00001810 error("connect %.100s port %d: failed.", host, host_port);
Damien Millerb38eff82000-04-01 11:09:21 +10001811 return -1;
1812 }
1813 /* success */
1814 return sock;
1815}
Damien Miller0bc1bd82000-11-13 22:57:25 +11001816int
1817channel_connect_by_listen_adress(u_short listen_port)
1818{
1819 int i;
1820 for (i = 0; i < num_permitted_opens; i++)
1821 if (permitted_opens[i].listen_port == listen_port)
1822 return channel_connect_to(
1823 permitted_opens[i].host_to_connect,
1824 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00001825 error("WARNING: Server requests forwarding for unknown listen_port %d",
1826 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001827 return -1;
1828}
Damien Millerb38eff82000-04-01 11:09:21 +10001829
1830/*
1831 * This is called after receiving PORT_OPEN message. This attempts to
1832 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
1833 * or CHANNEL_OPEN_FAILURE.
1834 */
1835
Damien Miller4af51302000-04-16 11:18:38 +10001836void
Damien Miller62cee002000-09-23 17:15:56 +11001837channel_input_port_open(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001838{
1839 u_short host_port;
1840 char *host, *originator_string;
1841 int remote_channel, sock = -1, newch, i, denied;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001842 u_int host_len, originator_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001843
1844 /* Get remote channel number. */
1845 remote_channel = packet_get_int();
1846
1847 /* Get host name to connect to. */
1848 host = packet_get_string(&host_len);
1849
1850 /* Get port to connect to. */
1851 host_port = packet_get_int();
1852
1853 /* Get remote originator name. */
1854 if (have_hostname_in_open) {
1855 originator_string = packet_get_string(&originator_len);
1856 originator_len += 4; /* size of packet_int */
1857 } else {
1858 originator_string = xstrdup("unknown (remote did not supply name)");
1859 originator_len = 0; /* no originator supplied */
Damien Miller95def091999-11-25 00:26:21 +11001860 }
Damien Miller34132e52000-01-14 15:45:46 +11001861
Damien Millerb38eff82000-04-01 11:09:21 +10001862 packet_integrity_check(plen,
1863 4 + 4 + host_len + 4 + originator_len, SSH_MSG_PORT_OPEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001864
Damien Millerb38eff82000-04-01 11:09:21 +10001865 /* Check if opening that port is permitted. */
1866 denied = 0;
1867 if (!all_opens_permitted) {
1868 /* Go trough all permitted ports. */
1869 for (i = 0; i < num_permitted_opens; i++)
1870 if (permitted_opens[i].port_to_connect == host_port &&
1871 strcmp(permitted_opens[i].host_to_connect, host) == 0)
1872 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001873
Damien Millerb38eff82000-04-01 11:09:21 +10001874 /* Check if we found the requested port among those permitted. */
1875 if (i >= num_permitted_opens) {
1876 /* The port is not permitted. */
1877 log("Received request to connect to %.100s:%d, but the request was denied.",
1878 host, host_port);
1879 denied = 1;
1880 }
1881 }
1882 sock = denied ? -1 : channel_connect_to(host, host_port);
1883 if (sock > 0) {
1884 /* Allocate a channel for this connection. */
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001885 newch = channel_allocate(SSH_CHANNEL_CONNECTING,
1886 sock, originator_string);
1887/*XXX delay answer? */
Damien Millerb38eff82000-04-01 11:09:21 +10001888 channels[newch].remote_id = remote_channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001889
Damien Millerb38eff82000-04-01 11:09:21 +10001890 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1891 packet_put_int(remote_channel);
1892 packet_put_int(newch);
1893 packet_send();
1894 } else {
1895 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1896 packet_put_int(remote_channel);
1897 packet_send();
1898 }
Damien Miller95def091999-11-25 00:26:21 +11001899 xfree(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001900}
1901
Damien Miller5428f641999-11-25 11:54:57 +11001902/*
1903 * Creates an internet domain socket for listening for X11 connections.
1904 * Returns a suitable value for the DISPLAY variable, or NULL if an error
1905 * occurs.
1906 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001907
Damien Miller34132e52000-01-14 15:45:46 +11001908#define NUM_SOCKS 10
1909
Damien Miller95def091999-11-25 00:26:21 +11001910char *
Damien Millera34a28b1999-12-14 10:47:15 +11001911x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001912{
Damien Milleraae6c611999-12-06 11:47:28 +11001913 int display_number, sock;
1914 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11001915 struct addrinfo hints, *ai, *aitop;
1916 char strport[NI_MAXSERV];
1917 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
1918 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11001919 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001920
Damien Millera34a28b1999-12-14 10:47:15 +11001921 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11001922 display_number < MAX_DISPLAYS;
1923 display_number++) {
1924 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11001925 memset(&hints, 0, sizeof(hints));
1926 hints.ai_family = IPv4or6;
1927 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
1928 hints.ai_socktype = SOCK_STREAM;
1929 snprintf(strport, sizeof strport, "%d", port);
1930 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
1931 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11001932 return NULL;
1933 }
Damien Miller34132e52000-01-14 15:45:46 +11001934 for (ai = aitop; ai; ai = ai->ai_next) {
1935 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1936 continue;
1937 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1938 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11001939 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11001940 error("socket: %.100s", strerror(errno));
1941 return NULL;
1942 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11001943 debug("x11_create_display_inet: Socket family %d not supported",
1944 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11001945 continue;
1946 }
Damien Miller34132e52000-01-14 15:45:46 +11001947 }
1948 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1949 debug("bind port %d: %.100s", port, strerror(errno));
1950 shutdown(sock, SHUT_RDWR);
1951 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11001952
1953 if (ai->ai_next)
1954 continue;
1955
Damien Miller34132e52000-01-14 15:45:46 +11001956 for (n = 0; n < num_socks; n++) {
1957 shutdown(socks[n], SHUT_RDWR);
1958 close(socks[n]);
1959 }
1960 num_socks = 0;
1961 break;
1962 }
1963 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11001964#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11001965 if (num_socks == NUM_SOCKS)
1966 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11001967#else
1968 break;
1969#endif
Damien Miller95def091999-11-25 00:26:21 +11001970 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00001971 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11001972 if (num_socks > 0)
1973 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001974 }
Damien Miller95def091999-11-25 00:26:21 +11001975 if (display_number >= MAX_DISPLAYS) {
1976 error("Failed to allocate internet-domain X11 display socket.");
1977 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001978 }
Damien Miller95def091999-11-25 00:26:21 +11001979 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11001980 for (n = 0; n < num_socks; n++) {
1981 sock = socks[n];
1982 if (listen(sock, 5) < 0) {
1983 error("listen: %.100s", strerror(errno));
1984 shutdown(sock, SHUT_RDWR);
1985 close(sock);
1986 return NULL;
1987 }
Damien Miller95def091999-11-25 00:26:21 +11001988 }
Damien Miller34132e52000-01-14 15:45:46 +11001989
Damien Miller95def091999-11-25 00:26:21 +11001990 /* Set up a suitable value for the DISPLAY variable. */
1991 if (gethostname(hostname, sizeof(hostname)) < 0)
1992 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11001993
1994#ifdef IPADDR_IN_DISPLAY
Kevin Stevesef4eea92001-02-05 12:42:17 +00001995 /*
Damien Miller76112de1999-12-21 11:18:08 +11001996 * HPUX detects the local hostname in the DISPLAY variable and tries
1997 * to set up a shared memory connection to the server, which it
1998 * incorrectly supposes to be local.
1999 *
2000 * The workaround - as used in later $$H and other programs - is
2001 * is to set display to the host's IP address.
2002 */
2003 {
2004 struct hostent *he;
2005 struct in_addr my_addr;
2006
2007 he = gethostbyname(hostname);
2008 if (he == NULL) {
2009 error("[X11-broken-fwd-hostname-workaround] Could not get "
2010 "IP address for hostname %s.", hostname);
2011
2012 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
2013 "Could not get IP address for hostname %s.", hostname);
2014
2015 shutdown(sock, SHUT_RDWR);
2016 close(sock);
2017
2018 return NULL;
2019 }
2020
2021 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2022
2023 /* Set DISPLAY to <ip address>:screen.display */
Kevin Stevesef4eea92001-02-05 12:42:17 +00002024 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Kevin Stevesda6cd592000-12-29 18:41:21 +00002025 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002026 }
2027#else /* IPADDR_IN_DISPLAY */
2028 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11002029 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Kevin Stevesda6cd592000-12-29 18:41:21 +00002030 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002031#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002032
Damien Miller34132e52000-01-14 15:45:46 +11002033 /* Allocate a channel for each socket. */
2034 for (n = 0; n < num_socks; n++) {
2035 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10002036 (void) channel_new("x11 listener",
2037 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2038 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002039 0, xstrdup("X11 inet listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11002040 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002041
Damien Miller95def091999-11-25 00:26:21 +11002042 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11002043 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002044}
2045
2046#ifndef X_UNIX_PATH
2047#define X_UNIX_PATH "/tmp/.X11-unix/X"
2048#endif
2049
2050static
2051int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002052connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002053{
Damien Miller95def091999-11-25 00:26:21 +11002054 static const char *const x_sockets[] = {
2055 X_UNIX_PATH "%u",
2056 "/var/X/.X11-unix/X" "%u",
2057 "/usr/spool/sockets/X11/" "%u",
2058 NULL
2059 };
2060 int sock;
2061 struct sockaddr_un addr;
2062 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002063
Damien Miller95def091999-11-25 00:26:21 +11002064 for (path = x_sockets; *path; ++path) {
2065 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2066 if (sock < 0)
2067 error("socket: %.100s", strerror(errno));
2068 memset(&addr, 0, sizeof(addr));
2069 addr.sun_family = AF_UNIX;
2070 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
2071 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2072 return sock;
2073 close(sock);
2074 }
2075 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2076 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002077}
2078
Damien Millerbd483e72000-04-30 10:00:53 +10002079int
2080x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002081{
Damien Millerbd483e72000-04-30 10:00:53 +10002082 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002083 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002084 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002085 struct addrinfo hints, *ai, *aitop;
2086 char strport[NI_MAXSERV];
2087 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002088
Damien Miller95def091999-11-25 00:26:21 +11002089 /* Try to open a socket for the local X server. */
2090 display = getenv("DISPLAY");
2091 if (!display) {
2092 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002093 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002094 }
Damien Miller5428f641999-11-25 11:54:57 +11002095 /*
2096 * Now we decode the value of the DISPLAY variable and make a
2097 * connection to the real X server.
2098 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002099
Damien Miller5428f641999-11-25 11:54:57 +11002100 /*
2101 * Check if it is a unix domain socket. Unix domain displays are in
2102 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2103 */
Damien Miller95def091999-11-25 00:26:21 +11002104 if (strncmp(display, "unix:", 5) == 0 ||
2105 display[0] == ':') {
2106 /* Connect to the unix domain socket. */
2107 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2108 error("Could not parse display number from DISPLAY: %.100s",
2109 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002110 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002111 }
2112 /* Create a socket. */
2113 sock = connect_local_xsocket(display_number);
2114 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002115 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002116
2117 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002118 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002119 }
Damien Miller5428f641999-11-25 11:54:57 +11002120 /*
2121 * Connect to an inet socket. The DISPLAY value is supposedly
2122 * hostname:d[.s], where hostname may also be numeric IP address.
2123 */
Damien Miller95def091999-11-25 00:26:21 +11002124 strncpy(buf, display, sizeof(buf));
2125 buf[sizeof(buf) - 1] = 0;
2126 cp = strchr(buf, ':');
2127 if (!cp) {
2128 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002129 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002130 }
Damien Miller95def091999-11-25 00:26:21 +11002131 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002132 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002133 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2134 error("Could not parse display number from DISPLAY: %.100s",
2135 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002136 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002137 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002138
Damien Miller34132e52000-01-14 15:45:46 +11002139 /* Look up the host address */
2140 memset(&hints, 0, sizeof(hints));
2141 hints.ai_family = IPv4or6;
2142 hints.ai_socktype = SOCK_STREAM;
2143 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2144 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2145 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002146 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002147 }
Damien Miller34132e52000-01-14 15:45:46 +11002148 for (ai = aitop; ai; ai = ai->ai_next) {
2149 /* Create a socket. */
2150 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2151 if (sock < 0) {
2152 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002153 continue;
2154 }
2155 /* Connect it to the display. */
2156 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2157 debug("connect %.100s port %d: %.100s", buf,
2158 6000 + display_number, strerror(errno));
2159 close(sock);
2160 continue;
2161 }
2162 /* Success */
2163 break;
Damien Miller34132e52000-01-14 15:45:46 +11002164 }
Damien Miller34132e52000-01-14 15:45:46 +11002165 freeaddrinfo(aitop);
2166 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002167 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002168 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002169 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002170 }
Damien Millerbd483e72000-04-30 10:00:53 +10002171 return sock;
2172}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002173
Damien Millerbd483e72000-04-30 10:00:53 +10002174/*
2175 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2176 * the remote channel number. We should do whatever we want, and respond
2177 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2178 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002179
Damien Millerbd483e72000-04-30 10:00:53 +10002180void
Damien Miller62cee002000-09-23 17:15:56 +11002181x11_input_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002182{
2183 int remote_channel, sock = 0, newch;
2184 char *remote_host;
Ben Lindstrom46c16222000-12-22 01:43:59 +00002185 u_int remote_len;
Damien Miller95def091999-11-25 00:26:21 +11002186
Damien Millerbd483e72000-04-30 10:00:53 +10002187 /* Get remote channel number. */
2188 remote_channel = packet_get_int();
Damien Miller95def091999-11-25 00:26:21 +11002189
Damien Millerbd483e72000-04-30 10:00:53 +10002190 /* Get remote originator name. */
2191 if (have_hostname_in_open) {
2192 remote_host = packet_get_string(&remote_len);
2193 remote_len += 4;
2194 } else {
2195 remote_host = xstrdup("unknown (remote did not supply name)");
2196 remote_len = 0;
2197 }
2198
2199 debug("Received X11 open request.");
2200 packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN);
2201
2202 /* Obtain a connection to the real X display. */
2203 sock = x11_connect_display();
2204 if (sock == -1) {
2205 /* Send refusal to the remote host. */
2206 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2207 packet_put_int(remote_channel);
2208 packet_send();
2209 } else {
2210 /* Allocate a channel for this connection. */
2211 newch = channel_allocate(
2212 (x11_saved_proto == NULL) ?
2213 SSH_CHANNEL_OPEN : SSH_CHANNEL_X11_OPEN,
2214 sock, remote_host);
2215 channels[newch].remote_id = remote_channel;
2216
2217 /* Send a confirmation to the remote host. */
2218 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2219 packet_put_int(remote_channel);
2220 packet_put_int(newch);
2221 packet_send();
2222 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002223}
2224
Damien Miller69b69aa2000-10-28 14:19:58 +11002225/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2226void
2227deny_input_open(int type, int plen, void *ctxt)
2228{
2229 int rchan = packet_get_int();
2230 switch(type){
2231 case SSH_SMSG_AGENT_OPEN:
2232 error("Warning: ssh server tried agent forwarding.");
2233 break;
2234 case SSH_SMSG_X11_OPEN:
2235 error("Warning: ssh server tried X11 forwarding.");
2236 break;
2237 default:
2238 error("deny_input_open: type %d plen %d", type, plen);
2239 break;
2240 }
2241 error("Warning: this is probably a break in attempt by a malicious server.");
2242 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2243 packet_put_int(rchan);
2244 packet_send();
2245}
2246
Damien Miller5428f641999-11-25 11:54:57 +11002247/*
2248 * Requests forwarding of X11 connections, generates fake authentication
2249 * data, and enables authentication spoofing.
2250 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002251
Damien Miller4af51302000-04-16 11:18:38 +10002252void
Damien Millerbd483e72000-04-30 10:00:53 +10002253x11_request_forwarding_with_spoofing(int client_session_id,
2254 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002255{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002256 u_int data_len = (u_int) strlen(data) / 2;
Ben Lindstromb3211a82001-02-10 22:33:19 +00002257 u_int i, value, len;
Damien Miller95def091999-11-25 00:26:21 +11002258 char *new_data;
2259 int screen_number;
2260 const char *cp;
2261 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002262
Damien Miller95def091999-11-25 00:26:21 +11002263 cp = getenv("DISPLAY");
2264 if (cp)
2265 cp = strchr(cp, ':');
2266 if (cp)
2267 cp = strchr(cp, '.');
2268 if (cp)
2269 screen_number = atoi(cp + 1);
2270 else
2271 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002272
Damien Miller95def091999-11-25 00:26:21 +11002273 /* Save protocol name. */
2274 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002275
Damien Miller5428f641999-11-25 11:54:57 +11002276 /*
2277 * Extract real authentication data and generate fake data of the
2278 * same length.
2279 */
Damien Miller95def091999-11-25 00:26:21 +11002280 x11_saved_data = xmalloc(data_len);
2281 x11_fake_data = xmalloc(data_len);
2282 for (i = 0; i < data_len; i++) {
2283 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2284 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2285 if (i % 4 == 0)
2286 rand = arc4random();
2287 x11_saved_data[i] = value;
2288 x11_fake_data[i] = rand & 0xff;
2289 rand >>= 8;
2290 }
2291 x11_saved_data_len = data_len;
2292 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002293
Damien Miller95def091999-11-25 00:26:21 +11002294 /* Convert the fake data into hex. */
Ben Lindstromb3211a82001-02-10 22:33:19 +00002295 len = 2 * data_len + 1;
2296 new_data = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +11002297 for (i = 0; i < data_len; i++)
Ben Lindstromb3211a82001-02-10 22:33:19 +00002298 snprintf(new_data + 2 * i, len - 2 * i,
2299 "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002300
Damien Miller95def091999-11-25 00:26:21 +11002301 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002302 if (compat20) {
2303 channel_request_start(client_session_id, "x11-req", 0);
2304 packet_put_char(0); /* XXX bool single connection */
2305 } else {
2306 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2307 }
2308 packet_put_cstring(proto);
2309 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002310 packet_put_int(screen_number);
2311 packet_send();
2312 packet_write_wait();
2313 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002314}
2315
2316/* Sends a message to the server to request authentication fd forwarding. */
2317
Damien Miller4af51302000-04-16 11:18:38 +10002318void
Damien Miller95def091999-11-25 00:26:21 +11002319auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002320{
Damien Miller95def091999-11-25 00:26:21 +11002321 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2322 packet_send();
2323 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002324}
2325
Damien Miller5428f641999-11-25 11:54:57 +11002326/*
2327 * Returns the name of the forwarded authentication socket. Returns NULL if
2328 * there is no forwarded authentication socket. The returned value points to
2329 * a static buffer.
2330 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002331
Damien Miller95def091999-11-25 00:26:21 +11002332char *
2333auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002334{
Damien Miller95def091999-11-25 00:26:21 +11002335 return channel_forwarded_auth_socket_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002336}
2337
2338/* removes the agent forwarding socket */
2339
Damien Miller4af51302000-04-16 11:18:38 +10002340void
Damien Miller95def091999-11-25 00:26:21 +11002341cleanup_socket(void)
2342{
Damien Miller78315eb2000-09-29 23:01:36 +11002343 unlink(channel_forwarded_auth_socket_name);
Damien Miller95def091999-11-25 00:26:21 +11002344 rmdir(channel_forwarded_auth_socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002345}
2346
Damien Miller5428f641999-11-25 11:54:57 +11002347/*
Damien Millerd3a18572000-06-07 19:55:44 +10002348 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002349 * This starts forwarding authentication requests.
2350 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002351
Damien Millerd3a18572000-06-07 19:55:44 +10002352int
Damien Miller95def091999-11-25 00:26:21 +11002353auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002354{
Damien Miller95def091999-11-25 00:26:21 +11002355 int sock, newch;
2356 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002357
Damien Miller95def091999-11-25 00:26:21 +11002358 if (auth_get_socket_name() != NULL)
2359 fatal("Protocol error: authentication forwarding requested twice.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002360
Damien Miller95def091999-11-25 00:26:21 +11002361 /* Temporarily drop privileged uid for mkdir/bind. */
2362 temporarily_use_uid(pw->pw_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002363
Damien Miller95def091999-11-25 00:26:21 +11002364 /* Allocate a buffer for the socket name, and format the name. */
2365 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
2366 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
2367 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002368
Damien Miller95def091999-11-25 00:26:21 +11002369 /* Create private directory for socket */
Damien Millerd3a18572000-06-07 19:55:44 +10002370 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL) {
2371 packet_send_debug("Agent forwarding disabled: mkdtemp() failed: %.100s",
2372 strerror(errno));
2373 restore_uid();
2374 xfree(channel_forwarded_auth_socket_name);
2375 xfree(channel_forwarded_auth_socket_dir);
2376 channel_forwarded_auth_socket_name = NULL;
2377 channel_forwarded_auth_socket_dir = NULL;
2378 return 0;
2379 }
Damien Miller95def091999-11-25 00:26:21 +11002380 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
2381 channel_forwarded_auth_socket_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002382
Damien Miller95def091999-11-25 00:26:21 +11002383 if (atexit(cleanup_socket) < 0) {
2384 int saved = errno;
2385 cleanup_socket();
2386 packet_disconnect("socket: %.100s", strerror(saved));
2387 }
2388 /* Create the socket. */
2389 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2390 if (sock < 0)
2391 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002392
Damien Miller95def091999-11-25 00:26:21 +11002393 /* Bind it to the name. */
2394 memset(&sunaddr, 0, sizeof(sunaddr));
2395 sunaddr.sun_family = AF_UNIX;
2396 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
2397 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002398
Damien Miller95def091999-11-25 00:26:21 +11002399 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2400 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002401
Damien Miller95def091999-11-25 00:26:21 +11002402 /* Restore the privileged uid. */
2403 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002404
Damien Miller95def091999-11-25 00:26:21 +11002405 /* Start listening on the socket. */
2406 if (listen(sock, 5) < 0)
2407 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002408
Damien Miller95def091999-11-25 00:26:21 +11002409 /* Allocate a channel for the authentication agent socket. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002410 newch = channel_new("auth socket",
2411 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2412 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2413 0, xstrdup("auth socket"), 1);
2414
Damien Miller037a0dc1999-12-07 15:38:31 +11002415 strlcpy(channels[newch].path, channel_forwarded_auth_socket_name,
2416 sizeof(channels[newch].path));
Damien Millerd3a18572000-06-07 19:55:44 +10002417 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002418}
2419
2420/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2421
Damien Miller4af51302000-04-16 11:18:38 +10002422void
Damien Miller62cee002000-09-23 17:15:56 +11002423auth_input_open_request(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002424{
Damien Miller95def091999-11-25 00:26:21 +11002425 int remch, sock, newch;
2426 char *dummyname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002427
Damien Millerb38eff82000-04-01 11:09:21 +10002428 packet_integrity_check(plen, 4, type);
2429
Damien Miller95def091999-11-25 00:26:21 +11002430 /* Read the remote channel number from the message. */
2431 remch = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002432
Damien Miller5428f641999-11-25 11:54:57 +11002433 /*
2434 * Get a connection to the local authentication agent (this may again
2435 * get forwarded).
2436 */
Damien Miller95def091999-11-25 00:26:21 +11002437 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002438
Damien Miller5428f641999-11-25 11:54:57 +11002439 /*
2440 * If we could not connect the agent, send an error message back to
2441 * the server. This should never happen unless the agent dies,
2442 * because authentication forwarding is only enabled if we have an
2443 * agent.
2444 */
Damien Miller95def091999-11-25 00:26:21 +11002445 if (sock < 0) {
2446 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2447 packet_put_int(remch);
2448 packet_send();
2449 return;
2450 }
2451 debug("Forwarding authentication connection.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002452
Damien Miller5428f641999-11-25 11:54:57 +11002453 /*
2454 * Dummy host name. This will be freed when the channel is freed; it
2455 * will still be valid in the packet_put_string below since the
2456 * channel cannot yet be freed at that point.
2457 */
Damien Miller95def091999-11-25 00:26:21 +11002458 dummyname = xstrdup("authentication agent connection");
2459
2460 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);
2461 channels[newch].remote_id = remch;
2462
2463 /* Send a confirmation to the remote host. */
2464 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2465 packet_put_int(remch);
2466 packet_put_int(newch);
2467 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002468}
Damien Miller33b13562000-04-04 14:38:59 +10002469
2470void
Damien Millerbd483e72000-04-30 10:00:53 +10002471channel_start_open(int id)
Damien Miller33b13562000-04-04 14:38:59 +10002472{
2473 Channel *c = channel_lookup(id);
2474 if (c == NULL) {
2475 log("channel_open: %d: bad id", id);
2476 return;
2477 }
Damien Millerbd483e72000-04-30 10:00:53 +10002478 debug("send channel open %d", id);
Damien Miller33b13562000-04-04 14:38:59 +10002479 packet_start(SSH2_MSG_CHANNEL_OPEN);
2480 packet_put_cstring(c->ctype);
2481 packet_put_int(c->self);
2482 packet_put_int(c->local_window);
2483 packet_put_int(c->local_maxpacket);
Damien Millerbd483e72000-04-30 10:00:53 +10002484}
2485void
2486channel_open(int id)
2487{
2488 /* XXX REMOVE ME */
2489 channel_start_open(id);
Damien Miller33b13562000-04-04 14:38:59 +10002490 packet_send();
Damien Miller33b13562000-04-04 14:38:59 +10002491}
2492void
2493channel_request(int id, char *service, int wantconfirm)
2494{
2495 channel_request_start(id, service, wantconfirm);
2496 packet_send();
2497 debug("channel request %d: %s", id, service) ;
2498}
2499void
2500channel_request_start(int id, char *service, int wantconfirm)
2501{
2502 Channel *c = channel_lookup(id);
2503 if (c == NULL) {
2504 log("channel_request: %d: bad id", id);
2505 return;
2506 }
2507 packet_start(SSH2_MSG_CHANNEL_REQUEST);
2508 packet_put_int(c->remote_id);
2509 packet_put_cstring(service);
2510 packet_put_char(wantconfirm);
2511}
2512void
2513channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
2514{
2515 Channel *c = channel_lookup(id);
2516 if (c == NULL) {
2517 log("channel_register_callback: %d: bad id", id);
2518 return;
2519 }
2520 c->cb_event = mtype;
2521 c->cb_fn = fn;
2522 c->cb_arg = arg;
2523}
2524void
2525channel_register_cleanup(int id, channel_callback_fn *fn)
2526{
2527 Channel *c = channel_lookup(id);
2528 if (c == NULL) {
2529 log("channel_register_cleanup: %d: bad id", id);
2530 return;
2531 }
2532 c->dettach_user = fn;
2533}
2534void
2535channel_cancel_cleanup(int id)
2536{
2537 Channel *c = channel_lookup(id);
2538 if (c == NULL) {
2539 log("channel_cancel_cleanup: %d: bad id", id);
2540 return;
2541 }
2542 c->dettach_user = NULL;
2543}
Kevin Stevesef4eea92001-02-05 12:42:17 +00002544void
Damien Millerad833b32000-08-23 10:46:23 +10002545channel_register_filter(int id, channel_filter_fn *fn)
2546{
2547 Channel *c = channel_lookup(id);
2548 if (c == NULL) {
2549 log("channel_register_filter: %d: bad id", id);
2550 return;
2551 }
2552 c->input_filter = fn;
2553}
Damien Miller33b13562000-04-04 14:38:59 +10002554
2555void
Damien Miller69b69aa2000-10-28 14:19:58 +11002556channel_set_fds(int id, int rfd, int wfd, int efd,
2557 int extusage, int nonblock)
Damien Miller33b13562000-04-04 14:38:59 +10002558{
2559 Channel *c = channel_lookup(id);
2560 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
2561 fatal("channel_activate for non-larval channel %d.", id);
Damien Miller69b69aa2000-10-28 14:19:58 +11002562 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller33b13562000-04-04 14:38:59 +10002563 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002564 /* XXX window size? */
Damien Millere4340be2000-09-16 13:29:08 +11002565 c->local_window = c->local_window_max = c->local_maxpacket * 2;
Damien Miller33b13562000-04-04 14:38:59 +10002566 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2567 packet_put_int(c->remote_id);
2568 packet_put_int(c->local_window);
2569 packet_send();
2570}