blob: 758ea506da0691b1bb706488c2e229be46683325 [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 Milleraf5f2e62001-10-10 15:01:16 +100042RCSID("$OpenBSD: channels.c,v 1.136 2001/10/04 15:05: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 Miller95def091999-11-25 00:26:21 +1100244 buffer_init(&c->input);
245 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000246 buffer_init(&c->extended);
Damien Miller95def091999-11-25 00:26:21 +1100247 chan_init_iostates(c);
Damien Miller69b69aa2000-10-28 14:19:58 +1100248 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100249 c->self = found;
250 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000251 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000252 c->local_window = window;
253 c->local_window_max = window;
254 c->local_consumed = 0;
255 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100256 c->remote_id = -1;
257 c->remote_name = remote_name;
Damien Millerb38eff82000-04-01 11:09:21 +1000258 c->remote_window = 0;
259 c->remote_maxpacket = 0;
260 c->cb_fn = NULL;
261 c->cb_arg = NULL;
262 c->cb_event = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000263 c->force_drain = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000264 c->detach_user = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000265 c->input_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100266 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000267 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000269
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000270static int
271channel_find_maxfd(void)
272{
273 int i, max = 0;
274 Channel *c;
275
276 for (i = 0; i < channels_alloc; i++) {
277 c = channels[i];
278 if (c != NULL) {
279 max = MAX(max, c->rfd);
280 max = MAX(max, c->wfd);
281 max = MAX(max, c->efd);
282 }
283 }
284 return max;
285}
286
287int
288channel_close_fd(int *fdp)
289{
290 int ret = 0, fd = *fdp;
291
292 if (fd != -1) {
293 ret = close(fd);
294 *fdp = -1;
295 if (fd == channel_max_fd)
296 channel_max_fd = channel_find_maxfd();
297 }
298 return ret;
299}
300
Damien Miller6f83b8e2000-05-02 09:23:45 +1000301/* Close all channel fd/socket. */
302
Ben Lindstrombba81212001-06-25 05:01:22 +0000303static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000304channel_close_fds(Channel *c)
305{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000306 debug3("channel_close_fds: channel %d: r %d w %d e %d",
307 c->self, c->rfd, c->wfd, c->efd);
308
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000309 channel_close_fd(&c->sock);
310 channel_close_fd(&c->rfd);
311 channel_close_fd(&c->wfd);
312 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000313}
314
315/* Free the channel and close its fd/socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000316
Damien Miller4af51302000-04-16 11:18:38 +1000317void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000318channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000320 char *s;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000321 int i, n;
322
323 for (n = 0, i = 0; i < channels_alloc; i++)
324 if (channels[i])
325 n++;
Ben Lindstrom4c247552001-06-05 20:56:47 +0000326 debug("channel_free: channel %d: %s, nchannels %d", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000327 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000328
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000329 s = channel_open_message();
Ben Lindstrom4c247552001-06-05 20:56:47 +0000330 debug3("channel_free: status: %s", s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000331 xfree(s);
332
Ben Lindstrom809744e2001-07-04 05:26:06 +0000333 if (c->detach_user != NULL) {
334 debug("channel_free: channel %d: detaching channel user", c->self);
335 c->detach_user(c->self, NULL);
Damien Miller33b13562000-04-04 14:38:59 +1000336 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000337 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000338 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000339 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000340 buffer_free(&c->input);
341 buffer_free(&c->output);
342 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000343 if (c->remote_name) {
344 xfree(c->remote_name);
345 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100346 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000347 channels[c->self] = NULL;
348 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349}
350
Ben Lindstrome9c99912001-06-09 00:41:05 +0000351void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000352channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000353{
354 int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000355
Ben Lindstrom601e4362001-06-21 03:19:23 +0000356 for (i = 0; i < channels_alloc; i++)
357 if (channels[i] != NULL)
358 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000359}
360
Ben Lindstrom809744e2001-07-04 05:26:06 +0000361void
362channel_detach_all(void)
363{
364 int i;
365 Channel *c;
366
367 for (i = 0; i < channels_alloc; i++) {
368 c = channels[i];
369 if (c != NULL && c->detach_user != NULL) {
370 debug("channel_detach_all: channel %d", c->self);
371 c->detach_user(c->self, NULL);
372 c->detach_user = NULL;
373 }
374 }
375}
376
Ben Lindstrome9c99912001-06-09 00:41:05 +0000377/*
378 * Closes the sockets/fds of all channels. This is used to close extra file
379 * descriptors after a fork.
380 */
381
382void
383channel_close_all()
384{
385 int i;
386
387 for (i = 0; i < channels_alloc; i++)
388 if (channels[i] != NULL)
389 channel_close_fds(channels[i]);
390}
391
392/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000393 * Stop listening to channels.
394 */
395
396void
397channel_stop_listening(void)
398{
399 int i;
400 Channel *c;
401
402 for (i = 0; i < channels_alloc; i++) {
403 c = channels[i];
404 if (c != NULL) {
405 switch (c->type) {
406 case SSH_CHANNEL_AUTH_SOCKET:
407 case SSH_CHANNEL_PORT_LISTENER:
408 case SSH_CHANNEL_RPORT_LISTENER:
409 case SSH_CHANNEL_X11_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000410 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000411 channel_free(c);
412 break;
413 }
414 }
415 }
416}
417
418/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000419 * Returns true if no channel has too much buffered data, and false if one or
420 * more channel is overfull.
421 */
422
423int
424channel_not_very_much_buffered_data()
425{
426 u_int i;
427 Channel *c;
428
429 for (i = 0; i < channels_alloc; i++) {
430 c = channels[i];
431 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000432#if 0
433 if (!compat20 &&
434 buffer_len(&c->input) > packet_get_maxsize()) {
Ben Lindstrome9c99912001-06-09 00:41:05 +0000435 debug("channel %d: big input buffer %d",
436 c->self, buffer_len(&c->input));
437 return 0;
438 }
Damien Milleraf5f2e62001-10-10 15:01:16 +1000439#endif
Ben Lindstrome9c99912001-06-09 00:41:05 +0000440 if (buffer_len(&c->output) > packet_get_maxsize()) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000441 debug("channel %d: big output buffer %d > %d",
442 c->self, buffer_len(&c->output),
443 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000444 return 0;
445 }
446 }
447 }
448 return 1;
449}
450
451/* Returns true if any channel is still open. */
452
453int
454channel_still_open()
455{
456 int i;
457 Channel *c;
458
459 for (i = 0; i < channels_alloc; i++) {
460 c = channels[i];
461 if (c == NULL)
462 continue;
463 switch (c->type) {
464 case SSH_CHANNEL_X11_LISTENER:
465 case SSH_CHANNEL_PORT_LISTENER:
466 case SSH_CHANNEL_RPORT_LISTENER:
467 case SSH_CHANNEL_CLOSED:
468 case SSH_CHANNEL_AUTH_SOCKET:
469 case SSH_CHANNEL_DYNAMIC:
470 case SSH_CHANNEL_CONNECTING:
471 case SSH_CHANNEL_ZOMBIE:
472 continue;
473 case SSH_CHANNEL_LARVAL:
474 if (!compat20)
475 fatal("cannot happen: SSH_CHANNEL_LARVAL");
476 continue;
477 case SSH_CHANNEL_OPENING:
478 case SSH_CHANNEL_OPEN:
479 case SSH_CHANNEL_X11_OPEN:
480 return 1;
481 case SSH_CHANNEL_INPUT_DRAINING:
482 case SSH_CHANNEL_OUTPUT_DRAINING:
483 if (!compat13)
484 fatal("cannot happen: OUT_DRAIN");
485 return 1;
486 default:
487 fatal("channel_still_open: bad channel type %d", c->type);
488 /* NOTREACHED */
489 }
490 }
491 return 0;
492}
493
494/* Returns the id of an open channel suitable for keepaliving */
495
496int
497channel_find_open()
498{
499 int i;
500 Channel *c;
501
502 for (i = 0; i < channels_alloc; i++) {
503 c = channels[i];
504 if (c == NULL)
505 continue;
506 switch (c->type) {
507 case SSH_CHANNEL_CLOSED:
508 case SSH_CHANNEL_DYNAMIC:
509 case SSH_CHANNEL_X11_LISTENER:
510 case SSH_CHANNEL_PORT_LISTENER:
511 case SSH_CHANNEL_RPORT_LISTENER:
512 case SSH_CHANNEL_OPENING:
513 case SSH_CHANNEL_CONNECTING:
514 case SSH_CHANNEL_ZOMBIE:
515 continue;
516 case SSH_CHANNEL_LARVAL:
517 case SSH_CHANNEL_AUTH_SOCKET:
518 case SSH_CHANNEL_OPEN:
519 case SSH_CHANNEL_X11_OPEN:
520 return i;
521 case SSH_CHANNEL_INPUT_DRAINING:
522 case SSH_CHANNEL_OUTPUT_DRAINING:
523 if (!compat13)
524 fatal("cannot happen: OUT_DRAIN");
525 return i;
526 default:
527 fatal("channel_find_open: bad channel type %d", c->type);
528 /* NOTREACHED */
529 }
530 }
531 return -1;
532}
533
534
535/*
536 * Returns a message describing the currently open forwarded connections,
537 * suitable for sending to the client. The message contains crlf pairs for
538 * newlines.
539 */
540
541char *
542channel_open_message()
543{
544 Buffer buffer;
545 Channel *c;
546 char buf[1024], *cp;
547 int i;
548
549 buffer_init(&buffer);
550 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
551 buffer_append(&buffer, buf, strlen(buf));
552 for (i = 0; i < channels_alloc; i++) {
553 c = channels[i];
554 if (c == NULL)
555 continue;
556 switch (c->type) {
557 case SSH_CHANNEL_X11_LISTENER:
558 case SSH_CHANNEL_PORT_LISTENER:
559 case SSH_CHANNEL_RPORT_LISTENER:
560 case SSH_CHANNEL_CLOSED:
561 case SSH_CHANNEL_AUTH_SOCKET:
562 case SSH_CHANNEL_ZOMBIE:
563 continue;
564 case SSH_CHANNEL_LARVAL:
565 case SSH_CHANNEL_OPENING:
566 case SSH_CHANNEL_CONNECTING:
567 case SSH_CHANNEL_DYNAMIC:
568 case SSH_CHANNEL_OPEN:
569 case SSH_CHANNEL_X11_OPEN:
570 case SSH_CHANNEL_INPUT_DRAINING:
571 case SSH_CHANNEL_OUTPUT_DRAINING:
572 snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
573 c->self, c->remote_name,
574 c->type, c->remote_id,
575 c->istate, buffer_len(&c->input),
576 c->ostate, buffer_len(&c->output),
577 c->rfd, c->wfd);
578 buffer_append(&buffer, buf, strlen(buf));
579 continue;
580 default:
581 fatal("channel_open_message: bad channel type %d", c->type);
582 /* NOTREACHED */
583 }
584 }
585 buffer_append(&buffer, "\0", 1);
586 cp = xstrdup(buffer_ptr(&buffer));
587 buffer_free(&buffer);
588 return cp;
589}
590
591void
592channel_send_open(int id)
593{
594 Channel *c = channel_lookup(id);
595 if (c == NULL) {
596 log("channel_send_open: %d: bad id", id);
597 return;
598 }
599 debug("send channel open %d", id);
600 packet_start(SSH2_MSG_CHANNEL_OPEN);
601 packet_put_cstring(c->ctype);
602 packet_put_int(c->self);
603 packet_put_int(c->local_window);
604 packet_put_int(c->local_maxpacket);
605 packet_send();
606}
607
608void
609channel_request(int id, char *service, int wantconfirm)
610{
611 channel_request_start(id, service, wantconfirm);
612 packet_send();
613 debug("channel request %d: %s", id, service) ;
614}
615void
616channel_request_start(int id, char *service, int wantconfirm)
617{
618 Channel *c = channel_lookup(id);
619 if (c == NULL) {
620 log("channel_request: %d: bad id", id);
621 return;
622 }
623 packet_start(SSH2_MSG_CHANNEL_REQUEST);
624 packet_put_int(c->remote_id);
625 packet_put_cstring(service);
626 packet_put_char(wantconfirm);
627}
628void
629channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
630{
631 Channel *c = channel_lookup(id);
632 if (c == NULL) {
633 log("channel_register_callback: %d: bad id", id);
634 return;
635 }
636 c->cb_event = mtype;
637 c->cb_fn = fn;
638 c->cb_arg = arg;
639}
640void
641channel_register_cleanup(int id, channel_callback_fn *fn)
642{
643 Channel *c = channel_lookup(id);
644 if (c == NULL) {
645 log("channel_register_cleanup: %d: bad id", id);
646 return;
647 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000648 c->detach_user = fn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000649}
650void
651channel_cancel_cleanup(int id)
652{
653 Channel *c = channel_lookup(id);
654 if (c == NULL) {
655 log("channel_cancel_cleanup: %d: bad id", id);
656 return;
657 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000658 c->detach_user = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000659}
660void
661channel_register_filter(int id, channel_filter_fn *fn)
662{
663 Channel *c = channel_lookup(id);
664 if (c == NULL) {
665 log("channel_register_filter: %d: bad id", id);
666 return;
667 }
668 c->input_filter = fn;
669}
670
671void
672channel_set_fds(int id, int rfd, int wfd, int efd,
673 int extusage, int nonblock)
674{
675 Channel *c = channel_lookup(id);
676 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
677 fatal("channel_activate for non-larval channel %d.", id);
678 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
679 c->type = SSH_CHANNEL_OPEN;
680 /* XXX window size? */
681 c->local_window = c->local_window_max = c->local_maxpacket * 2;
682 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
683 packet_put_int(c->remote_id);
684 packet_put_int(c->local_window);
685 packet_send();
686}
687
Damien Miller5428f641999-11-25 11:54:57 +1100688/*
Damien Millerb38eff82000-04-01 11:09:21 +1000689 * 'channel_pre*' are called just before select() to add any bits relevant to
690 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100691 */
Damien Millerb38eff82000-04-01 11:09:21 +1000692/*
693 * 'channel_post*': perform any appropriate operations for channels which
694 * have events pending.
695 */
696typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
697chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
698chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
699
Ben Lindstrombba81212001-06-25 05:01:22 +0000700static void
Damien Millerb38eff82000-04-01 11:09:21 +1000701channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
702{
703 FD_SET(c->sock, readset);
704}
705
Ben Lindstrombba81212001-06-25 05:01:22 +0000706static void
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000707channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
708{
709 debug3("channel %d: waiting for connection", c->self);
710 FD_SET(c->sock, writeset);
711}
712
Ben Lindstrombba81212001-06-25 05:01:22 +0000713static void
Damien Millerb38eff82000-04-01 11:09:21 +1000714channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
715{
716 if (buffer_len(&c->input) < packet_get_maxsize())
717 FD_SET(c->sock, readset);
718 if (buffer_len(&c->output) > 0)
719 FD_SET(c->sock, writeset);
720}
721
Ben Lindstrombba81212001-06-25 05:01:22 +0000722static void
Damien Millerb38eff82000-04-01 11:09:21 +1000723channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
724{
725 /* test whether sockets are 'alive' for read/write */
726 if (c->istate == CHAN_INPUT_OPEN)
727 if (buffer_len(&c->input) < packet_get_maxsize())
728 FD_SET(c->sock, readset);
729 if (c->ostate == CHAN_OUTPUT_OPEN ||
730 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
731 if (buffer_len(&c->output) > 0) {
732 FD_SET(c->sock, writeset);
733 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
734 chan_obuf_empty(c);
735 }
736 }
737}
738
Ben Lindstrombba81212001-06-25 05:01:22 +0000739static void
Damien Miller33b13562000-04-04 14:38:59 +1000740channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
741{
742 if (c->istate == CHAN_INPUT_OPEN &&
743 c->remote_window > 0 &&
744 buffer_len(&c->input) < c->remote_window)
745 FD_SET(c->rfd, readset);
746 if (c->ostate == CHAN_OUTPUT_OPEN ||
747 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
748 if (buffer_len(&c->output) > 0) {
749 FD_SET(c->wfd, writeset);
750 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
751 chan_obuf_empty(c);
752 }
753 }
754 /** XXX check close conditions, too */
755 if (c->efd != -1) {
756 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
757 buffer_len(&c->extended) > 0)
758 FD_SET(c->efd, writeset);
759 else if (c->extended_usage == CHAN_EXTENDED_READ &&
760 buffer_len(&c->extended) < c->remote_window)
761 FD_SET(c->efd, readset);
762 }
763}
764
Ben Lindstrombba81212001-06-25 05:01:22 +0000765static void
Damien Millerb38eff82000-04-01 11:09:21 +1000766channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
767{
768 if (buffer_len(&c->input) == 0) {
769 packet_start(SSH_MSG_CHANNEL_CLOSE);
770 packet_put_int(c->remote_id);
771 packet_send();
772 c->type = SSH_CHANNEL_CLOSED;
Ben Lindstromc486d882001-04-11 16:08:34 +0000773 debug("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000774 }
775}
776
Ben Lindstrombba81212001-06-25 05:01:22 +0000777static void
Damien Millerb38eff82000-04-01 11:09:21 +1000778channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
779{
780 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000781 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000782 else
Damien Millerb38eff82000-04-01 11:09:21 +1000783 FD_SET(c->sock, writeset);
784}
785
786/*
787 * This is a special state for X11 authentication spoofing. An opened X11
788 * connection (when authentication spoofing is being done) remains in this
789 * state until the first packet has been completely read. The authentication
790 * data in that packet is then substituted by the real data if it matches the
791 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000792 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000793 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000794 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000795static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000796x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000797{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000798 u_char *ucp;
799 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000800
801 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000802 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000803 return 0;
804
805 /* Parse the lengths of variable-length fields. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000806 ucp = (u_char *) buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000807 if (ucp[0] == 0x42) { /* Byte order MSB first. */
808 proto_len = 256 * ucp[6] + ucp[7];
809 data_len = 256 * ucp[8] + ucp[9];
810 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
811 proto_len = ucp[6] + 256 * ucp[7];
812 data_len = ucp[8] + 256 * ucp[9];
813 } else {
814 debug("Initial X11 packet contains bad byte order byte: 0x%x",
815 ucp[0]);
816 return -1;
817 }
818
819 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000820 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000821 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
822 return 0;
823
824 /* Check if authentication protocol matches. */
825 if (proto_len != strlen(x11_saved_proto) ||
826 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
827 debug("X11 connection uses different authentication protocol.");
828 return -1;
829 }
830 /* Check if authentication data matches our fake data. */
831 if (data_len != x11_fake_data_len ||
832 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
833 x11_fake_data, x11_fake_data_len) != 0) {
834 debug("X11 auth data does not match fake data.");
835 return -1;
836 }
837 /* Check fake data length */
838 if (x11_fake_data_len != x11_saved_data_len) {
839 error("X11 fake_data_len %d != saved_data_len %d",
840 x11_fake_data_len, x11_saved_data_len);
841 return -1;
842 }
843 /*
844 * Received authentication protocol and data match
845 * our fake data. Substitute the fake data with real
846 * data.
847 */
848 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
849 x11_saved_data, x11_saved_data_len);
850 return 1;
851}
852
Ben Lindstrombba81212001-06-25 05:01:22 +0000853static void
Damien Millerb38eff82000-04-01 11:09:21 +1000854channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
855{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000856 int ret = x11_open_helper(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000857 if (ret == 1) {
858 /* Start normal processing for the channel. */
859 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000860 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000861 } else if (ret == -1) {
862 /*
863 * We have received an X11 connection that has bad
864 * authentication information.
865 */
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000866 log("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000867 buffer_clear(&c->input);
868 buffer_clear(&c->output);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000869 channel_close_fd(&c->sock);
Damien Millerb38eff82000-04-01 11:09:21 +1000870 c->sock = -1;
871 c->type = SSH_CHANNEL_CLOSED;
872 packet_start(SSH_MSG_CHANNEL_CLOSE);
873 packet_put_int(c->remote_id);
874 packet_send();
875 }
876}
877
Ben Lindstrombba81212001-06-25 05:01:22 +0000878static void
Damien Millerbd483e72000-04-30 10:00:53 +1000879channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000880{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000881 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000882
883 /* c->force_drain = 1; */
884
Damien Millerb38eff82000-04-01 11:09:21 +1000885 if (ret == 1) {
886 c->type = SSH_CHANNEL_OPEN;
Damien Miller30c3d422000-05-09 11:02:59 +1000887 if (compat20)
888 channel_pre_open_20(c, readset, writeset);
889 else
890 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000891 } else if (ret == -1) {
892 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerbd483e72000-04-30 10:00:53 +1000893 chan_read_failed(c); /** force close? */
Damien Millerb38eff82000-04-01 11:09:21 +1000894 chan_write_failed(c);
895 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
896 }
897}
898
Ben Lindstromb3921512001-04-11 15:57:50 +0000899/* try to decode a socks4 header */
Ben Lindstrombba81212001-06-25 05:01:22 +0000900static int
Ben Lindstromb3921512001-04-11 15:57:50 +0000901channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000902{
903 u_char *p, *host;
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000904 int len, have, i, found;
Ben Lindstromb3921512001-04-11 15:57:50 +0000905 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000906 struct {
907 u_int8_t version;
908 u_int8_t command;
909 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000910 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000911 } s4_req, s4_rsp;
912
Ben Lindstromb3921512001-04-11 15:57:50 +0000913 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000914
915 have = buffer_len(&c->input);
916 len = sizeof(s4_req);
917 if (have < len)
918 return 0;
919 p = buffer_ptr(&c->input);
920 for (found = 0, i = len; i < have; i++) {
921 if (p[i] == '\0') {
922 found = 1;
923 break;
924 }
925 if (i > 1024) {
926 /* the peer is probably sending garbage */
927 debug("channel %d: decode socks4: too long",
928 c->self);
929 return -1;
930 }
931 }
932 if (!found)
933 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000934 buffer_get(&c->input, (char *)&s4_req.version, 1);
935 buffer_get(&c->input, (char *)&s4_req.command, 1);
936 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000937 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000938 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000939 p = buffer_ptr(&c->input);
940 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000941 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000942 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000943 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000944 c->self, len, have);
945 strlcpy(username, p, sizeof(username));
946 buffer_consume(&c->input, len);
947 buffer_consume(&c->input, 1); /* trailing '\0' */
948
Ben Lindstromb3921512001-04-11 15:57:50 +0000949 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000950 strlcpy(c->path, host, sizeof(c->path));
951 c->host_port = ntohs(s4_req.dest_port);
952
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000953 debug("channel %d: dynamic request: socks4 host %s port %u command %u",
954 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000955
Ben Lindstromb3921512001-04-11 15:57:50 +0000956 if (s4_req.command != 1) {
957 debug("channel %d: cannot handle: socks4 cn %d",
958 c->self, s4_req.command);
959 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000960 }
Ben Lindstromb3921512001-04-11 15:57:50 +0000961 s4_rsp.version = 0; /* vn: 0 for reply */
962 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000963 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +0000964 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000965 buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +0000966 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000967}
968
Ben Lindstromb3921512001-04-11 15:57:50 +0000969/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +0000970static void
Ben Lindstromb3921512001-04-11 15:57:50 +0000971channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
972{
973 u_char *p;
974 int have, ret;
975
976 have = buffer_len(&c->input);
977
978 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +0000979 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +0000980 /* check if the fixed size part of the packet is in buffer. */
981 if (have < 4) {
982 /* need more */
983 FD_SET(c->sock, readset);
984 return;
985 }
986 /* try to guess the protocol */
987 p = buffer_ptr(&c->input);
988 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000989 case 0x04:
990 ret = channel_decode_socks4(c, readset, writeset);
991 break;
Ben Lindstromb3921512001-04-11 15:57:50 +0000992 default:
993 ret = -1;
994 break;
995 }
996 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000997 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +0000998 } else if (ret == 0) {
999 debug2("channel %d: pre_dynamic: need more", c->self);
1000 /* need more */
1001 FD_SET(c->sock, readset);
1002 } else {
1003 /* switch to the next state */
1004 c->type = SSH_CHANNEL_OPENING;
1005 port_open_helper(c, "direct-tcpip");
1006 }
1007}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001008
Damien Millerb38eff82000-04-01 11:09:21 +10001009/* This is our fake X11 server socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +00001010static void
Damien Millerb38eff82000-04-01 11:09:21 +10001011channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
1012{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001013 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001014 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001015 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001016 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001017 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001018 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001019
1020 if (FD_ISSET(c->sock, readset)) {
1021 debug("X11 connection requested.");
1022 addrlen = sizeof(addr);
1023 newsock = accept(c->sock, &addr, &addrlen);
1024 if (newsock < 0) {
1025 error("accept: %.100s", strerror(errno));
1026 return;
1027 }
Damien Millerd83ff352001-01-30 09:19:34 +11001028 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001029 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001030 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001031 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001032
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001033 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001034 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1035 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +11001036 0, xstrdup(buf), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001037 if (nc == NULL) {
1038 close(newsock);
1039 xfree(remote_ipaddr);
1040 return;
1041 }
Damien Millerbd483e72000-04-30 10:00:53 +10001042 if (compat20) {
1043 packet_start(SSH2_MSG_CHANNEL_OPEN);
1044 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001045 packet_put_int(nc->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001046 packet_put_int(c->local_window_max);
1047 packet_put_int(c->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +11001048 /* originator ipaddr and port */
1049 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +10001050 if (datafellows & SSH_BUG_X11FWD) {
1051 debug("ssh2 x11 bug compat mode");
1052 } else {
1053 packet_put_int(remote_port);
1054 }
Damien Millerbd483e72000-04-30 10:00:53 +10001055 packet_send();
1056 } else {
1057 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001058 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001059 if (packet_get_protocol_flags() &
1060 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +00001061 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +10001062 packet_send();
1063 }
Damien Millerd83ff352001-01-30 09:19:34 +11001064 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001065 }
1066}
1067
Ben Lindstrombba81212001-06-25 05:01:22 +00001068static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001069port_open_helper(Channel *c, char *rtype)
1070{
1071 int direct;
1072 char buf[1024];
1073 char *remote_ipaddr = get_peer_ipaddr(c->sock);
1074 u_short remote_port = get_peer_port(c->sock);
1075
1076 direct = (strcmp(rtype, "direct-tcpip") == 0);
1077
1078 snprintf(buf, sizeof buf,
1079 "%s: listening port %d for %.100s port %d, "
1080 "connect from %.200s port %d",
1081 rtype, c->listening_port, c->path, c->host_port,
1082 remote_ipaddr, remote_port);
1083
1084 xfree(c->remote_name);
1085 c->remote_name = xstrdup(buf);
1086
1087 if (compat20) {
1088 packet_start(SSH2_MSG_CHANNEL_OPEN);
1089 packet_put_cstring(rtype);
1090 packet_put_int(c->self);
1091 packet_put_int(c->local_window_max);
1092 packet_put_int(c->local_maxpacket);
1093 if (direct) {
1094 /* target host, port */
1095 packet_put_cstring(c->path);
1096 packet_put_int(c->host_port);
1097 } else {
1098 /* listen address, port */
1099 packet_put_cstring(c->path);
1100 packet_put_int(c->listening_port);
1101 }
1102 /* originator host and port */
1103 packet_put_cstring(remote_ipaddr);
1104 packet_put_int(remote_port);
1105 packet_send();
1106 } else {
1107 packet_start(SSH_MSG_PORT_OPEN);
1108 packet_put_int(c->self);
1109 packet_put_cstring(c->path);
1110 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001111 if (packet_get_protocol_flags() &
1112 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001113 packet_put_cstring(c->remote_name);
1114 packet_send();
1115 }
1116 xfree(remote_ipaddr);
1117}
1118
Damien Millerb38eff82000-04-01 11:09:21 +10001119/*
1120 * This socket is listening for connections to a forwarded TCP/IP port.
1121 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001122static void
Damien Millerb38eff82000-04-01 11:09:21 +10001123channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
1124{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001125 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001126 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001127 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001128 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001129 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001130
Damien Millerb38eff82000-04-01 11:09:21 +10001131 if (FD_ISSET(c->sock, readset)) {
1132 debug("Connection to port %d forwarding "
1133 "to %.100s port %d requested.",
1134 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001135
1136 rtype = (c->type == SSH_CHANNEL_RPORT_LISTENER) ?
1137 "forwarded-tcpip" : "direct-tcpip";
Ben Lindstrom6d618462001-05-10 23:24:49 +00001138 nextstate = (c->host_port == 0 &&
1139 c->type != SSH_CHANNEL_RPORT_LISTENER) ?
1140 SSH_CHANNEL_DYNAMIC : SSH_CHANNEL_OPENING;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001141
Damien Millerb38eff82000-04-01 11:09:21 +10001142 addrlen = sizeof(addr);
1143 newsock = accept(c->sock, &addr, &addrlen);
1144 if (newsock < 0) {
1145 error("accept: %.100s", strerror(errno));
1146 return;
1147 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001148 nc = channel_new(rtype,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001149 nextstate, newsock, newsock, -1,
Damien Millerb38eff82000-04-01 11:09:21 +10001150 c->local_window_max, c->local_maxpacket,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001151 0, xstrdup(rtype), 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001152 if (nc == NULL) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001153 error("channel_post_port_listener: no new channel:");
1154 close(newsock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001155 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001156 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001157 nc->listening_port = c->listening_port;
1158 nc->host_port = c->host_port;
1159 strlcpy(nc->path, c->path, sizeof(nc->path));
1160
1161 if (nextstate != SSH_CHANNEL_DYNAMIC)
1162 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001163 }
1164}
1165
1166/*
1167 * This is the authentication agent socket listening for connections from
1168 * clients.
1169 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001170static void
Damien Millerb38eff82000-04-01 11:09:21 +10001171channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
1172{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001173 Channel *nc;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001174 char *name;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001175 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001176 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001177 socklen_t addrlen;
1178
1179 if (FD_ISSET(c->sock, readset)) {
1180 addrlen = sizeof(addr);
1181 newsock = accept(c->sock, &addr, &addrlen);
1182 if (newsock < 0) {
1183 error("accept from auth socket: %.100s", strerror(errno));
1184 return;
1185 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001186 name = xstrdup("accepted auth socket");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001187 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001188 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1189 c->local_window_max, c->local_maxpacket,
Ben Lindstrome9c99912001-06-09 00:41:05 +00001190 0, name, 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001191 if (nc == NULL) {
1192 error("channel_post_auth_listener: channel_new failed");
Ben Lindstrome9c99912001-06-09 00:41:05 +00001193 xfree(name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001194 close(newsock);
1195 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001196 if (compat20) {
1197 packet_start(SSH2_MSG_CHANNEL_OPEN);
1198 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001199 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001200 packet_put_int(c->local_window_max);
1201 packet_put_int(c->local_maxpacket);
1202 } else {
1203 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001204 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001205 }
Damien Millerb38eff82000-04-01 11:09:21 +10001206 packet_send();
1207 }
1208}
1209
Ben Lindstrombba81212001-06-25 05:01:22 +00001210static void
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001211channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
1212{
Ben Lindstrom69128662001-05-08 20:07:39 +00001213 int err = 0;
Ben Lindstrom11180952001-07-04 05:13:35 +00001214 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001215
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001216 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001217 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err,
1218 &sz) < 0) {
1219 err = errno;
1220 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001221 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001222 if (err == 0) {
1223 debug("channel %d: connected", c->self);
1224 c->type = SSH_CHANNEL_OPEN;
1225 if (compat20) {
1226 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1227 packet_put_int(c->remote_id);
1228 packet_put_int(c->self);
1229 packet_put_int(c->local_window);
1230 packet_put_int(c->local_maxpacket);
1231 } else {
1232 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1233 packet_put_int(c->remote_id);
1234 packet_put_int(c->self);
1235 }
1236 } else {
1237 debug("channel %d: not connected: %s",
1238 c->self, strerror(err));
1239 if (compat20) {
1240 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1241 packet_put_int(c->remote_id);
1242 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1243 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1244 packet_put_cstring(strerror(err));
1245 packet_put_cstring("");
1246 }
1247 } else {
1248 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1249 packet_put_int(c->remote_id);
1250 }
1251 chan_mark_dead(c);
1252 }
1253 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001254 }
1255}
1256
Ben Lindstrombba81212001-06-25 05:01:22 +00001257static int
Damien Millerb38eff82000-04-01 11:09:21 +10001258channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
1259{
1260 char buf[16*1024];
1261 int len;
1262
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001263 if (c->rfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001264 FD_ISSET(c->rfd, readset)) {
1265 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +10001266 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1267 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001268 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +10001269 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001270 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001271 if (c->type != SSH_CHANNEL_OPEN) {
1272 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001273 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001274 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001275 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001276 buffer_consume(&c->output, buffer_len(&c->output));
1277 c->type = SSH_CHANNEL_INPUT_DRAINING;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001278 debug("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001279 } else {
1280 chan_read_failed(c);
1281 }
1282 return -1;
1283 }
Damien Millerad833b32000-08-23 10:46:23 +10001284 if(c->input_filter != NULL) {
1285 if (c->input_filter(c, buf, len) == -1) {
Ben Lindstromc486d882001-04-11 16:08:34 +00001286 debug("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001287 chan_read_failed(c);
1288 }
1289 } else {
1290 buffer_append(&c->input, buf, len);
1291 }
Damien Millerb38eff82000-04-01 11:09:21 +10001292 }
1293 return 1;
1294}
Ben Lindstrombba81212001-06-25 05:01:22 +00001295static int
Damien Millerb38eff82000-04-01 11:09:21 +10001296channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
1297{
Ben Lindstrome229b252001-03-05 06:28:06 +00001298 struct termios tio;
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001299 u_char *data;
1300 u_int dlen;
Damien Millerb38eff82000-04-01 11:09:21 +10001301 int len;
1302
1303 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001304 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001305 FD_ISSET(c->wfd, writeset) &&
1306 buffer_len(&c->output) > 0) {
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001307 data = buffer_ptr(&c->output);
1308 dlen = buffer_len(&c->output);
1309 len = write(c->wfd, data, dlen);
Damien Miller6f83b8e2000-05-02 09:23:45 +10001310 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1311 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001312 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001313 if (c->type != SSH_CHANNEL_OPEN) {
1314 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001315 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001316 return -1;
1317 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001318 buffer_consume(&c->output, buffer_len(&c->output));
Ben Lindstrome9c99912001-06-09 00:41:05 +00001319 debug("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001320 c->type = SSH_CHANNEL_INPUT_DRAINING;
1321 } else {
1322 chan_write_failed(c);
1323 }
1324 return -1;
1325 }
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001326 if (compat20 && c->isatty && dlen >= 1 && data[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001327 if (tcgetattr(c->wfd, &tio) == 0 &&
1328 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1329 /*
1330 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001331 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001332 * size of a SSH2_MSG_CHANNEL_DATA message
1333 * (4 byte channel id + data)
Damien Miller79438cc2001-02-16 12:34:57 +11001334 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001335 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001336 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001337 }
1338 }
Damien Millerb38eff82000-04-01 11:09:21 +10001339 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001340 if (compat20 && len > 0) {
1341 c->local_consumed += len;
1342 }
1343 }
1344 return 1;
1345}
Ben Lindstrombba81212001-06-25 05:01:22 +00001346static int
Damien Miller33b13562000-04-04 14:38:59 +10001347channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
1348{
1349 char buf[16*1024];
1350 int len;
1351
Damien Miller1383bd82000-04-06 12:32:37 +10001352/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001353 if (c->efd != -1) {
1354 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1355 FD_ISSET(c->efd, writeset) &&
1356 buffer_len(&c->extended) > 0) {
1357 len = write(c->efd, buffer_ptr(&c->extended),
1358 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001359 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001360 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001361 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1362 return 1;
1363 if (len <= 0) {
1364 debug2("channel %d: closing write-efd %d",
1365 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001366 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001367 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001368 buffer_consume(&c->extended, len);
1369 c->local_consumed += len;
1370 }
1371 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1372 FD_ISSET(c->efd, readset)) {
1373 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001374 debug2("channel %d: read %d from efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001375 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001376 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1377 return 1;
1378 if (len <= 0) {
1379 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001380 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001381 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001382 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001383 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001384 }
Damien Miller33b13562000-04-04 14:38:59 +10001385 }
1386 }
1387 return 1;
1388}
Ben Lindstrombba81212001-06-25 05:01:22 +00001389static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001390channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001391{
Ben Lindstromb3921512001-04-11 15:57:50 +00001392 if (c->type == SSH_CHANNEL_OPEN &&
1393 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001394 c->local_window < c->local_window_max/2 &&
1395 c->local_consumed > 0) {
1396 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1397 packet_put_int(c->remote_id);
1398 packet_put_int(c->local_consumed);
1399 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001400 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001401 c->self, c->local_window,
1402 c->local_consumed);
1403 c->local_window += c->local_consumed;
1404 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001405 }
1406 return 1;
1407}
1408
Ben Lindstrombba81212001-06-25 05:01:22 +00001409static void
Damien Millerb38eff82000-04-01 11:09:21 +10001410channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
1411{
1412 channel_handle_rfd(c, readset, writeset);
1413 channel_handle_wfd(c, readset, writeset);
1414}
1415
Ben Lindstrombba81212001-06-25 05:01:22 +00001416static void
Damien Miller33b13562000-04-04 14:38:59 +10001417channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
1418{
1419 channel_handle_rfd(c, readset, writeset);
1420 channel_handle_wfd(c, readset, writeset);
1421 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001422
1423 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001424}
1425
Ben Lindstrombba81212001-06-25 05:01:22 +00001426static void
Damien Millerb38eff82000-04-01 11:09:21 +10001427channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1428{
1429 int len;
1430 /* Send buffered output data to the socket. */
1431 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1432 len = write(c->sock, buffer_ptr(&c->output),
1433 buffer_len(&c->output));
1434 if (len <= 0)
1435 buffer_consume(&c->output, buffer_len(&c->output));
1436 else
1437 buffer_consume(&c->output, len);
1438 }
1439}
1440
Ben Lindstrombba81212001-06-25 05:01:22 +00001441static void
Damien Miller33b13562000-04-04 14:38:59 +10001442channel_handler_init_20(void)
1443{
1444 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +10001445 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001446 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001447 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001448 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001449 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001450 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001451 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001452
1453 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
1454 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001455 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001456 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001457 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001458 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001459 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_2;
Damien Miller33b13562000-04-04 14:38:59 +10001460}
1461
Ben Lindstrombba81212001-06-25 05:01:22 +00001462static void
Damien Millerb38eff82000-04-01 11:09:21 +10001463channel_handler_init_13(void)
1464{
1465 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1466 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1467 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1468 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1469 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1470 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1471 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001472 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001473 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001474
1475 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
1476 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1477 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1478 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1479 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001480 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001481 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001482}
1483
Ben Lindstrombba81212001-06-25 05:01:22 +00001484static void
Damien Millerb38eff82000-04-01 11:09:21 +10001485channel_handler_init_15(void)
1486{
1487 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +10001488 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001489 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1490 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1491 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
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_X11_LISTENER] = &channel_post_x11_listener;
1496 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1497 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1498 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001499 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001500 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001501}
1502
Ben Lindstrombba81212001-06-25 05:01:22 +00001503static void
Damien Millerb38eff82000-04-01 11:09:21 +10001504channel_handler_init(void)
1505{
1506 int i;
1507 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1508 channel_pre[i] = NULL;
1509 channel_post[i] = NULL;
1510 }
Damien Miller33b13562000-04-04 14:38:59 +10001511 if (compat20)
1512 channel_handler_init_20();
1513 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001514 channel_handler_init_13();
1515 else
1516 channel_handler_init_15();
1517}
1518
Ben Lindstrombba81212001-06-25 05:01:22 +00001519static void
Damien Millerb38eff82000-04-01 11:09:21 +10001520channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1521{
1522 static int did_init = 0;
1523 int i;
1524 Channel *c;
1525
1526 if (!did_init) {
1527 channel_handler_init();
1528 did_init = 1;
1529 }
1530 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001531 c = channels[i];
1532 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001533 continue;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001534 if (ftab[c->type] != NULL)
1535 (*ftab[c->type])(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001536 if (chan_is_dead(c)) {
1537 /*
1538 * we have to remove the fd's from the select mask
1539 * before the channels are free'd and the fd's are
1540 * closed
1541 */
1542 if (c->wfd != -1)
1543 FD_CLR(c->wfd, writeset);
1544 if (c->rfd != -1)
1545 FD_CLR(c->rfd, readset);
1546 if (c->efd != -1) {
1547 if (c->extended_usage == CHAN_EXTENDED_READ)
1548 FD_CLR(c->efd, readset);
1549 if (c->extended_usage == CHAN_EXTENDED_WRITE)
1550 FD_CLR(c->efd, writeset);
1551 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001552 channel_free(c);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001553 }
Damien Millerb38eff82000-04-01 11:09:21 +10001554 }
1555}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001556
Ben Lindstrome9c99912001-06-09 00:41:05 +00001557/*
1558 * Allocate/update select bitmasks and add any bits relevant to channels in
1559 * select bitmasks.
1560 */
Damien Miller4af51302000-04-16 11:18:38 +10001561void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001562channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001563 int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001564{
Damien Miller5e953212001-01-30 09:14:00 +11001565 int n;
1566 u_int sz;
1567
1568 n = MAX(*maxfdp, channel_max_fd);
1569
1570 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001571 /* perhaps check sz < nalloc/2 and shrink? */
1572 if (*readsetp == NULL || sz > *nallocp) {
1573 *readsetp = xrealloc(*readsetp, sz);
1574 *writesetp = xrealloc(*writesetp, sz);
1575 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11001576 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001577 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11001578 memset(*readsetp, 0, sz);
1579 memset(*writesetp, 0, sz);
1580
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001581 if (!rekeying)
1582 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001583}
1584
Ben Lindstrome9c99912001-06-09 00:41:05 +00001585/*
1586 * After select, perform any appropriate operations for channels which have
1587 * events pending.
1588 */
Damien Miller4af51302000-04-16 11:18:38 +10001589void
Damien Miller95def091999-11-25 00:26:21 +11001590channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001591{
Damien Millerb38eff82000-04-01 11:09:21 +10001592 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001593}
1594
Ben Lindstrome9c99912001-06-09 00:41:05 +00001595
Damien Miller5e953212001-01-30 09:14:00 +11001596/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001597
Damien Miller4af51302000-04-16 11:18:38 +10001598void
Damien Miller95def091999-11-25 00:26:21 +11001599channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001600{
Damien Miller95def091999-11-25 00:26:21 +11001601 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001602 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001603
Damien Miller95def091999-11-25 00:26:21 +11001604 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001605 c = channels[i];
1606 if (c == NULL)
1607 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001608
Ben Lindstrome9c99912001-06-09 00:41:05 +00001609 /*
1610 * We are only interested in channels that can have buffered
1611 * incoming data.
1612 */
Damien Miller34132e52000-01-14 15:45:46 +11001613 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001614 if (c->type != SSH_CHANNEL_OPEN &&
1615 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001616 continue;
1617 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001618 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001619 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001620 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001621 if (compat20 &&
1622 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001623 /* XXX is this true? */
Ben Lindstromb1131e92001-03-05 07:27:13 +00001624 debug2("channel %d: no data after CLOSE", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001625 continue;
1626 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001627
Damien Miller95def091999-11-25 00:26:21 +11001628 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001629 if ((c->istate == CHAN_INPUT_OPEN ||
1630 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1631 (len = buffer_len(&c->input)) > 0) {
Ben Lindstrome9c99912001-06-09 00:41:05 +00001632 /*
1633 * Send some data for the other side over the secure
1634 * connection.
1635 */
Damien Miller33b13562000-04-04 14:38:59 +10001636 if (compat20) {
1637 if (len > c->remote_window)
1638 len = c->remote_window;
1639 if (len > c->remote_maxpacket)
1640 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001641 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001642 if (packet_is_interactive()) {
1643 if (len > 1024)
1644 len = 512;
1645 } else {
1646 /* Keep the packets at reasonable size. */
1647 if (len > packet_get_maxsize()/2)
1648 len = packet_get_maxsize()/2;
1649 }
Damien Miller95def091999-11-25 00:26:21 +11001650 }
Damien Millerb38eff82000-04-01 11:09:21 +10001651 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001652 packet_start(compat20 ?
1653 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001654 packet_put_int(c->remote_id);
1655 packet_put_string(buffer_ptr(&c->input), len);
1656 packet_send();
1657 buffer_consume(&c->input, len);
1658 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001659 }
1660 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001661 if (compat13)
1662 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001663 /*
1664 * input-buffer is empty and read-socket shutdown:
1665 * tell peer, that we will not send more data: send IEOF
1666 */
Damien Millerb38eff82000-04-01 11:09:21 +10001667 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001668 }
Damien Miller33b13562000-04-04 14:38:59 +10001669 /* Send extended data, i.e. stderr */
1670 if (compat20 &&
1671 c->remote_window > 0 &&
1672 (len = buffer_len(&c->extended)) > 0 &&
1673 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001674 debug2("channel %d: rwin %d elen %d euse %d",
1675 c->self, c->remote_window, buffer_len(&c->extended),
1676 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001677 if (len > c->remote_window)
1678 len = c->remote_window;
1679 if (len > c->remote_maxpacket)
1680 len = c->remote_maxpacket;
1681 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1682 packet_put_int(c->remote_id);
1683 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1684 packet_put_string(buffer_ptr(&c->extended), len);
1685 packet_send();
1686 buffer_consume(&c->extended, len);
1687 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001688 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001689 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001690 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001691}
1692
Ben Lindstrome9c99912001-06-09 00:41:05 +00001693
1694/* -- protocol input */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001695
Damien Miller4af51302000-04-16 11:18:38 +10001696void
Damien Miller62cee002000-09-23 17:15:56 +11001697channel_input_data(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001698{
Damien Miller34132e52000-01-14 15:45:46 +11001699 int id;
Damien Miller95def091999-11-25 00:26:21 +11001700 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001701 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001702 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001703
Damien Miller95def091999-11-25 00:26:21 +11001704 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001705 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001706 c = channel_lookup(id);
1707 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001708 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001709
Damien Miller95def091999-11-25 00:26:21 +11001710 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001711 if (c->type != SSH_CHANNEL_OPEN &&
1712 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001713 return;
1714
1715 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001716 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001717 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001718
Damien Miller95def091999-11-25 00:26:21 +11001719 /* Get the data. */
1720 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001721 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001722
Damien Miller33b13562000-04-04 14:38:59 +10001723 if (compat20){
1724 if (data_len > c->local_maxpacket) {
1725 log("channel %d: rcvd big packet %d, maxpack %d",
1726 c->self, data_len, c->local_maxpacket);
1727 }
1728 if (data_len > c->local_window) {
1729 log("channel %d: rcvd too much data %d, win %d",
1730 c->self, data_len, c->local_window);
1731 xfree(data);
1732 return;
1733 }
1734 c->local_window -= data_len;
1735 }else{
1736 packet_integrity_check(plen, 4 + 4 + data_len, type);
1737 }
Damien Millerb38eff82000-04-01 11:09:21 +10001738 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001739 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001740}
Ben Lindstrome9c99912001-06-09 00:41:05 +00001741
Damien Miller4af51302000-04-16 11:18:38 +10001742void
Damien Miller62cee002000-09-23 17:15:56 +11001743channel_input_extended_data(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001744{
1745 int id;
1746 int tcode;
1747 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001748 u_int data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001749 Channel *c;
1750
1751 /* Get the channel number and verify it. */
1752 id = packet_get_int();
1753 c = channel_lookup(id);
1754
1755 if (c == NULL)
1756 packet_disconnect("Received extended_data for bad channel %d.", id);
1757 if (c->type != SSH_CHANNEL_OPEN) {
1758 log("channel %d: ext data for non open", id);
1759 return;
1760 }
1761 tcode = packet_get_int();
1762 if (c->efd == -1 ||
1763 c->extended_usage != CHAN_EXTENDED_WRITE ||
1764 tcode != SSH2_EXTENDED_DATA_STDERR) {
1765 log("channel %d: bad ext data", c->self);
1766 return;
1767 }
1768 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001769 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001770 if (data_len > c->local_window) {
1771 log("channel %d: rcvd too much extended_data %d, win %d",
1772 c->self, data_len, c->local_window);
1773 xfree(data);
1774 return;
1775 }
Damien Millerd3444942000-09-30 14:20:03 +11001776 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001777 c->local_window -= data_len;
1778 buffer_append(&c->extended, data, data_len);
1779 xfree(data);
1780}
1781
Damien Miller4af51302000-04-16 11:18:38 +10001782void
Damien Miller62cee002000-09-23 17:15:56 +11001783channel_input_ieof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001784{
1785 int id;
1786 Channel *c;
1787
1788 packet_integrity_check(plen, 4, type);
1789
1790 id = packet_get_int();
1791 c = channel_lookup(id);
1792 if (c == NULL)
1793 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1794 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001795
1796 /* XXX force input close */
1797 if (c->force_drain) {
1798 debug("channel %d: FORCE input drain", c->self);
1799 c->istate = CHAN_INPUT_WAIT_DRAIN;
1800 }
1801
Damien Millerb38eff82000-04-01 11:09:21 +10001802}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001803
Damien Miller4af51302000-04-16 11:18:38 +10001804void
Damien Miller62cee002000-09-23 17:15:56 +11001805channel_input_close(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001806{
Damien Millerb38eff82000-04-01 11:09:21 +10001807 int id;
1808 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001809
Damien Millerb38eff82000-04-01 11:09:21 +10001810 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001811
Damien Millerb38eff82000-04-01 11:09:21 +10001812 id = packet_get_int();
1813 c = channel_lookup(id);
1814 if (c == NULL)
1815 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001816
1817 /*
1818 * Send a confirmation that we have closed the channel and no more
1819 * data is coming for it.
1820 */
Damien Miller95def091999-11-25 00:26:21 +11001821 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001822 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001823 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001824
Damien Miller5428f641999-11-25 11:54:57 +11001825 /*
1826 * If the channel is in closed state, we have sent a close request,
1827 * and the other side will eventually respond with a confirmation.
1828 * Thus, we cannot free the channel here, because then there would be
1829 * no-one to receive the confirmation. The channel gets freed when
1830 * the confirmation arrives.
1831 */
Damien Millerb38eff82000-04-01 11:09:21 +10001832 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001833 /*
1834 * Not a closed channel - mark it as draining, which will
1835 * cause it to be freed later.
1836 */
Damien Millerb38eff82000-04-01 11:09:21 +10001837 buffer_consume(&c->input, buffer_len(&c->input));
1838 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001839 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001840}
1841
Damien Millerb38eff82000-04-01 11:09:21 +10001842/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001843void
Damien Miller62cee002000-09-23 17:15:56 +11001844channel_input_oclose(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001845{
Damien Millerb38eff82000-04-01 11:09:21 +10001846 int id = packet_get_int();
1847 Channel *c = channel_lookup(id);
1848 packet_integrity_check(plen, 4, type);
1849 if (c == NULL)
1850 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1851 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001852}
1853
Damien Miller4af51302000-04-16 11:18:38 +10001854void
Damien Miller62cee002000-09-23 17:15:56 +11001855channel_input_close_confirmation(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001856{
1857 int id = packet_get_int();
1858 Channel *c = channel_lookup(id);
1859
Damien Miller4af51302000-04-16 11:18:38 +10001860 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001861 if (c == NULL)
1862 packet_disconnect("Received close confirmation for "
1863 "out-of-range channel %d.", id);
1864 if (c->type != SSH_CHANNEL_CLOSED)
1865 packet_disconnect("Received close confirmation for "
1866 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001867 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001868}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001869
Damien Miller4af51302000-04-16 11:18:38 +10001870void
Damien Miller62cee002000-09-23 17:15:56 +11001871channel_input_open_confirmation(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001872{
Damien Millerb38eff82000-04-01 11:09:21 +10001873 int id, remote_id;
1874 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001875
Damien Miller33b13562000-04-04 14:38:59 +10001876 if (!compat20)
1877 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001878
Damien Millerb38eff82000-04-01 11:09:21 +10001879 id = packet_get_int();
1880 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001881
Damien Millerb38eff82000-04-01 11:09:21 +10001882 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1883 packet_disconnect("Received open confirmation for "
1884 "non-opening channel %d.", id);
1885 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001886 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001887 c->remote_id = remote_id;
1888 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001889
1890 if (compat20) {
1891 c->remote_window = packet_get_int();
1892 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001893 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001894 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001895 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001896 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001897 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001898 }
1899 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1900 c->remote_window, c->remote_maxpacket);
1901 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001902}
1903
Ben Lindstrombba81212001-06-25 05:01:22 +00001904static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00001905reason2txt(int reason)
1906{
1907 switch(reason) {
1908 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
1909 return "administratively prohibited";
1910 case SSH2_OPEN_CONNECT_FAILED:
1911 return "connect failed";
1912 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
1913 return "unknown channel type";
1914 case SSH2_OPEN_RESOURCE_SHORTAGE:
1915 return "resource shortage";
1916 }
Ben Lindstrome2595442001-06-05 20:01:39 +00001917 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00001918}
1919
Damien Miller4af51302000-04-16 11:18:38 +10001920void
Damien Miller62cee002000-09-23 17:15:56 +11001921channel_input_open_failure(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001922{
Kevin Steves12057502001-02-05 14:54:34 +00001923 int id, reason;
1924 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001925 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001926
Damien Miller33b13562000-04-04 14:38:59 +10001927 if (!compat20)
1928 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001929
1930 id = packet_get_int();
1931 c = channel_lookup(id);
1932
1933 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1934 packet_disconnect("Received open failure for "
1935 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001936 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00001937 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00001938 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00001939 msg = packet_get_string(NULL);
1940 lang = packet_get_string(NULL);
1941 }
Damien Miller4af51302000-04-16 11:18:38 +10001942 packet_done();
Ben Lindstrom69128662001-05-08 20:07:39 +00001943 log("channel %d: open failed: %s%s%s", id,
1944 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00001945 if (msg != NULL)
1946 xfree(msg);
1947 if (lang != NULL)
1948 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001949 }
Damien Miller95def091999-11-25 00:26:21 +11001950 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001951 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001952}
1953
Damien Miller33b13562000-04-04 14:38:59 +10001954void
Damien Miller62cee002000-09-23 17:15:56 +11001955channel_input_channel_request(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001956{
1957 int id;
1958 Channel *c;
1959
1960 id = packet_get_int();
1961 c = channel_lookup(id);
1962
1963 if (c == NULL ||
1964 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1965 packet_disconnect("Received request for "
1966 "non-open channel %d.", id);
1967 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001968 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001969 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001970 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001971 } else {
1972 char *service = packet_get_string(NULL);
Ben Lindstromcc74df72001-03-05 06:20:14 +00001973 debug("channel %d: rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001974 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001975 xfree(service);
1976 }
1977}
1978
Damien Miller4af51302000-04-16 11:18:38 +10001979void
Damien Miller62cee002000-09-23 17:15:56 +11001980channel_input_window_adjust(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001981{
1982 Channel *c;
1983 int id, adjust;
1984
1985 if (!compat20)
1986 return;
1987
1988 /* Get the channel number and verify it. */
1989 id = packet_get_int();
1990 c = channel_lookup(id);
1991
1992 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1993 log("Received window adjust for "
1994 "non-open channel %d.", id);
1995 return;
1996 }
1997 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001998 packet_done();
Damien Millerd3444942000-09-30 14:20:03 +11001999 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10002000 c->remote_window += adjust;
2001}
2002
Damien Miller4af51302000-04-16 11:18:38 +10002003void
Ben Lindstrome9c99912001-06-09 00:41:05 +00002004channel_input_port_open(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002005{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002006 Channel *c = NULL;
2007 u_short host_port;
2008 char *host, *originator_string;
2009 int remote_id, sock = -1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002010
Ben Lindstrome9c99912001-06-09 00:41:05 +00002011 remote_id = packet_get_int();
2012 host = packet_get_string(NULL);
2013 host_port = packet_get_int();
2014
2015 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2016 originator_string = packet_get_string(NULL);
2017 } else {
2018 originator_string = xstrdup("unknown (remote did not supply name)");
2019 }
2020 packet_done();
2021 sock = channel_connect_to(host, host_port);
2022 if (sock != -1) {
2023 c = channel_new("connected socket",
2024 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2025 originator_string, 1);
2026 if (c == NULL) {
2027 error("channel_input_port_open: channel_new failed");
2028 close(sock);
2029 } else {
2030 c->remote_id = remote_id;
Damien Miller95def091999-11-25 00:26:21 +11002031 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002032 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002033 if (c == NULL) {
2034 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2035 packet_put_int(remote_id);
2036 packet_send();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002037 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002038 xfree(host);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002039}
2040
2041
Ben Lindstrome9c99912001-06-09 00:41:05 +00002042/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002043
Ben Lindstrom908afed2001-10-03 17:34:59 +00002044void
2045channel_set_af(int af)
2046{
2047 IPv4or6 = af;
2048}
2049
Damien Miller5428f641999-11-25 11:54:57 +11002050/*
2051 * Initiate forwarding of connections to local port "port" through the secure
2052 * channel to host:port from remote side.
2053 */
Kevin Steves12057502001-02-05 14:54:34 +00002054int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002055channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
2056 u_short port_to_connect, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002057{
Kevin Steves12057502001-02-05 14:54:34 +00002058 return channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002059 NULL, listen_port,
2060 host_to_connect, port_to_connect,
2061 gateway_ports, /*remote_fwd*/ 0);
2062}
2063
2064/*
2065 * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
2066 * (SSH_CHANNEL_RPORT_LISTENER).
2067 */
Kevin Steves12057502001-02-05 14:54:34 +00002068int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002069channel_request_forwarding(
2070 const char *listen_address, u_short listen_port,
2071 const char *host_to_connect, u_short port_to_connect,
2072 int gateway_ports, int remote_fwd)
2073{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002074 Channel *c;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002075 int success, sock, on = 1, type;
Damien Miller34132e52000-01-14 15:45:46 +11002076 struct addrinfo hints, *ai, *aitop;
2077 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0bc1bd82000-11-13 22:57:25 +11002078 const char *host;
Damien Miller5428f641999-11-25 11:54:57 +11002079 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002080
Kevin Steves12057502001-02-05 14:54:34 +00002081 success = 0;
2082
Damien Miller0bc1bd82000-11-13 22:57:25 +11002083 if (remote_fwd) {
2084 host = listen_address;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002085 type = SSH_CHANNEL_RPORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002086 } else {
2087 host = host_to_connect;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002088 type = SSH_CHANNEL_PORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002089 }
2090
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002091 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00002092 error("Forward host name too long.");
2093 return success;
2094 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002095
Damien Miller0bc1bd82000-11-13 22:57:25 +11002096 /* XXX listen_address is currently ignored */
Damien Miller5428f641999-11-25 11:54:57 +11002097 /*
Damien Miller34132e52000-01-14 15:45:46 +11002098 * getaddrinfo returns a loopback address if the hostname is
2099 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002100 */
Damien Miller34132e52000-01-14 15:45:46 +11002101 memset(&hints, 0, sizeof(hints));
2102 hints.ai_family = IPv4or6;
2103 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
2104 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002105 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002106 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
2107 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11002108
Damien Miller34132e52000-01-14 15:45:46 +11002109 for (ai = aitop; ai; ai = ai->ai_next) {
2110 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2111 continue;
2112 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2113 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11002114 error("channel_request_forwarding: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002115 continue;
2116 }
2117 /* Create a port to listen for the host. */
2118 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2119 if (sock < 0) {
2120 /* this is no error since kernel may not support ipv6 */
2121 verbose("socket: %.100s", strerror(errno));
2122 continue;
2123 }
2124 /*
2125 * Set socket options. We would like the socket to disappear
2126 * as soon as it has been closed for whatever reason.
2127 */
2128 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
2129 linger.l_onoff = 1;
2130 linger.l_linger = 5;
2131 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
2132 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002133
Damien Miller34132e52000-01-14 15:45:46 +11002134 /* Bind the socket to the address. */
2135 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2136 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002137 if (!ai->ai_next)
2138 error("bind: %.100s", strerror(errno));
2139 else
2140 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002141
Damien Miller34132e52000-01-14 15:45:46 +11002142 close(sock);
2143 continue;
2144 }
2145 /* Start listening for connections on the socket. */
2146 if (listen(sock, 5) < 0) {
2147 error("listen: %.100s", strerror(errno));
2148 close(sock);
2149 continue;
2150 }
2151 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002152 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002153 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002154 0, xstrdup("port listener"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002155 if (c == NULL) {
2156 error("channel_request_forwarding: channel_new failed");
2157 close(sock);
2158 continue;
2159 }
2160 strlcpy(c->path, host, sizeof(c->path));
2161 c->host_port = port_to_connect;
2162 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002163 success = 1;
2164 }
2165 if (success == 0)
Kevin Steves12057502001-02-05 14:54:34 +00002166 error("channel_request_forwarding: cannot listen to port: %d",
2167 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002168 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002169 return success;
Damien Miller95def091999-11-25 00:26:21 +11002170}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002171
Damien Miller5428f641999-11-25 11:54:57 +11002172/*
2173 * Initiate forwarding of connections to port "port" on remote host through
2174 * the secure channel to host:port from local side.
2175 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002176
Damien Miller4af51302000-04-16 11:18:38 +10002177void
Damien Miller0bc1bd82000-11-13 22:57:25 +11002178channel_request_remote_forwarding(u_short listen_port,
2179 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002180{
Damien Miller0bc1bd82000-11-13 22:57:25 +11002181 int payload_len, type, success = 0;
2182
Damien Miller95def091999-11-25 00:26:21 +11002183 /* Record locally that connection to this host/port is permitted. */
2184 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2185 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002186
Damien Miller95def091999-11-25 00:26:21 +11002187 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002188 if (compat20) {
2189 const char *address_to_bind = "0.0.0.0";
2190 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2191 packet_put_cstring("tcpip-forward");
2192 packet_put_char(0); /* boolean: want reply */
2193 packet_put_cstring(address_to_bind);
2194 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002195 packet_send();
2196 packet_write_wait();
2197 /* Assume that server accepts the request */
2198 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002199 } else {
2200 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002201 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002202 packet_put_cstring(host_to_connect);
2203 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002204 packet_send();
2205 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002206
2207 /* Wait for response from the remote side. */
2208 type = packet_read(&payload_len);
2209 switch (type) {
2210 case SSH_SMSG_SUCCESS:
2211 success = 1;
2212 break;
2213 case SSH_SMSG_FAILURE:
2214 log("Warning: Server denied remote port forwarding.");
2215 break;
2216 default:
2217 /* Unknown packet */
2218 packet_disconnect("Protocol error for port forward request:"
2219 "received packet type %d.", type);
2220 }
2221 }
2222 if (success) {
2223 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2224 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2225 permitted_opens[num_permitted_opens].listen_port = listen_port;
2226 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002227 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002228}
2229
Damien Miller5428f641999-11-25 11:54:57 +11002230/*
2231 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2232 * listening for the port, and sends back a success reply (or disconnect
2233 * message if there was an error). This never returns if there was an error.
2234 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002235
Damien Miller4af51302000-04-16 11:18:38 +10002236void
Damien Millere247cc42000-05-07 12:03:14 +10002237channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002238{
Damien Milleraae6c611999-12-06 11:47:28 +11002239 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11002240 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002241
Damien Miller95def091999-11-25 00:26:21 +11002242 /* Get arguments from the packet. */
2243 port = packet_get_int();
2244 hostname = packet_get_string(NULL);
2245 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002246
Damien Millerbac2d8a2000-09-05 16:13:06 +11002247#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002248 /*
2249 * Check that an unprivileged user is not trying to forward a
2250 * privileged port.
2251 */
Damien Miller95def091999-11-25 00:26:21 +11002252 if (port < IPPORT_RESERVED && !is_root)
2253 packet_disconnect("Requested forwarding of port %d but user is not root.",
2254 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11002255#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11002256 /* Initiate forwarding */
Damien Millere247cc42000-05-07 12:03:14 +10002257 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002258
2259 /* Free the argument string. */
2260 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002261}
2262
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002263/*
2264 * Permits opening to any host/port if permitted_opens[] is empty. This is
2265 * usually called by the server, because the user could connect to any port
2266 * anyway, and the server has no way to know but to trust the client anyway.
2267 */
2268void
2269channel_permit_all_opens()
2270{
2271 if (num_permitted_opens == 0)
2272 all_opens_permitted = 1;
2273}
2274
Ben Lindstroma3700052001-04-05 23:26:32 +00002275void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002276channel_add_permitted_opens(char *host, int port)
2277{
2278 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2279 fatal("channel_request_remote_forwarding: too many forwards");
2280 debug("allow port forwarding to host %s port %d", host, port);
2281
2282 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2283 permitted_opens[num_permitted_opens].port_to_connect = port;
2284 num_permitted_opens++;
2285
2286 all_opens_permitted = 0;
2287}
2288
Ben Lindstroma3700052001-04-05 23:26:32 +00002289void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002290channel_clear_permitted_opens(void)
2291{
2292 int i;
2293
2294 for (i = 0; i < num_permitted_opens; i++)
2295 xfree(permitted_opens[i].host_to_connect);
2296 num_permitted_opens = 0;
2297
2298}
2299
2300
2301/* return socket to remote host, port */
Ben Lindstrombba81212001-06-25 05:01:22 +00002302static int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002303connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002304{
Damien Miller34132e52000-01-14 15:45:46 +11002305 struct addrinfo hints, *ai, *aitop;
2306 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2307 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002308 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002309
Damien Miller34132e52000-01-14 15:45:46 +11002310 memset(&hints, 0, sizeof(hints));
2311 hints.ai_family = IPv4or6;
2312 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002313 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002314 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002315 error("connect_to %.100s: unknown host (%s)", host,
2316 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002317 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002318 }
Damien Miller34132e52000-01-14 15:45:46 +11002319 for (ai = aitop; ai; ai = ai->ai_next) {
2320 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2321 continue;
2322 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2323 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002324 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002325 continue;
2326 }
Damien Miller34132e52000-01-14 15:45:46 +11002327 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2328 if (sock < 0) {
2329 error("socket: %.100s", strerror(errno));
2330 continue;
2331 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +00002332 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002333 fatal("connect_to: F_SETFL: %s", strerror(errno));
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002334 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2335 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002336 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002337 strerror(errno));
2338 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002339 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002340 }
2341 break; /* success */
2342
2343 }
2344 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002345 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002346 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002347 return -1;
2348 }
2349 /* success */
2350 return sock;
2351}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002352
Damien Miller0bc1bd82000-11-13 22:57:25 +11002353int
Ben Lindstrom173e6462001-07-04 05:15:15 +00002354channel_connect_by_listen_address(u_short listen_port)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002355{
2356 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002357
Damien Miller0bc1bd82000-11-13 22:57:25 +11002358 for (i = 0; i < num_permitted_opens; i++)
2359 if (permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002360 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002361 permitted_opens[i].host_to_connect,
2362 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002363 error("WARNING: Server requests forwarding for unknown listen_port %d",
2364 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002365 return -1;
2366}
Damien Millerb38eff82000-04-01 11:09:21 +10002367
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002368/* Check if connecting to that port is permitted and connect. */
2369int
2370channel_connect_to(const char *host, u_short port)
2371{
2372 int i, permit;
2373
2374 permit = all_opens_permitted;
2375 if (!permit) {
2376 for (i = 0; i < num_permitted_opens; i++)
2377 if (permitted_opens[i].port_to_connect == port &&
2378 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2379 permit = 1;
2380
2381 }
2382 if (!permit) {
2383 log("Received request to connect to host %.100s port %d, "
2384 "but the request was denied.", host, port);
2385 return -1;
2386 }
2387 return connect_to(host, port);
2388}
2389
Ben Lindstrome9c99912001-06-09 00:41:05 +00002390/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002391
Damien Miller5428f641999-11-25 11:54:57 +11002392/*
2393 * Creates an internet domain socket for listening for X11 connections.
2394 * Returns a suitable value for the DISPLAY variable, or NULL if an error
2395 * occurs.
2396 */
Damien Miller95def091999-11-25 00:26:21 +11002397char *
Damien Millera34a28b1999-12-14 10:47:15 +11002398x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002399{
Damien Milleraae6c611999-12-06 11:47:28 +11002400 int display_number, sock;
2401 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002402 struct addrinfo hints, *ai, *aitop;
2403 char strport[NI_MAXSERV];
2404 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
2405 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11002406 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002407
Damien Millera34a28b1999-12-14 10:47:15 +11002408 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11002409 display_number < MAX_DISPLAYS;
2410 display_number++) {
2411 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002412 memset(&hints, 0, sizeof(hints));
2413 hints.ai_family = IPv4or6;
2414 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
2415 hints.ai_socktype = SOCK_STREAM;
2416 snprintf(strport, sizeof strport, "%d", port);
2417 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2418 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11002419 return NULL;
2420 }
Damien Miller34132e52000-01-14 15:45:46 +11002421 for (ai = aitop; ai; ai = ai->ai_next) {
2422 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2423 continue;
2424 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2425 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002426 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002427 error("socket: %.100s", strerror(errno));
2428 return NULL;
2429 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002430 debug("x11_create_display_inet: Socket family %d not supported",
2431 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002432 continue;
2433 }
Damien Miller34132e52000-01-14 15:45:46 +11002434 }
2435 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2436 debug("bind port %d: %.100s", port, strerror(errno));
2437 shutdown(sock, SHUT_RDWR);
2438 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002439
2440 if (ai->ai_next)
2441 continue;
2442
Damien Miller34132e52000-01-14 15:45:46 +11002443 for (n = 0; n < num_socks; n++) {
2444 shutdown(socks[n], SHUT_RDWR);
2445 close(socks[n]);
2446 }
2447 num_socks = 0;
2448 break;
2449 }
2450 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002451#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002452 if (num_socks == NUM_SOCKS)
2453 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002454#else
2455 break;
2456#endif
Damien Miller95def091999-11-25 00:26:21 +11002457 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002458 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002459 if (num_socks > 0)
2460 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002461 }
Damien Miller95def091999-11-25 00:26:21 +11002462 if (display_number >= MAX_DISPLAYS) {
2463 error("Failed to allocate internet-domain X11 display socket.");
2464 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002465 }
Damien Miller95def091999-11-25 00:26:21 +11002466 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002467 for (n = 0; n < num_socks; n++) {
2468 sock = socks[n];
2469 if (listen(sock, 5) < 0) {
2470 error("listen: %.100s", strerror(errno));
2471 shutdown(sock, SHUT_RDWR);
2472 close(sock);
2473 return NULL;
2474 }
Damien Miller95def091999-11-25 00:26:21 +11002475 }
Damien Miller34132e52000-01-14 15:45:46 +11002476
Damien Miller95def091999-11-25 00:26:21 +11002477 /* Set up a suitable value for the DISPLAY variable. */
2478 if (gethostname(hostname, sizeof(hostname)) < 0)
2479 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11002480
2481#ifdef IPADDR_IN_DISPLAY
Kevin Stevesef4eea92001-02-05 12:42:17 +00002482 /*
Damien Miller76112de1999-12-21 11:18:08 +11002483 * HPUX detects the local hostname in the DISPLAY variable and tries
2484 * to set up a shared memory connection to the server, which it
2485 * incorrectly supposes to be local.
2486 *
2487 * The workaround - as used in later $$H and other programs - is
2488 * is to set display to the host's IP address.
2489 */
2490 {
2491 struct hostent *he;
2492 struct in_addr my_addr;
2493
2494 he = gethostbyname(hostname);
2495 if (he == NULL) {
2496 error("[X11-broken-fwd-hostname-workaround] Could not get "
2497 "IP address for hostname %s.", hostname);
2498
2499 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
2500 "Could not get IP address for hostname %s.", hostname);
2501
2502 shutdown(sock, SHUT_RDWR);
2503 close(sock);
2504
2505 return NULL;
2506 }
2507
2508 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2509
2510 /* Set DISPLAY to <ip address>:screen.display */
Kevin Stevesef4eea92001-02-05 12:42:17 +00002511 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Kevin Stevesda6cd592000-12-29 18:41:21 +00002512 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002513 }
2514#else /* IPADDR_IN_DISPLAY */
2515 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11002516 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Kevin Stevesda6cd592000-12-29 18:41:21 +00002517 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002518#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002519
Damien Miller34132e52000-01-14 15:45:46 +11002520 /* Allocate a channel for each socket. */
2521 for (n = 0; n < num_socks; n++) {
2522 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10002523 (void) channel_new("x11 listener",
2524 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2525 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002526 0, xstrdup("X11 inet listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11002527 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002528
Damien Miller95def091999-11-25 00:26:21 +11002529 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11002530 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002531}
2532
2533#ifndef X_UNIX_PATH
2534#define X_UNIX_PATH "/tmp/.X11-unix/X"
2535#endif
2536
Ben Lindstrombba81212001-06-25 05:01:22 +00002537static int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002538connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002539{
Damien Miller95def091999-11-25 00:26:21 +11002540 static const char *const x_sockets[] = {
2541 X_UNIX_PATH "%u",
2542 "/var/X/.X11-unix/X" "%u",
2543 "/usr/spool/sockets/X11/" "%u",
2544 NULL
2545 };
2546 int sock;
2547 struct sockaddr_un addr;
2548 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002549
Damien Miller95def091999-11-25 00:26:21 +11002550 for (path = x_sockets; *path; ++path) {
2551 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2552 if (sock < 0)
2553 error("socket: %.100s", strerror(errno));
2554 memset(&addr, 0, sizeof(addr));
2555 addr.sun_family = AF_UNIX;
2556 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
2557 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2558 return sock;
2559 close(sock);
2560 }
2561 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2562 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002563}
2564
Damien Millerbd483e72000-04-30 10:00:53 +10002565int
2566x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002567{
Damien Millerbd483e72000-04-30 10:00:53 +10002568 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002569 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002570 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002571 struct addrinfo hints, *ai, *aitop;
2572 char strport[NI_MAXSERV];
2573 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002574
Damien Miller95def091999-11-25 00:26:21 +11002575 /* Try to open a socket for the local X server. */
2576 display = getenv("DISPLAY");
2577 if (!display) {
2578 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002579 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002580 }
Damien Miller5428f641999-11-25 11:54:57 +11002581 /*
2582 * Now we decode the value of the DISPLAY variable and make a
2583 * connection to the real X server.
2584 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002585
Damien Miller5428f641999-11-25 11:54:57 +11002586 /*
2587 * Check if it is a unix domain socket. Unix domain displays are in
2588 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2589 */
Damien Miller95def091999-11-25 00:26:21 +11002590 if (strncmp(display, "unix:", 5) == 0 ||
2591 display[0] == ':') {
2592 /* Connect to the unix domain socket. */
2593 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2594 error("Could not parse display number from DISPLAY: %.100s",
2595 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002596 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002597 }
2598 /* Create a socket. */
2599 sock = connect_local_xsocket(display_number);
2600 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002601 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002602
2603 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002604 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002605 }
Damien Miller5428f641999-11-25 11:54:57 +11002606 /*
2607 * Connect to an inet socket. The DISPLAY value is supposedly
2608 * hostname:d[.s], where hostname may also be numeric IP address.
2609 */
Damien Miller95def091999-11-25 00:26:21 +11002610 strncpy(buf, display, sizeof(buf));
2611 buf[sizeof(buf) - 1] = 0;
2612 cp = strchr(buf, ':');
2613 if (!cp) {
2614 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002615 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002616 }
Damien Miller95def091999-11-25 00:26:21 +11002617 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002618 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002619 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2620 error("Could not parse display number from DISPLAY: %.100s",
2621 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002622 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002623 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002624
Damien Miller34132e52000-01-14 15:45:46 +11002625 /* Look up the host address */
2626 memset(&hints, 0, sizeof(hints));
2627 hints.ai_family = IPv4or6;
2628 hints.ai_socktype = SOCK_STREAM;
2629 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2630 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2631 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002632 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002633 }
Damien Miller34132e52000-01-14 15:45:46 +11002634 for (ai = aitop; ai; ai = ai->ai_next) {
2635 /* Create a socket. */
2636 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2637 if (sock < 0) {
2638 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002639 continue;
2640 }
2641 /* Connect it to the display. */
2642 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2643 debug("connect %.100s port %d: %.100s", buf,
2644 6000 + display_number, strerror(errno));
2645 close(sock);
2646 continue;
2647 }
2648 /* Success */
2649 break;
Damien Miller34132e52000-01-14 15:45:46 +11002650 }
Damien Miller34132e52000-01-14 15:45:46 +11002651 freeaddrinfo(aitop);
2652 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002653 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002654 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002655 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002656 }
Damien Millerbd483e72000-04-30 10:00:53 +10002657 return sock;
2658}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002659
Damien Millerbd483e72000-04-30 10:00:53 +10002660/*
2661 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2662 * the remote channel number. We should do whatever we want, and respond
2663 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2664 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002665
Damien Millerbd483e72000-04-30 10:00:53 +10002666void
Damien Miller62cee002000-09-23 17:15:56 +11002667x11_input_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002668{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002669 Channel *c = NULL;
2670 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002671 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10002672
2673 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002674
2675 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002676
2677 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002678 remote_host = packet_get_string(NULL);
2679 } else {
2680 remote_host = xstrdup("unknown (remote did not supply name)");
2681 }
2682 packet_done();
Damien Millerbd483e72000-04-30 10:00:53 +10002683
2684 /* Obtain a connection to the real X display. */
2685 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002686 if (sock != -1) {
2687 /* Allocate a channel for this connection. */
2688 c = channel_new("connected x11 socket",
2689 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
2690 remote_host, 1);
2691 if (c == NULL) {
2692 error("x11_input_open: channel_new failed");
2693 close(sock);
2694 } else {
2695 c->remote_id = remote_id;
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002696 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002697 }
2698 }
2699 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10002700 /* Send refusal to the remote host. */
2701 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002702 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10002703 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10002704 /* Send a confirmation to the remote host. */
2705 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002706 packet_put_int(remote_id);
2707 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10002708 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002709 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002710}
2711
Damien Miller69b69aa2000-10-28 14:19:58 +11002712/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2713void
2714deny_input_open(int type, int plen, void *ctxt)
2715{
2716 int rchan = packet_get_int();
2717 switch(type){
2718 case SSH_SMSG_AGENT_OPEN:
2719 error("Warning: ssh server tried agent forwarding.");
2720 break;
2721 case SSH_SMSG_X11_OPEN:
2722 error("Warning: ssh server tried X11 forwarding.");
2723 break;
2724 default:
2725 error("deny_input_open: type %d plen %d", type, plen);
2726 break;
2727 }
2728 error("Warning: this is probably a break in attempt by a malicious server.");
2729 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2730 packet_put_int(rchan);
2731 packet_send();
2732}
2733
Damien Miller5428f641999-11-25 11:54:57 +11002734/*
2735 * Requests forwarding of X11 connections, generates fake authentication
2736 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00002737 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11002738 */
Damien Miller4af51302000-04-16 11:18:38 +10002739void
Damien Millerbd483e72000-04-30 10:00:53 +10002740x11_request_forwarding_with_spoofing(int client_session_id,
2741 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002742{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002743 u_int data_len = (u_int) strlen(data) / 2;
Ben Lindstromb3211a82001-02-10 22:33:19 +00002744 u_int i, value, len;
Damien Miller95def091999-11-25 00:26:21 +11002745 char *new_data;
2746 int screen_number;
2747 const char *cp;
2748 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002749
Damien Miller95def091999-11-25 00:26:21 +11002750 cp = getenv("DISPLAY");
2751 if (cp)
2752 cp = strchr(cp, ':');
2753 if (cp)
2754 cp = strchr(cp, '.');
2755 if (cp)
2756 screen_number = atoi(cp + 1);
2757 else
2758 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002759
Damien Miller95def091999-11-25 00:26:21 +11002760 /* Save protocol name. */
2761 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002762
Damien Miller5428f641999-11-25 11:54:57 +11002763 /*
2764 * Extract real authentication data and generate fake data of the
2765 * same length.
2766 */
Damien Miller95def091999-11-25 00:26:21 +11002767 x11_saved_data = xmalloc(data_len);
2768 x11_fake_data = xmalloc(data_len);
2769 for (i = 0; i < data_len; i++) {
2770 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2771 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2772 if (i % 4 == 0)
2773 rand = arc4random();
2774 x11_saved_data[i] = value;
2775 x11_fake_data[i] = rand & 0xff;
2776 rand >>= 8;
2777 }
2778 x11_saved_data_len = data_len;
2779 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002780
Damien Miller95def091999-11-25 00:26:21 +11002781 /* Convert the fake data into hex. */
Ben Lindstromb3211a82001-02-10 22:33:19 +00002782 len = 2 * data_len + 1;
2783 new_data = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +11002784 for (i = 0; i < data_len; i++)
Ben Lindstromb3211a82001-02-10 22:33:19 +00002785 snprintf(new_data + 2 * i, len - 2 * i,
2786 "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002787
Damien Miller95def091999-11-25 00:26:21 +11002788 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002789 if (compat20) {
2790 channel_request_start(client_session_id, "x11-req", 0);
2791 packet_put_char(0); /* XXX bool single connection */
2792 } else {
2793 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2794 }
2795 packet_put_cstring(proto);
2796 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002797 packet_put_int(screen_number);
2798 packet_send();
2799 packet_write_wait();
2800 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002801}
2802
Ben Lindstrome9c99912001-06-09 00:41:05 +00002803
2804/* -- agent forwarding */
2805
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002806/* Sends a message to the server to request authentication fd forwarding. */
2807
Damien Miller4af51302000-04-16 11:18:38 +10002808void
Damien Miller95def091999-11-25 00:26:21 +11002809auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002810{
Damien Miller95def091999-11-25 00:26:21 +11002811 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2812 packet_send();
2813 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002814}
2815
Damien Miller5428f641999-11-25 11:54:57 +11002816/*
2817 * Returns the name of the forwarded authentication socket. Returns NULL if
2818 * there is no forwarded authentication socket. The returned value points to
2819 * a static buffer.
2820 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002821
Damien Miller95def091999-11-25 00:26:21 +11002822char *
2823auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002824{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002825 return auth_sock_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002826}
2827
2828/* removes the agent forwarding socket */
2829
Damien Miller4af51302000-04-16 11:18:38 +10002830void
Ben Lindstrom983c0982001-06-09 01:20:06 +00002831auth_sock_cleanup_proc(void *_pw)
Damien Miller95def091999-11-25 00:26:21 +11002832{
Ben Lindstrom983c0982001-06-09 01:20:06 +00002833 struct passwd *pw = _pw;
2834
Ben Lindstrom838394c2001-06-09 01:11:59 +00002835 if (auth_sock_name) {
Ben Lindstrom983c0982001-06-09 01:20:06 +00002836 temporarily_use_uid(pw);
Ben Lindstrom838394c2001-06-09 01:11:59 +00002837 unlink(auth_sock_name);
2838 rmdir(auth_sock_dir);
2839 auth_sock_name = NULL;
Ben Lindstrom983c0982001-06-09 01:20:06 +00002840 restore_uid();
Ben Lindstrom838394c2001-06-09 01:11:59 +00002841 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002842}
2843
Damien Miller5428f641999-11-25 11:54:57 +11002844/*
Damien Millerd3a18572000-06-07 19:55:44 +10002845 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002846 * This starts forwarding authentication requests.
2847 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002848
Damien Millerd3a18572000-06-07 19:55:44 +10002849int
Damien Miller95def091999-11-25 00:26:21 +11002850auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002851{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002852 Channel *nc;
2853 int sock;
Damien Miller95def091999-11-25 00:26:21 +11002854 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002855
Ben Lindstrome9c99912001-06-09 00:41:05 +00002856 if (auth_get_socket_name() != NULL) {
2857 error("authentication forwarding requested twice.");
2858 return 0;
2859 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002860
Damien Miller95def091999-11-25 00:26:21 +11002861 /* Temporarily drop privileged uid for mkdir/bind. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00002862 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002863
Damien Miller95def091999-11-25 00:26:21 +11002864 /* Allocate a buffer for the socket name, and format the name. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002865 auth_sock_name = xmalloc(MAXPATHLEN);
2866 auth_sock_dir = xmalloc(MAXPATHLEN);
2867 strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002868
Damien Miller95def091999-11-25 00:26:21 +11002869 /* Create private directory for socket */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002870 if (mkdtemp(auth_sock_dir) == NULL) {
2871 packet_send_debug("Agent forwarding disabled: "
2872 "mkdtemp() failed: %.100s", strerror(errno));
Damien Millerd3a18572000-06-07 19:55:44 +10002873 restore_uid();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002874 xfree(auth_sock_name);
2875 xfree(auth_sock_dir);
2876 auth_sock_name = NULL;
2877 auth_sock_dir = NULL;
Damien Millerd3a18572000-06-07 19:55:44 +10002878 return 0;
2879 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002880 snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%d",
2881 auth_sock_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002882
Ben Lindstrom838394c2001-06-09 01:11:59 +00002883 /* delete agent socket on fatal() */
Ben Lindstrom983c0982001-06-09 01:20:06 +00002884 fatal_add_cleanup(auth_sock_cleanup_proc, pw);
Ben Lindstrom838394c2001-06-09 01:11:59 +00002885
Damien Miller95def091999-11-25 00:26:21 +11002886 /* Create the socket. */
2887 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2888 if (sock < 0)
2889 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002890
Damien Miller95def091999-11-25 00:26:21 +11002891 /* Bind it to the name. */
2892 memset(&sunaddr, 0, sizeof(sunaddr));
2893 sunaddr.sun_family = AF_UNIX;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002894 strncpy(sunaddr.sun_path, auth_sock_name,
Damien Miller95def091999-11-25 00:26:21 +11002895 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002896
Damien Miller95def091999-11-25 00:26:21 +11002897 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2898 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002899
Damien Miller95def091999-11-25 00:26:21 +11002900 /* Restore the privileged uid. */
2901 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002902
Damien Miller95def091999-11-25 00:26:21 +11002903 /* Start listening on the socket. */
2904 if (listen(sock, 5) < 0)
2905 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002906
Damien Miller95def091999-11-25 00:26:21 +11002907 /* Allocate a channel for the authentication agent socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002908 nc = channel_new("auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11002909 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2910 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2911 0, xstrdup("auth socket"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002912 if (nc == NULL) {
2913 error("auth_input_request_forwarding: channel_new failed");
Ben Lindstrom983c0982001-06-09 01:20:06 +00002914 auth_sock_cleanup_proc(pw);
Ben Lindstromdf4981b2001-06-09 01:32:29 +00002915 fatal_remove_cleanup(auth_sock_cleanup_proc, pw);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002916 close(sock);
2917 return 0;
2918 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002919 strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
Damien Millerd3a18572000-06-07 19:55:44 +10002920 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002921}
2922
2923/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2924
Damien Miller4af51302000-04-16 11:18:38 +10002925void
Damien Miller62cee002000-09-23 17:15:56 +11002926auth_input_open_request(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002927{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002928 Channel *c = NULL;
2929 int remote_id, sock;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002930 char *name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002931
Damien Millerb38eff82000-04-01 11:09:21 +10002932 packet_integrity_check(plen, 4, type);
2933
Damien Miller95def091999-11-25 00:26:21 +11002934 /* Read the remote channel number from the message. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002935 remote_id = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002936
Damien Miller5428f641999-11-25 11:54:57 +11002937 /*
2938 * Get a connection to the local authentication agent (this may again
2939 * get forwarded).
2940 */
Damien Miller95def091999-11-25 00:26:21 +11002941 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002942
Damien Miller5428f641999-11-25 11:54:57 +11002943 /*
2944 * If we could not connect the agent, send an error message back to
2945 * the server. This should never happen unless the agent dies,
2946 * because authentication forwarding is only enabled if we have an
2947 * agent.
2948 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002949 if (sock >= 0) {
Ben Lindstrome9c99912001-06-09 00:41:05 +00002950 name = xstrdup("authentication agent connection");
2951 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
2952 -1, 0, 0, 0, name, 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002953 if (c == NULL) {
2954 error("auth_input_open_request: channel_new failed");
Ben Lindstrome9c99912001-06-09 00:41:05 +00002955 xfree(name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002956 close(sock);
2957 } else {
2958 c->remote_id = remote_id;
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002959 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002960 }
Damien Miller95def091999-11-25 00:26:21 +11002961 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002962 if (c == NULL) {
2963 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2964 packet_put_int(remote_id);
2965 } else {
2966 /* Send a confirmation to the remote host. */
2967 debug("Forwarding authentication connection.");
2968 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2969 packet_put_int(remote_id);
2970 packet_put_int(c->self);
2971 }
Damien Miller95def091999-11-25 00:26:21 +11002972 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002973}