blob: cd73bd72647dc438dddced9d9ce701263e5d2a19 [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 Lindstromf3436742001-04-29 19:52:00 +000043RCSID("$OpenBSD: channels.c,v 1.110 2001/04/29 19:16:52 markus Exp $");
Ben Lindstrom226cfa02001-01-22 05:34:40 +000044
45#include <openssl/rsa.h>
46#include <openssl/dsa.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047
48#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000049#include "ssh1.h"
50#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051#include "packet.h"
52#include "xmalloc.h"
53#include "buffer.h"
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +000054#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "log.h"
57#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058#include "channels.h"
59#include "nchan.h"
60#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000061#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100062#include "key.h"
63#include "authfd.h"
64
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065/* Maximum number of fake X11 displays to try. */
66#define MAX_DISPLAYS 1000
67
68/* Max len of agent socket */
69#define MAX_SOCKET_NAME 100
70
Damien Miller5428f641999-11-25 11:54:57 +110071/*
72 * Pointer to an array containing all allocated channels. The array is
73 * dynamically extended as needed.
74 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075static Channel *channels = NULL;
76
Damien Miller5428f641999-11-25 11:54:57 +110077/*
78 * Size of the channel array. All slots of the array must always be
79 * initialized (at least the type field); unused slots are marked with type
80 * SSH_CHANNEL_FREE.
81 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082static int channels_alloc = 0;
83
Damien Miller5428f641999-11-25 11:54:57 +110084/*
85 * Maximum file descriptor value used in any of the channels. This is
86 * updated in channel_allocate.
87 */
Damien Miller5e953212001-01-30 09:14:00 +110088static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
90/* Name and directory of socket for authentication agent forwarding. */
91static char *channel_forwarded_auth_socket_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +110092static char *channel_forwarded_auth_socket_dir = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
94/* Saved X11 authentication protocol name. */
95char *x11_saved_proto = NULL;
96
97/* Saved X11 authentication data. This is the real data. */
98char *x11_saved_data = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +000099u_int x11_saved_data_len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
Damien Miller5428f641999-11-25 11:54:57 +1100101/*
102 * Fake X11 authentication data. This is what the server will be sending us;
103 * we should replace any occurrences of this by the real data.
104 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105char *x11_fake_data = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000106u_int x11_fake_data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
Damien Miller5428f641999-11-25 11:54:57 +1100108/*
109 * Data structure for storing which hosts are permitted for forward requests.
110 * The local sides of any remote forwards are stored in this array to prevent
111 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
112 * network (which might be behind a firewall).
113 */
Damien Miller95def091999-11-25 00:26:21 +1100114typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000115 char *host_to_connect; /* Connect to 'host'. */
116 u_short port_to_connect; /* Connect to 'port'. */
117 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118} ForwardPermission;
119
120/* List of all permitted host/port pairs to connect. */
121static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
122/* Number of permitted host/port pairs in the array. */
123static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +1100124/*
125 * If this is true, all opens are permitted. This is the case on the server
126 * on which we have to trust the client anyway, and the user could do
127 * anything after logging in anyway.
128 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129static int all_opens_permitted = 0;
130
131/* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */
132static int have_hostname_in_open = 0;
133
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000134/* AF_UNSPEC or AF_INET or AF_INET6 */
135extern int IPv4or6;
136
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000137void port_open_helper(Channel *c, char *rtype);
138
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139/* Sets specific protocol options. */
140
Damien Miller4af51302000-04-16 11:18:38 +1000141void
Damien Miller95def091999-11-25 00:26:21 +1100142channel_set_options(int hostname_in_open)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143{
Damien Miller95def091999-11-25 00:26:21 +1100144 have_hostname_in_open = hostname_in_open;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145}
146
Damien Millerb38eff82000-04-01 11:09:21 +1000147/* lookup channel by id */
148
149Channel *
150channel_lookup(int id)
151{
152 Channel *c;
Damien Millerc0fd17f2000-06-26 10:22:53 +1000153 if (id < 0 || id > channels_alloc) {
Damien Millerb38eff82000-04-01 11:09:21 +1000154 log("channel_lookup: %d: bad id", id);
155 return NULL;
156 }
157 c = &channels[id];
158 if (c->type == SSH_CHANNEL_FREE) {
159 log("channel_lookup: %d: bad id: channel free", id);
160 return NULL;
161 }
162 return c;
163}
164
Damien Miller5428f641999-11-25 11:54:57 +1100165/*
Damien Millere247cc42000-05-07 12:03:14 +1000166 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000167 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100168 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169
Damien Miller6f83b8e2000-05-02 09:23:45 +1000170void
Damien Miller69b69aa2000-10-28 14:19:58 +1100171channel_register_fds(Channel *c, int rfd, int wfd, int efd,
172 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173{
Damien Miller95def091999-11-25 00:26:21 +1100174 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100175 channel_max_fd = MAX(channel_max_fd, rfd);
176 channel_max_fd = MAX(channel_max_fd, wfd);
177 channel_max_fd = MAX(channel_max_fd, efd);
178
Damien Miller5428f641999-11-25 11:54:57 +1100179 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000180
Damien Miller6f83b8e2000-05-02 09:23:45 +1000181 c->rfd = rfd;
182 c->wfd = wfd;
183 c->sock = (rfd == wfd) ? rfd : -1;
184 c->efd = efd;
185 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100186
Damien Miller79438cc2001-02-16 12:34:57 +1100187 /* XXX ugly hack: nonblock is only set by the server */
188 if (nonblock && isatty(c->rfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000189 debug("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100190 c->isatty = 1;
191 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000192 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100193 c->self, c->wfd);
194 }
195 } else {
196 c->isatty = 0;
197 }
198
Damien Miller69b69aa2000-10-28 14:19:58 +1100199 /* enable nonblocking mode */
200 if (nonblock) {
201 if (rfd != -1)
202 set_nonblock(rfd);
203 if (wfd != -1)
204 set_nonblock(wfd);
205 if (efd != -1)
206 set_nonblock(efd);
207 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000208}
209
210/*
211 * Allocate a new channel object and set its type and socket. This will cause
212 * remote_name to be freed.
213 */
214
215int
216channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Damien Miller69b69aa2000-10-28 14:19:58 +1100217 int window, int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000218{
219 int i, found;
220 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221
Damien Miller95def091999-11-25 00:26:21 +1100222 /* Do initial allocation if this is the first call. */
223 if (channels_alloc == 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000224 chan_init();
Damien Miller95def091999-11-25 00:26:21 +1100225 channels_alloc = 10;
226 channels = xmalloc(channels_alloc * sizeof(Channel));
227 for (i = 0; i < channels_alloc; i++)
228 channels[i].type = SSH_CHANNEL_FREE;
Damien Miller5428f641999-11-25 11:54:57 +1100229 /*
230 * Kludge: arrange a call to channel_stop_listening if we
231 * terminate with fatal().
232 */
Damien Miller95def091999-11-25 00:26:21 +1100233 fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL);
234 }
235 /* Try to find a free slot where to put the new channel. */
236 for (found = -1, i = 0; i < channels_alloc; i++)
237 if (channels[i].type == SSH_CHANNEL_FREE) {
238 /* Found a free slot. */
239 found = i;
240 break;
241 }
242 if (found == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100243 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100244 found = channels_alloc;
245 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100246 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100247 channels = xrealloc(channels, channels_alloc * sizeof(Channel));
248 for (i = found; i < channels_alloc; i++)
249 channels[i].type = SSH_CHANNEL_FREE;
250 }
251 /* Initialize and return new channel number. */
252 c = &channels[found];
253 buffer_init(&c->input);
254 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000255 buffer_init(&c->extended);
Damien Miller95def091999-11-25 00:26:21 +1100256 chan_init_iostates(c);
Damien Miller69b69aa2000-10-28 14:19:58 +1100257 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100258 c->self = found;
259 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000260 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000261 c->local_window = window;
262 c->local_window_max = window;
263 c->local_consumed = 0;
264 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100265 c->remote_id = -1;
266 c->remote_name = remote_name;
Damien Millerb38eff82000-04-01 11:09:21 +1000267 c->remote_window = 0;
268 c->remote_maxpacket = 0;
269 c->cb_fn = NULL;
270 c->cb_arg = NULL;
271 c->cb_event = 0;
272 c->dettach_user = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000273 c->input_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100274 debug("channel %d: new [%s]", found, remote_name);
275 return found;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000277/* old interface XXX */
Damien Miller4af51302000-04-16 11:18:38 +1000278int
Damien Millerb38eff82000-04-01 11:09:21 +1000279channel_allocate(int type, int sock, char *remote_name)
280{
Damien Miller69b69aa2000-10-28 14:19:58 +1100281 return channel_new("", type, sock, sock, -1, 0, 0, 0, remote_name, 1);
Damien Millerb38eff82000-04-01 11:09:21 +1000282}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283
Damien Miller6f83b8e2000-05-02 09:23:45 +1000284
285/* Close all channel fd/socket. */
286
287void
288channel_close_fds(Channel *c)
289{
290 if (c->sock != -1) {
291 close(c->sock);
292 c->sock = -1;
293 }
294 if (c->rfd != -1) {
295 close(c->rfd);
296 c->rfd = -1;
297 }
298 if (c->wfd != -1) {
299 close(c->wfd);
300 c->wfd = -1;
301 }
302 if (c->efd != -1) {
303 close(c->efd);
304 c->efd = -1;
305 }
306}
307
308/* Free the channel and close its fd/socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000309
Damien Miller4af51302000-04-16 11:18:38 +1000310void
Damien Millerb38eff82000-04-01 11:09:21 +1000311channel_free(int id)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312{
Damien Millerb38eff82000-04-01 11:09:21 +1000313 Channel *c = channel_lookup(id);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000314 char *s = channel_open_message();
315
Damien Millerb38eff82000-04-01 11:09:21 +1000316 if (c == NULL)
317 packet_disconnect("channel free: bad local channel %d", id);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000318 debug("channel_free: channel %d: status: %s", id, s);
319 xfree(s);
320
Damien Miller33b13562000-04-04 14:38:59 +1000321 if (c->dettach_user != NULL) {
322 debug("channel_free: channel %d: dettaching channel user", id);
323 c->dettach_user(c->self, NULL);
324 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000325 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000326 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000327 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000328 buffer_free(&c->input);
329 buffer_free(&c->output);
330 buffer_free(&c->extended);
331 c->type = SSH_CHANNEL_FREE;
332 if (c->remote_name) {
333 xfree(c->remote_name);
334 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100335 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000336}
337
Damien Miller5428f641999-11-25 11:54:57 +1100338/*
Damien Millerb38eff82000-04-01 11:09:21 +1000339 * 'channel_pre*' are called just before select() to add any bits relevant to
340 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100341 */
Damien Millerb38eff82000-04-01 11:09:21 +1000342/*
343 * 'channel_post*': perform any appropriate operations for channels which
344 * have events pending.
345 */
346typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
347chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
348chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
349
350void
351channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
352{
353 FD_SET(c->sock, readset);
354}
355
356void
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000357channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
358{
359 debug3("channel %d: waiting for connection", c->self);
360 FD_SET(c->sock, writeset);
361}
362
363void
Damien Millerb38eff82000-04-01 11:09:21 +1000364channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
365{
366 if (buffer_len(&c->input) < packet_get_maxsize())
367 FD_SET(c->sock, readset);
368 if (buffer_len(&c->output) > 0)
369 FD_SET(c->sock, writeset);
370}
371
372void
373channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
374{
375 /* test whether sockets are 'alive' for read/write */
376 if (c->istate == CHAN_INPUT_OPEN)
377 if (buffer_len(&c->input) < packet_get_maxsize())
378 FD_SET(c->sock, readset);
379 if (c->ostate == CHAN_OUTPUT_OPEN ||
380 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
381 if (buffer_len(&c->output) > 0) {
382 FD_SET(c->sock, writeset);
383 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
384 chan_obuf_empty(c);
385 }
386 }
387}
388
389void
Damien Miller33b13562000-04-04 14:38:59 +1000390channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
391{
392 if (c->istate == CHAN_INPUT_OPEN &&
393 c->remote_window > 0 &&
394 buffer_len(&c->input) < c->remote_window)
395 FD_SET(c->rfd, readset);
396 if (c->ostate == CHAN_OUTPUT_OPEN ||
397 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
398 if (buffer_len(&c->output) > 0) {
399 FD_SET(c->wfd, writeset);
400 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
401 chan_obuf_empty(c);
402 }
403 }
404 /** XXX check close conditions, too */
405 if (c->efd != -1) {
406 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
407 buffer_len(&c->extended) > 0)
408 FD_SET(c->efd, writeset);
409 else if (c->extended_usage == CHAN_EXTENDED_READ &&
410 buffer_len(&c->extended) < c->remote_window)
411 FD_SET(c->efd, readset);
412 }
413}
414
415void
Damien Millerb38eff82000-04-01 11:09:21 +1000416channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
417{
418 if (buffer_len(&c->input) == 0) {
419 packet_start(SSH_MSG_CHANNEL_CLOSE);
420 packet_put_int(c->remote_id);
421 packet_send();
422 c->type = SSH_CHANNEL_CLOSED;
Ben Lindstromc486d882001-04-11 16:08:34 +0000423 debug("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000424 }
425}
426
427void
428channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
429{
430 if (buffer_len(&c->output) == 0)
431 channel_free(c->self);
Damien Miller4af51302000-04-16 11:18:38 +1000432 else
Damien Millerb38eff82000-04-01 11:09:21 +1000433 FD_SET(c->sock, writeset);
434}
435
436/*
437 * This is a special state for X11 authentication spoofing. An opened X11
438 * connection (when authentication spoofing is being done) remains in this
439 * state until the first packet has been completely read. The authentication
440 * data in that packet is then substituted by the real data if it matches the
441 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000442 * XXX All this happens at the client side.
Damien Millerb38eff82000-04-01 11:09:21 +1000443 */
444int
445x11_open_helper(Channel *c)
446{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000447 u_char *ucp;
448 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000449
450 /* Check if the fixed size part of the packet is in buffer. */
451 if (buffer_len(&c->output) < 12)
452 return 0;
453
454 /* Parse the lengths of variable-length fields. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000455 ucp = (u_char *) buffer_ptr(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000456 if (ucp[0] == 0x42) { /* Byte order MSB first. */
457 proto_len = 256 * ucp[6] + ucp[7];
458 data_len = 256 * ucp[8] + ucp[9];
459 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
460 proto_len = ucp[6] + 256 * ucp[7];
461 data_len = ucp[8] + 256 * ucp[9];
462 } else {
463 debug("Initial X11 packet contains bad byte order byte: 0x%x",
464 ucp[0]);
465 return -1;
466 }
467
468 /* Check if the whole packet is in buffer. */
469 if (buffer_len(&c->output) <
470 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
471 return 0;
472
473 /* Check if authentication protocol matches. */
474 if (proto_len != strlen(x11_saved_proto) ||
475 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
476 debug("X11 connection uses different authentication protocol.");
477 return -1;
478 }
479 /* Check if authentication data matches our fake data. */
480 if (data_len != x11_fake_data_len ||
481 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
482 x11_fake_data, x11_fake_data_len) != 0) {
483 debug("X11 auth data does not match fake data.");
484 return -1;
485 }
486 /* Check fake data length */
487 if (x11_fake_data_len != x11_saved_data_len) {
488 error("X11 fake_data_len %d != saved_data_len %d",
489 x11_fake_data_len, x11_saved_data_len);
490 return -1;
491 }
492 /*
493 * Received authentication protocol and data match
494 * our fake data. Substitute the fake data with real
495 * data.
496 */
497 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
498 x11_saved_data, x11_saved_data_len);
499 return 1;
500}
501
502void
503channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
504{
505 int ret = x11_open_helper(c);
506 if (ret == 1) {
507 /* Start normal processing for the channel. */
508 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000509 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000510 } else if (ret == -1) {
511 /*
512 * We have received an X11 connection that has bad
513 * authentication information.
514 */
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000515 log("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000516 buffer_clear(&c->input);
517 buffer_clear(&c->output);
518 close(c->sock);
519 c->sock = -1;
520 c->type = SSH_CHANNEL_CLOSED;
521 packet_start(SSH_MSG_CHANNEL_CLOSE);
522 packet_put_int(c->remote_id);
523 packet_send();
524 }
525}
526
527void
Damien Millerbd483e72000-04-30 10:00:53 +1000528channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000529{
530 int ret = x11_open_helper(c);
531 if (ret == 1) {
532 c->type = SSH_CHANNEL_OPEN;
Damien Miller30c3d422000-05-09 11:02:59 +1000533 if (compat20)
534 channel_pre_open_20(c, readset, writeset);
535 else
536 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000537 } else if (ret == -1) {
538 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerbd483e72000-04-30 10:00:53 +1000539 chan_read_failed(c); /** force close? */
Damien Millerb38eff82000-04-01 11:09:21 +1000540 chan_write_failed(c);
541 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
542 }
543}
544
Ben Lindstromb3921512001-04-11 15:57:50 +0000545/* try to decode a socks4 header */
546int
547channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000548{
549 u_char *p, *host;
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000550 int len, have, i, found;
Ben Lindstromb3921512001-04-11 15:57:50 +0000551 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000552 struct {
553 u_int8_t version;
554 u_int8_t command;
555 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000556 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000557 } s4_req, s4_rsp;
558
Ben Lindstromb3921512001-04-11 15:57:50 +0000559 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000560
561 have = buffer_len(&c->input);
562 len = sizeof(s4_req);
563 if (have < len)
564 return 0;
565 p = buffer_ptr(&c->input);
566 for (found = 0, i = len; i < have; i++) {
567 if (p[i] == '\0') {
568 found = 1;
569 break;
570 }
571 if (i > 1024) {
572 /* the peer is probably sending garbage */
573 debug("channel %d: decode socks4: too long",
574 c->self);
575 return -1;
576 }
577 }
578 if (!found)
579 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000580 buffer_get(&c->input, (char *)&s4_req.version, 1);
581 buffer_get(&c->input, (char *)&s4_req.command, 1);
582 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000583 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000584 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000585 p = buffer_ptr(&c->input);
586 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000587 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000588 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000589 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000590 c->self, len, have);
591 strlcpy(username, p, sizeof(username));
592 buffer_consume(&c->input, len);
593 buffer_consume(&c->input, 1); /* trailing '\0' */
594
Ben Lindstromb3921512001-04-11 15:57:50 +0000595 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000596 strlcpy(c->path, host, sizeof(c->path));
597 c->host_port = ntohs(s4_req.dest_port);
598
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000599 debug("channel %d: dynamic request: socks4 host %s port %u command %u",
600 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000601
Ben Lindstromb3921512001-04-11 15:57:50 +0000602 if (s4_req.command != 1) {
603 debug("channel %d: cannot handle: socks4 cn %d",
604 c->self, s4_req.command);
605 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000606 }
Ben Lindstromb3921512001-04-11 15:57:50 +0000607 s4_rsp.version = 0; /* vn: 0 for reply */
608 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000609 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +0000610 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000611 buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +0000612 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000613}
614
Ben Lindstromb3921512001-04-11 15:57:50 +0000615/* dynamic port forwarding */
616void
617channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
618{
619 u_char *p;
620 int have, ret;
621
622 have = buffer_len(&c->input);
623
624 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +0000625 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +0000626 /* check if the fixed size part of the packet is in buffer. */
627 if (have < 4) {
628 /* need more */
629 FD_SET(c->sock, readset);
630 return;
631 }
632 /* try to guess the protocol */
633 p = buffer_ptr(&c->input);
634 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000635 case 0x04:
636 ret = channel_decode_socks4(c, readset, writeset);
637 break;
Ben Lindstromb3921512001-04-11 15:57:50 +0000638 default:
639 ret = -1;
640 break;
641 }
642 if (ret < 0) {
643 channel_free(c->self);
644 } else if (ret == 0) {
645 debug2("channel %d: pre_dynamic: need more", c->self);
646 /* need more */
647 FD_SET(c->sock, readset);
648 } else {
649 /* switch to the next state */
650 c->type = SSH_CHANNEL_OPENING;
651 port_open_helper(c, "direct-tcpip");
652 }
653}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000654
Damien Millerb38eff82000-04-01 11:09:21 +1000655/* This is our fake X11 server socket. */
656void
657channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
658{
659 struct sockaddr addr;
660 int newsock, newch;
661 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +1100662 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +1000663 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +1000664
665 if (FD_ISSET(c->sock, readset)) {
666 debug("X11 connection requested.");
667 addrlen = sizeof(addr);
668 newsock = accept(c->sock, &addr, &addrlen);
669 if (newsock < 0) {
670 error("accept: %.100s", strerror(errno));
671 return;
672 }
Damien Millerd83ff352001-01-30 09:19:34 +1100673 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +1000674 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +1000675 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +1100676 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +1000677
678 newch = channel_new("x11",
679 SSH_CHANNEL_OPENING, newsock, newsock, -1,
680 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100681 0, xstrdup(buf), 1);
Damien Millerbd483e72000-04-30 10:00:53 +1000682 if (compat20) {
683 packet_start(SSH2_MSG_CHANNEL_OPEN);
684 packet_put_cstring("x11");
685 packet_put_int(newch);
686 packet_put_int(c->local_window_max);
687 packet_put_int(c->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +1100688 /* originator ipaddr and port */
689 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +1000690 if (datafellows & SSH_BUG_X11FWD) {
691 debug("ssh2 x11 bug compat mode");
692 } else {
693 packet_put_int(remote_port);
694 }
Damien Millerbd483e72000-04-30 10:00:53 +1000695 packet_send();
696 } else {
697 packet_start(SSH_SMSG_X11_OPEN);
698 packet_put_int(newch);
699 if (have_hostname_in_open)
700 packet_put_string(buf, strlen(buf));
701 packet_send();
702 }
Damien Millerd83ff352001-01-30 09:19:34 +1100703 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +1000704 }
705}
706
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000707void
708port_open_helper(Channel *c, char *rtype)
709{
710 int direct;
711 char buf[1024];
712 char *remote_ipaddr = get_peer_ipaddr(c->sock);
713 u_short remote_port = get_peer_port(c->sock);
714
715 direct = (strcmp(rtype, "direct-tcpip") == 0);
716
717 snprintf(buf, sizeof buf,
718 "%s: listening port %d for %.100s port %d, "
719 "connect from %.200s port %d",
720 rtype, c->listening_port, c->path, c->host_port,
721 remote_ipaddr, remote_port);
722
723 xfree(c->remote_name);
724 c->remote_name = xstrdup(buf);
725
726 if (compat20) {
727 packet_start(SSH2_MSG_CHANNEL_OPEN);
728 packet_put_cstring(rtype);
729 packet_put_int(c->self);
730 packet_put_int(c->local_window_max);
731 packet_put_int(c->local_maxpacket);
732 if (direct) {
733 /* target host, port */
734 packet_put_cstring(c->path);
735 packet_put_int(c->host_port);
736 } else {
737 /* listen address, port */
738 packet_put_cstring(c->path);
739 packet_put_int(c->listening_port);
740 }
741 /* originator host and port */
742 packet_put_cstring(remote_ipaddr);
743 packet_put_int(remote_port);
744 packet_send();
745 } else {
746 packet_start(SSH_MSG_PORT_OPEN);
747 packet_put_int(c->self);
748 packet_put_cstring(c->path);
749 packet_put_int(c->host_port);
750 if (have_hostname_in_open)
751 packet_put_cstring(c->remote_name);
752 packet_send();
753 }
754 xfree(remote_ipaddr);
755}
756
Damien Millerb38eff82000-04-01 11:09:21 +1000757/*
758 * This socket is listening for connections to a forwarded TCP/IP port.
759 */
760void
761channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
762{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000763 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +1000764 struct sockaddr addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000765 int newsock, newch, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +1000766 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000767 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100768
Damien Millerb38eff82000-04-01 11:09:21 +1000769 if (FD_ISSET(c->sock, readset)) {
770 debug("Connection to port %d forwarding "
771 "to %.100s port %d requested.",
772 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000773
774 rtype = (c->type == SSH_CHANNEL_RPORT_LISTENER) ?
775 "forwarded-tcpip" : "direct-tcpip";
776 nextstate = (c->host_port == 0) ? SSH_CHANNEL_DYNAMIC :
777 SSH_CHANNEL_OPENING;
778
Damien Millerb38eff82000-04-01 11:09:21 +1000779 addrlen = sizeof(addr);
780 newsock = accept(c->sock, &addr, &addrlen);
781 if (newsock < 0) {
782 error("accept: %.100s", strerror(errno));
783 return;
784 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100785 newch = channel_new(rtype,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000786 nextstate, newsock, newsock, -1,
Damien Millerb38eff82000-04-01 11:09:21 +1000787 c->local_window_max, c->local_maxpacket,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000788 0, xstrdup(rtype), 1);
789
790 nc = channel_lookup(newch);
791 if (nc == NULL) {
792 error("xxx: no new channel:");
793 return;
Damien Millerb38eff82000-04-01 11:09:21 +1000794 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000795 nc->listening_port = c->listening_port;
796 nc->host_port = c->host_port;
797 strlcpy(nc->path, c->path, sizeof(nc->path));
798
799 if (nextstate != SSH_CHANNEL_DYNAMIC)
800 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +1000801 }
802}
803
804/*
805 * This is the authentication agent socket listening for connections from
806 * clients.
807 */
808void
809channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
810{
811 struct sockaddr addr;
812 int newsock, newch;
813 socklen_t addrlen;
814
815 if (FD_ISSET(c->sock, readset)) {
816 addrlen = sizeof(addr);
817 newsock = accept(c->sock, &addr, &addrlen);
818 if (newsock < 0) {
819 error("accept from auth socket: %.100s", strerror(errno));
820 return;
821 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100822 newch = channel_new("accepted auth socket",
823 SSH_CHANNEL_OPENING, newsock, newsock, -1,
824 c->local_window_max, c->local_maxpacket,
825 0, xstrdup("accepted auth socket"), 1);
826 if (compat20) {
827 packet_start(SSH2_MSG_CHANNEL_OPEN);
828 packet_put_cstring("auth-agent@openssh.com");
829 packet_put_int(newch);
830 packet_put_int(c->local_window_max);
831 packet_put_int(c->local_maxpacket);
832 } else {
833 packet_start(SSH_SMSG_AGENT_OPEN);
834 packet_put_int(newch);
835 }
Damien Millerb38eff82000-04-01 11:09:21 +1000836 packet_send();
837 }
838}
839
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000840void
841channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
842{
843 if (FD_ISSET(c->sock, writeset)) {
844 int err = 0;
845 int sz = sizeof(err);
846 c->type = SSH_CHANNEL_OPEN;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000847 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err, &sz) < 0) {
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000848 debug("getsockopt SO_ERROR failed");
849 } else {
850 if (err == 0) {
851 debug("channel %d: connected)", c->self);
852 } else {
853 debug("channel %d: not connected: %s",
854 c->self, strerror(err));
855 chan_read_failed(c);
856 chan_write_failed(c);
857 }
858 }
859 }
860}
861
Damien Millerb38eff82000-04-01 11:09:21 +1000862int
863channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
864{
865 char buf[16*1024];
866 int len;
867
868 if (c->rfd != -1 &&
869 FD_ISSET(c->rfd, readset)) {
870 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +1000871 if (len < 0 && (errno == EINTR || errno == EAGAIN))
872 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000873 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +1000874 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +1000875 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +0000876 if (c->type != SSH_CHANNEL_OPEN) {
877 debug("channel %d: not open", c->self);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000878 channel_free(c->self);
879 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +0000880 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000881 buffer_consume(&c->output, buffer_len(&c->output));
882 c->type = SSH_CHANNEL_INPUT_DRAINING;
Ben Lindstromc486d882001-04-11 16:08:34 +0000883 debug("channel %d: status set to input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000884 } else {
885 chan_read_failed(c);
886 }
887 return -1;
888 }
Damien Millerad833b32000-08-23 10:46:23 +1000889 if(c->input_filter != NULL) {
890 if (c->input_filter(c, buf, len) == -1) {
Ben Lindstromc486d882001-04-11 16:08:34 +0000891 debug("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +1000892 chan_read_failed(c);
893 }
894 } else {
895 buffer_append(&c->input, buf, len);
896 }
Damien Millerb38eff82000-04-01 11:09:21 +1000897 }
898 return 1;
899}
900int
901channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
902{
Ben Lindstrome229b252001-03-05 06:28:06 +0000903 struct termios tio;
Damien Millerb38eff82000-04-01 11:09:21 +1000904 int len;
905
906 /* Send buffered output data to the socket. */
907 if (c->wfd != -1 &&
908 FD_ISSET(c->wfd, writeset) &&
909 buffer_len(&c->output) > 0) {
910 len = write(c->wfd, buffer_ptr(&c->output),
Damien Miller6f83b8e2000-05-02 09:23:45 +1000911 buffer_len(&c->output));
912 if (len < 0 && (errno == EINTR || errno == EAGAIN))
913 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000914 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +0000915 if (c->type != SSH_CHANNEL_OPEN) {
916 debug("channel %d: not open", c->self);
917 channel_free(c->self);
918 return -1;
919 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000920 buffer_consume(&c->output, buffer_len(&c->output));
Ben Lindstromc486d882001-04-11 16:08:34 +0000921 debug("channel %d: status set to input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000922 c->type = SSH_CHANNEL_INPUT_DRAINING;
923 } else {
924 chan_write_failed(c);
925 }
926 return -1;
927 }
Damien Miller79438cc2001-02-16 12:34:57 +1100928 if (compat20 && c->isatty) {
Damien Miller79438cc2001-02-16 12:34:57 +1100929 if (tcgetattr(c->wfd, &tio) == 0 &&
930 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
931 /*
932 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +0000933 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +0000934 * size of a SSH2_MSG_CHANNEL_DATA message
935 * (4 byte channel id + data)
Damien Miller79438cc2001-02-16 12:34:57 +1100936 */
Ben Lindstrome229b252001-03-05 06:28:06 +0000937 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +1100938 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +1100939 }
940 }
Damien Millerb38eff82000-04-01 11:09:21 +1000941 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +1000942 if (compat20 && len > 0) {
943 c->local_consumed += len;
944 }
945 }
946 return 1;
947}
948int
949channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
950{
951 char buf[16*1024];
952 int len;
953
Damien Miller1383bd82000-04-06 12:32:37 +1000954/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +1000955 if (c->efd != -1) {
956 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
957 FD_ISSET(c->efd, writeset) &&
958 buffer_len(&c->extended) > 0) {
959 len = write(c->efd, buffer_ptr(&c->extended),
960 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +1100961 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000962 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000963 if (len < 0 && (errno == EINTR || errno == EAGAIN))
964 return 1;
965 if (len <= 0) {
966 debug2("channel %d: closing write-efd %d",
967 c->self, c->efd);
968 close(c->efd);
969 c->efd = -1;
970 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000971 buffer_consume(&c->extended, len);
972 c->local_consumed += len;
973 }
974 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
975 FD_ISSET(c->efd, readset)) {
976 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +1100977 debug2("channel %d: read %d from efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000978 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000979 if (len < 0 && (errno == EINTR || errno == EAGAIN))
980 return 1;
981 if (len <= 0) {
982 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +1000983 c->self, c->efd);
984 close(c->efd);
985 c->efd = -1;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000986 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000987 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000988 }
Damien Miller33b13562000-04-04 14:38:59 +1000989 }
990 }
991 return 1;
992}
993int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000994channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000995{
Ben Lindstromb3921512001-04-11 15:57:50 +0000996 if (c->type == SSH_CHANNEL_OPEN &&
997 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +1000998 c->local_window < c->local_window_max/2 &&
999 c->local_consumed > 0) {
1000 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1001 packet_put_int(c->remote_id);
1002 packet_put_int(c->local_consumed);
1003 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001004 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001005 c->self, c->local_window,
1006 c->local_consumed);
1007 c->local_window += c->local_consumed;
1008 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001009 }
1010 return 1;
1011}
1012
1013void
1014channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
1015{
1016 channel_handle_rfd(c, readset, writeset);
1017 channel_handle_wfd(c, readset, writeset);
1018}
1019
1020void
Damien Miller33b13562000-04-04 14:38:59 +10001021channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
1022{
1023 channel_handle_rfd(c, readset, writeset);
1024 channel_handle_wfd(c, readset, writeset);
1025 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001026
1027 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001028}
1029
1030void
Damien Millerb38eff82000-04-01 11:09:21 +10001031channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1032{
1033 int len;
1034 /* Send buffered output data to the socket. */
1035 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1036 len = write(c->sock, buffer_ptr(&c->output),
1037 buffer_len(&c->output));
1038 if (len <= 0)
1039 buffer_consume(&c->output, buffer_len(&c->output));
1040 else
1041 buffer_consume(&c->output, len);
1042 }
1043}
1044
1045void
Damien Miller33b13562000-04-04 14:38:59 +10001046channel_handler_init_20(void)
1047{
1048 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +10001049 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001050 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001051 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001052 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001053 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001054 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001055 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001056
1057 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
1058 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001059 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001060 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001061 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001062 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001063 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_2;
Damien Miller33b13562000-04-04 14:38:59 +10001064}
1065
1066void
Damien Millerb38eff82000-04-01 11:09:21 +10001067channel_handler_init_13(void)
1068{
1069 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1070 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1071 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1072 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1073 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1074 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1075 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001076 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001077 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001078
1079 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
1080 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1081 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1082 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1083 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001084 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001085 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001086}
1087
1088void
1089channel_handler_init_15(void)
1090{
1091 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +10001092 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001093 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1094 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1095 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001096 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001097 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001098
1099 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1100 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1101 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1102 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001103 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001104 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001105}
1106
1107void
1108channel_handler_init(void)
1109{
1110 int i;
1111 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1112 channel_pre[i] = NULL;
1113 channel_post[i] = NULL;
1114 }
Damien Miller33b13562000-04-04 14:38:59 +10001115 if (compat20)
1116 channel_handler_init_20();
1117 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001118 channel_handler_init_13();
1119 else
1120 channel_handler_init_15();
1121}
1122
Damien Miller4af51302000-04-16 11:18:38 +10001123void
Damien Millerb38eff82000-04-01 11:09:21 +10001124channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1125{
1126 static int did_init = 0;
1127 int i;
1128 Channel *c;
1129
1130 if (!did_init) {
1131 channel_handler_init();
1132 did_init = 1;
1133 }
1134 for (i = 0; i < channels_alloc; i++) {
1135 c = &channels[i];
1136 if (c->type == SSH_CHANNEL_FREE)
1137 continue;
1138 if (ftab[c->type] == NULL)
1139 continue;
1140 (*ftab[c->type])(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001141 if (chan_is_dead(c)) {
1142 /*
1143 * we have to remove the fd's from the select mask
1144 * before the channels are free'd and the fd's are
1145 * closed
1146 */
1147 if (c->wfd != -1)
1148 FD_CLR(c->wfd, writeset);
1149 if (c->rfd != -1)
1150 FD_CLR(c->rfd, readset);
1151 if (c->efd != -1) {
1152 if (c->extended_usage == CHAN_EXTENDED_READ)
1153 FD_CLR(c->efd, readset);
1154 if (c->extended_usage == CHAN_EXTENDED_WRITE)
1155 FD_CLR(c->efd, writeset);
1156 }
1157 channel_free(c->self);
1158 }
Damien Millerb38eff82000-04-01 11:09:21 +10001159 }
1160}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001161
Damien Miller4af51302000-04-16 11:18:38 +10001162void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001163channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1164 int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001165{
Damien Miller5e953212001-01-30 09:14:00 +11001166 int n;
1167 u_int sz;
1168
1169 n = MAX(*maxfdp, channel_max_fd);
1170
1171 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1172 if (*readsetp == NULL || n > *maxfdp) {
1173 if (*readsetp)
1174 xfree(*readsetp);
1175 if (*writesetp)
1176 xfree(*writesetp);
1177 *readsetp = xmalloc(sz);
1178 *writesetp = xmalloc(sz);
1179 *maxfdp = n;
1180 }
1181 memset(*readsetp, 0, sz);
1182 memset(*writesetp, 0, sz);
1183
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001184 if (!rekeying)
1185 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001186}
1187
Damien Miller4af51302000-04-16 11:18:38 +10001188void
Damien Miller95def091999-11-25 00:26:21 +11001189channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001190{
Damien Millerb38eff82000-04-01 11:09:21 +10001191 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192}
1193
Damien Miller5e953212001-01-30 09:14:00 +11001194/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001195
Damien Miller4af51302000-04-16 11:18:38 +10001196void
Damien Miller95def091999-11-25 00:26:21 +11001197channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001198{
Damien Miller95def091999-11-25 00:26:21 +11001199 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001200 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001201
Damien Miller95def091999-11-25 00:26:21 +11001202 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001203 c = &channels[i];
Damien Miller34132e52000-01-14 15:45:46 +11001204
Damien Miller5428f641999-11-25 11:54:57 +11001205 /* We are only interested in channels that can have buffered incoming data. */
Damien Miller34132e52000-01-14 15:45:46 +11001206 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001207 if (c->type != SSH_CHANNEL_OPEN &&
1208 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001209 continue;
1210 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001211 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001212 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001213 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001214 if (compat20 &&
1215 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001216 /* XXX is this true? */
Ben Lindstromb1131e92001-03-05 07:27:13 +00001217 debug2("channel %d: no data after CLOSE", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001218 continue;
1219 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001220
Damien Miller95def091999-11-25 00:26:21 +11001221 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001222 if ((c->istate == CHAN_INPUT_OPEN ||
1223 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1224 (len = buffer_len(&c->input)) > 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001225 /* Send some data for the other side over the secure connection. */
Damien Miller33b13562000-04-04 14:38:59 +10001226 if (compat20) {
1227 if (len > c->remote_window)
1228 len = c->remote_window;
1229 if (len > c->remote_maxpacket)
1230 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001231 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001232 if (packet_is_interactive()) {
1233 if (len > 1024)
1234 len = 512;
1235 } else {
1236 /* Keep the packets at reasonable size. */
1237 if (len > packet_get_maxsize()/2)
1238 len = packet_get_maxsize()/2;
1239 }
Damien Miller95def091999-11-25 00:26:21 +11001240 }
Damien Millerb38eff82000-04-01 11:09:21 +10001241 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001242 packet_start(compat20 ?
1243 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001244 packet_put_int(c->remote_id);
1245 packet_put_string(buffer_ptr(&c->input), len);
1246 packet_send();
1247 buffer_consume(&c->input, len);
1248 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001249 }
1250 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001251 if (compat13)
1252 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001253 /*
1254 * input-buffer is empty and read-socket shutdown:
1255 * tell peer, that we will not send more data: send IEOF
1256 */
Damien Millerb38eff82000-04-01 11:09:21 +10001257 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001258 }
Damien Miller33b13562000-04-04 14:38:59 +10001259 /* Send extended data, i.e. stderr */
1260 if (compat20 &&
1261 c->remote_window > 0 &&
1262 (len = buffer_len(&c->extended)) > 0 &&
1263 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001264 debug2("channel %d: rwin %d elen %d euse %d",
1265 c->self, c->remote_window, buffer_len(&c->extended),
1266 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001267 if (len > c->remote_window)
1268 len = c->remote_window;
1269 if (len > c->remote_maxpacket)
1270 len = c->remote_maxpacket;
1271 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1272 packet_put_int(c->remote_id);
1273 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1274 packet_put_string(buffer_ptr(&c->extended), len);
1275 packet_send();
1276 buffer_consume(&c->extended, len);
1277 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001278 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001279 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001280 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001281}
1282
Damien Miller5428f641999-11-25 11:54:57 +11001283/*
1284 * This is called when a packet of type CHANNEL_DATA has just been received.
1285 * The message type has already been consumed, but channel number and data is
1286 * still there.
1287 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001288
Damien Miller4af51302000-04-16 11:18:38 +10001289void
Damien Miller62cee002000-09-23 17:15:56 +11001290channel_input_data(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001291{
Damien Miller34132e52000-01-14 15:45:46 +11001292 int id;
Damien Miller95def091999-11-25 00:26:21 +11001293 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001294 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001295 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001296
Damien Miller95def091999-11-25 00:26:21 +11001297 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001298 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001299 c = channel_lookup(id);
1300 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001301 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001302
Damien Miller95def091999-11-25 00:26:21 +11001303 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001304 if (c->type != SSH_CHANNEL_OPEN &&
1305 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001306 return;
1307
1308 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001309 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001310 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001311
Damien Miller95def091999-11-25 00:26:21 +11001312 /* Get the data. */
1313 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001314 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001315
Damien Miller33b13562000-04-04 14:38:59 +10001316 if (compat20){
1317 if (data_len > c->local_maxpacket) {
1318 log("channel %d: rcvd big packet %d, maxpack %d",
1319 c->self, data_len, c->local_maxpacket);
1320 }
1321 if (data_len > c->local_window) {
1322 log("channel %d: rcvd too much data %d, win %d",
1323 c->self, data_len, c->local_window);
1324 xfree(data);
1325 return;
1326 }
1327 c->local_window -= data_len;
1328 }else{
1329 packet_integrity_check(plen, 4 + 4 + data_len, type);
1330 }
Damien Millerb38eff82000-04-01 11:09:21 +10001331 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001332 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001333}
Damien Miller4af51302000-04-16 11:18:38 +10001334void
Damien Miller62cee002000-09-23 17:15:56 +11001335channel_input_extended_data(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001336{
1337 int id;
1338 int tcode;
1339 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001340 u_int data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001341 Channel *c;
1342
1343 /* Get the channel number and verify it. */
1344 id = packet_get_int();
1345 c = channel_lookup(id);
1346
1347 if (c == NULL)
1348 packet_disconnect("Received extended_data for bad channel %d.", id);
1349 if (c->type != SSH_CHANNEL_OPEN) {
1350 log("channel %d: ext data for non open", id);
1351 return;
1352 }
1353 tcode = packet_get_int();
1354 if (c->efd == -1 ||
1355 c->extended_usage != CHAN_EXTENDED_WRITE ||
1356 tcode != SSH2_EXTENDED_DATA_STDERR) {
1357 log("channel %d: bad ext data", c->self);
1358 return;
1359 }
1360 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001361 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001362 if (data_len > c->local_window) {
1363 log("channel %d: rcvd too much extended_data %d, win %d",
1364 c->self, data_len, c->local_window);
1365 xfree(data);
1366 return;
1367 }
Damien Millerd3444942000-09-30 14:20:03 +11001368 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001369 c->local_window -= data_len;
1370 buffer_append(&c->extended, data, data_len);
1371 xfree(data);
1372}
1373
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001374
Damien Miller5428f641999-11-25 11:54:57 +11001375/*
1376 * Returns true if no channel has too much buffered data, and false if one or
1377 * more channel is overfull.
1378 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001379
Damien Miller4af51302000-04-16 11:18:38 +10001380int
Damien Miller95def091999-11-25 00:26:21 +11001381channel_not_very_much_buffered_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001382{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001383 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001384 Channel *c;
Damien Miller95def091999-11-25 00:26:21 +11001385
1386 for (i = 0; i < channels_alloc; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001387 c = &channels[i];
1388 if (c->type == SSH_CHANNEL_OPEN) {
Damien Miller33b13562000-04-04 14:38:59 +10001389 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
Damien Millerb38eff82000-04-01 11:09:21 +10001390 debug("channel %d: big input buffer %d",
1391 c->self, buffer_len(&c->input));
Damien Miller95def091999-11-25 00:26:21 +11001392 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001393 }
1394 if (buffer_len(&c->output) > packet_get_maxsize()) {
1395 debug("channel %d: big output buffer %d",
1396 c->self, buffer_len(&c->output));
Damien Miller95def091999-11-25 00:26:21 +11001397 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001398 }
Damien Miller95def091999-11-25 00:26:21 +11001399 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001400 }
Damien Miller95def091999-11-25 00:26:21 +11001401 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001402}
1403
Damien Miller4af51302000-04-16 11:18:38 +10001404void
Damien Miller62cee002000-09-23 17:15:56 +11001405channel_input_ieof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001406{
1407 int id;
1408 Channel *c;
1409
1410 packet_integrity_check(plen, 4, type);
1411
1412 id = packet_get_int();
1413 c = channel_lookup(id);
1414 if (c == NULL)
1415 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1416 chan_rcvd_ieof(c);
1417}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001418
Damien Miller4af51302000-04-16 11:18:38 +10001419void
Damien Miller62cee002000-09-23 17:15:56 +11001420channel_input_close(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001421{
Damien Millerb38eff82000-04-01 11:09:21 +10001422 int id;
1423 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001424
Damien Millerb38eff82000-04-01 11:09:21 +10001425 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001426
Damien Millerb38eff82000-04-01 11:09:21 +10001427 id = packet_get_int();
1428 c = channel_lookup(id);
1429 if (c == NULL)
1430 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001431
1432 /*
1433 * Send a confirmation that we have closed the channel and no more
1434 * data is coming for it.
1435 */
Damien Miller95def091999-11-25 00:26:21 +11001436 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001437 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001438 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001439
Damien Miller5428f641999-11-25 11:54:57 +11001440 /*
1441 * If the channel is in closed state, we have sent a close request,
1442 * and the other side will eventually respond with a confirmation.
1443 * Thus, we cannot free the channel here, because then there would be
1444 * no-one to receive the confirmation. The channel gets freed when
1445 * the confirmation arrives.
1446 */
Damien Millerb38eff82000-04-01 11:09:21 +10001447 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001448 /*
1449 * Not a closed channel - mark it as draining, which will
1450 * cause it to be freed later.
1451 */
Damien Millerb38eff82000-04-01 11:09:21 +10001452 buffer_consume(&c->input, buffer_len(&c->input));
1453 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001454 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001455}
1456
Damien Millerb38eff82000-04-01 11:09:21 +10001457/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001458void
Damien Miller62cee002000-09-23 17:15:56 +11001459channel_input_oclose(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001460{
Damien Millerb38eff82000-04-01 11:09:21 +10001461 int id = packet_get_int();
1462 Channel *c = channel_lookup(id);
1463 packet_integrity_check(plen, 4, type);
1464 if (c == NULL)
1465 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1466 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001467}
1468
Damien Miller4af51302000-04-16 11:18:38 +10001469void
Damien Miller62cee002000-09-23 17:15:56 +11001470channel_input_close_confirmation(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001471{
1472 int id = packet_get_int();
1473 Channel *c = channel_lookup(id);
1474
Damien Miller4af51302000-04-16 11:18:38 +10001475 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001476 if (c == NULL)
1477 packet_disconnect("Received close confirmation for "
1478 "out-of-range channel %d.", id);
1479 if (c->type != SSH_CHANNEL_CLOSED)
1480 packet_disconnect("Received close confirmation for "
1481 "non-closed channel %d (type %d).", id, c->type);
1482 channel_free(c->self);
1483}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001484
Damien Miller4af51302000-04-16 11:18:38 +10001485void
Damien Miller62cee002000-09-23 17:15:56 +11001486channel_input_open_confirmation(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001487{
Damien Millerb38eff82000-04-01 11:09:21 +10001488 int id, remote_id;
1489 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001490
Damien Miller33b13562000-04-04 14:38:59 +10001491 if (!compat20)
1492 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001493
Damien Millerb38eff82000-04-01 11:09:21 +10001494 id = packet_get_int();
1495 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001496
Damien Millerb38eff82000-04-01 11:09:21 +10001497 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1498 packet_disconnect("Received open confirmation for "
1499 "non-opening channel %d.", id);
1500 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001501 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001502 c->remote_id = remote_id;
1503 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001504
1505 if (compat20) {
1506 c->remote_window = packet_get_int();
1507 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001508 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001509 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001510 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001511 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001512 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001513 }
1514 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1515 c->remote_window, c->remote_maxpacket);
1516 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001517}
1518
Damien Miller4af51302000-04-16 11:18:38 +10001519void
Damien Miller62cee002000-09-23 17:15:56 +11001520channel_input_open_failure(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001521{
Kevin Steves12057502001-02-05 14:54:34 +00001522 int id, reason;
1523 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001524 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001525
Damien Miller33b13562000-04-04 14:38:59 +10001526 if (!compat20)
1527 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001528
1529 id = packet_get_int();
1530 c = channel_lookup(id);
1531
1532 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1533 packet_disconnect("Received open failure for "
1534 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001535 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00001536 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00001537 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00001538 msg = packet_get_string(NULL);
1539 lang = packet_get_string(NULL);
1540 }
Damien Miller4af51302000-04-16 11:18:38 +10001541 packet_done();
Kevin Steves12057502001-02-05 14:54:34 +00001542 log("channel_open_failure: %d: reason %d %s", id,
1543 reason, msg ? msg : "<no additional info>");
1544 if (msg != NULL)
1545 xfree(msg);
1546 if (lang != NULL)
1547 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001548 }
Damien Miller95def091999-11-25 00:26:21 +11001549 /* Free the channel. This will also close the socket. */
Damien Millerb38eff82000-04-01 11:09:21 +10001550 channel_free(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001551}
1552
Damien Miller33b13562000-04-04 14:38:59 +10001553void
Damien Miller62cee002000-09-23 17:15:56 +11001554channel_input_channel_request(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001555{
1556 int id;
1557 Channel *c;
1558
1559 id = packet_get_int();
1560 c = channel_lookup(id);
1561
1562 if (c == NULL ||
1563 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1564 packet_disconnect("Received request for "
1565 "non-open channel %d.", id);
1566 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001567 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001568 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001569 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001570 } else {
1571 char *service = packet_get_string(NULL);
Ben Lindstromcc74df72001-03-05 06:20:14 +00001572 debug("channel %d: rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001573 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001574 xfree(service);
1575 }
1576}
1577
Damien Miller4af51302000-04-16 11:18:38 +10001578void
Damien Miller62cee002000-09-23 17:15:56 +11001579channel_input_window_adjust(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001580{
1581 Channel *c;
1582 int id, adjust;
1583
1584 if (!compat20)
1585 return;
1586
1587 /* Get the channel number and verify it. */
1588 id = packet_get_int();
1589 c = channel_lookup(id);
1590
1591 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1592 log("Received window adjust for "
1593 "non-open channel %d.", id);
1594 return;
1595 }
1596 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001597 packet_done();
Damien Millerd3444942000-09-30 14:20:03 +11001598 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10001599 c->remote_window += adjust;
1600}
1601
Damien Miller5428f641999-11-25 11:54:57 +11001602/*
1603 * Stops listening for channels, and removes any unix domain sockets that we
1604 * might have.
1605 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001606
Damien Miller4af51302000-04-16 11:18:38 +10001607void
Damien Miller95def091999-11-25 00:26:21 +11001608channel_stop_listening()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001609{
Damien Miller95def091999-11-25 00:26:21 +11001610 int i;
1611 for (i = 0; i < channels_alloc; i++) {
1612 switch (channels[i].type) {
1613 case SSH_CHANNEL_AUTH_SOCKET:
1614 close(channels[i].sock);
Damien Miller78315eb2000-09-29 23:01:36 +11001615 unlink(channels[i].path);
Damien Miller95def091999-11-25 00:26:21 +11001616 channel_free(i);
1617 break;
1618 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001619 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001620 case SSH_CHANNEL_X11_LISTENER:
1621 close(channels[i].sock);
1622 channel_free(i);
1623 break;
1624 default:
1625 break;
1626 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001627 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001628}
1629
Damien Miller5428f641999-11-25 11:54:57 +11001630/*
Damien Miller6f83b8e2000-05-02 09:23:45 +10001631 * Closes the sockets/fds of all channels. This is used to close extra file
Damien Miller5428f641999-11-25 11:54:57 +11001632 * descriptors after a fork.
1633 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001634
Damien Miller4af51302000-04-16 11:18:38 +10001635void
Damien Miller95def091999-11-25 00:26:21 +11001636channel_close_all()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001637{
Damien Miller95def091999-11-25 00:26:21 +11001638 int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +10001639 for (i = 0; i < channels_alloc; i++)
Damien Miller95def091999-11-25 00:26:21 +11001640 if (channels[i].type != SSH_CHANNEL_FREE)
Damien Miller6f83b8e2000-05-02 09:23:45 +10001641 channel_close_fds(&channels[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001642}
1643
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001644/* Returns true if any channel is still open. */
1645
Damien Miller4af51302000-04-16 11:18:38 +10001646int
Damien Miller95def091999-11-25 00:26:21 +11001647channel_still_open()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001648{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001649 u_int i;
Damien Miller95def091999-11-25 00:26:21 +11001650 for (i = 0; i < channels_alloc; i++)
1651 switch (channels[i].type) {
1652 case SSH_CHANNEL_FREE:
1653 case SSH_CHANNEL_X11_LISTENER:
1654 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001655 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001656 case SSH_CHANNEL_CLOSED:
1657 case SSH_CHANNEL_AUTH_SOCKET:
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001658 case SSH_CHANNEL_DYNAMIC:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001659 case SSH_CHANNEL_CONNECTING: /* XXX ??? */
Damien Miller95def091999-11-25 00:26:21 +11001660 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001661 case SSH_CHANNEL_LARVAL:
1662 if (!compat20)
1663 fatal("cannot happen: SSH_CHANNEL_LARVAL");
1664 continue;
Damien Miller95def091999-11-25 00:26:21 +11001665 case SSH_CHANNEL_OPENING:
1666 case SSH_CHANNEL_OPEN:
1667 case SSH_CHANNEL_X11_OPEN:
1668 return 1;
1669 case SSH_CHANNEL_INPUT_DRAINING:
1670 case SSH_CHANNEL_OUTPUT_DRAINING:
1671 if (!compat13)
1672 fatal("cannot happen: OUT_DRAIN");
1673 return 1;
1674 default:
1675 fatal("channel_still_open: bad channel type %d", channels[i].type);
1676 /* NOTREACHED */
1677 }
1678 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001679}
1680
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001681/* Returns the id of an open channel suitable for keepaliving */
1682
1683int
1684channel_find_open()
1685{
1686 u_int i;
1687 for (i = 0; i < channels_alloc; i++)
1688 switch (channels[i].type) {
1689 case SSH_CHANNEL_CLOSED:
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001690 case SSH_CHANNEL_DYNAMIC:
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001691 case SSH_CHANNEL_FREE:
1692 case SSH_CHANNEL_X11_LISTENER:
1693 case SSH_CHANNEL_PORT_LISTENER:
1694 case SSH_CHANNEL_RPORT_LISTENER:
1695 case SSH_CHANNEL_OPENING:
Ben Lindstromd334b272001-04-14 23:08:36 +00001696 continue;
1697 case SSH_CHANNEL_LARVAL:
1698 case SSH_CHANNEL_AUTH_SOCKET:
1699 case SSH_CHANNEL_CONNECTING: /* XXX ??? */
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001700 case SSH_CHANNEL_OPEN:
1701 case SSH_CHANNEL_X11_OPEN:
1702 return i;
1703 case SSH_CHANNEL_INPUT_DRAINING:
1704 case SSH_CHANNEL_OUTPUT_DRAINING:
1705 if (!compat13)
1706 fatal("cannot happen: OUT_DRAIN");
1707 return i;
1708 default:
1709 fatal("channel_find_open: bad channel type %d", channels[i].type);
1710 /* NOTREACHED */
1711 }
1712 return -1;
1713}
1714
1715
Damien Miller5428f641999-11-25 11:54:57 +11001716/*
1717 * Returns a message describing the currently open forwarded connections,
1718 * suitable for sending to the client. The message contains crlf pairs for
1719 * newlines.
1720 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001721
Damien Miller95def091999-11-25 00:26:21 +11001722char *
1723channel_open_message()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001724{
Damien Miller95def091999-11-25 00:26:21 +11001725 Buffer buffer;
1726 int i;
1727 char buf[512], *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001728
Damien Miller95def091999-11-25 00:26:21 +11001729 buffer_init(&buffer);
1730 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001731 buffer_append(&buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001732 for (i = 0; i < channels_alloc; i++) {
1733 Channel *c = &channels[i];
1734 switch (c->type) {
1735 case SSH_CHANNEL_FREE:
1736 case SSH_CHANNEL_X11_LISTENER:
1737 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001738 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001739 case SSH_CHANNEL_CLOSED:
1740 case SSH_CHANNEL_AUTH_SOCKET:
1741 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001742 case SSH_CHANNEL_LARVAL:
Damien Miller95def091999-11-25 00:26:21 +11001743 case SSH_CHANNEL_OPENING:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001744 case SSH_CHANNEL_CONNECTING:
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001745 case SSH_CHANNEL_DYNAMIC:
Damien Miller95def091999-11-25 00:26:21 +11001746 case SSH_CHANNEL_OPEN:
1747 case SSH_CHANNEL_X11_OPEN:
1748 case SSH_CHANNEL_INPUT_DRAINING:
1749 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Millerb38eff82000-04-01 11:09:21 +10001750 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 +11001751 c->self, c->remote_name,
1752 c->type, c->remote_id,
1753 c->istate, buffer_len(&c->input),
Damien Millerb38eff82000-04-01 11:09:21 +10001754 c->ostate, buffer_len(&c->output),
1755 c->rfd, c->wfd);
Damien Miller95def091999-11-25 00:26:21 +11001756 buffer_append(&buffer, buf, strlen(buf));
1757 continue;
1758 default:
Damien Millerb38eff82000-04-01 11:09:21 +10001759 fatal("channel_open_message: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001760 /* NOTREACHED */
1761 }
1762 }
1763 buffer_append(&buffer, "\0", 1);
1764 cp = xstrdup(buffer_ptr(&buffer));
1765 buffer_free(&buffer);
1766 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001767}
1768
Damien Miller5428f641999-11-25 11:54:57 +11001769/*
1770 * Initiate forwarding of connections to local port "port" through the secure
1771 * channel to host:port from remote side.
1772 */
Kevin Steves12057502001-02-05 14:54:34 +00001773int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001774channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
1775 u_short port_to_connect, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001776{
Kevin Steves12057502001-02-05 14:54:34 +00001777 return channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +11001778 NULL, listen_port,
1779 host_to_connect, port_to_connect,
1780 gateway_ports, /*remote_fwd*/ 0);
1781}
1782
1783/*
1784 * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
1785 * (SSH_CHANNEL_RPORT_LISTENER).
1786 */
Kevin Steves12057502001-02-05 14:54:34 +00001787int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001788channel_request_forwarding(
1789 const char *listen_address, u_short listen_port,
1790 const char *host_to_connect, u_short port_to_connect,
1791 int gateway_ports, int remote_fwd)
1792{
1793 int success, ch, sock, on = 1, ctype;
Damien Miller34132e52000-01-14 15:45:46 +11001794 struct addrinfo hints, *ai, *aitop;
1795 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0bc1bd82000-11-13 22:57:25 +11001796 const char *host;
Damien Miller5428f641999-11-25 11:54:57 +11001797 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001798
Kevin Steves12057502001-02-05 14:54:34 +00001799 success = 0;
1800
Damien Miller0bc1bd82000-11-13 22:57:25 +11001801 if (remote_fwd) {
1802 host = listen_address;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001803 ctype = SSH_CHANNEL_RPORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001804 } else {
1805 host = host_to_connect;
1806 ctype =SSH_CHANNEL_PORT_LISTENER;
1807 }
1808
Kevin Steves12057502001-02-05 14:54:34 +00001809 if (strlen(host) > sizeof(channels[0].path) - 1) {
1810 error("Forward host name too long.");
1811 return success;
1812 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001813
Damien Miller0bc1bd82000-11-13 22:57:25 +11001814 /* XXX listen_address is currently ignored */
Damien Miller5428f641999-11-25 11:54:57 +11001815 /*
Damien Miller34132e52000-01-14 15:45:46 +11001816 * getaddrinfo returns a loopback address if the hostname is
1817 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11001818 */
Damien Miller34132e52000-01-14 15:45:46 +11001819 memset(&hints, 0, sizeof(hints));
1820 hints.ai_family = IPv4or6;
1821 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
1822 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001823 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001824 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
1825 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11001826
Damien Miller34132e52000-01-14 15:45:46 +11001827 for (ai = aitop; ai; ai = ai->ai_next) {
1828 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1829 continue;
1830 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1831 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001832 error("channel_request_forwarding: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001833 continue;
1834 }
1835 /* Create a port to listen for the host. */
1836 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1837 if (sock < 0) {
1838 /* this is no error since kernel may not support ipv6 */
1839 verbose("socket: %.100s", strerror(errno));
1840 continue;
1841 }
1842 /*
1843 * Set socket options. We would like the socket to disappear
1844 * as soon as it has been closed for whatever reason.
1845 */
1846 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
1847 linger.l_onoff = 1;
1848 linger.l_linger = 5;
1849 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
1850 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001851
Damien Miller34132e52000-01-14 15:45:46 +11001852 /* Bind the socket to the address. */
1853 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1854 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11001855 if (!ai->ai_next)
1856 error("bind: %.100s", strerror(errno));
1857 else
1858 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001859
Damien Miller34132e52000-01-14 15:45:46 +11001860 close(sock);
1861 continue;
1862 }
1863 /* Start listening for connections on the socket. */
1864 if (listen(sock, 5) < 0) {
1865 error("listen: %.100s", strerror(errno));
1866 close(sock);
1867 continue;
1868 }
1869 /* Allocate a channel number for the socket. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001870 ch = channel_new("port listener", ctype, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10001871 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11001872 0, xstrdup("port listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11001873 strlcpy(channels[ch].path, host, sizeof(channels[ch].path));
Damien Miller0bc1bd82000-11-13 22:57:25 +11001874 channels[ch].host_port = port_to_connect;
1875 channels[ch].listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11001876 success = 1;
1877 }
1878 if (success == 0)
Kevin Steves12057502001-02-05 14:54:34 +00001879 error("channel_request_forwarding: cannot listen to port: %d",
1880 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001881 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00001882 return success;
Damien Miller95def091999-11-25 00:26:21 +11001883}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001884
Damien Miller5428f641999-11-25 11:54:57 +11001885/*
1886 * Initiate forwarding of connections to port "port" on remote host through
1887 * the secure channel to host:port from local side.
1888 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001889
Damien Miller4af51302000-04-16 11:18:38 +10001890void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001891channel_request_remote_forwarding(u_short listen_port,
1892 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001893{
Damien Miller0bc1bd82000-11-13 22:57:25 +11001894 int payload_len, type, success = 0;
1895
Damien Miller95def091999-11-25 00:26:21 +11001896 /* Record locally that connection to this host/port is permitted. */
1897 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1898 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001899
Damien Miller95def091999-11-25 00:26:21 +11001900 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10001901 if (compat20) {
1902 const char *address_to_bind = "0.0.0.0";
1903 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1904 packet_put_cstring("tcpip-forward");
1905 packet_put_char(0); /* boolean: want reply */
1906 packet_put_cstring(address_to_bind);
1907 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001908 packet_send();
1909 packet_write_wait();
1910 /* Assume that server accepts the request */
1911 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10001912 } else {
1913 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10001914 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10001915 packet_put_cstring(host_to_connect);
1916 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10001917 packet_send();
1918 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001919
1920 /* Wait for response from the remote side. */
1921 type = packet_read(&payload_len);
1922 switch (type) {
1923 case SSH_SMSG_SUCCESS:
1924 success = 1;
1925 break;
1926 case SSH_SMSG_FAILURE:
1927 log("Warning: Server denied remote port forwarding.");
1928 break;
1929 default:
1930 /* Unknown packet */
1931 packet_disconnect("Protocol error for port forward request:"
1932 "received packet type %d.", type);
1933 }
1934 }
1935 if (success) {
1936 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
1937 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
1938 permitted_opens[num_permitted_opens].listen_port = listen_port;
1939 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10001940 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001941}
1942
Damien Miller5428f641999-11-25 11:54:57 +11001943/*
1944 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
1945 * listening for the port, and sends back a success reply (or disconnect
1946 * message if there was an error). This never returns if there was an error.
1947 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001948
Damien Miller4af51302000-04-16 11:18:38 +10001949void
Damien Millere247cc42000-05-07 12:03:14 +10001950channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001951{
Damien Milleraae6c611999-12-06 11:47:28 +11001952 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11001953 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001954
Damien Miller95def091999-11-25 00:26:21 +11001955 /* Get arguments from the packet. */
1956 port = packet_get_int();
1957 hostname = packet_get_string(NULL);
1958 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001959
Damien Millerbac2d8a2000-09-05 16:13:06 +11001960#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11001961 /*
1962 * Check that an unprivileged user is not trying to forward a
1963 * privileged port.
1964 */
Damien Miller95def091999-11-25 00:26:21 +11001965 if (port < IPPORT_RESERVED && !is_root)
1966 packet_disconnect("Requested forwarding of port %d but user is not root.",
1967 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001968#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001969 /* Initiate forwarding */
Damien Millere247cc42000-05-07 12:03:14 +10001970 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11001971
1972 /* Free the argument string. */
1973 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001974}
1975
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001976/*
1977 * Permits opening to any host/port if permitted_opens[] is empty. This is
1978 * usually called by the server, because the user could connect to any port
1979 * anyway, and the server has no way to know but to trust the client anyway.
1980 */
1981void
1982channel_permit_all_opens()
1983{
1984 if (num_permitted_opens == 0)
1985 all_opens_permitted = 1;
1986}
1987
Ben Lindstroma3700052001-04-05 23:26:32 +00001988void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00001989channel_add_permitted_opens(char *host, int port)
1990{
1991 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1992 fatal("channel_request_remote_forwarding: too many forwards");
1993 debug("allow port forwarding to host %s port %d", host, port);
1994
1995 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
1996 permitted_opens[num_permitted_opens].port_to_connect = port;
1997 num_permitted_opens++;
1998
1999 all_opens_permitted = 0;
2000}
2001
Ben Lindstroma3700052001-04-05 23:26:32 +00002002void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002003channel_clear_permitted_opens(void)
2004{
2005 int i;
2006
2007 for (i = 0; i < num_permitted_opens; i++)
2008 xfree(permitted_opens[i].host_to_connect);
2009 num_permitted_opens = 0;
2010
2011}
2012
2013
2014/* return socket to remote host, port */
Damien Millerb38eff82000-04-01 11:09:21 +10002015int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002016connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002017{
Damien Miller34132e52000-01-14 15:45:46 +11002018 struct addrinfo hints, *ai, *aitop;
2019 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2020 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002021 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002022
Damien Miller34132e52000-01-14 15:45:46 +11002023 memset(&hints, 0, sizeof(hints));
2024 hints.ai_family = IPv4or6;
2025 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002026 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002027 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002028 error("connect_to %.100s: unknown host (%s)", host,
2029 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002030 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002031 }
Damien Miller34132e52000-01-14 15:45:46 +11002032 for (ai = aitop; ai; ai = ai->ai_next) {
2033 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2034 continue;
2035 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2036 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002037 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002038 continue;
2039 }
Damien Miller34132e52000-01-14 15:45:46 +11002040 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2041 if (sock < 0) {
2042 error("socket: %.100s", strerror(errno));
2043 continue;
2044 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +00002045 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002046 fatal("connect_to: F_SETFL: %s", strerror(errno));
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002047 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2048 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002049 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002050 strerror(errno));
2051 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002052 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002053 }
2054 break; /* success */
2055
2056 }
2057 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002058 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002059 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002060 return -1;
2061 }
2062 /* success */
2063 return sock;
2064}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002065
Damien Miller0bc1bd82000-11-13 22:57:25 +11002066int
2067channel_connect_by_listen_adress(u_short listen_port)
2068{
2069 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002070
Damien Miller0bc1bd82000-11-13 22:57:25 +11002071 for (i = 0; i < num_permitted_opens; i++)
2072 if (permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002073 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002074 permitted_opens[i].host_to_connect,
2075 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002076 error("WARNING: Server requests forwarding for unknown listen_port %d",
2077 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002078 return -1;
2079}
Damien Millerb38eff82000-04-01 11:09:21 +10002080
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002081/* Check if connecting to that port is permitted and connect. */
2082int
2083channel_connect_to(const char *host, u_short port)
2084{
2085 int i, permit;
2086
2087 permit = all_opens_permitted;
2088 if (!permit) {
2089 for (i = 0; i < num_permitted_opens; i++)
2090 if (permitted_opens[i].port_to_connect == port &&
2091 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2092 permit = 1;
2093
2094 }
2095 if (!permit) {
2096 log("Received request to connect to host %.100s port %d, "
2097 "but the request was denied.", host, port);
2098 return -1;
2099 }
2100 return connect_to(host, port);
2101}
2102
Damien Millerb38eff82000-04-01 11:09:21 +10002103/*
2104 * This is called after receiving PORT_OPEN message. This attempts to
2105 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
2106 * or CHANNEL_OPEN_FAILURE.
2107 */
2108
Damien Miller4af51302000-04-16 11:18:38 +10002109void
Damien Miller62cee002000-09-23 17:15:56 +11002110channel_input_port_open(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002111{
2112 u_short host_port;
2113 char *host, *originator_string;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002114 int remote_channel, sock = -1, newch;
Damien Millerb38eff82000-04-01 11:09:21 +10002115
Damien Millerb38eff82000-04-01 11:09:21 +10002116 remote_channel = packet_get_int();
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002117 host = packet_get_string(NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10002118 host_port = packet_get_int();
2119
Damien Millerb38eff82000-04-01 11:09:21 +10002120 if (have_hostname_in_open) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002121 originator_string = packet_get_string(NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10002122 } else {
2123 originator_string = xstrdup("unknown (remote did not supply name)");
Damien Miller95def091999-11-25 00:26:21 +11002124 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002125 packet_done();
2126 sock = channel_connect_to(host, host_port);
2127 if (sock != -1) {
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002128 newch = channel_allocate(SSH_CHANNEL_CONNECTING,
2129 sock, originator_string);
Damien Millerb38eff82000-04-01 11:09:21 +10002130 channels[newch].remote_id = remote_channel;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002131
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002132 /*XXX delay answer? */
Damien Millerb38eff82000-04-01 11:09:21 +10002133 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2134 packet_put_int(remote_channel);
2135 packet_put_int(newch);
2136 packet_send();
2137 } else {
2138 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2139 packet_put_int(remote_channel);
2140 packet_send();
2141 }
Damien Miller95def091999-11-25 00:26:21 +11002142 xfree(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002143}
2144
Damien Miller5428f641999-11-25 11:54:57 +11002145/*
2146 * Creates an internet domain socket for listening for X11 connections.
2147 * Returns a suitable value for the DISPLAY variable, or NULL if an error
2148 * occurs.
2149 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002150
Damien Miller34132e52000-01-14 15:45:46 +11002151#define NUM_SOCKS 10
2152
Damien Miller95def091999-11-25 00:26:21 +11002153char *
Damien Millera34a28b1999-12-14 10:47:15 +11002154x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002155{
Damien Milleraae6c611999-12-06 11:47:28 +11002156 int display_number, sock;
2157 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002158 struct addrinfo hints, *ai, *aitop;
2159 char strport[NI_MAXSERV];
2160 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
2161 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11002162 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002163
Damien Millera34a28b1999-12-14 10:47:15 +11002164 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11002165 display_number < MAX_DISPLAYS;
2166 display_number++) {
2167 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002168 memset(&hints, 0, sizeof(hints));
2169 hints.ai_family = IPv4or6;
2170 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
2171 hints.ai_socktype = SOCK_STREAM;
2172 snprintf(strport, sizeof strport, "%d", port);
2173 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2174 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11002175 return NULL;
2176 }
Damien Miller34132e52000-01-14 15:45:46 +11002177 for (ai = aitop; ai; ai = ai->ai_next) {
2178 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2179 continue;
2180 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2181 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002182 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002183 error("socket: %.100s", strerror(errno));
2184 return NULL;
2185 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002186 debug("x11_create_display_inet: Socket family %d not supported",
2187 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002188 continue;
2189 }
Damien Miller34132e52000-01-14 15:45:46 +11002190 }
2191 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2192 debug("bind port %d: %.100s", port, strerror(errno));
2193 shutdown(sock, SHUT_RDWR);
2194 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002195
2196 if (ai->ai_next)
2197 continue;
2198
Damien Miller34132e52000-01-14 15:45:46 +11002199 for (n = 0; n < num_socks; n++) {
2200 shutdown(socks[n], SHUT_RDWR);
2201 close(socks[n]);
2202 }
2203 num_socks = 0;
2204 break;
2205 }
2206 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002207#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002208 if (num_socks == NUM_SOCKS)
2209 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002210#else
2211 break;
2212#endif
Damien Miller95def091999-11-25 00:26:21 +11002213 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002214 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002215 if (num_socks > 0)
2216 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002217 }
Damien Miller95def091999-11-25 00:26:21 +11002218 if (display_number >= MAX_DISPLAYS) {
2219 error("Failed to allocate internet-domain X11 display socket.");
2220 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002221 }
Damien Miller95def091999-11-25 00:26:21 +11002222 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002223 for (n = 0; n < num_socks; n++) {
2224 sock = socks[n];
2225 if (listen(sock, 5) < 0) {
2226 error("listen: %.100s", strerror(errno));
2227 shutdown(sock, SHUT_RDWR);
2228 close(sock);
2229 return NULL;
2230 }
Damien Miller95def091999-11-25 00:26:21 +11002231 }
Damien Miller34132e52000-01-14 15:45:46 +11002232
Damien Miller95def091999-11-25 00:26:21 +11002233 /* Set up a suitable value for the DISPLAY variable. */
2234 if (gethostname(hostname, sizeof(hostname)) < 0)
2235 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11002236
2237#ifdef IPADDR_IN_DISPLAY
Kevin Stevesef4eea92001-02-05 12:42:17 +00002238 /*
Damien Miller76112de1999-12-21 11:18:08 +11002239 * HPUX detects the local hostname in the DISPLAY variable and tries
2240 * to set up a shared memory connection to the server, which it
2241 * incorrectly supposes to be local.
2242 *
2243 * The workaround - as used in later $$H and other programs - is
2244 * is to set display to the host's IP address.
2245 */
2246 {
2247 struct hostent *he;
2248 struct in_addr my_addr;
2249
2250 he = gethostbyname(hostname);
2251 if (he == NULL) {
2252 error("[X11-broken-fwd-hostname-workaround] Could not get "
2253 "IP address for hostname %s.", hostname);
2254
2255 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
2256 "Could not get IP address for hostname %s.", hostname);
2257
2258 shutdown(sock, SHUT_RDWR);
2259 close(sock);
2260
2261 return NULL;
2262 }
2263
2264 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2265
2266 /* Set DISPLAY to <ip address>:screen.display */
Kevin Stevesef4eea92001-02-05 12:42:17 +00002267 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Kevin Stevesda6cd592000-12-29 18:41:21 +00002268 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002269 }
2270#else /* IPADDR_IN_DISPLAY */
2271 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11002272 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Kevin Stevesda6cd592000-12-29 18:41:21 +00002273 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002274#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002275
Damien Miller34132e52000-01-14 15:45:46 +11002276 /* Allocate a channel for each socket. */
2277 for (n = 0; n < num_socks; n++) {
2278 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10002279 (void) channel_new("x11 listener",
2280 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2281 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002282 0, xstrdup("X11 inet listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11002283 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002284
Damien Miller95def091999-11-25 00:26:21 +11002285 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11002286 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002287}
2288
2289#ifndef X_UNIX_PATH
2290#define X_UNIX_PATH "/tmp/.X11-unix/X"
2291#endif
2292
2293static
2294int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002295connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002296{
Damien Miller95def091999-11-25 00:26:21 +11002297 static const char *const x_sockets[] = {
2298 X_UNIX_PATH "%u",
2299 "/var/X/.X11-unix/X" "%u",
2300 "/usr/spool/sockets/X11/" "%u",
2301 NULL
2302 };
2303 int sock;
2304 struct sockaddr_un addr;
2305 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002306
Damien Miller95def091999-11-25 00:26:21 +11002307 for (path = x_sockets; *path; ++path) {
2308 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2309 if (sock < 0)
2310 error("socket: %.100s", strerror(errno));
2311 memset(&addr, 0, sizeof(addr));
2312 addr.sun_family = AF_UNIX;
2313 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
2314 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2315 return sock;
2316 close(sock);
2317 }
2318 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2319 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002320}
2321
Damien Millerbd483e72000-04-30 10:00:53 +10002322int
2323x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002324{
Damien Millerbd483e72000-04-30 10:00:53 +10002325 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002326 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002327 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002328 struct addrinfo hints, *ai, *aitop;
2329 char strport[NI_MAXSERV];
2330 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002331
Damien Miller95def091999-11-25 00:26:21 +11002332 /* Try to open a socket for the local X server. */
2333 display = getenv("DISPLAY");
2334 if (!display) {
2335 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002336 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002337 }
Damien Miller5428f641999-11-25 11:54:57 +11002338 /*
2339 * Now we decode the value of the DISPLAY variable and make a
2340 * connection to the real X server.
2341 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002342
Damien Miller5428f641999-11-25 11:54:57 +11002343 /*
2344 * Check if it is a unix domain socket. Unix domain displays are in
2345 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2346 */
Damien Miller95def091999-11-25 00:26:21 +11002347 if (strncmp(display, "unix:", 5) == 0 ||
2348 display[0] == ':') {
2349 /* Connect to the unix domain socket. */
2350 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2351 error("Could not parse display number from DISPLAY: %.100s",
2352 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002353 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002354 }
2355 /* Create a socket. */
2356 sock = connect_local_xsocket(display_number);
2357 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002358 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002359
2360 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002361 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002362 }
Damien Miller5428f641999-11-25 11:54:57 +11002363 /*
2364 * Connect to an inet socket. The DISPLAY value is supposedly
2365 * hostname:d[.s], where hostname may also be numeric IP address.
2366 */
Damien Miller95def091999-11-25 00:26:21 +11002367 strncpy(buf, display, sizeof(buf));
2368 buf[sizeof(buf) - 1] = 0;
2369 cp = strchr(buf, ':');
2370 if (!cp) {
2371 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002372 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002373 }
Damien Miller95def091999-11-25 00:26:21 +11002374 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002375 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002376 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2377 error("Could not parse display number from DISPLAY: %.100s",
2378 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002379 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002380 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002381
Damien Miller34132e52000-01-14 15:45:46 +11002382 /* Look up the host address */
2383 memset(&hints, 0, sizeof(hints));
2384 hints.ai_family = IPv4or6;
2385 hints.ai_socktype = SOCK_STREAM;
2386 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2387 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2388 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002389 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002390 }
Damien Miller34132e52000-01-14 15:45:46 +11002391 for (ai = aitop; ai; ai = ai->ai_next) {
2392 /* Create a socket. */
2393 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2394 if (sock < 0) {
2395 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002396 continue;
2397 }
2398 /* Connect it to the display. */
2399 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2400 debug("connect %.100s port %d: %.100s", buf,
2401 6000 + display_number, strerror(errno));
2402 close(sock);
2403 continue;
2404 }
2405 /* Success */
2406 break;
Damien Miller34132e52000-01-14 15:45:46 +11002407 }
Damien Miller34132e52000-01-14 15:45:46 +11002408 freeaddrinfo(aitop);
2409 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002410 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002411 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002412 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002413 }
Damien Millerbd483e72000-04-30 10:00:53 +10002414 return sock;
2415}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002416
Damien Millerbd483e72000-04-30 10:00:53 +10002417/*
2418 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2419 * the remote channel number. We should do whatever we want, and respond
2420 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2421 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002422
Damien Millerbd483e72000-04-30 10:00:53 +10002423void
Damien Miller62cee002000-09-23 17:15:56 +11002424x11_input_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002425{
2426 int remote_channel, sock = 0, newch;
2427 char *remote_host;
Ben Lindstrom46c16222000-12-22 01:43:59 +00002428 u_int remote_len;
Damien Miller95def091999-11-25 00:26:21 +11002429
Damien Millerbd483e72000-04-30 10:00:53 +10002430 /* Get remote channel number. */
2431 remote_channel = packet_get_int();
Damien Miller95def091999-11-25 00:26:21 +11002432
Damien Millerbd483e72000-04-30 10:00:53 +10002433 /* Get remote originator name. */
2434 if (have_hostname_in_open) {
2435 remote_host = packet_get_string(&remote_len);
2436 remote_len += 4;
2437 } else {
2438 remote_host = xstrdup("unknown (remote did not supply name)");
2439 remote_len = 0;
2440 }
2441
2442 debug("Received X11 open request.");
2443 packet_integrity_check(plen, 4 + remote_len, SSH_SMSG_X11_OPEN);
2444
2445 /* Obtain a connection to the real X display. */
2446 sock = x11_connect_display();
2447 if (sock == -1) {
2448 /* Send refusal to the remote host. */
2449 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2450 packet_put_int(remote_channel);
2451 packet_send();
2452 } else {
2453 /* Allocate a channel for this connection. */
2454 newch = channel_allocate(
2455 (x11_saved_proto == NULL) ?
2456 SSH_CHANNEL_OPEN : SSH_CHANNEL_X11_OPEN,
2457 sock, remote_host);
2458 channels[newch].remote_id = remote_channel;
2459
2460 /* Send a confirmation to the remote host. */
2461 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2462 packet_put_int(remote_channel);
2463 packet_put_int(newch);
2464 packet_send();
2465 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002466}
2467
Damien Miller69b69aa2000-10-28 14:19:58 +11002468/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2469void
2470deny_input_open(int type, int plen, void *ctxt)
2471{
2472 int rchan = packet_get_int();
2473 switch(type){
2474 case SSH_SMSG_AGENT_OPEN:
2475 error("Warning: ssh server tried agent forwarding.");
2476 break;
2477 case SSH_SMSG_X11_OPEN:
2478 error("Warning: ssh server tried X11 forwarding.");
2479 break;
2480 default:
2481 error("deny_input_open: type %d plen %d", type, plen);
2482 break;
2483 }
2484 error("Warning: this is probably a break in attempt by a malicious server.");
2485 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2486 packet_put_int(rchan);
2487 packet_send();
2488}
2489
Damien Miller5428f641999-11-25 11:54:57 +11002490/*
2491 * Requests forwarding of X11 connections, generates fake authentication
2492 * data, and enables authentication spoofing.
2493 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002494
Damien Miller4af51302000-04-16 11:18:38 +10002495void
Damien Millerbd483e72000-04-30 10:00:53 +10002496x11_request_forwarding_with_spoofing(int client_session_id,
2497 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002498{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002499 u_int data_len = (u_int) strlen(data) / 2;
Ben Lindstromb3211a82001-02-10 22:33:19 +00002500 u_int i, value, len;
Damien Miller95def091999-11-25 00:26:21 +11002501 char *new_data;
2502 int screen_number;
2503 const char *cp;
2504 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002505
Damien Miller95def091999-11-25 00:26:21 +11002506 cp = getenv("DISPLAY");
2507 if (cp)
2508 cp = strchr(cp, ':');
2509 if (cp)
2510 cp = strchr(cp, '.');
2511 if (cp)
2512 screen_number = atoi(cp + 1);
2513 else
2514 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002515
Damien Miller95def091999-11-25 00:26:21 +11002516 /* Save protocol name. */
2517 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002518
Damien Miller5428f641999-11-25 11:54:57 +11002519 /*
2520 * Extract real authentication data and generate fake data of the
2521 * same length.
2522 */
Damien Miller95def091999-11-25 00:26:21 +11002523 x11_saved_data = xmalloc(data_len);
2524 x11_fake_data = xmalloc(data_len);
2525 for (i = 0; i < data_len; i++) {
2526 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2527 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2528 if (i % 4 == 0)
2529 rand = arc4random();
2530 x11_saved_data[i] = value;
2531 x11_fake_data[i] = rand & 0xff;
2532 rand >>= 8;
2533 }
2534 x11_saved_data_len = data_len;
2535 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002536
Damien Miller95def091999-11-25 00:26:21 +11002537 /* Convert the fake data into hex. */
Ben Lindstromb3211a82001-02-10 22:33:19 +00002538 len = 2 * data_len + 1;
2539 new_data = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +11002540 for (i = 0; i < data_len; i++)
Ben Lindstromb3211a82001-02-10 22:33:19 +00002541 snprintf(new_data + 2 * i, len - 2 * i,
2542 "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002543
Damien Miller95def091999-11-25 00:26:21 +11002544 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002545 if (compat20) {
2546 channel_request_start(client_session_id, "x11-req", 0);
2547 packet_put_char(0); /* XXX bool single connection */
2548 } else {
2549 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2550 }
2551 packet_put_cstring(proto);
2552 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002553 packet_put_int(screen_number);
2554 packet_send();
2555 packet_write_wait();
2556 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002557}
2558
2559/* Sends a message to the server to request authentication fd forwarding. */
2560
Damien Miller4af51302000-04-16 11:18:38 +10002561void
Damien Miller95def091999-11-25 00:26:21 +11002562auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002563{
Damien Miller95def091999-11-25 00:26:21 +11002564 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2565 packet_send();
2566 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002567}
2568
Damien Miller5428f641999-11-25 11:54:57 +11002569/*
2570 * Returns the name of the forwarded authentication socket. Returns NULL if
2571 * there is no forwarded authentication socket. The returned value points to
2572 * a static buffer.
2573 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002574
Damien Miller95def091999-11-25 00:26:21 +11002575char *
2576auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002577{
Damien Miller95def091999-11-25 00:26:21 +11002578 return channel_forwarded_auth_socket_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002579}
2580
2581/* removes the agent forwarding socket */
2582
Damien Miller4af51302000-04-16 11:18:38 +10002583void
Damien Miller95def091999-11-25 00:26:21 +11002584cleanup_socket(void)
2585{
Damien Miller78315eb2000-09-29 23:01:36 +11002586 unlink(channel_forwarded_auth_socket_name);
Damien Miller95def091999-11-25 00:26:21 +11002587 rmdir(channel_forwarded_auth_socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002588}
2589
Damien Miller5428f641999-11-25 11:54:57 +11002590/*
Damien Millerd3a18572000-06-07 19:55:44 +10002591 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002592 * This starts forwarding authentication requests.
2593 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002594
Damien Millerd3a18572000-06-07 19:55:44 +10002595int
Damien Miller95def091999-11-25 00:26:21 +11002596auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002597{
Damien Miller95def091999-11-25 00:26:21 +11002598 int sock, newch;
2599 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002600
Damien Miller95def091999-11-25 00:26:21 +11002601 if (auth_get_socket_name() != NULL)
2602 fatal("Protocol error: authentication forwarding requested twice.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002603
Damien Miller95def091999-11-25 00:26:21 +11002604 /* Temporarily drop privileged uid for mkdir/bind. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00002605 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002606
Damien Miller95def091999-11-25 00:26:21 +11002607 /* Allocate a buffer for the socket name, and format the name. */
2608 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
2609 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
2610 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002611
Damien Miller95def091999-11-25 00:26:21 +11002612 /* Create private directory for socket */
Damien Millerd3a18572000-06-07 19:55:44 +10002613 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL) {
2614 packet_send_debug("Agent forwarding disabled: mkdtemp() failed: %.100s",
2615 strerror(errno));
2616 restore_uid();
2617 xfree(channel_forwarded_auth_socket_name);
2618 xfree(channel_forwarded_auth_socket_dir);
2619 channel_forwarded_auth_socket_name = NULL;
2620 channel_forwarded_auth_socket_dir = NULL;
2621 return 0;
2622 }
Damien Miller95def091999-11-25 00:26:21 +11002623 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
2624 channel_forwarded_auth_socket_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002625
Damien Miller95def091999-11-25 00:26:21 +11002626 if (atexit(cleanup_socket) < 0) {
2627 int saved = errno;
2628 cleanup_socket();
2629 packet_disconnect("socket: %.100s", strerror(saved));
2630 }
2631 /* Create the socket. */
2632 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2633 if (sock < 0)
2634 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002635
Damien Miller95def091999-11-25 00:26:21 +11002636 /* Bind it to the name. */
2637 memset(&sunaddr, 0, sizeof(sunaddr));
2638 sunaddr.sun_family = AF_UNIX;
2639 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
2640 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002641
Damien Miller95def091999-11-25 00:26:21 +11002642 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2643 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002644
Damien Miller95def091999-11-25 00:26:21 +11002645 /* Restore the privileged uid. */
2646 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002647
Damien Miller95def091999-11-25 00:26:21 +11002648 /* Start listening on the socket. */
2649 if (listen(sock, 5) < 0)
2650 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002651
Damien Miller95def091999-11-25 00:26:21 +11002652 /* Allocate a channel for the authentication agent socket. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002653 newch = channel_new("auth socket",
2654 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2655 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2656 0, xstrdup("auth socket"), 1);
2657
Damien Miller037a0dc1999-12-07 15:38:31 +11002658 strlcpy(channels[newch].path, channel_forwarded_auth_socket_name,
2659 sizeof(channels[newch].path));
Damien Millerd3a18572000-06-07 19:55:44 +10002660 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002661}
2662
2663/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2664
Damien Miller4af51302000-04-16 11:18:38 +10002665void
Damien Miller62cee002000-09-23 17:15:56 +11002666auth_input_open_request(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002667{
Damien Miller95def091999-11-25 00:26:21 +11002668 int remch, sock, newch;
2669 char *dummyname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002670
Damien Millerb38eff82000-04-01 11:09:21 +10002671 packet_integrity_check(plen, 4, type);
2672
Damien Miller95def091999-11-25 00:26:21 +11002673 /* Read the remote channel number from the message. */
2674 remch = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002675
Damien Miller5428f641999-11-25 11:54:57 +11002676 /*
2677 * Get a connection to the local authentication agent (this may again
2678 * get forwarded).
2679 */
Damien Miller95def091999-11-25 00:26:21 +11002680 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002681
Damien Miller5428f641999-11-25 11:54:57 +11002682 /*
2683 * If we could not connect the agent, send an error message back to
2684 * the server. This should never happen unless the agent dies,
2685 * because authentication forwarding is only enabled if we have an
2686 * agent.
2687 */
Damien Miller95def091999-11-25 00:26:21 +11002688 if (sock < 0) {
2689 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2690 packet_put_int(remch);
2691 packet_send();
2692 return;
2693 }
2694 debug("Forwarding authentication connection.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002695
Damien Miller5428f641999-11-25 11:54:57 +11002696 /*
2697 * Dummy host name. This will be freed when the channel is freed; it
2698 * will still be valid in the packet_put_string below since the
2699 * channel cannot yet be freed at that point.
2700 */
Damien Miller95def091999-11-25 00:26:21 +11002701 dummyname = xstrdup("authentication agent connection");
2702
2703 newch = channel_allocate(SSH_CHANNEL_OPEN, sock, dummyname);
2704 channels[newch].remote_id = remch;
2705
2706 /* Send a confirmation to the remote host. */
2707 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2708 packet_put_int(remch);
2709 packet_put_int(newch);
2710 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002711}
Damien Miller33b13562000-04-04 14:38:59 +10002712
2713void
Damien Millerbd483e72000-04-30 10:00:53 +10002714channel_start_open(int id)
Damien Miller33b13562000-04-04 14:38:59 +10002715{
2716 Channel *c = channel_lookup(id);
2717 if (c == NULL) {
2718 log("channel_open: %d: bad id", id);
2719 return;
2720 }
Damien Millerbd483e72000-04-30 10:00:53 +10002721 debug("send channel open %d", id);
Damien Miller33b13562000-04-04 14:38:59 +10002722 packet_start(SSH2_MSG_CHANNEL_OPEN);
2723 packet_put_cstring(c->ctype);
2724 packet_put_int(c->self);
2725 packet_put_int(c->local_window);
2726 packet_put_int(c->local_maxpacket);
Damien Millerbd483e72000-04-30 10:00:53 +10002727}
2728void
2729channel_open(int id)
2730{
2731 /* XXX REMOVE ME */
2732 channel_start_open(id);
Damien Miller33b13562000-04-04 14:38:59 +10002733 packet_send();
Damien Miller33b13562000-04-04 14:38:59 +10002734}
2735void
2736channel_request(int id, char *service, int wantconfirm)
2737{
2738 channel_request_start(id, service, wantconfirm);
2739 packet_send();
2740 debug("channel request %d: %s", id, service) ;
2741}
2742void
2743channel_request_start(int id, char *service, int wantconfirm)
2744{
2745 Channel *c = channel_lookup(id);
2746 if (c == NULL) {
2747 log("channel_request: %d: bad id", id);
2748 return;
2749 }
2750 packet_start(SSH2_MSG_CHANNEL_REQUEST);
2751 packet_put_int(c->remote_id);
2752 packet_put_cstring(service);
2753 packet_put_char(wantconfirm);
2754}
2755void
2756channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
2757{
2758 Channel *c = channel_lookup(id);
2759 if (c == NULL) {
2760 log("channel_register_callback: %d: bad id", id);
2761 return;
2762 }
2763 c->cb_event = mtype;
2764 c->cb_fn = fn;
2765 c->cb_arg = arg;
2766}
2767void
2768channel_register_cleanup(int id, channel_callback_fn *fn)
2769{
2770 Channel *c = channel_lookup(id);
2771 if (c == NULL) {
2772 log("channel_register_cleanup: %d: bad id", id);
2773 return;
2774 }
2775 c->dettach_user = fn;
2776}
2777void
2778channel_cancel_cleanup(int id)
2779{
2780 Channel *c = channel_lookup(id);
2781 if (c == NULL) {
2782 log("channel_cancel_cleanup: %d: bad id", id);
2783 return;
2784 }
2785 c->dettach_user = NULL;
2786}
Kevin Stevesef4eea92001-02-05 12:42:17 +00002787void
Damien Millerad833b32000-08-23 10:46:23 +10002788channel_register_filter(int id, channel_filter_fn *fn)
2789{
2790 Channel *c = channel_lookup(id);
2791 if (c == NULL) {
2792 log("channel_register_filter: %d: bad id", id);
2793 return;
2794 }
2795 c->input_filter = fn;
2796}
Damien Miller33b13562000-04-04 14:38:59 +10002797
2798void
Damien Miller69b69aa2000-10-28 14:19:58 +11002799channel_set_fds(int id, int rfd, int wfd, int efd,
2800 int extusage, int nonblock)
Damien Miller33b13562000-04-04 14:38:59 +10002801{
2802 Channel *c = channel_lookup(id);
2803 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
2804 fatal("channel_activate for non-larval channel %d.", id);
Damien Miller69b69aa2000-10-28 14:19:58 +11002805 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller33b13562000-04-04 14:38:59 +10002806 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002807 /* XXX window size? */
Damien Millere4340be2000-09-16 13:29:08 +11002808 c->local_window = c->local_window_max = c->local_maxpacket * 2;
Damien Miller33b13562000-04-04 14:38:59 +10002809 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2810 packet_put_int(c->remote_id);
2811 packet_put_int(c->local_window);
2812 packet_send();
2813}