blob: 1ec6074b51a689eaec06dd07ec4c9ee75587ecfb [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 *
Damien Miller33b13562000-04-04 14:38:59 +100015 * SSH2 support added by Markus Friedl.
Ben Lindstrom44697232001-07-04 03:32:30 +000016 * Copyright (c) 1999, 2000, 2001 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110017 * Copyright (c) 1999 Dug Song. All rights reserved.
18 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
19 *
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
31 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
34 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
38 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110039 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040
41#include "includes.h"
Damien Miller4623a752001-10-10 15:03:58 +100042RCSID("$OpenBSD: channels.c,v 1.137 2001/10/07 17:49:40 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
44#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "ssh1.h"
46#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047#include "packet.h"
48#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000050#include "log.h"
51#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000054#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100055#include "key.h"
56#include "authfd.h"
57
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
Ben Lindstrome9c99912001-06-09 00:41:05 +000059/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
Damien Miller5428f641999-11-25 11:54:57 +110061/*
62 * Pointer to an array containing all allocated channels. The array is
63 * dynamically extended as needed.
64 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000065static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
Damien Miller5428f641999-11-25 11:54:57 +110067/*
68 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000069 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110070 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071static int channels_alloc = 0;
72
Damien Miller5428f641999-11-25 11:54:57 +110073/*
74 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +000075 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +110076 */
Damien Miller5e953212001-01-30 09:14:00 +110077static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Ben Lindstrome9c99912001-06-09 00:41:05 +000080/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
Damien Miller5428f641999-11-25 11:54:57 +110082/*
83 * Data structure for storing which hosts are permitted for forward requests.
84 * The local sides of any remote forwards are stored in this array to prevent
85 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
86 * network (which might be behind a firewall).
87 */
Damien Miller95def091999-11-25 00:26:21 +110088typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +100089 char *host_to_connect; /* Connect to 'host'. */
90 u_short port_to_connect; /* Connect to 'port'. */
91 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092} ForwardPermission;
93
94/* List of all permitted host/port pairs to connect. */
95static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
Ben Lindstrome9c99912001-06-09 00:41:05 +000096
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097/* Number of permitted host/port pairs in the array. */
98static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +110099/*
100 * If this is true, all opens are permitted. This is the case on the server
101 * on which we have to trust the client anyway, and the user could do
102 * anything after logging in anyway.
103 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104static int all_opens_permitted = 0;
105
Ben Lindstrome9c99912001-06-09 00:41:05 +0000106
107/* -- X11 forwarding */
108
109/* Maximum number of fake X11 displays to try. */
110#define MAX_DISPLAYS 1000
111
112/* Saved X11 authentication protocol name. */
113static char *x11_saved_proto = NULL;
114
115/* Saved X11 authentication data. This is the real data. */
116static char *x11_saved_data = NULL;
117static u_int x11_saved_data_len = 0;
118
119/*
120 * Fake X11 authentication data. This is what the server will be sending us;
121 * we should replace any occurrences of this by the real data.
122 */
123static char *x11_fake_data = NULL;
124static u_int x11_fake_data_len;
125
126
127/* -- agent forwarding */
128
129#define NUM_SOCKS 10
130
131/* Name and directory of socket for authentication agent forwarding. */
132static char *auth_sock_name = NULL;
133static char *auth_sock_dir = NULL;
134
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000135/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000136static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000137
Ben Lindstrome9c99912001-06-09 00:41:05 +0000138/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000139static void port_open_helper(Channel *c, char *rtype);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000140
Ben Lindstrome9c99912001-06-09 00:41:05 +0000141/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000142
143Channel *
144channel_lookup(int id)
145{
146 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000147
Damien Millerc0fd17f2000-06-26 10:22:53 +1000148 if (id < 0 || id > channels_alloc) {
Damien Millerb38eff82000-04-01 11:09:21 +1000149 log("channel_lookup: %d: bad id", id);
150 return NULL;
151 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000152 c = channels[id];
153 if (c == NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +1000154 log("channel_lookup: %d: bad id: channel free", id);
155 return NULL;
156 }
157 return c;
158}
159
Damien Miller5428f641999-11-25 11:54:57 +1100160/*
Damien Millere247cc42000-05-07 12:03:14 +1000161 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000162 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100163 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164
Ben Lindstrombba81212001-06-25 05:01:22 +0000165static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100166channel_register_fds(Channel *c, int rfd, int wfd, int efd,
167 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168{
Damien Miller95def091999-11-25 00:26:21 +1100169 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100170 channel_max_fd = MAX(channel_max_fd, rfd);
171 channel_max_fd = MAX(channel_max_fd, wfd);
172 channel_max_fd = MAX(channel_max_fd, efd);
173
Damien Miller5428f641999-11-25 11:54:57 +1100174 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000175
Damien Miller6f83b8e2000-05-02 09:23:45 +1000176 c->rfd = rfd;
177 c->wfd = wfd;
178 c->sock = (rfd == wfd) ? rfd : -1;
179 c->efd = efd;
180 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100181
Damien Miller79438cc2001-02-16 12:34:57 +1100182 /* XXX ugly hack: nonblock is only set by the server */
183 if (nonblock && isatty(c->rfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000184 debug("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100185 c->isatty = 1;
186 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000187 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100188 c->self, c->wfd);
189 }
190 } else {
191 c->isatty = 0;
192 }
193
Damien Miller69b69aa2000-10-28 14:19:58 +1100194 /* enable nonblocking mode */
195 if (nonblock) {
196 if (rfd != -1)
197 set_nonblock(rfd);
198 if (wfd != -1)
199 set_nonblock(wfd);
200 if (efd != -1)
201 set_nonblock(efd);
202 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000203}
204
205/*
206 * Allocate a new channel object and set its type and socket. This will cause
207 * remote_name to be freed.
208 */
209
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000210Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000211channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Damien Miller69b69aa2000-10-28 14:19:58 +1100212 int window, int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000213{
214 int i, found;
215 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216
Damien Miller95def091999-11-25 00:26:21 +1100217 /* Do initial allocation if this is the first call. */
218 if (channels_alloc == 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000219 chan_init();
Damien Miller95def091999-11-25 00:26:21 +1100220 channels_alloc = 10;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000221 channels = xmalloc(channels_alloc * sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100222 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000223 channels[i] = NULL;
Ben Lindstrom601e4362001-06-21 03:19:23 +0000224 fatal_add_cleanup((void (*) (void *)) channel_free_all, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100225 }
226 /* Try to find a free slot where to put the new channel. */
227 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000228 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100229 /* Found a free slot. */
230 found = i;
231 break;
232 }
233 if (found == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100234 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100235 found = channels_alloc;
236 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100237 debug2("channel: expanding %d", channels_alloc);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000238 channels = xrealloc(channels, channels_alloc * sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100239 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000240 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100241 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000242 /* Initialize and return new channel. */
243 c = channels[found] = xmalloc(sizeof(Channel));
Damien Miller4623a752001-10-10 15:03:58 +1000244 memset(c, 0, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100245 buffer_init(&c->input);
246 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000247 buffer_init(&c->extended);
Damien Miller95def091999-11-25 00:26:21 +1100248 chan_init_iostates(c);
Damien Miller69b69aa2000-10-28 14:19:58 +1100249 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100250 c->self = found;
251 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000252 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000253 c->local_window = window;
254 c->local_window_max = window;
255 c->local_consumed = 0;
256 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100257 c->remote_id = -1;
258 c->remote_name = remote_name;
Damien Millerb38eff82000-04-01 11:09:21 +1000259 c->remote_window = 0;
260 c->remote_maxpacket = 0;
261 c->cb_fn = NULL;
262 c->cb_arg = NULL;
263 c->cb_event = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000264 c->force_drain = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000265 c->detach_user = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000266 c->input_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100267 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000268 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000270
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000271static int
272channel_find_maxfd(void)
273{
274 int i, max = 0;
275 Channel *c;
276
277 for (i = 0; i < channels_alloc; i++) {
278 c = channels[i];
279 if (c != NULL) {
280 max = MAX(max, c->rfd);
281 max = MAX(max, c->wfd);
282 max = MAX(max, c->efd);
283 }
284 }
285 return max;
286}
287
288int
289channel_close_fd(int *fdp)
290{
291 int ret = 0, fd = *fdp;
292
293 if (fd != -1) {
294 ret = close(fd);
295 *fdp = -1;
296 if (fd == channel_max_fd)
297 channel_max_fd = channel_find_maxfd();
298 }
299 return ret;
300}
301
Damien Miller6f83b8e2000-05-02 09:23:45 +1000302/* Close all channel fd/socket. */
303
Ben Lindstrombba81212001-06-25 05:01:22 +0000304static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000305channel_close_fds(Channel *c)
306{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000307 debug3("channel_close_fds: channel %d: r %d w %d e %d",
308 c->self, c->rfd, c->wfd, c->efd);
309
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000310 channel_close_fd(&c->sock);
311 channel_close_fd(&c->rfd);
312 channel_close_fd(&c->wfd);
313 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000314}
315
316/* Free the channel and close its fd/socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317
Damien Miller4af51302000-04-16 11:18:38 +1000318void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000319channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000320{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000321 char *s;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000322 int i, n;
323
324 for (n = 0, i = 0; i < channels_alloc; i++)
325 if (channels[i])
326 n++;
Ben Lindstrom4c247552001-06-05 20:56:47 +0000327 debug("channel_free: channel %d: %s, nchannels %d", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000328 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000329
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000330 s = channel_open_message();
Ben Lindstrom4c247552001-06-05 20:56:47 +0000331 debug3("channel_free: status: %s", s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000332 xfree(s);
333
Ben Lindstrom809744e2001-07-04 05:26:06 +0000334 if (c->detach_user != NULL) {
335 debug("channel_free: channel %d: detaching channel user", c->self);
336 c->detach_user(c->self, NULL);
Damien Miller33b13562000-04-04 14:38:59 +1000337 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000338 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000339 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000340 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000341 buffer_free(&c->input);
342 buffer_free(&c->output);
343 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000344 if (c->remote_name) {
345 xfree(c->remote_name);
346 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100347 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000348 channels[c->self] = NULL;
349 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000350}
351
Ben Lindstrome9c99912001-06-09 00:41:05 +0000352void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000353channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000354{
355 int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000356
Ben Lindstrom601e4362001-06-21 03:19:23 +0000357 for (i = 0; i < channels_alloc; i++)
358 if (channels[i] != NULL)
359 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000360}
361
Ben Lindstrom809744e2001-07-04 05:26:06 +0000362void
363channel_detach_all(void)
364{
365 int i;
366 Channel *c;
367
368 for (i = 0; i < channels_alloc; i++) {
369 c = channels[i];
370 if (c != NULL && c->detach_user != NULL) {
371 debug("channel_detach_all: channel %d", c->self);
372 c->detach_user(c->self, NULL);
373 c->detach_user = NULL;
374 }
375 }
376}
377
Ben Lindstrome9c99912001-06-09 00:41:05 +0000378/*
379 * Closes the sockets/fds of all channels. This is used to close extra file
380 * descriptors after a fork.
381 */
382
383void
384channel_close_all()
385{
386 int i;
387
388 for (i = 0; i < channels_alloc; i++)
389 if (channels[i] != NULL)
390 channel_close_fds(channels[i]);
391}
392
393/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000394 * Stop listening to channels.
395 */
396
397void
398channel_stop_listening(void)
399{
400 int i;
401 Channel *c;
402
403 for (i = 0; i < channels_alloc; i++) {
404 c = channels[i];
405 if (c != NULL) {
406 switch (c->type) {
407 case SSH_CHANNEL_AUTH_SOCKET:
408 case SSH_CHANNEL_PORT_LISTENER:
409 case SSH_CHANNEL_RPORT_LISTENER:
410 case SSH_CHANNEL_X11_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000411 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000412 channel_free(c);
413 break;
414 }
415 }
416 }
417}
418
419/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000420 * Returns true if no channel has too much buffered data, and false if one or
421 * more channel is overfull.
422 */
423
424int
425channel_not_very_much_buffered_data()
426{
427 u_int i;
428 Channel *c;
429
430 for (i = 0; i < channels_alloc; i++) {
431 c = channels[i];
432 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000433#if 0
434 if (!compat20 &&
435 buffer_len(&c->input) > packet_get_maxsize()) {
Ben Lindstrome9c99912001-06-09 00:41:05 +0000436 debug("channel %d: big input buffer %d",
437 c->self, buffer_len(&c->input));
438 return 0;
439 }
Damien Milleraf5f2e62001-10-10 15:01:16 +1000440#endif
Ben Lindstrome9c99912001-06-09 00:41:05 +0000441 if (buffer_len(&c->output) > packet_get_maxsize()) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000442 debug("channel %d: big output buffer %d > %d",
443 c->self, buffer_len(&c->output),
444 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000445 return 0;
446 }
447 }
448 }
449 return 1;
450}
451
452/* Returns true if any channel is still open. */
453
454int
455channel_still_open()
456{
457 int i;
458 Channel *c;
459
460 for (i = 0; i < channels_alloc; i++) {
461 c = channels[i];
462 if (c == NULL)
463 continue;
464 switch (c->type) {
465 case SSH_CHANNEL_X11_LISTENER:
466 case SSH_CHANNEL_PORT_LISTENER:
467 case SSH_CHANNEL_RPORT_LISTENER:
468 case SSH_CHANNEL_CLOSED:
469 case SSH_CHANNEL_AUTH_SOCKET:
470 case SSH_CHANNEL_DYNAMIC:
471 case SSH_CHANNEL_CONNECTING:
472 case SSH_CHANNEL_ZOMBIE:
473 continue;
474 case SSH_CHANNEL_LARVAL:
475 if (!compat20)
476 fatal("cannot happen: SSH_CHANNEL_LARVAL");
477 continue;
478 case SSH_CHANNEL_OPENING:
479 case SSH_CHANNEL_OPEN:
480 case SSH_CHANNEL_X11_OPEN:
481 return 1;
482 case SSH_CHANNEL_INPUT_DRAINING:
483 case SSH_CHANNEL_OUTPUT_DRAINING:
484 if (!compat13)
485 fatal("cannot happen: OUT_DRAIN");
486 return 1;
487 default:
488 fatal("channel_still_open: bad channel type %d", c->type);
489 /* NOTREACHED */
490 }
491 }
492 return 0;
493}
494
495/* Returns the id of an open channel suitable for keepaliving */
496
497int
498channel_find_open()
499{
500 int i;
501 Channel *c;
502
503 for (i = 0; i < channels_alloc; i++) {
504 c = channels[i];
505 if (c == NULL)
506 continue;
507 switch (c->type) {
508 case SSH_CHANNEL_CLOSED:
509 case SSH_CHANNEL_DYNAMIC:
510 case SSH_CHANNEL_X11_LISTENER:
511 case SSH_CHANNEL_PORT_LISTENER:
512 case SSH_CHANNEL_RPORT_LISTENER:
513 case SSH_CHANNEL_OPENING:
514 case SSH_CHANNEL_CONNECTING:
515 case SSH_CHANNEL_ZOMBIE:
516 continue;
517 case SSH_CHANNEL_LARVAL:
518 case SSH_CHANNEL_AUTH_SOCKET:
519 case SSH_CHANNEL_OPEN:
520 case SSH_CHANNEL_X11_OPEN:
521 return i;
522 case SSH_CHANNEL_INPUT_DRAINING:
523 case SSH_CHANNEL_OUTPUT_DRAINING:
524 if (!compat13)
525 fatal("cannot happen: OUT_DRAIN");
526 return i;
527 default:
528 fatal("channel_find_open: bad channel type %d", c->type);
529 /* NOTREACHED */
530 }
531 }
532 return -1;
533}
534
535
536/*
537 * Returns a message describing the currently open forwarded connections,
538 * suitable for sending to the client. The message contains crlf pairs for
539 * newlines.
540 */
541
542char *
543channel_open_message()
544{
545 Buffer buffer;
546 Channel *c;
547 char buf[1024], *cp;
548 int i;
549
550 buffer_init(&buffer);
551 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
552 buffer_append(&buffer, buf, strlen(buf));
553 for (i = 0; i < channels_alloc; i++) {
554 c = channels[i];
555 if (c == NULL)
556 continue;
557 switch (c->type) {
558 case SSH_CHANNEL_X11_LISTENER:
559 case SSH_CHANNEL_PORT_LISTENER:
560 case SSH_CHANNEL_RPORT_LISTENER:
561 case SSH_CHANNEL_CLOSED:
562 case SSH_CHANNEL_AUTH_SOCKET:
563 case SSH_CHANNEL_ZOMBIE:
564 continue;
565 case SSH_CHANNEL_LARVAL:
566 case SSH_CHANNEL_OPENING:
567 case SSH_CHANNEL_CONNECTING:
568 case SSH_CHANNEL_DYNAMIC:
569 case SSH_CHANNEL_OPEN:
570 case SSH_CHANNEL_X11_OPEN:
571 case SSH_CHANNEL_INPUT_DRAINING:
572 case SSH_CHANNEL_OUTPUT_DRAINING:
573 snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
574 c->self, c->remote_name,
575 c->type, c->remote_id,
576 c->istate, buffer_len(&c->input),
577 c->ostate, buffer_len(&c->output),
578 c->rfd, c->wfd);
579 buffer_append(&buffer, buf, strlen(buf));
580 continue;
581 default:
582 fatal("channel_open_message: bad channel type %d", c->type);
583 /* NOTREACHED */
584 }
585 }
586 buffer_append(&buffer, "\0", 1);
587 cp = xstrdup(buffer_ptr(&buffer));
588 buffer_free(&buffer);
589 return cp;
590}
591
592void
593channel_send_open(int id)
594{
595 Channel *c = channel_lookup(id);
596 if (c == NULL) {
597 log("channel_send_open: %d: bad id", id);
598 return;
599 }
600 debug("send channel open %d", id);
601 packet_start(SSH2_MSG_CHANNEL_OPEN);
602 packet_put_cstring(c->ctype);
603 packet_put_int(c->self);
604 packet_put_int(c->local_window);
605 packet_put_int(c->local_maxpacket);
606 packet_send();
607}
608
609void
610channel_request(int id, char *service, int wantconfirm)
611{
612 channel_request_start(id, service, wantconfirm);
613 packet_send();
614 debug("channel request %d: %s", id, service) ;
615}
616void
617channel_request_start(int id, char *service, int wantconfirm)
618{
619 Channel *c = channel_lookup(id);
620 if (c == NULL) {
621 log("channel_request: %d: bad id", id);
622 return;
623 }
624 packet_start(SSH2_MSG_CHANNEL_REQUEST);
625 packet_put_int(c->remote_id);
626 packet_put_cstring(service);
627 packet_put_char(wantconfirm);
628}
629void
630channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
631{
632 Channel *c = channel_lookup(id);
633 if (c == NULL) {
634 log("channel_register_callback: %d: bad id", id);
635 return;
636 }
637 c->cb_event = mtype;
638 c->cb_fn = fn;
639 c->cb_arg = arg;
640}
641void
642channel_register_cleanup(int id, channel_callback_fn *fn)
643{
644 Channel *c = channel_lookup(id);
645 if (c == NULL) {
646 log("channel_register_cleanup: %d: bad id", id);
647 return;
648 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000649 c->detach_user = fn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000650}
651void
652channel_cancel_cleanup(int id)
653{
654 Channel *c = channel_lookup(id);
655 if (c == NULL) {
656 log("channel_cancel_cleanup: %d: bad id", id);
657 return;
658 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000659 c->detach_user = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000660}
661void
662channel_register_filter(int id, channel_filter_fn *fn)
663{
664 Channel *c = channel_lookup(id);
665 if (c == NULL) {
666 log("channel_register_filter: %d: bad id", id);
667 return;
668 }
669 c->input_filter = fn;
670}
671
672void
673channel_set_fds(int id, int rfd, int wfd, int efd,
674 int extusage, int nonblock)
675{
676 Channel *c = channel_lookup(id);
677 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
678 fatal("channel_activate for non-larval channel %d.", id);
679 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
680 c->type = SSH_CHANNEL_OPEN;
681 /* XXX window size? */
682 c->local_window = c->local_window_max = c->local_maxpacket * 2;
683 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
684 packet_put_int(c->remote_id);
685 packet_put_int(c->local_window);
686 packet_send();
687}
688
Damien Miller5428f641999-11-25 11:54:57 +1100689/*
Damien Millerb38eff82000-04-01 11:09:21 +1000690 * 'channel_pre*' are called just before select() to add any bits relevant to
691 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100692 */
Damien Millerb38eff82000-04-01 11:09:21 +1000693/*
694 * 'channel_post*': perform any appropriate operations for channels which
695 * have events pending.
696 */
697typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
698chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
699chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
700
Ben Lindstrombba81212001-06-25 05:01:22 +0000701static void
Damien Millerb38eff82000-04-01 11:09:21 +1000702channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
703{
704 FD_SET(c->sock, readset);
705}
706
Ben Lindstrombba81212001-06-25 05:01:22 +0000707static void
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000708channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
709{
710 debug3("channel %d: waiting for connection", c->self);
711 FD_SET(c->sock, writeset);
712}
713
Ben Lindstrombba81212001-06-25 05:01:22 +0000714static void
Damien Millerb38eff82000-04-01 11:09:21 +1000715channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
716{
717 if (buffer_len(&c->input) < packet_get_maxsize())
718 FD_SET(c->sock, readset);
719 if (buffer_len(&c->output) > 0)
720 FD_SET(c->sock, writeset);
721}
722
Ben Lindstrombba81212001-06-25 05:01:22 +0000723static void
Damien Millerb38eff82000-04-01 11:09:21 +1000724channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
725{
726 /* test whether sockets are 'alive' for read/write */
727 if (c->istate == CHAN_INPUT_OPEN)
728 if (buffer_len(&c->input) < packet_get_maxsize())
729 FD_SET(c->sock, readset);
730 if (c->ostate == CHAN_OUTPUT_OPEN ||
731 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
732 if (buffer_len(&c->output) > 0) {
733 FD_SET(c->sock, writeset);
734 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
735 chan_obuf_empty(c);
736 }
737 }
738}
739
Ben Lindstrombba81212001-06-25 05:01:22 +0000740static void
Damien Miller33b13562000-04-04 14:38:59 +1000741channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
742{
743 if (c->istate == CHAN_INPUT_OPEN &&
744 c->remote_window > 0 &&
745 buffer_len(&c->input) < c->remote_window)
746 FD_SET(c->rfd, readset);
747 if (c->ostate == CHAN_OUTPUT_OPEN ||
748 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
749 if (buffer_len(&c->output) > 0) {
750 FD_SET(c->wfd, writeset);
751 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
752 chan_obuf_empty(c);
753 }
754 }
755 /** XXX check close conditions, too */
756 if (c->efd != -1) {
757 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
758 buffer_len(&c->extended) > 0)
759 FD_SET(c->efd, writeset);
760 else if (c->extended_usage == CHAN_EXTENDED_READ &&
761 buffer_len(&c->extended) < c->remote_window)
762 FD_SET(c->efd, readset);
763 }
764}
765
Ben Lindstrombba81212001-06-25 05:01:22 +0000766static void
Damien Millerb38eff82000-04-01 11:09:21 +1000767channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
768{
769 if (buffer_len(&c->input) == 0) {
770 packet_start(SSH_MSG_CHANNEL_CLOSE);
771 packet_put_int(c->remote_id);
772 packet_send();
773 c->type = SSH_CHANNEL_CLOSED;
Ben Lindstromc486d882001-04-11 16:08:34 +0000774 debug("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000775 }
776}
777
Ben Lindstrombba81212001-06-25 05:01:22 +0000778static void
Damien Millerb38eff82000-04-01 11:09:21 +1000779channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
780{
781 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000782 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000783 else
Damien Millerb38eff82000-04-01 11:09:21 +1000784 FD_SET(c->sock, writeset);
785}
786
787/*
788 * This is a special state for X11 authentication spoofing. An opened X11
789 * connection (when authentication spoofing is being done) remains in this
790 * state until the first packet has been completely read. The authentication
791 * data in that packet is then substituted by the real data if it matches the
792 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000793 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000794 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000795 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000796static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000797x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000798{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000799 u_char *ucp;
800 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000801
802 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000803 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000804 return 0;
805
806 /* Parse the lengths of variable-length fields. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000807 ucp = (u_char *) buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000808 if (ucp[0] == 0x42) { /* Byte order MSB first. */
809 proto_len = 256 * ucp[6] + ucp[7];
810 data_len = 256 * ucp[8] + ucp[9];
811 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
812 proto_len = ucp[6] + 256 * ucp[7];
813 data_len = ucp[8] + 256 * ucp[9];
814 } else {
815 debug("Initial X11 packet contains bad byte order byte: 0x%x",
816 ucp[0]);
817 return -1;
818 }
819
820 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000821 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000822 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
823 return 0;
824
825 /* Check if authentication protocol matches. */
826 if (proto_len != strlen(x11_saved_proto) ||
827 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
828 debug("X11 connection uses different authentication protocol.");
829 return -1;
830 }
831 /* Check if authentication data matches our fake data. */
832 if (data_len != x11_fake_data_len ||
833 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
834 x11_fake_data, x11_fake_data_len) != 0) {
835 debug("X11 auth data does not match fake data.");
836 return -1;
837 }
838 /* Check fake data length */
839 if (x11_fake_data_len != x11_saved_data_len) {
840 error("X11 fake_data_len %d != saved_data_len %d",
841 x11_fake_data_len, x11_saved_data_len);
842 return -1;
843 }
844 /*
845 * Received authentication protocol and data match
846 * our fake data. Substitute the fake data with real
847 * data.
848 */
849 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
850 x11_saved_data, x11_saved_data_len);
851 return 1;
852}
853
Ben Lindstrombba81212001-06-25 05:01:22 +0000854static void
Damien Millerb38eff82000-04-01 11:09:21 +1000855channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
856{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000857 int ret = x11_open_helper(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000858 if (ret == 1) {
859 /* Start normal processing for the channel. */
860 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000861 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000862 } else if (ret == -1) {
863 /*
864 * We have received an X11 connection that has bad
865 * authentication information.
866 */
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000867 log("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000868 buffer_clear(&c->input);
869 buffer_clear(&c->output);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000870 channel_close_fd(&c->sock);
Damien Millerb38eff82000-04-01 11:09:21 +1000871 c->sock = -1;
872 c->type = SSH_CHANNEL_CLOSED;
873 packet_start(SSH_MSG_CHANNEL_CLOSE);
874 packet_put_int(c->remote_id);
875 packet_send();
876 }
877}
878
Ben Lindstrombba81212001-06-25 05:01:22 +0000879static void
Damien Millerbd483e72000-04-30 10:00:53 +1000880channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000881{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000882 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000883
884 /* c->force_drain = 1; */
885
Damien Millerb38eff82000-04-01 11:09:21 +1000886 if (ret == 1) {
887 c->type = SSH_CHANNEL_OPEN;
Damien Miller30c3d422000-05-09 11:02:59 +1000888 if (compat20)
889 channel_pre_open_20(c, readset, writeset);
890 else
891 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000892 } else if (ret == -1) {
893 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerbd483e72000-04-30 10:00:53 +1000894 chan_read_failed(c); /** force close? */
Damien Millerb38eff82000-04-01 11:09:21 +1000895 chan_write_failed(c);
896 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
897 }
898}
899
Ben Lindstromb3921512001-04-11 15:57:50 +0000900/* try to decode a socks4 header */
Ben Lindstrombba81212001-06-25 05:01:22 +0000901static int
Ben Lindstromb3921512001-04-11 15:57:50 +0000902channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000903{
904 u_char *p, *host;
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000905 int len, have, i, found;
Ben Lindstromb3921512001-04-11 15:57:50 +0000906 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000907 struct {
908 u_int8_t version;
909 u_int8_t command;
910 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000911 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000912 } s4_req, s4_rsp;
913
Ben Lindstromb3921512001-04-11 15:57:50 +0000914 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000915
916 have = buffer_len(&c->input);
917 len = sizeof(s4_req);
918 if (have < len)
919 return 0;
920 p = buffer_ptr(&c->input);
921 for (found = 0, i = len; i < have; i++) {
922 if (p[i] == '\0') {
923 found = 1;
924 break;
925 }
926 if (i > 1024) {
927 /* the peer is probably sending garbage */
928 debug("channel %d: decode socks4: too long",
929 c->self);
930 return -1;
931 }
932 }
933 if (!found)
934 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000935 buffer_get(&c->input, (char *)&s4_req.version, 1);
936 buffer_get(&c->input, (char *)&s4_req.command, 1);
937 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000938 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000939 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000940 p = buffer_ptr(&c->input);
941 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000942 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000943 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000944 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000945 c->self, len, have);
946 strlcpy(username, p, sizeof(username));
947 buffer_consume(&c->input, len);
948 buffer_consume(&c->input, 1); /* trailing '\0' */
949
Ben Lindstromb3921512001-04-11 15:57:50 +0000950 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000951 strlcpy(c->path, host, sizeof(c->path));
952 c->host_port = ntohs(s4_req.dest_port);
953
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000954 debug("channel %d: dynamic request: socks4 host %s port %u command %u",
955 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000956
Ben Lindstromb3921512001-04-11 15:57:50 +0000957 if (s4_req.command != 1) {
958 debug("channel %d: cannot handle: socks4 cn %d",
959 c->self, s4_req.command);
960 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000961 }
Ben Lindstromb3921512001-04-11 15:57:50 +0000962 s4_rsp.version = 0; /* vn: 0 for reply */
963 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000964 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +0000965 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000966 buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +0000967 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000968}
969
Ben Lindstromb3921512001-04-11 15:57:50 +0000970/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +0000971static void
Ben Lindstromb3921512001-04-11 15:57:50 +0000972channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
973{
974 u_char *p;
975 int have, ret;
976
977 have = buffer_len(&c->input);
Damien Miller4623a752001-10-10 15:03:58 +1000978 c->delayed = 0;
Ben Lindstromb3921512001-04-11 15:57:50 +0000979 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +0000980 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +0000981 /* check if the fixed size part of the packet is in buffer. */
982 if (have < 4) {
983 /* need more */
984 FD_SET(c->sock, readset);
985 return;
986 }
987 /* try to guess the protocol */
988 p = buffer_ptr(&c->input);
989 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000990 case 0x04:
991 ret = channel_decode_socks4(c, readset, writeset);
992 break;
Ben Lindstromb3921512001-04-11 15:57:50 +0000993 default:
994 ret = -1;
995 break;
996 }
997 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000998 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +0000999 } else if (ret == 0) {
1000 debug2("channel %d: pre_dynamic: need more", c->self);
1001 /* need more */
1002 FD_SET(c->sock, readset);
1003 } else {
1004 /* switch to the next state */
1005 c->type = SSH_CHANNEL_OPENING;
1006 port_open_helper(c, "direct-tcpip");
1007 }
1008}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001009
Damien Millerb38eff82000-04-01 11:09:21 +10001010/* This is our fake X11 server socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +00001011static void
Damien Millerb38eff82000-04-01 11:09:21 +10001012channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
1013{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001014 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001015 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001016 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001017 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001018 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001019 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001020
1021 if (FD_ISSET(c->sock, readset)) {
1022 debug("X11 connection requested.");
1023 addrlen = sizeof(addr);
1024 newsock = accept(c->sock, &addr, &addrlen);
1025 if (newsock < 0) {
1026 error("accept: %.100s", strerror(errno));
1027 return;
1028 }
Damien Millerd83ff352001-01-30 09:19:34 +11001029 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001030 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001031 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001032 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001033
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001034 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001035 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1036 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +11001037 0, xstrdup(buf), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001038 if (nc == NULL) {
1039 close(newsock);
1040 xfree(remote_ipaddr);
1041 return;
1042 }
Damien Millerbd483e72000-04-30 10:00:53 +10001043 if (compat20) {
1044 packet_start(SSH2_MSG_CHANNEL_OPEN);
1045 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001046 packet_put_int(nc->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001047 packet_put_int(c->local_window_max);
1048 packet_put_int(c->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +11001049 /* originator ipaddr and port */
1050 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +10001051 if (datafellows & SSH_BUG_X11FWD) {
1052 debug("ssh2 x11 bug compat mode");
1053 } else {
1054 packet_put_int(remote_port);
1055 }
Damien Millerbd483e72000-04-30 10:00:53 +10001056 packet_send();
1057 } else {
1058 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001059 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001060 if (packet_get_protocol_flags() &
1061 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +00001062 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +10001063 packet_send();
1064 }
Damien Millerd83ff352001-01-30 09:19:34 +11001065 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001066 }
1067}
1068
Ben Lindstrombba81212001-06-25 05:01:22 +00001069static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001070port_open_helper(Channel *c, char *rtype)
1071{
1072 int direct;
1073 char buf[1024];
1074 char *remote_ipaddr = get_peer_ipaddr(c->sock);
1075 u_short remote_port = get_peer_port(c->sock);
1076
1077 direct = (strcmp(rtype, "direct-tcpip") == 0);
1078
1079 snprintf(buf, sizeof buf,
1080 "%s: listening port %d for %.100s port %d, "
1081 "connect from %.200s port %d",
1082 rtype, c->listening_port, c->path, c->host_port,
1083 remote_ipaddr, remote_port);
1084
1085 xfree(c->remote_name);
1086 c->remote_name = xstrdup(buf);
1087
1088 if (compat20) {
1089 packet_start(SSH2_MSG_CHANNEL_OPEN);
1090 packet_put_cstring(rtype);
1091 packet_put_int(c->self);
1092 packet_put_int(c->local_window_max);
1093 packet_put_int(c->local_maxpacket);
1094 if (direct) {
1095 /* target host, port */
1096 packet_put_cstring(c->path);
1097 packet_put_int(c->host_port);
1098 } else {
1099 /* listen address, port */
1100 packet_put_cstring(c->path);
1101 packet_put_int(c->listening_port);
1102 }
1103 /* originator host and port */
1104 packet_put_cstring(remote_ipaddr);
1105 packet_put_int(remote_port);
1106 packet_send();
1107 } else {
1108 packet_start(SSH_MSG_PORT_OPEN);
1109 packet_put_int(c->self);
1110 packet_put_cstring(c->path);
1111 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001112 if (packet_get_protocol_flags() &
1113 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001114 packet_put_cstring(c->remote_name);
1115 packet_send();
1116 }
1117 xfree(remote_ipaddr);
1118}
1119
Damien Millerb38eff82000-04-01 11:09:21 +10001120/*
1121 * This socket is listening for connections to a forwarded TCP/IP port.
1122 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001123static void
Damien Millerb38eff82000-04-01 11:09:21 +10001124channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
1125{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001126 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001127 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001128 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001129 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001130 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001131
Damien Millerb38eff82000-04-01 11:09:21 +10001132 if (FD_ISSET(c->sock, readset)) {
1133 debug("Connection to port %d forwarding "
1134 "to %.100s port %d requested.",
1135 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001136
Damien Miller4623a752001-10-10 15:03:58 +10001137 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1138 nextstate = SSH_CHANNEL_OPENING;
1139 rtype = "forwarded-tcpip";
1140 } else {
1141 if (c->host_port == 0) {
1142 nextstate = SSH_CHANNEL_DYNAMIC;
1143 rtype = "direct-tcpip";
1144 } else {
1145 nextstate = SSH_CHANNEL_OPENING;
1146 rtype = "direct-tcpip";
1147 }
1148 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001149
Damien Millerb38eff82000-04-01 11:09:21 +10001150 addrlen = sizeof(addr);
1151 newsock = accept(c->sock, &addr, &addrlen);
1152 if (newsock < 0) {
1153 error("accept: %.100s", strerror(errno));
1154 return;
1155 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001156 nc = channel_new(rtype,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001157 nextstate, newsock, newsock, -1,
Damien Millerb38eff82000-04-01 11:09:21 +10001158 c->local_window_max, c->local_maxpacket,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001159 0, xstrdup(rtype), 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001160 if (nc == NULL) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001161 error("channel_post_port_listener: no new channel:");
1162 close(newsock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001163 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001164 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001165 nc->listening_port = c->listening_port;
1166 nc->host_port = c->host_port;
1167 strlcpy(nc->path, c->path, sizeof(nc->path));
1168
Damien Miller4623a752001-10-10 15:03:58 +10001169 if (nextstate == SSH_CHANNEL_DYNAMIC) {
1170 /*
1171 * do not call the channel_post handler until
1172 * this flag has been reset by a pre-handler.
1173 * otherwise the FD_ISSET calls might overflow
1174 */
1175 nc->delayed = 1;
1176 } else {
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001177 port_open_helper(nc, rtype);
Damien Miller4623a752001-10-10 15:03:58 +10001178 }
Damien Millerb38eff82000-04-01 11:09:21 +10001179 }
1180}
1181
1182/*
1183 * This is the authentication agent socket listening for connections from
1184 * clients.
1185 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001186static void
Damien Millerb38eff82000-04-01 11:09:21 +10001187channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
1188{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001189 Channel *nc;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001190 char *name;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001191 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001192 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001193 socklen_t addrlen;
1194
1195 if (FD_ISSET(c->sock, readset)) {
1196 addrlen = sizeof(addr);
1197 newsock = accept(c->sock, &addr, &addrlen);
1198 if (newsock < 0) {
1199 error("accept from auth socket: %.100s", strerror(errno));
1200 return;
1201 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001202 name = xstrdup("accepted auth socket");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001203 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001204 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1205 c->local_window_max, c->local_maxpacket,
Ben Lindstrome9c99912001-06-09 00:41:05 +00001206 0, name, 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001207 if (nc == NULL) {
1208 error("channel_post_auth_listener: channel_new failed");
Ben Lindstrome9c99912001-06-09 00:41:05 +00001209 xfree(name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001210 close(newsock);
1211 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001212 if (compat20) {
1213 packet_start(SSH2_MSG_CHANNEL_OPEN);
1214 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001215 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001216 packet_put_int(c->local_window_max);
1217 packet_put_int(c->local_maxpacket);
1218 } else {
1219 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001220 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001221 }
Damien Millerb38eff82000-04-01 11:09:21 +10001222 packet_send();
1223 }
1224}
1225
Ben Lindstrombba81212001-06-25 05:01:22 +00001226static void
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001227channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
1228{
Ben Lindstrom69128662001-05-08 20:07:39 +00001229 int err = 0;
Ben Lindstrom11180952001-07-04 05:13:35 +00001230 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001231
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001232 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001233 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err,
1234 &sz) < 0) {
1235 err = errno;
1236 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001237 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001238 if (err == 0) {
1239 debug("channel %d: connected", c->self);
1240 c->type = SSH_CHANNEL_OPEN;
1241 if (compat20) {
1242 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1243 packet_put_int(c->remote_id);
1244 packet_put_int(c->self);
1245 packet_put_int(c->local_window);
1246 packet_put_int(c->local_maxpacket);
1247 } else {
1248 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1249 packet_put_int(c->remote_id);
1250 packet_put_int(c->self);
1251 }
1252 } else {
1253 debug("channel %d: not connected: %s",
1254 c->self, strerror(err));
1255 if (compat20) {
1256 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1257 packet_put_int(c->remote_id);
1258 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1259 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1260 packet_put_cstring(strerror(err));
1261 packet_put_cstring("");
1262 }
1263 } else {
1264 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1265 packet_put_int(c->remote_id);
1266 }
1267 chan_mark_dead(c);
1268 }
1269 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001270 }
1271}
1272
Ben Lindstrombba81212001-06-25 05:01:22 +00001273static int
Damien Millerb38eff82000-04-01 11:09:21 +10001274channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
1275{
1276 char buf[16*1024];
1277 int len;
1278
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001279 if (c->rfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001280 FD_ISSET(c->rfd, readset)) {
1281 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +10001282 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1283 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001284 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +10001285 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001286 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001287 if (c->type != SSH_CHANNEL_OPEN) {
1288 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001289 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001290 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001291 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001292 buffer_consume(&c->output, buffer_len(&c->output));
1293 c->type = SSH_CHANNEL_INPUT_DRAINING;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001294 debug("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001295 } else {
1296 chan_read_failed(c);
1297 }
1298 return -1;
1299 }
Damien Millerad833b32000-08-23 10:46:23 +10001300 if(c->input_filter != NULL) {
1301 if (c->input_filter(c, buf, len) == -1) {
Ben Lindstromc486d882001-04-11 16:08:34 +00001302 debug("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001303 chan_read_failed(c);
1304 }
1305 } else {
1306 buffer_append(&c->input, buf, len);
1307 }
Damien Millerb38eff82000-04-01 11:09:21 +10001308 }
1309 return 1;
1310}
Ben Lindstrombba81212001-06-25 05:01:22 +00001311static int
Damien Millerb38eff82000-04-01 11:09:21 +10001312channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
1313{
Ben Lindstrome229b252001-03-05 06:28:06 +00001314 struct termios tio;
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001315 u_char *data;
1316 u_int dlen;
Damien Millerb38eff82000-04-01 11:09:21 +10001317 int len;
1318
1319 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001320 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001321 FD_ISSET(c->wfd, writeset) &&
1322 buffer_len(&c->output) > 0) {
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001323 data = buffer_ptr(&c->output);
1324 dlen = buffer_len(&c->output);
1325 len = write(c->wfd, data, dlen);
Damien Miller6f83b8e2000-05-02 09:23:45 +10001326 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1327 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001328 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001329 if (c->type != SSH_CHANNEL_OPEN) {
1330 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001331 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001332 return -1;
1333 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001334 buffer_consume(&c->output, buffer_len(&c->output));
Ben Lindstrome9c99912001-06-09 00:41:05 +00001335 debug("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001336 c->type = SSH_CHANNEL_INPUT_DRAINING;
1337 } else {
1338 chan_write_failed(c);
1339 }
1340 return -1;
1341 }
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001342 if (compat20 && c->isatty && dlen >= 1 && data[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001343 if (tcgetattr(c->wfd, &tio) == 0 &&
1344 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1345 /*
1346 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001347 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001348 * size of a SSH2_MSG_CHANNEL_DATA message
1349 * (4 byte channel id + data)
Damien Miller79438cc2001-02-16 12:34:57 +11001350 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001351 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001352 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001353 }
1354 }
Damien Millerb38eff82000-04-01 11:09:21 +10001355 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001356 if (compat20 && len > 0) {
1357 c->local_consumed += len;
1358 }
1359 }
1360 return 1;
1361}
Ben Lindstrombba81212001-06-25 05:01:22 +00001362static int
Damien Miller33b13562000-04-04 14:38:59 +10001363channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
1364{
1365 char buf[16*1024];
1366 int len;
1367
Damien Miller1383bd82000-04-06 12:32:37 +10001368/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001369 if (c->efd != -1) {
1370 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1371 FD_ISSET(c->efd, writeset) &&
1372 buffer_len(&c->extended) > 0) {
1373 len = write(c->efd, buffer_ptr(&c->extended),
1374 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001375 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001376 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001377 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1378 return 1;
1379 if (len <= 0) {
1380 debug2("channel %d: closing write-efd %d",
1381 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001382 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001383 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001384 buffer_consume(&c->extended, len);
1385 c->local_consumed += len;
1386 }
1387 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1388 FD_ISSET(c->efd, readset)) {
1389 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001390 debug2("channel %d: read %d from efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001391 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001392 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1393 return 1;
1394 if (len <= 0) {
1395 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001396 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001397 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001398 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001399 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001400 }
Damien Miller33b13562000-04-04 14:38:59 +10001401 }
1402 }
1403 return 1;
1404}
Ben Lindstrombba81212001-06-25 05:01:22 +00001405static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001406channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001407{
Ben Lindstromb3921512001-04-11 15:57:50 +00001408 if (c->type == SSH_CHANNEL_OPEN &&
1409 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001410 c->local_window < c->local_window_max/2 &&
1411 c->local_consumed > 0) {
1412 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1413 packet_put_int(c->remote_id);
1414 packet_put_int(c->local_consumed);
1415 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001416 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001417 c->self, c->local_window,
1418 c->local_consumed);
1419 c->local_window += c->local_consumed;
1420 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001421 }
1422 return 1;
1423}
1424
Ben Lindstrombba81212001-06-25 05:01:22 +00001425static void
Damien Millerb38eff82000-04-01 11:09:21 +10001426channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
1427{
Damien Miller4623a752001-10-10 15:03:58 +10001428 if (c->delayed)
1429 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001430 channel_handle_rfd(c, readset, writeset);
1431 channel_handle_wfd(c, readset, writeset);
1432}
1433
Ben Lindstrombba81212001-06-25 05:01:22 +00001434static void
Damien Miller33b13562000-04-04 14:38:59 +10001435channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
1436{
Damien Miller4623a752001-10-10 15:03:58 +10001437 if (c->delayed)
1438 return;
Damien Miller33b13562000-04-04 14:38:59 +10001439 channel_handle_rfd(c, readset, writeset);
1440 channel_handle_wfd(c, readset, writeset);
1441 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001442
1443 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001444}
1445
Ben Lindstrombba81212001-06-25 05:01:22 +00001446static void
Damien Millerb38eff82000-04-01 11:09:21 +10001447channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1448{
1449 int len;
1450 /* Send buffered output data to the socket. */
1451 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1452 len = write(c->sock, buffer_ptr(&c->output),
1453 buffer_len(&c->output));
1454 if (len <= 0)
1455 buffer_consume(&c->output, buffer_len(&c->output));
1456 else
1457 buffer_consume(&c->output, len);
1458 }
1459}
1460
Ben Lindstrombba81212001-06-25 05:01:22 +00001461static void
Damien Miller33b13562000-04-04 14:38:59 +10001462channel_handler_init_20(void)
1463{
1464 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +10001465 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001466 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001467 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001468 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001469 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001470 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001471 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001472
1473 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
1474 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001475 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001476 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001477 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001478 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001479 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_2;
Damien Miller33b13562000-04-04 14:38:59 +10001480}
1481
Ben Lindstrombba81212001-06-25 05:01:22 +00001482static void
Damien Millerb38eff82000-04-01 11:09:21 +10001483channel_handler_init_13(void)
1484{
1485 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1486 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1487 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1488 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1489 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1490 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1491 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001492 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001493 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001494
1495 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
1496 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1497 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1498 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1499 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001500 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001501 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001502}
1503
Ben Lindstrombba81212001-06-25 05:01:22 +00001504static void
Damien Millerb38eff82000-04-01 11:09:21 +10001505channel_handler_init_15(void)
1506{
1507 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +10001508 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001509 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1510 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1511 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001512 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001513 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001514
1515 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1516 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1517 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1518 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001519 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001520 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001521}
1522
Ben Lindstrombba81212001-06-25 05:01:22 +00001523static void
Damien Millerb38eff82000-04-01 11:09:21 +10001524channel_handler_init(void)
1525{
1526 int i;
1527 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1528 channel_pre[i] = NULL;
1529 channel_post[i] = NULL;
1530 }
Damien Miller33b13562000-04-04 14:38:59 +10001531 if (compat20)
1532 channel_handler_init_20();
1533 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001534 channel_handler_init_13();
1535 else
1536 channel_handler_init_15();
1537}
1538
Ben Lindstrombba81212001-06-25 05:01:22 +00001539static void
Damien Millerb38eff82000-04-01 11:09:21 +10001540channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1541{
1542 static int did_init = 0;
1543 int i;
1544 Channel *c;
1545
1546 if (!did_init) {
1547 channel_handler_init();
1548 did_init = 1;
1549 }
1550 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001551 c = channels[i];
1552 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001553 continue;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001554 if (ftab[c->type] != NULL)
1555 (*ftab[c->type])(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001556 if (chan_is_dead(c)) {
1557 /*
1558 * we have to remove the fd's from the select mask
1559 * before the channels are free'd and the fd's are
1560 * closed
1561 */
1562 if (c->wfd != -1)
1563 FD_CLR(c->wfd, writeset);
1564 if (c->rfd != -1)
1565 FD_CLR(c->rfd, readset);
1566 if (c->efd != -1) {
1567 if (c->extended_usage == CHAN_EXTENDED_READ)
1568 FD_CLR(c->efd, readset);
1569 if (c->extended_usage == CHAN_EXTENDED_WRITE)
1570 FD_CLR(c->efd, writeset);
1571 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001572 channel_free(c);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001573 }
Damien Millerb38eff82000-04-01 11:09:21 +10001574 }
1575}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001576
Ben Lindstrome9c99912001-06-09 00:41:05 +00001577/*
1578 * Allocate/update select bitmasks and add any bits relevant to channels in
1579 * select bitmasks.
1580 */
Damien Miller4af51302000-04-16 11:18:38 +10001581void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001582channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001583 int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001584{
Damien Miller5e953212001-01-30 09:14:00 +11001585 int n;
1586 u_int sz;
1587
1588 n = MAX(*maxfdp, channel_max_fd);
1589
1590 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001591 /* perhaps check sz < nalloc/2 and shrink? */
1592 if (*readsetp == NULL || sz > *nallocp) {
1593 *readsetp = xrealloc(*readsetp, sz);
1594 *writesetp = xrealloc(*writesetp, sz);
1595 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11001596 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001597 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11001598 memset(*readsetp, 0, sz);
1599 memset(*writesetp, 0, sz);
1600
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001601 if (!rekeying)
1602 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001603}
1604
Ben Lindstrome9c99912001-06-09 00:41:05 +00001605/*
1606 * After select, perform any appropriate operations for channels which have
1607 * events pending.
1608 */
Damien Miller4af51302000-04-16 11:18:38 +10001609void
Damien Miller95def091999-11-25 00:26:21 +11001610channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001611{
Damien Millerb38eff82000-04-01 11:09:21 +10001612 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001613}
1614
Ben Lindstrome9c99912001-06-09 00:41:05 +00001615
Damien Miller5e953212001-01-30 09:14:00 +11001616/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001617
Damien Miller4af51302000-04-16 11:18:38 +10001618void
Damien Miller95def091999-11-25 00:26:21 +11001619channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001620{
Damien Miller95def091999-11-25 00:26:21 +11001621 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001622 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001623
Damien Miller95def091999-11-25 00:26:21 +11001624 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001625 c = channels[i];
1626 if (c == NULL)
1627 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001628
Ben Lindstrome9c99912001-06-09 00:41:05 +00001629 /*
1630 * We are only interested in channels that can have buffered
1631 * incoming data.
1632 */
Damien Miller34132e52000-01-14 15:45:46 +11001633 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001634 if (c->type != SSH_CHANNEL_OPEN &&
1635 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001636 continue;
1637 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001638 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001639 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001640 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001641 if (compat20 &&
1642 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001643 /* XXX is this true? */
Ben Lindstromb1131e92001-03-05 07:27:13 +00001644 debug2("channel %d: no data after CLOSE", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001645 continue;
1646 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001647
Damien Miller95def091999-11-25 00:26:21 +11001648 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001649 if ((c->istate == CHAN_INPUT_OPEN ||
1650 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1651 (len = buffer_len(&c->input)) > 0) {
Ben Lindstrome9c99912001-06-09 00:41:05 +00001652 /*
1653 * Send some data for the other side over the secure
1654 * connection.
1655 */
Damien Miller33b13562000-04-04 14:38:59 +10001656 if (compat20) {
1657 if (len > c->remote_window)
1658 len = c->remote_window;
1659 if (len > c->remote_maxpacket)
1660 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001661 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001662 if (packet_is_interactive()) {
1663 if (len > 1024)
1664 len = 512;
1665 } else {
1666 /* Keep the packets at reasonable size. */
1667 if (len > packet_get_maxsize()/2)
1668 len = packet_get_maxsize()/2;
1669 }
Damien Miller95def091999-11-25 00:26:21 +11001670 }
Damien Millerb38eff82000-04-01 11:09:21 +10001671 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001672 packet_start(compat20 ?
1673 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001674 packet_put_int(c->remote_id);
1675 packet_put_string(buffer_ptr(&c->input), len);
1676 packet_send();
1677 buffer_consume(&c->input, len);
1678 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001679 }
1680 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001681 if (compat13)
1682 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001683 /*
1684 * input-buffer is empty and read-socket shutdown:
1685 * tell peer, that we will not send more data: send IEOF
1686 */
Damien Millerb38eff82000-04-01 11:09:21 +10001687 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001688 }
Damien Miller33b13562000-04-04 14:38:59 +10001689 /* Send extended data, i.e. stderr */
1690 if (compat20 &&
1691 c->remote_window > 0 &&
1692 (len = buffer_len(&c->extended)) > 0 &&
1693 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001694 debug2("channel %d: rwin %d elen %d euse %d",
1695 c->self, c->remote_window, buffer_len(&c->extended),
1696 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001697 if (len > c->remote_window)
1698 len = c->remote_window;
1699 if (len > c->remote_maxpacket)
1700 len = c->remote_maxpacket;
1701 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1702 packet_put_int(c->remote_id);
1703 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1704 packet_put_string(buffer_ptr(&c->extended), len);
1705 packet_send();
1706 buffer_consume(&c->extended, len);
1707 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001708 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001709 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001710 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001711}
1712
Ben Lindstrome9c99912001-06-09 00:41:05 +00001713
1714/* -- protocol input */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001715
Damien Miller4af51302000-04-16 11:18:38 +10001716void
Damien Miller62cee002000-09-23 17:15:56 +11001717channel_input_data(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001718{
Damien Miller34132e52000-01-14 15:45:46 +11001719 int id;
Damien Miller95def091999-11-25 00:26:21 +11001720 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001721 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001722 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001723
Damien Miller95def091999-11-25 00:26:21 +11001724 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001725 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001726 c = channel_lookup(id);
1727 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001728 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001729
Damien Miller95def091999-11-25 00:26:21 +11001730 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001731 if (c->type != SSH_CHANNEL_OPEN &&
1732 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001733 return;
1734
1735 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001736 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001737 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001738
Damien Miller95def091999-11-25 00:26:21 +11001739 /* Get the data. */
1740 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001741 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001742
Damien Miller33b13562000-04-04 14:38:59 +10001743 if (compat20){
1744 if (data_len > c->local_maxpacket) {
1745 log("channel %d: rcvd big packet %d, maxpack %d",
1746 c->self, data_len, c->local_maxpacket);
1747 }
1748 if (data_len > c->local_window) {
1749 log("channel %d: rcvd too much data %d, win %d",
1750 c->self, data_len, c->local_window);
1751 xfree(data);
1752 return;
1753 }
1754 c->local_window -= data_len;
1755 }else{
1756 packet_integrity_check(plen, 4 + 4 + data_len, type);
1757 }
Damien Millerb38eff82000-04-01 11:09:21 +10001758 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001759 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001760}
Ben Lindstrome9c99912001-06-09 00:41:05 +00001761
Damien Miller4af51302000-04-16 11:18:38 +10001762void
Damien Miller62cee002000-09-23 17:15:56 +11001763channel_input_extended_data(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001764{
1765 int id;
1766 int tcode;
1767 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001768 u_int data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001769 Channel *c;
1770
1771 /* Get the channel number and verify it. */
1772 id = packet_get_int();
1773 c = channel_lookup(id);
1774
1775 if (c == NULL)
1776 packet_disconnect("Received extended_data for bad channel %d.", id);
1777 if (c->type != SSH_CHANNEL_OPEN) {
1778 log("channel %d: ext data for non open", id);
1779 return;
1780 }
1781 tcode = packet_get_int();
1782 if (c->efd == -1 ||
1783 c->extended_usage != CHAN_EXTENDED_WRITE ||
1784 tcode != SSH2_EXTENDED_DATA_STDERR) {
1785 log("channel %d: bad ext data", c->self);
1786 return;
1787 }
1788 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001789 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001790 if (data_len > c->local_window) {
1791 log("channel %d: rcvd too much extended_data %d, win %d",
1792 c->self, data_len, c->local_window);
1793 xfree(data);
1794 return;
1795 }
Damien Millerd3444942000-09-30 14:20:03 +11001796 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001797 c->local_window -= data_len;
1798 buffer_append(&c->extended, data, data_len);
1799 xfree(data);
1800}
1801
Damien Miller4af51302000-04-16 11:18:38 +10001802void
Damien Miller62cee002000-09-23 17:15:56 +11001803channel_input_ieof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001804{
1805 int id;
1806 Channel *c;
1807
1808 packet_integrity_check(plen, 4, type);
1809
1810 id = packet_get_int();
1811 c = channel_lookup(id);
1812 if (c == NULL)
1813 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1814 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001815
1816 /* XXX force input close */
1817 if (c->force_drain) {
1818 debug("channel %d: FORCE input drain", c->self);
1819 c->istate = CHAN_INPUT_WAIT_DRAIN;
1820 }
1821
Damien Millerb38eff82000-04-01 11:09:21 +10001822}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001823
Damien Miller4af51302000-04-16 11:18:38 +10001824void
Damien Miller62cee002000-09-23 17:15:56 +11001825channel_input_close(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001826{
Damien Millerb38eff82000-04-01 11:09:21 +10001827 int id;
1828 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001829
Damien Millerb38eff82000-04-01 11:09:21 +10001830 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001831
Damien Millerb38eff82000-04-01 11:09:21 +10001832 id = packet_get_int();
1833 c = channel_lookup(id);
1834 if (c == NULL)
1835 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001836
1837 /*
1838 * Send a confirmation that we have closed the channel and no more
1839 * data is coming for it.
1840 */
Damien Miller95def091999-11-25 00:26:21 +11001841 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001842 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001843 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001844
Damien Miller5428f641999-11-25 11:54:57 +11001845 /*
1846 * If the channel is in closed state, we have sent a close request,
1847 * and the other side will eventually respond with a confirmation.
1848 * Thus, we cannot free the channel here, because then there would be
1849 * no-one to receive the confirmation. The channel gets freed when
1850 * the confirmation arrives.
1851 */
Damien Millerb38eff82000-04-01 11:09:21 +10001852 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001853 /*
1854 * Not a closed channel - mark it as draining, which will
1855 * cause it to be freed later.
1856 */
Damien Millerb38eff82000-04-01 11:09:21 +10001857 buffer_consume(&c->input, buffer_len(&c->input));
1858 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001859 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001860}
1861
Damien Millerb38eff82000-04-01 11:09:21 +10001862/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001863void
Damien Miller62cee002000-09-23 17:15:56 +11001864channel_input_oclose(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001865{
Damien Millerb38eff82000-04-01 11:09:21 +10001866 int id = packet_get_int();
1867 Channel *c = channel_lookup(id);
1868 packet_integrity_check(plen, 4, type);
1869 if (c == NULL)
1870 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1871 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001872}
1873
Damien Miller4af51302000-04-16 11:18:38 +10001874void
Damien Miller62cee002000-09-23 17:15:56 +11001875channel_input_close_confirmation(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001876{
1877 int id = packet_get_int();
1878 Channel *c = channel_lookup(id);
1879
Damien Miller4af51302000-04-16 11:18:38 +10001880 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001881 if (c == NULL)
1882 packet_disconnect("Received close confirmation for "
1883 "out-of-range channel %d.", id);
1884 if (c->type != SSH_CHANNEL_CLOSED)
1885 packet_disconnect("Received close confirmation for "
1886 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001887 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001888}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001889
Damien Miller4af51302000-04-16 11:18:38 +10001890void
Damien Miller62cee002000-09-23 17:15:56 +11001891channel_input_open_confirmation(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001892{
Damien Millerb38eff82000-04-01 11:09:21 +10001893 int id, remote_id;
1894 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001895
Damien Miller33b13562000-04-04 14:38:59 +10001896 if (!compat20)
1897 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001898
Damien Millerb38eff82000-04-01 11:09:21 +10001899 id = packet_get_int();
1900 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001901
Damien Millerb38eff82000-04-01 11:09:21 +10001902 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1903 packet_disconnect("Received open confirmation for "
1904 "non-opening channel %d.", id);
1905 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001906 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001907 c->remote_id = remote_id;
1908 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001909
1910 if (compat20) {
1911 c->remote_window = packet_get_int();
1912 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001913 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001914 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001915 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001916 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001917 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001918 }
1919 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1920 c->remote_window, c->remote_maxpacket);
1921 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001922}
1923
Ben Lindstrombba81212001-06-25 05:01:22 +00001924static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00001925reason2txt(int reason)
1926{
1927 switch(reason) {
1928 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
1929 return "administratively prohibited";
1930 case SSH2_OPEN_CONNECT_FAILED:
1931 return "connect failed";
1932 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
1933 return "unknown channel type";
1934 case SSH2_OPEN_RESOURCE_SHORTAGE:
1935 return "resource shortage";
1936 }
Ben Lindstrome2595442001-06-05 20:01:39 +00001937 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00001938}
1939
Damien Miller4af51302000-04-16 11:18:38 +10001940void
Damien Miller62cee002000-09-23 17:15:56 +11001941channel_input_open_failure(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001942{
Kevin Steves12057502001-02-05 14:54:34 +00001943 int id, reason;
1944 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001945 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001946
Damien Miller33b13562000-04-04 14:38:59 +10001947 if (!compat20)
1948 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001949
1950 id = packet_get_int();
1951 c = channel_lookup(id);
1952
1953 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1954 packet_disconnect("Received open failure for "
1955 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001956 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00001957 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00001958 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00001959 msg = packet_get_string(NULL);
1960 lang = packet_get_string(NULL);
1961 }
Damien Miller4af51302000-04-16 11:18:38 +10001962 packet_done();
Ben Lindstrom69128662001-05-08 20:07:39 +00001963 log("channel %d: open failed: %s%s%s", id,
1964 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00001965 if (msg != NULL)
1966 xfree(msg);
1967 if (lang != NULL)
1968 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001969 }
Damien Miller95def091999-11-25 00:26:21 +11001970 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001971 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001972}
1973
Damien Miller33b13562000-04-04 14:38:59 +10001974void
Damien Miller62cee002000-09-23 17:15:56 +11001975channel_input_channel_request(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001976{
1977 int id;
1978 Channel *c;
1979
1980 id = packet_get_int();
1981 c = channel_lookup(id);
1982
1983 if (c == NULL ||
1984 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1985 packet_disconnect("Received request for "
1986 "non-open channel %d.", id);
1987 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001988 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001989 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001990 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001991 } else {
1992 char *service = packet_get_string(NULL);
Ben Lindstromcc74df72001-03-05 06:20:14 +00001993 debug("channel %d: rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001994 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001995 xfree(service);
1996 }
1997}
1998
Damien Miller4af51302000-04-16 11:18:38 +10001999void
Damien Miller62cee002000-09-23 17:15:56 +11002000channel_input_window_adjust(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002001{
2002 Channel *c;
2003 int id, adjust;
2004
2005 if (!compat20)
2006 return;
2007
2008 /* Get the channel number and verify it. */
2009 id = packet_get_int();
2010 c = channel_lookup(id);
2011
2012 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
2013 log("Received window adjust for "
2014 "non-open channel %d.", id);
2015 return;
2016 }
2017 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10002018 packet_done();
Damien Millerd3444942000-09-30 14:20:03 +11002019 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10002020 c->remote_window += adjust;
2021}
2022
Damien Miller4af51302000-04-16 11:18:38 +10002023void
Ben Lindstrome9c99912001-06-09 00:41:05 +00002024channel_input_port_open(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002025{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002026 Channel *c = NULL;
2027 u_short host_port;
2028 char *host, *originator_string;
2029 int remote_id, sock = -1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002030
Ben Lindstrome9c99912001-06-09 00:41:05 +00002031 remote_id = packet_get_int();
2032 host = packet_get_string(NULL);
2033 host_port = packet_get_int();
2034
2035 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2036 originator_string = packet_get_string(NULL);
2037 } else {
2038 originator_string = xstrdup("unknown (remote did not supply name)");
2039 }
2040 packet_done();
2041 sock = channel_connect_to(host, host_port);
2042 if (sock != -1) {
2043 c = channel_new("connected socket",
2044 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2045 originator_string, 1);
2046 if (c == NULL) {
2047 error("channel_input_port_open: channel_new failed");
2048 close(sock);
2049 } else {
2050 c->remote_id = remote_id;
Damien Miller95def091999-11-25 00:26:21 +11002051 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002052 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002053 if (c == NULL) {
2054 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2055 packet_put_int(remote_id);
2056 packet_send();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002057 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002058 xfree(host);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002059}
2060
2061
Ben Lindstrome9c99912001-06-09 00:41:05 +00002062/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002063
Ben Lindstrom908afed2001-10-03 17:34:59 +00002064void
2065channel_set_af(int af)
2066{
2067 IPv4or6 = af;
2068}
2069
Damien Miller5428f641999-11-25 11:54:57 +11002070/*
2071 * Initiate forwarding of connections to local port "port" through the secure
2072 * channel to host:port from remote side.
2073 */
Kevin Steves12057502001-02-05 14:54:34 +00002074int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002075channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
2076 u_short port_to_connect, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002077{
Kevin Steves12057502001-02-05 14:54:34 +00002078 return channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002079 NULL, listen_port,
2080 host_to_connect, port_to_connect,
2081 gateway_ports, /*remote_fwd*/ 0);
2082}
2083
2084/*
2085 * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
2086 * (SSH_CHANNEL_RPORT_LISTENER).
2087 */
Kevin Steves12057502001-02-05 14:54:34 +00002088int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002089channel_request_forwarding(
2090 const char *listen_address, u_short listen_port,
2091 const char *host_to_connect, u_short port_to_connect,
2092 int gateway_ports, int remote_fwd)
2093{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002094 Channel *c;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002095 int success, sock, on = 1, type;
Damien Miller34132e52000-01-14 15:45:46 +11002096 struct addrinfo hints, *ai, *aitop;
2097 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0bc1bd82000-11-13 22:57:25 +11002098 const char *host;
Damien Miller5428f641999-11-25 11:54:57 +11002099 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002100
Kevin Steves12057502001-02-05 14:54:34 +00002101 success = 0;
2102
Damien Miller0bc1bd82000-11-13 22:57:25 +11002103 if (remote_fwd) {
2104 host = listen_address;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002105 type = SSH_CHANNEL_RPORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002106 } else {
2107 host = host_to_connect;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002108 type = SSH_CHANNEL_PORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002109 }
2110
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002111 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00002112 error("Forward host name too long.");
2113 return success;
2114 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002115
Damien Miller0bc1bd82000-11-13 22:57:25 +11002116 /* XXX listen_address is currently ignored */
Damien Miller5428f641999-11-25 11:54:57 +11002117 /*
Damien Miller34132e52000-01-14 15:45:46 +11002118 * getaddrinfo returns a loopback address if the hostname is
2119 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002120 */
Damien Miller34132e52000-01-14 15:45:46 +11002121 memset(&hints, 0, sizeof(hints));
2122 hints.ai_family = IPv4or6;
2123 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
2124 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002125 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002126 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
2127 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11002128
Damien Miller34132e52000-01-14 15:45:46 +11002129 for (ai = aitop; ai; ai = ai->ai_next) {
2130 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2131 continue;
2132 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2133 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11002134 error("channel_request_forwarding: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002135 continue;
2136 }
2137 /* Create a port to listen for the host. */
2138 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2139 if (sock < 0) {
2140 /* this is no error since kernel may not support ipv6 */
2141 verbose("socket: %.100s", strerror(errno));
2142 continue;
2143 }
2144 /*
2145 * Set socket options. We would like the socket to disappear
2146 * as soon as it has been closed for whatever reason.
2147 */
2148 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
2149 linger.l_onoff = 1;
2150 linger.l_linger = 5;
2151 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
2152 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002153
Damien Miller34132e52000-01-14 15:45:46 +11002154 /* Bind the socket to the address. */
2155 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2156 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002157 if (!ai->ai_next)
2158 error("bind: %.100s", strerror(errno));
2159 else
2160 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002161
Damien Miller34132e52000-01-14 15:45:46 +11002162 close(sock);
2163 continue;
2164 }
2165 /* Start listening for connections on the socket. */
2166 if (listen(sock, 5) < 0) {
2167 error("listen: %.100s", strerror(errno));
2168 close(sock);
2169 continue;
2170 }
2171 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002172 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002173 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002174 0, xstrdup("port listener"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002175 if (c == NULL) {
2176 error("channel_request_forwarding: channel_new failed");
2177 close(sock);
2178 continue;
2179 }
2180 strlcpy(c->path, host, sizeof(c->path));
2181 c->host_port = port_to_connect;
2182 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002183 success = 1;
2184 }
2185 if (success == 0)
Kevin Steves12057502001-02-05 14:54:34 +00002186 error("channel_request_forwarding: cannot listen to port: %d",
2187 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002188 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002189 return success;
Damien Miller95def091999-11-25 00:26:21 +11002190}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002191
Damien Miller5428f641999-11-25 11:54:57 +11002192/*
2193 * Initiate forwarding of connections to port "port" on remote host through
2194 * the secure channel to host:port from local side.
2195 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002196
Damien Miller4af51302000-04-16 11:18:38 +10002197void
Damien Miller0bc1bd82000-11-13 22:57:25 +11002198channel_request_remote_forwarding(u_short listen_port,
2199 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002200{
Damien Miller0bc1bd82000-11-13 22:57:25 +11002201 int payload_len, type, success = 0;
2202
Damien Miller95def091999-11-25 00:26:21 +11002203 /* Record locally that connection to this host/port is permitted. */
2204 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2205 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002206
Damien Miller95def091999-11-25 00:26:21 +11002207 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002208 if (compat20) {
2209 const char *address_to_bind = "0.0.0.0";
2210 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2211 packet_put_cstring("tcpip-forward");
2212 packet_put_char(0); /* boolean: want reply */
2213 packet_put_cstring(address_to_bind);
2214 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002215 packet_send();
2216 packet_write_wait();
2217 /* Assume that server accepts the request */
2218 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002219 } else {
2220 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002221 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002222 packet_put_cstring(host_to_connect);
2223 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002224 packet_send();
2225 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002226
2227 /* Wait for response from the remote side. */
2228 type = packet_read(&payload_len);
2229 switch (type) {
2230 case SSH_SMSG_SUCCESS:
2231 success = 1;
2232 break;
2233 case SSH_SMSG_FAILURE:
2234 log("Warning: Server denied remote port forwarding.");
2235 break;
2236 default:
2237 /* Unknown packet */
2238 packet_disconnect("Protocol error for port forward request:"
2239 "received packet type %d.", type);
2240 }
2241 }
2242 if (success) {
2243 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2244 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2245 permitted_opens[num_permitted_opens].listen_port = listen_port;
2246 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002247 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002248}
2249
Damien Miller5428f641999-11-25 11:54:57 +11002250/*
2251 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2252 * listening for the port, and sends back a success reply (or disconnect
2253 * message if there was an error). This never returns if there was an error.
2254 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002255
Damien Miller4af51302000-04-16 11:18:38 +10002256void
Damien Millere247cc42000-05-07 12:03:14 +10002257channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002258{
Damien Milleraae6c611999-12-06 11:47:28 +11002259 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11002260 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002261
Damien Miller95def091999-11-25 00:26:21 +11002262 /* Get arguments from the packet. */
2263 port = packet_get_int();
2264 hostname = packet_get_string(NULL);
2265 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002266
Damien Millerbac2d8a2000-09-05 16:13:06 +11002267#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002268 /*
2269 * Check that an unprivileged user is not trying to forward a
2270 * privileged port.
2271 */
Damien Miller95def091999-11-25 00:26:21 +11002272 if (port < IPPORT_RESERVED && !is_root)
2273 packet_disconnect("Requested forwarding of port %d but user is not root.",
2274 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11002275#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11002276 /* Initiate forwarding */
Damien Millere247cc42000-05-07 12:03:14 +10002277 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002278
2279 /* Free the argument string. */
2280 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002281}
2282
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002283/*
2284 * Permits opening to any host/port if permitted_opens[] is empty. This is
2285 * usually called by the server, because the user could connect to any port
2286 * anyway, and the server has no way to know but to trust the client anyway.
2287 */
2288void
2289channel_permit_all_opens()
2290{
2291 if (num_permitted_opens == 0)
2292 all_opens_permitted = 1;
2293}
2294
Ben Lindstroma3700052001-04-05 23:26:32 +00002295void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002296channel_add_permitted_opens(char *host, int port)
2297{
2298 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2299 fatal("channel_request_remote_forwarding: too many forwards");
2300 debug("allow port forwarding to host %s port %d", host, port);
2301
2302 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2303 permitted_opens[num_permitted_opens].port_to_connect = port;
2304 num_permitted_opens++;
2305
2306 all_opens_permitted = 0;
2307}
2308
Ben Lindstroma3700052001-04-05 23:26:32 +00002309void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002310channel_clear_permitted_opens(void)
2311{
2312 int i;
2313
2314 for (i = 0; i < num_permitted_opens; i++)
2315 xfree(permitted_opens[i].host_to_connect);
2316 num_permitted_opens = 0;
2317
2318}
2319
2320
2321/* return socket to remote host, port */
Ben Lindstrombba81212001-06-25 05:01:22 +00002322static int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002323connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002324{
Damien Miller34132e52000-01-14 15:45:46 +11002325 struct addrinfo hints, *ai, *aitop;
2326 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2327 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002328 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002329
Damien Miller34132e52000-01-14 15:45:46 +11002330 memset(&hints, 0, sizeof(hints));
2331 hints.ai_family = IPv4or6;
2332 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002333 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002334 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002335 error("connect_to %.100s: unknown host (%s)", host,
2336 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002337 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002338 }
Damien Miller34132e52000-01-14 15:45:46 +11002339 for (ai = aitop; ai; ai = ai->ai_next) {
2340 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2341 continue;
2342 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2343 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002344 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002345 continue;
2346 }
Damien Miller34132e52000-01-14 15:45:46 +11002347 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2348 if (sock < 0) {
2349 error("socket: %.100s", strerror(errno));
2350 continue;
2351 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +00002352 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002353 fatal("connect_to: F_SETFL: %s", strerror(errno));
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002354 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2355 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002356 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002357 strerror(errno));
2358 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002359 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002360 }
2361 break; /* success */
2362
2363 }
2364 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002365 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002366 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002367 return -1;
2368 }
2369 /* success */
2370 return sock;
2371}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002372
Damien Miller0bc1bd82000-11-13 22:57:25 +11002373int
Ben Lindstrom173e6462001-07-04 05:15:15 +00002374channel_connect_by_listen_address(u_short listen_port)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002375{
2376 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002377
Damien Miller0bc1bd82000-11-13 22:57:25 +11002378 for (i = 0; i < num_permitted_opens; i++)
2379 if (permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002380 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002381 permitted_opens[i].host_to_connect,
2382 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002383 error("WARNING: Server requests forwarding for unknown listen_port %d",
2384 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002385 return -1;
2386}
Damien Millerb38eff82000-04-01 11:09:21 +10002387
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002388/* Check if connecting to that port is permitted and connect. */
2389int
2390channel_connect_to(const char *host, u_short port)
2391{
2392 int i, permit;
2393
2394 permit = all_opens_permitted;
2395 if (!permit) {
2396 for (i = 0; i < num_permitted_opens; i++)
2397 if (permitted_opens[i].port_to_connect == port &&
2398 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2399 permit = 1;
2400
2401 }
2402 if (!permit) {
2403 log("Received request to connect to host %.100s port %d, "
2404 "but the request was denied.", host, port);
2405 return -1;
2406 }
2407 return connect_to(host, port);
2408}
2409
Ben Lindstrome9c99912001-06-09 00:41:05 +00002410/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002411
Damien Miller5428f641999-11-25 11:54:57 +11002412/*
2413 * Creates an internet domain socket for listening for X11 connections.
2414 * Returns a suitable value for the DISPLAY variable, or NULL if an error
2415 * occurs.
2416 */
Damien Miller95def091999-11-25 00:26:21 +11002417char *
Damien Millera34a28b1999-12-14 10:47:15 +11002418x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002419{
Damien Milleraae6c611999-12-06 11:47:28 +11002420 int display_number, sock;
2421 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002422 struct addrinfo hints, *ai, *aitop;
2423 char strport[NI_MAXSERV];
2424 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
2425 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11002426 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002427
Damien Millera34a28b1999-12-14 10:47:15 +11002428 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11002429 display_number < MAX_DISPLAYS;
2430 display_number++) {
2431 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002432 memset(&hints, 0, sizeof(hints));
2433 hints.ai_family = IPv4or6;
2434 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
2435 hints.ai_socktype = SOCK_STREAM;
2436 snprintf(strport, sizeof strport, "%d", port);
2437 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2438 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11002439 return NULL;
2440 }
Damien Miller34132e52000-01-14 15:45:46 +11002441 for (ai = aitop; ai; ai = ai->ai_next) {
2442 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2443 continue;
2444 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2445 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002446 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002447 error("socket: %.100s", strerror(errno));
2448 return NULL;
2449 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002450 debug("x11_create_display_inet: Socket family %d not supported",
2451 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002452 continue;
2453 }
Damien Miller34132e52000-01-14 15:45:46 +11002454 }
2455 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2456 debug("bind port %d: %.100s", port, strerror(errno));
2457 shutdown(sock, SHUT_RDWR);
2458 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002459
2460 if (ai->ai_next)
2461 continue;
2462
Damien Miller34132e52000-01-14 15:45:46 +11002463 for (n = 0; n < num_socks; n++) {
2464 shutdown(socks[n], SHUT_RDWR);
2465 close(socks[n]);
2466 }
2467 num_socks = 0;
2468 break;
2469 }
2470 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002471#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002472 if (num_socks == NUM_SOCKS)
2473 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002474#else
2475 break;
2476#endif
Damien Miller95def091999-11-25 00:26:21 +11002477 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002478 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002479 if (num_socks > 0)
2480 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002481 }
Damien Miller95def091999-11-25 00:26:21 +11002482 if (display_number >= MAX_DISPLAYS) {
2483 error("Failed to allocate internet-domain X11 display socket.");
2484 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002485 }
Damien Miller95def091999-11-25 00:26:21 +11002486 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002487 for (n = 0; n < num_socks; n++) {
2488 sock = socks[n];
2489 if (listen(sock, 5) < 0) {
2490 error("listen: %.100s", strerror(errno));
2491 shutdown(sock, SHUT_RDWR);
2492 close(sock);
2493 return NULL;
2494 }
Damien Miller95def091999-11-25 00:26:21 +11002495 }
Damien Miller34132e52000-01-14 15:45:46 +11002496
Damien Miller95def091999-11-25 00:26:21 +11002497 /* Set up a suitable value for the DISPLAY variable. */
2498 if (gethostname(hostname, sizeof(hostname)) < 0)
2499 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11002500
2501#ifdef IPADDR_IN_DISPLAY
Kevin Stevesef4eea92001-02-05 12:42:17 +00002502 /*
Damien Miller76112de1999-12-21 11:18:08 +11002503 * HPUX detects the local hostname in the DISPLAY variable and tries
2504 * to set up a shared memory connection to the server, which it
2505 * incorrectly supposes to be local.
2506 *
2507 * The workaround - as used in later $$H and other programs - is
2508 * is to set display to the host's IP address.
2509 */
2510 {
2511 struct hostent *he;
2512 struct in_addr my_addr;
2513
2514 he = gethostbyname(hostname);
2515 if (he == NULL) {
2516 error("[X11-broken-fwd-hostname-workaround] Could not get "
2517 "IP address for hostname %s.", hostname);
2518
2519 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
2520 "Could not get IP address for hostname %s.", hostname);
2521
2522 shutdown(sock, SHUT_RDWR);
2523 close(sock);
2524
2525 return NULL;
2526 }
2527
2528 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2529
2530 /* Set DISPLAY to <ip address>:screen.display */
Kevin Stevesef4eea92001-02-05 12:42:17 +00002531 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Kevin Stevesda6cd592000-12-29 18:41:21 +00002532 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002533 }
2534#else /* IPADDR_IN_DISPLAY */
2535 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11002536 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Kevin Stevesda6cd592000-12-29 18:41:21 +00002537 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002538#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002539
Damien Miller34132e52000-01-14 15:45:46 +11002540 /* Allocate a channel for each socket. */
2541 for (n = 0; n < num_socks; n++) {
2542 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10002543 (void) channel_new("x11 listener",
2544 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2545 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002546 0, xstrdup("X11 inet listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11002547 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002548
Damien Miller95def091999-11-25 00:26:21 +11002549 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11002550 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002551}
2552
2553#ifndef X_UNIX_PATH
2554#define X_UNIX_PATH "/tmp/.X11-unix/X"
2555#endif
2556
Ben Lindstrombba81212001-06-25 05:01:22 +00002557static int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002558connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002559{
Damien Miller95def091999-11-25 00:26:21 +11002560 static const char *const x_sockets[] = {
2561 X_UNIX_PATH "%u",
2562 "/var/X/.X11-unix/X" "%u",
2563 "/usr/spool/sockets/X11/" "%u",
2564 NULL
2565 };
2566 int sock;
2567 struct sockaddr_un addr;
2568 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002569
Damien Miller95def091999-11-25 00:26:21 +11002570 for (path = x_sockets; *path; ++path) {
2571 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2572 if (sock < 0)
2573 error("socket: %.100s", strerror(errno));
2574 memset(&addr, 0, sizeof(addr));
2575 addr.sun_family = AF_UNIX;
2576 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
2577 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2578 return sock;
2579 close(sock);
2580 }
2581 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2582 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002583}
2584
Damien Millerbd483e72000-04-30 10:00:53 +10002585int
2586x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002587{
Damien Millerbd483e72000-04-30 10:00:53 +10002588 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002589 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002590 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002591 struct addrinfo hints, *ai, *aitop;
2592 char strport[NI_MAXSERV];
2593 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002594
Damien Miller95def091999-11-25 00:26:21 +11002595 /* Try to open a socket for the local X server. */
2596 display = getenv("DISPLAY");
2597 if (!display) {
2598 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002599 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002600 }
Damien Miller5428f641999-11-25 11:54:57 +11002601 /*
2602 * Now we decode the value of the DISPLAY variable and make a
2603 * connection to the real X server.
2604 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002605
Damien Miller5428f641999-11-25 11:54:57 +11002606 /*
2607 * Check if it is a unix domain socket. Unix domain displays are in
2608 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2609 */
Damien Miller95def091999-11-25 00:26:21 +11002610 if (strncmp(display, "unix:", 5) == 0 ||
2611 display[0] == ':') {
2612 /* Connect to the unix domain socket. */
2613 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2614 error("Could not parse display number from DISPLAY: %.100s",
2615 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002616 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002617 }
2618 /* Create a socket. */
2619 sock = connect_local_xsocket(display_number);
2620 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002621 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002622
2623 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002624 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002625 }
Damien Miller5428f641999-11-25 11:54:57 +11002626 /*
2627 * Connect to an inet socket. The DISPLAY value is supposedly
2628 * hostname:d[.s], where hostname may also be numeric IP address.
2629 */
Damien Miller95def091999-11-25 00:26:21 +11002630 strncpy(buf, display, sizeof(buf));
2631 buf[sizeof(buf) - 1] = 0;
2632 cp = strchr(buf, ':');
2633 if (!cp) {
2634 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002635 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002636 }
Damien Miller95def091999-11-25 00:26:21 +11002637 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002638 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002639 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2640 error("Could not parse display number from DISPLAY: %.100s",
2641 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002642 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002643 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002644
Damien Miller34132e52000-01-14 15:45:46 +11002645 /* Look up the host address */
2646 memset(&hints, 0, sizeof(hints));
2647 hints.ai_family = IPv4or6;
2648 hints.ai_socktype = SOCK_STREAM;
2649 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2650 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2651 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002652 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002653 }
Damien Miller34132e52000-01-14 15:45:46 +11002654 for (ai = aitop; ai; ai = ai->ai_next) {
2655 /* Create a socket. */
2656 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2657 if (sock < 0) {
2658 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002659 continue;
2660 }
2661 /* Connect it to the display. */
2662 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2663 debug("connect %.100s port %d: %.100s", buf,
2664 6000 + display_number, strerror(errno));
2665 close(sock);
2666 continue;
2667 }
2668 /* Success */
2669 break;
Damien Miller34132e52000-01-14 15:45:46 +11002670 }
Damien Miller34132e52000-01-14 15:45:46 +11002671 freeaddrinfo(aitop);
2672 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002673 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002674 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002675 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002676 }
Damien Millerbd483e72000-04-30 10:00:53 +10002677 return sock;
2678}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002679
Damien Millerbd483e72000-04-30 10:00:53 +10002680/*
2681 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2682 * the remote channel number. We should do whatever we want, and respond
2683 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2684 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002685
Damien Millerbd483e72000-04-30 10:00:53 +10002686void
Damien Miller62cee002000-09-23 17:15:56 +11002687x11_input_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002688{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002689 Channel *c = NULL;
2690 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002691 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10002692
2693 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002694
2695 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002696
2697 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002698 remote_host = packet_get_string(NULL);
2699 } else {
2700 remote_host = xstrdup("unknown (remote did not supply name)");
2701 }
2702 packet_done();
Damien Millerbd483e72000-04-30 10:00:53 +10002703
2704 /* Obtain a connection to the real X display. */
2705 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002706 if (sock != -1) {
2707 /* Allocate a channel for this connection. */
2708 c = channel_new("connected x11 socket",
2709 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
2710 remote_host, 1);
2711 if (c == NULL) {
2712 error("x11_input_open: channel_new failed");
2713 close(sock);
2714 } else {
2715 c->remote_id = remote_id;
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002716 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002717 }
2718 }
2719 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10002720 /* Send refusal to the remote host. */
2721 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002722 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10002723 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10002724 /* Send a confirmation to the remote host. */
2725 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002726 packet_put_int(remote_id);
2727 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10002728 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002729 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002730}
2731
Damien Miller69b69aa2000-10-28 14:19:58 +11002732/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2733void
2734deny_input_open(int type, int plen, void *ctxt)
2735{
2736 int rchan = packet_get_int();
2737 switch(type){
2738 case SSH_SMSG_AGENT_OPEN:
2739 error("Warning: ssh server tried agent forwarding.");
2740 break;
2741 case SSH_SMSG_X11_OPEN:
2742 error("Warning: ssh server tried X11 forwarding.");
2743 break;
2744 default:
2745 error("deny_input_open: type %d plen %d", type, plen);
2746 break;
2747 }
2748 error("Warning: this is probably a break in attempt by a malicious server.");
2749 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2750 packet_put_int(rchan);
2751 packet_send();
2752}
2753
Damien Miller5428f641999-11-25 11:54:57 +11002754/*
2755 * Requests forwarding of X11 connections, generates fake authentication
2756 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00002757 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11002758 */
Damien Miller4af51302000-04-16 11:18:38 +10002759void
Damien Millerbd483e72000-04-30 10:00:53 +10002760x11_request_forwarding_with_spoofing(int client_session_id,
2761 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002762{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002763 u_int data_len = (u_int) strlen(data) / 2;
Ben Lindstromb3211a82001-02-10 22:33:19 +00002764 u_int i, value, len;
Damien Miller95def091999-11-25 00:26:21 +11002765 char *new_data;
2766 int screen_number;
2767 const char *cp;
2768 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002769
Damien Miller95def091999-11-25 00:26:21 +11002770 cp = getenv("DISPLAY");
2771 if (cp)
2772 cp = strchr(cp, ':');
2773 if (cp)
2774 cp = strchr(cp, '.');
2775 if (cp)
2776 screen_number = atoi(cp + 1);
2777 else
2778 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002779
Damien Miller95def091999-11-25 00:26:21 +11002780 /* Save protocol name. */
2781 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002782
Damien Miller5428f641999-11-25 11:54:57 +11002783 /*
2784 * Extract real authentication data and generate fake data of the
2785 * same length.
2786 */
Damien Miller95def091999-11-25 00:26:21 +11002787 x11_saved_data = xmalloc(data_len);
2788 x11_fake_data = xmalloc(data_len);
2789 for (i = 0; i < data_len; i++) {
2790 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2791 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2792 if (i % 4 == 0)
2793 rand = arc4random();
2794 x11_saved_data[i] = value;
2795 x11_fake_data[i] = rand & 0xff;
2796 rand >>= 8;
2797 }
2798 x11_saved_data_len = data_len;
2799 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002800
Damien Miller95def091999-11-25 00:26:21 +11002801 /* Convert the fake data into hex. */
Ben Lindstromb3211a82001-02-10 22:33:19 +00002802 len = 2 * data_len + 1;
2803 new_data = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +11002804 for (i = 0; i < data_len; i++)
Ben Lindstromb3211a82001-02-10 22:33:19 +00002805 snprintf(new_data + 2 * i, len - 2 * i,
2806 "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002807
Damien Miller95def091999-11-25 00:26:21 +11002808 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002809 if (compat20) {
2810 channel_request_start(client_session_id, "x11-req", 0);
2811 packet_put_char(0); /* XXX bool single connection */
2812 } else {
2813 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2814 }
2815 packet_put_cstring(proto);
2816 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002817 packet_put_int(screen_number);
2818 packet_send();
2819 packet_write_wait();
2820 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002821}
2822
Ben Lindstrome9c99912001-06-09 00:41:05 +00002823
2824/* -- agent forwarding */
2825
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002826/* Sends a message to the server to request authentication fd forwarding. */
2827
Damien Miller4af51302000-04-16 11:18:38 +10002828void
Damien Miller95def091999-11-25 00:26:21 +11002829auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002830{
Damien Miller95def091999-11-25 00:26:21 +11002831 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2832 packet_send();
2833 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002834}
2835
Damien Miller5428f641999-11-25 11:54:57 +11002836/*
2837 * Returns the name of the forwarded authentication socket. Returns NULL if
2838 * there is no forwarded authentication socket. The returned value points to
2839 * a static buffer.
2840 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002841
Damien Miller95def091999-11-25 00:26:21 +11002842char *
2843auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002844{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002845 return auth_sock_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002846}
2847
2848/* removes the agent forwarding socket */
2849
Damien Miller4af51302000-04-16 11:18:38 +10002850void
Ben Lindstrom983c0982001-06-09 01:20:06 +00002851auth_sock_cleanup_proc(void *_pw)
Damien Miller95def091999-11-25 00:26:21 +11002852{
Ben Lindstrom983c0982001-06-09 01:20:06 +00002853 struct passwd *pw = _pw;
2854
Ben Lindstrom838394c2001-06-09 01:11:59 +00002855 if (auth_sock_name) {
Ben Lindstrom983c0982001-06-09 01:20:06 +00002856 temporarily_use_uid(pw);
Ben Lindstrom838394c2001-06-09 01:11:59 +00002857 unlink(auth_sock_name);
2858 rmdir(auth_sock_dir);
2859 auth_sock_name = NULL;
Ben Lindstrom983c0982001-06-09 01:20:06 +00002860 restore_uid();
Ben Lindstrom838394c2001-06-09 01:11:59 +00002861 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002862}
2863
Damien Miller5428f641999-11-25 11:54:57 +11002864/*
Damien Millerd3a18572000-06-07 19:55:44 +10002865 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002866 * This starts forwarding authentication requests.
2867 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002868
Damien Millerd3a18572000-06-07 19:55:44 +10002869int
Damien Miller95def091999-11-25 00:26:21 +11002870auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002871{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002872 Channel *nc;
2873 int sock;
Damien Miller95def091999-11-25 00:26:21 +11002874 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002875
Ben Lindstrome9c99912001-06-09 00:41:05 +00002876 if (auth_get_socket_name() != NULL) {
2877 error("authentication forwarding requested twice.");
2878 return 0;
2879 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002880
Damien Miller95def091999-11-25 00:26:21 +11002881 /* Temporarily drop privileged uid for mkdir/bind. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00002882 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002883
Damien Miller95def091999-11-25 00:26:21 +11002884 /* Allocate a buffer for the socket name, and format the name. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002885 auth_sock_name = xmalloc(MAXPATHLEN);
2886 auth_sock_dir = xmalloc(MAXPATHLEN);
2887 strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002888
Damien Miller95def091999-11-25 00:26:21 +11002889 /* Create private directory for socket */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002890 if (mkdtemp(auth_sock_dir) == NULL) {
2891 packet_send_debug("Agent forwarding disabled: "
2892 "mkdtemp() failed: %.100s", strerror(errno));
Damien Millerd3a18572000-06-07 19:55:44 +10002893 restore_uid();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002894 xfree(auth_sock_name);
2895 xfree(auth_sock_dir);
2896 auth_sock_name = NULL;
2897 auth_sock_dir = NULL;
Damien Millerd3a18572000-06-07 19:55:44 +10002898 return 0;
2899 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002900 snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%d",
2901 auth_sock_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002902
Ben Lindstrom838394c2001-06-09 01:11:59 +00002903 /* delete agent socket on fatal() */
Ben Lindstrom983c0982001-06-09 01:20:06 +00002904 fatal_add_cleanup(auth_sock_cleanup_proc, pw);
Ben Lindstrom838394c2001-06-09 01:11:59 +00002905
Damien Miller95def091999-11-25 00:26:21 +11002906 /* Create the socket. */
2907 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2908 if (sock < 0)
2909 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002910
Damien Miller95def091999-11-25 00:26:21 +11002911 /* Bind it to the name. */
2912 memset(&sunaddr, 0, sizeof(sunaddr));
2913 sunaddr.sun_family = AF_UNIX;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002914 strncpy(sunaddr.sun_path, auth_sock_name,
Damien Miller95def091999-11-25 00:26:21 +11002915 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002916
Damien Miller95def091999-11-25 00:26:21 +11002917 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2918 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002919
Damien Miller95def091999-11-25 00:26:21 +11002920 /* Restore the privileged uid. */
2921 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002922
Damien Miller95def091999-11-25 00:26:21 +11002923 /* Start listening on the socket. */
2924 if (listen(sock, 5) < 0)
2925 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002926
Damien Miller95def091999-11-25 00:26:21 +11002927 /* Allocate a channel for the authentication agent socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002928 nc = channel_new("auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11002929 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2930 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2931 0, xstrdup("auth socket"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002932 if (nc == NULL) {
2933 error("auth_input_request_forwarding: channel_new failed");
Ben Lindstrom983c0982001-06-09 01:20:06 +00002934 auth_sock_cleanup_proc(pw);
Ben Lindstromdf4981b2001-06-09 01:32:29 +00002935 fatal_remove_cleanup(auth_sock_cleanup_proc, pw);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002936 close(sock);
2937 return 0;
2938 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002939 strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
Damien Millerd3a18572000-06-07 19:55:44 +10002940 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002941}
2942
2943/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2944
Damien Miller4af51302000-04-16 11:18:38 +10002945void
Damien Miller62cee002000-09-23 17:15:56 +11002946auth_input_open_request(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002947{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002948 Channel *c = NULL;
2949 int remote_id, sock;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002950 char *name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002951
Damien Millerb38eff82000-04-01 11:09:21 +10002952 packet_integrity_check(plen, 4, type);
2953
Damien Miller95def091999-11-25 00:26:21 +11002954 /* Read the remote channel number from the message. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002955 remote_id = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002956
Damien Miller5428f641999-11-25 11:54:57 +11002957 /*
2958 * Get a connection to the local authentication agent (this may again
2959 * get forwarded).
2960 */
Damien Miller95def091999-11-25 00:26:21 +11002961 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002962
Damien Miller5428f641999-11-25 11:54:57 +11002963 /*
2964 * If we could not connect the agent, send an error message back to
2965 * the server. This should never happen unless the agent dies,
2966 * because authentication forwarding is only enabled if we have an
2967 * agent.
2968 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002969 if (sock >= 0) {
Ben Lindstrome9c99912001-06-09 00:41:05 +00002970 name = xstrdup("authentication agent connection");
2971 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
2972 -1, 0, 0, 0, name, 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002973 if (c == NULL) {
2974 error("auth_input_open_request: channel_new failed");
Ben Lindstrome9c99912001-06-09 00:41:05 +00002975 xfree(name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002976 close(sock);
2977 } else {
2978 c->remote_id = remote_id;
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002979 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002980 }
Damien Miller95def091999-11-25 00:26:21 +11002981 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002982 if (c == NULL) {
2983 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2984 packet_put_int(remote_id);
2985 } else {
2986 /* Send a confirmation to the remote host. */
2987 debug("Forwarding authentication connection.");
2988 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2989 packet_put_int(remote_id);
2990 packet_put_int(c->self);
2991 }
Damien Miller95def091999-11-25 00:26:21 +11002992 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002993}