blob: 0e334db88a24025549f2bf814851f3ab7e1f43a2 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * This file contains functions for generic socket connection forwarding.
6 * There is also code for initiating connection forwarding for X11 connections,
7 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 *
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Millere4340be2000-09-16 13:29:08 +110017 * Copyright (c) 1999,2000 Markus Friedl. All rights reserved.
18 * Copyright (c) 1999 Dug Song. All rights reserved.
19 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +000043RCSID("$OpenBSD: channels.c,v 1.103 2001/04/07 08:55:17 markus Exp $");
Ben Lindstrom226cfa02001-01-22 05:34:40 +000044
45#include <openssl/rsa.h>
46#include <openssl/dsa.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047
48#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000049#include "ssh1.h"
50#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051#include "packet.h"
52#include "xmalloc.h"
53#include "buffer.h"
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +000054#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "log.h"
57#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058#include "channels.h"
59#include "nchan.h"
60#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000061#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100062#include "key.h"
63#include "authfd.h"
64
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065/* Maximum number of fake X11 displays to try. */
66#define MAX_DISPLAYS 1000
67
68/* Max len of agent socket */
69#define MAX_SOCKET_NAME 100
70
Damien Miller5428f641999-11-25 11:54:57 +110071/*
72 * Pointer to an array containing all allocated channels. The array is
73 * dynamically extended as needed.
74 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075static Channel *channels = NULL;
76
Damien Miller5428f641999-11-25 11:54:57 +110077/*
78 * Size of the channel array. All slots of the array must always be
79 * initialized (at least the type field); unused slots are marked with type
80 * SSH_CHANNEL_FREE.
81 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082static int channels_alloc = 0;
83
Damien Miller5428f641999-11-25 11:54:57 +110084/*
85 * Maximum file descriptor value used in any of the channels. This is
86 * updated in channel_allocate.
87 */
Damien Miller5e953212001-01-30 09:14:00 +110088static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
90/* Name and directory of socket for authentication agent forwarding. */
91static char *channel_forwarded_auth_socket_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +110092static char *channel_forwarded_auth_socket_dir = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
94/* Saved X11 authentication protocol name. */
95char *x11_saved_proto = NULL;
96
97/* Saved X11 authentication data. This is the real data. */
98char *x11_saved_data = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +000099u_int x11_saved_data_len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
Damien Miller5428f641999-11-25 11:54:57 +1100101/*
102 * Fake X11 authentication data. This is what the server will be sending us;
103 * we should replace any occurrences of this by the real data.
104 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105char *x11_fake_data = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000106u_int x11_fake_data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
Damien Miller5428f641999-11-25 11:54:57 +1100108/*
109 * Data structure for storing which hosts are permitted for forward requests.
110 * The local sides of any remote forwards are stored in this array to prevent
111 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
112 * network (which might be behind a firewall).
113 */
Damien Miller95def091999-11-25 00:26:21 +1100114typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000115 char *host_to_connect; /* Connect to 'host'. */
116 u_short port_to_connect; /* Connect to 'port'. */
117 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118} ForwardPermission;
119
120/* List of all permitted host/port pairs to connect. */
121static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
122/* Number of permitted host/port pairs in the array. */
123static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +1100124/*
125 * If this is true, all opens are permitted. This is the case on the server
126 * on which we have to trust the client anyway, and the user could do
127 * anything after logging in anyway.
128 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129static int all_opens_permitted = 0;
130
131/* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */
132static int have_hostname_in_open = 0;
133
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000134/* AF_UNSPEC or AF_INET or AF_INET6 */
135extern int IPv4or6;
136
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000137void port_open_helper(Channel *c, char *rtype);
138
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139/* Sets specific protocol options. */
140
Damien Miller4af51302000-04-16 11:18:38 +1000141void
Damien Miller95def091999-11-25 00:26:21 +1100142channel_set_options(int hostname_in_open)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143{
Damien Miller95def091999-11-25 00:26:21 +1100144 have_hostname_in_open = hostname_in_open;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145}
146
Damien Millerb38eff82000-04-01 11:09:21 +1000147/* lookup channel by id */
148
149Channel *
150channel_lookup(int id)
151{
152 Channel *c;
Damien Millerc0fd17f2000-06-26 10:22:53 +1000153 if (id < 0 || id > channels_alloc) {
Damien Millerb38eff82000-04-01 11:09:21 +1000154 log("channel_lookup: %d: bad id", id);
155 return NULL;
156 }
157 c = &channels[id];
158 if (c->type == SSH_CHANNEL_FREE) {
159 log("channel_lookup: %d: bad id: channel free", id);
160 return NULL;
161 }
162 return c;
163}
164
Damien Miller5428f641999-11-25 11:54:57 +1100165/*
Damien Millere247cc42000-05-07 12:03:14 +1000166 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000167 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100168 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169
Damien Miller6f83b8e2000-05-02 09:23:45 +1000170void
Damien Miller69b69aa2000-10-28 14:19:58 +1100171channel_register_fds(Channel *c, int rfd, int wfd, int efd,
172 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173{
Damien Miller95def091999-11-25 00:26:21 +1100174 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100175 channel_max_fd = MAX(channel_max_fd, rfd);
176 channel_max_fd = MAX(channel_max_fd, wfd);
177 channel_max_fd = MAX(channel_max_fd, efd);
178
Damien Miller5428f641999-11-25 11:54:57 +1100179 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000180
Damien Miller6f83b8e2000-05-02 09:23:45 +1000181 c->rfd = rfd;
182 c->wfd = wfd;
183 c->sock = (rfd == wfd) ? rfd : -1;
184 c->efd = efd;
185 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100186
Damien Miller79438cc2001-02-16 12:34:57 +1100187 /* XXX ugly hack: nonblock is only set by the server */
188 if (nonblock && isatty(c->rfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000189 debug("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100190 c->isatty = 1;
191 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000192 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100193 c->self, c->wfd);
194 }
195 } else {
196 c->isatty = 0;
197 }
198
Damien Miller69b69aa2000-10-28 14:19:58 +1100199 /* enable nonblocking mode */
200 if (nonblock) {
201 if (rfd != -1)
202 set_nonblock(rfd);
203 if (wfd != -1)
204 set_nonblock(wfd);
205 if (efd != -1)
206 set_nonblock(efd);
207 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000208}
209
210/*
211 * Allocate a new channel object and set its type and socket. This will cause
212 * remote_name to be freed.
213 */
214
215int
216channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Damien Miller69b69aa2000-10-28 14:19:58 +1100217 int window, int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000218{
219 int i, found;
220 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221
Damien Miller95def091999-11-25 00:26:21 +1100222 /* Do initial allocation if this is the first call. */
223 if (channels_alloc == 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000224 chan_init();
Damien Miller95def091999-11-25 00:26:21 +1100225 channels_alloc = 10;
226 channels = xmalloc(channels_alloc * sizeof(Channel));
227 for (i = 0; i < channels_alloc; i++)
228 channels[i].type = SSH_CHANNEL_FREE;
Damien Miller5428f641999-11-25 11:54:57 +1100229 /*
230 * Kludge: arrange a call to channel_stop_listening if we
231 * terminate with fatal().
232 */
Damien Miller95def091999-11-25 00:26:21 +1100233 fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL);
234 }
235 /* Try to find a free slot where to put the new channel. */
236 for (found = -1, i = 0; i < channels_alloc; i++)
237 if (channels[i].type == SSH_CHANNEL_FREE) {
238 /* Found a free slot. */
239 found = i;
240 break;
241 }
242 if (found == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100243 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100244 found = channels_alloc;
245 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100246 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100247 channels = xrealloc(channels, channels_alloc * sizeof(Channel));
248 for (i = found; i < channels_alloc; i++)
249 channels[i].type = SSH_CHANNEL_FREE;
250 }
251 /* Initialize and return new channel number. */
252 c = &channels[found];
253 buffer_init(&c->input);
254 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000255 buffer_init(&c->extended);
Damien Miller95def091999-11-25 00:26:21 +1100256 chan_init_iostates(c);
Damien Miller69b69aa2000-10-28 14:19:58 +1100257 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100258 c->self = found;
259 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000260 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000261 c->local_window = window;
262 c->local_window_max = window;
263 c->local_consumed = 0;
264 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100265 c->remote_id = -1;
266 c->remote_name = remote_name;
Damien Millerb38eff82000-04-01 11:09:21 +1000267 c->remote_window = 0;
268 c->remote_maxpacket = 0;
269 c->cb_fn = NULL;
270 c->cb_arg = NULL;
271 c->cb_event = 0;
272 c->dettach_user = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000273 c->input_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100274 debug("channel %d: new [%s]", found, remote_name);
275 return found;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000277/* old interface XXX */
Damien Miller4af51302000-04-16 11:18:38 +1000278int
Damien Millerb38eff82000-04-01 11:09:21 +1000279channel_allocate(int type, int sock, char *remote_name)
280{
Damien Miller69b69aa2000-10-28 14:19:58 +1100281 return channel_new("", type, sock, sock, -1, 0, 0, 0, remote_name, 1);
Damien Millerb38eff82000-04-01 11:09:21 +1000282}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283
Damien Miller6f83b8e2000-05-02 09:23:45 +1000284
285/* Close all channel fd/socket. */
286
287void
288channel_close_fds(Channel *c)
289{
290 if (c->sock != -1) {
291 close(c->sock);
292 c->sock = -1;
293 }
294 if (c->rfd != -1) {
295 close(c->rfd);
296 c->rfd = -1;
297 }
298 if (c->wfd != -1) {
299 close(c->wfd);
300 c->wfd = -1;
301 }
302 if (c->efd != -1) {
303 close(c->efd);
304 c->efd = -1;
305 }
306}
307
308/* Free the channel and close its fd/socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309
Damien Miller4af51302000-04-16 11:18:38 +1000310void
Damien Millerb38eff82000-04-01 11:09:21 +1000311channel_free(int id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312{
Damien Millerb38eff82000-04-01 11:09:21 +1000313 Channel *c = channel_lookup(id);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000314 char *s = channel_open_message();
315
Damien Millerb38eff82000-04-01 11:09:21 +1000316 if (c == NULL)
317 packet_disconnect("channel free: bad local channel %d", id);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000318 debug("channel_free: channel %d: status: %s", id, s);
319 xfree(s);
320
Damien Miller33b13562000-04-04 14:38:59 +1000321 if (c->dettach_user != NULL) {
322 debug("channel_free: channel %d: dettaching channel user", id);
323 c->dettach_user(c->self, NULL);
324 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000325 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000326 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000327 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000328 buffer_free(&c->input);
329 buffer_free(&c->output);
330 buffer_free(&c->extended);
331 c->type = SSH_CHANNEL_FREE;
332 if (c->remote_name) {
333 xfree(c->remote_name);
334 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100335 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336}
337
Damien Miller5428f641999-11-25 11:54:57 +1100338/*
Damien Millerb38eff82000-04-01 11:09:21 +1000339 * 'channel_pre*' are called just before select() to add any bits relevant to
340 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100341 */
Damien Millerb38eff82000-04-01 11:09:21 +1000342/*
343 * 'channel_post*': perform any appropriate operations for channels which
344 * have events pending.
345 */
346typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
347chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
348chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
349
350void
351channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
352{
353 FD_SET(c->sock, readset);
354}
355
356void
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000357channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
358{
359 debug3("channel %d: waiting for connection", c->self);
360 FD_SET(c->sock, writeset);
361}
362
363void
Damien Millerb38eff82000-04-01 11:09:21 +1000364channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
365{
366 if (buffer_len(&c->input) < packet_get_maxsize())
367 FD_SET(c->sock, readset);
368 if (buffer_len(&c->output) > 0)
369 FD_SET(c->sock, writeset);
370}
371
372void
373channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
374{
375 /* test whether sockets are 'alive' for read/write */
376 if (c->istate == CHAN_INPUT_OPEN)
377 if (buffer_len(&c->input) < packet_get_maxsize())
378 FD_SET(c->sock, readset);
379 if (c->ostate == CHAN_OUTPUT_OPEN ||
380 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
381 if (buffer_len(&c->output) > 0) {
382 FD_SET(c->sock, writeset);
383 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
384 chan_obuf_empty(c);
385 }
386 }
387}
388
389void
Damien Miller33b13562000-04-04 14:38:59 +1000390channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
391{
392 if (c->istate == CHAN_INPUT_OPEN &&
393 c->remote_window > 0 &&
394 buffer_len(&c->input) < c->remote_window)
395 FD_SET(c->rfd, readset);
396 if (c->ostate == CHAN_OUTPUT_OPEN ||
397 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
398 if (buffer_len(&c->output) > 0) {
399 FD_SET(c->wfd, writeset);
400 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
401 chan_obuf_empty(c);
402 }
403 }
404 /** XXX check close conditions, too */
405 if (c->efd != -1) {
406 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
407 buffer_len(&c->extended) > 0)
408 FD_SET(c->efd, writeset);
409 else if (c->extended_usage == CHAN_EXTENDED_READ &&
410 buffer_len(&c->extended) < c->remote_window)
411 FD_SET(c->efd, readset);
412 }
413}
414
415void
Damien Millerb38eff82000-04-01 11:09:21 +1000416channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
417{
418 if (buffer_len(&c->input) == 0) {
419 packet_start(SSH_MSG_CHANNEL_CLOSE);
420 packet_put_int(c->remote_id);
421 packet_send();
422 c->type = SSH_CHANNEL_CLOSED;
423 debug("Closing channel %d after input drain.", c->self);
424 }
425}
426
427void
428channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
429{
430 if (buffer_len(&c->output) == 0)
431 channel_free(c->self);
Damien Miller4af51302000-04-16 11:18:38 +1000432 else
Damien Millerb38eff82000-04-01 11:09:21 +1000433 FD_SET(c->sock, writeset);
434}
435
436/*
437 * This is a special state for X11 authentication spoofing. An opened X11
438 * connection (when authentication spoofing is being done) remains in this
439 * state until the first packet has been completely read. The authentication
440 * data in that packet is then substituted by the real data if it matches the
441 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000442 * XXX All this happens at the client side.
Damien Millerb38eff82000-04-01 11:09:21 +1000443 */
444int
445x11_open_helper(Channel *c)
446{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000447 u_char *ucp;
448 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000449
450 /* Check if the fixed size part of the packet is in buffer. */
451 if (buffer_len(&c->output) < 12)
452 return 0;
453
454 /* Parse the lengths of variable-length fields. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000455 ucp = (u_char *) buffer_ptr(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000456 if (ucp[0] == 0x42) { /* Byte order MSB first. */
457 proto_len = 256 * ucp[6] + ucp[7];
458 data_len = 256 * ucp[8] + ucp[9];
459 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
460 proto_len = ucp[6] + 256 * ucp[7];
461 data_len = ucp[8] + 256 * ucp[9];
462 } else {
463 debug("Initial X11 packet contains bad byte order byte: 0x%x",
464 ucp[0]);
465 return -1;
466 }
467
468 /* Check if the whole packet is in buffer. */
469 if (buffer_len(&c->output) <
470 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
471 return 0;
472
473 /* Check if authentication protocol matches. */
474 if (proto_len != strlen(x11_saved_proto) ||
475 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
476 debug("X11 connection uses different authentication protocol.");
477 return -1;
478 }
479 /* Check if authentication data matches our fake data. */
480 if (data_len != x11_fake_data_len ||
481 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
482 x11_fake_data, x11_fake_data_len) != 0) {
483 debug("X11 auth data does not match fake data.");
484 return -1;
485 }
486 /* Check fake data length */
487 if (x11_fake_data_len != x11_saved_data_len) {
488 error("X11 fake_data_len %d != saved_data_len %d",
489 x11_fake_data_len, x11_saved_data_len);
490 return -1;
491 }
492 /*
493 * Received authentication protocol and data match
494 * our fake data. Substitute the fake data with real
495 * data.
496 */
497 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
498 x11_saved_data, x11_saved_data_len);
499 return 1;
500}
501
502void
503channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
504{
505 int ret = x11_open_helper(c);
506 if (ret == 1) {
507 /* Start normal processing for the channel. */
508 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000509 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000510 } else if (ret == -1) {
511 /*
512 * We have received an X11 connection that has bad
513 * authentication information.
514 */
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000515 log("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000516 buffer_clear(&c->input);
517 buffer_clear(&c->output);
518 close(c->sock);
519 c->sock = -1;
520 c->type = SSH_CHANNEL_CLOSED;
521 packet_start(SSH_MSG_CHANNEL_CLOSE);
522 packet_put_int(c->remote_id);
523 packet_send();
524 }
525}
526
527void
Damien Millerbd483e72000-04-30 10:00:53 +1000528channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000529{
530 int ret = x11_open_helper(c);
531 if (ret == 1) {
532 c->type = SSH_CHANNEL_OPEN;
Damien Miller30c3d422000-05-09 11:02:59 +1000533 if (compat20)
534 channel_pre_open_20(c, readset, writeset);
535 else
536 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000537 } else if (ret == -1) {
538 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerbd483e72000-04-30 10:00:53 +1000539 chan_read_failed(c); /** force close? */
Damien Millerb38eff82000-04-01 11:09:21 +1000540 chan_write_failed(c);
541 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
542 }
543}
544
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000545#define SSH_SOCKS_HEAD 1+1+2+4
546
547void
548channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
549{
550 u_char *p, *host;
551 int len, i, done, have;
552 char username[256];
553 struct {
554 u_int8_t version;
555 u_int8_t command;
556 u_int16_t dest_port;
557 struct in_addr dest_ip;
558 } s4_req, s4_rsp;
559
560 have = buffer_len(&c->input);
561
562 debug("channel %d: pre_dynamic have: %d", c->self, have);
563 /*buffer_dump(&c->input);*/
564
565 /* Check if the fixed size part of the packet is in buffer. */
566 if (have < SSH_SOCKS_HEAD + 1) {
567 /* need more */
568 FD_SET(c->sock, readset);
569 return;
570 }
571 /* Check for end of username */
572 p = buffer_ptr(&c->input);
573 done = 0;
574 for (i = SSH_SOCKS_HEAD; i < have; i++) {
575 if (p[i] == '\0') {
576 done = 1;
577 break;
578 }
579 }
580 if (!done) {
581 /* need more */
582 FD_SET(c->sock, readset);
583 return;
584 }
585 buffer_get(&c->input, (char *)&s4_req.version, 1);
586 buffer_get(&c->input, (char *)&s4_req.command, 1);
587 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
588 buffer_get(&c->input, (char *)&s4_req.dest_ip, 4);
589 p = buffer_ptr(&c->input);
590 len = strlen(p);
591 have = buffer_len(&c->input);
592 debug2("channel %d: pre_dynamic user: %s/%d", c->self, p, len);
593 if (len > have)
594 fatal("channel %d: pre_dynamic: len %d > have %d",
595 c->self, len, have);
596 strlcpy(username, p, sizeof(username));
597 buffer_consume(&c->input, len);
598 buffer_consume(&c->input, 1); /* trailing '\0' */
599
600 host = inet_ntoa(s4_req.dest_ip);
601 strlcpy(c->path, host, sizeof(c->path));
602 c->host_port = ntohs(s4_req.dest_port);
603
604 debug("channel %d: dynamic request received: "
605 "socks%x://%s@%s:%u/command?%u",
606 c->self, s4_req.version, username, host, c->host_port,
607 s4_req.command);
608
609 if ((s4_req.version != 4) || (s4_req.command != 1)) {
610 debug("channel %d: cannot handle: socks VN %d CN %d",
611 c->self, s4_req.version, s4_req.command);
612 channel_free(c->self);
613 return;
614 }
615
616 s4_rsp.version = 0; /* VN: version of reply code */
617 s4_rsp.command = 90; /* CD: request granted */
618 s4_rsp.dest_port = 0; /* ignored */
619 s4_rsp.dest_ip.s_addr = INADDR_ANY; /* ignored */
620 buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
621
622 /* switch to next state */
623 c->type = SSH_CHANNEL_OPENING;
624 port_open_helper(c, "direct-tcpip");
625}
626
627
Damien Millerb38eff82000-04-01 11:09:21 +1000628/* This is our fake X11 server socket. */
629void
630channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
631{
632 struct sockaddr addr;
633 int newsock, newch;
634 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +1100635 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +1000636 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +1000637
638 if (FD_ISSET(c->sock, readset)) {
639 debug("X11 connection requested.");
640 addrlen = sizeof(addr);
641 newsock = accept(c->sock, &addr, &addrlen);
642 if (newsock < 0) {
643 error("accept: %.100s", strerror(errno));
644 return;
645 }
Damien Millerd83ff352001-01-30 09:19:34 +1100646 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +1000647 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +1000648 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +1100649 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +1000650
651 newch = channel_new("x11",
652 SSH_CHANNEL_OPENING, newsock, newsock, -1,
653 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100654 0, xstrdup(buf), 1);
Damien Millerbd483e72000-04-30 10:00:53 +1000655 if (compat20) {
656 packet_start(SSH2_MSG_CHANNEL_OPEN);
657 packet_put_cstring("x11");
658 packet_put_int(newch);
659 packet_put_int(c->local_window_max);
660 packet_put_int(c->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +1100661 /* originator ipaddr and port */
662 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +1000663 if (datafellows & SSH_BUG_X11FWD) {
664 debug("ssh2 x11 bug compat mode");
665 } else {
666 packet_put_int(remote_port);
667 }
Damien Millerbd483e72000-04-30 10:00:53 +1000668 packet_send();
669 } else {
670 packet_start(SSH_SMSG_X11_OPEN);
671 packet_put_int(newch);
672 if (have_hostname_in_open)
673 packet_put_string(buf, strlen(buf));
674 packet_send();
675 }
Damien Millerd83ff352001-01-30 09:19:34 +1100676 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +1000677 }
678}
679
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000680void
681port_open_helper(Channel *c, char *rtype)
682{
683 int direct;
684 char buf[1024];
685 char *remote_ipaddr = get_peer_ipaddr(c->sock);
686 u_short remote_port = get_peer_port(c->sock);
687
688 direct = (strcmp(rtype, "direct-tcpip") == 0);
689
690 snprintf(buf, sizeof buf,
691 "%s: listening port %d for %.100s port %d, "
692 "connect from %.200s port %d",
693 rtype, c->listening_port, c->path, c->host_port,
694 remote_ipaddr, remote_port);
695
696 xfree(c->remote_name);
697 c->remote_name = xstrdup(buf);
698
699 if (compat20) {
700 packet_start(SSH2_MSG_CHANNEL_OPEN);
701 packet_put_cstring(rtype);
702 packet_put_int(c->self);
703 packet_put_int(c->local_window_max);
704 packet_put_int(c->local_maxpacket);
705 if (direct) {
706 /* target host, port */
707 packet_put_cstring(c->path);
708 packet_put_int(c->host_port);
709 } else {
710 /* listen address, port */
711 packet_put_cstring(c->path);
712 packet_put_int(c->listening_port);
713 }
714 /* originator host and port */
715 packet_put_cstring(remote_ipaddr);
716 packet_put_int(remote_port);
717 packet_send();
718 } else {
719 packet_start(SSH_MSG_PORT_OPEN);
720 packet_put_int(c->self);
721 packet_put_cstring(c->path);
722 packet_put_int(c->host_port);
723 if (have_hostname_in_open)
724 packet_put_cstring(c->remote_name);
725 packet_send();
726 }
727 xfree(remote_ipaddr);
728}
729
Damien Millerb38eff82000-04-01 11:09:21 +1000730/*
731 * This socket is listening for connections to a forwarded TCP/IP port.
732 */
733void
734channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
735{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000736 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +1000737 struct sockaddr addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000738 int newsock, newch, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +1000739 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000740 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100741
Damien Millerb38eff82000-04-01 11:09:21 +1000742 if (FD_ISSET(c->sock, readset)) {
743 debug("Connection to port %d forwarding "
744 "to %.100s port %d requested.",
745 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000746
747 rtype = (c->type == SSH_CHANNEL_RPORT_LISTENER) ?
748 "forwarded-tcpip" : "direct-tcpip";
749 nextstate = (c->host_port == 0) ? SSH_CHANNEL_DYNAMIC :
750 SSH_CHANNEL_OPENING;
751
Damien Millerb38eff82000-04-01 11:09:21 +1000752 addrlen = sizeof(addr);
753 newsock = accept(c->sock, &addr, &addrlen);
754 if (newsock < 0) {
755 error("accept: %.100s", strerror(errno));
756 return;
757 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100758 newch = channel_new(rtype,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000759 nextstate, newsock, newsock, -1,
Damien Millerb38eff82000-04-01 11:09:21 +1000760 c->local_window_max, c->local_maxpacket,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000761 0, xstrdup(rtype), 1);
762
763 nc = channel_lookup(newch);
764 if (nc == NULL) {
765 error("xxx: no new channel:");
766 return;
Damien Millerb38eff82000-04-01 11:09:21 +1000767 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000768 nc->listening_port = c->listening_port;
769 nc->host_port = c->host_port;
770 strlcpy(nc->path, c->path, sizeof(nc->path));
771
772 if (nextstate != SSH_CHANNEL_DYNAMIC)
773 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +1000774 }
775}
776
777/*
778 * This is the authentication agent socket listening for connections from
779 * clients.
780 */
781void
782channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
783{
784 struct sockaddr addr;
785 int newsock, newch;
786 socklen_t addrlen;
787
788 if (FD_ISSET(c->sock, readset)) {
789 addrlen = sizeof(addr);
790 newsock = accept(c->sock, &addr, &addrlen);
791 if (newsock < 0) {
792 error("accept from auth socket: %.100s", strerror(errno));
793 return;
794 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100795 newch = channel_new("accepted auth socket",
796 SSH_CHANNEL_OPENING, newsock, newsock, -1,
797 c->local_window_max, c->local_maxpacket,
798 0, xstrdup("accepted auth socket"), 1);
799 if (compat20) {
800 packet_start(SSH2_MSG_CHANNEL_OPEN);
801 packet_put_cstring("auth-agent@openssh.com");
802 packet_put_int(newch);
803 packet_put_int(c->local_window_max);
804 packet_put_int(c->local_maxpacket);
805 } else {
806 packet_start(SSH_SMSG_AGENT_OPEN);
807 packet_put_int(newch);
808 }
Damien Millerb38eff82000-04-01 11:09:21 +1000809 packet_send();
810 }
811}
812
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000813void
814channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
815{
816 if (FD_ISSET(c->sock, writeset)) {
817 int err = 0;
818 int sz = sizeof(err);
819 c->type = SSH_CHANNEL_OPEN;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000820 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err, &sz) < 0) {
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000821 debug("getsockopt SO_ERROR failed");
822 } else {
823 if (err == 0) {
824 debug("channel %d: connected)", c->self);
825 } else {
826 debug("channel %d: not connected: %s",
827 c->self, strerror(err));
828 chan_read_failed(c);
829 chan_write_failed(c);
830 }
831 }
832 }
833}
834
Damien Millerb38eff82000-04-01 11:09:21 +1000835int
836channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
837{
838 char buf[16*1024];
839 int len;
840
841 if (c->rfd != -1 &&
842 FD_ISSET(c->rfd, readset)) {
843 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +1000844 if (len < 0 && (errno == EINTR || errno == EAGAIN))
845 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000846 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +1000847 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +1000848 c->self, c->rfd, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000849 if (c->type == SSH_CHANNEL_DYNAMIC) {
850 /*
851 * we are not yet connected to a remote peer,
852 * so the connection-close protocol won't work
853 */
854 debug("channel %d: dynamic: closed", c->self);
855 channel_free(c->self);
856 return -1;
857 }
Damien Millerb38eff82000-04-01 11:09:21 +1000858 if (compat13) {
859 buffer_consume(&c->output, buffer_len(&c->output));
860 c->type = SSH_CHANNEL_INPUT_DRAINING;
861 debug("Channel %d status set to input draining.", c->self);
862 } else {
863 chan_read_failed(c);
864 }
865 return -1;
866 }
Damien Millerad833b32000-08-23 10:46:23 +1000867 if(c->input_filter != NULL) {
868 if (c->input_filter(c, buf, len) == -1) {
869 debug("filter stops channel %d", c->self);
870 chan_read_failed(c);
871 }
872 } else {
873 buffer_append(&c->input, buf, len);
874 }
Damien Millerb38eff82000-04-01 11:09:21 +1000875 }
876 return 1;
877}
878int
879channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
880{
Ben Lindstrome229b252001-03-05 06:28:06 +0000881 struct termios tio;
Damien Millerb38eff82000-04-01 11:09:21 +1000882 int len;
883
884 /* Send buffered output data to the socket. */
885 if (c->wfd != -1 &&
886 FD_ISSET(c->wfd, writeset) &&
887 buffer_len(&c->output) > 0) {
888 len = write(c->wfd, buffer_ptr(&c->output),
Damien Miller6f83b8e2000-05-02 09:23:45 +1000889 buffer_len(&c->output));
890 if (len < 0 && (errno == EINTR || errno == EAGAIN))
891 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000892 if (len <= 0) {
893 if (compat13) {
894 buffer_consume(&c->output, buffer_len(&c->output));
895 debug("Channel %d status set to input draining.", c->self);
896 c->type = SSH_CHANNEL_INPUT_DRAINING;
897 } else {
898 chan_write_failed(c);
899 }
900 return -1;
901 }
Damien Miller79438cc2001-02-16 12:34:57 +1100902 if (compat20 && c->isatty) {
Damien Miller79438cc2001-02-16 12:34:57 +1100903 if (tcgetattr(c->wfd, &tio) == 0 &&
904 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
905 /*
906 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +0000907 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +0000908 * size of a SSH2_MSG_CHANNEL_DATA message
909 * (4 byte channel id + data)
Damien Miller79438cc2001-02-16 12:34:57 +1100910 */
Ben Lindstrome229b252001-03-05 06:28:06 +0000911 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +1100912 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +1100913 }
914 }
Damien Millerb38eff82000-04-01 11:09:21 +1000915 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +1000916 if (compat20 && len > 0) {
917 c->local_consumed += len;
918 }
919 }
920 return 1;
921}
922int
923channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
924{
925 char buf[16*1024];
926 int len;
927
Damien Miller1383bd82000-04-06 12:32:37 +1000928/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +1000929 if (c->efd != -1) {
930 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
931 FD_ISSET(c->efd, writeset) &&
932 buffer_len(&c->extended) > 0) {
933 len = write(c->efd, buffer_ptr(&c->extended),
934 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +1100935 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000936 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000937 if (len < 0 && (errno == EINTR || errno == EAGAIN))
938 return 1;
939 if (len <= 0) {
940 debug2("channel %d: closing write-efd %d",
941 c->self, c->efd);
942 close(c->efd);
943 c->efd = -1;
944 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000945 buffer_consume(&c->extended, len);
946 c->local_consumed += len;
947 }
948 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
949 FD_ISSET(c->efd, readset)) {
950 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +1100951 debug2("channel %d: read %d from efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000952 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000953 if (len < 0 && (errno == EINTR || errno == EAGAIN))
954 return 1;
955 if (len <= 0) {
956 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +1000957 c->self, c->efd);
958 close(c->efd);
959 c->efd = -1;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000960 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000961 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000962 }
Damien Miller33b13562000-04-04 14:38:59 +1000963 }
964 }
965 return 1;
966}
967int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000968channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000969{
Damien Millerefb4afe2000-04-12 18:45:05 +1000970 if (!(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +1000971 c->local_window < c->local_window_max/2 &&
972 c->local_consumed > 0) {
973 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
974 packet_put_int(c->remote_id);
975 packet_put_int(c->local_consumed);
976 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +1100977 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +1000978 c->self, c->local_window,
979 c->local_consumed);
980 c->local_window += c->local_consumed;
981 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +1000982 }
983 return 1;
984}
985
986void
987channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
988{
989 channel_handle_rfd(c, readset, writeset);
990 channel_handle_wfd(c, readset, writeset);
991}
992
993void
Damien Miller33b13562000-04-04 14:38:59 +1000994channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
995{
996 channel_handle_rfd(c, readset, writeset);
997 channel_handle_wfd(c, readset, writeset);
998 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000999
1000 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001001}
1002
1003void
Damien Millerb38eff82000-04-01 11:09:21 +10001004channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1005{
1006 int len;
1007 /* Send buffered output data to the socket. */
1008 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1009 len = write(c->sock, buffer_ptr(&c->output),
1010 buffer_len(&c->output));
1011 if (len <= 0)
1012 buffer_consume(&c->output, buffer_len(&c->output));
1013 else
1014 buffer_consume(&c->output, len);
1015 }
1016}
1017
1018void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001019channel_post_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
1020{
1021 channel_handle_rfd(c, readset, writeset);
1022}
1023
1024void
Damien Miller33b13562000-04-04 14:38:59 +10001025channel_handler_init_20(void)
1026{
1027 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +10001028 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001029 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001030 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001031 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001032 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001033 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001034 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001035
1036 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
1037 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001038 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001039 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001040 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001041 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001042 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001043}
1044
1045void
Damien Millerb38eff82000-04-01 11:09:21 +10001046channel_handler_init_13(void)
1047{
1048 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1049 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1050 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1051 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1052 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1053 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1054 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001055 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001056 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001057
1058 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
1059 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1060 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1061 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1062 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001063 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001064 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001065}
1066
1067void
1068channel_handler_init_15(void)
1069{
1070 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +10001071 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001072 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1073 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1074 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001075 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001076 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001077
1078 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1079 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1080 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1081 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001082 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001083 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001084}
1085
1086void
1087channel_handler_init(void)
1088{
1089 int i;
1090 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1091 channel_pre[i] = NULL;
1092 channel_post[i] = NULL;
1093 }
Damien Miller33b13562000-04-04 14:38:59 +10001094 if (compat20)
1095 channel_handler_init_20();
1096 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001097 channel_handler_init_13();
1098 else
1099 channel_handler_init_15();
1100}
1101
Damien Miller4af51302000-04-16 11:18:38 +10001102void
Damien Millerb38eff82000-04-01 11:09:21 +10001103channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1104{
1105 static int did_init = 0;
1106 int i;
1107 Channel *c;
1108
1109 if (!did_init) {
1110 channel_handler_init();
1111 did_init = 1;
1112 }
1113 for (i = 0; i < channels_alloc; i++) {
1114 c = &channels[i];
1115 if (c->type == SSH_CHANNEL_FREE)
1116 continue;
1117 if (ftab[c->type] == NULL)
1118 continue;
1119 (*ftab[c->type])(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001120 if (chan_is_dead(c)) {
1121 /*
1122 * we have to remove the fd's from the select mask
1123 * before the channels are free'd and the fd's are
1124 * closed
1125 */
1126 if (c->wfd != -1)
1127 FD_CLR(c->wfd, writeset);
1128 if (c->rfd != -1)
1129 FD_CLR(c->rfd, readset);
1130 if (c->efd != -1) {
1131 if (c->extended_usage == CHAN_EXTENDED_READ)
1132 FD_CLR(c->efd, readset);
1133 if (c->extended_usage == CHAN_EXTENDED_WRITE)
1134 FD_CLR(c->efd, writeset);
1135 }
1136 channel_free(c->self);
1137 }
Damien Millerb38eff82000-04-01 11:09:21 +10001138 }
1139}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001140
Damien Miller4af51302000-04-16 11:18:38 +10001141void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001142channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1143 int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001144{
Damien Miller5e953212001-01-30 09:14:00 +11001145 int n;
1146 u_int sz;
1147
1148 n = MAX(*maxfdp, channel_max_fd);
1149
1150 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1151 if (*readsetp == NULL || n > *maxfdp) {
1152 if (*readsetp)
1153 xfree(*readsetp);
1154 if (*writesetp)
1155 xfree(*writesetp);
1156 *readsetp = xmalloc(sz);
1157 *writesetp = xmalloc(sz);
1158 *maxfdp = n;
1159 }
1160 memset(*readsetp, 0, sz);
1161 memset(*writesetp, 0, sz);
1162
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001163 if (!rekeying)
1164 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001165}
1166
Damien Miller4af51302000-04-16 11:18:38 +10001167void
Damien Miller95def091999-11-25 00:26:21 +11001168channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001169{
Damien Millerb38eff82000-04-01 11:09:21 +10001170 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001171}
1172
Damien Miller5e953212001-01-30 09:14:00 +11001173/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001174
Damien Miller4af51302000-04-16 11:18:38 +10001175void
Damien Miller95def091999-11-25 00:26:21 +11001176channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001177{
Damien Miller95def091999-11-25 00:26:21 +11001178 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001179 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001180
Damien Miller95def091999-11-25 00:26:21 +11001181 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001182 c = &channels[i];
Damien Miller34132e52000-01-14 15:45:46 +11001183
Damien Miller5428f641999-11-25 11:54:57 +11001184 /* We are only interested in channels that can have buffered incoming data. */
Damien Miller34132e52000-01-14 15:45:46 +11001185 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001186 if (c->type != SSH_CHANNEL_OPEN &&
1187 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001188 continue;
1189 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001190 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001191 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001192 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001193 if (compat20 &&
1194 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001195 /* XXX is this true? */
Ben Lindstromb1131e92001-03-05 07:27:13 +00001196 debug2("channel %d: no data after CLOSE", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001197 continue;
1198 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001199
Damien Miller95def091999-11-25 00:26:21 +11001200 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001201 if ((c->istate == CHAN_INPUT_OPEN ||
1202 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1203 (len = buffer_len(&c->input)) > 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001204 /* Send some data for the other side over the secure connection. */
Damien Miller33b13562000-04-04 14:38:59 +10001205 if (compat20) {
1206 if (len > c->remote_window)
1207 len = c->remote_window;
1208 if (len > c->remote_maxpacket)
1209 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001210 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001211 if (packet_is_interactive()) {
1212 if (len > 1024)
1213 len = 512;
1214 } else {
1215 /* Keep the packets at reasonable size. */
1216 if (len > packet_get_maxsize()/2)
1217 len = packet_get_maxsize()/2;
1218 }
Damien Miller95def091999-11-25 00:26:21 +11001219 }
Damien Millerb38eff82000-04-01 11:09:21 +10001220 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001221 packet_start(compat20 ?
1222 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001223 packet_put_int(c->remote_id);
1224 packet_put_string(buffer_ptr(&c->input), len);
1225 packet_send();
1226 buffer_consume(&c->input, len);
1227 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001228 }
1229 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001230 if (compat13)
1231 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001232 /*
1233 * input-buffer is empty and read-socket shutdown:
1234 * tell peer, that we will not send more data: send IEOF
1235 */
Damien Millerb38eff82000-04-01 11:09:21 +10001236 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001237 }
Damien Miller33b13562000-04-04 14:38:59 +10001238 /* Send extended data, i.e. stderr */
1239 if (compat20 &&
1240 c->remote_window > 0 &&
1241 (len = buffer_len(&c->extended)) > 0 &&
1242 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001243 debug2("channel %d: rwin %d elen %d euse %d",
1244 c->self, c->remote_window, buffer_len(&c->extended),
1245 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001246 if (len > c->remote_window)
1247 len = c->remote_window;
1248 if (len > c->remote_maxpacket)
1249 len = c->remote_maxpacket;
1250 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1251 packet_put_int(c->remote_id);
1252 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1253 packet_put_string(buffer_ptr(&c->extended), len);
1254 packet_send();
1255 buffer_consume(&c->extended, len);
1256 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001257 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001258 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001259 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001260}
1261
Damien Miller5428f641999-11-25 11:54:57 +11001262/*
1263 * This is called when a packet of type CHANNEL_DATA has just been received.
1264 * The message type has already been consumed, but channel number and data is
1265 * still there.
1266 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001267
Damien Miller4af51302000-04-16 11:18:38 +10001268void
Damien Miller62cee002000-09-23 17:15:56 +11001269channel_input_data(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001270{
Damien Miller34132e52000-01-14 15:45:46 +11001271 int id;
Damien Miller95def091999-11-25 00:26:21 +11001272 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001273 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001274 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001275
Damien Miller95def091999-11-25 00:26:21 +11001276 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001277 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001278 c = channel_lookup(id);
1279 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001280 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001281
Damien Miller95def091999-11-25 00:26:21 +11001282 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001283 if (c->type != SSH_CHANNEL_OPEN &&
1284 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001285 return;
1286
1287 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001288 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001289 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001290
Damien Miller95def091999-11-25 00:26:21 +11001291 /* Get the data. */
1292 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001293 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001294
Damien Miller33b13562000-04-04 14:38:59 +10001295 if (compat20){
1296 if (data_len > c->local_maxpacket) {
1297 log("channel %d: rcvd big packet %d, maxpack %d",
1298 c->self, data_len, c->local_maxpacket);
1299 }
1300 if (data_len > c->local_window) {
1301 log("channel %d: rcvd too much data %d, win %d",
1302 c->self, data_len, c->local_window);
1303 xfree(data);
1304 return;
1305 }
1306 c->local_window -= data_len;
1307 }else{
1308 packet_integrity_check(plen, 4 + 4 + data_len, type);
1309 }
Damien Millerb38eff82000-04-01 11:09:21 +10001310 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001311 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001312}
Damien Miller4af51302000-04-16 11:18:38 +10001313void
Damien Miller62cee002000-09-23 17:15:56 +11001314channel_input_extended_data(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001315{
1316 int id;
1317 int tcode;
1318 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001319 u_int data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001320 Channel *c;
1321
1322 /* Get the channel number and verify it. */
1323 id = packet_get_int();
1324 c = channel_lookup(id);
1325
1326 if (c == NULL)
1327 packet_disconnect("Received extended_data for bad channel %d.", id);
1328 if (c->type != SSH_CHANNEL_OPEN) {
1329 log("channel %d: ext data for non open", id);
1330 return;
1331 }
1332 tcode = packet_get_int();
1333 if (c->efd == -1 ||
1334 c->extended_usage != CHAN_EXTENDED_WRITE ||
1335 tcode != SSH2_EXTENDED_DATA_STDERR) {
1336 log("channel %d: bad ext data", c->self);
1337 return;
1338 }
1339 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001340 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001341 if (data_len > c->local_window) {
1342 log("channel %d: rcvd too much extended_data %d, win %d",
1343 c->self, data_len, c->local_window);
1344 xfree(data);
1345 return;
1346 }
Damien Millerd3444942000-09-30 14:20:03 +11001347 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001348 c->local_window -= data_len;
1349 buffer_append(&c->extended, data, data_len);
1350 xfree(data);
1351}
1352
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001353
Damien Miller5428f641999-11-25 11:54:57 +11001354/*
1355 * Returns true if no channel has too much buffered data, and false if one or
1356 * more channel is overfull.
1357 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001358
Damien Miller4af51302000-04-16 11:18:38 +10001359int
Damien Miller95def091999-11-25 00:26:21 +11001360channel_not_very_much_buffered_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001361{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001362 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001363 Channel *c;
Damien Miller95def091999-11-25 00:26:21 +11001364
1365 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001366 c = &channels[i];
1367 if (c->type == SSH_CHANNEL_OPEN) {
Damien Miller33b13562000-04-04 14:38:59 +10001368 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
Damien Millerb38eff82000-04-01 11:09:21 +10001369 debug("channel %d: big input buffer %d",
1370 c->self, buffer_len(&c->input));
Damien Miller95def091999-11-25 00:26:21 +11001371 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001372 }
1373 if (buffer_len(&c->output) > packet_get_maxsize()) {
1374 debug("channel %d: big output buffer %d",
1375 c->self, buffer_len(&c->output));
Damien Miller95def091999-11-25 00:26:21 +11001376 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001377 }
Damien Miller95def091999-11-25 00:26:21 +11001378 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001379 }
Damien Miller95def091999-11-25 00:26:21 +11001380 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001381}
1382
Damien Miller4af51302000-04-16 11:18:38 +10001383void
Damien Miller62cee002000-09-23 17:15:56 +11001384channel_input_ieof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001385{
1386 int id;
1387 Channel *c;
1388
1389 packet_integrity_check(plen, 4, type);
1390
1391 id = packet_get_int();
1392 c = channel_lookup(id);
1393 if (c == NULL)
1394 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1395 chan_rcvd_ieof(c);
1396}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001397
Damien Miller4af51302000-04-16 11:18:38 +10001398void
Damien Miller62cee002000-09-23 17:15:56 +11001399channel_input_close(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400{
Damien Millerb38eff82000-04-01 11:09:21 +10001401 int id;
1402 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001403
Damien Millerb38eff82000-04-01 11:09:21 +10001404 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001405
Damien Millerb38eff82000-04-01 11:09:21 +10001406 id = packet_get_int();
1407 c = channel_lookup(id);
1408 if (c == NULL)
1409 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001410
1411 /*
1412 * Send a confirmation that we have closed the channel and no more
1413 * data is coming for it.
1414 */
Damien Miller95def091999-11-25 00:26:21 +11001415 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001416 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001417 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001418
Damien Miller5428f641999-11-25 11:54:57 +11001419 /*
1420 * If the channel is in closed state, we have sent a close request,
1421 * and the other side will eventually respond with a confirmation.
1422 * Thus, we cannot free the channel here, because then there would be
1423 * no-one to receive the confirmation. The channel gets freed when
1424 * the confirmation arrives.
1425 */
Damien Millerb38eff82000-04-01 11:09:21 +10001426 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001427 /*
1428 * Not a closed channel - mark it as draining, which will
1429 * cause it to be freed later.
1430 */
Damien Millerb38eff82000-04-01 11:09:21 +10001431 buffer_consume(&c->input, buffer_len(&c->input));
1432 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001433 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001434}
1435
Damien Millerb38eff82000-04-01 11:09:21 +10001436/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001437void
Damien Miller62cee002000-09-23 17:15:56 +11001438channel_input_oclose(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001439{
Damien Millerb38eff82000-04-01 11:09:21 +10001440 int id = packet_get_int();
1441 Channel *c = channel_lookup(id);
1442 packet_integrity_check(plen, 4, type);
1443 if (c == NULL)
1444 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1445 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001446}
1447
Damien Miller4af51302000-04-16 11:18:38 +10001448void
Damien Miller62cee002000-09-23 17:15:56 +11001449channel_input_close_confirmation(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001450{
1451 int id = packet_get_int();
1452 Channel *c = channel_lookup(id);
1453
Damien Miller4af51302000-04-16 11:18:38 +10001454 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001455 if (c == NULL)
1456 packet_disconnect("Received close confirmation for "
1457 "out-of-range channel %d.", id);
1458 if (c->type != SSH_CHANNEL_CLOSED)
1459 packet_disconnect("Received close confirmation for "
1460 "non-closed channel %d (type %d).", id, c->type);
1461 channel_free(c->self);
1462}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001463
Damien Miller4af51302000-04-16 11:18:38 +10001464void
Damien Miller62cee002000-09-23 17:15:56 +11001465channel_input_open_confirmation(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001466{
Damien Millerb38eff82000-04-01 11:09:21 +10001467 int id, remote_id;
1468 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001469
Damien Miller33b13562000-04-04 14:38:59 +10001470 if (!compat20)
1471 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472
Damien Millerb38eff82000-04-01 11:09:21 +10001473 id = packet_get_int();
1474 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001475
Damien Millerb38eff82000-04-01 11:09:21 +10001476 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1477 packet_disconnect("Received open confirmation for "
1478 "non-opening channel %d.", id);
1479 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001480 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001481 c->remote_id = remote_id;
1482 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001483
1484 if (compat20) {
1485 c->remote_window = packet_get_int();
1486 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001487 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001488 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001489 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001490 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001491 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001492 }
1493 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1494 c->remote_window, c->remote_maxpacket);
1495 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001496}
1497
Damien Miller4af51302000-04-16 11:18:38 +10001498void
Damien Miller62cee002000-09-23 17:15:56 +11001499channel_input_open_failure(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001500{
Kevin Steves12057502001-02-05 14:54:34 +00001501 int id, reason;
1502 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001503 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001504
Damien Miller33b13562000-04-04 14:38:59 +10001505 if (!compat20)
1506 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001507
1508 id = packet_get_int();
1509 c = channel_lookup(id);
1510
1511 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1512 packet_disconnect("Received open failure for "
1513 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001514 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00001515 reason = packet_get_int();
1516 if (packet_remaining() > 0) {
1517 msg = packet_get_string(NULL);
1518 lang = packet_get_string(NULL);
1519 }
Damien Miller4af51302000-04-16 11:18:38 +10001520 packet_done();
Kevin Steves12057502001-02-05 14:54:34 +00001521 log("channel_open_failure: %d: reason %d %s", id,
1522 reason, msg ? msg : "<no additional info>");
1523 if (msg != NULL)
1524 xfree(msg);
1525 if (lang != NULL)
1526 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001527 }
Damien Miller95def091999-11-25 00:26:21 +11001528 /* Free the channel. This will also close the socket. */
Damien Millerb38eff82000-04-01 11:09:21 +10001529 channel_free(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001530}
1531
Damien Miller33b13562000-04-04 14:38:59 +10001532void
Damien Miller62cee002000-09-23 17:15:56 +11001533channel_input_channel_request(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001534{
1535 int id;
1536 Channel *c;
1537
1538 id = packet_get_int();
1539 c = channel_lookup(id);
1540
1541 if (c == NULL ||
1542 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1543 packet_disconnect("Received request for "
1544 "non-open channel %d.", id);
1545 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001546 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001547 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001548 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001549 } else {
1550 char *service = packet_get_string(NULL);
Ben Lindstromcc74df72001-03-05 06:20:14 +00001551 debug("channel %d: rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001552 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001553 xfree(service);
1554 }
1555}
1556
Damien Miller4af51302000-04-16 11:18:38 +10001557void
Damien Miller62cee002000-09-23 17:15:56 +11001558channel_input_window_adjust(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001559{
1560 Channel *c;
1561 int id, adjust;
1562
1563 if (!compat20)
1564 return;
1565
1566 /* Get the channel number and verify it. */
1567 id = packet_get_int();
1568 c = channel_lookup(id);
1569
1570 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1571 log("Received window adjust for "
1572 "non-open channel %d.", id);
1573 return;
1574 }
1575 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001576 packet_done();
Damien Millerd3444942000-09-30 14:20:03 +11001577 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10001578 c->remote_window += adjust;
1579}
1580
Damien Miller5428f641999-11-25 11:54:57 +11001581/*
1582 * Stops listening for channels, and removes any unix domain sockets that we
1583 * might have.
1584 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001585
Damien Miller4af51302000-04-16 11:18:38 +10001586void
Damien Miller95def091999-11-25 00:26:21 +11001587channel_stop_listening()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001588{
Damien Miller95def091999-11-25 00:26:21 +11001589 int i;
1590 for (i = 0; i < channels_alloc; i++) {
1591 switch (channels[i].type) {
1592 case SSH_CHANNEL_AUTH_SOCKET:
1593 close(channels[i].sock);
Damien Miller78315eb2000-09-29 23:01:36 +11001594 unlink(channels[i].path);
Damien Miller95def091999-11-25 00:26:21 +11001595 channel_free(i);
1596 break;
1597 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001598 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001599 case SSH_CHANNEL_X11_LISTENER:
1600 close(channels[i].sock);
1601 channel_free(i);
1602 break;
1603 default:
1604 break;
1605 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001606 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001607}
1608
Damien Miller5428f641999-11-25 11:54:57 +11001609/*
Damien Miller6f83b8e2000-05-02 09:23:45 +10001610 * Closes the sockets/fds of all channels. This is used to close extra file
Damien Miller5428f641999-11-25 11:54:57 +11001611 * descriptors after a fork.
1612 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001613
Damien Miller4af51302000-04-16 11:18:38 +10001614void
Damien Miller95def091999-11-25 00:26:21 +11001615channel_close_all()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001616{
Damien Miller95def091999-11-25 00:26:21 +11001617 int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +10001618 for (i = 0; i < channels_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001619 if (channels[i].type != SSH_CHANNEL_FREE)
Damien Miller6f83b8e2000-05-02 09:23:45 +10001620 channel_close_fds(&channels[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001621}
1622
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001623/* Returns true if any channel is still open. */
1624
Damien Miller4af51302000-04-16 11:18:38 +10001625int
Damien Miller95def091999-11-25 00:26:21 +11001626channel_still_open()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001627{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001628 u_int i;
Damien Miller95def091999-11-25 00:26:21 +11001629 for (i = 0; i < channels_alloc; i++)
1630 switch (channels[i].type) {
1631 case SSH_CHANNEL_FREE:
1632 case SSH_CHANNEL_X11_LISTENER:
1633 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001634 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001635 case SSH_CHANNEL_CLOSED:
1636 case SSH_CHANNEL_AUTH_SOCKET:
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001637 case SSH_CHANNEL_DYNAMIC:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001638 case SSH_CHANNEL_CONNECTING: /* XXX ??? */
Damien Miller95def091999-11-25 00:26:21 +11001639 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001640 case SSH_CHANNEL_LARVAL:
1641 if (!compat20)
1642 fatal("cannot happen: SSH_CHANNEL_LARVAL");
1643 continue;
Damien Miller95def091999-11-25 00:26:21 +11001644 case SSH_CHANNEL_OPENING:
1645 case SSH_CHANNEL_OPEN:
1646 case SSH_CHANNEL_X11_OPEN:
1647 return 1;
1648 case SSH_CHANNEL_INPUT_DRAINING:
1649 case SSH_CHANNEL_OUTPUT_DRAINING:
1650 if (!compat13)
1651 fatal("cannot happen: OUT_DRAIN");
1652 return 1;
1653 default:
1654 fatal("channel_still_open: bad channel type %d", channels[i].type);
1655 /* NOTREACHED */
1656 }
1657 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001658}
1659
Damien Miller5428f641999-11-25 11:54:57 +11001660/*
1661 * Returns a message describing the currently open forwarded connections,
1662 * suitable for sending to the client. The message contains crlf pairs for
1663 * newlines.
1664 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001665
Damien Miller95def091999-11-25 00:26:21 +11001666char *
1667channel_open_message()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001668{
Damien Miller95def091999-11-25 00:26:21 +11001669 Buffer buffer;
1670 int i;
1671 char buf[512], *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001672
Damien Miller95def091999-11-25 00:26:21 +11001673 buffer_init(&buffer);
1674 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001675 buffer_append(&buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001676 for (i = 0; i < channels_alloc; i++) {
1677 Channel *c = &channels[i];
1678 switch (c->type) {
1679 case SSH_CHANNEL_FREE:
1680 case SSH_CHANNEL_X11_LISTENER:
1681 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001682 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001683 case SSH_CHANNEL_CLOSED:
1684 case SSH_CHANNEL_AUTH_SOCKET:
1685 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001686 case SSH_CHANNEL_LARVAL:
Damien Miller95def091999-11-25 00:26:21 +11001687 case SSH_CHANNEL_OPENING:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001688 case SSH_CHANNEL_CONNECTING:
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001689 case SSH_CHANNEL_DYNAMIC:
Damien Miller95def091999-11-25 00:26:21 +11001690 case SSH_CHANNEL_OPEN:
1691 case SSH_CHANNEL_X11_OPEN:
1692 case SSH_CHANNEL_INPUT_DRAINING:
1693 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Millerb38eff82000-04-01 11:09:21 +10001694 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 +11001695 c->self, c->remote_name,
1696 c->type, c->remote_id,
1697 c->istate, buffer_len(&c->input),
Damien Millerb38eff82000-04-01 11:09:21 +10001698 c->ostate, buffer_len(&c->output),
1699 c->rfd, c->wfd);
Damien Miller95def091999-11-25 00:26:21 +11001700 buffer_append(&buffer, buf, strlen(buf));
1701 continue;
1702 default:
Damien Millerb38eff82000-04-01 11:09:21 +10001703 fatal("channel_open_message: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001704 /* NOTREACHED */
1705 }
1706 }
1707 buffer_append(&buffer, "\0", 1);
1708 cp = xstrdup(buffer_ptr(&buffer));
1709 buffer_free(&buffer);
1710 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001711}
1712
Damien Miller5428f641999-11-25 11:54:57 +11001713/*
1714 * Initiate forwarding of connections to local port "port" through the secure
1715 * channel to host:port from remote side.
1716 */
Kevin Steves12057502001-02-05 14:54:34 +00001717int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001718channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
1719 u_short port_to_connect, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001720{
Kevin Steves12057502001-02-05 14:54:34 +00001721 return channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +11001722 NULL, listen_port,
1723 host_to_connect, port_to_connect,
1724 gateway_ports, /*remote_fwd*/ 0);
1725}
1726
1727/*
1728 * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
1729 * (SSH_CHANNEL_RPORT_LISTENER).
1730 */
Kevin Steves12057502001-02-05 14:54:34 +00001731int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001732channel_request_forwarding(
1733 const char *listen_address, u_short listen_port,
1734 const char *host_to_connect, u_short port_to_connect,
1735 int gateway_ports, int remote_fwd)
1736{
1737 int success, ch, sock, on = 1, ctype;
Damien Miller34132e52000-01-14 15:45:46 +11001738 struct addrinfo hints, *ai, *aitop;
1739 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0bc1bd82000-11-13 22:57:25 +11001740 const char *host;
Damien Miller5428f641999-11-25 11:54:57 +11001741 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001742
Kevin Steves12057502001-02-05 14:54:34 +00001743 success = 0;
1744
Damien Miller0bc1bd82000-11-13 22:57:25 +11001745 if (remote_fwd) {
1746 host = listen_address;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001747 ctype = SSH_CHANNEL_RPORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001748 } else {
1749 host = host_to_connect;
1750 ctype =SSH_CHANNEL_PORT_LISTENER;
1751 }
1752
Kevin Steves12057502001-02-05 14:54:34 +00001753 if (strlen(host) > sizeof(channels[0].path) - 1) {
1754 error("Forward host name too long.");
1755 return success;
1756 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001757
Damien Miller0bc1bd82000-11-13 22:57:25 +11001758 /* XXX listen_address is currently ignored */
Damien Miller5428f641999-11-25 11:54:57 +11001759 /*
Damien Miller34132e52000-01-14 15:45:46 +11001760 * getaddrinfo returns a loopback address if the hostname is
1761 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11001762 */
Damien Miller34132e52000-01-14 15:45:46 +11001763 memset(&hints, 0, sizeof(hints));
1764 hints.ai_family = IPv4or6;
1765 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
1766 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001767 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001768 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
1769 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11001770
Damien Miller34132e52000-01-14 15:45:46 +11001771 for (ai = aitop; ai; ai = ai->ai_next) {
1772 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1773 continue;
1774 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1775 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001776 error("channel_request_forwarding: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001777 continue;
1778 }
1779 /* Create a port to listen for the host. */
1780 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1781 if (sock < 0) {
1782 /* this is no error since kernel may not support ipv6 */
1783 verbose("socket: %.100s", strerror(errno));
1784 continue;
1785 }
1786 /*
1787 * Set socket options. We would like the socket to disappear
1788 * as soon as it has been closed for whatever reason.
1789 */
1790 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
1791 linger.l_onoff = 1;
1792 linger.l_linger = 5;
1793 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
1794 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001795
Damien Miller34132e52000-01-14 15:45:46 +11001796 /* Bind the socket to the address. */
1797 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1798 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11001799 if (!ai->ai_next)
1800 error("bind: %.100s", strerror(errno));
1801 else
1802 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001803
Damien Miller34132e52000-01-14 15:45:46 +11001804 close(sock);
1805 continue;
1806 }
1807 /* Start listening for connections on the socket. */
1808 if (listen(sock, 5) < 0) {
1809 error("listen: %.100s", strerror(errno));
1810 close(sock);
1811 continue;
1812 }
1813 /* Allocate a channel number for the socket. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001814 ch = channel_new("port listener", ctype, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10001815 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11001816 0, xstrdup("port listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11001817 strlcpy(channels[ch].path, host, sizeof(channels[ch].path));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001818 channels[ch].host_port = port_to_connect;
1819 channels[ch].listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11001820 success = 1;
1821 }
1822 if (success == 0)
Kevin Steves12057502001-02-05 14:54:34 +00001823 error("channel_request_forwarding: cannot listen to port: %d",
1824 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001825 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00001826 return success;
Damien Miller95def091999-11-25 00:26:21 +11001827}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001828
Damien Miller5428f641999-11-25 11:54:57 +11001829/*
1830 * Initiate forwarding of connections to port "port" on remote host through
1831 * the secure channel to host:port from local side.
1832 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001833
Damien Miller4af51302000-04-16 11:18:38 +10001834void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001835channel_request_remote_forwarding(u_short listen_port,
1836 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001837{
Damien Miller0bc1bd82000-11-13 22:57:25 +11001838 int payload_len, type, success = 0;
1839
Damien Miller95def091999-11-25 00:26:21 +11001840 /* Record locally that connection to this host/port is permitted. */
1841 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1842 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001843
Damien Miller95def091999-11-25 00:26:21 +11001844 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10001845 if (compat20) {
1846 const char *address_to_bind = "0.0.0.0";
1847 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1848 packet_put_cstring("tcpip-forward");
1849 packet_put_char(0); /* boolean: want reply */
1850 packet_put_cstring(address_to_bind);
1851 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001852 packet_send();
1853 packet_write_wait();
1854 /* Assume that server accepts the request */
1855 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10001856 } else {
1857 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10001858 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10001859 packet_put_cstring(host_to_connect);
1860 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10001861 packet_send();
1862 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001863
1864 /* Wait for response from the remote side. */
1865 type = packet_read(&payload_len);
1866 switch (type) {
1867 case SSH_SMSG_SUCCESS:
1868 success = 1;
1869 break;
1870 case SSH_SMSG_FAILURE:
1871 log("Warning: Server denied remote port forwarding.");
1872 break;
1873 default:
1874 /* Unknown packet */
1875 packet_disconnect("Protocol error for port forward request:"
1876 "received packet type %d.", type);
1877 }
1878 }
1879 if (success) {
1880 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
1881 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
1882 permitted_opens[num_permitted_opens].listen_port = listen_port;
1883 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10001884 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001885}
1886
Damien Miller5428f641999-11-25 11:54:57 +11001887/*
1888 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
1889 * listening for the port, and sends back a success reply (or disconnect
1890 * message if there was an error). This never returns if there was an error.
1891 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001892
Damien Miller4af51302000-04-16 11:18:38 +10001893void
Damien Millere247cc42000-05-07 12:03:14 +10001894channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001895{
Damien Milleraae6c611999-12-06 11:47:28 +11001896 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11001897 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001898
Damien Miller95def091999-11-25 00:26:21 +11001899 /* Get arguments from the packet. */
1900 port = packet_get_int();
1901 hostname = packet_get_string(NULL);
1902 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001903
Damien Millerbac2d8a2000-09-05 16:13:06 +11001904#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11001905 /*
1906 * Check that an unprivileged user is not trying to forward a
1907 * privileged port.
1908 */
Damien Miller95def091999-11-25 00:26:21 +11001909 if (port < IPPORT_RESERVED && !is_root)
1910 packet_disconnect("Requested forwarding of port %d but user is not root.",
1911 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001912#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001913 /* Initiate forwarding */
Damien Millere247cc42000-05-07 12:03:14 +10001914 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11001915
1916 /* Free the argument string. */
1917 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001918}
1919
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001920/*
1921 * Permits opening to any host/port if permitted_opens[] is empty. This is
1922 * usually called by the server, because the user could connect to any port
1923 * anyway, and the server has no way to know but to trust the client anyway.
1924 */
1925void
1926channel_permit_all_opens()
1927{
1928 if (num_permitted_opens == 0)
1929 all_opens_permitted = 1;
1930}
1931
Ben Lindstroma3700052001-04-05 23:26:32 +00001932void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001933channel_add_permitted_opens(char *host, int port)
1934{
1935 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1936 fatal("channel_request_remote_forwarding: too many forwards");
1937 debug("allow port forwarding to host %s port %d", host, port);
1938
1939 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
1940 permitted_opens[num_permitted_opens].port_to_connect = port;
1941 num_permitted_opens++;
1942
1943 all_opens_permitted = 0;
1944}
1945
Ben Lindstroma3700052001-04-05 23:26:32 +00001946void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001947channel_clear_permitted_opens(void)
1948{
1949 int i;
1950
1951 for (i = 0; i < num_permitted_opens; i++)
1952 xfree(permitted_opens[i].host_to_connect);
1953 num_permitted_opens = 0;
1954
1955}
1956
1957
1958/* return socket to remote host, port */
Damien Millerb38eff82000-04-01 11:09:21 +10001959int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001960connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001961{
Damien Miller34132e52000-01-14 15:45:46 +11001962 struct addrinfo hints, *ai, *aitop;
1963 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
1964 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10001965 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11001966
Damien Miller34132e52000-01-14 15:45:46 +11001967 memset(&hints, 0, sizeof(hints));
1968 hints.ai_family = IPv4or6;
1969 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001970 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11001971 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001972 error("connect_to %.100s: unknown host (%s)", host,
1973 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10001974 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001975 }
Damien Miller34132e52000-01-14 15:45:46 +11001976 for (ai = aitop; ai; ai = ai->ai_next) {
1977 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1978 continue;
1979 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1980 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001981 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001982 continue;
1983 }
Damien Miller34132e52000-01-14 15:45:46 +11001984 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1985 if (sock < 0) {
1986 error("socket: %.100s", strerror(errno));
1987 continue;
1988 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +00001989 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001990 fatal("connect_to: F_SETFL: %s", strerror(errno));
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001991 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
1992 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001993 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11001994 strerror(errno));
1995 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00001996 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11001997 }
1998 break; /* success */
1999
2000 }
2001 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002002 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002003 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002004 return -1;
2005 }
2006 /* success */
2007 return sock;
2008}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002009
Damien Miller0bc1bd82000-11-13 22:57:25 +11002010int
2011channel_connect_by_listen_adress(u_short listen_port)
2012{
2013 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002014
Damien Miller0bc1bd82000-11-13 22:57:25 +11002015 for (i = 0; i < num_permitted_opens; i++)
2016 if (permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002017 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002018 permitted_opens[i].host_to_connect,
2019 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002020 error("WARNING: Server requests forwarding for unknown listen_port %d",
2021 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002022 return -1;
2023}
Damien Millerb38eff82000-04-01 11:09:21 +10002024
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002025/* Check if connecting to that port is permitted and connect. */
2026int
2027channel_connect_to(const char *host, u_short port)
2028{
2029 int i, permit;
2030
2031 permit = all_opens_permitted;
2032 if (!permit) {
2033 for (i = 0; i < num_permitted_opens; i++)
2034 if (permitted_opens[i].port_to_connect == port &&
2035 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2036 permit = 1;
2037
2038 }
2039 if (!permit) {
2040 log("Received request to connect to host %.100s port %d, "
2041 "but the request was denied.", host, port);
2042 return -1;
2043 }
2044 return connect_to(host, port);
2045}
2046
Damien Millerb38eff82000-04-01 11:09:21 +10002047/*
2048 * This is called after receiving PORT_OPEN message. This attempts to
2049 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
2050 * or CHANNEL_OPEN_FAILURE.
2051 */
2052
Damien Miller4af51302000-04-16 11:18:38 +10002053void
Damien Miller62cee002000-09-23 17:15:56 +11002054channel_input_port_open(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002055{
2056 u_short host_port;
2057 char *host, *originator_string;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002058 int remote_channel, sock = -1, newch;
Damien Millerb38eff82000-04-01 11:09:21 +10002059
Damien Millerb38eff82000-04-01 11:09:21 +10002060 remote_channel = packet_get_int();
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002061 host = packet_get_string(NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10002062 host_port = packet_get_int();
2063
Damien Millerb38eff82000-04-01 11:09:21 +10002064 if (have_hostname_in_open) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002065 originator_string = packet_get_string(NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10002066 } else {
2067 originator_string = xstrdup("unknown (remote did not supply name)");
Damien Miller95def091999-11-25 00:26:21 +11002068 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002069 packet_done();
2070 sock = channel_connect_to(host, host_port);
2071 if (sock != -1) {
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002072 newch = channel_allocate(SSH_CHANNEL_CONNECTING,
2073 sock, originator_string);
Damien Millerb38eff82000-04-01 11:09:21 +10002074 channels[newch].remote_id = remote_channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002075
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002076 /*XXX delay answer? */
Damien Millerb38eff82000-04-01 11:09:21 +10002077 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2078 packet_put_int(remote_channel);
2079 packet_put_int(newch);
2080 packet_send();
2081 } else {
2082 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2083 packet_put_int(remote_channel);
2084 packet_send();
2085 }
Damien Miller95def091999-11-25 00:26:21 +11002086 xfree(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002087}
2088
Damien Miller5428f641999-11-25 11:54:57 +11002089/*
2090 * Creates an internet domain socket for listening for X11 connections.
2091 * Returns a suitable value for the DISPLAY variable, or NULL if an error
2092 * occurs.
2093 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002094
Damien Miller34132e52000-01-14 15:45:46 +11002095#define NUM_SOCKS 10
2096
Damien Miller95def091999-11-25 00:26:21 +11002097char *
Damien Millera34a28b1999-12-14 10:47:15 +11002098x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002099{
Damien Milleraae6c611999-12-06 11:47:28 +11002100 int display_number, sock;
2101 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002102 struct addrinfo hints, *ai, *aitop;
2103 char strport[NI_MAXSERV];
2104 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
2105 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11002106 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002107
Damien Millera34a28b1999-12-14 10:47:15 +11002108 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11002109 display_number < MAX_DISPLAYS;
2110 display_number++) {
2111 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002112 memset(&hints, 0, sizeof(hints));
2113 hints.ai_family = IPv4or6;
2114 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
2115 hints.ai_socktype = SOCK_STREAM;
2116 snprintf(strport, sizeof strport, "%d", port);
2117 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2118 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11002119 return NULL;
2120 }
Damien Miller34132e52000-01-14 15:45:46 +11002121 for (ai = aitop; ai; ai = ai->ai_next) {
2122 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2123 continue;
2124 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2125 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002126 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002127 error("socket: %.100s", strerror(errno));
2128 return NULL;
2129 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002130 debug("x11_create_display_inet: Socket family %d not supported",
2131 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002132 continue;
2133 }
Damien Miller34132e52000-01-14 15:45:46 +11002134 }
2135 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2136 debug("bind port %d: %.100s", port, strerror(errno));
2137 shutdown(sock, SHUT_RDWR);
2138 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002139
2140 if (ai->ai_next)
2141 continue;
2142
Damien Miller34132e52000-01-14 15:45:46 +11002143 for (n = 0; n < num_socks; n++) {
2144 shutdown(socks[n], SHUT_RDWR);
2145 close(socks[n]);
2146 }
2147 num_socks = 0;
2148 break;
2149 }
2150 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002151#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002152 if (num_socks == NUM_SOCKS)
2153 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002154#else
2155 break;
2156#endif
Damien Miller95def091999-11-25 00:26:21 +11002157 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002158 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002159 if (num_socks > 0)
2160 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002161 }
Damien Miller95def091999-11-25 00:26:21 +11002162 if (display_number >= MAX_DISPLAYS) {
2163 error("Failed to allocate internet-domain X11 display socket.");
2164 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002165 }
Damien Miller95def091999-11-25 00:26:21 +11002166 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002167 for (n = 0; n < num_socks; n++) {
2168 sock = socks[n];
2169 if (listen(sock, 5) < 0) {
2170 error("listen: %.100s", strerror(errno));
2171 shutdown(sock, SHUT_RDWR);
2172 close(sock);
2173 return NULL;
2174 }
Damien Miller95def091999-11-25 00:26:21 +11002175 }
Damien Miller34132e52000-01-14 15:45:46 +11002176
Damien Miller95def091999-11-25 00:26:21 +11002177 /* Set up a suitable value for the DISPLAY variable. */
2178 if (gethostname(hostname, sizeof(hostname)) < 0)
2179 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11002180
2181#ifdef IPADDR_IN_DISPLAY
Kevin Stevesef4eea92001-02-05 12:42:17 +00002182 /*
Damien Miller76112de1999-12-21 11:18:08 +11002183 * HPUX detects the local hostname in the DISPLAY variable and tries
2184 * to set up a shared memory connection to the server, which it
2185 * incorrectly supposes to be local.
2186 *
2187 * The workaround - as used in later $$H and other programs - is
2188 * is to set display to the host's IP address.
2189 */
2190 {
2191 struct hostent *he;
2192 struct in_addr my_addr;
2193
2194 he = gethostbyname(hostname);
2195 if (he == NULL) {
2196 error("[X11-broken-fwd-hostname-workaround] Could not get "
2197 "IP address for hostname %s.", hostname);
2198
2199 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
2200 "Could not get IP address for hostname %s.", hostname);
2201
2202 shutdown(sock, SHUT_RDWR);
2203 close(sock);
2204
2205 return NULL;
2206 }
2207
2208 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2209
2210 /* Set DISPLAY to <ip address>:screen.display */
Kevin Stevesef4eea92001-02-05 12:42:17 +00002211 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Kevin Stevesda6cd592000-12-29 18:41:21 +00002212 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002213 }
2214#else /* IPADDR_IN_DISPLAY */
2215 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11002216 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Kevin Stevesda6cd592000-12-29 18:41:21 +00002217 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002218#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002219
Damien Miller34132e52000-01-14 15:45:46 +11002220 /* Allocate a channel for each socket. */
2221 for (n = 0; n < num_socks; n++) {
2222 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10002223 (void) channel_new("x11 listener",
2224 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2225 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002226 0, xstrdup("X11 inet listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11002227 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002228
Damien Miller95def091999-11-25 00:26:21 +11002229 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11002230 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002231}
2232
2233#ifndef X_UNIX_PATH
2234#define X_UNIX_PATH "/tmp/.X11-unix/X"
2235#endif
2236
2237static
2238int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002239connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002240{
Damien Miller95def091999-11-25 00:26:21 +11002241 static const char *const x_sockets[] = {
2242 X_UNIX_PATH "%u",
2243 "/var/X/.X11-unix/X" "%u",
2244 "/usr/spool/sockets/X11/" "%u",
2245 NULL
2246 };
2247 int sock;
2248 struct sockaddr_un addr;
2249 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002250
Damien Miller95def091999-11-25 00:26:21 +11002251 for (path = x_sockets; *path; ++path) {
2252 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2253 if (sock < 0)
2254 error("socket: %.100s", strerror(errno));
2255 memset(&addr, 0, sizeof(addr));
2256 addr.sun_family = AF_UNIX;
2257 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
2258 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2259 return sock;
2260 close(sock);
2261 }
2262 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2263 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002264}
2265
Damien Millerbd483e72000-04-30 10:00:53 +10002266int
2267x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002268{
Damien Millerbd483e72000-04-30 10:00:53 +10002269 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002270 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002271 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002272 struct addrinfo hints, *ai, *aitop;
2273 char strport[NI_MAXSERV];
2274 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002275
Damien Miller95def091999-11-25 00:26:21 +11002276 /* Try to open a socket for the local X server. */
2277 display = getenv("DISPLAY");
2278 if (!display) {
2279 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002280 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002281 }
Damien Miller5428f641999-11-25 11:54:57 +11002282 /*
2283 * Now we decode the value of the DISPLAY variable and make a
2284 * connection to the real X server.
2285 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002286
Damien Miller5428f641999-11-25 11:54:57 +11002287 /*
2288 * Check if it is a unix domain socket. Unix domain displays are in
2289 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2290 */
Damien Miller95def091999-11-25 00:26:21 +11002291 if (strncmp(display, "unix:", 5) == 0 ||
2292 display[0] == ':') {
2293 /* Connect to the unix domain socket. */
2294 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2295 error("Could not parse display number from DISPLAY: %.100s",
2296 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002297 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002298 }
2299 /* Create a socket. */
2300 sock = connect_local_xsocket(display_number);
2301 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002302 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002303
2304 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002305 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002306 }
Damien Miller5428f641999-11-25 11:54:57 +11002307 /*
2308 * Connect to an inet socket. The DISPLAY value is supposedly
2309 * hostname:d[.s], where hostname may also be numeric IP address.
2310 */
Damien Miller95def091999-11-25 00:26:21 +11002311 strncpy(buf, display, sizeof(buf));
2312 buf[sizeof(buf) - 1] = 0;
2313 cp = strchr(buf, ':');
2314 if (!cp) {
2315 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002316 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002317 }
Damien Miller95def091999-11-25 00:26:21 +11002318 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002319 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002320 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2321 error("Could not parse display number from DISPLAY: %.100s",
2322 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002323 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002324 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002325
Damien Miller34132e52000-01-14 15:45:46 +11002326 /* Look up the host address */
2327 memset(&hints, 0, sizeof(hints));
2328 hints.ai_family = IPv4or6;
2329 hints.ai_socktype = SOCK_STREAM;
2330 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2331 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2332 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002333 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002334 }
Damien Miller34132e52000-01-14 15:45:46 +11002335 for (ai = aitop; ai; ai = ai->ai_next) {
2336 /* Create a socket. */
2337 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2338 if (sock < 0) {
2339 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002340 continue;
2341 }
2342 /* Connect it to the display. */
2343 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2344 debug("connect %.100s port %d: %.100s", buf,
2345 6000 + display_number, strerror(errno));
2346 close(sock);
2347 continue;
2348 }
2349 /* Success */
2350 break;
Damien Miller34132e52000-01-14 15:45:46 +11002351 }
Damien Miller34132e52000-01-14 15:45:46 +11002352 freeaddrinfo(aitop);
2353 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002354 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002355 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002356 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002357 }
Damien Millerbd483e72000-04-30 10:00:53 +10002358 return sock;
2359}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002360
Damien Millerbd483e72000-04-30 10:00:53 +10002361/*
2362 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2363 * the remote channel number. We should do whatever we want, and respond
2364 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2365 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002366
Damien Millerbd483e72000-04-30 10:00:53 +10002367void
Damien Miller62cee002000-09-23 17:15:56 +11002368x11_input_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002369{
2370 int remote_channel, sock = 0, newch;
2371 char *remote_host;
Ben Lindstrom46c16222000-12-22 01:43:59 +00002372 u_int remote_len;
Damien Miller95def091999-11-25 00:26:21 +11002373
Damien Millerbd483e72000-04-30 10:00:53 +10002374 /* Get remote channel number. */
2375 remote_channel = packet_get_int();
Damien Miller95def091999-11-25 00:26:21 +11002376
Damien Millerbd483e72000-04-30 10:00:53 +10002377 /* Get remote originator name. */
2378 if (have_hostname_in_open) {
2379 remote_host = packet_get_string(&remote_len);
2380 remote_len += 4;
2381 } else {
2382 remote_host = xstrdup("unknown (remote did not supply name)");
2383 remote_len = 0;
2384 }
2385
2386 debug("Received X11 open request.");
2387 packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN);
2388
2389 /* Obtain a connection to the real X display. */
2390 sock = x11_connect_display();
2391 if (sock == -1) {
2392 /* Send refusal to the remote host. */
2393 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2394 packet_put_int(remote_channel);
2395 packet_send();
2396 } else {
2397 /* Allocate a channel for this connection. */
2398 newch = channel_allocate(
2399 (x11_saved_proto == NULL) ?
2400 SSH_CHANNEL_OPEN : SSH_CHANNEL_X11_OPEN,
2401 sock, remote_host);
2402 channels[newch].remote_id = remote_channel;
2403
2404 /* Send a confirmation to the remote host. */
2405 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2406 packet_put_int(remote_channel);
2407 packet_put_int(newch);
2408 packet_send();
2409 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002410}
2411
Damien Miller69b69aa2000-10-28 14:19:58 +11002412/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2413void
2414deny_input_open(int type, int plen, void *ctxt)
2415{
2416 int rchan = packet_get_int();
2417 switch(type){
2418 case SSH_SMSG_AGENT_OPEN:
2419 error("Warning: ssh server tried agent forwarding.");
2420 break;
2421 case SSH_SMSG_X11_OPEN:
2422 error("Warning: ssh server tried X11 forwarding.");
2423 break;
2424 default:
2425 error("deny_input_open: type %d plen %d", type, plen);
2426 break;
2427 }
2428 error("Warning: this is probably a break in attempt by a malicious server.");
2429 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2430 packet_put_int(rchan);
2431 packet_send();
2432}
2433
Damien Miller5428f641999-11-25 11:54:57 +11002434/*
2435 * Requests forwarding of X11 connections, generates fake authentication
2436 * data, and enables authentication spoofing.
2437 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002438
Damien Miller4af51302000-04-16 11:18:38 +10002439void
Damien Millerbd483e72000-04-30 10:00:53 +10002440x11_request_forwarding_with_spoofing(int client_session_id,
2441 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002442{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002443 u_int data_len = (u_int) strlen(data) / 2;
Ben Lindstromb3211a82001-02-10 22:33:19 +00002444 u_int i, value, len;
Damien Miller95def091999-11-25 00:26:21 +11002445 char *new_data;
2446 int screen_number;
2447 const char *cp;
2448 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002449
Damien Miller95def091999-11-25 00:26:21 +11002450 cp = getenv("DISPLAY");
2451 if (cp)
2452 cp = strchr(cp, ':');
2453 if (cp)
2454 cp = strchr(cp, '.');
2455 if (cp)
2456 screen_number = atoi(cp + 1);
2457 else
2458 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002459
Damien Miller95def091999-11-25 00:26:21 +11002460 /* Save protocol name. */
2461 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002462
Damien Miller5428f641999-11-25 11:54:57 +11002463 /*
2464 * Extract real authentication data and generate fake data of the
2465 * same length.
2466 */
Damien Miller95def091999-11-25 00:26:21 +11002467 x11_saved_data = xmalloc(data_len);
2468 x11_fake_data = xmalloc(data_len);
2469 for (i = 0; i < data_len; i++) {
2470 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2471 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2472 if (i % 4 == 0)
2473 rand = arc4random();
2474 x11_saved_data[i] = value;
2475 x11_fake_data[i] = rand & 0xff;
2476 rand >>= 8;
2477 }
2478 x11_saved_data_len = data_len;
2479 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002480
Damien Miller95def091999-11-25 00:26:21 +11002481 /* Convert the fake data into hex. */
Ben Lindstromb3211a82001-02-10 22:33:19 +00002482 len = 2 * data_len + 1;
2483 new_data = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +11002484 for (i = 0; i < data_len; i++)
Ben Lindstromb3211a82001-02-10 22:33:19 +00002485 snprintf(new_data + 2 * i, len - 2 * i,
2486 "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002487
Damien Miller95def091999-11-25 00:26:21 +11002488 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002489 if (compat20) {
2490 channel_request_start(client_session_id, "x11-req", 0);
2491 packet_put_char(0); /* XXX bool single connection */
2492 } else {
2493 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2494 }
2495 packet_put_cstring(proto);
2496 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002497 packet_put_int(screen_number);
2498 packet_send();
2499 packet_write_wait();
2500 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002501}
2502
2503/* Sends a message to the server to request authentication fd forwarding. */
2504
Damien Miller4af51302000-04-16 11:18:38 +10002505void
Damien Miller95def091999-11-25 00:26:21 +11002506auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002507{
Damien Miller95def091999-11-25 00:26:21 +11002508 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2509 packet_send();
2510 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002511}
2512
Damien Miller5428f641999-11-25 11:54:57 +11002513/*
2514 * Returns the name of the forwarded authentication socket. Returns NULL if
2515 * there is no forwarded authentication socket. The returned value points to
2516 * a static buffer.
2517 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002518
Damien Miller95def091999-11-25 00:26:21 +11002519char *
2520auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002521{
Damien Miller95def091999-11-25 00:26:21 +11002522 return channel_forwarded_auth_socket_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002523}
2524
2525/* removes the agent forwarding socket */
2526
Damien Miller4af51302000-04-16 11:18:38 +10002527void
Damien Miller95def091999-11-25 00:26:21 +11002528cleanup_socket(void)
2529{
Damien Miller78315eb2000-09-29 23:01:36 +11002530 unlink(channel_forwarded_auth_socket_name);
Damien Miller95def091999-11-25 00:26:21 +11002531 rmdir(channel_forwarded_auth_socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002532}
2533
Damien Miller5428f641999-11-25 11:54:57 +11002534/*
Damien Millerd3a18572000-06-07 19:55:44 +10002535 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002536 * This starts forwarding authentication requests.
2537 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002538
Damien Millerd3a18572000-06-07 19:55:44 +10002539int
Damien Miller95def091999-11-25 00:26:21 +11002540auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002541{
Damien Miller95def091999-11-25 00:26:21 +11002542 int sock, newch;
2543 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002544
Damien Miller95def091999-11-25 00:26:21 +11002545 if (auth_get_socket_name() != NULL)
2546 fatal("Protocol error: authentication forwarding requested twice.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002547
Damien Miller95def091999-11-25 00:26:21 +11002548 /* Temporarily drop privileged uid for mkdir/bind. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00002549 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002550
Damien Miller95def091999-11-25 00:26:21 +11002551 /* Allocate a buffer for the socket name, and format the name. */
2552 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
2553 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
2554 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002555
Damien Miller95def091999-11-25 00:26:21 +11002556 /* Create private directory for socket */
Damien Millerd3a18572000-06-07 19:55:44 +10002557 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL) {
2558 packet_send_debug("Agent forwarding disabled: mkdtemp() failed: %.100s",
2559 strerror(errno));
2560 restore_uid();
2561 xfree(channel_forwarded_auth_socket_name);
2562 xfree(channel_forwarded_auth_socket_dir);
2563 channel_forwarded_auth_socket_name = NULL;
2564 channel_forwarded_auth_socket_dir = NULL;
2565 return 0;
2566 }
Damien Miller95def091999-11-25 00:26:21 +11002567 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
2568 channel_forwarded_auth_socket_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002569
Damien Miller95def091999-11-25 00:26:21 +11002570 if (atexit(cleanup_socket) < 0) {
2571 int saved = errno;
2572 cleanup_socket();
2573 packet_disconnect("socket: %.100s", strerror(saved));
2574 }
2575 /* Create the socket. */
2576 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2577 if (sock < 0)
2578 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002579
Damien Miller95def091999-11-25 00:26:21 +11002580 /* Bind it to the name. */
2581 memset(&sunaddr, 0, sizeof(sunaddr));
2582 sunaddr.sun_family = AF_UNIX;
2583 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
2584 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002585
Damien Miller95def091999-11-25 00:26:21 +11002586 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2587 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002588
Damien Miller95def091999-11-25 00:26:21 +11002589 /* Restore the privileged uid. */
2590 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002591
Damien Miller95def091999-11-25 00:26:21 +11002592 /* Start listening on the socket. */
2593 if (listen(sock, 5) < 0)
2594 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002595
Damien Miller95def091999-11-25 00:26:21 +11002596 /* Allocate a channel for the authentication agent socket. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002597 newch = channel_new("auth socket",
2598 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2599 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2600 0, xstrdup("auth socket"), 1);
2601
Damien Miller037a0dc1999-12-07 15:38:31 +11002602 strlcpy(channels[newch].path, channel_forwarded_auth_socket_name,
2603 sizeof(channels[newch].path));
Damien Millerd3a18572000-06-07 19:55:44 +10002604 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002605}
2606
2607/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2608
Damien Miller4af51302000-04-16 11:18:38 +10002609void
Damien Miller62cee002000-09-23 17:15:56 +11002610auth_input_open_request(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002611{
Damien Miller95def091999-11-25 00:26:21 +11002612 int remch, sock, newch;
2613 char *dummyname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002614
Damien Millerb38eff82000-04-01 11:09:21 +10002615 packet_integrity_check(plen, 4, type);
2616
Damien Miller95def091999-11-25 00:26:21 +11002617 /* Read the remote channel number from the message. */
2618 remch = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002619
Damien Miller5428f641999-11-25 11:54:57 +11002620 /*
2621 * Get a connection to the local authentication agent (this may again
2622 * get forwarded).
2623 */
Damien Miller95def091999-11-25 00:26:21 +11002624 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002625
Damien Miller5428f641999-11-25 11:54:57 +11002626 /*
2627 * If we could not connect the agent, send an error message back to
2628 * the server. This should never happen unless the agent dies,
2629 * because authentication forwarding is only enabled if we have an
2630 * agent.
2631 */
Damien Miller95def091999-11-25 00:26:21 +11002632 if (sock < 0) {
2633 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2634 packet_put_int(remch);
2635 packet_send();
2636 return;
2637 }
2638 debug("Forwarding authentication connection.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002639
Damien Miller5428f641999-11-25 11:54:57 +11002640 /*
2641 * Dummy host name. This will be freed when the channel is freed; it
2642 * will still be valid in the packet_put_string below since the
2643 * channel cannot yet be freed at that point.
2644 */
Damien Miller95def091999-11-25 00:26:21 +11002645 dummyname = xstrdup("authentication agent connection");
2646
2647 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);
2648 channels[newch].remote_id = remch;
2649
2650 /* Send a confirmation to the remote host. */
2651 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2652 packet_put_int(remch);
2653 packet_put_int(newch);
2654 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002655}
Damien Miller33b13562000-04-04 14:38:59 +10002656
2657void
Damien Millerbd483e72000-04-30 10:00:53 +10002658channel_start_open(int id)
Damien Miller33b13562000-04-04 14:38:59 +10002659{
2660 Channel *c = channel_lookup(id);
2661 if (c == NULL) {
2662 log("channel_open: %d: bad id", id);
2663 return;
2664 }
Damien Millerbd483e72000-04-30 10:00:53 +10002665 debug("send channel open %d", id);
Damien Miller33b13562000-04-04 14:38:59 +10002666 packet_start(SSH2_MSG_CHANNEL_OPEN);
2667 packet_put_cstring(c->ctype);
2668 packet_put_int(c->self);
2669 packet_put_int(c->local_window);
2670 packet_put_int(c->local_maxpacket);
Damien Millerbd483e72000-04-30 10:00:53 +10002671}
2672void
2673channel_open(int id)
2674{
2675 /* XXX REMOVE ME */
2676 channel_start_open(id);
Damien Miller33b13562000-04-04 14:38:59 +10002677 packet_send();
Damien Miller33b13562000-04-04 14:38:59 +10002678}
2679void
2680channel_request(int id, char *service, int wantconfirm)
2681{
2682 channel_request_start(id, service, wantconfirm);
2683 packet_send();
2684 debug("channel request %d: %s", id, service) ;
2685}
2686void
2687channel_request_start(int id, char *service, int wantconfirm)
2688{
2689 Channel *c = channel_lookup(id);
2690 if (c == NULL) {
2691 log("channel_request: %d: bad id", id);
2692 return;
2693 }
2694 packet_start(SSH2_MSG_CHANNEL_REQUEST);
2695 packet_put_int(c->remote_id);
2696 packet_put_cstring(service);
2697 packet_put_char(wantconfirm);
2698}
2699void
2700channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
2701{
2702 Channel *c = channel_lookup(id);
2703 if (c == NULL) {
2704 log("channel_register_callback: %d: bad id", id);
2705 return;
2706 }
2707 c->cb_event = mtype;
2708 c->cb_fn = fn;
2709 c->cb_arg = arg;
2710}
2711void
2712channel_register_cleanup(int id, channel_callback_fn *fn)
2713{
2714 Channel *c = channel_lookup(id);
2715 if (c == NULL) {
2716 log("channel_register_cleanup: %d: bad id", id);
2717 return;
2718 }
2719 c->dettach_user = fn;
2720}
2721void
2722channel_cancel_cleanup(int id)
2723{
2724 Channel *c = channel_lookup(id);
2725 if (c == NULL) {
2726 log("channel_cancel_cleanup: %d: bad id", id);
2727 return;
2728 }
2729 c->dettach_user = NULL;
2730}
Kevin Stevesef4eea92001-02-05 12:42:17 +00002731void
Damien Millerad833b32000-08-23 10:46:23 +10002732channel_register_filter(int id, channel_filter_fn *fn)
2733{
2734 Channel *c = channel_lookup(id);
2735 if (c == NULL) {
2736 log("channel_register_filter: %d: bad id", id);
2737 return;
2738 }
2739 c->input_filter = fn;
2740}
Damien Miller33b13562000-04-04 14:38:59 +10002741
2742void
Damien Miller69b69aa2000-10-28 14:19:58 +11002743channel_set_fds(int id, int rfd, int wfd, int efd,
2744 int extusage, int nonblock)
Damien Miller33b13562000-04-04 14:38:59 +10002745{
2746 Channel *c = channel_lookup(id);
2747 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
2748 fatal("channel_activate for non-larval channel %d.", id);
Damien Miller69b69aa2000-10-28 14:19:58 +11002749 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller33b13562000-04-04 14:38:59 +10002750 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002751 /* XXX window size? */
Damien Millere4340be2000-09-16 13:29:08 +11002752 c->local_window = c->local_window_max = c->local_maxpacket * 2;
Damien Miller33b13562000-04-04 14:38:59 +10002753 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2754 packet_put_int(c->remote_id);
2755 packet_put_int(c->local_window);
2756 packet_send();
2757}