blob: fe1db03ab718dc5a2a3cbf54f35f3b984775f668 [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.
Damien Millera90fc082002-01-22 23:19:38 +110016 * Copyright (c) 1999, 2000, 2001, 2002 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 Millera90fc082002-01-22 23:19:38 +110042RCSID("$OpenBSD: channels.c,v 1.156 2002/01/05 10:43: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"
Damien Miller3afe3752001-12-21 12:39:51 +110057#include "pathnames.h"
Damien Miller994cf142000-07-21 10:19:44 +100058
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Ben Lindstrome9c99912001-06-09 00:41:05 +000060/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
Damien Miller5428f641999-11-25 11:54:57 +110062/*
63 * Pointer to an array containing all allocated channels. The array is
64 * dynamically extended as needed.
65 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000066static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067
Damien Miller5428f641999-11-25 11:54:57 +110068/*
69 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000070 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110071 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072static int channels_alloc = 0;
73
Damien Miller5428f641999-11-25 11:54:57 +110074/*
75 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +000076 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +110077 */
Damien Miller5e953212001-01-30 09:14:00 +110078static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
Ben Lindstrome9c99912001-06-09 00:41:05 +000081/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Miller5428f641999-11-25 11:54:57 +110083/*
84 * Data structure for storing which hosts are permitted for forward requests.
85 * The local sides of any remote forwards are stored in this array to prevent
86 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
87 * network (which might be behind a firewall).
88 */
Damien Miller95def091999-11-25 00:26:21 +110089typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +100090 char *host_to_connect; /* Connect to 'host'. */
91 u_short port_to_connect; /* Connect to 'port'. */
92 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093} ForwardPermission;
94
95/* List of all permitted host/port pairs to connect. */
96static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
Ben Lindstrome9c99912001-06-09 00:41:05 +000097
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098/* Number of permitted host/port pairs in the array. */
99static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * If this is true, all opens are permitted. This is the case on the server
102 * on which we have to trust the client anyway, and the user could do
103 * anything after logging in anyway.
104 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105static int all_opens_permitted = 0;
106
Ben Lindstrome9c99912001-06-09 00:41:05 +0000107
108/* -- X11 forwarding */
109
110/* Maximum number of fake X11 displays to try. */
111#define MAX_DISPLAYS 1000
112
113/* Saved X11 authentication protocol name. */
114static char *x11_saved_proto = NULL;
115
116/* Saved X11 authentication data. This is the real data. */
117static char *x11_saved_data = NULL;
118static u_int x11_saved_data_len = 0;
119
120/*
121 * Fake X11 authentication data. This is what the server will be sending us;
122 * we should replace any occurrences of this by the real data.
123 */
124static char *x11_fake_data = NULL;
125static u_int x11_fake_data_len;
126
127
128/* -- agent forwarding */
129
130#define NUM_SOCKS 10
131
132/* Name and directory of socket for authentication agent forwarding. */
133static char *auth_sock_name = NULL;
134static char *auth_sock_dir = NULL;
135
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000136/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000137static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000138
Ben Lindstrome9c99912001-06-09 00:41:05 +0000139/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000140static void port_open_helper(Channel *c, char *rtype);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000141
Ben Lindstrome9c99912001-06-09 00:41:05 +0000142/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000143
144Channel *
145channel_lookup(int id)
146{
147 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000148
Damien Millerc0fd17f2000-06-26 10:22:53 +1000149 if (id < 0 || id > channels_alloc) {
Damien Millerb38eff82000-04-01 11:09:21 +1000150 log("channel_lookup: %d: bad id", id);
151 return NULL;
152 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000153 c = channels[id];
154 if (c == NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +1000155 log("channel_lookup: %d: bad id: channel free", id);
156 return NULL;
157 }
158 return c;
159}
160
Damien Miller5428f641999-11-25 11:54:57 +1100161/*
Damien Millere247cc42000-05-07 12:03:14 +1000162 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000163 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100164 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165
Ben Lindstrombba81212001-06-25 05:01:22 +0000166static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100167channel_register_fds(Channel *c, int rfd, int wfd, int efd,
168 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169{
Damien Miller95def091999-11-25 00:26:21 +1100170 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100171 channel_max_fd = MAX(channel_max_fd, rfd);
172 channel_max_fd = MAX(channel_max_fd, wfd);
173 channel_max_fd = MAX(channel_max_fd, efd);
174
Damien Miller5428f641999-11-25 11:54:57 +1100175 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000176
Damien Miller6f83b8e2000-05-02 09:23:45 +1000177 c->rfd = rfd;
178 c->wfd = wfd;
179 c->sock = (rfd == wfd) ? rfd : -1;
180 c->efd = efd;
181 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100182
Damien Miller79438cc2001-02-16 12:34:57 +1100183 /* XXX ugly hack: nonblock is only set by the server */
184 if (nonblock && isatty(c->rfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000185 debug("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100186 c->isatty = 1;
187 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000188 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100189 c->self, c->wfd);
190 }
191 } else {
192 c->isatty = 0;
193 }
194
Damien Miller69b69aa2000-10-28 14:19:58 +1100195 /* enable nonblocking mode */
196 if (nonblock) {
197 if (rfd != -1)
198 set_nonblock(rfd);
199 if (wfd != -1)
200 set_nonblock(wfd);
201 if (efd != -1)
202 set_nonblock(efd);
203 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000204}
205
206/*
207 * Allocate a new channel object and set its type and socket. This will cause
208 * remote_name to be freed.
209 */
210
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000211Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000212channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Damien Miller69b69aa2000-10-28 14:19:58 +1100213 int window, int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000214{
215 int i, found;
216 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217
Damien Miller95def091999-11-25 00:26:21 +1100218 /* Do initial allocation if this is the first call. */
219 if (channels_alloc == 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000220 chan_init();
Damien Miller95def091999-11-25 00:26:21 +1100221 channels_alloc = 10;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000222 channels = xmalloc(channels_alloc * sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100223 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000224 channels[i] = NULL;
Ben Lindstrom601e4362001-06-21 03:19:23 +0000225 fatal_add_cleanup((void (*) (void *)) channel_free_all, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100226 }
227 /* Try to find a free slot where to put the new channel. */
228 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000229 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100230 /* Found a free slot. */
231 found = i;
232 break;
233 }
234 if (found == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100235 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100236 found = channels_alloc;
237 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100238 debug2("channel: expanding %d", channels_alloc);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000239 channels = xrealloc(channels, channels_alloc * sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100240 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000241 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100242 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000243 /* Initialize and return new channel. */
244 c = channels[found] = xmalloc(sizeof(Channel));
Damien Miller4623a752001-10-10 15:03:58 +1000245 memset(c, 0, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100246 buffer_init(&c->input);
247 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000248 buffer_init(&c->extended);
Damien Miller95def091999-11-25 00:26:21 +1100249 chan_init_iostates(c);
Damien Miller69b69aa2000-10-28 14:19:58 +1100250 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100251 c->self = found;
252 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000253 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000254 c->local_window = window;
255 c->local_window_max = window;
256 c->local_consumed = 0;
257 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100258 c->remote_id = -1;
259 c->remote_name = remote_name;
Damien Millerb38eff82000-04-01 11:09:21 +1000260 c->remote_window = 0;
261 c->remote_maxpacket = 0;
262 c->cb_fn = NULL;
263 c->cb_arg = NULL;
264 c->cb_event = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000265 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100266 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000267 c->detach_user = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000268 c->input_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100269 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000270 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000272
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000273static int
274channel_find_maxfd(void)
275{
276 int i, max = 0;
277 Channel *c;
278
279 for (i = 0; i < channels_alloc; i++) {
280 c = channels[i];
281 if (c != NULL) {
282 max = MAX(max, c->rfd);
283 max = MAX(max, c->wfd);
284 max = MAX(max, c->efd);
285 }
286 }
287 return max;
288}
289
290int
291channel_close_fd(int *fdp)
292{
293 int ret = 0, fd = *fdp;
294
295 if (fd != -1) {
296 ret = close(fd);
297 *fdp = -1;
298 if (fd == channel_max_fd)
299 channel_max_fd = channel_find_maxfd();
300 }
301 return ret;
302}
303
Damien Miller6f83b8e2000-05-02 09:23:45 +1000304/* Close all channel fd/socket. */
305
Ben Lindstrombba81212001-06-25 05:01:22 +0000306static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000307channel_close_fds(Channel *c)
308{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000309 debug3("channel_close_fds: channel %d: r %d w %d e %d",
310 c->self, c->rfd, c->wfd, c->efd);
311
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000312 channel_close_fd(&c->sock);
313 channel_close_fd(&c->rfd);
314 channel_close_fd(&c->wfd);
315 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000316}
317
318/* Free the channel and close its fd/socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000319
Damien Miller4af51302000-04-16 11:18:38 +1000320void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000321channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000323 char *s;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000324 int i, n;
325
326 for (n = 0, i = 0; i < channels_alloc; i++)
327 if (channels[i])
328 n++;
Ben Lindstrom4c247552001-06-05 20:56:47 +0000329 debug("channel_free: channel %d: %s, nchannels %d", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000330 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000331
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000332 s = channel_open_message();
Ben Lindstrom4c247552001-06-05 20:56:47 +0000333 debug3("channel_free: status: %s", s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000334 xfree(s);
335
Damien Miller6f83b8e2000-05-02 09:23:45 +1000336 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000337 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000338 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000339 buffer_free(&c->input);
340 buffer_free(&c->output);
341 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000342 if (c->remote_name) {
343 xfree(c->remote_name);
344 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100345 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000346 channels[c->self] = NULL;
347 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000348}
349
Ben Lindstrome9c99912001-06-09 00:41:05 +0000350void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000351channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000352{
353 int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000354
Ben Lindstrom601e4362001-06-21 03:19:23 +0000355 for (i = 0; i < channels_alloc; i++)
356 if (channels[i] != NULL)
357 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000358}
359
360/*
361 * Closes the sockets/fds of all channels. This is used to close extra file
362 * descriptors after a fork.
363 */
364
365void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000366channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000367{
368 int i;
369
370 for (i = 0; i < channels_alloc; i++)
371 if (channels[i] != NULL)
372 channel_close_fds(channels[i]);
373}
374
375/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000376 * Stop listening to channels.
377 */
378
379void
380channel_stop_listening(void)
381{
382 int i;
383 Channel *c;
384
385 for (i = 0; i < channels_alloc; i++) {
386 c = channels[i];
387 if (c != NULL) {
388 switch (c->type) {
389 case SSH_CHANNEL_AUTH_SOCKET:
390 case SSH_CHANNEL_PORT_LISTENER:
391 case SSH_CHANNEL_RPORT_LISTENER:
392 case SSH_CHANNEL_X11_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000393 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000394 channel_free(c);
395 break;
396 }
397 }
398 }
399}
400
401/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000402 * Returns true if no channel has too much buffered data, and false if one or
403 * more channel is overfull.
404 */
405
406int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000407channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000408{
409 u_int i;
410 Channel *c;
411
412 for (i = 0; i < channels_alloc; i++) {
413 c = channels[i];
414 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000415#if 0
416 if (!compat20 &&
417 buffer_len(&c->input) > packet_get_maxsize()) {
Ben Lindstrome9c99912001-06-09 00:41:05 +0000418 debug("channel %d: big input buffer %d",
419 c->self, buffer_len(&c->input));
420 return 0;
421 }
Damien Milleraf5f2e62001-10-10 15:01:16 +1000422#endif
Ben Lindstrome9c99912001-06-09 00:41:05 +0000423 if (buffer_len(&c->output) > packet_get_maxsize()) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000424 debug("channel %d: big output buffer %d > %d",
425 c->self, buffer_len(&c->output),
426 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000427 return 0;
428 }
429 }
430 }
431 return 1;
432}
433
434/* Returns true if any channel is still open. */
435
436int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000437channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000438{
439 int i;
440 Channel *c;
441
442 for (i = 0; i < channels_alloc; i++) {
443 c = channels[i];
444 if (c == NULL)
445 continue;
446 switch (c->type) {
447 case SSH_CHANNEL_X11_LISTENER:
448 case SSH_CHANNEL_PORT_LISTENER:
449 case SSH_CHANNEL_RPORT_LISTENER:
450 case SSH_CHANNEL_CLOSED:
451 case SSH_CHANNEL_AUTH_SOCKET:
452 case SSH_CHANNEL_DYNAMIC:
453 case SSH_CHANNEL_CONNECTING:
454 case SSH_CHANNEL_ZOMBIE:
455 continue;
456 case SSH_CHANNEL_LARVAL:
457 if (!compat20)
458 fatal("cannot happen: SSH_CHANNEL_LARVAL");
459 continue;
460 case SSH_CHANNEL_OPENING:
461 case SSH_CHANNEL_OPEN:
462 case SSH_CHANNEL_X11_OPEN:
463 return 1;
464 case SSH_CHANNEL_INPUT_DRAINING:
465 case SSH_CHANNEL_OUTPUT_DRAINING:
466 if (!compat13)
467 fatal("cannot happen: OUT_DRAIN");
468 return 1;
469 default:
470 fatal("channel_still_open: bad channel type %d", c->type);
471 /* NOTREACHED */
472 }
473 }
474 return 0;
475}
476
477/* Returns the id of an open channel suitable for keepaliving */
478
479int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000480channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000481{
482 int i;
483 Channel *c;
484
485 for (i = 0; i < channels_alloc; i++) {
486 c = channels[i];
487 if (c == NULL)
488 continue;
489 switch (c->type) {
490 case SSH_CHANNEL_CLOSED:
491 case SSH_CHANNEL_DYNAMIC:
492 case SSH_CHANNEL_X11_LISTENER:
493 case SSH_CHANNEL_PORT_LISTENER:
494 case SSH_CHANNEL_RPORT_LISTENER:
495 case SSH_CHANNEL_OPENING:
496 case SSH_CHANNEL_CONNECTING:
497 case SSH_CHANNEL_ZOMBIE:
498 continue;
499 case SSH_CHANNEL_LARVAL:
500 case SSH_CHANNEL_AUTH_SOCKET:
501 case SSH_CHANNEL_OPEN:
502 case SSH_CHANNEL_X11_OPEN:
503 return i;
504 case SSH_CHANNEL_INPUT_DRAINING:
505 case SSH_CHANNEL_OUTPUT_DRAINING:
506 if (!compat13)
507 fatal("cannot happen: OUT_DRAIN");
508 return i;
509 default:
510 fatal("channel_find_open: bad channel type %d", c->type);
511 /* NOTREACHED */
512 }
513 }
514 return -1;
515}
516
517
518/*
519 * Returns a message describing the currently open forwarded connections,
520 * suitable for sending to the client. The message contains crlf pairs for
521 * newlines.
522 */
523
524char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000525channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000526{
527 Buffer buffer;
528 Channel *c;
529 char buf[1024], *cp;
530 int i;
531
532 buffer_init(&buffer);
533 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
534 buffer_append(&buffer, buf, strlen(buf));
535 for (i = 0; i < channels_alloc; i++) {
536 c = channels[i];
537 if (c == NULL)
538 continue;
539 switch (c->type) {
540 case SSH_CHANNEL_X11_LISTENER:
541 case SSH_CHANNEL_PORT_LISTENER:
542 case SSH_CHANNEL_RPORT_LISTENER:
543 case SSH_CHANNEL_CLOSED:
544 case SSH_CHANNEL_AUTH_SOCKET:
545 case SSH_CHANNEL_ZOMBIE:
546 continue;
547 case SSH_CHANNEL_LARVAL:
548 case SSH_CHANNEL_OPENING:
549 case SSH_CHANNEL_CONNECTING:
550 case SSH_CHANNEL_DYNAMIC:
551 case SSH_CHANNEL_OPEN:
552 case SSH_CHANNEL_X11_OPEN:
553 case SSH_CHANNEL_INPUT_DRAINING:
554 case SSH_CHANNEL_OUTPUT_DRAINING:
555 snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
556 c->self, c->remote_name,
557 c->type, c->remote_id,
558 c->istate, buffer_len(&c->input),
559 c->ostate, buffer_len(&c->output),
560 c->rfd, c->wfd);
561 buffer_append(&buffer, buf, strlen(buf));
562 continue;
563 default:
564 fatal("channel_open_message: bad channel type %d", c->type);
565 /* NOTREACHED */
566 }
567 }
568 buffer_append(&buffer, "\0", 1);
569 cp = xstrdup(buffer_ptr(&buffer));
570 buffer_free(&buffer);
571 return cp;
572}
573
574void
575channel_send_open(int id)
576{
577 Channel *c = channel_lookup(id);
578 if (c == NULL) {
579 log("channel_send_open: %d: bad id", id);
580 return;
581 }
582 debug("send channel open %d", id);
583 packet_start(SSH2_MSG_CHANNEL_OPEN);
584 packet_put_cstring(c->ctype);
585 packet_put_int(c->self);
586 packet_put_int(c->local_window);
587 packet_put_int(c->local_maxpacket);
588 packet_send();
589}
590
591void
592channel_request(int id, char *service, int wantconfirm)
593{
594 channel_request_start(id, service, wantconfirm);
595 packet_send();
596 debug("channel request %d: %s", id, service) ;
597}
598void
599channel_request_start(int id, char *service, int wantconfirm)
600{
601 Channel *c = channel_lookup(id);
602 if (c == NULL) {
603 log("channel_request: %d: bad id", id);
604 return;
605 }
606 packet_start(SSH2_MSG_CHANNEL_REQUEST);
607 packet_put_int(c->remote_id);
608 packet_put_cstring(service);
609 packet_put_char(wantconfirm);
610}
611void
612channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
613{
614 Channel *c = channel_lookup(id);
615 if (c == NULL) {
616 log("channel_register_callback: %d: bad id", id);
617 return;
618 }
619 c->cb_event = mtype;
620 c->cb_fn = fn;
621 c->cb_arg = arg;
622}
623void
624channel_register_cleanup(int id, channel_callback_fn *fn)
625{
626 Channel *c = channel_lookup(id);
627 if (c == NULL) {
628 log("channel_register_cleanup: %d: bad id", id);
629 return;
630 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000631 c->detach_user = fn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000632}
633void
634channel_cancel_cleanup(int id)
635{
636 Channel *c = channel_lookup(id);
637 if (c == NULL) {
638 log("channel_cancel_cleanup: %d: bad id", id);
639 return;
640 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000641 c->detach_user = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000642}
643void
644channel_register_filter(int id, channel_filter_fn *fn)
645{
646 Channel *c = channel_lookup(id);
647 if (c == NULL) {
648 log("channel_register_filter: %d: bad id", id);
649 return;
650 }
651 c->input_filter = fn;
652}
653
654void
655channel_set_fds(int id, int rfd, int wfd, int efd,
656 int extusage, int nonblock)
657{
658 Channel *c = channel_lookup(id);
659 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
660 fatal("channel_activate for non-larval channel %d.", id);
661 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
662 c->type = SSH_CHANNEL_OPEN;
663 /* XXX window size? */
664 c->local_window = c->local_window_max = c->local_maxpacket * 2;
665 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
666 packet_put_int(c->remote_id);
667 packet_put_int(c->local_window);
668 packet_send();
669}
670
Damien Miller5428f641999-11-25 11:54:57 +1100671/*
Damien Millerb38eff82000-04-01 11:09:21 +1000672 * 'channel_pre*' are called just before select() to add any bits relevant to
673 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100674 */
Damien Millerb38eff82000-04-01 11:09:21 +1000675/*
676 * 'channel_post*': perform any appropriate operations for channels which
677 * have events pending.
678 */
679typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
680chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
681chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
682
Ben Lindstrombba81212001-06-25 05:01:22 +0000683static void
Damien Millerb38eff82000-04-01 11:09:21 +1000684channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
685{
686 FD_SET(c->sock, readset);
687}
688
Ben Lindstrombba81212001-06-25 05:01:22 +0000689static void
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000690channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
691{
692 debug3("channel %d: waiting for connection", c->self);
693 FD_SET(c->sock, writeset);
694}
695
Ben Lindstrombba81212001-06-25 05:01:22 +0000696static void
Damien Millerb38eff82000-04-01 11:09:21 +1000697channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
698{
699 if (buffer_len(&c->input) < packet_get_maxsize())
700 FD_SET(c->sock, readset);
701 if (buffer_len(&c->output) > 0)
702 FD_SET(c->sock, writeset);
703}
704
Ben Lindstrombba81212001-06-25 05:01:22 +0000705static void
Damien Millerb38eff82000-04-01 11:09:21 +1000706channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
707{
708 /* test whether sockets are 'alive' for read/write */
709 if (c->istate == CHAN_INPUT_OPEN)
710 if (buffer_len(&c->input) < packet_get_maxsize())
711 FD_SET(c->sock, readset);
712 if (c->ostate == CHAN_OUTPUT_OPEN ||
713 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
714 if (buffer_len(&c->output) > 0) {
715 FD_SET(c->sock, writeset);
716 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
717 chan_obuf_empty(c);
718 }
719 }
720}
721
Ben Lindstrombba81212001-06-25 05:01:22 +0000722static void
Damien Miller33b13562000-04-04 14:38:59 +1000723channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
724{
725 if (c->istate == CHAN_INPUT_OPEN &&
726 c->remote_window > 0 &&
727 buffer_len(&c->input) < c->remote_window)
728 FD_SET(c->rfd, 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->wfd, writeset);
733 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
734 chan_obuf_empty(c);
735 }
736 }
737 /** XXX check close conditions, too */
738 if (c->efd != -1) {
739 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
740 buffer_len(&c->extended) > 0)
741 FD_SET(c->efd, writeset);
742 else if (c->extended_usage == CHAN_EXTENDED_READ &&
743 buffer_len(&c->extended) < c->remote_window)
744 FD_SET(c->efd, readset);
745 }
746}
747
Ben Lindstrombba81212001-06-25 05:01:22 +0000748static void
Damien Millerb38eff82000-04-01 11:09:21 +1000749channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
750{
751 if (buffer_len(&c->input) == 0) {
752 packet_start(SSH_MSG_CHANNEL_CLOSE);
753 packet_put_int(c->remote_id);
754 packet_send();
755 c->type = SSH_CHANNEL_CLOSED;
Ben Lindstromc486d882001-04-11 16:08:34 +0000756 debug("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000757 }
758}
759
Ben Lindstrombba81212001-06-25 05:01:22 +0000760static void
Damien Millerb38eff82000-04-01 11:09:21 +1000761channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
762{
763 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000764 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000765 else
Damien Millerb38eff82000-04-01 11:09:21 +1000766 FD_SET(c->sock, writeset);
767}
768
769/*
770 * This is a special state for X11 authentication spoofing. An opened X11
771 * connection (when authentication spoofing is being done) remains in this
772 * state until the first packet has been completely read. The authentication
773 * data in that packet is then substituted by the real data if it matches the
774 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000775 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000776 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000777 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000778static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000779x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000780{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000781 u_char *ucp;
782 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000783
784 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000785 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000786 return 0;
787
788 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100789 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000790 if (ucp[0] == 0x42) { /* Byte order MSB first. */
791 proto_len = 256 * ucp[6] + ucp[7];
792 data_len = 256 * ucp[8] + ucp[9];
793 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
794 proto_len = ucp[6] + 256 * ucp[7];
795 data_len = ucp[8] + 256 * ucp[9];
796 } else {
797 debug("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100798 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000799 return -1;
800 }
801
802 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000803 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000804 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
805 return 0;
806
807 /* Check if authentication protocol matches. */
808 if (proto_len != strlen(x11_saved_proto) ||
809 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
810 debug("X11 connection uses different authentication protocol.");
811 return -1;
812 }
813 /* Check if authentication data matches our fake data. */
814 if (data_len != x11_fake_data_len ||
815 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
816 x11_fake_data, x11_fake_data_len) != 0) {
817 debug("X11 auth data does not match fake data.");
818 return -1;
819 }
820 /* Check fake data length */
821 if (x11_fake_data_len != x11_saved_data_len) {
822 error("X11 fake_data_len %d != saved_data_len %d",
823 x11_fake_data_len, x11_saved_data_len);
824 return -1;
825 }
826 /*
827 * Received authentication protocol and data match
828 * our fake data. Substitute the fake data with real
829 * data.
830 */
831 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
832 x11_saved_data, x11_saved_data_len);
833 return 1;
834}
835
Ben Lindstrombba81212001-06-25 05:01:22 +0000836static void
Damien Millerb38eff82000-04-01 11:09:21 +1000837channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
838{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000839 int ret = x11_open_helper(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000840 if (ret == 1) {
841 /* Start normal processing for the channel. */
842 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000843 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000844 } else if (ret == -1) {
845 /*
846 * We have received an X11 connection that has bad
847 * authentication information.
848 */
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000849 log("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000850 buffer_clear(&c->input);
851 buffer_clear(&c->output);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000852 channel_close_fd(&c->sock);
Damien Millerb38eff82000-04-01 11:09:21 +1000853 c->sock = -1;
854 c->type = SSH_CHANNEL_CLOSED;
855 packet_start(SSH_MSG_CHANNEL_CLOSE);
856 packet_put_int(c->remote_id);
857 packet_send();
858 }
859}
860
Ben Lindstrombba81212001-06-25 05:01:22 +0000861static void
Damien Millerbd483e72000-04-30 10:00:53 +1000862channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000863{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000864 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000865
866 /* c->force_drain = 1; */
867
Damien Millerb38eff82000-04-01 11:09:21 +1000868 if (ret == 1) {
869 c->type = SSH_CHANNEL_OPEN;
Damien Miller30c3d422000-05-09 11:02:59 +1000870 if (compat20)
871 channel_pre_open_20(c, readset, writeset);
872 else
873 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000874 } else if (ret == -1) {
Damien Millera90fc082002-01-22 23:19:38 +1100875 log("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000876 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +1100877 chan_read_failed(c);
878 buffer_clear(&c->input);
879 chan_ibuf_empty(c);
880 buffer_clear(&c->output);
881 /* for proto v1, the peer will send an IEOF */
882 if (compat20)
883 chan_write_failed(c);
884 else
885 c->type = SSH_CHANNEL_OPEN;
Damien Millerb38eff82000-04-01 11:09:21 +1000886 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
887 }
888}
889
Ben Lindstromb3921512001-04-11 15:57:50 +0000890/* try to decode a socks4 header */
Ben Lindstrombba81212001-06-25 05:01:22 +0000891static int
Ben Lindstromb3921512001-04-11 15:57:50 +0000892channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000893{
894 u_char *p, *host;
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000895 int len, have, i, found;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100896 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000897 struct {
898 u_int8_t version;
899 u_int8_t command;
900 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000901 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000902 } s4_req, s4_rsp;
903
Ben Lindstromb3921512001-04-11 15:57:50 +0000904 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000905
906 have = buffer_len(&c->input);
907 len = sizeof(s4_req);
908 if (have < len)
909 return 0;
910 p = buffer_ptr(&c->input);
911 for (found = 0, i = len; i < have; i++) {
912 if (p[i] == '\0') {
913 found = 1;
914 break;
915 }
916 if (i > 1024) {
917 /* the peer is probably sending garbage */
918 debug("channel %d: decode socks4: too long",
919 c->self);
920 return -1;
921 }
922 }
923 if (!found)
924 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000925 buffer_get(&c->input, (char *)&s4_req.version, 1);
926 buffer_get(&c->input, (char *)&s4_req.command, 1);
927 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000928 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000929 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000930 p = buffer_ptr(&c->input);
931 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000932 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000933 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000934 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000935 c->self, len, have);
936 strlcpy(username, p, sizeof(username));
937 buffer_consume(&c->input, len);
938 buffer_consume(&c->input, 1); /* trailing '\0' */
939
Ben Lindstromb3921512001-04-11 15:57:50 +0000940 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000941 strlcpy(c->path, host, sizeof(c->path));
942 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100943
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000944 debug("channel %d: dynamic request: socks4 host %s port %u command %u",
945 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000946
Ben Lindstromb3921512001-04-11 15:57:50 +0000947 if (s4_req.command != 1) {
948 debug("channel %d: cannot handle: socks4 cn %d",
949 c->self, s4_req.command);
950 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000951 }
Ben Lindstromb3921512001-04-11 15:57:50 +0000952 s4_rsp.version = 0; /* vn: 0 for reply */
953 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000954 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +0000955 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000956 buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +0000957 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000958}
959
Ben Lindstromb3921512001-04-11 15:57:50 +0000960/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +0000961static void
Ben Lindstromb3921512001-04-11 15:57:50 +0000962channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
963{
964 u_char *p;
965 int have, ret;
966
967 have = buffer_len(&c->input);
Damien Miller4623a752001-10-10 15:03:58 +1000968 c->delayed = 0;
Ben Lindstromb3921512001-04-11 15:57:50 +0000969 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +0000970 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +0000971 /* check if the fixed size part of the packet is in buffer. */
972 if (have < 4) {
973 /* need more */
974 FD_SET(c->sock, readset);
975 return;
976 }
977 /* try to guess the protocol */
978 p = buffer_ptr(&c->input);
979 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000980 case 0x04:
981 ret = channel_decode_socks4(c, readset, writeset);
982 break;
Ben Lindstromb3921512001-04-11 15:57:50 +0000983 default:
984 ret = -1;
985 break;
986 }
987 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000988 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +0000989 } else if (ret == 0) {
990 debug2("channel %d: pre_dynamic: need more", c->self);
991 /* need more */
992 FD_SET(c->sock, readset);
993 } else {
994 /* switch to the next state */
995 c->type = SSH_CHANNEL_OPENING;
996 port_open_helper(c, "direct-tcpip");
997 }
998}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000999
Damien Millerb38eff82000-04-01 11:09:21 +10001000/* This is our fake X11 server socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +00001001static void
Damien Millerb38eff82000-04-01 11:09:21 +10001002channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
1003{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001004 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001005 struct sockaddr addr;
Ben Lindstrom73f57be2001-12-07 17:28:34 +00001006 int newsock, on = 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001007 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001008 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001009 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001010
1011 if (FD_ISSET(c->sock, readset)) {
1012 debug("X11 connection requested.");
1013 addrlen = sizeof(addr);
1014 newsock = accept(c->sock, &addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001015 if (c->single_connection) {
1016 debug("single_connection: closing X11 listener.");
1017 channel_close_fd(&c->sock);
1018 chan_mark_dead(c);
1019 }
Damien Millerb38eff82000-04-01 11:09:21 +10001020 if (newsock < 0) {
1021 error("accept: %.100s", strerror(errno));
1022 return;
1023 }
Ben Lindstrom73f57be2001-12-07 17:28:34 +00001024 if (setsockopt(newsock, IPPROTO_TCP, TCP_NODELAY, &on,
1025 sizeof on) == -1)
1026 error("setsockopt TCP_NODELAY: %.100s",
1027 strerror(errno));
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 Millere7378562001-12-21 14:58:35 +11001046 packet_put_int(nc->local_window_max);
1047 packet_put_int(nc->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
Damien Miller4623a752001-10-10 15:03:58 +10001136 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1137 nextstate = SSH_CHANNEL_OPENING;
1138 rtype = "forwarded-tcpip";
1139 } else {
1140 if (c->host_port == 0) {
1141 nextstate = SSH_CHANNEL_DYNAMIC;
Damien Millerd3c04b92001-10-10 15:04:20 +10001142 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001143 } else {
1144 nextstate = SSH_CHANNEL_OPENING;
1145 rtype = "direct-tcpip";
1146 }
1147 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001148
Damien Millerb38eff82000-04-01 11:09:21 +10001149 addrlen = sizeof(addr);
1150 newsock = accept(c->sock, &addr, &addrlen);
1151 if (newsock < 0) {
1152 error("accept: %.100s", strerror(errno));
1153 return;
1154 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001155 nc = channel_new(rtype,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001156 nextstate, newsock, newsock, -1,
Damien Millerb38eff82000-04-01 11:09:21 +10001157 c->local_window_max, c->local_maxpacket,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001158 0, xstrdup(rtype), 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001159 if (nc == NULL) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001160 error("channel_post_port_listener: no new channel:");
1161 close(newsock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001162 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001163 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001164 nc->listening_port = c->listening_port;
1165 nc->host_port = c->host_port;
1166 strlcpy(nc->path, c->path, sizeof(nc->path));
1167
Damien Miller4623a752001-10-10 15:03:58 +10001168 if (nextstate == SSH_CHANNEL_DYNAMIC) {
1169 /*
1170 * do not call the channel_post handler until
1171 * this flag has been reset by a pre-handler.
1172 * otherwise the FD_ISSET calls might overflow
1173 */
1174 nc->delayed = 1;
1175 } else {
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001176 port_open_helper(nc, rtype);
Damien Miller4623a752001-10-10 15:03:58 +10001177 }
Damien Millerb38eff82000-04-01 11:09:21 +10001178 }
1179}
1180
1181/*
1182 * This is the authentication agent socket listening for connections from
1183 * clients.
1184 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001185static void
Damien Millerb38eff82000-04-01 11:09:21 +10001186channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
1187{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001188 Channel *nc;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001189 char *name;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001190 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001191 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001192 socklen_t addrlen;
1193
1194 if (FD_ISSET(c->sock, readset)) {
1195 addrlen = sizeof(addr);
1196 newsock = accept(c->sock, &addr, &addrlen);
1197 if (newsock < 0) {
1198 error("accept from auth socket: %.100s", strerror(errno));
1199 return;
1200 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001201 name = xstrdup("accepted auth socket");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001202 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001203 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1204 c->local_window_max, c->local_maxpacket,
Ben Lindstrome9c99912001-06-09 00:41:05 +00001205 0, name, 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001206 if (nc == NULL) {
1207 error("channel_post_auth_listener: channel_new failed");
Ben Lindstrome9c99912001-06-09 00:41:05 +00001208 xfree(name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001209 close(newsock);
1210 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001211 if (compat20) {
1212 packet_start(SSH2_MSG_CHANNEL_OPEN);
1213 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001214 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001215 packet_put_int(c->local_window_max);
1216 packet_put_int(c->local_maxpacket);
1217 } else {
1218 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001219 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001220 }
Damien Millerb38eff82000-04-01 11:09:21 +10001221 packet_send();
1222 }
1223}
1224
Ben Lindstrombba81212001-06-25 05:01:22 +00001225static void
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001226channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
1227{
Ben Lindstrom69128662001-05-08 20:07:39 +00001228 int err = 0;
Ben Lindstrom11180952001-07-04 05:13:35 +00001229 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001230
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001231 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001232 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err,
1233 &sz) < 0) {
1234 err = errno;
1235 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001236 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001237 if (err == 0) {
1238 debug("channel %d: connected", c->self);
1239 c->type = SSH_CHANNEL_OPEN;
1240 if (compat20) {
1241 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1242 packet_put_int(c->remote_id);
1243 packet_put_int(c->self);
1244 packet_put_int(c->local_window);
1245 packet_put_int(c->local_maxpacket);
1246 } else {
1247 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1248 packet_put_int(c->remote_id);
1249 packet_put_int(c->self);
1250 }
1251 } else {
1252 debug("channel %d: not connected: %s",
1253 c->self, strerror(err));
1254 if (compat20) {
1255 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1256 packet_put_int(c->remote_id);
1257 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1258 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1259 packet_put_cstring(strerror(err));
1260 packet_put_cstring("");
1261 }
1262 } else {
1263 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1264 packet_put_int(c->remote_id);
1265 }
1266 chan_mark_dead(c);
1267 }
1268 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001269 }
1270}
1271
Ben Lindstrombba81212001-06-25 05:01:22 +00001272static int
Damien Millerb38eff82000-04-01 11:09:21 +10001273channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
1274{
1275 char buf[16*1024];
1276 int len;
1277
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001278 if (c->rfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001279 FD_ISSET(c->rfd, readset)) {
1280 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +10001281 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1282 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001283 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +10001284 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001285 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001286 if (c->type != SSH_CHANNEL_OPEN) {
1287 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001288 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001289 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001290 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001291 buffer_consume(&c->output, buffer_len(&c->output));
1292 c->type = SSH_CHANNEL_INPUT_DRAINING;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001293 debug("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001294 } else {
1295 chan_read_failed(c);
1296 }
1297 return -1;
1298 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001299 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001300 if (c->input_filter(c, buf, len) == -1) {
Ben Lindstromc486d882001-04-11 16:08:34 +00001301 debug("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001302 chan_read_failed(c);
1303 }
1304 } else {
1305 buffer_append(&c->input, buf, len);
1306 }
Damien Millerb38eff82000-04-01 11:09:21 +10001307 }
1308 return 1;
1309}
Ben Lindstrombba81212001-06-25 05:01:22 +00001310static int
Damien Millerb38eff82000-04-01 11:09:21 +10001311channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
1312{
Ben Lindstrome229b252001-03-05 06:28:06 +00001313 struct termios tio;
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001314 u_char *data;
1315 u_int dlen;
Damien Millerb38eff82000-04-01 11:09:21 +10001316 int len;
1317
1318 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001319 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001320 FD_ISSET(c->wfd, writeset) &&
1321 buffer_len(&c->output) > 0) {
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001322 data = buffer_ptr(&c->output);
1323 dlen = buffer_len(&c->output);
1324 len = write(c->wfd, data, dlen);
Damien Miller6f83b8e2000-05-02 09:23:45 +10001325 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1326 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001327 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001328 if (c->type != SSH_CHANNEL_OPEN) {
1329 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001330 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001331 return -1;
1332 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001333 buffer_consume(&c->output, buffer_len(&c->output));
Ben Lindstrome9c99912001-06-09 00:41:05 +00001334 debug("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001335 c->type = SSH_CHANNEL_INPUT_DRAINING;
1336 } else {
1337 chan_write_failed(c);
1338 }
1339 return -1;
1340 }
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001341 if (compat20 && c->isatty && dlen >= 1 && data[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001342 if (tcgetattr(c->wfd, &tio) == 0 &&
1343 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1344 /*
1345 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001346 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001347 * size of a SSH2_MSG_CHANNEL_DATA message
1348 * (4 byte channel id + data)
Damien Miller79438cc2001-02-16 12:34:57 +11001349 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001350 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001351 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001352 }
1353 }
Damien Millerb38eff82000-04-01 11:09:21 +10001354 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001355 if (compat20 && len > 0) {
1356 c->local_consumed += len;
1357 }
1358 }
1359 return 1;
1360}
Ben Lindstrombba81212001-06-25 05:01:22 +00001361static int
Damien Miller33b13562000-04-04 14:38:59 +10001362channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
1363{
1364 char buf[16*1024];
1365 int len;
1366
Damien Miller1383bd82000-04-06 12:32:37 +10001367/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001368 if (c->efd != -1) {
1369 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1370 FD_ISSET(c->efd, writeset) &&
1371 buffer_len(&c->extended) > 0) {
1372 len = write(c->efd, buffer_ptr(&c->extended),
1373 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001374 debug2("channel %d: written %d to 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 write-efd %d",
1380 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_consume(&c->extended, len);
1384 c->local_consumed += len;
1385 }
1386 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1387 FD_ISSET(c->efd, readset)) {
1388 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001389 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001390 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001391 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1392 return 1;
1393 if (len <= 0) {
1394 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001395 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001396 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001397 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001398 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001399 }
Damien Miller33b13562000-04-04 14:38:59 +10001400 }
1401 }
1402 return 1;
1403}
Ben Lindstrombba81212001-06-25 05:01:22 +00001404static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001405channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001406{
Ben Lindstromb3921512001-04-11 15:57:50 +00001407 if (c->type == SSH_CHANNEL_OPEN &&
1408 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001409 c->local_window < c->local_window_max/2 &&
1410 c->local_consumed > 0) {
1411 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1412 packet_put_int(c->remote_id);
1413 packet_put_int(c->local_consumed);
1414 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001415 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001416 c->self, c->local_window,
1417 c->local_consumed);
1418 c->local_window += c->local_consumed;
1419 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001420 }
1421 return 1;
1422}
1423
Ben Lindstrombba81212001-06-25 05:01:22 +00001424static void
Damien Millerb38eff82000-04-01 11:09:21 +10001425channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
1426{
Damien Miller4623a752001-10-10 15:03:58 +10001427 if (c->delayed)
1428 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001429 channel_handle_rfd(c, readset, writeset);
1430 channel_handle_wfd(c, readset, writeset);
1431}
1432
Ben Lindstrombba81212001-06-25 05:01:22 +00001433static void
Damien Miller33b13562000-04-04 14:38:59 +10001434channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
1435{
Damien Miller4623a752001-10-10 15:03:58 +10001436 if (c->delayed)
1437 return;
Damien Miller33b13562000-04-04 14:38:59 +10001438 channel_handle_rfd(c, readset, writeset);
1439 channel_handle_wfd(c, readset, writeset);
1440 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001441
1442 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001443}
1444
Ben Lindstrombba81212001-06-25 05:01:22 +00001445static void
Damien Millerb38eff82000-04-01 11:09:21 +10001446channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1447{
1448 int len;
1449 /* Send buffered output data to the socket. */
1450 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1451 len = write(c->sock, buffer_ptr(&c->output),
1452 buffer_len(&c->output));
1453 if (len <= 0)
1454 buffer_consume(&c->output, buffer_len(&c->output));
1455 else
1456 buffer_consume(&c->output, len);
1457 }
1458}
1459
Ben Lindstrombba81212001-06-25 05:01:22 +00001460static void
Damien Miller33b13562000-04-04 14:38:59 +10001461channel_handler_init_20(void)
1462{
1463 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +10001464 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001465 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001466 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001467 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001468 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001469 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001470 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001471
1472 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
1473 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001474 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001475 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001476 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001477 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001478 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_2;
Damien Miller33b13562000-04-04 14:38:59 +10001479}
1480
Ben Lindstrombba81212001-06-25 05:01:22 +00001481static void
Damien Millerb38eff82000-04-01 11:09:21 +10001482channel_handler_init_13(void)
1483{
1484 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1485 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1486 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1487 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1488 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1489 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1490 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001491 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001492 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001493
1494 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
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_OUTPUT_DRAINING] = &channel_post_output_drain_13;
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_15(void)
1505{
1506 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +10001507 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001508 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1509 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1510 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001511 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001512 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001513
1514 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1515 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1516 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1517 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001518 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001519 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001520}
1521
Ben Lindstrombba81212001-06-25 05:01:22 +00001522static void
Damien Millerb38eff82000-04-01 11:09:21 +10001523channel_handler_init(void)
1524{
1525 int i;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001526 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001527 channel_pre[i] = NULL;
1528 channel_post[i] = NULL;
1529 }
Damien Miller33b13562000-04-04 14:38:59 +10001530 if (compat20)
1531 channel_handler_init_20();
1532 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001533 channel_handler_init_13();
1534 else
1535 channel_handler_init_15();
1536}
1537
Damien Miller3ec27592001-10-12 11:35:04 +10001538/* gc dead channels */
1539static void
1540channel_garbage_collect(Channel *c)
1541{
1542 if (c == NULL)
1543 return;
1544 if (c->detach_user != NULL) {
1545 if (!chan_is_dead(c, 0))
1546 return;
1547 debug("channel %d: gc: notify user", c->self);
1548 c->detach_user(c->self, NULL);
1549 /* if we still have a callback */
1550 if (c->detach_user != NULL)
1551 return;
1552 debug("channel %d: gc: user detached", c->self);
1553 }
1554 if (!chan_is_dead(c, 1))
1555 return;
1556 debug("channel %d: garbage collecting", c->self);
1557 channel_free(c);
1558}
1559
Ben Lindstrombba81212001-06-25 05:01:22 +00001560static void
Damien Millerb38eff82000-04-01 11:09:21 +10001561channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1562{
1563 static int did_init = 0;
1564 int i;
1565 Channel *c;
1566
1567 if (!did_init) {
1568 channel_handler_init();
1569 did_init = 1;
1570 }
1571 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001572 c = channels[i];
1573 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001574 continue;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001575 if (ftab[c->type] != NULL)
1576 (*ftab[c->type])(c, readset, writeset);
Damien Miller3ec27592001-10-12 11:35:04 +10001577 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001578 }
1579}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001580
Ben Lindstrome9c99912001-06-09 00:41:05 +00001581/*
1582 * Allocate/update select bitmasks and add any bits relevant to channels in
1583 * select bitmasks.
1584 */
Damien Miller4af51302000-04-16 11:18:38 +10001585void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001586channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001587 int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001588{
Damien Miller5e953212001-01-30 09:14:00 +11001589 int n;
1590 u_int sz;
1591
1592 n = MAX(*maxfdp, channel_max_fd);
1593
1594 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001595 /* perhaps check sz < nalloc/2 and shrink? */
1596 if (*readsetp == NULL || sz > *nallocp) {
1597 *readsetp = xrealloc(*readsetp, sz);
1598 *writesetp = xrealloc(*writesetp, sz);
1599 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11001600 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001601 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11001602 memset(*readsetp, 0, sz);
1603 memset(*writesetp, 0, sz);
1604
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001605 if (!rekeying)
1606 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001607}
1608
Ben Lindstrome9c99912001-06-09 00:41:05 +00001609/*
1610 * After select, perform any appropriate operations for channels which have
1611 * events pending.
1612 */
Damien Miller4af51302000-04-16 11:18:38 +10001613void
Damien Miller95def091999-11-25 00:26:21 +11001614channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001615{
Damien Millerb38eff82000-04-01 11:09:21 +10001616 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001617}
1618
Ben Lindstrome9c99912001-06-09 00:41:05 +00001619
Damien Miller5e953212001-01-30 09:14:00 +11001620/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001621
Damien Miller4af51302000-04-16 11:18:38 +10001622void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001623channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001624{
Damien Miller95def091999-11-25 00:26:21 +11001625 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001626 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001627
Damien Miller95def091999-11-25 00:26:21 +11001628 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001629 c = channels[i];
1630 if (c == NULL)
1631 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001632
Ben Lindstrome9c99912001-06-09 00:41:05 +00001633 /*
1634 * We are only interested in channels that can have buffered
1635 * incoming data.
1636 */
Damien Miller34132e52000-01-14 15:45:46 +11001637 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001638 if (c->type != SSH_CHANNEL_OPEN &&
1639 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001640 continue;
1641 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001642 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001643 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001644 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001645 if (compat20 &&
1646 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001647 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10001648 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001649 continue;
1650 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001651
Damien Miller95def091999-11-25 00:26:21 +11001652 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001653 if ((c->istate == CHAN_INPUT_OPEN ||
1654 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1655 (len = buffer_len(&c->input)) > 0) {
Ben Lindstrome9c99912001-06-09 00:41:05 +00001656 /*
1657 * Send some data for the other side over the secure
1658 * connection.
1659 */
Damien Miller33b13562000-04-04 14:38:59 +10001660 if (compat20) {
1661 if (len > c->remote_window)
1662 len = c->remote_window;
1663 if (len > c->remote_maxpacket)
1664 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001665 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001666 if (packet_is_interactive()) {
1667 if (len > 1024)
1668 len = 512;
1669 } else {
1670 /* Keep the packets at reasonable size. */
1671 if (len > packet_get_maxsize()/2)
1672 len = packet_get_maxsize()/2;
1673 }
Damien Miller95def091999-11-25 00:26:21 +11001674 }
Damien Millerb38eff82000-04-01 11:09:21 +10001675 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001676 packet_start(compat20 ?
1677 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001678 packet_put_int(c->remote_id);
1679 packet_put_string(buffer_ptr(&c->input), len);
1680 packet_send();
1681 buffer_consume(&c->input, len);
1682 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001683 }
1684 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001685 if (compat13)
1686 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001687 /*
1688 * input-buffer is empty and read-socket shutdown:
1689 * tell peer, that we will not send more data: send IEOF
1690 */
Damien Millerb38eff82000-04-01 11:09:21 +10001691 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001692 }
Damien Miller33b13562000-04-04 14:38:59 +10001693 /* Send extended data, i.e. stderr */
1694 if (compat20 &&
1695 c->remote_window > 0 &&
1696 (len = buffer_len(&c->extended)) > 0 &&
1697 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001698 debug2("channel %d: rwin %d elen %d euse %d",
1699 c->self, c->remote_window, buffer_len(&c->extended),
1700 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001701 if (len > c->remote_window)
1702 len = c->remote_window;
1703 if (len > c->remote_maxpacket)
1704 len = c->remote_maxpacket;
1705 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1706 packet_put_int(c->remote_id);
1707 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1708 packet_put_string(buffer_ptr(&c->extended), len);
1709 packet_send();
1710 buffer_consume(&c->extended, len);
1711 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001712 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001713 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001714 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001715}
1716
Ben Lindstrome9c99912001-06-09 00:41:05 +00001717
1718/* -- protocol input */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001719
Damien Miller4af51302000-04-16 11:18:38 +10001720void
Damien Miller630d6f42002-01-22 23:17:30 +11001721channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001722{
Damien Miller34132e52000-01-14 15:45:46 +11001723 int id;
Damien Miller95def091999-11-25 00:26:21 +11001724 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001725 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001726 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001727
Damien Miller95def091999-11-25 00:26:21 +11001728 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001729 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001730 c = channel_lookup(id);
1731 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001732 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001733
Damien Miller95def091999-11-25 00:26:21 +11001734 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001735 if (c->type != SSH_CHANNEL_OPEN &&
1736 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001737 return;
1738
1739 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001740 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001741 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001742
Damien Miller95def091999-11-25 00:26:21 +11001743 /* Get the data. */
1744 data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10001745
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001746 if (compat20) {
Damien Miller33b13562000-04-04 14:38:59 +10001747 if (data_len > c->local_maxpacket) {
1748 log("channel %d: rcvd big packet %d, maxpack %d",
1749 c->self, data_len, c->local_maxpacket);
1750 }
1751 if (data_len > c->local_window) {
1752 log("channel %d: rcvd too much data %d, win %d",
1753 c->self, data_len, c->local_window);
1754 xfree(data);
1755 return;
1756 }
1757 c->local_window -= data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001758 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001759 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001760 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001761 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001762}
Ben Lindstrome9c99912001-06-09 00:41:05 +00001763
Damien Miller4af51302000-04-16 11:18:38 +10001764void
Damien Miller630d6f42002-01-22 23:17:30 +11001765channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001766{
1767 int id;
1768 int tcode;
1769 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001770 u_int data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001771 Channel *c;
1772
1773 /* Get the channel number and verify it. */
1774 id = packet_get_int();
1775 c = channel_lookup(id);
1776
1777 if (c == NULL)
1778 packet_disconnect("Received extended_data for bad channel %d.", id);
1779 if (c->type != SSH_CHANNEL_OPEN) {
1780 log("channel %d: ext data for non open", id);
1781 return;
1782 }
1783 tcode = packet_get_int();
1784 if (c->efd == -1 ||
1785 c->extended_usage != CHAN_EXTENDED_WRITE ||
1786 tcode != SSH2_EXTENDED_DATA_STDERR) {
1787 log("channel %d: bad ext data", c->self);
1788 return;
1789 }
1790 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11001791 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10001792 if (data_len > c->local_window) {
1793 log("channel %d: rcvd too much extended_data %d, win %d",
1794 c->self, data_len, c->local_window);
1795 xfree(data);
1796 return;
1797 }
Damien Millerd3444942000-09-30 14:20:03 +11001798 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001799 c->local_window -= data_len;
1800 buffer_append(&c->extended, data, data_len);
1801 xfree(data);
1802}
1803
Damien Miller4af51302000-04-16 11:18:38 +10001804void
Damien Miller630d6f42002-01-22 23:17:30 +11001805channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001806{
1807 int id;
1808 Channel *c;
1809
Damien Millerb38eff82000-04-01 11:09:21 +10001810 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001811 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001812 c = channel_lookup(id);
1813 if (c == NULL)
1814 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1815 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001816
1817 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11001818 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001819 debug("channel %d: FORCE input drain", c->self);
1820 c->istate = CHAN_INPUT_WAIT_DRAIN;
1821 }
1822
Damien Millerb38eff82000-04-01 11:09:21 +10001823}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001824
Damien Miller4af51302000-04-16 11:18:38 +10001825void
Damien Miller630d6f42002-01-22 23:17:30 +11001826channel_input_close(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001827{
Damien Millerb38eff82000-04-01 11:09:21 +10001828 int id;
1829 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001830
Damien Millerb38eff82000-04-01 11:09:21 +10001831 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11001832 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001833 c = channel_lookup(id);
1834 if (c == NULL)
1835 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001836
1837 /*
1838 * Send a confirmation that we have closed the channel and no more
1839 * data is coming for it.
1840 */
Damien Miller95def091999-11-25 00:26:21 +11001841 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001842 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001843 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001844
Damien Miller5428f641999-11-25 11:54:57 +11001845 /*
1846 * If the channel is in closed state, we have sent a close request,
1847 * and the other side will eventually respond with a confirmation.
1848 * Thus, we cannot free the channel here, because then there would be
1849 * no-one to receive the confirmation. The channel gets freed when
1850 * the confirmation arrives.
1851 */
Damien Millerb38eff82000-04-01 11:09:21 +10001852 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001853 /*
1854 * Not a closed channel - mark it as draining, which will
1855 * cause it to be freed later.
1856 */
Damien Millerb38eff82000-04-01 11:09:21 +10001857 buffer_consume(&c->input, buffer_len(&c->input));
1858 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001859 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001860}
1861
Damien Millerb38eff82000-04-01 11:09:21 +10001862/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001863void
Damien Miller630d6f42002-01-22 23:17:30 +11001864channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001865{
Damien Millerb38eff82000-04-01 11:09:21 +10001866 int id = packet_get_int();
1867 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11001868
Damien Miller48b03fc2002-01-22 23:11:40 +11001869 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001870 if (c == NULL)
1871 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1872 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001873}
1874
Damien Miller4af51302000-04-16 11:18:38 +10001875void
Damien Miller630d6f42002-01-22 23:17:30 +11001876channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001877{
1878 int id = packet_get_int();
1879 Channel *c = channel_lookup(id);
1880
Damien Miller48b03fc2002-01-22 23:11:40 +11001881 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10001882 if (c == NULL)
1883 packet_disconnect("Received close confirmation for "
1884 "out-of-range channel %d.", id);
1885 if (c->type != SSH_CHANNEL_CLOSED)
1886 packet_disconnect("Received close confirmation for "
1887 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001888 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001889}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001890
Damien Miller4af51302000-04-16 11:18:38 +10001891void
Damien Miller630d6f42002-01-22 23:17:30 +11001892channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001893{
Damien Millerb38eff82000-04-01 11:09:21 +10001894 int id, remote_id;
1895 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001896
Damien Millerb38eff82000-04-01 11:09:21 +10001897 id = packet_get_int();
1898 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001899
Damien Millerb38eff82000-04-01 11:09:21 +10001900 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1901 packet_disconnect("Received open confirmation for "
1902 "non-opening channel %d.", id);
1903 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001904 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001905 c->remote_id = remote_id;
1906 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001907
1908 if (compat20) {
1909 c->remote_window = packet_get_int();
1910 c->remote_maxpacket = packet_get_int();
1911 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001912 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001913 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001914 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001915 }
1916 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1917 c->remote_window, c->remote_maxpacket);
1918 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001919 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001920}
1921
Ben Lindstrombba81212001-06-25 05:01:22 +00001922static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00001923reason2txt(int reason)
1924{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001925 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001926 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
1927 return "administratively prohibited";
1928 case SSH2_OPEN_CONNECT_FAILED:
1929 return "connect failed";
1930 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
1931 return "unknown channel type";
1932 case SSH2_OPEN_RESOURCE_SHORTAGE:
1933 return "resource shortage";
1934 }
Ben Lindstrome2595442001-06-05 20:01:39 +00001935 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00001936}
1937
Damien Miller4af51302000-04-16 11:18:38 +10001938void
Damien Miller630d6f42002-01-22 23:17:30 +11001939channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001940{
Kevin Steves12057502001-02-05 14:54:34 +00001941 int id, reason;
1942 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001943 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001944
Damien Millerb38eff82000-04-01 11:09:21 +10001945 id = packet_get_int();
1946 c = channel_lookup(id);
1947
1948 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1949 packet_disconnect("Received open failure for "
1950 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001951 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00001952 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00001953 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00001954 msg = packet_get_string(NULL);
1955 lang = packet_get_string(NULL);
1956 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001957 log("channel %d: open failed: %s%s%s", id,
1958 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00001959 if (msg != NULL)
1960 xfree(msg);
1961 if (lang != NULL)
1962 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001963 }
Damien Miller48b03fc2002-01-22 23:11:40 +11001964 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11001965 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001966 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001967}
1968
Damien Miller33b13562000-04-04 14:38:59 +10001969void
Damien Miller630d6f42002-01-22 23:17:30 +11001970channel_input_channel_request(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001971{
1972 int id;
1973 Channel *c;
1974
1975 id = packet_get_int();
1976 c = channel_lookup(id);
1977
1978 if (c == NULL ||
1979 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1980 packet_disconnect("Received request for "
1981 "non-open channel %d.", id);
1982 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001983 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001984 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001985 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001986 } else {
1987 char *service = packet_get_string(NULL);
Ben Lindstromcc74df72001-03-05 06:20:14 +00001988 debug("channel %d: rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001989 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001990 xfree(service);
1991 }
1992}
1993
Damien Miller4af51302000-04-16 11:18:38 +10001994void
Damien Miller630d6f42002-01-22 23:17:30 +11001995channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001996{
1997 Channel *c;
1998 int id, adjust;
1999
2000 if (!compat20)
2001 return;
2002
2003 /* Get the channel number and verify it. */
2004 id = packet_get_int();
2005 c = channel_lookup(id);
2006
2007 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
2008 log("Received window adjust for "
2009 "non-open channel %d.", id);
2010 return;
2011 }
2012 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002013 packet_check_eom();
Damien Millerd3444942000-09-30 14:20:03 +11002014 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10002015 c->remote_window += adjust;
2016}
2017
Damien Miller4af51302000-04-16 11:18:38 +10002018void
Damien Miller630d6f42002-01-22 23:17:30 +11002019channel_input_port_open(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002020{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002021 Channel *c = NULL;
2022 u_short host_port;
2023 char *host, *originator_string;
2024 int remote_id, sock = -1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002025
Ben Lindstrome9c99912001-06-09 00:41:05 +00002026 remote_id = packet_get_int();
2027 host = packet_get_string(NULL);
2028 host_port = packet_get_int();
2029
2030 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2031 originator_string = packet_get_string(NULL);
2032 } else {
2033 originator_string = xstrdup("unknown (remote did not supply name)");
2034 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002035 packet_check_eom();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002036 sock = channel_connect_to(host, host_port);
2037 if (sock != -1) {
2038 c = channel_new("connected socket",
2039 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2040 originator_string, 1);
2041 if (c == NULL) {
2042 error("channel_input_port_open: channel_new failed");
2043 close(sock);
2044 } else {
2045 c->remote_id = remote_id;
Damien Miller95def091999-11-25 00:26:21 +11002046 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002047 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002048 if (c == NULL) {
2049 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2050 packet_put_int(remote_id);
2051 packet_send();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002052 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002053 xfree(host);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002054}
2055
2056
Ben Lindstrome9c99912001-06-09 00:41:05 +00002057/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002058
Ben Lindstrom908afed2001-10-03 17:34:59 +00002059void
2060channel_set_af(int af)
2061{
2062 IPv4or6 = af;
2063}
2064
Damien Miller5428f641999-11-25 11:54:57 +11002065/*
2066 * Initiate forwarding of connections to local port "port" through the secure
2067 * channel to host:port from remote side.
2068 */
Kevin Steves12057502001-02-05 14:54:34 +00002069int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002070channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
2071 u_short port_to_connect, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002072{
Kevin Steves12057502001-02-05 14:54:34 +00002073 return channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002074 NULL, listen_port,
2075 host_to_connect, port_to_connect,
2076 gateway_ports, /*remote_fwd*/ 0);
2077}
2078
2079/*
2080 * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
2081 * (SSH_CHANNEL_RPORT_LISTENER).
2082 */
Kevin Steves12057502001-02-05 14:54:34 +00002083int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002084channel_request_forwarding(
2085 const char *listen_address, u_short listen_port,
2086 const char *host_to_connect, u_short port_to_connect,
2087 int gateway_ports, int remote_fwd)
2088{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002089 Channel *c;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002090 int success, sock, on = 1, type;
Damien Miller34132e52000-01-14 15:45:46 +11002091 struct addrinfo hints, *ai, *aitop;
2092 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0bc1bd82000-11-13 22:57:25 +11002093 const char *host;
Damien Miller5428f641999-11-25 11:54:57 +11002094 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002095
Kevin Steves12057502001-02-05 14:54:34 +00002096 success = 0;
2097
Damien Miller0bc1bd82000-11-13 22:57:25 +11002098 if (remote_fwd) {
2099 host = listen_address;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002100 type = SSH_CHANNEL_RPORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002101 } else {
2102 host = host_to_connect;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002103 type = SSH_CHANNEL_PORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002104 }
2105
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002106 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00002107 error("Forward host name too long.");
2108 return success;
2109 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002110
Damien Miller0bc1bd82000-11-13 22:57:25 +11002111 /* XXX listen_address is currently ignored */
Damien Miller5428f641999-11-25 11:54:57 +11002112 /*
Damien Miller34132e52000-01-14 15:45:46 +11002113 * getaddrinfo returns a loopback address if the hostname is
2114 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002115 */
Damien Miller34132e52000-01-14 15:45:46 +11002116 memset(&hints, 0, sizeof(hints));
2117 hints.ai_family = IPv4or6;
2118 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
2119 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002120 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002121 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
2122 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11002123
Damien Miller34132e52000-01-14 15:45:46 +11002124 for (ai = aitop; ai; ai = ai->ai_next) {
2125 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2126 continue;
2127 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2128 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11002129 error("channel_request_forwarding: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002130 continue;
2131 }
2132 /* Create a port to listen for the host. */
2133 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2134 if (sock < 0) {
2135 /* this is no error since kernel may not support ipv6 */
2136 verbose("socket: %.100s", strerror(errno));
2137 continue;
2138 }
2139 /*
2140 * Set socket options. We would like the socket to disappear
2141 * as soon as it has been closed for whatever reason.
2142 */
2143 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
2144 linger.l_onoff = 1;
2145 linger.l_linger = 5;
2146 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
2147 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002148
Damien Miller34132e52000-01-14 15:45:46 +11002149 /* Bind the socket to the address. */
2150 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2151 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002152 if (!ai->ai_next)
2153 error("bind: %.100s", strerror(errno));
2154 else
2155 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002156
Damien Miller34132e52000-01-14 15:45:46 +11002157 close(sock);
2158 continue;
2159 }
2160 /* Start listening for connections on the socket. */
2161 if (listen(sock, 5) < 0) {
2162 error("listen: %.100s", strerror(errno));
2163 close(sock);
2164 continue;
2165 }
2166 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002167 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002168 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002169 0, xstrdup("port listener"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002170 if (c == NULL) {
2171 error("channel_request_forwarding: channel_new failed");
2172 close(sock);
2173 continue;
2174 }
2175 strlcpy(c->path, host, sizeof(c->path));
2176 c->host_port = port_to_connect;
2177 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002178 success = 1;
2179 }
2180 if (success == 0)
Kevin Steves12057502001-02-05 14:54:34 +00002181 error("channel_request_forwarding: cannot listen to port: %d",
2182 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002183 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002184 return success;
Damien Miller95def091999-11-25 00:26:21 +11002185}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002186
Damien Miller5428f641999-11-25 11:54:57 +11002187/*
2188 * Initiate forwarding of connections to port "port" on remote host through
2189 * the secure channel to host:port from local side.
2190 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002191
Damien Miller4af51302000-04-16 11:18:38 +10002192void
Damien Miller0bc1bd82000-11-13 22:57:25 +11002193channel_request_remote_forwarding(u_short listen_port,
2194 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002195{
Damien Millerdff50992002-01-22 23:16:32 +11002196 int type, success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002197
Damien Miller95def091999-11-25 00:26:21 +11002198 /* Record locally that connection to this host/port is permitted. */
2199 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2200 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002201
Damien Miller95def091999-11-25 00:26:21 +11002202 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002203 if (compat20) {
2204 const char *address_to_bind = "0.0.0.0";
2205 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2206 packet_put_cstring("tcpip-forward");
2207 packet_put_char(0); /* boolean: want reply */
2208 packet_put_cstring(address_to_bind);
2209 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002210 packet_send();
2211 packet_write_wait();
2212 /* Assume that server accepts the request */
2213 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002214 } else {
2215 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002216 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002217 packet_put_cstring(host_to_connect);
2218 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002219 packet_send();
2220 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002221
2222 /* Wait for response from the remote side. */
Damien Millerdff50992002-01-22 23:16:32 +11002223 type = packet_read();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002224 switch (type) {
2225 case SSH_SMSG_SUCCESS:
2226 success = 1;
2227 break;
2228 case SSH_SMSG_FAILURE:
2229 log("Warning: Server denied remote port forwarding.");
2230 break;
2231 default:
2232 /* Unknown packet */
2233 packet_disconnect("Protocol error for port forward request:"
2234 "received packet type %d.", type);
2235 }
2236 }
2237 if (success) {
2238 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2239 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2240 permitted_opens[num_permitted_opens].listen_port = listen_port;
2241 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002242 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002243}
2244
Damien Miller5428f641999-11-25 11:54:57 +11002245/*
2246 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2247 * listening for the port, and sends back a success reply (or disconnect
2248 * message if there was an error). This never returns if there was an error.
2249 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002250
Damien Miller4af51302000-04-16 11:18:38 +10002251void
Damien Millere247cc42000-05-07 12:03:14 +10002252channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002253{
Damien Milleraae6c611999-12-06 11:47:28 +11002254 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11002255 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002256
Damien Miller95def091999-11-25 00:26:21 +11002257 /* Get arguments from the packet. */
2258 port = packet_get_int();
2259 hostname = packet_get_string(NULL);
2260 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002261
Damien Millerbac2d8a2000-09-05 16:13:06 +11002262#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002263 /*
2264 * Check that an unprivileged user is not trying to forward a
2265 * privileged port.
2266 */
Damien Miller95def091999-11-25 00:26:21 +11002267 if (port < IPPORT_RESERVED && !is_root)
2268 packet_disconnect("Requested forwarding of port %d but user is not root.",
2269 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11002270#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11002271 /* Initiate forwarding */
Damien Millere247cc42000-05-07 12:03:14 +10002272 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002273
2274 /* Free the argument string. */
2275 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002276}
2277
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002278/*
2279 * Permits opening to any host/port if permitted_opens[] is empty. This is
2280 * usually called by the server, because the user could connect to any port
2281 * anyway, and the server has no way to know but to trust the client anyway.
2282 */
2283void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002284channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002285{
2286 if (num_permitted_opens == 0)
2287 all_opens_permitted = 1;
2288}
2289
Ben Lindstroma3700052001-04-05 23:26:32 +00002290void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002291channel_add_permitted_opens(char *host, int port)
2292{
2293 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2294 fatal("channel_request_remote_forwarding: too many forwards");
2295 debug("allow port forwarding to host %s port %d", host, port);
2296
2297 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2298 permitted_opens[num_permitted_opens].port_to_connect = port;
2299 num_permitted_opens++;
2300
2301 all_opens_permitted = 0;
2302}
2303
Ben Lindstroma3700052001-04-05 23:26:32 +00002304void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002305channel_clear_permitted_opens(void)
2306{
2307 int i;
2308
2309 for (i = 0; i < num_permitted_opens; i++)
2310 xfree(permitted_opens[i].host_to_connect);
2311 num_permitted_opens = 0;
2312
2313}
2314
2315
2316/* return socket to remote host, port */
Ben Lindstrombba81212001-06-25 05:01:22 +00002317static int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002318connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002319{
Damien Miller34132e52000-01-14 15:45:46 +11002320 struct addrinfo hints, *ai, *aitop;
2321 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2322 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002323 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002324
Damien Miller34132e52000-01-14 15:45:46 +11002325 memset(&hints, 0, sizeof(hints));
2326 hints.ai_family = IPv4or6;
2327 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002328 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002329 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002330 error("connect_to %.100s: unknown host (%s)", host,
2331 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002332 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002333 }
Damien Miller34132e52000-01-14 15:45:46 +11002334 for (ai = aitop; ai; ai = ai->ai_next) {
2335 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2336 continue;
2337 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2338 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002339 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002340 continue;
2341 }
Damien Miller34132e52000-01-14 15:45:46 +11002342 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2343 if (sock < 0) {
2344 error("socket: %.100s", strerror(errno));
2345 continue;
2346 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +00002347 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002348 fatal("connect_to: F_SETFL: %s", strerror(errno));
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002349 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2350 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002351 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002352 strerror(errno));
2353 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002354 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002355 }
2356 break; /* success */
2357
2358 }
2359 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002360 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002361 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002362 return -1;
2363 }
2364 /* success */
2365 return sock;
2366}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002367
Damien Miller0bc1bd82000-11-13 22:57:25 +11002368int
Ben Lindstrom173e6462001-07-04 05:15:15 +00002369channel_connect_by_listen_address(u_short listen_port)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002370{
2371 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002372
Damien Miller0bc1bd82000-11-13 22:57:25 +11002373 for (i = 0; i < num_permitted_opens; i++)
2374 if (permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002375 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002376 permitted_opens[i].host_to_connect,
2377 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002378 error("WARNING: Server requests forwarding for unknown listen_port %d",
2379 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002380 return -1;
2381}
Damien Millerb38eff82000-04-01 11:09:21 +10002382
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002383/* Check if connecting to that port is permitted and connect. */
2384int
2385channel_connect_to(const char *host, u_short port)
2386{
2387 int i, permit;
2388
2389 permit = all_opens_permitted;
2390 if (!permit) {
2391 for (i = 0; i < num_permitted_opens; i++)
2392 if (permitted_opens[i].port_to_connect == port &&
2393 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2394 permit = 1;
2395
2396 }
2397 if (!permit) {
2398 log("Received request to connect to host %.100s port %d, "
2399 "but the request was denied.", host, port);
2400 return -1;
2401 }
2402 return connect_to(host, port);
2403}
2404
Ben Lindstrome9c99912001-06-09 00:41:05 +00002405/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002406
Damien Miller5428f641999-11-25 11:54:57 +11002407/*
2408 * Creates an internet domain socket for listening for X11 connections.
Kevin Steves366298c2001-12-19 17:58:01 +00002409 * Returns a suitable display number for the DISPLAY variable, or -1 if
2410 * an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11002411 */
Kevin Steves366298c2001-12-19 17:58:01 +00002412int
Damien Millere7378562001-12-21 14:58:35 +11002413x11_create_display_inet(int x11_display_offset, int gateway_ports,
2414 int single_connection)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002415{
Damien Millere7378562001-12-21 14:58:35 +11002416 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11002417 int display_number, sock;
2418 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002419 struct addrinfo hints, *ai, *aitop;
2420 char strport[NI_MAXSERV];
2421 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002422
Damien Millera34a28b1999-12-14 10:47:15 +11002423 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002424 display_number < MAX_DISPLAYS;
2425 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11002426 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002427 memset(&hints, 0, sizeof(hints));
2428 hints.ai_family = IPv4or6;
Kevin Steves366298c2001-12-19 17:58:01 +00002429 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002430 hints.ai_socktype = SOCK_STREAM;
2431 snprintf(strport, sizeof strport, "%d", port);
2432 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2433 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00002434 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002435 }
Damien Miller34132e52000-01-14 15:45:46 +11002436 for (ai = aitop; ai; ai = ai->ai_next) {
2437 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2438 continue;
2439 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2440 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002441 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002442 error("socket: %.100s", strerror(errno));
Kevin Steves366298c2001-12-19 17:58:01 +00002443 return -1;
Damien Millere2192732000-01-17 13:22:55 +11002444 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002445 debug("x11_create_display_inet: Socket family %d not supported",
2446 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002447 continue;
2448 }
Damien Miller34132e52000-01-14 15:45:46 +11002449 }
2450 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2451 debug("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002452 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002453
2454 if (ai->ai_next)
2455 continue;
2456
Damien Miller34132e52000-01-14 15:45:46 +11002457 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11002458 close(socks[n]);
2459 }
2460 num_socks = 0;
2461 break;
2462 }
2463 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002464#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002465 if (num_socks == NUM_SOCKS)
2466 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002467#else
2468 break;
2469#endif
Damien Miller95def091999-11-25 00:26:21 +11002470 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002471 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002472 if (num_socks > 0)
2473 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002474 }
Damien Miller95def091999-11-25 00:26:21 +11002475 if (display_number >= MAX_DISPLAYS) {
2476 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00002477 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002478 }
Damien Miller95def091999-11-25 00:26:21 +11002479 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002480 for (n = 0; n < num_socks; n++) {
2481 sock = socks[n];
2482 if (listen(sock, 5) < 0) {
2483 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002484 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00002485 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11002486 }
Damien Miller95def091999-11-25 00:26:21 +11002487 }
Damien Miller34132e52000-01-14 15:45:46 +11002488
Damien Miller34132e52000-01-14 15:45:46 +11002489 /* Allocate a channel for each socket. */
2490 for (n = 0; n < num_socks; n++) {
2491 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11002492 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10002493 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2494 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002495 0, xstrdup("X11 inet listener"), 1);
Damien Millere7378562001-12-21 14:58:35 +11002496 if (nc != NULL)
2497 nc->single_connection = single_connection;
Damien Miller34132e52000-01-14 15:45:46 +11002498 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002499
Kevin Steves366298c2001-12-19 17:58:01 +00002500 /* Return the display number for the DISPLAY environment variable. */
2501 return display_number;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002502}
2503
Ben Lindstrombba81212001-06-25 05:01:22 +00002504static int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002505connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002506{
Damien Miller95def091999-11-25 00:26:21 +11002507 int sock;
2508 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002509
Damien Miller3afe3752001-12-21 12:39:51 +11002510 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2511 if (sock < 0)
2512 error("socket: %.100s", strerror(errno));
2513 memset(&addr, 0, sizeof(addr));
2514 addr.sun_family = AF_UNIX;
2515 snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
2516 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2517 return sock;
2518 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11002519 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2520 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002521}
2522
Damien Millerbd483e72000-04-30 10:00:53 +10002523int
2524x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002525{
Ben Lindstrom73f57be2001-12-07 17:28:34 +00002526 int display_number, sock = 0, on = 1;
Damien Miller95def091999-11-25 00:26:21 +11002527 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002528 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002529 struct addrinfo hints, *ai, *aitop;
2530 char strport[NI_MAXSERV];
2531 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002532
Damien Miller95def091999-11-25 00:26:21 +11002533 /* Try to open a socket for the local X server. */
2534 display = getenv("DISPLAY");
2535 if (!display) {
2536 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002537 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002538 }
Damien Miller5428f641999-11-25 11:54:57 +11002539 /*
2540 * Now we decode the value of the DISPLAY variable and make a
2541 * connection to the real X server.
2542 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002543
Damien Miller5428f641999-11-25 11:54:57 +11002544 /*
2545 * Check if it is a unix domain socket. Unix domain displays are in
2546 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2547 */
Damien Miller95def091999-11-25 00:26:21 +11002548 if (strncmp(display, "unix:", 5) == 0 ||
2549 display[0] == ':') {
2550 /* Connect to the unix domain socket. */
2551 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2552 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11002553 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002554 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002555 }
2556 /* Create a socket. */
2557 sock = connect_local_xsocket(display_number);
2558 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002559 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002560
2561 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002562 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002563 }
Damien Miller5428f641999-11-25 11:54:57 +11002564 /*
2565 * Connect to an inet socket. The DISPLAY value is supposedly
2566 * hostname:d[.s], where hostname may also be numeric IP address.
2567 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00002568 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11002569 cp = strchr(buf, ':');
2570 if (!cp) {
2571 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002572 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002573 }
Damien Miller95def091999-11-25 00:26:21 +11002574 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002575 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002576 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2577 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11002578 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002579 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002580 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002581
Damien Miller34132e52000-01-14 15:45:46 +11002582 /* Look up the host address */
2583 memset(&hints, 0, sizeof(hints));
2584 hints.ai_family = IPv4or6;
2585 hints.ai_socktype = SOCK_STREAM;
2586 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2587 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2588 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002589 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002590 }
Damien Miller34132e52000-01-14 15:45:46 +11002591 for (ai = aitop; ai; ai = ai->ai_next) {
2592 /* Create a socket. */
2593 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2594 if (sock < 0) {
2595 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002596 continue;
2597 }
2598 /* Connect it to the display. */
2599 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2600 debug("connect %.100s port %d: %.100s", buf,
2601 6000 + display_number, strerror(errno));
2602 close(sock);
2603 continue;
2604 }
2605 /* Success */
2606 break;
Damien Miller34132e52000-01-14 15:45:46 +11002607 }
Damien Miller34132e52000-01-14 15:45:46 +11002608 freeaddrinfo(aitop);
2609 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002610 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002611 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002612 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002613 }
Ben Lindstrom73f57be2001-12-07 17:28:34 +00002614 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on) == -1)
2615 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002616 return sock;
2617}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002618
Damien Millerbd483e72000-04-30 10:00:53 +10002619/*
2620 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2621 * the remote channel number. We should do whatever we want, and respond
2622 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2623 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002624
Damien Millerbd483e72000-04-30 10:00:53 +10002625void
Damien Miller630d6f42002-01-22 23:17:30 +11002626x11_input_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002627{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002628 Channel *c = NULL;
2629 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002630 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10002631
2632 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002633
2634 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002635
2636 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002637 remote_host = packet_get_string(NULL);
2638 } else {
2639 remote_host = xstrdup("unknown (remote did not supply name)");
2640 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002641 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10002642
2643 /* Obtain a connection to the real X display. */
2644 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002645 if (sock != -1) {
2646 /* Allocate a channel for this connection. */
2647 c = channel_new("connected x11 socket",
2648 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
2649 remote_host, 1);
2650 if (c == NULL) {
2651 error("x11_input_open: channel_new failed");
2652 close(sock);
2653 } else {
2654 c->remote_id = remote_id;
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002655 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002656 }
2657 }
2658 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10002659 /* Send refusal to the remote host. */
2660 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002661 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10002662 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10002663 /* Send a confirmation to the remote host. */
2664 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002665 packet_put_int(remote_id);
2666 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10002667 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002668 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002669}
2670
Damien Miller69b69aa2000-10-28 14:19:58 +11002671/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2672void
Damien Miller630d6f42002-01-22 23:17:30 +11002673deny_input_open(int type, u_int32_t seq, void *ctxt)
Damien Miller69b69aa2000-10-28 14:19:58 +11002674{
2675 int rchan = packet_get_int();
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002676 switch (type) {
Damien Miller69b69aa2000-10-28 14:19:58 +11002677 case SSH_SMSG_AGENT_OPEN:
2678 error("Warning: ssh server tried agent forwarding.");
2679 break;
2680 case SSH_SMSG_X11_OPEN:
2681 error("Warning: ssh server tried X11 forwarding.");
2682 break;
2683 default:
Damien Miller630d6f42002-01-22 23:17:30 +11002684 error("deny_input_open: type %d", type);
Damien Miller69b69aa2000-10-28 14:19:58 +11002685 break;
2686 }
2687 error("Warning: this is probably a break in attempt by a malicious server.");
2688 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2689 packet_put_int(rchan);
2690 packet_send();
2691}
2692
Damien Miller5428f641999-11-25 11:54:57 +11002693/*
2694 * Requests forwarding of X11 connections, generates fake authentication
2695 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00002696 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11002697 */
Damien Miller4af51302000-04-16 11:18:38 +10002698void
Damien Millerbd483e72000-04-30 10:00:53 +10002699x11_request_forwarding_with_spoofing(int client_session_id,
2700 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002701{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002702 u_int data_len = (u_int) strlen(data) / 2;
Ben Lindstromb3211a82001-02-10 22:33:19 +00002703 u_int i, value, len;
Damien Miller95def091999-11-25 00:26:21 +11002704 char *new_data;
2705 int screen_number;
2706 const char *cp;
2707 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002708
Damien Miller95def091999-11-25 00:26:21 +11002709 cp = getenv("DISPLAY");
2710 if (cp)
2711 cp = strchr(cp, ':');
2712 if (cp)
2713 cp = strchr(cp, '.');
2714 if (cp)
2715 screen_number = atoi(cp + 1);
2716 else
2717 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002718
Damien Miller95def091999-11-25 00:26:21 +11002719 /* Save protocol name. */
2720 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002721
Damien Miller5428f641999-11-25 11:54:57 +11002722 /*
2723 * Extract real authentication data and generate fake data of the
2724 * same length.
2725 */
Damien Miller95def091999-11-25 00:26:21 +11002726 x11_saved_data = xmalloc(data_len);
2727 x11_fake_data = xmalloc(data_len);
2728 for (i = 0; i < data_len; i++) {
2729 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2730 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2731 if (i % 4 == 0)
2732 rand = arc4random();
2733 x11_saved_data[i] = value;
2734 x11_fake_data[i] = rand & 0xff;
2735 rand >>= 8;
2736 }
2737 x11_saved_data_len = data_len;
2738 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002739
Damien Miller95def091999-11-25 00:26:21 +11002740 /* Convert the fake data into hex. */
Ben Lindstromb3211a82001-02-10 22:33:19 +00002741 len = 2 * data_len + 1;
2742 new_data = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +11002743 for (i = 0; i < data_len; i++)
Ben Lindstromb3211a82001-02-10 22:33:19 +00002744 snprintf(new_data + 2 * i, len - 2 * i,
2745 "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002746
Damien Miller95def091999-11-25 00:26:21 +11002747 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002748 if (compat20) {
2749 channel_request_start(client_session_id, "x11-req", 0);
2750 packet_put_char(0); /* XXX bool single connection */
2751 } else {
2752 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2753 }
2754 packet_put_cstring(proto);
2755 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002756 packet_put_int(screen_number);
2757 packet_send();
2758 packet_write_wait();
2759 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002760}
2761
Ben Lindstrome9c99912001-06-09 00:41:05 +00002762
2763/* -- agent forwarding */
2764
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002765/* Sends a message to the server to request authentication fd forwarding. */
2766
Damien Miller4af51302000-04-16 11:18:38 +10002767void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002768auth_request_forwarding(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002769{
Damien Miller95def091999-11-25 00:26:21 +11002770 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2771 packet_send();
2772 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002773}
2774
Damien Miller5428f641999-11-25 11:54:57 +11002775/*
2776 * Returns the name of the forwarded authentication socket. Returns NULL if
2777 * there is no forwarded authentication socket. The returned value points to
2778 * a static buffer.
2779 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002780
Damien Miller95def091999-11-25 00:26:21 +11002781char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002782auth_get_socket_name(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002783{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002784 return auth_sock_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002785}
2786
2787/* removes the agent forwarding socket */
2788
Damien Miller4af51302000-04-16 11:18:38 +10002789void
Ben Lindstrom983c0982001-06-09 01:20:06 +00002790auth_sock_cleanup_proc(void *_pw)
Damien Miller95def091999-11-25 00:26:21 +11002791{
Ben Lindstrom983c0982001-06-09 01:20:06 +00002792 struct passwd *pw = _pw;
2793
Ben Lindstrom838394c2001-06-09 01:11:59 +00002794 if (auth_sock_name) {
Ben Lindstrom983c0982001-06-09 01:20:06 +00002795 temporarily_use_uid(pw);
Ben Lindstrom838394c2001-06-09 01:11:59 +00002796 unlink(auth_sock_name);
2797 rmdir(auth_sock_dir);
2798 auth_sock_name = NULL;
Ben Lindstrom983c0982001-06-09 01:20:06 +00002799 restore_uid();
Ben Lindstrom838394c2001-06-09 01:11:59 +00002800 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002801}
2802
Damien Miller5428f641999-11-25 11:54:57 +11002803/*
Damien Millerd3a18572000-06-07 19:55:44 +10002804 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002805 * This starts forwarding authentication requests.
2806 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002807
Damien Millerd3a18572000-06-07 19:55:44 +10002808int
Damien Miller95def091999-11-25 00:26:21 +11002809auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002810{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002811 Channel *nc;
2812 int sock;
Damien Miller95def091999-11-25 00:26:21 +11002813 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002814
Ben Lindstrome9c99912001-06-09 00:41:05 +00002815 if (auth_get_socket_name() != NULL) {
2816 error("authentication forwarding requested twice.");
2817 return 0;
2818 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002819
Damien Miller95def091999-11-25 00:26:21 +11002820 /* Temporarily drop privileged uid for mkdir/bind. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00002821 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002822
Damien Miller95def091999-11-25 00:26:21 +11002823 /* Allocate a buffer for the socket name, and format the name. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002824 auth_sock_name = xmalloc(MAXPATHLEN);
2825 auth_sock_dir = xmalloc(MAXPATHLEN);
2826 strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002827
Damien Miller95def091999-11-25 00:26:21 +11002828 /* Create private directory for socket */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002829 if (mkdtemp(auth_sock_dir) == NULL) {
2830 packet_send_debug("Agent forwarding disabled: "
2831 "mkdtemp() failed: %.100s", strerror(errno));
Damien Millerd3a18572000-06-07 19:55:44 +10002832 restore_uid();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002833 xfree(auth_sock_name);
2834 xfree(auth_sock_dir);
2835 auth_sock_name = NULL;
2836 auth_sock_dir = NULL;
Damien Millerd3a18572000-06-07 19:55:44 +10002837 return 0;
2838 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002839 snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%d",
2840 auth_sock_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002841
Ben Lindstrom838394c2001-06-09 01:11:59 +00002842 /* delete agent socket on fatal() */
Ben Lindstrom983c0982001-06-09 01:20:06 +00002843 fatal_add_cleanup(auth_sock_cleanup_proc, pw);
Ben Lindstrom838394c2001-06-09 01:11:59 +00002844
Damien Miller95def091999-11-25 00:26:21 +11002845 /* Create the socket. */
2846 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2847 if (sock < 0)
2848 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002849
Damien Miller95def091999-11-25 00:26:21 +11002850 /* Bind it to the name. */
2851 memset(&sunaddr, 0, sizeof(sunaddr));
2852 sunaddr.sun_family = AF_UNIX;
Ben Lindstromccd8d072001-12-07 17:26:48 +00002853 strlcpy(sunaddr.sun_path, auth_sock_name, sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002854
Damien Miller95def091999-11-25 00:26:21 +11002855 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2856 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002857
Damien Miller95def091999-11-25 00:26:21 +11002858 /* Restore the privileged uid. */
2859 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002860
Damien Miller95def091999-11-25 00:26:21 +11002861 /* Start listening on the socket. */
2862 if (listen(sock, 5) < 0)
2863 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002864
Damien Miller95def091999-11-25 00:26:21 +11002865 /* Allocate a channel for the authentication agent socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002866 nc = channel_new("auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11002867 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2868 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2869 0, xstrdup("auth socket"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002870 if (nc == NULL) {
2871 error("auth_input_request_forwarding: channel_new failed");
Ben Lindstrom983c0982001-06-09 01:20:06 +00002872 auth_sock_cleanup_proc(pw);
Ben Lindstromdf4981b2001-06-09 01:32:29 +00002873 fatal_remove_cleanup(auth_sock_cleanup_proc, pw);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002874 close(sock);
2875 return 0;
2876 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002877 strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
Damien Millerd3a18572000-06-07 19:55:44 +10002878 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002879}
2880
2881/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2882
Damien Miller4af51302000-04-16 11:18:38 +10002883void
Damien Miller630d6f42002-01-22 23:17:30 +11002884auth_input_open_request(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002885{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002886 Channel *c = NULL;
2887 int remote_id, sock;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002888 char *name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002889
Damien Miller95def091999-11-25 00:26:21 +11002890 /* Read the remote channel number from the message. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002891 remote_id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002892 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002893
Damien Miller5428f641999-11-25 11:54:57 +11002894 /*
2895 * Get a connection to the local authentication agent (this may again
2896 * get forwarded).
2897 */
Damien Miller95def091999-11-25 00:26:21 +11002898 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002899
Damien Miller5428f641999-11-25 11:54:57 +11002900 /*
2901 * If we could not connect the agent, send an error message back to
2902 * the server. This should never happen unless the agent dies,
2903 * because authentication forwarding is only enabled if we have an
2904 * agent.
2905 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002906 if (sock >= 0) {
Ben Lindstrome9c99912001-06-09 00:41:05 +00002907 name = xstrdup("authentication agent connection");
2908 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
2909 -1, 0, 0, 0, name, 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002910 if (c == NULL) {
2911 error("auth_input_open_request: channel_new failed");
Ben Lindstrome9c99912001-06-09 00:41:05 +00002912 xfree(name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002913 close(sock);
2914 } else {
2915 c->remote_id = remote_id;
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002916 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002917 }
Damien Miller95def091999-11-25 00:26:21 +11002918 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002919 if (c == NULL) {
2920 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2921 packet_put_int(remote_id);
2922 } else {
2923 /* Send a confirmation to the remote host. */
2924 debug("Forwarding authentication connection.");
2925 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2926 packet_put_int(remote_id);
2927 packet_put_int(c->self);
2928 }
Damien Miller95def091999-11-25 00:26:21 +11002929 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002930}