blob: ce4c5952dad59ca9dfc5778eb58e41ef02475a32 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * This file contains functions for generic socket connection forwarding.
6 * There is also code for initiating connection forwarding for X11 connections,
7 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 *
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Millere4340be2000-09-16 13:29:08 +110017 * Copyright (c) 1999,2000 Markus Friedl. All rights reserved.
18 * Copyright (c) 1999 Dug Song. All rights reserved.
19 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110040 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041
42#include "includes.h"
Ben Lindstrombba81212001-06-25 05:01:22 +000043RCSID("$OpenBSD: channels.c,v 1.127 2001/06/23 15:12:17 itojun Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000046#include "ssh1.h"
47#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048#include "packet.h"
49#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "log.h"
52#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100056#include "key.h"
57#include "authfd.h"
58
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 */
137extern int IPv4or6;
138
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 Miller95def091999-11-25 00:26:21 +1100245 buffer_init(&c->input);
246 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000247 buffer_init(&c->extended);
Damien Miller95def091999-11-25 00:26:21 +1100248 chan_init_iostates(c);
Damien Miller69b69aa2000-10-28 14:19:58 +1100249 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100250 c->self = found;
251 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000252 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000253 c->local_window = window;
254 c->local_window_max = window;
255 c->local_consumed = 0;
256 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100257 c->remote_id = -1;
258 c->remote_name = remote_name;
Damien Millerb38eff82000-04-01 11:09:21 +1000259 c->remote_window = 0;
260 c->remote_maxpacket = 0;
261 c->cb_fn = NULL;
262 c->cb_arg = NULL;
263 c->cb_event = 0;
264 c->dettach_user = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000265 c->input_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100266 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000267 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000269
270/* Close all channel fd/socket. */
271
Ben Lindstrombba81212001-06-25 05:01:22 +0000272static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000273channel_close_fds(Channel *c)
274{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000275 debug3("channel_close_fds: channel %d: r %d w %d e %d",
276 c->self, c->rfd, c->wfd, c->efd);
277
Damien Miller6f83b8e2000-05-02 09:23:45 +1000278 if (c->sock != -1) {
279 close(c->sock);
280 c->sock = -1;
281 }
282 if (c->rfd != -1) {
283 close(c->rfd);
284 c->rfd = -1;
285 }
286 if (c->wfd != -1) {
287 close(c->wfd);
288 c->wfd = -1;
289 }
290 if (c->efd != -1) {
291 close(c->efd);
292 c->efd = -1;
293 }
294}
295
296/* Free the channel and close its fd/socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297
Damien Miller4af51302000-04-16 11:18:38 +1000298void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000299channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000301 char *s;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000302 int i, n;
303
304 for (n = 0, i = 0; i < channels_alloc; i++)
305 if (channels[i])
306 n++;
Ben Lindstrom4c247552001-06-05 20:56:47 +0000307 debug("channel_free: channel %d: %s, nchannels %d", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000308 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000309
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000310 s = channel_open_message();
Ben Lindstrom4c247552001-06-05 20:56:47 +0000311 debug3("channel_free: status: %s", s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000312 xfree(s);
313
Damien Miller33b13562000-04-04 14:38:59 +1000314 if (c->dettach_user != NULL) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000315 debug("channel_free: channel %d: dettaching channel user", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000316 c->dettach_user(c->self, NULL);
317 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000318 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000319 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000320 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000321 buffer_free(&c->input);
322 buffer_free(&c->output);
323 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000324 if (c->remote_name) {
325 xfree(c->remote_name);
326 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100327 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000328 channels[c->self] = NULL;
329 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000330}
331
Ben Lindstrome9c99912001-06-09 00:41:05 +0000332void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000333channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000334{
335 int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000336
Ben Lindstrom601e4362001-06-21 03:19:23 +0000337 for (i = 0; i < channels_alloc; i++)
338 if (channels[i] != NULL)
339 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000340}
341
342/*
343 * Closes the sockets/fds of all channels. This is used to close extra file
344 * descriptors after a fork.
345 */
346
347void
348channel_close_all()
349{
350 int i;
351
352 for (i = 0; i < channels_alloc; i++)
353 if (channels[i] != NULL)
354 channel_close_fds(channels[i]);
355}
356
357/*
358 * Returns true if no channel has too much buffered data, and false if one or
359 * more channel is overfull.
360 */
361
362int
363channel_not_very_much_buffered_data()
364{
365 u_int i;
366 Channel *c;
367
368 for (i = 0; i < channels_alloc; i++) {
369 c = channels[i];
370 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
371 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
372 debug("channel %d: big input buffer %d",
373 c->self, buffer_len(&c->input));
374 return 0;
375 }
376 if (buffer_len(&c->output) > packet_get_maxsize()) {
377 debug("channel %d: big output buffer %d",
378 c->self, buffer_len(&c->output));
379 return 0;
380 }
381 }
382 }
383 return 1;
384}
385
386/* Returns true if any channel is still open. */
387
388int
389channel_still_open()
390{
391 int i;
392 Channel *c;
393
394 for (i = 0; i < channels_alloc; i++) {
395 c = channels[i];
396 if (c == NULL)
397 continue;
398 switch (c->type) {
399 case SSH_CHANNEL_X11_LISTENER:
400 case SSH_CHANNEL_PORT_LISTENER:
401 case SSH_CHANNEL_RPORT_LISTENER:
402 case SSH_CHANNEL_CLOSED:
403 case SSH_CHANNEL_AUTH_SOCKET:
404 case SSH_CHANNEL_DYNAMIC:
405 case SSH_CHANNEL_CONNECTING:
406 case SSH_CHANNEL_ZOMBIE:
407 continue;
408 case SSH_CHANNEL_LARVAL:
409 if (!compat20)
410 fatal("cannot happen: SSH_CHANNEL_LARVAL");
411 continue;
412 case SSH_CHANNEL_OPENING:
413 case SSH_CHANNEL_OPEN:
414 case SSH_CHANNEL_X11_OPEN:
415 return 1;
416 case SSH_CHANNEL_INPUT_DRAINING:
417 case SSH_CHANNEL_OUTPUT_DRAINING:
418 if (!compat13)
419 fatal("cannot happen: OUT_DRAIN");
420 return 1;
421 default:
422 fatal("channel_still_open: bad channel type %d", c->type);
423 /* NOTREACHED */
424 }
425 }
426 return 0;
427}
428
429/* Returns the id of an open channel suitable for keepaliving */
430
431int
432channel_find_open()
433{
434 int i;
435 Channel *c;
436
437 for (i = 0; i < channels_alloc; i++) {
438 c = channels[i];
439 if (c == NULL)
440 continue;
441 switch (c->type) {
442 case SSH_CHANNEL_CLOSED:
443 case SSH_CHANNEL_DYNAMIC:
444 case SSH_CHANNEL_X11_LISTENER:
445 case SSH_CHANNEL_PORT_LISTENER:
446 case SSH_CHANNEL_RPORT_LISTENER:
447 case SSH_CHANNEL_OPENING:
448 case SSH_CHANNEL_CONNECTING:
449 case SSH_CHANNEL_ZOMBIE:
450 continue;
451 case SSH_CHANNEL_LARVAL:
452 case SSH_CHANNEL_AUTH_SOCKET:
453 case SSH_CHANNEL_OPEN:
454 case SSH_CHANNEL_X11_OPEN:
455 return i;
456 case SSH_CHANNEL_INPUT_DRAINING:
457 case SSH_CHANNEL_OUTPUT_DRAINING:
458 if (!compat13)
459 fatal("cannot happen: OUT_DRAIN");
460 return i;
461 default:
462 fatal("channel_find_open: bad channel type %d", c->type);
463 /* NOTREACHED */
464 }
465 }
466 return -1;
467}
468
469
470/*
471 * Returns a message describing the currently open forwarded connections,
472 * suitable for sending to the client. The message contains crlf pairs for
473 * newlines.
474 */
475
476char *
477channel_open_message()
478{
479 Buffer buffer;
480 Channel *c;
481 char buf[1024], *cp;
482 int i;
483
484 buffer_init(&buffer);
485 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
486 buffer_append(&buffer, buf, strlen(buf));
487 for (i = 0; i < channels_alloc; i++) {
488 c = channels[i];
489 if (c == NULL)
490 continue;
491 switch (c->type) {
492 case SSH_CHANNEL_X11_LISTENER:
493 case SSH_CHANNEL_PORT_LISTENER:
494 case SSH_CHANNEL_RPORT_LISTENER:
495 case SSH_CHANNEL_CLOSED:
496 case SSH_CHANNEL_AUTH_SOCKET:
497 case SSH_CHANNEL_ZOMBIE:
498 continue;
499 case SSH_CHANNEL_LARVAL:
500 case SSH_CHANNEL_OPENING:
501 case SSH_CHANNEL_CONNECTING:
502 case SSH_CHANNEL_DYNAMIC:
503 case SSH_CHANNEL_OPEN:
504 case SSH_CHANNEL_X11_OPEN:
505 case SSH_CHANNEL_INPUT_DRAINING:
506 case SSH_CHANNEL_OUTPUT_DRAINING:
507 snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
508 c->self, c->remote_name,
509 c->type, c->remote_id,
510 c->istate, buffer_len(&c->input),
511 c->ostate, buffer_len(&c->output),
512 c->rfd, c->wfd);
513 buffer_append(&buffer, buf, strlen(buf));
514 continue;
515 default:
516 fatal("channel_open_message: bad channel type %d", c->type);
517 /* NOTREACHED */
518 }
519 }
520 buffer_append(&buffer, "\0", 1);
521 cp = xstrdup(buffer_ptr(&buffer));
522 buffer_free(&buffer);
523 return cp;
524}
525
526void
527channel_send_open(int id)
528{
529 Channel *c = channel_lookup(id);
530 if (c == NULL) {
531 log("channel_send_open: %d: bad id", id);
532 return;
533 }
534 debug("send channel open %d", id);
535 packet_start(SSH2_MSG_CHANNEL_OPEN);
536 packet_put_cstring(c->ctype);
537 packet_put_int(c->self);
538 packet_put_int(c->local_window);
539 packet_put_int(c->local_maxpacket);
540 packet_send();
541}
542
543void
544channel_request(int id, char *service, int wantconfirm)
545{
546 channel_request_start(id, service, wantconfirm);
547 packet_send();
548 debug("channel request %d: %s", id, service) ;
549}
550void
551channel_request_start(int id, char *service, int wantconfirm)
552{
553 Channel *c = channel_lookup(id);
554 if (c == NULL) {
555 log("channel_request: %d: bad id", id);
556 return;
557 }
558 packet_start(SSH2_MSG_CHANNEL_REQUEST);
559 packet_put_int(c->remote_id);
560 packet_put_cstring(service);
561 packet_put_char(wantconfirm);
562}
563void
564channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
565{
566 Channel *c = channel_lookup(id);
567 if (c == NULL) {
568 log("channel_register_callback: %d: bad id", id);
569 return;
570 }
571 c->cb_event = mtype;
572 c->cb_fn = fn;
573 c->cb_arg = arg;
574}
575void
576channel_register_cleanup(int id, channel_callback_fn *fn)
577{
578 Channel *c = channel_lookup(id);
579 if (c == NULL) {
580 log("channel_register_cleanup: %d: bad id", id);
581 return;
582 }
583 c->dettach_user = fn;
584}
585void
586channel_cancel_cleanup(int id)
587{
588 Channel *c = channel_lookup(id);
589 if (c == NULL) {
590 log("channel_cancel_cleanup: %d: bad id", id);
591 return;
592 }
593 c->dettach_user = NULL;
594}
595void
596channel_register_filter(int id, channel_filter_fn *fn)
597{
598 Channel *c = channel_lookup(id);
599 if (c == NULL) {
600 log("channel_register_filter: %d: bad id", id);
601 return;
602 }
603 c->input_filter = fn;
604}
605
606void
607channel_set_fds(int id, int rfd, int wfd, int efd,
608 int extusage, int nonblock)
609{
610 Channel *c = channel_lookup(id);
611 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
612 fatal("channel_activate for non-larval channel %d.", id);
613 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
614 c->type = SSH_CHANNEL_OPEN;
615 /* XXX window size? */
616 c->local_window = c->local_window_max = c->local_maxpacket * 2;
617 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
618 packet_put_int(c->remote_id);
619 packet_put_int(c->local_window);
620 packet_send();
621}
622
Damien Miller5428f641999-11-25 11:54:57 +1100623/*
Damien Millerb38eff82000-04-01 11:09:21 +1000624 * 'channel_pre*' are called just before select() to add any bits relevant to
625 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100626 */
Damien Millerb38eff82000-04-01 11:09:21 +1000627/*
628 * 'channel_post*': perform any appropriate operations for channels which
629 * have events pending.
630 */
631typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
632chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
633chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
634
Ben Lindstrombba81212001-06-25 05:01:22 +0000635static void
Damien Millerb38eff82000-04-01 11:09:21 +1000636channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
637{
638 FD_SET(c->sock, readset);
639}
640
Ben Lindstrombba81212001-06-25 05:01:22 +0000641static void
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000642channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
643{
644 debug3("channel %d: waiting for connection", c->self);
645 FD_SET(c->sock, writeset);
646}
647
Ben Lindstrombba81212001-06-25 05:01:22 +0000648static void
Damien Millerb38eff82000-04-01 11:09:21 +1000649channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
650{
651 if (buffer_len(&c->input) < packet_get_maxsize())
652 FD_SET(c->sock, readset);
653 if (buffer_len(&c->output) > 0)
654 FD_SET(c->sock, writeset);
655}
656
Ben Lindstrombba81212001-06-25 05:01:22 +0000657static void
Damien Millerb38eff82000-04-01 11:09:21 +1000658channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
659{
660 /* test whether sockets are 'alive' for read/write */
661 if (c->istate == CHAN_INPUT_OPEN)
662 if (buffer_len(&c->input) < packet_get_maxsize())
663 FD_SET(c->sock, readset);
664 if (c->ostate == CHAN_OUTPUT_OPEN ||
665 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
666 if (buffer_len(&c->output) > 0) {
667 FD_SET(c->sock, writeset);
668 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
669 chan_obuf_empty(c);
670 }
671 }
672}
673
Ben Lindstrombba81212001-06-25 05:01:22 +0000674static void
Damien Miller33b13562000-04-04 14:38:59 +1000675channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
676{
677 if (c->istate == CHAN_INPUT_OPEN &&
678 c->remote_window > 0 &&
679 buffer_len(&c->input) < c->remote_window)
680 FD_SET(c->rfd, readset);
681 if (c->ostate == CHAN_OUTPUT_OPEN ||
682 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
683 if (buffer_len(&c->output) > 0) {
684 FD_SET(c->wfd, writeset);
685 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
686 chan_obuf_empty(c);
687 }
688 }
689 /** XXX check close conditions, too */
690 if (c->efd != -1) {
691 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
692 buffer_len(&c->extended) > 0)
693 FD_SET(c->efd, writeset);
694 else if (c->extended_usage == CHAN_EXTENDED_READ &&
695 buffer_len(&c->extended) < c->remote_window)
696 FD_SET(c->efd, readset);
697 }
698}
699
Ben Lindstrombba81212001-06-25 05:01:22 +0000700static void
Damien Millerb38eff82000-04-01 11:09:21 +1000701channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
702{
703 if (buffer_len(&c->input) == 0) {
704 packet_start(SSH_MSG_CHANNEL_CLOSE);
705 packet_put_int(c->remote_id);
706 packet_send();
707 c->type = SSH_CHANNEL_CLOSED;
Ben Lindstromc486d882001-04-11 16:08:34 +0000708 debug("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000709 }
710}
711
Ben Lindstrombba81212001-06-25 05:01:22 +0000712static void
Damien Millerb38eff82000-04-01 11:09:21 +1000713channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
714{
715 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000716 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000717 else
Damien Millerb38eff82000-04-01 11:09:21 +1000718 FD_SET(c->sock, writeset);
719}
720
721/*
722 * This is a special state for X11 authentication spoofing. An opened X11
723 * connection (when authentication spoofing is being done) remains in this
724 * state until the first packet has been completely read. The authentication
725 * data in that packet is then substituted by the real data if it matches the
726 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000727 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000728 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000729 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000730static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000731x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000732{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000733 u_char *ucp;
734 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000735
736 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000737 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000738 return 0;
739
740 /* Parse the lengths of variable-length fields. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000741 ucp = (u_char *) buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000742 if (ucp[0] == 0x42) { /* Byte order MSB first. */
743 proto_len = 256 * ucp[6] + ucp[7];
744 data_len = 256 * ucp[8] + ucp[9];
745 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
746 proto_len = ucp[6] + 256 * ucp[7];
747 data_len = ucp[8] + 256 * ucp[9];
748 } else {
749 debug("Initial X11 packet contains bad byte order byte: 0x%x",
750 ucp[0]);
751 return -1;
752 }
753
754 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000755 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000756 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
757 return 0;
758
759 /* Check if authentication protocol matches. */
760 if (proto_len != strlen(x11_saved_proto) ||
761 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
762 debug("X11 connection uses different authentication protocol.");
763 return -1;
764 }
765 /* Check if authentication data matches our fake data. */
766 if (data_len != x11_fake_data_len ||
767 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
768 x11_fake_data, x11_fake_data_len) != 0) {
769 debug("X11 auth data does not match fake data.");
770 return -1;
771 }
772 /* Check fake data length */
773 if (x11_fake_data_len != x11_saved_data_len) {
774 error("X11 fake_data_len %d != saved_data_len %d",
775 x11_fake_data_len, x11_saved_data_len);
776 return -1;
777 }
778 /*
779 * Received authentication protocol and data match
780 * our fake data. Substitute the fake data with real
781 * data.
782 */
783 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
784 x11_saved_data, x11_saved_data_len);
785 return 1;
786}
787
Ben Lindstrombba81212001-06-25 05:01:22 +0000788static void
Damien Millerb38eff82000-04-01 11:09:21 +1000789channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
790{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000791 int ret = x11_open_helper(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000792 if (ret == 1) {
793 /* Start normal processing for the channel. */
794 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000795 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000796 } else if (ret == -1) {
797 /*
798 * We have received an X11 connection that has bad
799 * authentication information.
800 */
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000801 log("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000802 buffer_clear(&c->input);
803 buffer_clear(&c->output);
804 close(c->sock);
805 c->sock = -1;
806 c->type = SSH_CHANNEL_CLOSED;
807 packet_start(SSH_MSG_CHANNEL_CLOSE);
808 packet_put_int(c->remote_id);
809 packet_send();
810 }
811}
812
Ben Lindstrombba81212001-06-25 05:01:22 +0000813static void
Damien Millerbd483e72000-04-30 10:00:53 +1000814channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000815{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000816 int ret = x11_open_helper(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000817 if (ret == 1) {
818 c->type = SSH_CHANNEL_OPEN;
Damien Miller30c3d422000-05-09 11:02:59 +1000819 if (compat20)
820 channel_pre_open_20(c, readset, writeset);
821 else
822 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000823 } else if (ret == -1) {
824 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerbd483e72000-04-30 10:00:53 +1000825 chan_read_failed(c); /** force close? */
Damien Millerb38eff82000-04-01 11:09:21 +1000826 chan_write_failed(c);
827 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
828 }
829}
830
Ben Lindstromb3921512001-04-11 15:57:50 +0000831/* try to decode a socks4 header */
Ben Lindstrombba81212001-06-25 05:01:22 +0000832static int
Ben Lindstromb3921512001-04-11 15:57:50 +0000833channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000834{
835 u_char *p, *host;
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000836 int len, have, i, found;
Ben Lindstromb3921512001-04-11 15:57:50 +0000837 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000838 struct {
839 u_int8_t version;
840 u_int8_t command;
841 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000842 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000843 } s4_req, s4_rsp;
844
Ben Lindstromb3921512001-04-11 15:57:50 +0000845 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000846
847 have = buffer_len(&c->input);
848 len = sizeof(s4_req);
849 if (have < len)
850 return 0;
851 p = buffer_ptr(&c->input);
852 for (found = 0, i = len; i < have; i++) {
853 if (p[i] == '\0') {
854 found = 1;
855 break;
856 }
857 if (i > 1024) {
858 /* the peer is probably sending garbage */
859 debug("channel %d: decode socks4: too long",
860 c->self);
861 return -1;
862 }
863 }
864 if (!found)
865 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000866 buffer_get(&c->input, (char *)&s4_req.version, 1);
867 buffer_get(&c->input, (char *)&s4_req.command, 1);
868 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000869 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000870 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000871 p = buffer_ptr(&c->input);
872 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000873 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000874 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000875 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000876 c->self, len, have);
877 strlcpy(username, p, sizeof(username));
878 buffer_consume(&c->input, len);
879 buffer_consume(&c->input, 1); /* trailing '\0' */
880
Ben Lindstromb3921512001-04-11 15:57:50 +0000881 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000882 strlcpy(c->path, host, sizeof(c->path));
883 c->host_port = ntohs(s4_req.dest_port);
884
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000885 debug("channel %d: dynamic request: socks4 host %s port %u command %u",
886 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000887
Ben Lindstromb3921512001-04-11 15:57:50 +0000888 if (s4_req.command != 1) {
889 debug("channel %d: cannot handle: socks4 cn %d",
890 c->self, s4_req.command);
891 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000892 }
Ben Lindstromb3921512001-04-11 15:57:50 +0000893 s4_rsp.version = 0; /* vn: 0 for reply */
894 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000895 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +0000896 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000897 buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +0000898 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000899}
900
Ben Lindstromb3921512001-04-11 15:57:50 +0000901/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +0000902static void
Ben Lindstromb3921512001-04-11 15:57:50 +0000903channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
904{
905 u_char *p;
906 int have, ret;
907
908 have = buffer_len(&c->input);
909
910 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +0000911 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +0000912 /* check if the fixed size part of the packet is in buffer. */
913 if (have < 4) {
914 /* need more */
915 FD_SET(c->sock, readset);
916 return;
917 }
918 /* try to guess the protocol */
919 p = buffer_ptr(&c->input);
920 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000921 case 0x04:
922 ret = channel_decode_socks4(c, readset, writeset);
923 break;
Ben Lindstromb3921512001-04-11 15:57:50 +0000924 default:
925 ret = -1;
926 break;
927 }
928 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000929 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +0000930 } else if (ret == 0) {
931 debug2("channel %d: pre_dynamic: need more", c->self);
932 /* need more */
933 FD_SET(c->sock, readset);
934 } else {
935 /* switch to the next state */
936 c->type = SSH_CHANNEL_OPENING;
937 port_open_helper(c, "direct-tcpip");
938 }
939}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000940
Damien Millerb38eff82000-04-01 11:09:21 +1000941/* This is our fake X11 server socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000942static void
Damien Millerb38eff82000-04-01 11:09:21 +1000943channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
944{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000945 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +1000946 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000947 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +1000948 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +1100949 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +1000950 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +1000951
952 if (FD_ISSET(c->sock, readset)) {
953 debug("X11 connection requested.");
954 addrlen = sizeof(addr);
955 newsock = accept(c->sock, &addr, &addrlen);
956 if (newsock < 0) {
957 error("accept: %.100s", strerror(errno));
958 return;
959 }
Damien Millerd83ff352001-01-30 09:19:34 +1100960 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +1000961 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +1000962 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +1100963 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +1000964
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000965 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +1000966 SSH_CHANNEL_OPENING, newsock, newsock, -1,
967 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100968 0, xstrdup(buf), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000969 if (nc == NULL) {
970 close(newsock);
971 xfree(remote_ipaddr);
972 return;
973 }
Damien Millerbd483e72000-04-30 10:00:53 +1000974 if (compat20) {
975 packet_start(SSH2_MSG_CHANNEL_OPEN);
976 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000977 packet_put_int(nc->self);
Damien Millerbd483e72000-04-30 10:00:53 +1000978 packet_put_int(c->local_window_max);
979 packet_put_int(c->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +1100980 /* originator ipaddr and port */
981 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +1000982 if (datafellows & SSH_BUG_X11FWD) {
983 debug("ssh2 x11 bug compat mode");
984 } else {
985 packet_put_int(remote_port);
986 }
Damien Millerbd483e72000-04-30 10:00:53 +1000987 packet_send();
988 } else {
989 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000990 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000991 if (packet_get_protocol_flags() &
992 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +0000993 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +1000994 packet_send();
995 }
Damien Millerd83ff352001-01-30 09:19:34 +1100996 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +1000997 }
998}
999
Ben Lindstrombba81212001-06-25 05:01:22 +00001000static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001001port_open_helper(Channel *c, char *rtype)
1002{
1003 int direct;
1004 char buf[1024];
1005 char *remote_ipaddr = get_peer_ipaddr(c->sock);
1006 u_short remote_port = get_peer_port(c->sock);
1007
1008 direct = (strcmp(rtype, "direct-tcpip") == 0);
1009
1010 snprintf(buf, sizeof buf,
1011 "%s: listening port %d for %.100s port %d, "
1012 "connect from %.200s port %d",
1013 rtype, c->listening_port, c->path, c->host_port,
1014 remote_ipaddr, remote_port);
1015
1016 xfree(c->remote_name);
1017 c->remote_name = xstrdup(buf);
1018
1019 if (compat20) {
1020 packet_start(SSH2_MSG_CHANNEL_OPEN);
1021 packet_put_cstring(rtype);
1022 packet_put_int(c->self);
1023 packet_put_int(c->local_window_max);
1024 packet_put_int(c->local_maxpacket);
1025 if (direct) {
1026 /* target host, port */
1027 packet_put_cstring(c->path);
1028 packet_put_int(c->host_port);
1029 } else {
1030 /* listen address, port */
1031 packet_put_cstring(c->path);
1032 packet_put_int(c->listening_port);
1033 }
1034 /* originator host and port */
1035 packet_put_cstring(remote_ipaddr);
1036 packet_put_int(remote_port);
1037 packet_send();
1038 } else {
1039 packet_start(SSH_MSG_PORT_OPEN);
1040 packet_put_int(c->self);
1041 packet_put_cstring(c->path);
1042 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001043 if (packet_get_protocol_flags() &
1044 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001045 packet_put_cstring(c->remote_name);
1046 packet_send();
1047 }
1048 xfree(remote_ipaddr);
1049}
1050
Damien Millerb38eff82000-04-01 11:09:21 +10001051/*
1052 * This socket is listening for connections to a forwarded TCP/IP port.
1053 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001054static void
Damien Millerb38eff82000-04-01 11:09:21 +10001055channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
1056{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001057 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001058 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001059 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001060 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001061 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001062
Damien Millerb38eff82000-04-01 11:09:21 +10001063 if (FD_ISSET(c->sock, readset)) {
1064 debug("Connection to port %d forwarding "
1065 "to %.100s port %d requested.",
1066 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001067
1068 rtype = (c->type == SSH_CHANNEL_RPORT_LISTENER) ?
1069 "forwarded-tcpip" : "direct-tcpip";
Ben Lindstrom6d618462001-05-10 23:24:49 +00001070 nextstate = (c->host_port == 0 &&
1071 c->type != SSH_CHANNEL_RPORT_LISTENER) ?
1072 SSH_CHANNEL_DYNAMIC : SSH_CHANNEL_OPENING;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001073
Damien Millerb38eff82000-04-01 11:09:21 +10001074 addrlen = sizeof(addr);
1075 newsock = accept(c->sock, &addr, &addrlen);
1076 if (newsock < 0) {
1077 error("accept: %.100s", strerror(errno));
1078 return;
1079 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001080 nc = channel_new(rtype,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001081 nextstate, newsock, newsock, -1,
Damien Millerb38eff82000-04-01 11:09:21 +10001082 c->local_window_max, c->local_maxpacket,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001083 0, xstrdup(rtype), 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001084 if (nc == NULL) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001085 error("channel_post_port_listener: no new channel:");
1086 close(newsock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001087 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001088 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001089 nc->listening_port = c->listening_port;
1090 nc->host_port = c->host_port;
1091 strlcpy(nc->path, c->path, sizeof(nc->path));
1092
1093 if (nextstate != SSH_CHANNEL_DYNAMIC)
1094 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001095 }
1096}
1097
1098/*
1099 * This is the authentication agent socket listening for connections from
1100 * clients.
1101 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001102static void
Damien Millerb38eff82000-04-01 11:09:21 +10001103channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
1104{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001105 Channel *nc;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001106 char *name;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001107 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001108 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001109 socklen_t addrlen;
1110
1111 if (FD_ISSET(c->sock, readset)) {
1112 addrlen = sizeof(addr);
1113 newsock = accept(c->sock, &addr, &addrlen);
1114 if (newsock < 0) {
1115 error("accept from auth socket: %.100s", strerror(errno));
1116 return;
1117 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001118 name = xstrdup("accepted auth socket");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001119 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001120 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1121 c->local_window_max, c->local_maxpacket,
Ben Lindstrome9c99912001-06-09 00:41:05 +00001122 0, name, 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001123 if (nc == NULL) {
1124 error("channel_post_auth_listener: channel_new failed");
Ben Lindstrome9c99912001-06-09 00:41:05 +00001125 xfree(name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001126 close(newsock);
1127 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001128 if (compat20) {
1129 packet_start(SSH2_MSG_CHANNEL_OPEN);
1130 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001131 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001132 packet_put_int(c->local_window_max);
1133 packet_put_int(c->local_maxpacket);
1134 } else {
1135 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001136 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001137 }
Damien Millerb38eff82000-04-01 11:09:21 +10001138 packet_send();
1139 }
1140}
1141
Ben Lindstrombba81212001-06-25 05:01:22 +00001142static void
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001143channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
1144{
Ben Lindstrom69128662001-05-08 20:07:39 +00001145 int err = 0;
1146 int sz = sizeof(err);
1147
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001148 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001149 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err,
1150 &sz) < 0) {
1151 err = errno;
1152 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001153 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001154 if (err == 0) {
1155 debug("channel %d: connected", c->self);
1156 c->type = SSH_CHANNEL_OPEN;
1157 if (compat20) {
1158 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1159 packet_put_int(c->remote_id);
1160 packet_put_int(c->self);
1161 packet_put_int(c->local_window);
1162 packet_put_int(c->local_maxpacket);
1163 } else {
1164 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1165 packet_put_int(c->remote_id);
1166 packet_put_int(c->self);
1167 }
1168 } else {
1169 debug("channel %d: not connected: %s",
1170 c->self, strerror(err));
1171 if (compat20) {
1172 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1173 packet_put_int(c->remote_id);
1174 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1175 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1176 packet_put_cstring(strerror(err));
1177 packet_put_cstring("");
1178 }
1179 } else {
1180 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1181 packet_put_int(c->remote_id);
1182 }
1183 chan_mark_dead(c);
1184 }
1185 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001186 }
1187}
1188
Ben Lindstrombba81212001-06-25 05:01:22 +00001189static int
Damien Millerb38eff82000-04-01 11:09:21 +10001190channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
1191{
1192 char buf[16*1024];
1193 int len;
1194
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001195 if (c->rfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001196 FD_ISSET(c->rfd, readset)) {
1197 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +10001198 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1199 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001200 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +10001201 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001202 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001203 if (c->type != SSH_CHANNEL_OPEN) {
1204 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001205 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001206 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001207 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001208 buffer_consume(&c->output, buffer_len(&c->output));
1209 c->type = SSH_CHANNEL_INPUT_DRAINING;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001210 debug("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001211 } else {
1212 chan_read_failed(c);
1213 }
1214 return -1;
1215 }
Damien Millerad833b32000-08-23 10:46:23 +10001216 if(c->input_filter != NULL) {
1217 if (c->input_filter(c, buf, len) == -1) {
Ben Lindstromc486d882001-04-11 16:08:34 +00001218 debug("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001219 chan_read_failed(c);
1220 }
1221 } else {
1222 buffer_append(&c->input, buf, len);
1223 }
Damien Millerb38eff82000-04-01 11:09:21 +10001224 }
1225 return 1;
1226}
Ben Lindstrombba81212001-06-25 05:01:22 +00001227static int
Damien Millerb38eff82000-04-01 11:09:21 +10001228channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
1229{
Ben Lindstrome229b252001-03-05 06:28:06 +00001230 struct termios tio;
Damien Millerb38eff82000-04-01 11:09:21 +10001231 int len;
1232
1233 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001234 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001235 FD_ISSET(c->wfd, writeset) &&
1236 buffer_len(&c->output) > 0) {
1237 len = write(c->wfd, buffer_ptr(&c->output),
Damien Miller6f83b8e2000-05-02 09:23:45 +10001238 buffer_len(&c->output));
1239 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1240 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001241 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001242 if (c->type != SSH_CHANNEL_OPEN) {
1243 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001244 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001245 return -1;
1246 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001247 buffer_consume(&c->output, buffer_len(&c->output));
Ben Lindstrome9c99912001-06-09 00:41:05 +00001248 debug("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001249 c->type = SSH_CHANNEL_INPUT_DRAINING;
1250 } else {
1251 chan_write_failed(c);
1252 }
1253 return -1;
1254 }
Damien Miller79438cc2001-02-16 12:34:57 +11001255 if (compat20 && c->isatty) {
Damien Miller79438cc2001-02-16 12:34:57 +11001256 if (tcgetattr(c->wfd, &tio) == 0 &&
1257 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1258 /*
1259 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001260 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001261 * size of a SSH2_MSG_CHANNEL_DATA message
1262 * (4 byte channel id + data)
Damien Miller79438cc2001-02-16 12:34:57 +11001263 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001264 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001265 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001266 }
1267 }
Damien Millerb38eff82000-04-01 11:09:21 +10001268 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001269 if (compat20 && len > 0) {
1270 c->local_consumed += len;
1271 }
1272 }
1273 return 1;
1274}
Ben Lindstrombba81212001-06-25 05:01:22 +00001275static int
Damien Miller33b13562000-04-04 14:38:59 +10001276channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
1277{
1278 char buf[16*1024];
1279 int len;
1280
Damien Miller1383bd82000-04-06 12:32:37 +10001281/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001282 if (c->efd != -1) {
1283 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1284 FD_ISSET(c->efd, writeset) &&
1285 buffer_len(&c->extended) > 0) {
1286 len = write(c->efd, buffer_ptr(&c->extended),
1287 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001288 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001289 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001290 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1291 return 1;
1292 if (len <= 0) {
1293 debug2("channel %d: closing write-efd %d",
1294 c->self, c->efd);
1295 close(c->efd);
1296 c->efd = -1;
1297 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001298 buffer_consume(&c->extended, len);
1299 c->local_consumed += len;
1300 }
1301 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1302 FD_ISSET(c->efd, readset)) {
1303 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001304 debug2("channel %d: read %d from efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001305 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001306 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1307 return 1;
1308 if (len <= 0) {
1309 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001310 c->self, c->efd);
1311 close(c->efd);
1312 c->efd = -1;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001313 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001314 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001315 }
Damien Miller33b13562000-04-04 14:38:59 +10001316 }
1317 }
1318 return 1;
1319}
Ben Lindstrombba81212001-06-25 05:01:22 +00001320static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001321channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001322{
Ben Lindstromb3921512001-04-11 15:57:50 +00001323 if (c->type == SSH_CHANNEL_OPEN &&
1324 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001325 c->local_window < c->local_window_max/2 &&
1326 c->local_consumed > 0) {
1327 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1328 packet_put_int(c->remote_id);
1329 packet_put_int(c->local_consumed);
1330 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001331 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001332 c->self, c->local_window,
1333 c->local_consumed);
1334 c->local_window += c->local_consumed;
1335 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001336 }
1337 return 1;
1338}
1339
Ben Lindstrombba81212001-06-25 05:01:22 +00001340static void
Damien Millerb38eff82000-04-01 11:09:21 +10001341channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
1342{
1343 channel_handle_rfd(c, readset, writeset);
1344 channel_handle_wfd(c, readset, writeset);
1345}
1346
Ben Lindstrombba81212001-06-25 05:01:22 +00001347static void
Damien Miller33b13562000-04-04 14:38:59 +10001348channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
1349{
1350 channel_handle_rfd(c, readset, writeset);
1351 channel_handle_wfd(c, readset, writeset);
1352 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001353
1354 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001355}
1356
Ben Lindstrombba81212001-06-25 05:01:22 +00001357static void
Damien Millerb38eff82000-04-01 11:09:21 +10001358channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1359{
1360 int len;
1361 /* Send buffered output data to the socket. */
1362 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1363 len = write(c->sock, buffer_ptr(&c->output),
1364 buffer_len(&c->output));
1365 if (len <= 0)
1366 buffer_consume(&c->output, buffer_len(&c->output));
1367 else
1368 buffer_consume(&c->output, len);
1369 }
1370}
1371
Ben Lindstrombba81212001-06-25 05:01:22 +00001372static void
Damien Miller33b13562000-04-04 14:38:59 +10001373channel_handler_init_20(void)
1374{
1375 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +10001376 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001377 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001378 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001379 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001380 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001381 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001382 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001383
1384 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
1385 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001386 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001387 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001388 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001389 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001390 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_2;
Damien Miller33b13562000-04-04 14:38:59 +10001391}
1392
Ben Lindstrombba81212001-06-25 05:01:22 +00001393static void
Damien Millerb38eff82000-04-01 11:09:21 +10001394channel_handler_init_13(void)
1395{
1396 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1397 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1398 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1399 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1400 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1401 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1402 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001403 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001404 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001405
1406 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
1407 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1408 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1409 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1410 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001411 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001412 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001413}
1414
Ben Lindstrombba81212001-06-25 05:01:22 +00001415static void
Damien Millerb38eff82000-04-01 11:09:21 +10001416channel_handler_init_15(void)
1417{
1418 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +10001419 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001420 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1421 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1422 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001423 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001424 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001425
1426 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1427 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1428 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1429 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001430 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001431 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001432}
1433
Ben Lindstrombba81212001-06-25 05:01:22 +00001434static void
Damien Millerb38eff82000-04-01 11:09:21 +10001435channel_handler_init(void)
1436{
1437 int i;
1438 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1439 channel_pre[i] = NULL;
1440 channel_post[i] = NULL;
1441 }
Damien Miller33b13562000-04-04 14:38:59 +10001442 if (compat20)
1443 channel_handler_init_20();
1444 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001445 channel_handler_init_13();
1446 else
1447 channel_handler_init_15();
1448}
1449
Ben Lindstrombba81212001-06-25 05:01:22 +00001450static void
Damien Millerb38eff82000-04-01 11:09:21 +10001451channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1452{
1453 static int did_init = 0;
1454 int i;
1455 Channel *c;
1456
1457 if (!did_init) {
1458 channel_handler_init();
1459 did_init = 1;
1460 }
1461 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001462 c = channels[i];
1463 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001464 continue;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001465 if (ftab[c->type] != NULL)
1466 (*ftab[c->type])(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001467 if (chan_is_dead(c)) {
1468 /*
1469 * we have to remove the fd's from the select mask
1470 * before the channels are free'd and the fd's are
1471 * closed
1472 */
1473 if (c->wfd != -1)
1474 FD_CLR(c->wfd, writeset);
1475 if (c->rfd != -1)
1476 FD_CLR(c->rfd, readset);
1477 if (c->efd != -1) {
1478 if (c->extended_usage == CHAN_EXTENDED_READ)
1479 FD_CLR(c->efd, readset);
1480 if (c->extended_usage == CHAN_EXTENDED_WRITE)
1481 FD_CLR(c->efd, writeset);
1482 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001483 channel_free(c);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001484 }
Damien Millerb38eff82000-04-01 11:09:21 +10001485 }
1486}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001487
Ben Lindstrome9c99912001-06-09 00:41:05 +00001488/*
1489 * Allocate/update select bitmasks and add any bits relevant to channels in
1490 * select bitmasks.
1491 */
Damien Miller4af51302000-04-16 11:18:38 +10001492void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001493channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1494 int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001495{
Damien Miller5e953212001-01-30 09:14:00 +11001496 int n;
1497 u_int sz;
1498
1499 n = MAX(*maxfdp, channel_max_fd);
1500
1501 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1502 if (*readsetp == NULL || n > *maxfdp) {
1503 if (*readsetp)
1504 xfree(*readsetp);
1505 if (*writesetp)
1506 xfree(*writesetp);
1507 *readsetp = xmalloc(sz);
1508 *writesetp = xmalloc(sz);
1509 *maxfdp = n;
1510 }
1511 memset(*readsetp, 0, sz);
1512 memset(*writesetp, 0, sz);
1513
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001514 if (!rekeying)
1515 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001516}
1517
Ben Lindstrome9c99912001-06-09 00:41:05 +00001518/*
1519 * After select, perform any appropriate operations for channels which have
1520 * events pending.
1521 */
Damien Miller4af51302000-04-16 11:18:38 +10001522void
Damien Miller95def091999-11-25 00:26:21 +11001523channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001524{
Damien Millerb38eff82000-04-01 11:09:21 +10001525 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001526}
1527
Ben Lindstrome9c99912001-06-09 00:41:05 +00001528
Damien Miller5e953212001-01-30 09:14:00 +11001529/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001530
Damien Miller4af51302000-04-16 11:18:38 +10001531void
Damien Miller95def091999-11-25 00:26:21 +11001532channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001533{
Damien Miller95def091999-11-25 00:26:21 +11001534 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001535 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001536
Damien Miller95def091999-11-25 00:26:21 +11001537 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001538 c = channels[i];
1539 if (c == NULL)
1540 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001541
Ben Lindstrome9c99912001-06-09 00:41:05 +00001542 /*
1543 * We are only interested in channels that can have buffered
1544 * incoming data.
1545 */
Damien Miller34132e52000-01-14 15:45:46 +11001546 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001547 if (c->type != SSH_CHANNEL_OPEN &&
1548 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001549 continue;
1550 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001551 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001552 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001553 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001554 if (compat20 &&
1555 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001556 /* XXX is this true? */
Ben Lindstromb1131e92001-03-05 07:27:13 +00001557 debug2("channel %d: no data after CLOSE", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001558 continue;
1559 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001560
Damien Miller95def091999-11-25 00:26:21 +11001561 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001562 if ((c->istate == CHAN_INPUT_OPEN ||
1563 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1564 (len = buffer_len(&c->input)) > 0) {
Ben Lindstrome9c99912001-06-09 00:41:05 +00001565 /*
1566 * Send some data for the other side over the secure
1567 * connection.
1568 */
Damien Miller33b13562000-04-04 14:38:59 +10001569 if (compat20) {
1570 if (len > c->remote_window)
1571 len = c->remote_window;
1572 if (len > c->remote_maxpacket)
1573 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001574 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001575 if (packet_is_interactive()) {
1576 if (len > 1024)
1577 len = 512;
1578 } else {
1579 /* Keep the packets at reasonable size. */
1580 if (len > packet_get_maxsize()/2)
1581 len = packet_get_maxsize()/2;
1582 }
Damien Miller95def091999-11-25 00:26:21 +11001583 }
Damien Millerb38eff82000-04-01 11:09:21 +10001584 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001585 packet_start(compat20 ?
1586 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001587 packet_put_int(c->remote_id);
1588 packet_put_string(buffer_ptr(&c->input), len);
1589 packet_send();
1590 buffer_consume(&c->input, len);
1591 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001592 }
1593 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001594 if (compat13)
1595 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001596 /*
1597 * input-buffer is empty and read-socket shutdown:
1598 * tell peer, that we will not send more data: send IEOF
1599 */
Damien Millerb38eff82000-04-01 11:09:21 +10001600 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001601 }
Damien Miller33b13562000-04-04 14:38:59 +10001602 /* Send extended data, i.e. stderr */
1603 if (compat20 &&
1604 c->remote_window > 0 &&
1605 (len = buffer_len(&c->extended)) > 0 &&
1606 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001607 debug2("channel %d: rwin %d elen %d euse %d",
1608 c->self, c->remote_window, buffer_len(&c->extended),
1609 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001610 if (len > c->remote_window)
1611 len = c->remote_window;
1612 if (len > c->remote_maxpacket)
1613 len = c->remote_maxpacket;
1614 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1615 packet_put_int(c->remote_id);
1616 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1617 packet_put_string(buffer_ptr(&c->extended), len);
1618 packet_send();
1619 buffer_consume(&c->extended, len);
1620 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001621 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001622 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001623 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001624}
1625
Ben Lindstrome9c99912001-06-09 00:41:05 +00001626
1627/* -- protocol input */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001628
Damien Miller4af51302000-04-16 11:18:38 +10001629void
Damien Miller62cee002000-09-23 17:15:56 +11001630channel_input_data(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001631{
Damien Miller34132e52000-01-14 15:45:46 +11001632 int id;
Damien Miller95def091999-11-25 00:26:21 +11001633 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001634 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001635 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001636
Damien Miller95def091999-11-25 00:26:21 +11001637 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001638 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001639 c = channel_lookup(id);
1640 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001641 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001642
Damien Miller95def091999-11-25 00:26:21 +11001643 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001644 if (c->type != SSH_CHANNEL_OPEN &&
1645 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001646 return;
1647
1648 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001649 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001650 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001651
Damien Miller95def091999-11-25 00:26:21 +11001652 /* Get the data. */
1653 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001654 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001655
Damien Miller33b13562000-04-04 14:38:59 +10001656 if (compat20){
1657 if (data_len > c->local_maxpacket) {
1658 log("channel %d: rcvd big packet %d, maxpack %d",
1659 c->self, data_len, c->local_maxpacket);
1660 }
1661 if (data_len > c->local_window) {
1662 log("channel %d: rcvd too much data %d, win %d",
1663 c->self, data_len, c->local_window);
1664 xfree(data);
1665 return;
1666 }
1667 c->local_window -= data_len;
1668 }else{
1669 packet_integrity_check(plen, 4 + 4 + data_len, type);
1670 }
Damien Millerb38eff82000-04-01 11:09:21 +10001671 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001672 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001673}
Ben Lindstrome9c99912001-06-09 00:41:05 +00001674
Damien Miller4af51302000-04-16 11:18:38 +10001675void
Damien Miller62cee002000-09-23 17:15:56 +11001676channel_input_extended_data(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001677{
1678 int id;
1679 int tcode;
1680 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001681 u_int data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001682 Channel *c;
1683
1684 /* Get the channel number and verify it. */
1685 id = packet_get_int();
1686 c = channel_lookup(id);
1687
1688 if (c == NULL)
1689 packet_disconnect("Received extended_data for bad channel %d.", id);
1690 if (c->type != SSH_CHANNEL_OPEN) {
1691 log("channel %d: ext data for non open", id);
1692 return;
1693 }
1694 tcode = packet_get_int();
1695 if (c->efd == -1 ||
1696 c->extended_usage != CHAN_EXTENDED_WRITE ||
1697 tcode != SSH2_EXTENDED_DATA_STDERR) {
1698 log("channel %d: bad ext data", c->self);
1699 return;
1700 }
1701 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001702 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001703 if (data_len > c->local_window) {
1704 log("channel %d: rcvd too much extended_data %d, win %d",
1705 c->self, data_len, c->local_window);
1706 xfree(data);
1707 return;
1708 }
Damien Millerd3444942000-09-30 14:20:03 +11001709 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001710 c->local_window -= data_len;
1711 buffer_append(&c->extended, data, data_len);
1712 xfree(data);
1713}
1714
Damien Miller4af51302000-04-16 11:18:38 +10001715void
Damien Miller62cee002000-09-23 17:15:56 +11001716channel_input_ieof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001717{
1718 int id;
1719 Channel *c;
1720
1721 packet_integrity_check(plen, 4, type);
1722
1723 id = packet_get_int();
1724 c = channel_lookup(id);
1725 if (c == NULL)
1726 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1727 chan_rcvd_ieof(c);
1728}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001729
Damien Miller4af51302000-04-16 11:18:38 +10001730void
Damien Miller62cee002000-09-23 17:15:56 +11001731channel_input_close(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001732{
Damien Millerb38eff82000-04-01 11:09:21 +10001733 int id;
1734 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001735
Damien Millerb38eff82000-04-01 11:09:21 +10001736 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001737
Damien Millerb38eff82000-04-01 11:09:21 +10001738 id = packet_get_int();
1739 c = channel_lookup(id);
1740 if (c == NULL)
1741 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001742
1743 /*
1744 * Send a confirmation that we have closed the channel and no more
1745 * data is coming for it.
1746 */
Damien Miller95def091999-11-25 00:26:21 +11001747 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001748 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001749 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001750
Damien Miller5428f641999-11-25 11:54:57 +11001751 /*
1752 * If the channel is in closed state, we have sent a close request,
1753 * and the other side will eventually respond with a confirmation.
1754 * Thus, we cannot free the channel here, because then there would be
1755 * no-one to receive the confirmation. The channel gets freed when
1756 * the confirmation arrives.
1757 */
Damien Millerb38eff82000-04-01 11:09:21 +10001758 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001759 /*
1760 * Not a closed channel - mark it as draining, which will
1761 * cause it to be freed later.
1762 */
Damien Millerb38eff82000-04-01 11:09:21 +10001763 buffer_consume(&c->input, buffer_len(&c->input));
1764 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001765 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001766}
1767
Damien Millerb38eff82000-04-01 11:09:21 +10001768/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001769void
Damien Miller62cee002000-09-23 17:15:56 +11001770channel_input_oclose(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001771{
Damien Millerb38eff82000-04-01 11:09:21 +10001772 int id = packet_get_int();
1773 Channel *c = channel_lookup(id);
1774 packet_integrity_check(plen, 4, type);
1775 if (c == NULL)
1776 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1777 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001778}
1779
Damien Miller4af51302000-04-16 11:18:38 +10001780void
Damien Miller62cee002000-09-23 17:15:56 +11001781channel_input_close_confirmation(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001782{
1783 int id = packet_get_int();
1784 Channel *c = channel_lookup(id);
1785
Damien Miller4af51302000-04-16 11:18:38 +10001786 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001787 if (c == NULL)
1788 packet_disconnect("Received close confirmation for "
1789 "out-of-range channel %d.", id);
1790 if (c->type != SSH_CHANNEL_CLOSED)
1791 packet_disconnect("Received close confirmation for "
1792 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001793 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001794}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001795
Damien Miller4af51302000-04-16 11:18:38 +10001796void
Damien Miller62cee002000-09-23 17:15:56 +11001797channel_input_open_confirmation(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001798{
Damien Millerb38eff82000-04-01 11:09:21 +10001799 int id, remote_id;
1800 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001801
Damien Miller33b13562000-04-04 14:38:59 +10001802 if (!compat20)
1803 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001804
Damien Millerb38eff82000-04-01 11:09:21 +10001805 id = packet_get_int();
1806 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001807
Damien Millerb38eff82000-04-01 11:09:21 +10001808 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1809 packet_disconnect("Received open confirmation for "
1810 "non-opening channel %d.", id);
1811 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001812 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001813 c->remote_id = remote_id;
1814 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001815
1816 if (compat20) {
1817 c->remote_window = packet_get_int();
1818 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001819 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001820 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001821 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001822 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001823 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001824 }
1825 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1826 c->remote_window, c->remote_maxpacket);
1827 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001828}
1829
Ben Lindstrombba81212001-06-25 05:01:22 +00001830static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00001831reason2txt(int reason)
1832{
1833 switch(reason) {
1834 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
1835 return "administratively prohibited";
1836 case SSH2_OPEN_CONNECT_FAILED:
1837 return "connect failed";
1838 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
1839 return "unknown channel type";
1840 case SSH2_OPEN_RESOURCE_SHORTAGE:
1841 return "resource shortage";
1842 }
Ben Lindstrome2595442001-06-05 20:01:39 +00001843 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00001844}
1845
Damien Miller4af51302000-04-16 11:18:38 +10001846void
Damien Miller62cee002000-09-23 17:15:56 +11001847channel_input_open_failure(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001848{
Kevin Steves12057502001-02-05 14:54:34 +00001849 int id, reason;
1850 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001851 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001852
Damien Miller33b13562000-04-04 14:38:59 +10001853 if (!compat20)
1854 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001855
1856 id = packet_get_int();
1857 c = channel_lookup(id);
1858
1859 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1860 packet_disconnect("Received open failure for "
1861 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001862 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00001863 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00001864 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00001865 msg = packet_get_string(NULL);
1866 lang = packet_get_string(NULL);
1867 }
Damien Miller4af51302000-04-16 11:18:38 +10001868 packet_done();
Ben Lindstrom69128662001-05-08 20:07:39 +00001869 log("channel %d: open failed: %s%s%s", id,
1870 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00001871 if (msg != NULL)
1872 xfree(msg);
1873 if (lang != NULL)
1874 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001875 }
Damien Miller95def091999-11-25 00:26:21 +11001876 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001877 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001878}
1879
Damien Miller33b13562000-04-04 14:38:59 +10001880void
Damien Miller62cee002000-09-23 17:15:56 +11001881channel_input_channel_request(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001882{
1883 int id;
1884 Channel *c;
1885
1886 id = packet_get_int();
1887 c = channel_lookup(id);
1888
1889 if (c == NULL ||
1890 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1891 packet_disconnect("Received request for "
1892 "non-open channel %d.", id);
1893 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001894 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001895 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001896 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001897 } else {
1898 char *service = packet_get_string(NULL);
Ben Lindstromcc74df72001-03-05 06:20:14 +00001899 debug("channel %d: rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001900 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001901 xfree(service);
1902 }
1903}
1904
Damien Miller4af51302000-04-16 11:18:38 +10001905void
Damien Miller62cee002000-09-23 17:15:56 +11001906channel_input_window_adjust(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001907{
1908 Channel *c;
1909 int id, adjust;
1910
1911 if (!compat20)
1912 return;
1913
1914 /* Get the channel number and verify it. */
1915 id = packet_get_int();
1916 c = channel_lookup(id);
1917
1918 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1919 log("Received window adjust for "
1920 "non-open channel %d.", id);
1921 return;
1922 }
1923 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001924 packet_done();
Damien Millerd3444942000-09-30 14:20:03 +11001925 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10001926 c->remote_window += adjust;
1927}
1928
Damien Miller4af51302000-04-16 11:18:38 +10001929void
Ben Lindstrome9c99912001-06-09 00:41:05 +00001930channel_input_port_open(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001931{
Ben Lindstrome9c99912001-06-09 00:41:05 +00001932 Channel *c = NULL;
1933 u_short host_port;
1934 char *host, *originator_string;
1935 int remote_id, sock = -1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001936
Ben Lindstrome9c99912001-06-09 00:41:05 +00001937 remote_id = packet_get_int();
1938 host = packet_get_string(NULL);
1939 host_port = packet_get_int();
1940
1941 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
1942 originator_string = packet_get_string(NULL);
1943 } else {
1944 originator_string = xstrdup("unknown (remote did not supply name)");
1945 }
1946 packet_done();
1947 sock = channel_connect_to(host, host_port);
1948 if (sock != -1) {
1949 c = channel_new("connected socket",
1950 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
1951 originator_string, 1);
1952 if (c == NULL) {
1953 error("channel_input_port_open: channel_new failed");
1954 close(sock);
1955 } else {
1956 c->remote_id = remote_id;
Damien Miller95def091999-11-25 00:26:21 +11001957 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001958 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001959 if (c == NULL) {
1960 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1961 packet_put_int(remote_id);
1962 packet_send();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001963 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001964 xfree(host);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001965}
1966
1967
Ben Lindstrome9c99912001-06-09 00:41:05 +00001968/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001969
Damien Miller5428f641999-11-25 11:54:57 +11001970/*
1971 * Initiate forwarding of connections to local port "port" through the secure
1972 * channel to host:port from remote side.
1973 */
Kevin Steves12057502001-02-05 14:54:34 +00001974int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001975channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
1976 u_short port_to_connect, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001977{
Kevin Steves12057502001-02-05 14:54:34 +00001978 return channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +11001979 NULL, listen_port,
1980 host_to_connect, port_to_connect,
1981 gateway_ports, /*remote_fwd*/ 0);
1982}
1983
1984/*
1985 * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
1986 * (SSH_CHANNEL_RPORT_LISTENER).
1987 */
Kevin Steves12057502001-02-05 14:54:34 +00001988int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001989channel_request_forwarding(
1990 const char *listen_address, u_short listen_port,
1991 const char *host_to_connect, u_short port_to_connect,
1992 int gateway_ports, int remote_fwd)
1993{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001994 Channel *c;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001995 int success, sock, on = 1, type;
Damien Miller34132e52000-01-14 15:45:46 +11001996 struct addrinfo hints, *ai, *aitop;
1997 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0bc1bd82000-11-13 22:57:25 +11001998 const char *host;
Damien Miller5428f641999-11-25 11:54:57 +11001999 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002000
Kevin Steves12057502001-02-05 14:54:34 +00002001 success = 0;
2002
Damien Miller0bc1bd82000-11-13 22:57:25 +11002003 if (remote_fwd) {
2004 host = listen_address;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002005 type = SSH_CHANNEL_RPORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002006 } else {
2007 host = host_to_connect;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002008 type = SSH_CHANNEL_PORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002009 }
2010
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002011 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00002012 error("Forward host name too long.");
2013 return success;
2014 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002015
Damien Miller0bc1bd82000-11-13 22:57:25 +11002016 /* XXX listen_address is currently ignored */
Damien Miller5428f641999-11-25 11:54:57 +11002017 /*
Damien Miller34132e52000-01-14 15:45:46 +11002018 * getaddrinfo returns a loopback address if the hostname is
2019 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002020 */
Damien Miller34132e52000-01-14 15:45:46 +11002021 memset(&hints, 0, sizeof(hints));
2022 hints.ai_family = IPv4or6;
2023 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
2024 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002025 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002026 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
2027 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11002028
Damien Miller34132e52000-01-14 15:45:46 +11002029 for (ai = aitop; ai; ai = ai->ai_next) {
2030 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2031 continue;
2032 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2033 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11002034 error("channel_request_forwarding: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002035 continue;
2036 }
2037 /* Create a port to listen for the host. */
2038 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2039 if (sock < 0) {
2040 /* this is no error since kernel may not support ipv6 */
2041 verbose("socket: %.100s", strerror(errno));
2042 continue;
2043 }
2044 /*
2045 * Set socket options. We would like the socket to disappear
2046 * as soon as it has been closed for whatever reason.
2047 */
2048 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
2049 linger.l_onoff = 1;
2050 linger.l_linger = 5;
2051 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
2052 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002053
Damien Miller34132e52000-01-14 15:45:46 +11002054 /* Bind the socket to the address. */
2055 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2056 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002057 if (!ai->ai_next)
2058 error("bind: %.100s", strerror(errno));
2059 else
2060 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002061
Damien Miller34132e52000-01-14 15:45:46 +11002062 close(sock);
2063 continue;
2064 }
2065 /* Start listening for connections on the socket. */
2066 if (listen(sock, 5) < 0) {
2067 error("listen: %.100s", strerror(errno));
2068 close(sock);
2069 continue;
2070 }
2071 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002072 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002073 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002074 0, xstrdup("port listener"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002075 if (c == NULL) {
2076 error("channel_request_forwarding: channel_new failed");
2077 close(sock);
2078 continue;
2079 }
2080 strlcpy(c->path, host, sizeof(c->path));
2081 c->host_port = port_to_connect;
2082 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002083 success = 1;
2084 }
2085 if (success == 0)
Kevin Steves12057502001-02-05 14:54:34 +00002086 error("channel_request_forwarding: cannot listen to port: %d",
2087 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002088 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002089 return success;
Damien Miller95def091999-11-25 00:26:21 +11002090}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002091
Damien Miller5428f641999-11-25 11:54:57 +11002092/*
2093 * Initiate forwarding of connections to port "port" on remote host through
2094 * the secure channel to host:port from local side.
2095 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002096
Damien Miller4af51302000-04-16 11:18:38 +10002097void
Damien Miller0bc1bd82000-11-13 22:57:25 +11002098channel_request_remote_forwarding(u_short listen_port,
2099 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002100{
Damien Miller0bc1bd82000-11-13 22:57:25 +11002101 int payload_len, type, success = 0;
2102
Damien Miller95def091999-11-25 00:26:21 +11002103 /* Record locally that connection to this host/port is permitted. */
2104 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2105 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002106
Damien Miller95def091999-11-25 00:26:21 +11002107 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002108 if (compat20) {
2109 const char *address_to_bind = "0.0.0.0";
2110 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2111 packet_put_cstring("tcpip-forward");
2112 packet_put_char(0); /* boolean: want reply */
2113 packet_put_cstring(address_to_bind);
2114 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002115 packet_send();
2116 packet_write_wait();
2117 /* Assume that server accepts the request */
2118 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002119 } else {
2120 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002121 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002122 packet_put_cstring(host_to_connect);
2123 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002124 packet_send();
2125 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002126
2127 /* Wait for response from the remote side. */
2128 type = packet_read(&payload_len);
2129 switch (type) {
2130 case SSH_SMSG_SUCCESS:
2131 success = 1;
2132 break;
2133 case SSH_SMSG_FAILURE:
2134 log("Warning: Server denied remote port forwarding.");
2135 break;
2136 default:
2137 /* Unknown packet */
2138 packet_disconnect("Protocol error for port forward request:"
2139 "received packet type %d.", type);
2140 }
2141 }
2142 if (success) {
2143 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2144 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2145 permitted_opens[num_permitted_opens].listen_port = listen_port;
2146 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002147 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002148}
2149
Damien Miller5428f641999-11-25 11:54:57 +11002150/*
2151 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2152 * listening for the port, and sends back a success reply (or disconnect
2153 * message if there was an error). This never returns if there was an error.
2154 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002155
Damien Miller4af51302000-04-16 11:18:38 +10002156void
Damien Millere247cc42000-05-07 12:03:14 +10002157channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002158{
Damien Milleraae6c611999-12-06 11:47:28 +11002159 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11002160 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002161
Damien Miller95def091999-11-25 00:26:21 +11002162 /* Get arguments from the packet. */
2163 port = packet_get_int();
2164 hostname = packet_get_string(NULL);
2165 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002166
Damien Millerbac2d8a2000-09-05 16:13:06 +11002167#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002168 /*
2169 * Check that an unprivileged user is not trying to forward a
2170 * privileged port.
2171 */
Damien Miller95def091999-11-25 00:26:21 +11002172 if (port < IPPORT_RESERVED && !is_root)
2173 packet_disconnect("Requested forwarding of port %d but user is not root.",
2174 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11002175#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11002176 /* Initiate forwarding */
Damien Millere247cc42000-05-07 12:03:14 +10002177 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002178
2179 /* Free the argument string. */
2180 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002181}
2182
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002183/*
2184 * Permits opening to any host/port if permitted_opens[] is empty. This is
2185 * usually called by the server, because the user could connect to any port
2186 * anyway, and the server has no way to know but to trust the client anyway.
2187 */
2188void
2189channel_permit_all_opens()
2190{
2191 if (num_permitted_opens == 0)
2192 all_opens_permitted = 1;
2193}
2194
Ben Lindstroma3700052001-04-05 23:26:32 +00002195void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002196channel_add_permitted_opens(char *host, int port)
2197{
2198 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2199 fatal("channel_request_remote_forwarding: too many forwards");
2200 debug("allow port forwarding to host %s port %d", host, port);
2201
2202 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2203 permitted_opens[num_permitted_opens].port_to_connect = port;
2204 num_permitted_opens++;
2205
2206 all_opens_permitted = 0;
2207}
2208
Ben Lindstroma3700052001-04-05 23:26:32 +00002209void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002210channel_clear_permitted_opens(void)
2211{
2212 int i;
2213
2214 for (i = 0; i < num_permitted_opens; i++)
2215 xfree(permitted_opens[i].host_to_connect);
2216 num_permitted_opens = 0;
2217
2218}
2219
2220
2221/* return socket to remote host, port */
Ben Lindstrombba81212001-06-25 05:01:22 +00002222static int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002223connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002224{
Damien Miller34132e52000-01-14 15:45:46 +11002225 struct addrinfo hints, *ai, *aitop;
2226 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2227 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002228 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002229
Damien Miller34132e52000-01-14 15:45:46 +11002230 memset(&hints, 0, sizeof(hints));
2231 hints.ai_family = IPv4or6;
2232 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002233 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002234 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002235 error("connect_to %.100s: unknown host (%s)", host,
2236 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002237 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002238 }
Damien Miller34132e52000-01-14 15:45:46 +11002239 for (ai = aitop; ai; ai = ai->ai_next) {
2240 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2241 continue;
2242 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2243 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002244 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002245 continue;
2246 }
Damien Miller34132e52000-01-14 15:45:46 +11002247 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2248 if (sock < 0) {
2249 error("socket: %.100s", strerror(errno));
2250 continue;
2251 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +00002252 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002253 fatal("connect_to: F_SETFL: %s", strerror(errno));
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002254 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2255 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002256 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002257 strerror(errno));
2258 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002259 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002260 }
2261 break; /* success */
2262
2263 }
2264 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002265 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002266 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002267 return -1;
2268 }
2269 /* success */
2270 return sock;
2271}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002272
Damien Miller0bc1bd82000-11-13 22:57:25 +11002273int
2274channel_connect_by_listen_adress(u_short listen_port)
2275{
2276 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002277
Damien Miller0bc1bd82000-11-13 22:57:25 +11002278 for (i = 0; i < num_permitted_opens; i++)
2279 if (permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002280 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002281 permitted_opens[i].host_to_connect,
2282 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002283 error("WARNING: Server requests forwarding for unknown listen_port %d",
2284 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002285 return -1;
2286}
Damien Millerb38eff82000-04-01 11:09:21 +10002287
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002288/* Check if connecting to that port is permitted and connect. */
2289int
2290channel_connect_to(const char *host, u_short port)
2291{
2292 int i, permit;
2293
2294 permit = all_opens_permitted;
2295 if (!permit) {
2296 for (i = 0; i < num_permitted_opens; i++)
2297 if (permitted_opens[i].port_to_connect == port &&
2298 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2299 permit = 1;
2300
2301 }
2302 if (!permit) {
2303 log("Received request to connect to host %.100s port %d, "
2304 "but the request was denied.", host, port);
2305 return -1;
2306 }
2307 return connect_to(host, port);
2308}
2309
Ben Lindstrome9c99912001-06-09 00:41:05 +00002310/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002311
Damien Miller5428f641999-11-25 11:54:57 +11002312/*
2313 * Creates an internet domain socket for listening for X11 connections.
2314 * Returns a suitable value for the DISPLAY variable, or NULL if an error
2315 * occurs.
2316 */
Damien Miller95def091999-11-25 00:26:21 +11002317char *
Damien Millera34a28b1999-12-14 10:47:15 +11002318x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002319{
Damien Milleraae6c611999-12-06 11:47:28 +11002320 int display_number, sock;
2321 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002322 struct addrinfo hints, *ai, *aitop;
2323 char strport[NI_MAXSERV];
2324 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
2325 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11002326 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002327
Damien Millera34a28b1999-12-14 10:47:15 +11002328 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11002329 display_number < MAX_DISPLAYS;
2330 display_number++) {
2331 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002332 memset(&hints, 0, sizeof(hints));
2333 hints.ai_family = IPv4or6;
2334 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
2335 hints.ai_socktype = SOCK_STREAM;
2336 snprintf(strport, sizeof strport, "%d", port);
2337 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2338 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11002339 return NULL;
2340 }
Damien Miller34132e52000-01-14 15:45:46 +11002341 for (ai = aitop; ai; ai = ai->ai_next) {
2342 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2343 continue;
2344 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2345 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002346 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002347 error("socket: %.100s", strerror(errno));
2348 return NULL;
2349 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002350 debug("x11_create_display_inet: Socket family %d not supported",
2351 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002352 continue;
2353 }
Damien Miller34132e52000-01-14 15:45:46 +11002354 }
2355 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2356 debug("bind port %d: %.100s", port, strerror(errno));
2357 shutdown(sock, SHUT_RDWR);
2358 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002359
2360 if (ai->ai_next)
2361 continue;
2362
Damien Miller34132e52000-01-14 15:45:46 +11002363 for (n = 0; n < num_socks; n++) {
2364 shutdown(socks[n], SHUT_RDWR);
2365 close(socks[n]);
2366 }
2367 num_socks = 0;
2368 break;
2369 }
2370 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002371#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002372 if (num_socks == NUM_SOCKS)
2373 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002374#else
2375 break;
2376#endif
Damien Miller95def091999-11-25 00:26:21 +11002377 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002378 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002379 if (num_socks > 0)
2380 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002381 }
Damien Miller95def091999-11-25 00:26:21 +11002382 if (display_number >= MAX_DISPLAYS) {
2383 error("Failed to allocate internet-domain X11 display socket.");
2384 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002385 }
Damien Miller95def091999-11-25 00:26:21 +11002386 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002387 for (n = 0; n < num_socks; n++) {
2388 sock = socks[n];
2389 if (listen(sock, 5) < 0) {
2390 error("listen: %.100s", strerror(errno));
2391 shutdown(sock, SHUT_RDWR);
2392 close(sock);
2393 return NULL;
2394 }
Damien Miller95def091999-11-25 00:26:21 +11002395 }
Damien Miller34132e52000-01-14 15:45:46 +11002396
Damien Miller95def091999-11-25 00:26:21 +11002397 /* Set up a suitable value for the DISPLAY variable. */
2398 if (gethostname(hostname, sizeof(hostname)) < 0)
2399 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11002400
2401#ifdef IPADDR_IN_DISPLAY
Kevin Stevesef4eea92001-02-05 12:42:17 +00002402 /*
Damien Miller76112de1999-12-21 11:18:08 +11002403 * HPUX detects the local hostname in the DISPLAY variable and tries
2404 * to set up a shared memory connection to the server, which it
2405 * incorrectly supposes to be local.
2406 *
2407 * The workaround - as used in later $$H and other programs - is
2408 * is to set display to the host's IP address.
2409 */
2410 {
2411 struct hostent *he;
2412 struct in_addr my_addr;
2413
2414 he = gethostbyname(hostname);
2415 if (he == NULL) {
2416 error("[X11-broken-fwd-hostname-workaround] Could not get "
2417 "IP address for hostname %s.", hostname);
2418
2419 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
2420 "Could not get IP address for hostname %s.", hostname);
2421
2422 shutdown(sock, SHUT_RDWR);
2423 close(sock);
2424
2425 return NULL;
2426 }
2427
2428 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2429
2430 /* Set DISPLAY to <ip address>:screen.display */
Kevin Stevesef4eea92001-02-05 12:42:17 +00002431 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Kevin Stevesda6cd592000-12-29 18:41:21 +00002432 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002433 }
2434#else /* IPADDR_IN_DISPLAY */
2435 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11002436 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Kevin Stevesda6cd592000-12-29 18:41:21 +00002437 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002438#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002439
Damien Miller34132e52000-01-14 15:45:46 +11002440 /* Allocate a channel for each socket. */
2441 for (n = 0; n < num_socks; n++) {
2442 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10002443 (void) channel_new("x11 listener",
2444 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2445 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002446 0, xstrdup("X11 inet listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11002447 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002448
Damien Miller95def091999-11-25 00:26:21 +11002449 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11002450 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002451}
2452
2453#ifndef X_UNIX_PATH
2454#define X_UNIX_PATH "/tmp/.X11-unix/X"
2455#endif
2456
Ben Lindstrombba81212001-06-25 05:01:22 +00002457static int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002458connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002459{
Damien Miller95def091999-11-25 00:26:21 +11002460 static const char *const x_sockets[] = {
2461 X_UNIX_PATH "%u",
2462 "/var/X/.X11-unix/X" "%u",
2463 "/usr/spool/sockets/X11/" "%u",
2464 NULL
2465 };
2466 int sock;
2467 struct sockaddr_un addr;
2468 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002469
Damien Miller95def091999-11-25 00:26:21 +11002470 for (path = x_sockets; *path; ++path) {
2471 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2472 if (sock < 0)
2473 error("socket: %.100s", strerror(errno));
2474 memset(&addr, 0, sizeof(addr));
2475 addr.sun_family = AF_UNIX;
2476 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
2477 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2478 return sock;
2479 close(sock);
2480 }
2481 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2482 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002483}
2484
Damien Millerbd483e72000-04-30 10:00:53 +10002485int
2486x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002487{
Damien Millerbd483e72000-04-30 10:00:53 +10002488 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002489 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002490 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002491 struct addrinfo hints, *ai, *aitop;
2492 char strport[NI_MAXSERV];
2493 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002494
Damien Miller95def091999-11-25 00:26:21 +11002495 /* Try to open a socket for the local X server. */
2496 display = getenv("DISPLAY");
2497 if (!display) {
2498 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002499 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002500 }
Damien Miller5428f641999-11-25 11:54:57 +11002501 /*
2502 * Now we decode the value of the DISPLAY variable and make a
2503 * connection to the real X server.
2504 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002505
Damien Miller5428f641999-11-25 11:54:57 +11002506 /*
2507 * Check if it is a unix domain socket. Unix domain displays are in
2508 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2509 */
Damien Miller95def091999-11-25 00:26:21 +11002510 if (strncmp(display, "unix:", 5) == 0 ||
2511 display[0] == ':') {
2512 /* Connect to the unix domain socket. */
2513 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2514 error("Could not parse display number from DISPLAY: %.100s",
2515 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002516 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002517 }
2518 /* Create a socket. */
2519 sock = connect_local_xsocket(display_number);
2520 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002521 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002522
2523 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002524 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002525 }
Damien Miller5428f641999-11-25 11:54:57 +11002526 /*
2527 * Connect to an inet socket. The DISPLAY value is supposedly
2528 * hostname:d[.s], where hostname may also be numeric IP address.
2529 */
Damien Miller95def091999-11-25 00:26:21 +11002530 strncpy(buf, display, sizeof(buf));
2531 buf[sizeof(buf) - 1] = 0;
2532 cp = strchr(buf, ':');
2533 if (!cp) {
2534 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002535 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002536 }
Damien Miller95def091999-11-25 00:26:21 +11002537 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002538 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002539 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2540 error("Could not parse display number from DISPLAY: %.100s",
2541 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002542 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002543 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002544
Damien Miller34132e52000-01-14 15:45:46 +11002545 /* Look up the host address */
2546 memset(&hints, 0, sizeof(hints));
2547 hints.ai_family = IPv4or6;
2548 hints.ai_socktype = SOCK_STREAM;
2549 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2550 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2551 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002552 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002553 }
Damien Miller34132e52000-01-14 15:45:46 +11002554 for (ai = aitop; ai; ai = ai->ai_next) {
2555 /* Create a socket. */
2556 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2557 if (sock < 0) {
2558 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002559 continue;
2560 }
2561 /* Connect it to the display. */
2562 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2563 debug("connect %.100s port %d: %.100s", buf,
2564 6000 + display_number, strerror(errno));
2565 close(sock);
2566 continue;
2567 }
2568 /* Success */
2569 break;
Damien Miller34132e52000-01-14 15:45:46 +11002570 }
Damien Miller34132e52000-01-14 15:45:46 +11002571 freeaddrinfo(aitop);
2572 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002573 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002574 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002575 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002576 }
Damien Millerbd483e72000-04-30 10:00:53 +10002577 return sock;
2578}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002579
Damien Millerbd483e72000-04-30 10:00:53 +10002580/*
2581 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2582 * the remote channel number. We should do whatever we want, and respond
2583 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2584 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002585
Damien Millerbd483e72000-04-30 10:00:53 +10002586void
Damien Miller62cee002000-09-23 17:15:56 +11002587x11_input_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002588{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002589 Channel *c = NULL;
2590 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002591 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10002592
2593 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002594
2595 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002596
2597 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002598 remote_host = packet_get_string(NULL);
2599 } else {
2600 remote_host = xstrdup("unknown (remote did not supply name)");
2601 }
2602 packet_done();
Damien Millerbd483e72000-04-30 10:00:53 +10002603
2604 /* Obtain a connection to the real X display. */
2605 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002606 if (sock != -1) {
2607 /* Allocate a channel for this connection. */
2608 c = channel_new("connected x11 socket",
2609 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
2610 remote_host, 1);
2611 if (c == NULL) {
2612 error("x11_input_open: channel_new failed");
2613 close(sock);
2614 } else {
2615 c->remote_id = remote_id;
2616 }
2617 }
2618 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10002619 /* Send refusal to the remote host. */
2620 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002621 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10002622 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10002623 /* Send a confirmation to the remote host. */
2624 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002625 packet_put_int(remote_id);
2626 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10002627 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002628 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002629}
2630
Damien Miller69b69aa2000-10-28 14:19:58 +11002631/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2632void
2633deny_input_open(int type, int plen, void *ctxt)
2634{
2635 int rchan = packet_get_int();
2636 switch(type){
2637 case SSH_SMSG_AGENT_OPEN:
2638 error("Warning: ssh server tried agent forwarding.");
2639 break;
2640 case SSH_SMSG_X11_OPEN:
2641 error("Warning: ssh server tried X11 forwarding.");
2642 break;
2643 default:
2644 error("deny_input_open: type %d plen %d", type, plen);
2645 break;
2646 }
2647 error("Warning: this is probably a break in attempt by a malicious server.");
2648 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2649 packet_put_int(rchan);
2650 packet_send();
2651}
2652
Damien Miller5428f641999-11-25 11:54:57 +11002653/*
2654 * Requests forwarding of X11 connections, generates fake authentication
2655 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00002656 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11002657 */
Damien Miller4af51302000-04-16 11:18:38 +10002658void
Damien Millerbd483e72000-04-30 10:00:53 +10002659x11_request_forwarding_with_spoofing(int client_session_id,
2660 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002661{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002662 u_int data_len = (u_int) strlen(data) / 2;
Ben Lindstromb3211a82001-02-10 22:33:19 +00002663 u_int i, value, len;
Damien Miller95def091999-11-25 00:26:21 +11002664 char *new_data;
2665 int screen_number;
2666 const char *cp;
2667 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002668
Damien Miller95def091999-11-25 00:26:21 +11002669 cp = getenv("DISPLAY");
2670 if (cp)
2671 cp = strchr(cp, ':');
2672 if (cp)
2673 cp = strchr(cp, '.');
2674 if (cp)
2675 screen_number = atoi(cp + 1);
2676 else
2677 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002678
Damien Miller95def091999-11-25 00:26:21 +11002679 /* Save protocol name. */
2680 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002681
Damien Miller5428f641999-11-25 11:54:57 +11002682 /*
2683 * Extract real authentication data and generate fake data of the
2684 * same length.
2685 */
Damien Miller95def091999-11-25 00:26:21 +11002686 x11_saved_data = xmalloc(data_len);
2687 x11_fake_data = xmalloc(data_len);
2688 for (i = 0; i < data_len; i++) {
2689 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2690 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2691 if (i % 4 == 0)
2692 rand = arc4random();
2693 x11_saved_data[i] = value;
2694 x11_fake_data[i] = rand & 0xff;
2695 rand >>= 8;
2696 }
2697 x11_saved_data_len = data_len;
2698 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002699
Damien Miller95def091999-11-25 00:26:21 +11002700 /* Convert the fake data into hex. */
Ben Lindstromb3211a82001-02-10 22:33:19 +00002701 len = 2 * data_len + 1;
2702 new_data = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +11002703 for (i = 0; i < data_len; i++)
Ben Lindstromb3211a82001-02-10 22:33:19 +00002704 snprintf(new_data + 2 * i, len - 2 * i,
2705 "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002706
Damien Miller95def091999-11-25 00:26:21 +11002707 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002708 if (compat20) {
2709 channel_request_start(client_session_id, "x11-req", 0);
2710 packet_put_char(0); /* XXX bool single connection */
2711 } else {
2712 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2713 }
2714 packet_put_cstring(proto);
2715 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002716 packet_put_int(screen_number);
2717 packet_send();
2718 packet_write_wait();
2719 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002720}
2721
Ben Lindstrome9c99912001-06-09 00:41:05 +00002722
2723/* -- agent forwarding */
2724
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002725/* Sends a message to the server to request authentication fd forwarding. */
2726
Damien Miller4af51302000-04-16 11:18:38 +10002727void
Damien Miller95def091999-11-25 00:26:21 +11002728auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002729{
Damien Miller95def091999-11-25 00:26:21 +11002730 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2731 packet_send();
2732 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002733}
2734
Damien Miller5428f641999-11-25 11:54:57 +11002735/*
2736 * Returns the name of the forwarded authentication socket. Returns NULL if
2737 * there is no forwarded authentication socket. The returned value points to
2738 * a static buffer.
2739 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002740
Damien Miller95def091999-11-25 00:26:21 +11002741char *
2742auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002743{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002744 return auth_sock_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002745}
2746
2747/* removes the agent forwarding socket */
2748
Damien Miller4af51302000-04-16 11:18:38 +10002749void
Ben Lindstrom983c0982001-06-09 01:20:06 +00002750auth_sock_cleanup_proc(void *_pw)
Damien Miller95def091999-11-25 00:26:21 +11002751{
Ben Lindstrom983c0982001-06-09 01:20:06 +00002752 struct passwd *pw = _pw;
2753
Ben Lindstrom838394c2001-06-09 01:11:59 +00002754 if (auth_sock_name) {
Ben Lindstrom983c0982001-06-09 01:20:06 +00002755 temporarily_use_uid(pw);
Ben Lindstrom838394c2001-06-09 01:11:59 +00002756 unlink(auth_sock_name);
2757 rmdir(auth_sock_dir);
2758 auth_sock_name = NULL;
Ben Lindstrom983c0982001-06-09 01:20:06 +00002759 restore_uid();
Ben Lindstrom838394c2001-06-09 01:11:59 +00002760 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002761}
2762
Damien Miller5428f641999-11-25 11:54:57 +11002763/*
Damien Millerd3a18572000-06-07 19:55:44 +10002764 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002765 * This starts forwarding authentication requests.
2766 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002767
Damien Millerd3a18572000-06-07 19:55:44 +10002768int
Damien Miller95def091999-11-25 00:26:21 +11002769auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002770{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002771 Channel *nc;
2772 int sock;
Damien Miller95def091999-11-25 00:26:21 +11002773 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002774
Ben Lindstrome9c99912001-06-09 00:41:05 +00002775 if (auth_get_socket_name() != NULL) {
2776 error("authentication forwarding requested twice.");
2777 return 0;
2778 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002779
Damien Miller95def091999-11-25 00:26:21 +11002780 /* Temporarily drop privileged uid for mkdir/bind. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00002781 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002782
Damien Miller95def091999-11-25 00:26:21 +11002783 /* Allocate a buffer for the socket name, and format the name. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002784 auth_sock_name = xmalloc(MAXPATHLEN);
2785 auth_sock_dir = xmalloc(MAXPATHLEN);
2786 strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002787
Damien Miller95def091999-11-25 00:26:21 +11002788 /* Create private directory for socket */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002789 if (mkdtemp(auth_sock_dir) == NULL) {
2790 packet_send_debug("Agent forwarding disabled: "
2791 "mkdtemp() failed: %.100s", strerror(errno));
Damien Millerd3a18572000-06-07 19:55:44 +10002792 restore_uid();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002793 xfree(auth_sock_name);
2794 xfree(auth_sock_dir);
2795 auth_sock_name = NULL;
2796 auth_sock_dir = NULL;
Damien Millerd3a18572000-06-07 19:55:44 +10002797 return 0;
2798 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002799 snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%d",
2800 auth_sock_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002801
Ben Lindstrom838394c2001-06-09 01:11:59 +00002802 /* delete agent socket on fatal() */
Ben Lindstrom983c0982001-06-09 01:20:06 +00002803 fatal_add_cleanup(auth_sock_cleanup_proc, pw);
Ben Lindstrom838394c2001-06-09 01:11:59 +00002804
Damien Miller95def091999-11-25 00:26:21 +11002805 /* Create the socket. */
2806 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2807 if (sock < 0)
2808 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002809
Damien Miller95def091999-11-25 00:26:21 +11002810 /* Bind it to the name. */
2811 memset(&sunaddr, 0, sizeof(sunaddr));
2812 sunaddr.sun_family = AF_UNIX;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002813 strncpy(sunaddr.sun_path, auth_sock_name,
Damien Miller95def091999-11-25 00:26:21 +11002814 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002815
Damien Miller95def091999-11-25 00:26:21 +11002816 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2817 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002818
Damien Miller95def091999-11-25 00:26:21 +11002819 /* Restore the privileged uid. */
2820 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002821
Damien Miller95def091999-11-25 00:26:21 +11002822 /* Start listening on the socket. */
2823 if (listen(sock, 5) < 0)
2824 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002825
Damien Miller95def091999-11-25 00:26:21 +11002826 /* Allocate a channel for the authentication agent socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002827 nc = channel_new("auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11002828 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2829 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2830 0, xstrdup("auth socket"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002831 if (nc == NULL) {
2832 error("auth_input_request_forwarding: channel_new failed");
Ben Lindstrom983c0982001-06-09 01:20:06 +00002833 auth_sock_cleanup_proc(pw);
Ben Lindstromdf4981b2001-06-09 01:32:29 +00002834 fatal_remove_cleanup(auth_sock_cleanup_proc, pw);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002835 close(sock);
2836 return 0;
2837 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002838 strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
Damien Millerd3a18572000-06-07 19:55:44 +10002839 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002840}
2841
2842/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2843
Damien Miller4af51302000-04-16 11:18:38 +10002844void
Damien Miller62cee002000-09-23 17:15:56 +11002845auth_input_open_request(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002846{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002847 Channel *c = NULL;
2848 int remote_id, sock;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002849 char *name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002850
Damien Millerb38eff82000-04-01 11:09:21 +10002851 packet_integrity_check(plen, 4, type);
2852
Damien Miller95def091999-11-25 00:26:21 +11002853 /* Read the remote channel number from the message. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002854 remote_id = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002855
Damien Miller5428f641999-11-25 11:54:57 +11002856 /*
2857 * Get a connection to the local authentication agent (this may again
2858 * get forwarded).
2859 */
Damien Miller95def091999-11-25 00:26:21 +11002860 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002861
Damien Miller5428f641999-11-25 11:54:57 +11002862 /*
2863 * If we could not connect the agent, send an error message back to
2864 * the server. This should never happen unless the agent dies,
2865 * because authentication forwarding is only enabled if we have an
2866 * agent.
2867 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002868 if (sock >= 0) {
Ben Lindstrome9c99912001-06-09 00:41:05 +00002869 name = xstrdup("authentication agent connection");
2870 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
2871 -1, 0, 0, 0, name, 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002872 if (c == NULL) {
2873 error("auth_input_open_request: channel_new failed");
Ben Lindstrome9c99912001-06-09 00:41:05 +00002874 xfree(name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002875 close(sock);
2876 } else {
2877 c->remote_id = remote_id;
2878 }
Damien Miller95def091999-11-25 00:26:21 +11002879 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002880 if (c == NULL) {
2881 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2882 packet_put_int(remote_id);
2883 } else {
2884 /* Send a confirmation to the remote host. */
2885 debug("Forwarding authentication connection.");
2886 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2887 packet_put_int(remote_id);
2888 packet_put_int(c->self);
2889 }
Damien Miller95def091999-11-25 00:26:21 +11002890 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002891}