blob: 9397d48e5aeb964e5bddf9b0df1358144a29055d [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 Lindstrom6d618462001-05-10 23:24:49 +000043RCSID("$OpenBSD: channels.c,v 1.115 2001/05/09 22:51:57 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 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000075static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076
Damien Miller5428f641999-11-25 11:54:57 +110077/*
78 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000079 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110080 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081static int channels_alloc = 0;
82
Damien Miller5428f641999-11-25 11:54:57 +110083/*
84 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +000085 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +110086 */
Damien Miller5e953212001-01-30 09:14:00 +110087static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
89/* Name and directory of socket for authentication agent forwarding. */
90static char *channel_forwarded_auth_socket_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +110091static char *channel_forwarded_auth_socket_dir = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
93/* Saved X11 authentication protocol name. */
94char *x11_saved_proto = NULL;
95
96/* Saved X11 authentication data. This is the real data. */
97char *x11_saved_data = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +000098u_int x11_saved_data_len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * Fake X11 authentication data. This is what the server will be sending us;
102 * we should replace any occurrences of this by the real data.
103 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104char *x11_fake_data = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000105u_int x11_fake_data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Miller5428f641999-11-25 11:54:57 +1100107/*
108 * Data structure for storing which hosts are permitted for forward requests.
109 * The local sides of any remote forwards are stored in this array to prevent
110 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
111 * network (which might be behind a firewall).
112 */
Damien Miller95def091999-11-25 00:26:21 +1100113typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000114 char *host_to_connect; /* Connect to 'host'. */
115 u_short port_to_connect; /* Connect to 'port'. */
116 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117} ForwardPermission;
118
119/* List of all permitted host/port pairs to connect. */
120static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
121/* Number of permitted host/port pairs in the array. */
122static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +1100123/*
124 * If this is true, all opens are permitted. This is the case on the server
125 * on which we have to trust the client anyway, and the user could do
126 * anything after logging in anyway.
127 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128static int all_opens_permitted = 0;
129
130/* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */
131static int have_hostname_in_open = 0;
132
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000133/* AF_UNSPEC or AF_INET or AF_INET6 */
134extern int IPv4or6;
135
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000136void port_open_helper(Channel *c, char *rtype);
137
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138/* Sets specific protocol options. */
139
Damien Miller4af51302000-04-16 11:18:38 +1000140void
Damien Miller95def091999-11-25 00:26:21 +1100141channel_set_options(int hostname_in_open)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142{
Damien Miller95def091999-11-25 00:26:21 +1100143 have_hostname_in_open = hostname_in_open;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144}
145
Damien Millerb38eff82000-04-01 11:09:21 +1000146/* lookup channel by id */
147
148Channel *
149channel_lookup(int id)
150{
151 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000152
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 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000157 c = channels[id];
158 if (c == NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +1000159 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
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000215Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000216channel_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;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000226 channels = xmalloc(channels_alloc * sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100227 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000228 channels[i] = NULL;
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++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000237 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100238 /* 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);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000247 channels = xrealloc(channels, channels_alloc * sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100248 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000249 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100250 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000251 /* Initialize and return new channel. */
252 c = channels[found] = xmalloc(sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100253 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);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000275 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000277
278/* Close all channel fd/socket. */
279
280void
281channel_close_fds(Channel *c)
282{
283 if (c->sock != -1) {
284 close(c->sock);
285 c->sock = -1;
286 }
287 if (c->rfd != -1) {
288 close(c->rfd);
289 c->rfd = -1;
290 }
291 if (c->wfd != -1) {
292 close(c->wfd);
293 c->wfd = -1;
294 }
295 if (c->efd != -1) {
296 close(c->efd);
297 c->efd = -1;
298 }
299}
300
301/* Free the channel and close its fd/socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302
Damien Miller4af51302000-04-16 11:18:38 +1000303void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000304channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000306 char *s;
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000307
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000308 s = channel_open_message();
309 debug("channel_free: channel %d: status: %s", c->self, s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000310 xfree(s);
311
Damien Miller33b13562000-04-04 14:38:59 +1000312 if (c->dettach_user != NULL) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000313 debug("channel_free: channel %d: dettaching channel user", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000314 c->dettach_user(c->self, NULL);
315 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000316 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000317 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000318 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000319 buffer_free(&c->input);
320 buffer_free(&c->output);
321 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000322 if (c->remote_name) {
323 xfree(c->remote_name);
324 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100325 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000326 channels[c->self] = NULL;
327 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328}
329
Damien Miller5428f641999-11-25 11:54:57 +1100330/*
Damien Millerb38eff82000-04-01 11:09:21 +1000331 * 'channel_pre*' are called just before select() to add any bits relevant to
332 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100333 */
Damien Millerb38eff82000-04-01 11:09:21 +1000334/*
335 * 'channel_post*': perform any appropriate operations for channels which
336 * have events pending.
337 */
338typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
339chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
340chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
341
342void
343channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
344{
345 FD_SET(c->sock, readset);
346}
347
348void
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000349channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
350{
351 debug3("channel %d: waiting for connection", c->self);
352 FD_SET(c->sock, writeset);
353}
354
355void
Damien Millerb38eff82000-04-01 11:09:21 +1000356channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
357{
358 if (buffer_len(&c->input) < packet_get_maxsize())
359 FD_SET(c->sock, readset);
360 if (buffer_len(&c->output) > 0)
361 FD_SET(c->sock, writeset);
362}
363
364void
365channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
366{
367 /* test whether sockets are 'alive' for read/write */
368 if (c->istate == CHAN_INPUT_OPEN)
369 if (buffer_len(&c->input) < packet_get_maxsize())
370 FD_SET(c->sock, readset);
371 if (c->ostate == CHAN_OUTPUT_OPEN ||
372 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
373 if (buffer_len(&c->output) > 0) {
374 FD_SET(c->sock, writeset);
375 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
376 chan_obuf_empty(c);
377 }
378 }
379}
380
381void
Damien Miller33b13562000-04-04 14:38:59 +1000382channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
383{
384 if (c->istate == CHAN_INPUT_OPEN &&
385 c->remote_window > 0 &&
386 buffer_len(&c->input) < c->remote_window)
387 FD_SET(c->rfd, readset);
388 if (c->ostate == CHAN_OUTPUT_OPEN ||
389 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
390 if (buffer_len(&c->output) > 0) {
391 FD_SET(c->wfd, writeset);
392 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
393 chan_obuf_empty(c);
394 }
395 }
396 /** XXX check close conditions, too */
397 if (c->efd != -1) {
398 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
399 buffer_len(&c->extended) > 0)
400 FD_SET(c->efd, writeset);
401 else if (c->extended_usage == CHAN_EXTENDED_READ &&
402 buffer_len(&c->extended) < c->remote_window)
403 FD_SET(c->efd, readset);
404 }
405}
406
407void
Damien Millerb38eff82000-04-01 11:09:21 +1000408channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
409{
410 if (buffer_len(&c->input) == 0) {
411 packet_start(SSH_MSG_CHANNEL_CLOSE);
412 packet_put_int(c->remote_id);
413 packet_send();
414 c->type = SSH_CHANNEL_CLOSED;
Ben Lindstromc486d882001-04-11 16:08:34 +0000415 debug("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000416 }
417}
418
419void
420channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
421{
422 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000423 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000424 else
Damien Millerb38eff82000-04-01 11:09:21 +1000425 FD_SET(c->sock, writeset);
426}
427
428/*
429 * This is a special state for X11 authentication spoofing. An opened X11
430 * connection (when authentication spoofing is being done) remains in this
431 * state until the first packet has been completely read. The authentication
432 * data in that packet is then substituted by the real data if it matches the
433 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000434 * XXX All this happens at the client side.
Damien Millerb38eff82000-04-01 11:09:21 +1000435 */
436int
437x11_open_helper(Channel *c)
438{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000439 u_char *ucp;
440 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000441
442 /* Check if the fixed size part of the packet is in buffer. */
443 if (buffer_len(&c->output) < 12)
444 return 0;
445
446 /* Parse the lengths of variable-length fields. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000447 ucp = (u_char *) buffer_ptr(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000448 if (ucp[0] == 0x42) { /* Byte order MSB first. */
449 proto_len = 256 * ucp[6] + ucp[7];
450 data_len = 256 * ucp[8] + ucp[9];
451 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
452 proto_len = ucp[6] + 256 * ucp[7];
453 data_len = ucp[8] + 256 * ucp[9];
454 } else {
455 debug("Initial X11 packet contains bad byte order byte: 0x%x",
456 ucp[0]);
457 return -1;
458 }
459
460 /* Check if the whole packet is in buffer. */
461 if (buffer_len(&c->output) <
462 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
463 return 0;
464
465 /* Check if authentication protocol matches. */
466 if (proto_len != strlen(x11_saved_proto) ||
467 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
468 debug("X11 connection uses different authentication protocol.");
469 return -1;
470 }
471 /* Check if authentication data matches our fake data. */
472 if (data_len != x11_fake_data_len ||
473 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
474 x11_fake_data, x11_fake_data_len) != 0) {
475 debug("X11 auth data does not match fake data.");
476 return -1;
477 }
478 /* Check fake data length */
479 if (x11_fake_data_len != x11_saved_data_len) {
480 error("X11 fake_data_len %d != saved_data_len %d",
481 x11_fake_data_len, x11_saved_data_len);
482 return -1;
483 }
484 /*
485 * Received authentication protocol and data match
486 * our fake data. Substitute the fake data with real
487 * data.
488 */
489 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
490 x11_saved_data, x11_saved_data_len);
491 return 1;
492}
493
494void
495channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
496{
497 int ret = x11_open_helper(c);
498 if (ret == 1) {
499 /* Start normal processing for the channel. */
500 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000501 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000502 } else if (ret == -1) {
503 /*
504 * We have received an X11 connection that has bad
505 * authentication information.
506 */
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000507 log("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000508 buffer_clear(&c->input);
509 buffer_clear(&c->output);
510 close(c->sock);
511 c->sock = -1;
512 c->type = SSH_CHANNEL_CLOSED;
513 packet_start(SSH_MSG_CHANNEL_CLOSE);
514 packet_put_int(c->remote_id);
515 packet_send();
516 }
517}
518
519void
Damien Millerbd483e72000-04-30 10:00:53 +1000520channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000521{
522 int ret = x11_open_helper(c);
523 if (ret == 1) {
524 c->type = SSH_CHANNEL_OPEN;
Damien Miller30c3d422000-05-09 11:02:59 +1000525 if (compat20)
526 channel_pre_open_20(c, readset, writeset);
527 else
528 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000529 } else if (ret == -1) {
530 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerbd483e72000-04-30 10:00:53 +1000531 chan_read_failed(c); /** force close? */
Damien Millerb38eff82000-04-01 11:09:21 +1000532 chan_write_failed(c);
533 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
534 }
535}
536
Ben Lindstromb3921512001-04-11 15:57:50 +0000537/* try to decode a socks4 header */
538int
539channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000540{
541 u_char *p, *host;
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000542 int len, have, i, found;
Ben Lindstromb3921512001-04-11 15:57:50 +0000543 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000544 struct {
545 u_int8_t version;
546 u_int8_t command;
547 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000548 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000549 } s4_req, s4_rsp;
550
Ben Lindstromb3921512001-04-11 15:57:50 +0000551 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000552
553 have = buffer_len(&c->input);
554 len = sizeof(s4_req);
555 if (have < len)
556 return 0;
557 p = buffer_ptr(&c->input);
558 for (found = 0, i = len; i < have; i++) {
559 if (p[i] == '\0') {
560 found = 1;
561 break;
562 }
563 if (i > 1024) {
564 /* the peer is probably sending garbage */
565 debug("channel %d: decode socks4: too long",
566 c->self);
567 return -1;
568 }
569 }
570 if (!found)
571 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000572 buffer_get(&c->input, (char *)&s4_req.version, 1);
573 buffer_get(&c->input, (char *)&s4_req.command, 1);
574 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000575 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000576 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000577 p = buffer_ptr(&c->input);
578 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000579 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000580 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000581 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000582 c->self, len, have);
583 strlcpy(username, p, sizeof(username));
584 buffer_consume(&c->input, len);
585 buffer_consume(&c->input, 1); /* trailing '\0' */
586
Ben Lindstromb3921512001-04-11 15:57:50 +0000587 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000588 strlcpy(c->path, host, sizeof(c->path));
589 c->host_port = ntohs(s4_req.dest_port);
590
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000591 debug("channel %d: dynamic request: socks4 host %s port %u command %u",
592 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000593
Ben Lindstromb3921512001-04-11 15:57:50 +0000594 if (s4_req.command != 1) {
595 debug("channel %d: cannot handle: socks4 cn %d",
596 c->self, s4_req.command);
597 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000598 }
Ben Lindstromb3921512001-04-11 15:57:50 +0000599 s4_rsp.version = 0; /* vn: 0 for reply */
600 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000601 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +0000602 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000603 buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +0000604 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000605}
606
Ben Lindstromb3921512001-04-11 15:57:50 +0000607/* dynamic port forwarding */
608void
609channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
610{
611 u_char *p;
612 int have, ret;
613
614 have = buffer_len(&c->input);
615
616 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +0000617 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +0000618 /* check if the fixed size part of the packet is in buffer. */
619 if (have < 4) {
620 /* need more */
621 FD_SET(c->sock, readset);
622 return;
623 }
624 /* try to guess the protocol */
625 p = buffer_ptr(&c->input);
626 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000627 case 0x04:
628 ret = channel_decode_socks4(c, readset, writeset);
629 break;
Ben Lindstromb3921512001-04-11 15:57:50 +0000630 default:
631 ret = -1;
632 break;
633 }
634 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000635 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +0000636 } else if (ret == 0) {
637 debug2("channel %d: pre_dynamic: need more", c->self);
638 /* need more */
639 FD_SET(c->sock, readset);
640 } else {
641 /* switch to the next state */
642 c->type = SSH_CHANNEL_OPENING;
643 port_open_helper(c, "direct-tcpip");
644 }
645}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000646
Damien Millerb38eff82000-04-01 11:09:21 +1000647/* This is our fake X11 server socket. */
648void
649channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
650{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000651 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +1000652 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000653 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +1000654 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +1100655 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +1000656 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +1000657
658 if (FD_ISSET(c->sock, readset)) {
659 debug("X11 connection requested.");
660 addrlen = sizeof(addr);
661 newsock = accept(c->sock, &addr, &addrlen);
662 if (newsock < 0) {
663 error("accept: %.100s", strerror(errno));
664 return;
665 }
Damien Millerd83ff352001-01-30 09:19:34 +1100666 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +1000667 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +1000668 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +1100669 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +1000670
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000671 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +1000672 SSH_CHANNEL_OPENING, newsock, newsock, -1,
673 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100674 0, xstrdup(buf), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000675 if (nc == NULL) {
676 close(newsock);
677 xfree(remote_ipaddr);
678 return;
679 }
Damien Millerbd483e72000-04-30 10:00:53 +1000680 if (compat20) {
681 packet_start(SSH2_MSG_CHANNEL_OPEN);
682 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000683 packet_put_int(nc->self);
Damien Millerbd483e72000-04-30 10:00:53 +1000684 packet_put_int(c->local_window_max);
685 packet_put_int(c->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +1100686 /* originator ipaddr and port */
687 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +1000688 if (datafellows & SSH_BUG_X11FWD) {
689 debug("ssh2 x11 bug compat mode");
690 } else {
691 packet_put_int(remote_port);
692 }
Damien Millerbd483e72000-04-30 10:00:53 +1000693 packet_send();
694 } else {
695 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000696 packet_put_int(nc->self);
Damien Millerbd483e72000-04-30 10:00:53 +1000697 if (have_hostname_in_open)
698 packet_put_string(buf, strlen(buf));
699 packet_send();
700 }
Damien Millerd83ff352001-01-30 09:19:34 +1100701 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +1000702 }
703}
704
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000705void
706port_open_helper(Channel *c, char *rtype)
707{
708 int direct;
709 char buf[1024];
710 char *remote_ipaddr = get_peer_ipaddr(c->sock);
711 u_short remote_port = get_peer_port(c->sock);
712
713 direct = (strcmp(rtype, "direct-tcpip") == 0);
714
715 snprintf(buf, sizeof buf,
716 "%s: listening port %d for %.100s port %d, "
717 "connect from %.200s port %d",
718 rtype, c->listening_port, c->path, c->host_port,
719 remote_ipaddr, remote_port);
720
721 xfree(c->remote_name);
722 c->remote_name = xstrdup(buf);
723
724 if (compat20) {
725 packet_start(SSH2_MSG_CHANNEL_OPEN);
726 packet_put_cstring(rtype);
727 packet_put_int(c->self);
728 packet_put_int(c->local_window_max);
729 packet_put_int(c->local_maxpacket);
730 if (direct) {
731 /* target host, port */
732 packet_put_cstring(c->path);
733 packet_put_int(c->host_port);
734 } else {
735 /* listen address, port */
736 packet_put_cstring(c->path);
737 packet_put_int(c->listening_port);
738 }
739 /* originator host and port */
740 packet_put_cstring(remote_ipaddr);
741 packet_put_int(remote_port);
742 packet_send();
743 } else {
744 packet_start(SSH_MSG_PORT_OPEN);
745 packet_put_int(c->self);
746 packet_put_cstring(c->path);
747 packet_put_int(c->host_port);
748 if (have_hostname_in_open)
749 packet_put_cstring(c->remote_name);
750 packet_send();
751 }
752 xfree(remote_ipaddr);
753}
754
Damien Millerb38eff82000-04-01 11:09:21 +1000755/*
756 * This socket is listening for connections to a forwarded TCP/IP port.
757 */
758void
759channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
760{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000761 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +1000762 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000763 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +1000764 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000765 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100766
Damien Millerb38eff82000-04-01 11:09:21 +1000767 if (FD_ISSET(c->sock, readset)) {
768 debug("Connection to port %d forwarding "
769 "to %.100s port %d requested.",
770 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000771
772 rtype = (c->type == SSH_CHANNEL_RPORT_LISTENER) ?
773 "forwarded-tcpip" : "direct-tcpip";
Ben Lindstrom6d618462001-05-10 23:24:49 +0000774 nextstate = (c->host_port == 0 &&
775 c->type != SSH_CHANNEL_RPORT_LISTENER) ?
776 SSH_CHANNEL_DYNAMIC : SSH_CHANNEL_OPENING;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000777
Damien Millerb38eff82000-04-01 11:09:21 +1000778 addrlen = sizeof(addr);
779 newsock = accept(c->sock, &addr, &addrlen);
780 if (newsock < 0) {
781 error("accept: %.100s", strerror(errno));
782 return;
783 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000784 nc = channel_new(rtype,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000785 nextstate, newsock, newsock, -1,
Damien Millerb38eff82000-04-01 11:09:21 +1000786 c->local_window_max, c->local_maxpacket,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000787 0, xstrdup(rtype), 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000788 if (nc == NULL) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000789 error("channel_post_port_listener: no new channel:");
790 close(newsock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000791 return;
Damien Millerb38eff82000-04-01 11:09:21 +1000792 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000793 nc->listening_port = c->listening_port;
794 nc->host_port = c->host_port;
795 strlcpy(nc->path, c->path, sizeof(nc->path));
796
797 if (nextstate != SSH_CHANNEL_DYNAMIC)
798 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +1000799 }
800}
801
802/*
803 * This is the authentication agent socket listening for connections from
804 * clients.
805 */
806void
807channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
808{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000809 Channel *nc;
810 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +1000811 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +1000812 socklen_t addrlen;
813
814 if (FD_ISSET(c->sock, readset)) {
815 addrlen = sizeof(addr);
816 newsock = accept(c->sock, &addr, &addrlen);
817 if (newsock < 0) {
818 error("accept from auth socket: %.100s", strerror(errno));
819 return;
820 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000821 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +1100822 SSH_CHANNEL_OPENING, newsock, newsock, -1,
823 c->local_window_max, c->local_maxpacket,
824 0, xstrdup("accepted auth socket"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000825 if (nc == NULL) {
826 error("channel_post_auth_listener: channel_new failed");
827 close(newsock);
828 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100829 if (compat20) {
830 packet_start(SSH2_MSG_CHANNEL_OPEN);
831 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000832 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100833 packet_put_int(c->local_window_max);
834 packet_put_int(c->local_maxpacket);
835 } else {
836 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000837 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100838 }
Damien Millerb38eff82000-04-01 11:09:21 +1000839 packet_send();
840 }
841}
842
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000843void
844channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
845{
Ben Lindstrom69128662001-05-08 20:07:39 +0000846 int err = 0;
847 int sz = sizeof(err);
848
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000849 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom69128662001-05-08 20:07:39 +0000850 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err,
851 &sz) < 0) {
852 err = errno;
853 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000854 }
Ben Lindstrom69128662001-05-08 20:07:39 +0000855 if (err == 0) {
856 debug("channel %d: connected", c->self);
857 c->type = SSH_CHANNEL_OPEN;
858 if (compat20) {
859 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
860 packet_put_int(c->remote_id);
861 packet_put_int(c->self);
862 packet_put_int(c->local_window);
863 packet_put_int(c->local_maxpacket);
864 } else {
865 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
866 packet_put_int(c->remote_id);
867 packet_put_int(c->self);
868 }
869 } else {
870 debug("channel %d: not connected: %s",
871 c->self, strerror(err));
872 if (compat20) {
873 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
874 packet_put_int(c->remote_id);
875 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
876 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
877 packet_put_cstring(strerror(err));
878 packet_put_cstring("");
879 }
880 } else {
881 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
882 packet_put_int(c->remote_id);
883 }
884 chan_mark_dead(c);
885 }
886 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000887 }
888}
889
Damien Millerb38eff82000-04-01 11:09:21 +1000890int
891channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
892{
893 char buf[16*1024];
894 int len;
895
896 if (c->rfd != -1 &&
897 FD_ISSET(c->rfd, readset)) {
898 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +1000899 if (len < 0 && (errno == EINTR || errno == EAGAIN))
900 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000901 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +1000902 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +1000903 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +0000904 if (c->type != SSH_CHANNEL_OPEN) {
905 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000906 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000907 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +0000908 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000909 buffer_consume(&c->output, buffer_len(&c->output));
910 c->type = SSH_CHANNEL_INPUT_DRAINING;
Ben Lindstromc486d882001-04-11 16:08:34 +0000911 debug("channel %d: status set to input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000912 } else {
913 chan_read_failed(c);
914 }
915 return -1;
916 }
Damien Millerad833b32000-08-23 10:46:23 +1000917 if(c->input_filter != NULL) {
918 if (c->input_filter(c, buf, len) == -1) {
Ben Lindstromc486d882001-04-11 16:08:34 +0000919 debug("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +1000920 chan_read_failed(c);
921 }
922 } else {
923 buffer_append(&c->input, buf, len);
924 }
Damien Millerb38eff82000-04-01 11:09:21 +1000925 }
926 return 1;
927}
928int
929channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
930{
Ben Lindstrome229b252001-03-05 06:28:06 +0000931 struct termios tio;
Damien Millerb38eff82000-04-01 11:09:21 +1000932 int len;
933
934 /* Send buffered output data to the socket. */
935 if (c->wfd != -1 &&
936 FD_ISSET(c->wfd, writeset) &&
937 buffer_len(&c->output) > 0) {
938 len = write(c->wfd, buffer_ptr(&c->output),
Damien Miller6f83b8e2000-05-02 09:23:45 +1000939 buffer_len(&c->output));
940 if (len < 0 && (errno == EINTR || errno == EAGAIN))
941 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000942 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +0000943 if (c->type != SSH_CHANNEL_OPEN) {
944 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000945 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +0000946 return -1;
947 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000948 buffer_consume(&c->output, buffer_len(&c->output));
Ben Lindstromc486d882001-04-11 16:08:34 +0000949 debug("channel %d: status set to input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000950 c->type = SSH_CHANNEL_INPUT_DRAINING;
951 } else {
952 chan_write_failed(c);
953 }
954 return -1;
955 }
Damien Miller79438cc2001-02-16 12:34:57 +1100956 if (compat20 && c->isatty) {
Damien Miller79438cc2001-02-16 12:34:57 +1100957 if (tcgetattr(c->wfd, &tio) == 0 &&
958 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
959 /*
960 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +0000961 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +0000962 * size of a SSH2_MSG_CHANNEL_DATA message
963 * (4 byte channel id + data)
Damien Miller79438cc2001-02-16 12:34:57 +1100964 */
Ben Lindstrome229b252001-03-05 06:28:06 +0000965 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +1100966 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +1100967 }
968 }
Damien Millerb38eff82000-04-01 11:09:21 +1000969 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +1000970 if (compat20 && len > 0) {
971 c->local_consumed += len;
972 }
973 }
974 return 1;
975}
976int
977channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
978{
979 char buf[16*1024];
980 int len;
981
Damien Miller1383bd82000-04-06 12:32:37 +1000982/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +1000983 if (c->efd != -1) {
984 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
985 FD_ISSET(c->efd, writeset) &&
986 buffer_len(&c->extended) > 0) {
987 len = write(c->efd, buffer_ptr(&c->extended),
988 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +1100989 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000990 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000991 if (len < 0 && (errno == EINTR || errno == EAGAIN))
992 return 1;
993 if (len <= 0) {
994 debug2("channel %d: closing write-efd %d",
995 c->self, c->efd);
996 close(c->efd);
997 c->efd = -1;
998 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000999 buffer_consume(&c->extended, len);
1000 c->local_consumed += len;
1001 }
1002 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1003 FD_ISSET(c->efd, readset)) {
1004 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001005 debug2("channel %d: read %d from efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001006 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001007 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1008 return 1;
1009 if (len <= 0) {
1010 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001011 c->self, c->efd);
1012 close(c->efd);
1013 c->efd = -1;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001014 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001015 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001016 }
Damien Miller33b13562000-04-04 14:38:59 +10001017 }
1018 }
1019 return 1;
1020}
1021int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001022channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001023{
Ben Lindstromb3921512001-04-11 15:57:50 +00001024 if (c->type == SSH_CHANNEL_OPEN &&
1025 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001026 c->local_window < c->local_window_max/2 &&
1027 c->local_consumed > 0) {
1028 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1029 packet_put_int(c->remote_id);
1030 packet_put_int(c->local_consumed);
1031 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001032 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001033 c->self, c->local_window,
1034 c->local_consumed);
1035 c->local_window += c->local_consumed;
1036 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001037 }
1038 return 1;
1039}
1040
1041void
1042channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
1043{
1044 channel_handle_rfd(c, readset, writeset);
1045 channel_handle_wfd(c, readset, writeset);
1046}
1047
1048void
Damien Miller33b13562000-04-04 14:38:59 +10001049channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
1050{
1051 channel_handle_rfd(c, readset, writeset);
1052 channel_handle_wfd(c, readset, writeset);
1053 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001054
1055 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001056}
1057
1058void
Damien Millerb38eff82000-04-01 11:09:21 +10001059channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1060{
1061 int len;
1062 /* Send buffered output data to the socket. */
1063 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1064 len = write(c->sock, buffer_ptr(&c->output),
1065 buffer_len(&c->output));
1066 if (len <= 0)
1067 buffer_consume(&c->output, buffer_len(&c->output));
1068 else
1069 buffer_consume(&c->output, len);
1070 }
1071}
1072
1073void
Damien Miller33b13562000-04-04 14:38:59 +10001074channel_handler_init_20(void)
1075{
1076 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +10001077 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001078 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001079 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001080 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001081 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001082 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001083 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001084
1085 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
1086 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001087 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001088 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001089 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001090 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001091 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_2;
Damien Miller33b13562000-04-04 14:38:59 +10001092}
1093
1094void
Damien Millerb38eff82000-04-01 11:09:21 +10001095channel_handler_init_13(void)
1096{
1097 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1098 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1099 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1100 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1101 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1102 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1103 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001104 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001105 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001106
1107 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
1108 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1109 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1110 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1111 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001112 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001113 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001114}
1115
1116void
1117channel_handler_init_15(void)
1118{
1119 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +10001120 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001121 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1122 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1123 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001124 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001125 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001126
1127 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1128 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1129 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1130 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001131 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001132 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001133}
1134
1135void
1136channel_handler_init(void)
1137{
1138 int i;
1139 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1140 channel_pre[i] = NULL;
1141 channel_post[i] = NULL;
1142 }
Damien Miller33b13562000-04-04 14:38:59 +10001143 if (compat20)
1144 channel_handler_init_20();
1145 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001146 channel_handler_init_13();
1147 else
1148 channel_handler_init_15();
1149}
1150
Damien Miller4af51302000-04-16 11:18:38 +10001151void
Damien Millerb38eff82000-04-01 11:09:21 +10001152channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1153{
1154 static int did_init = 0;
1155 int i;
1156 Channel *c;
1157
1158 if (!did_init) {
1159 channel_handler_init();
1160 did_init = 1;
1161 }
1162 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001163 c = channels[i];
1164 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001165 continue;
1166 if (ftab[c->type] == NULL)
1167 continue;
1168 (*ftab[c->type])(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001169 if (chan_is_dead(c)) {
1170 /*
1171 * we have to remove the fd's from the select mask
1172 * before the channels are free'd and the fd's are
1173 * closed
1174 */
1175 if (c->wfd != -1)
1176 FD_CLR(c->wfd, writeset);
1177 if (c->rfd != -1)
1178 FD_CLR(c->rfd, readset);
1179 if (c->efd != -1) {
1180 if (c->extended_usage == CHAN_EXTENDED_READ)
1181 FD_CLR(c->efd, readset);
1182 if (c->extended_usage == CHAN_EXTENDED_WRITE)
1183 FD_CLR(c->efd, writeset);
1184 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001185 channel_free(c);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001186 }
Damien Millerb38eff82000-04-01 11:09:21 +10001187 }
1188}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001189
Damien Miller4af51302000-04-16 11:18:38 +10001190void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001191channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1192 int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193{
Damien Miller5e953212001-01-30 09:14:00 +11001194 int n;
1195 u_int sz;
1196
1197 n = MAX(*maxfdp, channel_max_fd);
1198
1199 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1200 if (*readsetp == NULL || n > *maxfdp) {
1201 if (*readsetp)
1202 xfree(*readsetp);
1203 if (*writesetp)
1204 xfree(*writesetp);
1205 *readsetp = xmalloc(sz);
1206 *writesetp = xmalloc(sz);
1207 *maxfdp = n;
1208 }
1209 memset(*readsetp, 0, sz);
1210 memset(*writesetp, 0, sz);
1211
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001212 if (!rekeying)
1213 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001214}
1215
Damien Miller4af51302000-04-16 11:18:38 +10001216void
Damien Miller95def091999-11-25 00:26:21 +11001217channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001218{
Damien Millerb38eff82000-04-01 11:09:21 +10001219 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001220}
1221
Damien Miller5e953212001-01-30 09:14:00 +11001222/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001223
Damien Miller4af51302000-04-16 11:18:38 +10001224void
Damien Miller95def091999-11-25 00:26:21 +11001225channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001226{
Damien Miller95def091999-11-25 00:26:21 +11001227 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001228 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001229
Damien Miller95def091999-11-25 00:26:21 +11001230 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001231
1232 c = channels[i];
1233 if (c == NULL)
1234 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001235
Damien Miller5428f641999-11-25 11:54:57 +11001236 /* We are only interested in channels that can have buffered incoming data. */
Damien Miller34132e52000-01-14 15:45:46 +11001237 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001238 if (c->type != SSH_CHANNEL_OPEN &&
1239 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001240 continue;
1241 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001242 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001243 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001244 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001245 if (compat20 &&
1246 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001247 /* XXX is this true? */
Ben Lindstromb1131e92001-03-05 07:27:13 +00001248 debug2("channel %d: no data after CLOSE", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001249 continue;
1250 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001251
Damien Miller95def091999-11-25 00:26:21 +11001252 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001253 if ((c->istate == CHAN_INPUT_OPEN ||
1254 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1255 (len = buffer_len(&c->input)) > 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001256 /* Send some data for the other side over the secure connection. */
Damien Miller33b13562000-04-04 14:38:59 +10001257 if (compat20) {
1258 if (len > c->remote_window)
1259 len = c->remote_window;
1260 if (len > c->remote_maxpacket)
1261 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001262 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001263 if (packet_is_interactive()) {
1264 if (len > 1024)
1265 len = 512;
1266 } else {
1267 /* Keep the packets at reasonable size. */
1268 if (len > packet_get_maxsize()/2)
1269 len = packet_get_maxsize()/2;
1270 }
Damien Miller95def091999-11-25 00:26:21 +11001271 }
Damien Millerb38eff82000-04-01 11:09:21 +10001272 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001273 packet_start(compat20 ?
1274 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001275 packet_put_int(c->remote_id);
1276 packet_put_string(buffer_ptr(&c->input), len);
1277 packet_send();
1278 buffer_consume(&c->input, len);
1279 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001280 }
1281 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001282 if (compat13)
1283 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001284 /*
1285 * input-buffer is empty and read-socket shutdown:
1286 * tell peer, that we will not send more data: send IEOF
1287 */
Damien Millerb38eff82000-04-01 11:09:21 +10001288 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001289 }
Damien Miller33b13562000-04-04 14:38:59 +10001290 /* Send extended data, i.e. stderr */
1291 if (compat20 &&
1292 c->remote_window > 0 &&
1293 (len = buffer_len(&c->extended)) > 0 &&
1294 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001295 debug2("channel %d: rwin %d elen %d euse %d",
1296 c->self, c->remote_window, buffer_len(&c->extended),
1297 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001298 if (len > c->remote_window)
1299 len = c->remote_window;
1300 if (len > c->remote_maxpacket)
1301 len = c->remote_maxpacket;
1302 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1303 packet_put_int(c->remote_id);
1304 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1305 packet_put_string(buffer_ptr(&c->extended), len);
1306 packet_send();
1307 buffer_consume(&c->extended, len);
1308 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001309 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001310 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001311 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001312}
1313
Damien Miller5428f641999-11-25 11:54:57 +11001314/*
1315 * This is called when a packet of type CHANNEL_DATA has just been received.
1316 * The message type has already been consumed, but channel number and data is
1317 * still there.
1318 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001319
Damien Miller4af51302000-04-16 11:18:38 +10001320void
Damien Miller62cee002000-09-23 17:15:56 +11001321channel_input_data(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001322{
Damien Miller34132e52000-01-14 15:45:46 +11001323 int id;
Damien Miller95def091999-11-25 00:26:21 +11001324 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001325 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001326 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001327
Damien Miller95def091999-11-25 00:26:21 +11001328 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001329 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001330 c = channel_lookup(id);
1331 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001332 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001333
Damien Miller95def091999-11-25 00:26:21 +11001334 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001335 if (c->type != SSH_CHANNEL_OPEN &&
1336 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001337 return;
1338
1339 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001340 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001341 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001342
Damien Miller95def091999-11-25 00:26:21 +11001343 /* Get the data. */
1344 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001345 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001346
Damien Miller33b13562000-04-04 14:38:59 +10001347 if (compat20){
1348 if (data_len > c->local_maxpacket) {
1349 log("channel %d: rcvd big packet %d, maxpack %d",
1350 c->self, data_len, c->local_maxpacket);
1351 }
1352 if (data_len > c->local_window) {
1353 log("channel %d: rcvd too much data %d, win %d",
1354 c->self, data_len, c->local_window);
1355 xfree(data);
1356 return;
1357 }
1358 c->local_window -= data_len;
1359 }else{
1360 packet_integrity_check(plen, 4 + 4 + data_len, type);
1361 }
Damien Millerb38eff82000-04-01 11:09:21 +10001362 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001363 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001364}
Damien Miller4af51302000-04-16 11:18:38 +10001365void
Damien Miller62cee002000-09-23 17:15:56 +11001366channel_input_extended_data(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001367{
1368 int id;
1369 int tcode;
1370 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001371 u_int data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001372 Channel *c;
1373
1374 /* Get the channel number and verify it. */
1375 id = packet_get_int();
1376 c = channel_lookup(id);
1377
1378 if (c == NULL)
1379 packet_disconnect("Received extended_data for bad channel %d.", id);
1380 if (c->type != SSH_CHANNEL_OPEN) {
1381 log("channel %d: ext data for non open", id);
1382 return;
1383 }
1384 tcode = packet_get_int();
1385 if (c->efd == -1 ||
1386 c->extended_usage != CHAN_EXTENDED_WRITE ||
1387 tcode != SSH2_EXTENDED_DATA_STDERR) {
1388 log("channel %d: bad ext data", c->self);
1389 return;
1390 }
1391 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001392 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001393 if (data_len > c->local_window) {
1394 log("channel %d: rcvd too much extended_data %d, win %d",
1395 c->self, data_len, c->local_window);
1396 xfree(data);
1397 return;
1398 }
Damien Millerd3444942000-09-30 14:20:03 +11001399 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001400 c->local_window -= data_len;
1401 buffer_append(&c->extended, data, data_len);
1402 xfree(data);
1403}
1404
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001405
Damien Miller5428f641999-11-25 11:54:57 +11001406/*
1407 * Returns true if no channel has too much buffered data, and false if one or
1408 * more channel is overfull.
1409 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001410
Damien Miller4af51302000-04-16 11:18:38 +10001411int
Damien Miller95def091999-11-25 00:26:21 +11001412channel_not_very_much_buffered_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001413{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001414 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001415 Channel *c;
Damien Miller95def091999-11-25 00:26:21 +11001416
1417 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001418 c = channels[i];
1419 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Miller33b13562000-04-04 14:38:59 +10001420 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
Damien Millerb38eff82000-04-01 11:09:21 +10001421 debug("channel %d: big input buffer %d",
1422 c->self, buffer_len(&c->input));
Damien Miller95def091999-11-25 00:26:21 +11001423 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001424 }
1425 if (buffer_len(&c->output) > packet_get_maxsize()) {
1426 debug("channel %d: big output buffer %d",
1427 c->self, buffer_len(&c->output));
Damien Miller95def091999-11-25 00:26:21 +11001428 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001429 }
Damien Miller95def091999-11-25 00:26:21 +11001430 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001431 }
Damien Miller95def091999-11-25 00:26:21 +11001432 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001433}
1434
Damien Miller4af51302000-04-16 11:18:38 +10001435void
Damien Miller62cee002000-09-23 17:15:56 +11001436channel_input_ieof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001437{
1438 int id;
1439 Channel *c;
1440
1441 packet_integrity_check(plen, 4, type);
1442
1443 id = packet_get_int();
1444 c = channel_lookup(id);
1445 if (c == NULL)
1446 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1447 chan_rcvd_ieof(c);
1448}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001449
Damien Miller4af51302000-04-16 11:18:38 +10001450void
Damien Miller62cee002000-09-23 17:15:56 +11001451channel_input_close(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001452{
Damien Millerb38eff82000-04-01 11:09:21 +10001453 int id;
1454 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001455
Damien Millerb38eff82000-04-01 11:09:21 +10001456 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001457
Damien Millerb38eff82000-04-01 11:09:21 +10001458 id = packet_get_int();
1459 c = channel_lookup(id);
1460 if (c == NULL)
1461 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001462
1463 /*
1464 * Send a confirmation that we have closed the channel and no more
1465 * data is coming for it.
1466 */
Damien Miller95def091999-11-25 00:26:21 +11001467 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001468 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001469 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001470
Damien Miller5428f641999-11-25 11:54:57 +11001471 /*
1472 * If the channel is in closed state, we have sent a close request,
1473 * and the other side will eventually respond with a confirmation.
1474 * Thus, we cannot free the channel here, because then there would be
1475 * no-one to receive the confirmation. The channel gets freed when
1476 * the confirmation arrives.
1477 */
Damien Millerb38eff82000-04-01 11:09:21 +10001478 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001479 /*
1480 * Not a closed channel - mark it as draining, which will
1481 * cause it to be freed later.
1482 */
Damien Millerb38eff82000-04-01 11:09:21 +10001483 buffer_consume(&c->input, buffer_len(&c->input));
1484 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001485 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001486}
1487
Damien Millerb38eff82000-04-01 11:09:21 +10001488/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001489void
Damien Miller62cee002000-09-23 17:15:56 +11001490channel_input_oclose(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001491{
Damien Millerb38eff82000-04-01 11:09:21 +10001492 int id = packet_get_int();
1493 Channel *c = channel_lookup(id);
1494 packet_integrity_check(plen, 4, type);
1495 if (c == NULL)
1496 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1497 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001498}
1499
Damien Miller4af51302000-04-16 11:18:38 +10001500void
Damien Miller62cee002000-09-23 17:15:56 +11001501channel_input_close_confirmation(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001502{
1503 int id = packet_get_int();
1504 Channel *c = channel_lookup(id);
1505
Damien Miller4af51302000-04-16 11:18:38 +10001506 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001507 if (c == NULL)
1508 packet_disconnect("Received close confirmation for "
1509 "out-of-range channel %d.", id);
1510 if (c->type != SSH_CHANNEL_CLOSED)
1511 packet_disconnect("Received close confirmation for "
1512 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001513 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001514}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001515
Damien Miller4af51302000-04-16 11:18:38 +10001516void
Damien Miller62cee002000-09-23 17:15:56 +11001517channel_input_open_confirmation(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001518{
Damien Millerb38eff82000-04-01 11:09:21 +10001519 int id, remote_id;
1520 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001521
Damien Miller33b13562000-04-04 14:38:59 +10001522 if (!compat20)
1523 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001524
Damien Millerb38eff82000-04-01 11:09:21 +10001525 id = packet_get_int();
1526 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001527
Damien Millerb38eff82000-04-01 11:09:21 +10001528 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1529 packet_disconnect("Received open confirmation for "
1530 "non-opening channel %d.", id);
1531 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001532 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001533 c->remote_id = remote_id;
1534 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001535
1536 if (compat20) {
1537 c->remote_window = packet_get_int();
1538 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001539 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001540 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001541 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001542 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001543 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001544 }
1545 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1546 c->remote_window, c->remote_maxpacket);
1547 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001548}
1549
Ben Lindstrom69128662001-05-08 20:07:39 +00001550char *
1551reason2txt(int reason)
1552{
1553 switch(reason) {
1554 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
1555 return "administratively prohibited";
1556 case SSH2_OPEN_CONNECT_FAILED:
1557 return "connect failed";
1558 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
1559 return "unknown channel type";
1560 case SSH2_OPEN_RESOURCE_SHORTAGE:
1561 return "resource shortage";
1562 }
1563 return "unkown reason";
1564}
1565
Damien Miller4af51302000-04-16 11:18:38 +10001566void
Damien Miller62cee002000-09-23 17:15:56 +11001567channel_input_open_failure(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001568{
Kevin Steves12057502001-02-05 14:54:34 +00001569 int id, reason;
1570 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001571 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001572
Damien Miller33b13562000-04-04 14:38:59 +10001573 if (!compat20)
1574 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001575
1576 id = packet_get_int();
1577 c = channel_lookup(id);
1578
1579 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1580 packet_disconnect("Received open failure for "
1581 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001582 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00001583 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00001584 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00001585 msg = packet_get_string(NULL);
1586 lang = packet_get_string(NULL);
1587 }
Damien Miller4af51302000-04-16 11:18:38 +10001588 packet_done();
Ben Lindstrom69128662001-05-08 20:07:39 +00001589 log("channel %d: open failed: %s%s%s", id,
1590 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00001591 if (msg != NULL)
1592 xfree(msg);
1593 if (lang != NULL)
1594 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001595 }
Damien Miller95def091999-11-25 00:26:21 +11001596 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001597 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001598}
1599
Damien Miller33b13562000-04-04 14:38:59 +10001600void
Damien Miller62cee002000-09-23 17:15:56 +11001601channel_input_channel_request(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001602{
1603 int id;
1604 Channel *c;
1605
1606 id = packet_get_int();
1607 c = channel_lookup(id);
1608
1609 if (c == NULL ||
1610 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1611 packet_disconnect("Received request for "
1612 "non-open channel %d.", id);
1613 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001614 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001615 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001616 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001617 } else {
1618 char *service = packet_get_string(NULL);
Ben Lindstromcc74df72001-03-05 06:20:14 +00001619 debug("channel %d: rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001620 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001621 xfree(service);
1622 }
1623}
1624
Damien Miller4af51302000-04-16 11:18:38 +10001625void
Damien Miller62cee002000-09-23 17:15:56 +11001626channel_input_window_adjust(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001627{
1628 Channel *c;
1629 int id, adjust;
1630
1631 if (!compat20)
1632 return;
1633
1634 /* Get the channel number and verify it. */
1635 id = packet_get_int();
1636 c = channel_lookup(id);
1637
1638 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1639 log("Received window adjust for "
1640 "non-open channel %d.", id);
1641 return;
1642 }
1643 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001644 packet_done();
Damien Millerd3444942000-09-30 14:20:03 +11001645 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10001646 c->remote_window += adjust;
1647}
1648
Damien Miller5428f641999-11-25 11:54:57 +11001649/*
1650 * Stops listening for channels, and removes any unix domain sockets that we
1651 * might have.
1652 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001653
Damien Miller4af51302000-04-16 11:18:38 +10001654void
Damien Miller95def091999-11-25 00:26:21 +11001655channel_stop_listening()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001656{
Damien Miller95def091999-11-25 00:26:21 +11001657 int i;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001658 Channel *c;
1659
Damien Miller95def091999-11-25 00:26:21 +11001660 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001661 c = channels[i];
1662 if (c != NULL) {
1663 switch (c->type) {
1664 case SSH_CHANNEL_AUTH_SOCKET:
1665 close(c->sock);
1666 unlink(c->path);
1667 channel_free(c);
1668 break;
1669 case SSH_CHANNEL_PORT_LISTENER:
1670 case SSH_CHANNEL_RPORT_LISTENER:
1671 case SSH_CHANNEL_X11_LISTENER:
1672 close(c->sock);
1673 channel_free(c);
1674 break;
1675 default:
1676 break;
1677 }
Damien Miller95def091999-11-25 00:26:21 +11001678 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001679 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001680}
1681
Damien Miller5428f641999-11-25 11:54:57 +11001682/*
Damien Miller6f83b8e2000-05-02 09:23:45 +10001683 * Closes the sockets/fds of all channels. This is used to close extra file
Damien Miller5428f641999-11-25 11:54:57 +11001684 * descriptors after a fork.
1685 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001686
Damien Miller4af51302000-04-16 11:18:38 +10001687void
Damien Miller95def091999-11-25 00:26:21 +11001688channel_close_all()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001689{
Damien Miller95def091999-11-25 00:26:21 +11001690 int i;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001691
Damien Miller6f83b8e2000-05-02 09:23:45 +10001692 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001693 if (channels[i] != NULL)
1694 channel_close_fds(channels[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001695}
1696
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001697/* Returns true if any channel is still open. */
1698
Damien Miller4af51302000-04-16 11:18:38 +10001699int
Damien Miller95def091999-11-25 00:26:21 +11001700channel_still_open()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001701{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001702 int i;
1703 Channel *c;
1704
1705 for (i = 0; i < channels_alloc; i++) {
1706 c = channels[i];
1707 if (c == NULL)
1708 continue;
1709 switch (c->type) {
Damien Miller95def091999-11-25 00:26:21 +11001710 case SSH_CHANNEL_X11_LISTENER:
1711 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001712 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001713 case SSH_CHANNEL_CLOSED:
1714 case SSH_CHANNEL_AUTH_SOCKET:
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001715 case SSH_CHANNEL_DYNAMIC:
Ben Lindstrom69128662001-05-08 20:07:39 +00001716 case SSH_CHANNEL_CONNECTING:
Damien Miller95def091999-11-25 00:26:21 +11001717 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001718 case SSH_CHANNEL_LARVAL:
1719 if (!compat20)
1720 fatal("cannot happen: SSH_CHANNEL_LARVAL");
1721 continue;
Damien Miller95def091999-11-25 00:26:21 +11001722 case SSH_CHANNEL_OPENING:
1723 case SSH_CHANNEL_OPEN:
1724 case SSH_CHANNEL_X11_OPEN:
1725 return 1;
1726 case SSH_CHANNEL_INPUT_DRAINING:
1727 case SSH_CHANNEL_OUTPUT_DRAINING:
1728 if (!compat13)
1729 fatal("cannot happen: OUT_DRAIN");
1730 return 1;
1731 default:
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001732 fatal("channel_still_open: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001733 /* NOTREACHED */
1734 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001735 }
Damien Miller95def091999-11-25 00:26:21 +11001736 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001737}
1738
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001739/* Returns the id of an open channel suitable for keepaliving */
1740
1741int
1742channel_find_open()
1743{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001744 int i;
1745 Channel *c;
1746
1747 for (i = 0; i < channels_alloc; i++) {
1748 c = channels[i];
1749 if (c == NULL)
1750 continue;
1751 switch (c->type) {
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001752 case SSH_CHANNEL_CLOSED:
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001753 case SSH_CHANNEL_DYNAMIC:
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001754 case SSH_CHANNEL_X11_LISTENER:
1755 case SSH_CHANNEL_PORT_LISTENER:
1756 case SSH_CHANNEL_RPORT_LISTENER:
1757 case SSH_CHANNEL_OPENING:
Ben Lindstrom69128662001-05-08 20:07:39 +00001758 case SSH_CHANNEL_CONNECTING:
Ben Lindstromd334b272001-04-14 23:08:36 +00001759 continue;
1760 case SSH_CHANNEL_LARVAL:
1761 case SSH_CHANNEL_AUTH_SOCKET:
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001762 case SSH_CHANNEL_OPEN:
1763 case SSH_CHANNEL_X11_OPEN:
1764 return i;
1765 case SSH_CHANNEL_INPUT_DRAINING:
1766 case SSH_CHANNEL_OUTPUT_DRAINING:
1767 if (!compat13)
1768 fatal("cannot happen: OUT_DRAIN");
1769 return i;
1770 default:
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001771 fatal("channel_find_open: bad channel type %d", c->type);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001772 /* NOTREACHED */
1773 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001774 }
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001775 return -1;
1776}
1777
1778
Damien Miller5428f641999-11-25 11:54:57 +11001779/*
1780 * Returns a message describing the currently open forwarded connections,
1781 * suitable for sending to the client. The message contains crlf pairs for
1782 * newlines.
1783 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001784
Damien Miller95def091999-11-25 00:26:21 +11001785char *
1786channel_open_message()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001787{
Damien Miller95def091999-11-25 00:26:21 +11001788 Buffer buffer;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001789 Channel *c;
1790 char buf[1024], *cp;
Damien Miller95def091999-11-25 00:26:21 +11001791 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001792
Damien Miller95def091999-11-25 00:26:21 +11001793 buffer_init(&buffer);
1794 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001795 buffer_append(&buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001796 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001797 c = channels[i];
1798 if (c == NULL)
1799 continue;
Damien Miller95def091999-11-25 00:26:21 +11001800 switch (c->type) {
Damien Miller95def091999-11-25 00:26:21 +11001801 case SSH_CHANNEL_X11_LISTENER:
1802 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001803 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001804 case SSH_CHANNEL_CLOSED:
1805 case SSH_CHANNEL_AUTH_SOCKET:
1806 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001807 case SSH_CHANNEL_LARVAL:
Damien Miller95def091999-11-25 00:26:21 +11001808 case SSH_CHANNEL_OPENING:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001809 case SSH_CHANNEL_CONNECTING:
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001810 case SSH_CHANNEL_DYNAMIC:
Damien Miller95def091999-11-25 00:26:21 +11001811 case SSH_CHANNEL_OPEN:
1812 case SSH_CHANNEL_X11_OPEN:
1813 case SSH_CHANNEL_INPUT_DRAINING:
1814 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Millerb38eff82000-04-01 11:09:21 +10001815 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 +11001816 c->self, c->remote_name,
1817 c->type, c->remote_id,
1818 c->istate, buffer_len(&c->input),
Damien Millerb38eff82000-04-01 11:09:21 +10001819 c->ostate, buffer_len(&c->output),
1820 c->rfd, c->wfd);
Damien Miller95def091999-11-25 00:26:21 +11001821 buffer_append(&buffer, buf, strlen(buf));
1822 continue;
1823 default:
Damien Millerb38eff82000-04-01 11:09:21 +10001824 fatal("channel_open_message: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001825 /* NOTREACHED */
1826 }
1827 }
1828 buffer_append(&buffer, "\0", 1);
1829 cp = xstrdup(buffer_ptr(&buffer));
1830 buffer_free(&buffer);
1831 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001832}
1833
Damien Miller5428f641999-11-25 11:54:57 +11001834/*
1835 * Initiate forwarding of connections to local port "port" through the secure
1836 * channel to host:port from remote side.
1837 */
Kevin Steves12057502001-02-05 14:54:34 +00001838int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001839channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
1840 u_short port_to_connect, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001841{
Kevin Steves12057502001-02-05 14:54:34 +00001842 return channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +11001843 NULL, listen_port,
1844 host_to_connect, port_to_connect,
1845 gateway_ports, /*remote_fwd*/ 0);
1846}
1847
1848/*
1849 * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
1850 * (SSH_CHANNEL_RPORT_LISTENER).
1851 */
Kevin Steves12057502001-02-05 14:54:34 +00001852int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001853channel_request_forwarding(
1854 const char *listen_address, u_short listen_port,
1855 const char *host_to_connect, u_short port_to_connect,
1856 int gateway_ports, int remote_fwd)
1857{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001858 Channel *c;
1859 int success, sock, on = 1, ctype;
Damien Miller34132e52000-01-14 15:45:46 +11001860 struct addrinfo hints, *ai, *aitop;
1861 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0bc1bd82000-11-13 22:57:25 +11001862 const char *host;
Damien Miller5428f641999-11-25 11:54:57 +11001863 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001864
Kevin Steves12057502001-02-05 14:54:34 +00001865 success = 0;
1866
Damien Miller0bc1bd82000-11-13 22:57:25 +11001867 if (remote_fwd) {
1868 host = listen_address;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001869 ctype = SSH_CHANNEL_RPORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001870 } else {
1871 host = host_to_connect;
1872 ctype =SSH_CHANNEL_PORT_LISTENER;
1873 }
1874
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001875 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00001876 error("Forward host name too long.");
1877 return success;
1878 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001879
Damien Miller0bc1bd82000-11-13 22:57:25 +11001880 /* XXX listen_address is currently ignored */
Damien Miller5428f641999-11-25 11:54:57 +11001881 /*
Damien Miller34132e52000-01-14 15:45:46 +11001882 * getaddrinfo returns a loopback address if the hostname is
1883 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11001884 */
Damien Miller34132e52000-01-14 15:45:46 +11001885 memset(&hints, 0, sizeof(hints));
1886 hints.ai_family = IPv4or6;
1887 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
1888 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001889 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001890 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
1891 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11001892
Damien Miller34132e52000-01-14 15:45:46 +11001893 for (ai = aitop; ai; ai = ai->ai_next) {
1894 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1895 continue;
1896 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1897 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001898 error("channel_request_forwarding: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001899 continue;
1900 }
1901 /* Create a port to listen for the host. */
1902 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1903 if (sock < 0) {
1904 /* this is no error since kernel may not support ipv6 */
1905 verbose("socket: %.100s", strerror(errno));
1906 continue;
1907 }
1908 /*
1909 * Set socket options. We would like the socket to disappear
1910 * as soon as it has been closed for whatever reason.
1911 */
1912 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
1913 linger.l_onoff = 1;
1914 linger.l_linger = 5;
1915 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
1916 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001917
Damien Miller34132e52000-01-14 15:45:46 +11001918 /* Bind the socket to the address. */
1919 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1920 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11001921 if (!ai->ai_next)
1922 error("bind: %.100s", strerror(errno));
1923 else
1924 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001925
Damien Miller34132e52000-01-14 15:45:46 +11001926 close(sock);
1927 continue;
1928 }
1929 /* Start listening for connections on the socket. */
1930 if (listen(sock, 5) < 0) {
1931 error("listen: %.100s", strerror(errno));
1932 close(sock);
1933 continue;
1934 }
1935 /* Allocate a channel number for the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001936 c = channel_new("port listener", ctype, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10001937 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11001938 0, xstrdup("port listener"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001939 if (c == NULL) {
1940 error("channel_request_forwarding: channel_new failed");
1941 close(sock);
1942 continue;
1943 }
1944 strlcpy(c->path, host, sizeof(c->path));
1945 c->host_port = port_to_connect;
1946 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11001947 success = 1;
1948 }
1949 if (success == 0)
Kevin Steves12057502001-02-05 14:54:34 +00001950 error("channel_request_forwarding: cannot listen to port: %d",
1951 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001952 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00001953 return success;
Damien Miller95def091999-11-25 00:26:21 +11001954}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001955
Damien Miller5428f641999-11-25 11:54:57 +11001956/*
1957 * Initiate forwarding of connections to port "port" on remote host through
1958 * the secure channel to host:port from local side.
1959 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001960
Damien Miller4af51302000-04-16 11:18:38 +10001961void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001962channel_request_remote_forwarding(u_short listen_port,
1963 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001964{
Damien Miller0bc1bd82000-11-13 22:57:25 +11001965 int payload_len, type, success = 0;
1966
Damien Miller95def091999-11-25 00:26:21 +11001967 /* Record locally that connection to this host/port is permitted. */
1968 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1969 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001970
Damien Miller95def091999-11-25 00:26:21 +11001971 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10001972 if (compat20) {
1973 const char *address_to_bind = "0.0.0.0";
1974 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1975 packet_put_cstring("tcpip-forward");
1976 packet_put_char(0); /* boolean: want reply */
1977 packet_put_cstring(address_to_bind);
1978 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001979 packet_send();
1980 packet_write_wait();
1981 /* Assume that server accepts the request */
1982 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10001983 } else {
1984 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10001985 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10001986 packet_put_cstring(host_to_connect);
1987 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10001988 packet_send();
1989 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001990
1991 /* Wait for response from the remote side. */
1992 type = packet_read(&payload_len);
1993 switch (type) {
1994 case SSH_SMSG_SUCCESS:
1995 success = 1;
1996 break;
1997 case SSH_SMSG_FAILURE:
1998 log("Warning: Server denied remote port forwarding.");
1999 break;
2000 default:
2001 /* Unknown packet */
2002 packet_disconnect("Protocol error for port forward request:"
2003 "received packet type %d.", type);
2004 }
2005 }
2006 if (success) {
2007 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2008 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2009 permitted_opens[num_permitted_opens].listen_port = listen_port;
2010 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002011 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002012}
2013
Damien Miller5428f641999-11-25 11:54:57 +11002014/*
2015 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2016 * listening for the port, and sends back a success reply (or disconnect
2017 * message if there was an error). This never returns if there was an error.
2018 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002019
Damien Miller4af51302000-04-16 11:18:38 +10002020void
Damien Millere247cc42000-05-07 12:03:14 +10002021channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002022{
Damien Milleraae6c611999-12-06 11:47:28 +11002023 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11002024 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002025
Damien Miller95def091999-11-25 00:26:21 +11002026 /* Get arguments from the packet. */
2027 port = packet_get_int();
2028 hostname = packet_get_string(NULL);
2029 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002030
Damien Millerbac2d8a2000-09-05 16:13:06 +11002031#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002032 /*
2033 * Check that an unprivileged user is not trying to forward a
2034 * privileged port.
2035 */
Damien Miller95def091999-11-25 00:26:21 +11002036 if (port < IPPORT_RESERVED && !is_root)
2037 packet_disconnect("Requested forwarding of port %d but user is not root.",
2038 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11002039#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11002040 /* Initiate forwarding */
Damien Millere247cc42000-05-07 12:03:14 +10002041 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002042
2043 /* Free the argument string. */
2044 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002045}
2046
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002047/*
2048 * Permits opening to any host/port if permitted_opens[] is empty. This is
2049 * usually called by the server, because the user could connect to any port
2050 * anyway, and the server has no way to know but to trust the client anyway.
2051 */
2052void
2053channel_permit_all_opens()
2054{
2055 if (num_permitted_opens == 0)
2056 all_opens_permitted = 1;
2057}
2058
Ben Lindstroma3700052001-04-05 23:26:32 +00002059void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002060channel_add_permitted_opens(char *host, int port)
2061{
2062 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2063 fatal("channel_request_remote_forwarding: too many forwards");
2064 debug("allow port forwarding to host %s port %d", host, port);
2065
2066 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2067 permitted_opens[num_permitted_opens].port_to_connect = port;
2068 num_permitted_opens++;
2069
2070 all_opens_permitted = 0;
2071}
2072
Ben Lindstroma3700052001-04-05 23:26:32 +00002073void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002074channel_clear_permitted_opens(void)
2075{
2076 int i;
2077
2078 for (i = 0; i < num_permitted_opens; i++)
2079 xfree(permitted_opens[i].host_to_connect);
2080 num_permitted_opens = 0;
2081
2082}
2083
2084
2085/* return socket to remote host, port */
Damien Millerb38eff82000-04-01 11:09:21 +10002086int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002087connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002088{
Damien Miller34132e52000-01-14 15:45:46 +11002089 struct addrinfo hints, *ai, *aitop;
2090 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2091 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002092 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002093
Damien Miller34132e52000-01-14 15:45:46 +11002094 memset(&hints, 0, sizeof(hints));
2095 hints.ai_family = IPv4or6;
2096 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002097 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002098 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002099 error("connect_to %.100s: unknown host (%s)", host,
2100 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002101 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002102 }
Damien Miller34132e52000-01-14 15:45:46 +11002103 for (ai = aitop; ai; ai = ai->ai_next) {
2104 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2105 continue;
2106 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2107 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002108 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002109 continue;
2110 }
Damien Miller34132e52000-01-14 15:45:46 +11002111 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2112 if (sock < 0) {
2113 error("socket: %.100s", strerror(errno));
2114 continue;
2115 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +00002116 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002117 fatal("connect_to: F_SETFL: %s", strerror(errno));
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002118 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2119 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002120 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002121 strerror(errno));
2122 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002123 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002124 }
2125 break; /* success */
2126
2127 }
2128 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002129 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002130 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002131 return -1;
2132 }
2133 /* success */
2134 return sock;
2135}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002136
Damien Miller0bc1bd82000-11-13 22:57:25 +11002137int
2138channel_connect_by_listen_adress(u_short listen_port)
2139{
2140 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002141
Damien Miller0bc1bd82000-11-13 22:57:25 +11002142 for (i = 0; i < num_permitted_opens; i++)
2143 if (permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002144 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002145 permitted_opens[i].host_to_connect,
2146 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002147 error("WARNING: Server requests forwarding for unknown listen_port %d",
2148 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002149 return -1;
2150}
Damien Millerb38eff82000-04-01 11:09:21 +10002151
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002152/* Check if connecting to that port is permitted and connect. */
2153int
2154channel_connect_to(const char *host, u_short port)
2155{
2156 int i, permit;
2157
2158 permit = all_opens_permitted;
2159 if (!permit) {
2160 for (i = 0; i < num_permitted_opens; i++)
2161 if (permitted_opens[i].port_to_connect == port &&
2162 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2163 permit = 1;
2164
2165 }
2166 if (!permit) {
2167 log("Received request to connect to host %.100s port %d, "
2168 "but the request was denied.", host, port);
2169 return -1;
2170 }
2171 return connect_to(host, port);
2172}
2173
Damien Millerb38eff82000-04-01 11:09:21 +10002174/*
2175 * This is called after receiving PORT_OPEN message. This attempts to
2176 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
2177 * or CHANNEL_OPEN_FAILURE.
2178 */
2179
Damien Miller4af51302000-04-16 11:18:38 +10002180void
Damien Miller62cee002000-09-23 17:15:56 +11002181channel_input_port_open(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002182{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002183 Channel *c = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002184 u_short host_port;
2185 char *host, *originator_string;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002186 int remote_id, sock = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002187
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002188 remote_id = packet_get_int();
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002189 host = packet_get_string(NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10002190 host_port = packet_get_int();
2191
Damien Millerb38eff82000-04-01 11:09:21 +10002192 if (have_hostname_in_open) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002193 originator_string = packet_get_string(NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10002194 } else {
2195 originator_string = xstrdup("unknown (remote did not supply name)");
Damien Miller95def091999-11-25 00:26:21 +11002196 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002197 packet_done();
2198 sock = channel_connect_to(host, host_port);
2199 if (sock != -1) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002200 c = channel_new("connected socket",
2201 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2202 originator_string, 1);
2203 if (c == NULL) {
2204 error("channel_input_port_open: channel_new failed");
2205 close(sock);
2206 } else {
2207 c->remote_id = remote_id;
2208 }
2209 }
2210 if (c == NULL) {
2211 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2212 packet_put_int(remote_id);
Ben Lindstrom69128662001-05-08 20:07:39 +00002213 packet_send();
Damien Millerb38eff82000-04-01 11:09:21 +10002214 }
Damien Miller95def091999-11-25 00:26:21 +11002215 xfree(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002216}
2217
Damien Miller5428f641999-11-25 11:54:57 +11002218/*
2219 * Creates an internet domain socket for listening for X11 connections.
2220 * Returns a suitable value for the DISPLAY variable, or NULL if an error
2221 * occurs.
2222 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002223
Damien Miller34132e52000-01-14 15:45:46 +11002224#define NUM_SOCKS 10
2225
Damien Miller95def091999-11-25 00:26:21 +11002226char *
Damien Millera34a28b1999-12-14 10:47:15 +11002227x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002228{
Damien Milleraae6c611999-12-06 11:47:28 +11002229 int display_number, sock;
2230 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002231 struct addrinfo hints, *ai, *aitop;
2232 char strport[NI_MAXSERV];
2233 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
2234 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11002235 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002236
Damien Millera34a28b1999-12-14 10:47:15 +11002237 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11002238 display_number < MAX_DISPLAYS;
2239 display_number++) {
2240 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002241 memset(&hints, 0, sizeof(hints));
2242 hints.ai_family = IPv4or6;
2243 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
2244 hints.ai_socktype = SOCK_STREAM;
2245 snprintf(strport, sizeof strport, "%d", port);
2246 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2247 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11002248 return NULL;
2249 }
Damien Miller34132e52000-01-14 15:45:46 +11002250 for (ai = aitop; ai; ai = ai->ai_next) {
2251 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2252 continue;
2253 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2254 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002255 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002256 error("socket: %.100s", strerror(errno));
2257 return NULL;
2258 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002259 debug("x11_create_display_inet: Socket family %d not supported",
2260 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002261 continue;
2262 }
Damien Miller34132e52000-01-14 15:45:46 +11002263 }
2264 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2265 debug("bind port %d: %.100s", port, strerror(errno));
2266 shutdown(sock, SHUT_RDWR);
2267 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002268
2269 if (ai->ai_next)
2270 continue;
2271
Damien Miller34132e52000-01-14 15:45:46 +11002272 for (n = 0; n < num_socks; n++) {
2273 shutdown(socks[n], SHUT_RDWR);
2274 close(socks[n]);
2275 }
2276 num_socks = 0;
2277 break;
2278 }
2279 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002280#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002281 if (num_socks == NUM_SOCKS)
2282 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002283#else
2284 break;
2285#endif
Damien Miller95def091999-11-25 00:26:21 +11002286 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002287 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002288 if (num_socks > 0)
2289 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002290 }
Damien Miller95def091999-11-25 00:26:21 +11002291 if (display_number >= MAX_DISPLAYS) {
2292 error("Failed to allocate internet-domain X11 display socket.");
2293 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002294 }
Damien Miller95def091999-11-25 00:26:21 +11002295 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002296 for (n = 0; n < num_socks; n++) {
2297 sock = socks[n];
2298 if (listen(sock, 5) < 0) {
2299 error("listen: %.100s", strerror(errno));
2300 shutdown(sock, SHUT_RDWR);
2301 close(sock);
2302 return NULL;
2303 }
Damien Miller95def091999-11-25 00:26:21 +11002304 }
Damien Miller34132e52000-01-14 15:45:46 +11002305
Damien Miller95def091999-11-25 00:26:21 +11002306 /* Set up a suitable value for the DISPLAY variable. */
2307 if (gethostname(hostname, sizeof(hostname)) < 0)
2308 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11002309
2310#ifdef IPADDR_IN_DISPLAY
Kevin Stevesef4eea92001-02-05 12:42:17 +00002311 /*
Damien Miller76112de1999-12-21 11:18:08 +11002312 * HPUX detects the local hostname in the DISPLAY variable and tries
2313 * to set up a shared memory connection to the server, which it
2314 * incorrectly supposes to be local.
2315 *
2316 * The workaround - as used in later $$H and other programs - is
2317 * is to set display to the host's IP address.
2318 */
2319 {
2320 struct hostent *he;
2321 struct in_addr my_addr;
2322
2323 he = gethostbyname(hostname);
2324 if (he == NULL) {
2325 error("[X11-broken-fwd-hostname-workaround] Could not get "
2326 "IP address for hostname %s.", hostname);
2327
2328 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
2329 "Could not get IP address for hostname %s.", hostname);
2330
2331 shutdown(sock, SHUT_RDWR);
2332 close(sock);
2333
2334 return NULL;
2335 }
2336
2337 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2338
2339 /* Set DISPLAY to <ip address>:screen.display */
Kevin Stevesef4eea92001-02-05 12:42:17 +00002340 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Kevin Stevesda6cd592000-12-29 18:41:21 +00002341 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002342 }
2343#else /* IPADDR_IN_DISPLAY */
2344 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11002345 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Kevin Stevesda6cd592000-12-29 18:41:21 +00002346 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002347#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002348
Damien Miller34132e52000-01-14 15:45:46 +11002349 /* Allocate a channel for each socket. */
2350 for (n = 0; n < num_socks; n++) {
2351 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10002352 (void) channel_new("x11 listener",
2353 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2354 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002355 0, xstrdup("X11 inet listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11002356 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002357
Damien Miller95def091999-11-25 00:26:21 +11002358 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11002359 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002360}
2361
2362#ifndef X_UNIX_PATH
2363#define X_UNIX_PATH "/tmp/.X11-unix/X"
2364#endif
2365
2366static
2367int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002368connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002369{
Damien Miller95def091999-11-25 00:26:21 +11002370 static const char *const x_sockets[] = {
2371 X_UNIX_PATH "%u",
2372 "/var/X/.X11-unix/X" "%u",
2373 "/usr/spool/sockets/X11/" "%u",
2374 NULL
2375 };
2376 int sock;
2377 struct sockaddr_un addr;
2378 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002379
Damien Miller95def091999-11-25 00:26:21 +11002380 for (path = x_sockets; *path; ++path) {
2381 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2382 if (sock < 0)
2383 error("socket: %.100s", strerror(errno));
2384 memset(&addr, 0, sizeof(addr));
2385 addr.sun_family = AF_UNIX;
2386 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
2387 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2388 return sock;
2389 close(sock);
2390 }
2391 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2392 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002393}
2394
Damien Millerbd483e72000-04-30 10:00:53 +10002395int
2396x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002397{
Damien Millerbd483e72000-04-30 10:00:53 +10002398 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002399 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002400 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002401 struct addrinfo hints, *ai, *aitop;
2402 char strport[NI_MAXSERV];
2403 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002404
Damien Miller95def091999-11-25 00:26:21 +11002405 /* Try to open a socket for the local X server. */
2406 display = getenv("DISPLAY");
2407 if (!display) {
2408 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002409 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002410 }
Damien Miller5428f641999-11-25 11:54:57 +11002411 /*
2412 * Now we decode the value of the DISPLAY variable and make a
2413 * connection to the real X server.
2414 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002415
Damien Miller5428f641999-11-25 11:54:57 +11002416 /*
2417 * Check if it is a unix domain socket. Unix domain displays are in
2418 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2419 */
Damien Miller95def091999-11-25 00:26:21 +11002420 if (strncmp(display, "unix:", 5) == 0 ||
2421 display[0] == ':') {
2422 /* Connect to the unix domain socket. */
2423 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2424 error("Could not parse display number from DISPLAY: %.100s",
2425 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002426 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002427 }
2428 /* Create a socket. */
2429 sock = connect_local_xsocket(display_number);
2430 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002431 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002432
2433 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002434 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002435 }
Damien Miller5428f641999-11-25 11:54:57 +11002436 /*
2437 * Connect to an inet socket. The DISPLAY value is supposedly
2438 * hostname:d[.s], where hostname may also be numeric IP address.
2439 */
Damien Miller95def091999-11-25 00:26:21 +11002440 strncpy(buf, display, sizeof(buf));
2441 buf[sizeof(buf) - 1] = 0;
2442 cp = strchr(buf, ':');
2443 if (!cp) {
2444 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002445 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002446 }
Damien Miller95def091999-11-25 00:26:21 +11002447 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002448 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002449 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2450 error("Could not parse display number from DISPLAY: %.100s",
2451 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002452 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002453 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002454
Damien Miller34132e52000-01-14 15:45:46 +11002455 /* Look up the host address */
2456 memset(&hints, 0, sizeof(hints));
2457 hints.ai_family = IPv4or6;
2458 hints.ai_socktype = SOCK_STREAM;
2459 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2460 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2461 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002462 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002463 }
Damien Miller34132e52000-01-14 15:45:46 +11002464 for (ai = aitop; ai; ai = ai->ai_next) {
2465 /* Create a socket. */
2466 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2467 if (sock < 0) {
2468 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002469 continue;
2470 }
2471 /* Connect it to the display. */
2472 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2473 debug("connect %.100s port %d: %.100s", buf,
2474 6000 + display_number, strerror(errno));
2475 close(sock);
2476 continue;
2477 }
2478 /* Success */
2479 break;
Damien Miller34132e52000-01-14 15:45:46 +11002480 }
Damien Miller34132e52000-01-14 15:45:46 +11002481 freeaddrinfo(aitop);
2482 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002483 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002484 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002485 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002486 }
Damien Millerbd483e72000-04-30 10:00:53 +10002487 return sock;
2488}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002489
Damien Millerbd483e72000-04-30 10:00:53 +10002490/*
2491 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2492 * the remote channel number. We should do whatever we want, and respond
2493 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2494 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002495
Damien Millerbd483e72000-04-30 10:00:53 +10002496void
Damien Miller62cee002000-09-23 17:15:56 +11002497x11_input_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002498{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002499 Channel *c = NULL;
2500 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002501 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10002502
2503 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002504
2505 remote_id = packet_get_int();
2506 if (have_hostname_in_open) {
2507 remote_host = packet_get_string(NULL);
2508 } else {
2509 remote_host = xstrdup("unknown (remote did not supply name)");
2510 }
2511 packet_done();
Damien Millerbd483e72000-04-30 10:00:53 +10002512
2513 /* Obtain a connection to the real X display. */
2514 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002515 if (sock != -1) {
2516 /* Allocate a channel for this connection. */
2517 c = channel_new("connected x11 socket",
2518 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
2519 remote_host, 1);
2520 if (c == NULL) {
2521 error("x11_input_open: channel_new failed");
2522 close(sock);
2523 } else {
2524 c->remote_id = remote_id;
2525 }
2526 }
2527 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10002528 /* Send refusal to the remote host. */
2529 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002530 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10002531 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10002532 /* Send a confirmation to the remote host. */
2533 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002534 packet_put_int(remote_id);
2535 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10002536 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002537 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002538}
2539
Damien Miller69b69aa2000-10-28 14:19:58 +11002540/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2541void
2542deny_input_open(int type, int plen, void *ctxt)
2543{
2544 int rchan = packet_get_int();
2545 switch(type){
2546 case SSH_SMSG_AGENT_OPEN:
2547 error("Warning: ssh server tried agent forwarding.");
2548 break;
2549 case SSH_SMSG_X11_OPEN:
2550 error("Warning: ssh server tried X11 forwarding.");
2551 break;
2552 default:
2553 error("deny_input_open: type %d plen %d", type, plen);
2554 break;
2555 }
2556 error("Warning: this is probably a break in attempt by a malicious server.");
2557 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2558 packet_put_int(rchan);
2559 packet_send();
2560}
2561
Damien Miller5428f641999-11-25 11:54:57 +11002562/*
2563 * Requests forwarding of X11 connections, generates fake authentication
2564 * data, and enables authentication spoofing.
2565 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002566
Damien Miller4af51302000-04-16 11:18:38 +10002567void
Damien Millerbd483e72000-04-30 10:00:53 +10002568x11_request_forwarding_with_spoofing(int client_session_id,
2569 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002570{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002571 u_int data_len = (u_int) strlen(data) / 2;
Ben Lindstromb3211a82001-02-10 22:33:19 +00002572 u_int i, value, len;
Damien Miller95def091999-11-25 00:26:21 +11002573 char *new_data;
2574 int screen_number;
2575 const char *cp;
2576 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002577
Damien Miller95def091999-11-25 00:26:21 +11002578 cp = getenv("DISPLAY");
2579 if (cp)
2580 cp = strchr(cp, ':');
2581 if (cp)
2582 cp = strchr(cp, '.');
2583 if (cp)
2584 screen_number = atoi(cp + 1);
2585 else
2586 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002587
Damien Miller95def091999-11-25 00:26:21 +11002588 /* Save protocol name. */
2589 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002590
Damien Miller5428f641999-11-25 11:54:57 +11002591 /*
2592 * Extract real authentication data and generate fake data of the
2593 * same length.
2594 */
Damien Miller95def091999-11-25 00:26:21 +11002595 x11_saved_data = xmalloc(data_len);
2596 x11_fake_data = xmalloc(data_len);
2597 for (i = 0; i < data_len; i++) {
2598 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2599 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2600 if (i % 4 == 0)
2601 rand = arc4random();
2602 x11_saved_data[i] = value;
2603 x11_fake_data[i] = rand & 0xff;
2604 rand >>= 8;
2605 }
2606 x11_saved_data_len = data_len;
2607 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002608
Damien Miller95def091999-11-25 00:26:21 +11002609 /* Convert the fake data into hex. */
Ben Lindstromb3211a82001-02-10 22:33:19 +00002610 len = 2 * data_len + 1;
2611 new_data = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +11002612 for (i = 0; i < data_len; i++)
Ben Lindstromb3211a82001-02-10 22:33:19 +00002613 snprintf(new_data + 2 * i, len - 2 * i,
2614 "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002615
Damien Miller95def091999-11-25 00:26:21 +11002616 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002617 if (compat20) {
2618 channel_request_start(client_session_id, "x11-req", 0);
2619 packet_put_char(0); /* XXX bool single connection */
2620 } else {
2621 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2622 }
2623 packet_put_cstring(proto);
2624 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002625 packet_put_int(screen_number);
2626 packet_send();
2627 packet_write_wait();
2628 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002629}
2630
2631/* Sends a message to the server to request authentication fd forwarding. */
2632
Damien Miller4af51302000-04-16 11:18:38 +10002633void
Damien Miller95def091999-11-25 00:26:21 +11002634auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002635{
Damien Miller95def091999-11-25 00:26:21 +11002636 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2637 packet_send();
2638 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002639}
2640
Damien Miller5428f641999-11-25 11:54:57 +11002641/*
2642 * Returns the name of the forwarded authentication socket. Returns NULL if
2643 * there is no forwarded authentication socket. The returned value points to
2644 * a static buffer.
2645 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002646
Damien Miller95def091999-11-25 00:26:21 +11002647char *
2648auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002649{
Damien Miller95def091999-11-25 00:26:21 +11002650 return channel_forwarded_auth_socket_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002651}
2652
2653/* removes the agent forwarding socket */
2654
Damien Miller4af51302000-04-16 11:18:38 +10002655void
Damien Miller95def091999-11-25 00:26:21 +11002656cleanup_socket(void)
2657{
Damien Miller78315eb2000-09-29 23:01:36 +11002658 unlink(channel_forwarded_auth_socket_name);
Damien Miller95def091999-11-25 00:26:21 +11002659 rmdir(channel_forwarded_auth_socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002660}
2661
Damien Miller5428f641999-11-25 11:54:57 +11002662/*
Damien Millerd3a18572000-06-07 19:55:44 +10002663 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002664 * This starts forwarding authentication requests.
2665 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002666
Damien Millerd3a18572000-06-07 19:55:44 +10002667int
Damien Miller95def091999-11-25 00:26:21 +11002668auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002669{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002670 Channel *nc;
2671 int sock;
Damien Miller95def091999-11-25 00:26:21 +11002672 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002673
Damien Miller95def091999-11-25 00:26:21 +11002674 if (auth_get_socket_name() != NULL)
2675 fatal("Protocol error: authentication forwarding requested twice.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002676
Damien Miller95def091999-11-25 00:26:21 +11002677 /* Temporarily drop privileged uid for mkdir/bind. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00002678 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002679
Damien Miller95def091999-11-25 00:26:21 +11002680 /* Allocate a buffer for the socket name, and format the name. */
2681 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
2682 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
2683 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002684
Damien Miller95def091999-11-25 00:26:21 +11002685 /* Create private directory for socket */
Damien Millerd3a18572000-06-07 19:55:44 +10002686 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL) {
2687 packet_send_debug("Agent forwarding disabled: mkdtemp() failed: %.100s",
2688 strerror(errno));
2689 restore_uid();
2690 xfree(channel_forwarded_auth_socket_name);
2691 xfree(channel_forwarded_auth_socket_dir);
2692 channel_forwarded_auth_socket_name = NULL;
2693 channel_forwarded_auth_socket_dir = NULL;
2694 return 0;
2695 }
Damien Miller95def091999-11-25 00:26:21 +11002696 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
2697 channel_forwarded_auth_socket_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002698
Damien Miller95def091999-11-25 00:26:21 +11002699 if (atexit(cleanup_socket) < 0) {
2700 int saved = errno;
2701 cleanup_socket();
2702 packet_disconnect("socket: %.100s", strerror(saved));
2703 }
2704 /* Create the socket. */
2705 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2706 if (sock < 0)
2707 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002708
Damien Miller95def091999-11-25 00:26:21 +11002709 /* Bind it to the name. */
2710 memset(&sunaddr, 0, sizeof(sunaddr));
2711 sunaddr.sun_family = AF_UNIX;
2712 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
2713 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002714
Damien Miller95def091999-11-25 00:26:21 +11002715 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2716 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002717
Damien Miller95def091999-11-25 00:26:21 +11002718 /* Restore the privileged uid. */
2719 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002720
Damien Miller95def091999-11-25 00:26:21 +11002721 /* Start listening on the socket. */
2722 if (listen(sock, 5) < 0)
2723 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002724
Damien Miller95def091999-11-25 00:26:21 +11002725 /* Allocate a channel for the authentication agent socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002726 nc = channel_new("auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11002727 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2728 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2729 0, xstrdup("auth socket"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002730 if (nc == NULL) {
2731 error("auth_input_request_forwarding: channel_new failed");
2732 close(sock);
2733 return 0;
2734 }
2735 strlcpy(nc->path, channel_forwarded_auth_socket_name, sizeof(nc->path));
Damien Millerd3a18572000-06-07 19:55:44 +10002736 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002737}
2738
2739/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2740
Damien Miller4af51302000-04-16 11:18:38 +10002741void
Damien Miller62cee002000-09-23 17:15:56 +11002742auth_input_open_request(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002743{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002744 Channel *c = NULL;
2745 int remote_id, sock;
Damien Miller95def091999-11-25 00:26:21 +11002746 char *dummyname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002747
Damien Millerb38eff82000-04-01 11:09:21 +10002748 packet_integrity_check(plen, 4, type);
2749
Damien Miller95def091999-11-25 00:26:21 +11002750 /* Read the remote channel number from the message. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002751 remote_id = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002752
Damien Miller5428f641999-11-25 11:54:57 +11002753 /*
2754 * Get a connection to the local authentication agent (this may again
2755 * get forwarded).
2756 */
Damien Miller95def091999-11-25 00:26:21 +11002757 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002758
Damien Miller5428f641999-11-25 11:54:57 +11002759 /*
2760 * If we could not connect the agent, send an error message back to
2761 * the server. This should never happen unless the agent dies,
2762 * because authentication forwarding is only enabled if we have an
2763 * agent.
2764 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002765 if (sock >= 0) {
2766 dummyname = xstrdup("authentication agent connection");
2767 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock, -1, 0, 0, 0, dummyname, 1);
2768 if (c == NULL) {
2769 error("auth_input_open_request: channel_new failed");
2770 close(sock);
2771 } else {
2772 c->remote_id = remote_id;
2773 }
Damien Miller95def091999-11-25 00:26:21 +11002774 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002775 if (c == NULL) {
2776 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2777 packet_put_int(remote_id);
2778 } else {
2779 /* Send a confirmation to the remote host. */
2780 debug("Forwarding authentication connection.");
2781 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2782 packet_put_int(remote_id);
2783 packet_put_int(c->self);
2784 }
Damien Miller95def091999-11-25 00:26:21 +11002785 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002786}
Damien Miller33b13562000-04-04 14:38:59 +10002787
2788void
Damien Millerbd483e72000-04-30 10:00:53 +10002789channel_start_open(int id)
Damien Miller33b13562000-04-04 14:38:59 +10002790{
2791 Channel *c = channel_lookup(id);
2792 if (c == NULL) {
2793 log("channel_open: %d: bad id", id);
2794 return;
2795 }
Damien Millerbd483e72000-04-30 10:00:53 +10002796 debug("send channel open %d", id);
Damien Miller33b13562000-04-04 14:38:59 +10002797 packet_start(SSH2_MSG_CHANNEL_OPEN);
2798 packet_put_cstring(c->ctype);
2799 packet_put_int(c->self);
2800 packet_put_int(c->local_window);
2801 packet_put_int(c->local_maxpacket);
Damien Millerbd483e72000-04-30 10:00:53 +10002802}
2803void
2804channel_open(int id)
2805{
2806 /* XXX REMOVE ME */
2807 channel_start_open(id);
Damien Miller33b13562000-04-04 14:38:59 +10002808 packet_send();
Damien Miller33b13562000-04-04 14:38:59 +10002809}
2810void
2811channel_request(int id, char *service, int wantconfirm)
2812{
2813 channel_request_start(id, service, wantconfirm);
2814 packet_send();
2815 debug("channel request %d: %s", id, service) ;
2816}
2817void
2818channel_request_start(int id, char *service, int wantconfirm)
2819{
2820 Channel *c = channel_lookup(id);
2821 if (c == NULL) {
2822 log("channel_request: %d: bad id", id);
2823 return;
2824 }
2825 packet_start(SSH2_MSG_CHANNEL_REQUEST);
2826 packet_put_int(c->remote_id);
2827 packet_put_cstring(service);
2828 packet_put_char(wantconfirm);
2829}
2830void
2831channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
2832{
2833 Channel *c = channel_lookup(id);
2834 if (c == NULL) {
2835 log("channel_register_callback: %d: bad id", id);
2836 return;
2837 }
2838 c->cb_event = mtype;
2839 c->cb_fn = fn;
2840 c->cb_arg = arg;
2841}
2842void
2843channel_register_cleanup(int id, channel_callback_fn *fn)
2844{
2845 Channel *c = channel_lookup(id);
2846 if (c == NULL) {
2847 log("channel_register_cleanup: %d: bad id", id);
2848 return;
2849 }
2850 c->dettach_user = fn;
2851}
2852void
2853channel_cancel_cleanup(int id)
2854{
2855 Channel *c = channel_lookup(id);
2856 if (c == NULL) {
2857 log("channel_cancel_cleanup: %d: bad id", id);
2858 return;
2859 }
2860 c->dettach_user = NULL;
2861}
Kevin Stevesef4eea92001-02-05 12:42:17 +00002862void
Damien Millerad833b32000-08-23 10:46:23 +10002863channel_register_filter(int id, channel_filter_fn *fn)
2864{
2865 Channel *c = channel_lookup(id);
2866 if (c == NULL) {
2867 log("channel_register_filter: %d: bad id", id);
2868 return;
2869 }
2870 c->input_filter = fn;
2871}
Damien Miller33b13562000-04-04 14:38:59 +10002872
2873void
Damien Miller69b69aa2000-10-28 14:19:58 +11002874channel_set_fds(int id, int rfd, int wfd, int efd,
2875 int extusage, int nonblock)
Damien Miller33b13562000-04-04 14:38:59 +10002876{
2877 Channel *c = channel_lookup(id);
2878 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
2879 fatal("channel_activate for non-larval channel %d.", id);
Damien Miller69b69aa2000-10-28 14:19:58 +11002880 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller33b13562000-04-04 14:38:59 +10002881 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002882 /* XXX window size? */
Damien Millere4340be2000-09-16 13:29:08 +11002883 c->local_window = c->local_window_max = c->local_maxpacket * 2;
Damien Miller33b13562000-04-04 14:38:59 +10002884 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2885 packet_put_int(c->remote_id);
2886 packet_put_int(c->local_window);
2887 packet_send();
2888}