blob: 0f3d9ca97528e40425c9d0023a1a3c50b789ed2f [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 Lindstrom69128662001-05-08 20:07:39 +000043RCSID("$OpenBSD: channels.c,v 1.114 2001/05/08 19:17:30 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";
774 nextstate = (c->host_port == 0) ? SSH_CHANNEL_DYNAMIC :
775 SSH_CHANNEL_OPENING;
776
Damien Millerb38eff82000-04-01 11:09:21 +1000777 addrlen = sizeof(addr);
778 newsock = accept(c->sock, &addr, &addrlen);
779 if (newsock < 0) {
780 error("accept: %.100s", strerror(errno));
781 return;
782 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000783 nc = channel_new(rtype,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000784 nextstate, newsock, newsock, -1,
Damien Millerb38eff82000-04-01 11:09:21 +1000785 c->local_window_max, c->local_maxpacket,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000786 0, xstrdup(rtype), 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000787 if (nc == NULL) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000788 error("channel_post_port_listener: no new channel:");
789 close(newsock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000790 return;
Damien Millerb38eff82000-04-01 11:09:21 +1000791 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000792 nc->listening_port = c->listening_port;
793 nc->host_port = c->host_port;
794 strlcpy(nc->path, c->path, sizeof(nc->path));
795
796 if (nextstate != SSH_CHANNEL_DYNAMIC)
797 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +1000798 }
799}
800
801/*
802 * This is the authentication agent socket listening for connections from
803 * clients.
804 */
805void
806channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
807{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000808 Channel *nc;
809 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +1000810 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +1000811 socklen_t addrlen;
812
813 if (FD_ISSET(c->sock, readset)) {
814 addrlen = sizeof(addr);
815 newsock = accept(c->sock, &addr, &addrlen);
816 if (newsock < 0) {
817 error("accept from auth socket: %.100s", strerror(errno));
818 return;
819 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000820 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +1100821 SSH_CHANNEL_OPENING, newsock, newsock, -1,
822 c->local_window_max, c->local_maxpacket,
823 0, xstrdup("accepted auth socket"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000824 if (nc == NULL) {
825 error("channel_post_auth_listener: channel_new failed");
826 close(newsock);
827 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100828 if (compat20) {
829 packet_start(SSH2_MSG_CHANNEL_OPEN);
830 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000831 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100832 packet_put_int(c->local_window_max);
833 packet_put_int(c->local_maxpacket);
834 } else {
835 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000836 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100837 }
Damien Millerb38eff82000-04-01 11:09:21 +1000838 packet_send();
839 }
840}
841
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000842void
843channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
844{
Ben Lindstrom69128662001-05-08 20:07:39 +0000845 int err = 0;
846 int sz = sizeof(err);
847
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000848 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom69128662001-05-08 20:07:39 +0000849 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err,
850 &sz) < 0) {
851 err = errno;
852 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000853 }
Ben Lindstrom69128662001-05-08 20:07:39 +0000854 if (err == 0) {
855 debug("channel %d: connected", c->self);
856 c->type = SSH_CHANNEL_OPEN;
857 if (compat20) {
858 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
859 packet_put_int(c->remote_id);
860 packet_put_int(c->self);
861 packet_put_int(c->local_window);
862 packet_put_int(c->local_maxpacket);
863 } else {
864 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
865 packet_put_int(c->remote_id);
866 packet_put_int(c->self);
867 }
868 } else {
869 debug("channel %d: not connected: %s",
870 c->self, strerror(err));
871 if (compat20) {
872 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
873 packet_put_int(c->remote_id);
874 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
875 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
876 packet_put_cstring(strerror(err));
877 packet_put_cstring("");
878 }
879 } else {
880 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
881 packet_put_int(c->remote_id);
882 }
883 chan_mark_dead(c);
884 }
885 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000886 }
887}
888
Damien Millerb38eff82000-04-01 11:09:21 +1000889int
890channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
891{
892 char buf[16*1024];
893 int len;
894
895 if (c->rfd != -1 &&
896 FD_ISSET(c->rfd, readset)) {
897 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +1000898 if (len < 0 && (errno == EINTR || errno == EAGAIN))
899 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000900 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +1000901 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +1000902 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +0000903 if (c->type != SSH_CHANNEL_OPEN) {
904 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000905 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000906 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +0000907 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000908 buffer_consume(&c->output, buffer_len(&c->output));
909 c->type = SSH_CHANNEL_INPUT_DRAINING;
Ben Lindstromc486d882001-04-11 16:08:34 +0000910 debug("channel %d: status set to input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000911 } else {
912 chan_read_failed(c);
913 }
914 return -1;
915 }
Damien Millerad833b32000-08-23 10:46:23 +1000916 if(c->input_filter != NULL) {
917 if (c->input_filter(c, buf, len) == -1) {
Ben Lindstromc486d882001-04-11 16:08:34 +0000918 debug("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +1000919 chan_read_failed(c);
920 }
921 } else {
922 buffer_append(&c->input, buf, len);
923 }
Damien Millerb38eff82000-04-01 11:09:21 +1000924 }
925 return 1;
926}
927int
928channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
929{
Ben Lindstrome229b252001-03-05 06:28:06 +0000930 struct termios tio;
Damien Millerb38eff82000-04-01 11:09:21 +1000931 int len;
932
933 /* Send buffered output data to the socket. */
934 if (c->wfd != -1 &&
935 FD_ISSET(c->wfd, writeset) &&
936 buffer_len(&c->output) > 0) {
937 len = write(c->wfd, buffer_ptr(&c->output),
Damien Miller6f83b8e2000-05-02 09:23:45 +1000938 buffer_len(&c->output));
939 if (len < 0 && (errno == EINTR || errno == EAGAIN))
940 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000941 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +0000942 if (c->type != SSH_CHANNEL_OPEN) {
943 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000944 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +0000945 return -1;
946 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000947 buffer_consume(&c->output, buffer_len(&c->output));
Ben Lindstromc486d882001-04-11 16:08:34 +0000948 debug("channel %d: status set to input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000949 c->type = SSH_CHANNEL_INPUT_DRAINING;
950 } else {
951 chan_write_failed(c);
952 }
953 return -1;
954 }
Damien Miller79438cc2001-02-16 12:34:57 +1100955 if (compat20 && c->isatty) {
Damien Miller79438cc2001-02-16 12:34:57 +1100956 if (tcgetattr(c->wfd, &tio) == 0 &&
957 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
958 /*
959 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +0000960 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +0000961 * size of a SSH2_MSG_CHANNEL_DATA message
962 * (4 byte channel id + data)
Damien Miller79438cc2001-02-16 12:34:57 +1100963 */
Ben Lindstrome229b252001-03-05 06:28:06 +0000964 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +1100965 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +1100966 }
967 }
Damien Millerb38eff82000-04-01 11:09:21 +1000968 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +1000969 if (compat20 && len > 0) {
970 c->local_consumed += len;
971 }
972 }
973 return 1;
974}
975int
976channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
977{
978 char buf[16*1024];
979 int len;
980
Damien Miller1383bd82000-04-06 12:32:37 +1000981/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +1000982 if (c->efd != -1) {
983 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
984 FD_ISSET(c->efd, writeset) &&
985 buffer_len(&c->extended) > 0) {
986 len = write(c->efd, buffer_ptr(&c->extended),
987 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +1100988 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000989 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000990 if (len < 0 && (errno == EINTR || errno == EAGAIN))
991 return 1;
992 if (len <= 0) {
993 debug2("channel %d: closing write-efd %d",
994 c->self, c->efd);
995 close(c->efd);
996 c->efd = -1;
997 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000998 buffer_consume(&c->extended, len);
999 c->local_consumed += len;
1000 }
1001 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1002 FD_ISSET(c->efd, readset)) {
1003 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001004 debug2("channel %d: read %d from efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001005 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001006 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1007 return 1;
1008 if (len <= 0) {
1009 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001010 c->self, c->efd);
1011 close(c->efd);
1012 c->efd = -1;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001013 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001014 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001015 }
Damien Miller33b13562000-04-04 14:38:59 +10001016 }
1017 }
1018 return 1;
1019}
1020int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001021channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001022{
Ben Lindstromb3921512001-04-11 15:57:50 +00001023 if (c->type == SSH_CHANNEL_OPEN &&
1024 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001025 c->local_window < c->local_window_max/2 &&
1026 c->local_consumed > 0) {
1027 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1028 packet_put_int(c->remote_id);
1029 packet_put_int(c->local_consumed);
1030 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001031 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001032 c->self, c->local_window,
1033 c->local_consumed);
1034 c->local_window += c->local_consumed;
1035 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001036 }
1037 return 1;
1038}
1039
1040void
1041channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
1042{
1043 channel_handle_rfd(c, readset, writeset);
1044 channel_handle_wfd(c, readset, writeset);
1045}
1046
1047void
Damien Miller33b13562000-04-04 14:38:59 +10001048channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
1049{
1050 channel_handle_rfd(c, readset, writeset);
1051 channel_handle_wfd(c, readset, writeset);
1052 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001053
1054 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001055}
1056
1057void
Damien Millerb38eff82000-04-01 11:09:21 +10001058channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1059{
1060 int len;
1061 /* Send buffered output data to the socket. */
1062 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1063 len = write(c->sock, buffer_ptr(&c->output),
1064 buffer_len(&c->output));
1065 if (len <= 0)
1066 buffer_consume(&c->output, buffer_len(&c->output));
1067 else
1068 buffer_consume(&c->output, len);
1069 }
1070}
1071
1072void
Damien Miller33b13562000-04-04 14:38:59 +10001073channel_handler_init_20(void)
1074{
1075 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +10001076 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001077 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001078 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001079 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001080 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001081 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001082 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001083
1084 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
1085 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001086 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001087 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001088 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001089 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001090 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_2;
Damien Miller33b13562000-04-04 14:38:59 +10001091}
1092
1093void
Damien Millerb38eff82000-04-01 11:09:21 +10001094channel_handler_init_13(void)
1095{
1096 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1097 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1098 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1099 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1100 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1101 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1102 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001103 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001104 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001105
1106 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
1107 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1108 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1109 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1110 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001111 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001112 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001113}
1114
1115void
1116channel_handler_init_15(void)
1117{
1118 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +10001119 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001120 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1121 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1122 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001123 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001124 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001125
1126 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1127 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1128 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1129 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001130 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001131 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001132}
1133
1134void
1135channel_handler_init(void)
1136{
1137 int i;
1138 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1139 channel_pre[i] = NULL;
1140 channel_post[i] = NULL;
1141 }
Damien Miller33b13562000-04-04 14:38:59 +10001142 if (compat20)
1143 channel_handler_init_20();
1144 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001145 channel_handler_init_13();
1146 else
1147 channel_handler_init_15();
1148}
1149
Damien Miller4af51302000-04-16 11:18:38 +10001150void
Damien Millerb38eff82000-04-01 11:09:21 +10001151channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1152{
1153 static int did_init = 0;
1154 int i;
1155 Channel *c;
1156
1157 if (!did_init) {
1158 channel_handler_init();
1159 did_init = 1;
1160 }
1161 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001162 c = channels[i];
1163 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001164 continue;
1165 if (ftab[c->type] == NULL)
1166 continue;
1167 (*ftab[c->type])(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001168 if (chan_is_dead(c)) {
1169 /*
1170 * we have to remove the fd's from the select mask
1171 * before the channels are free'd and the fd's are
1172 * closed
1173 */
1174 if (c->wfd != -1)
1175 FD_CLR(c->wfd, writeset);
1176 if (c->rfd != -1)
1177 FD_CLR(c->rfd, readset);
1178 if (c->efd != -1) {
1179 if (c->extended_usage == CHAN_EXTENDED_READ)
1180 FD_CLR(c->efd, readset);
1181 if (c->extended_usage == CHAN_EXTENDED_WRITE)
1182 FD_CLR(c->efd, writeset);
1183 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001184 channel_free(c);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001185 }
Damien Millerb38eff82000-04-01 11:09:21 +10001186 }
1187}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001188
Damien Miller4af51302000-04-16 11:18:38 +10001189void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001190channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1191 int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192{
Damien Miller5e953212001-01-30 09:14:00 +11001193 int n;
1194 u_int sz;
1195
1196 n = MAX(*maxfdp, channel_max_fd);
1197
1198 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1199 if (*readsetp == NULL || n > *maxfdp) {
1200 if (*readsetp)
1201 xfree(*readsetp);
1202 if (*writesetp)
1203 xfree(*writesetp);
1204 *readsetp = xmalloc(sz);
1205 *writesetp = xmalloc(sz);
1206 *maxfdp = n;
1207 }
1208 memset(*readsetp, 0, sz);
1209 memset(*writesetp, 0, sz);
1210
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001211 if (!rekeying)
1212 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001213}
1214
Damien Miller4af51302000-04-16 11:18:38 +10001215void
Damien Miller95def091999-11-25 00:26:21 +11001216channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001217{
Damien Millerb38eff82000-04-01 11:09:21 +10001218 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001219}
1220
Damien Miller5e953212001-01-30 09:14:00 +11001221/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001222
Damien Miller4af51302000-04-16 11:18:38 +10001223void
Damien Miller95def091999-11-25 00:26:21 +11001224channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001225{
Damien Miller95def091999-11-25 00:26:21 +11001226 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001227 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001228
Damien Miller95def091999-11-25 00:26:21 +11001229 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001230
1231 c = channels[i];
1232 if (c == NULL)
1233 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001234
Damien Miller5428f641999-11-25 11:54:57 +11001235 /* We are only interested in channels that can have buffered incoming data. */
Damien Miller34132e52000-01-14 15:45:46 +11001236 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001237 if (c->type != SSH_CHANNEL_OPEN &&
1238 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001239 continue;
1240 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001241 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001242 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001243 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001244 if (compat20 &&
1245 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001246 /* XXX is this true? */
Ben Lindstromb1131e92001-03-05 07:27:13 +00001247 debug2("channel %d: no data after CLOSE", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001248 continue;
1249 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001250
Damien Miller95def091999-11-25 00:26:21 +11001251 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001252 if ((c->istate == CHAN_INPUT_OPEN ||
1253 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1254 (len = buffer_len(&c->input)) > 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001255 /* Send some data for the other side over the secure connection. */
Damien Miller33b13562000-04-04 14:38:59 +10001256 if (compat20) {
1257 if (len > c->remote_window)
1258 len = c->remote_window;
1259 if (len > c->remote_maxpacket)
1260 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001261 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001262 if (packet_is_interactive()) {
1263 if (len > 1024)
1264 len = 512;
1265 } else {
1266 /* Keep the packets at reasonable size. */
1267 if (len > packet_get_maxsize()/2)
1268 len = packet_get_maxsize()/2;
1269 }
Damien Miller95def091999-11-25 00:26:21 +11001270 }
Damien Millerb38eff82000-04-01 11:09:21 +10001271 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001272 packet_start(compat20 ?
1273 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001274 packet_put_int(c->remote_id);
1275 packet_put_string(buffer_ptr(&c->input), len);
1276 packet_send();
1277 buffer_consume(&c->input, len);
1278 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001279 }
1280 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001281 if (compat13)
1282 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001283 /*
1284 * input-buffer is empty and read-socket shutdown:
1285 * tell peer, that we will not send more data: send IEOF
1286 */
Damien Millerb38eff82000-04-01 11:09:21 +10001287 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001288 }
Damien Miller33b13562000-04-04 14:38:59 +10001289 /* Send extended data, i.e. stderr */
1290 if (compat20 &&
1291 c->remote_window > 0 &&
1292 (len = buffer_len(&c->extended)) > 0 &&
1293 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001294 debug2("channel %d: rwin %d elen %d euse %d",
1295 c->self, c->remote_window, buffer_len(&c->extended),
1296 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001297 if (len > c->remote_window)
1298 len = c->remote_window;
1299 if (len > c->remote_maxpacket)
1300 len = c->remote_maxpacket;
1301 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1302 packet_put_int(c->remote_id);
1303 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1304 packet_put_string(buffer_ptr(&c->extended), len);
1305 packet_send();
1306 buffer_consume(&c->extended, len);
1307 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001308 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001309 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001310 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001311}
1312
Damien Miller5428f641999-11-25 11:54:57 +11001313/*
1314 * This is called when a packet of type CHANNEL_DATA has just been received.
1315 * The message type has already been consumed, but channel number and data is
1316 * still there.
1317 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001318
Damien Miller4af51302000-04-16 11:18:38 +10001319void
Damien Miller62cee002000-09-23 17:15:56 +11001320channel_input_data(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001321{
Damien Miller34132e52000-01-14 15:45:46 +11001322 int id;
Damien Miller95def091999-11-25 00:26:21 +11001323 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001324 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001325 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001326
Damien Miller95def091999-11-25 00:26:21 +11001327 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001328 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001329 c = channel_lookup(id);
1330 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001331 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001332
Damien Miller95def091999-11-25 00:26:21 +11001333 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001334 if (c->type != SSH_CHANNEL_OPEN &&
1335 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001336 return;
1337
1338 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001339 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001340 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001341
Damien Miller95def091999-11-25 00:26:21 +11001342 /* Get the data. */
1343 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001344 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001345
Damien Miller33b13562000-04-04 14:38:59 +10001346 if (compat20){
1347 if (data_len > c->local_maxpacket) {
1348 log("channel %d: rcvd big packet %d, maxpack %d",
1349 c->self, data_len, c->local_maxpacket);
1350 }
1351 if (data_len > c->local_window) {
1352 log("channel %d: rcvd too much data %d, win %d",
1353 c->self, data_len, c->local_window);
1354 xfree(data);
1355 return;
1356 }
1357 c->local_window -= data_len;
1358 }else{
1359 packet_integrity_check(plen, 4 + 4 + data_len, type);
1360 }
Damien Millerb38eff82000-04-01 11:09:21 +10001361 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001362 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001363}
Damien Miller4af51302000-04-16 11:18:38 +10001364void
Damien Miller62cee002000-09-23 17:15:56 +11001365channel_input_extended_data(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001366{
1367 int id;
1368 int tcode;
1369 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001370 u_int data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001371 Channel *c;
1372
1373 /* Get the channel number and verify it. */
1374 id = packet_get_int();
1375 c = channel_lookup(id);
1376
1377 if (c == NULL)
1378 packet_disconnect("Received extended_data for bad channel %d.", id);
1379 if (c->type != SSH_CHANNEL_OPEN) {
1380 log("channel %d: ext data for non open", id);
1381 return;
1382 }
1383 tcode = packet_get_int();
1384 if (c->efd == -1 ||
1385 c->extended_usage != CHAN_EXTENDED_WRITE ||
1386 tcode != SSH2_EXTENDED_DATA_STDERR) {
1387 log("channel %d: bad ext data", c->self);
1388 return;
1389 }
1390 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001391 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001392 if (data_len > c->local_window) {
1393 log("channel %d: rcvd too much extended_data %d, win %d",
1394 c->self, data_len, c->local_window);
1395 xfree(data);
1396 return;
1397 }
Damien Millerd3444942000-09-30 14:20:03 +11001398 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001399 c->local_window -= data_len;
1400 buffer_append(&c->extended, data, data_len);
1401 xfree(data);
1402}
1403
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001404
Damien Miller5428f641999-11-25 11:54:57 +11001405/*
1406 * Returns true if no channel has too much buffered data, and false if one or
1407 * more channel is overfull.
1408 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001409
Damien Miller4af51302000-04-16 11:18:38 +10001410int
Damien Miller95def091999-11-25 00:26:21 +11001411channel_not_very_much_buffered_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001412{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001413 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001414 Channel *c;
Damien Miller95def091999-11-25 00:26:21 +11001415
1416 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001417 c = channels[i];
1418 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Miller33b13562000-04-04 14:38:59 +10001419 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
Damien Millerb38eff82000-04-01 11:09:21 +10001420 debug("channel %d: big input buffer %d",
1421 c->self, buffer_len(&c->input));
Damien Miller95def091999-11-25 00:26:21 +11001422 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001423 }
1424 if (buffer_len(&c->output) > packet_get_maxsize()) {
1425 debug("channel %d: big output buffer %d",
1426 c->self, buffer_len(&c->output));
Damien Miller95def091999-11-25 00:26:21 +11001427 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001428 }
Damien Miller95def091999-11-25 00:26:21 +11001429 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001430 }
Damien Miller95def091999-11-25 00:26:21 +11001431 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001432}
1433
Damien Miller4af51302000-04-16 11:18:38 +10001434void
Damien Miller62cee002000-09-23 17:15:56 +11001435channel_input_ieof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001436{
1437 int id;
1438 Channel *c;
1439
1440 packet_integrity_check(plen, 4, type);
1441
1442 id = packet_get_int();
1443 c = channel_lookup(id);
1444 if (c == NULL)
1445 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1446 chan_rcvd_ieof(c);
1447}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001448
Damien Miller4af51302000-04-16 11:18:38 +10001449void
Damien Miller62cee002000-09-23 17:15:56 +11001450channel_input_close(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001451{
Damien Millerb38eff82000-04-01 11:09:21 +10001452 int id;
1453 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001454
Damien Millerb38eff82000-04-01 11:09:21 +10001455 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001456
Damien Millerb38eff82000-04-01 11:09:21 +10001457 id = packet_get_int();
1458 c = channel_lookup(id);
1459 if (c == NULL)
1460 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001461
1462 /*
1463 * Send a confirmation that we have closed the channel and no more
1464 * data is coming for it.
1465 */
Damien Miller95def091999-11-25 00:26:21 +11001466 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001467 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001468 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001469
Damien Miller5428f641999-11-25 11:54:57 +11001470 /*
1471 * If the channel is in closed state, we have sent a close request,
1472 * and the other side will eventually respond with a confirmation.
1473 * Thus, we cannot free the channel here, because then there would be
1474 * no-one to receive the confirmation. The channel gets freed when
1475 * the confirmation arrives.
1476 */
Damien Millerb38eff82000-04-01 11:09:21 +10001477 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001478 /*
1479 * Not a closed channel - mark it as draining, which will
1480 * cause it to be freed later.
1481 */
Damien Millerb38eff82000-04-01 11:09:21 +10001482 buffer_consume(&c->input, buffer_len(&c->input));
1483 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001484 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001485}
1486
Damien Millerb38eff82000-04-01 11:09:21 +10001487/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001488void
Damien Miller62cee002000-09-23 17:15:56 +11001489channel_input_oclose(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001490{
Damien Millerb38eff82000-04-01 11:09:21 +10001491 int id = packet_get_int();
1492 Channel *c = channel_lookup(id);
1493 packet_integrity_check(plen, 4, type);
1494 if (c == NULL)
1495 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1496 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001497}
1498
Damien Miller4af51302000-04-16 11:18:38 +10001499void
Damien Miller62cee002000-09-23 17:15:56 +11001500channel_input_close_confirmation(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001501{
1502 int id = packet_get_int();
1503 Channel *c = channel_lookup(id);
1504
Damien Miller4af51302000-04-16 11:18:38 +10001505 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001506 if (c == NULL)
1507 packet_disconnect("Received close confirmation for "
1508 "out-of-range channel %d.", id);
1509 if (c->type != SSH_CHANNEL_CLOSED)
1510 packet_disconnect("Received close confirmation for "
1511 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001512 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001513}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001514
Damien Miller4af51302000-04-16 11:18:38 +10001515void
Damien Miller62cee002000-09-23 17:15:56 +11001516channel_input_open_confirmation(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001517{
Damien Millerb38eff82000-04-01 11:09:21 +10001518 int id, remote_id;
1519 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001520
Damien Miller33b13562000-04-04 14:38:59 +10001521 if (!compat20)
1522 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001523
Damien Millerb38eff82000-04-01 11:09:21 +10001524 id = packet_get_int();
1525 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001526
Damien Millerb38eff82000-04-01 11:09:21 +10001527 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1528 packet_disconnect("Received open confirmation for "
1529 "non-opening channel %d.", id);
1530 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001531 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001532 c->remote_id = remote_id;
1533 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001534
1535 if (compat20) {
1536 c->remote_window = packet_get_int();
1537 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001538 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001539 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001540 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001541 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001542 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001543 }
1544 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1545 c->remote_window, c->remote_maxpacket);
1546 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001547}
1548
Ben Lindstrom69128662001-05-08 20:07:39 +00001549char *
1550reason2txt(int reason)
1551{
1552 switch(reason) {
1553 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
1554 return "administratively prohibited";
1555 case SSH2_OPEN_CONNECT_FAILED:
1556 return "connect failed";
1557 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
1558 return "unknown channel type";
1559 case SSH2_OPEN_RESOURCE_SHORTAGE:
1560 return "resource shortage";
1561 }
1562 return "unkown reason";
1563}
1564
Damien Miller4af51302000-04-16 11:18:38 +10001565void
Damien Miller62cee002000-09-23 17:15:56 +11001566channel_input_open_failure(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001567{
Kevin Steves12057502001-02-05 14:54:34 +00001568 int id, reason;
1569 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001570 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001571
Damien Miller33b13562000-04-04 14:38:59 +10001572 if (!compat20)
1573 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001574
1575 id = packet_get_int();
1576 c = channel_lookup(id);
1577
1578 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1579 packet_disconnect("Received open failure for "
1580 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001581 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00001582 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00001583 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00001584 msg = packet_get_string(NULL);
1585 lang = packet_get_string(NULL);
1586 }
Damien Miller4af51302000-04-16 11:18:38 +10001587 packet_done();
Ben Lindstrom69128662001-05-08 20:07:39 +00001588 log("channel %d: open failed: %s%s%s", id,
1589 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00001590 if (msg != NULL)
1591 xfree(msg);
1592 if (lang != NULL)
1593 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001594 }
Damien Miller95def091999-11-25 00:26:21 +11001595 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001596 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001597}
1598
Damien Miller33b13562000-04-04 14:38:59 +10001599void
Damien Miller62cee002000-09-23 17:15:56 +11001600channel_input_channel_request(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001601{
1602 int id;
1603 Channel *c;
1604
1605 id = packet_get_int();
1606 c = channel_lookup(id);
1607
1608 if (c == NULL ||
1609 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1610 packet_disconnect("Received request for "
1611 "non-open channel %d.", id);
1612 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001613 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001614 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001615 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001616 } else {
1617 char *service = packet_get_string(NULL);
Ben Lindstromcc74df72001-03-05 06:20:14 +00001618 debug("channel %d: rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001619 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001620 xfree(service);
1621 }
1622}
1623
Damien Miller4af51302000-04-16 11:18:38 +10001624void
Damien Miller62cee002000-09-23 17:15:56 +11001625channel_input_window_adjust(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001626{
1627 Channel *c;
1628 int id, adjust;
1629
1630 if (!compat20)
1631 return;
1632
1633 /* Get the channel number and verify it. */
1634 id = packet_get_int();
1635 c = channel_lookup(id);
1636
1637 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1638 log("Received window adjust for "
1639 "non-open channel %d.", id);
1640 return;
1641 }
1642 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001643 packet_done();
Damien Millerd3444942000-09-30 14:20:03 +11001644 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10001645 c->remote_window += adjust;
1646}
1647
Damien Miller5428f641999-11-25 11:54:57 +11001648/*
1649 * Stops listening for channels, and removes any unix domain sockets that we
1650 * might have.
1651 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001652
Damien Miller4af51302000-04-16 11:18:38 +10001653void
Damien Miller95def091999-11-25 00:26:21 +11001654channel_stop_listening()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001655{
Damien Miller95def091999-11-25 00:26:21 +11001656 int i;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001657 Channel *c;
1658
Damien Miller95def091999-11-25 00:26:21 +11001659 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001660 c = channels[i];
1661 if (c != NULL) {
1662 switch (c->type) {
1663 case SSH_CHANNEL_AUTH_SOCKET:
1664 close(c->sock);
1665 unlink(c->path);
1666 channel_free(c);
1667 break;
1668 case SSH_CHANNEL_PORT_LISTENER:
1669 case SSH_CHANNEL_RPORT_LISTENER:
1670 case SSH_CHANNEL_X11_LISTENER:
1671 close(c->sock);
1672 channel_free(c);
1673 break;
1674 default:
1675 break;
1676 }
Damien Miller95def091999-11-25 00:26:21 +11001677 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001678 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001679}
1680
Damien Miller5428f641999-11-25 11:54:57 +11001681/*
Damien Miller6f83b8e2000-05-02 09:23:45 +10001682 * Closes the sockets/fds of all channels. This is used to close extra file
Damien Miller5428f641999-11-25 11:54:57 +11001683 * descriptors after a fork.
1684 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001685
Damien Miller4af51302000-04-16 11:18:38 +10001686void
Damien Miller95def091999-11-25 00:26:21 +11001687channel_close_all()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001688{
Damien Miller95def091999-11-25 00:26:21 +11001689 int i;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001690
Damien Miller6f83b8e2000-05-02 09:23:45 +10001691 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001692 if (channels[i] != NULL)
1693 channel_close_fds(channels[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001694}
1695
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001696/* Returns true if any channel is still open. */
1697
Damien Miller4af51302000-04-16 11:18:38 +10001698int
Damien Miller95def091999-11-25 00:26:21 +11001699channel_still_open()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001700{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001701 int i;
1702 Channel *c;
1703
1704 for (i = 0; i < channels_alloc; i++) {
1705 c = channels[i];
1706 if (c == NULL)
1707 continue;
1708 switch (c->type) {
Damien Miller95def091999-11-25 00:26:21 +11001709 case SSH_CHANNEL_X11_LISTENER:
1710 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001711 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001712 case SSH_CHANNEL_CLOSED:
1713 case SSH_CHANNEL_AUTH_SOCKET:
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001714 case SSH_CHANNEL_DYNAMIC:
Ben Lindstrom69128662001-05-08 20:07:39 +00001715 case SSH_CHANNEL_CONNECTING:
Damien Miller95def091999-11-25 00:26:21 +11001716 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001717 case SSH_CHANNEL_LARVAL:
1718 if (!compat20)
1719 fatal("cannot happen: SSH_CHANNEL_LARVAL");
1720 continue;
Damien Miller95def091999-11-25 00:26:21 +11001721 case SSH_CHANNEL_OPENING:
1722 case SSH_CHANNEL_OPEN:
1723 case SSH_CHANNEL_X11_OPEN:
1724 return 1;
1725 case SSH_CHANNEL_INPUT_DRAINING:
1726 case SSH_CHANNEL_OUTPUT_DRAINING:
1727 if (!compat13)
1728 fatal("cannot happen: OUT_DRAIN");
1729 return 1;
1730 default:
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001731 fatal("channel_still_open: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001732 /* NOTREACHED */
1733 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001734 }
Damien Miller95def091999-11-25 00:26:21 +11001735 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001736}
1737
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001738/* Returns the id of an open channel suitable for keepaliving */
1739
1740int
1741channel_find_open()
1742{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001743 int i;
1744 Channel *c;
1745
1746 for (i = 0; i < channels_alloc; i++) {
1747 c = channels[i];
1748 if (c == NULL)
1749 continue;
1750 switch (c->type) {
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001751 case SSH_CHANNEL_CLOSED:
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001752 case SSH_CHANNEL_DYNAMIC:
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001753 case SSH_CHANNEL_X11_LISTENER:
1754 case SSH_CHANNEL_PORT_LISTENER:
1755 case SSH_CHANNEL_RPORT_LISTENER:
1756 case SSH_CHANNEL_OPENING:
Ben Lindstrom69128662001-05-08 20:07:39 +00001757 case SSH_CHANNEL_CONNECTING:
Ben Lindstromd334b272001-04-14 23:08:36 +00001758 continue;
1759 case SSH_CHANNEL_LARVAL:
1760 case SSH_CHANNEL_AUTH_SOCKET:
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001761 case SSH_CHANNEL_OPEN:
1762 case SSH_CHANNEL_X11_OPEN:
1763 return i;
1764 case SSH_CHANNEL_INPUT_DRAINING:
1765 case SSH_CHANNEL_OUTPUT_DRAINING:
1766 if (!compat13)
1767 fatal("cannot happen: OUT_DRAIN");
1768 return i;
1769 default:
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001770 fatal("channel_find_open: bad channel type %d", c->type);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001771 /* NOTREACHED */
1772 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001773 }
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001774 return -1;
1775}
1776
1777
Damien Miller5428f641999-11-25 11:54:57 +11001778/*
1779 * Returns a message describing the currently open forwarded connections,
1780 * suitable for sending to the client. The message contains crlf pairs for
1781 * newlines.
1782 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001783
Damien Miller95def091999-11-25 00:26:21 +11001784char *
1785channel_open_message()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001786{
Damien Miller95def091999-11-25 00:26:21 +11001787 Buffer buffer;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001788 Channel *c;
1789 char buf[1024], *cp;
Damien Miller95def091999-11-25 00:26:21 +11001790 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001791
Damien Miller95def091999-11-25 00:26:21 +11001792 buffer_init(&buffer);
1793 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001794 buffer_append(&buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001795 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001796 c = channels[i];
1797 if (c == NULL)
1798 continue;
Damien Miller95def091999-11-25 00:26:21 +11001799 switch (c->type) {
Damien Miller95def091999-11-25 00:26:21 +11001800 case SSH_CHANNEL_X11_LISTENER:
1801 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001802 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001803 case SSH_CHANNEL_CLOSED:
1804 case SSH_CHANNEL_AUTH_SOCKET:
1805 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001806 case SSH_CHANNEL_LARVAL:
Damien Miller95def091999-11-25 00:26:21 +11001807 case SSH_CHANNEL_OPENING:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001808 case SSH_CHANNEL_CONNECTING:
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001809 case SSH_CHANNEL_DYNAMIC:
Damien Miller95def091999-11-25 00:26:21 +11001810 case SSH_CHANNEL_OPEN:
1811 case SSH_CHANNEL_X11_OPEN:
1812 case SSH_CHANNEL_INPUT_DRAINING:
1813 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Millerb38eff82000-04-01 11:09:21 +10001814 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 +11001815 c->self, c->remote_name,
1816 c->type, c->remote_id,
1817 c->istate, buffer_len(&c->input),
Damien Millerb38eff82000-04-01 11:09:21 +10001818 c->ostate, buffer_len(&c->output),
1819 c->rfd, c->wfd);
Damien Miller95def091999-11-25 00:26:21 +11001820 buffer_append(&buffer, buf, strlen(buf));
1821 continue;
1822 default:
Damien Millerb38eff82000-04-01 11:09:21 +10001823 fatal("channel_open_message: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001824 /* NOTREACHED */
1825 }
1826 }
1827 buffer_append(&buffer, "\0", 1);
1828 cp = xstrdup(buffer_ptr(&buffer));
1829 buffer_free(&buffer);
1830 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001831}
1832
Damien Miller5428f641999-11-25 11:54:57 +11001833/*
1834 * Initiate forwarding of connections to local port "port" through the secure
1835 * channel to host:port from remote side.
1836 */
Kevin Steves12057502001-02-05 14:54:34 +00001837int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001838channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
1839 u_short port_to_connect, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001840{
Kevin Steves12057502001-02-05 14:54:34 +00001841 return channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +11001842 NULL, listen_port,
1843 host_to_connect, port_to_connect,
1844 gateway_ports, /*remote_fwd*/ 0);
1845}
1846
1847/*
1848 * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
1849 * (SSH_CHANNEL_RPORT_LISTENER).
1850 */
Kevin Steves12057502001-02-05 14:54:34 +00001851int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001852channel_request_forwarding(
1853 const char *listen_address, u_short listen_port,
1854 const char *host_to_connect, u_short port_to_connect,
1855 int gateway_ports, int remote_fwd)
1856{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001857 Channel *c;
1858 int success, sock, on = 1, ctype;
Damien Miller34132e52000-01-14 15:45:46 +11001859 struct addrinfo hints, *ai, *aitop;
1860 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0bc1bd82000-11-13 22:57:25 +11001861 const char *host;
Damien Miller5428f641999-11-25 11:54:57 +11001862 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001863
Kevin Steves12057502001-02-05 14:54:34 +00001864 success = 0;
1865
Damien Miller0bc1bd82000-11-13 22:57:25 +11001866 if (remote_fwd) {
1867 host = listen_address;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001868 ctype = SSH_CHANNEL_RPORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001869 } else {
1870 host = host_to_connect;
1871 ctype =SSH_CHANNEL_PORT_LISTENER;
1872 }
1873
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001874 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00001875 error("Forward host name too long.");
1876 return success;
1877 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001878
Damien Miller0bc1bd82000-11-13 22:57:25 +11001879 /* XXX listen_address is currently ignored */
Damien Miller5428f641999-11-25 11:54:57 +11001880 /*
Damien Miller34132e52000-01-14 15:45:46 +11001881 * getaddrinfo returns a loopback address if the hostname is
1882 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11001883 */
Damien Miller34132e52000-01-14 15:45:46 +11001884 memset(&hints, 0, sizeof(hints));
1885 hints.ai_family = IPv4or6;
1886 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
1887 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001888 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001889 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
1890 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11001891
Damien Miller34132e52000-01-14 15:45:46 +11001892 for (ai = aitop; ai; ai = ai->ai_next) {
1893 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1894 continue;
1895 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1896 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001897 error("channel_request_forwarding: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001898 continue;
1899 }
1900 /* Create a port to listen for the host. */
1901 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1902 if (sock < 0) {
1903 /* this is no error since kernel may not support ipv6 */
1904 verbose("socket: %.100s", strerror(errno));
1905 continue;
1906 }
1907 /*
1908 * Set socket options. We would like the socket to disappear
1909 * as soon as it has been closed for whatever reason.
1910 */
1911 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
1912 linger.l_onoff = 1;
1913 linger.l_linger = 5;
1914 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
1915 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001916
Damien Miller34132e52000-01-14 15:45:46 +11001917 /* Bind the socket to the address. */
1918 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1919 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11001920 if (!ai->ai_next)
1921 error("bind: %.100s", strerror(errno));
1922 else
1923 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001924
Damien Miller34132e52000-01-14 15:45:46 +11001925 close(sock);
1926 continue;
1927 }
1928 /* Start listening for connections on the socket. */
1929 if (listen(sock, 5) < 0) {
1930 error("listen: %.100s", strerror(errno));
1931 close(sock);
1932 continue;
1933 }
1934 /* Allocate a channel number for the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001935 c = channel_new("port listener", ctype, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10001936 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11001937 0, xstrdup("port listener"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001938 if (c == NULL) {
1939 error("channel_request_forwarding: channel_new failed");
1940 close(sock);
1941 continue;
1942 }
1943 strlcpy(c->path, host, sizeof(c->path));
1944 c->host_port = port_to_connect;
1945 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11001946 success = 1;
1947 }
1948 if (success == 0)
Kevin Steves12057502001-02-05 14:54:34 +00001949 error("channel_request_forwarding: cannot listen to port: %d",
1950 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001951 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00001952 return success;
Damien Miller95def091999-11-25 00:26:21 +11001953}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001954
Damien Miller5428f641999-11-25 11:54:57 +11001955/*
1956 * Initiate forwarding of connections to port "port" on remote host through
1957 * the secure channel to host:port from local side.
1958 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001959
Damien Miller4af51302000-04-16 11:18:38 +10001960void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001961channel_request_remote_forwarding(u_short listen_port,
1962 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001963{
Damien Miller0bc1bd82000-11-13 22:57:25 +11001964 int payload_len, type, success = 0;
1965
Damien Miller95def091999-11-25 00:26:21 +11001966 /* Record locally that connection to this host/port is permitted. */
1967 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1968 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001969
Damien Miller95def091999-11-25 00:26:21 +11001970 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10001971 if (compat20) {
1972 const char *address_to_bind = "0.0.0.0";
1973 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1974 packet_put_cstring("tcpip-forward");
1975 packet_put_char(0); /* boolean: want reply */
1976 packet_put_cstring(address_to_bind);
1977 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001978 packet_send();
1979 packet_write_wait();
1980 /* Assume that server accepts the request */
1981 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10001982 } else {
1983 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10001984 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10001985 packet_put_cstring(host_to_connect);
1986 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10001987 packet_send();
1988 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001989
1990 /* Wait for response from the remote side. */
1991 type = packet_read(&payload_len);
1992 switch (type) {
1993 case SSH_SMSG_SUCCESS:
1994 success = 1;
1995 break;
1996 case SSH_SMSG_FAILURE:
1997 log("Warning: Server denied remote port forwarding.");
1998 break;
1999 default:
2000 /* Unknown packet */
2001 packet_disconnect("Protocol error for port forward request:"
2002 "received packet type %d.", type);
2003 }
2004 }
2005 if (success) {
2006 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2007 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2008 permitted_opens[num_permitted_opens].listen_port = listen_port;
2009 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002010 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002011}
2012
Damien Miller5428f641999-11-25 11:54:57 +11002013/*
2014 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2015 * listening for the port, and sends back a success reply (or disconnect
2016 * message if there was an error). This never returns if there was an error.
2017 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002018
Damien Miller4af51302000-04-16 11:18:38 +10002019void
Damien Millere247cc42000-05-07 12:03:14 +10002020channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002021{
Damien Milleraae6c611999-12-06 11:47:28 +11002022 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11002023 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002024
Damien Miller95def091999-11-25 00:26:21 +11002025 /* Get arguments from the packet. */
2026 port = packet_get_int();
2027 hostname = packet_get_string(NULL);
2028 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002029
Damien Millerbac2d8a2000-09-05 16:13:06 +11002030#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002031 /*
2032 * Check that an unprivileged user is not trying to forward a
2033 * privileged port.
2034 */
Damien Miller95def091999-11-25 00:26:21 +11002035 if (port < IPPORT_RESERVED && !is_root)
2036 packet_disconnect("Requested forwarding of port %d but user is not root.",
2037 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11002038#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11002039 /* Initiate forwarding */
Damien Millere247cc42000-05-07 12:03:14 +10002040 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002041
2042 /* Free the argument string. */
2043 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002044}
2045
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002046/*
2047 * Permits opening to any host/port if permitted_opens[] is empty. This is
2048 * usually called by the server, because the user could connect to any port
2049 * anyway, and the server has no way to know but to trust the client anyway.
2050 */
2051void
2052channel_permit_all_opens()
2053{
2054 if (num_permitted_opens == 0)
2055 all_opens_permitted = 1;
2056}
2057
Ben Lindstroma3700052001-04-05 23:26:32 +00002058void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002059channel_add_permitted_opens(char *host, int port)
2060{
2061 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2062 fatal("channel_request_remote_forwarding: too many forwards");
2063 debug("allow port forwarding to host %s port %d", host, port);
2064
2065 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2066 permitted_opens[num_permitted_opens].port_to_connect = port;
2067 num_permitted_opens++;
2068
2069 all_opens_permitted = 0;
2070}
2071
Ben Lindstroma3700052001-04-05 23:26:32 +00002072void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002073channel_clear_permitted_opens(void)
2074{
2075 int i;
2076
2077 for (i = 0; i < num_permitted_opens; i++)
2078 xfree(permitted_opens[i].host_to_connect);
2079 num_permitted_opens = 0;
2080
2081}
2082
2083
2084/* return socket to remote host, port */
Damien Millerb38eff82000-04-01 11:09:21 +10002085int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002086connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002087{
Damien Miller34132e52000-01-14 15:45:46 +11002088 struct addrinfo hints, *ai, *aitop;
2089 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2090 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002091 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002092
Damien Miller34132e52000-01-14 15:45:46 +11002093 memset(&hints, 0, sizeof(hints));
2094 hints.ai_family = IPv4or6;
2095 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002096 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002097 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002098 error("connect_to %.100s: unknown host (%s)", host,
2099 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002100 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002101 }
Damien Miller34132e52000-01-14 15:45:46 +11002102 for (ai = aitop; ai; ai = ai->ai_next) {
2103 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2104 continue;
2105 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2106 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002107 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002108 continue;
2109 }
Damien Miller34132e52000-01-14 15:45:46 +11002110 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2111 if (sock < 0) {
2112 error("socket: %.100s", strerror(errno));
2113 continue;
2114 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +00002115 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002116 fatal("connect_to: F_SETFL: %s", strerror(errno));
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002117 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2118 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002119 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002120 strerror(errno));
2121 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002122 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002123 }
2124 break; /* success */
2125
2126 }
2127 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002128 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002129 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002130 return -1;
2131 }
2132 /* success */
2133 return sock;
2134}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002135
Damien Miller0bc1bd82000-11-13 22:57:25 +11002136int
2137channel_connect_by_listen_adress(u_short listen_port)
2138{
2139 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002140
Damien Miller0bc1bd82000-11-13 22:57:25 +11002141 for (i = 0; i < num_permitted_opens; i++)
2142 if (permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002143 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002144 permitted_opens[i].host_to_connect,
2145 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002146 error("WARNING: Server requests forwarding for unknown listen_port %d",
2147 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002148 return -1;
2149}
Damien Millerb38eff82000-04-01 11:09:21 +10002150
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002151/* Check if connecting to that port is permitted and connect. */
2152int
2153channel_connect_to(const char *host, u_short port)
2154{
2155 int i, permit;
2156
2157 permit = all_opens_permitted;
2158 if (!permit) {
2159 for (i = 0; i < num_permitted_opens; i++)
2160 if (permitted_opens[i].port_to_connect == port &&
2161 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2162 permit = 1;
2163
2164 }
2165 if (!permit) {
2166 log("Received request to connect to host %.100s port %d, "
2167 "but the request was denied.", host, port);
2168 return -1;
2169 }
2170 return connect_to(host, port);
2171}
2172
Damien Millerb38eff82000-04-01 11:09:21 +10002173/*
2174 * This is called after receiving PORT_OPEN message. This attempts to
2175 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
2176 * or CHANNEL_OPEN_FAILURE.
2177 */
2178
Damien Miller4af51302000-04-16 11:18:38 +10002179void
Damien Miller62cee002000-09-23 17:15:56 +11002180channel_input_port_open(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002181{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002182 Channel *c = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002183 u_short host_port;
2184 char *host, *originator_string;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002185 int remote_id, sock = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002186
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002187 remote_id = packet_get_int();
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002188 host = packet_get_string(NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10002189 host_port = packet_get_int();
2190
Damien Millerb38eff82000-04-01 11:09:21 +10002191 if (have_hostname_in_open) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002192 originator_string = packet_get_string(NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10002193 } else {
2194 originator_string = xstrdup("unknown (remote did not supply name)");
Damien Miller95def091999-11-25 00:26:21 +11002195 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002196 packet_done();
2197 sock = channel_connect_to(host, host_port);
2198 if (sock != -1) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002199 c = channel_new("connected socket",
2200 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2201 originator_string, 1);
2202 if (c == NULL) {
2203 error("channel_input_port_open: channel_new failed");
2204 close(sock);
2205 } else {
2206 c->remote_id = remote_id;
2207 }
2208 }
2209 if (c == NULL) {
2210 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2211 packet_put_int(remote_id);
Ben Lindstrom69128662001-05-08 20:07:39 +00002212 packet_send();
Damien Millerb38eff82000-04-01 11:09:21 +10002213 }
Damien Miller95def091999-11-25 00:26:21 +11002214 xfree(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002215}
2216
Damien Miller5428f641999-11-25 11:54:57 +11002217/*
2218 * Creates an internet domain socket for listening for X11 connections.
2219 * Returns a suitable value for the DISPLAY variable, or NULL if an error
2220 * occurs.
2221 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002222
Damien Miller34132e52000-01-14 15:45:46 +11002223#define NUM_SOCKS 10
2224
Damien Miller95def091999-11-25 00:26:21 +11002225char *
Damien Millera34a28b1999-12-14 10:47:15 +11002226x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002227{
Damien Milleraae6c611999-12-06 11:47:28 +11002228 int display_number, sock;
2229 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002230 struct addrinfo hints, *ai, *aitop;
2231 char strport[NI_MAXSERV];
2232 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
2233 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11002234 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002235
Damien Millera34a28b1999-12-14 10:47:15 +11002236 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11002237 display_number < MAX_DISPLAYS;
2238 display_number++) {
2239 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002240 memset(&hints, 0, sizeof(hints));
2241 hints.ai_family = IPv4or6;
2242 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
2243 hints.ai_socktype = SOCK_STREAM;
2244 snprintf(strport, sizeof strport, "%d", port);
2245 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2246 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11002247 return NULL;
2248 }
Damien Miller34132e52000-01-14 15:45:46 +11002249 for (ai = aitop; ai; ai = ai->ai_next) {
2250 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2251 continue;
2252 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2253 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002254 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002255 error("socket: %.100s", strerror(errno));
2256 return NULL;
2257 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002258 debug("x11_create_display_inet: Socket family %d not supported",
2259 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002260 continue;
2261 }
Damien Miller34132e52000-01-14 15:45:46 +11002262 }
2263 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2264 debug("bind port %d: %.100s", port, strerror(errno));
2265 shutdown(sock, SHUT_RDWR);
2266 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002267
2268 if (ai->ai_next)
2269 continue;
2270
Damien Miller34132e52000-01-14 15:45:46 +11002271 for (n = 0; n < num_socks; n++) {
2272 shutdown(socks[n], SHUT_RDWR);
2273 close(socks[n]);
2274 }
2275 num_socks = 0;
2276 break;
2277 }
2278 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002279#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002280 if (num_socks == NUM_SOCKS)
2281 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002282#else
2283 break;
2284#endif
Damien Miller95def091999-11-25 00:26:21 +11002285 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002286 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002287 if (num_socks > 0)
2288 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002289 }
Damien Miller95def091999-11-25 00:26:21 +11002290 if (display_number >= MAX_DISPLAYS) {
2291 error("Failed to allocate internet-domain X11 display socket.");
2292 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002293 }
Damien Miller95def091999-11-25 00:26:21 +11002294 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002295 for (n = 0; n < num_socks; n++) {
2296 sock = socks[n];
2297 if (listen(sock, 5) < 0) {
2298 error("listen: %.100s", strerror(errno));
2299 shutdown(sock, SHUT_RDWR);
2300 close(sock);
2301 return NULL;
2302 }
Damien Miller95def091999-11-25 00:26:21 +11002303 }
Damien Miller34132e52000-01-14 15:45:46 +11002304
Damien Miller95def091999-11-25 00:26:21 +11002305 /* Set up a suitable value for the DISPLAY variable. */
2306 if (gethostname(hostname, sizeof(hostname)) < 0)
2307 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11002308
2309#ifdef IPADDR_IN_DISPLAY
Kevin Stevesef4eea92001-02-05 12:42:17 +00002310 /*
Damien Miller76112de1999-12-21 11:18:08 +11002311 * HPUX detects the local hostname in the DISPLAY variable and tries
2312 * to set up a shared memory connection to the server, which it
2313 * incorrectly supposes to be local.
2314 *
2315 * The workaround - as used in later $$H and other programs - is
2316 * is to set display to the host's IP address.
2317 */
2318 {
2319 struct hostent *he;
2320 struct in_addr my_addr;
2321
2322 he = gethostbyname(hostname);
2323 if (he == NULL) {
2324 error("[X11-broken-fwd-hostname-workaround] Could not get "
2325 "IP address for hostname %s.", hostname);
2326
2327 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
2328 "Could not get IP address for hostname %s.", hostname);
2329
2330 shutdown(sock, SHUT_RDWR);
2331 close(sock);
2332
2333 return NULL;
2334 }
2335
2336 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2337
2338 /* Set DISPLAY to <ip address>:screen.display */
Kevin Stevesef4eea92001-02-05 12:42:17 +00002339 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Kevin Stevesda6cd592000-12-29 18:41:21 +00002340 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002341 }
2342#else /* IPADDR_IN_DISPLAY */
2343 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11002344 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Kevin Stevesda6cd592000-12-29 18:41:21 +00002345 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002346#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002347
Damien Miller34132e52000-01-14 15:45:46 +11002348 /* Allocate a channel for each socket. */
2349 for (n = 0; n < num_socks; n++) {
2350 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10002351 (void) channel_new("x11 listener",
2352 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2353 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002354 0, xstrdup("X11 inet listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11002355 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002356
Damien Miller95def091999-11-25 00:26:21 +11002357 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11002358 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002359}
2360
2361#ifndef X_UNIX_PATH
2362#define X_UNIX_PATH "/tmp/.X11-unix/X"
2363#endif
2364
2365static
2366int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002367connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002368{
Damien Miller95def091999-11-25 00:26:21 +11002369 static const char *const x_sockets[] = {
2370 X_UNIX_PATH "%u",
2371 "/var/X/.X11-unix/X" "%u",
2372 "/usr/spool/sockets/X11/" "%u",
2373 NULL
2374 };
2375 int sock;
2376 struct sockaddr_un addr;
2377 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002378
Damien Miller95def091999-11-25 00:26:21 +11002379 for (path = x_sockets; *path; ++path) {
2380 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2381 if (sock < 0)
2382 error("socket: %.100s", strerror(errno));
2383 memset(&addr, 0, sizeof(addr));
2384 addr.sun_family = AF_UNIX;
2385 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
2386 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2387 return sock;
2388 close(sock);
2389 }
2390 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2391 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002392}
2393
Damien Millerbd483e72000-04-30 10:00:53 +10002394int
2395x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002396{
Damien Millerbd483e72000-04-30 10:00:53 +10002397 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002398 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002399 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002400 struct addrinfo hints, *ai, *aitop;
2401 char strport[NI_MAXSERV];
2402 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002403
Damien Miller95def091999-11-25 00:26:21 +11002404 /* Try to open a socket for the local X server. */
2405 display = getenv("DISPLAY");
2406 if (!display) {
2407 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002408 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002409 }
Damien Miller5428f641999-11-25 11:54:57 +11002410 /*
2411 * Now we decode the value of the DISPLAY variable and make a
2412 * connection to the real X server.
2413 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002414
Damien Miller5428f641999-11-25 11:54:57 +11002415 /*
2416 * Check if it is a unix domain socket. Unix domain displays are in
2417 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2418 */
Damien Miller95def091999-11-25 00:26:21 +11002419 if (strncmp(display, "unix:", 5) == 0 ||
2420 display[0] == ':') {
2421 /* Connect to the unix domain socket. */
2422 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2423 error("Could not parse display number from DISPLAY: %.100s",
2424 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002425 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002426 }
2427 /* Create a socket. */
2428 sock = connect_local_xsocket(display_number);
2429 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002430 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002431
2432 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002433 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002434 }
Damien Miller5428f641999-11-25 11:54:57 +11002435 /*
2436 * Connect to an inet socket. The DISPLAY value is supposedly
2437 * hostname:d[.s], where hostname may also be numeric IP address.
2438 */
Damien Miller95def091999-11-25 00:26:21 +11002439 strncpy(buf, display, sizeof(buf));
2440 buf[sizeof(buf) - 1] = 0;
2441 cp = strchr(buf, ':');
2442 if (!cp) {
2443 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002444 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002445 }
Damien Miller95def091999-11-25 00:26:21 +11002446 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002447 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002448 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2449 error("Could not parse display number from DISPLAY: %.100s",
2450 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002451 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002452 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002453
Damien Miller34132e52000-01-14 15:45:46 +11002454 /* Look up the host address */
2455 memset(&hints, 0, sizeof(hints));
2456 hints.ai_family = IPv4or6;
2457 hints.ai_socktype = SOCK_STREAM;
2458 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2459 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2460 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002461 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002462 }
Damien Miller34132e52000-01-14 15:45:46 +11002463 for (ai = aitop; ai; ai = ai->ai_next) {
2464 /* Create a socket. */
2465 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2466 if (sock < 0) {
2467 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002468 continue;
2469 }
2470 /* Connect it to the display. */
2471 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2472 debug("connect %.100s port %d: %.100s", buf,
2473 6000 + display_number, strerror(errno));
2474 close(sock);
2475 continue;
2476 }
2477 /* Success */
2478 break;
Damien Miller34132e52000-01-14 15:45:46 +11002479 }
Damien Miller34132e52000-01-14 15:45:46 +11002480 freeaddrinfo(aitop);
2481 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002482 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002483 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002484 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002485 }
Damien Millerbd483e72000-04-30 10:00:53 +10002486 return sock;
2487}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002488
Damien Millerbd483e72000-04-30 10:00:53 +10002489/*
2490 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2491 * the remote channel number. We should do whatever we want, and respond
2492 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2493 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002494
Damien Millerbd483e72000-04-30 10:00:53 +10002495void
Damien Miller62cee002000-09-23 17:15:56 +11002496x11_input_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002497{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002498 Channel *c = NULL;
2499 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002500 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10002501
2502 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002503
2504 remote_id = packet_get_int();
2505 if (have_hostname_in_open) {
2506 remote_host = packet_get_string(NULL);
2507 } else {
2508 remote_host = xstrdup("unknown (remote did not supply name)");
2509 }
2510 packet_done();
Damien Millerbd483e72000-04-30 10:00:53 +10002511
2512 /* Obtain a connection to the real X display. */
2513 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002514 if (sock != -1) {
2515 /* Allocate a channel for this connection. */
2516 c = channel_new("connected x11 socket",
2517 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
2518 remote_host, 1);
2519 if (c == NULL) {
2520 error("x11_input_open: channel_new failed");
2521 close(sock);
2522 } else {
2523 c->remote_id = remote_id;
2524 }
2525 }
2526 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10002527 /* Send refusal to the remote host. */
2528 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002529 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10002530 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10002531 /* Send a confirmation to the remote host. */
2532 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002533 packet_put_int(remote_id);
2534 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10002535 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002536 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002537}
2538
Damien Miller69b69aa2000-10-28 14:19:58 +11002539/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2540void
2541deny_input_open(int type, int plen, void *ctxt)
2542{
2543 int rchan = packet_get_int();
2544 switch(type){
2545 case SSH_SMSG_AGENT_OPEN:
2546 error("Warning: ssh server tried agent forwarding.");
2547 break;
2548 case SSH_SMSG_X11_OPEN:
2549 error("Warning: ssh server tried X11 forwarding.");
2550 break;
2551 default:
2552 error("deny_input_open: type %d plen %d", type, plen);
2553 break;
2554 }
2555 error("Warning: this is probably a break in attempt by a malicious server.");
2556 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2557 packet_put_int(rchan);
2558 packet_send();
2559}
2560
Damien Miller5428f641999-11-25 11:54:57 +11002561/*
2562 * Requests forwarding of X11 connections, generates fake authentication
2563 * data, and enables authentication spoofing.
2564 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002565
Damien Miller4af51302000-04-16 11:18:38 +10002566void
Damien Millerbd483e72000-04-30 10:00:53 +10002567x11_request_forwarding_with_spoofing(int client_session_id,
2568 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002569{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002570 u_int data_len = (u_int) strlen(data) / 2;
Ben Lindstromb3211a82001-02-10 22:33:19 +00002571 u_int i, value, len;
Damien Miller95def091999-11-25 00:26:21 +11002572 char *new_data;
2573 int screen_number;
2574 const char *cp;
2575 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002576
Damien Miller95def091999-11-25 00:26:21 +11002577 cp = getenv("DISPLAY");
2578 if (cp)
2579 cp = strchr(cp, ':');
2580 if (cp)
2581 cp = strchr(cp, '.');
2582 if (cp)
2583 screen_number = atoi(cp + 1);
2584 else
2585 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002586
Damien Miller95def091999-11-25 00:26:21 +11002587 /* Save protocol name. */
2588 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002589
Damien Miller5428f641999-11-25 11:54:57 +11002590 /*
2591 * Extract real authentication data and generate fake data of the
2592 * same length.
2593 */
Damien Miller95def091999-11-25 00:26:21 +11002594 x11_saved_data = xmalloc(data_len);
2595 x11_fake_data = xmalloc(data_len);
2596 for (i = 0; i < data_len; i++) {
2597 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2598 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2599 if (i % 4 == 0)
2600 rand = arc4random();
2601 x11_saved_data[i] = value;
2602 x11_fake_data[i] = rand & 0xff;
2603 rand >>= 8;
2604 }
2605 x11_saved_data_len = data_len;
2606 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002607
Damien Miller95def091999-11-25 00:26:21 +11002608 /* Convert the fake data into hex. */
Ben Lindstromb3211a82001-02-10 22:33:19 +00002609 len = 2 * data_len + 1;
2610 new_data = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +11002611 for (i = 0; i < data_len; i++)
Ben Lindstromb3211a82001-02-10 22:33:19 +00002612 snprintf(new_data + 2 * i, len - 2 * i,
2613 "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002614
Damien Miller95def091999-11-25 00:26:21 +11002615 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002616 if (compat20) {
2617 channel_request_start(client_session_id, "x11-req", 0);
2618 packet_put_char(0); /* XXX bool single connection */
2619 } else {
2620 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2621 }
2622 packet_put_cstring(proto);
2623 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002624 packet_put_int(screen_number);
2625 packet_send();
2626 packet_write_wait();
2627 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002628}
2629
2630/* Sends a message to the server to request authentication fd forwarding. */
2631
Damien Miller4af51302000-04-16 11:18:38 +10002632void
Damien Miller95def091999-11-25 00:26:21 +11002633auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002634{
Damien Miller95def091999-11-25 00:26:21 +11002635 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2636 packet_send();
2637 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002638}
2639
Damien Miller5428f641999-11-25 11:54:57 +11002640/*
2641 * Returns the name of the forwarded authentication socket. Returns NULL if
2642 * there is no forwarded authentication socket. The returned value points to
2643 * a static buffer.
2644 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002645
Damien Miller95def091999-11-25 00:26:21 +11002646char *
2647auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002648{
Damien Miller95def091999-11-25 00:26:21 +11002649 return channel_forwarded_auth_socket_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002650}
2651
2652/* removes the agent forwarding socket */
2653
Damien Miller4af51302000-04-16 11:18:38 +10002654void
Damien Miller95def091999-11-25 00:26:21 +11002655cleanup_socket(void)
2656{
Damien Miller78315eb2000-09-29 23:01:36 +11002657 unlink(channel_forwarded_auth_socket_name);
Damien Miller95def091999-11-25 00:26:21 +11002658 rmdir(channel_forwarded_auth_socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002659}
2660
Damien Miller5428f641999-11-25 11:54:57 +11002661/*
Damien Millerd3a18572000-06-07 19:55:44 +10002662 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002663 * This starts forwarding authentication requests.
2664 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002665
Damien Millerd3a18572000-06-07 19:55:44 +10002666int
Damien Miller95def091999-11-25 00:26:21 +11002667auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002668{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002669 Channel *nc;
2670 int sock;
Damien Miller95def091999-11-25 00:26:21 +11002671 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002672
Damien Miller95def091999-11-25 00:26:21 +11002673 if (auth_get_socket_name() != NULL)
2674 fatal("Protocol error: authentication forwarding requested twice.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002675
Damien Miller95def091999-11-25 00:26:21 +11002676 /* Temporarily drop privileged uid for mkdir/bind. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00002677 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002678
Damien Miller95def091999-11-25 00:26:21 +11002679 /* Allocate a buffer for the socket name, and format the name. */
2680 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
2681 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
2682 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002683
Damien Miller95def091999-11-25 00:26:21 +11002684 /* Create private directory for socket */
Damien Millerd3a18572000-06-07 19:55:44 +10002685 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL) {
2686 packet_send_debug("Agent forwarding disabled: mkdtemp() failed: %.100s",
2687 strerror(errno));
2688 restore_uid();
2689 xfree(channel_forwarded_auth_socket_name);
2690 xfree(channel_forwarded_auth_socket_dir);
2691 channel_forwarded_auth_socket_name = NULL;
2692 channel_forwarded_auth_socket_dir = NULL;
2693 return 0;
2694 }
Damien Miller95def091999-11-25 00:26:21 +11002695 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
2696 channel_forwarded_auth_socket_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002697
Damien Miller95def091999-11-25 00:26:21 +11002698 if (atexit(cleanup_socket) < 0) {
2699 int saved = errno;
2700 cleanup_socket();
2701 packet_disconnect("socket: %.100s", strerror(saved));
2702 }
2703 /* Create the socket. */
2704 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2705 if (sock < 0)
2706 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002707
Damien Miller95def091999-11-25 00:26:21 +11002708 /* Bind it to the name. */
2709 memset(&sunaddr, 0, sizeof(sunaddr));
2710 sunaddr.sun_family = AF_UNIX;
2711 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
2712 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002713
Damien Miller95def091999-11-25 00:26:21 +11002714 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2715 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002716
Damien Miller95def091999-11-25 00:26:21 +11002717 /* Restore the privileged uid. */
2718 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002719
Damien Miller95def091999-11-25 00:26:21 +11002720 /* Start listening on the socket. */
2721 if (listen(sock, 5) < 0)
2722 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002723
Damien Miller95def091999-11-25 00:26:21 +11002724 /* Allocate a channel for the authentication agent socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002725 nc = channel_new("auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11002726 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2727 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2728 0, xstrdup("auth socket"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002729 if (nc == NULL) {
2730 error("auth_input_request_forwarding: channel_new failed");
2731 close(sock);
2732 return 0;
2733 }
2734 strlcpy(nc->path, channel_forwarded_auth_socket_name, sizeof(nc->path));
Damien Millerd3a18572000-06-07 19:55:44 +10002735 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002736}
2737
2738/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2739
Damien Miller4af51302000-04-16 11:18:38 +10002740void
Damien Miller62cee002000-09-23 17:15:56 +11002741auth_input_open_request(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002742{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002743 Channel *c = NULL;
2744 int remote_id, sock;
Damien Miller95def091999-11-25 00:26:21 +11002745 char *dummyname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002746
Damien Millerb38eff82000-04-01 11:09:21 +10002747 packet_integrity_check(plen, 4, type);
2748
Damien Miller95def091999-11-25 00:26:21 +11002749 /* Read the remote channel number from the message. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002750 remote_id = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002751
Damien Miller5428f641999-11-25 11:54:57 +11002752 /*
2753 * Get a connection to the local authentication agent (this may again
2754 * get forwarded).
2755 */
Damien Miller95def091999-11-25 00:26:21 +11002756 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002757
Damien Miller5428f641999-11-25 11:54:57 +11002758 /*
2759 * If we could not connect the agent, send an error message back to
2760 * the server. This should never happen unless the agent dies,
2761 * because authentication forwarding is only enabled if we have an
2762 * agent.
2763 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002764 if (sock >= 0) {
2765 dummyname = xstrdup("authentication agent connection");
2766 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock, -1, 0, 0, 0, dummyname, 1);
2767 if (c == NULL) {
2768 error("auth_input_open_request: channel_new failed");
2769 close(sock);
2770 } else {
2771 c->remote_id = remote_id;
2772 }
Damien Miller95def091999-11-25 00:26:21 +11002773 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002774 if (c == NULL) {
2775 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2776 packet_put_int(remote_id);
2777 } else {
2778 /* Send a confirmation to the remote host. */
2779 debug("Forwarding authentication connection.");
2780 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2781 packet_put_int(remote_id);
2782 packet_put_int(c->self);
2783 }
Damien Miller95def091999-11-25 00:26:21 +11002784 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002785}
Damien Miller33b13562000-04-04 14:38:59 +10002786
2787void
Damien Millerbd483e72000-04-30 10:00:53 +10002788channel_start_open(int id)
Damien Miller33b13562000-04-04 14:38:59 +10002789{
2790 Channel *c = channel_lookup(id);
2791 if (c == NULL) {
2792 log("channel_open: %d: bad id", id);
2793 return;
2794 }
Damien Millerbd483e72000-04-30 10:00:53 +10002795 debug("send channel open %d", id);
Damien Miller33b13562000-04-04 14:38:59 +10002796 packet_start(SSH2_MSG_CHANNEL_OPEN);
2797 packet_put_cstring(c->ctype);
2798 packet_put_int(c->self);
2799 packet_put_int(c->local_window);
2800 packet_put_int(c->local_maxpacket);
Damien Millerbd483e72000-04-30 10:00:53 +10002801}
2802void
2803channel_open(int id)
2804{
2805 /* XXX REMOVE ME */
2806 channel_start_open(id);
Damien Miller33b13562000-04-04 14:38:59 +10002807 packet_send();
Damien Miller33b13562000-04-04 14:38:59 +10002808}
2809void
2810channel_request(int id, char *service, int wantconfirm)
2811{
2812 channel_request_start(id, service, wantconfirm);
2813 packet_send();
2814 debug("channel request %d: %s", id, service) ;
2815}
2816void
2817channel_request_start(int id, char *service, int wantconfirm)
2818{
2819 Channel *c = channel_lookup(id);
2820 if (c == NULL) {
2821 log("channel_request: %d: bad id", id);
2822 return;
2823 }
2824 packet_start(SSH2_MSG_CHANNEL_REQUEST);
2825 packet_put_int(c->remote_id);
2826 packet_put_cstring(service);
2827 packet_put_char(wantconfirm);
2828}
2829void
2830channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
2831{
2832 Channel *c = channel_lookup(id);
2833 if (c == NULL) {
2834 log("channel_register_callback: %d: bad id", id);
2835 return;
2836 }
2837 c->cb_event = mtype;
2838 c->cb_fn = fn;
2839 c->cb_arg = arg;
2840}
2841void
2842channel_register_cleanup(int id, channel_callback_fn *fn)
2843{
2844 Channel *c = channel_lookup(id);
2845 if (c == NULL) {
2846 log("channel_register_cleanup: %d: bad id", id);
2847 return;
2848 }
2849 c->dettach_user = fn;
2850}
2851void
2852channel_cancel_cleanup(int id)
2853{
2854 Channel *c = channel_lookup(id);
2855 if (c == NULL) {
2856 log("channel_cancel_cleanup: %d: bad id", id);
2857 return;
2858 }
2859 c->dettach_user = NULL;
2860}
Kevin Stevesef4eea92001-02-05 12:42:17 +00002861void
Damien Millerad833b32000-08-23 10:46:23 +10002862channel_register_filter(int id, channel_filter_fn *fn)
2863{
2864 Channel *c = channel_lookup(id);
2865 if (c == NULL) {
2866 log("channel_register_filter: %d: bad id", id);
2867 return;
2868 }
2869 c->input_filter = fn;
2870}
Damien Miller33b13562000-04-04 14:38:59 +10002871
2872void
Damien Miller69b69aa2000-10-28 14:19:58 +11002873channel_set_fds(int id, int rfd, int wfd, int efd,
2874 int extusage, int nonblock)
Damien Miller33b13562000-04-04 14:38:59 +10002875{
2876 Channel *c = channel_lookup(id);
2877 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
2878 fatal("channel_activate for non-larval channel %d.", id);
Damien Miller69b69aa2000-10-28 14:19:58 +11002879 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller33b13562000-04-04 14:38:59 +10002880 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002881 /* XXX window size? */
Damien Millere4340be2000-09-16 13:29:08 +11002882 c->local_window = c->local_window_max = c->local_maxpacket * 2;
Damien Miller33b13562000-04-04 14:38:59 +10002883 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2884 packet_put_int(c->remote_id);
2885 packet_put_int(c->local_window);
2886 packet_send();
2887}