blob: 283a70323ad5cc951f84879f95a4ea58f8297eb6 [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 Lindstrom664408d2001-06-09 01:42:01 +000043RCSID("$OpenBSD: channels.c,v 1.125 2001/06/07 20:23:04 markus 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
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000137/* AF_UNSPEC or AF_INET or AF_INET6 */
138extern int IPv4or6;
139
Ben Lindstrome9c99912001-06-09 00:41:05 +0000140/* helper */
141void port_open_helper(Channel *c, char *rtype);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000142
Ben Lindstrome9c99912001-06-09 00:41:05 +0000143/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000144
145Channel *
146channel_lookup(int id)
147{
148 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000149
Damien Millerc0fd17f2000-06-26 10:22:53 +1000150 if (id < 0 || id > channels_alloc) {
Damien Millerb38eff82000-04-01 11:09:21 +1000151 log("channel_lookup: %d: bad id", id);
152 return NULL;
153 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000154 c = channels[id];
155 if (c == NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +1000156 log("channel_lookup: %d: bad id: channel free", id);
157 return NULL;
158 }
159 return c;
160}
161
Damien Miller5428f641999-11-25 11:54:57 +1100162/*
Damien Millere247cc42000-05-07 12:03:14 +1000163 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000164 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100165 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166
Damien Miller6f83b8e2000-05-02 09:23:45 +1000167void
Damien Miller69b69aa2000-10-28 14:19:58 +1100168channel_register_fds(Channel *c, int rfd, int wfd, int efd,
169 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170{
Damien Miller95def091999-11-25 00:26:21 +1100171 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100172 channel_max_fd = MAX(channel_max_fd, rfd);
173 channel_max_fd = MAX(channel_max_fd, wfd);
174 channel_max_fd = MAX(channel_max_fd, efd);
175
Damien Miller5428f641999-11-25 11:54:57 +1100176 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000177
Damien Miller6f83b8e2000-05-02 09:23:45 +1000178 c->rfd = rfd;
179 c->wfd = wfd;
180 c->sock = (rfd == wfd) ? rfd : -1;
181 c->efd = efd;
182 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100183
Damien Miller79438cc2001-02-16 12:34:57 +1100184 /* XXX ugly hack: nonblock is only set by the server */
185 if (nonblock && isatty(c->rfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000186 debug("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100187 c->isatty = 1;
188 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000189 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100190 c->self, c->wfd);
191 }
192 } else {
193 c->isatty = 0;
194 }
195
Damien Miller69b69aa2000-10-28 14:19:58 +1100196 /* enable nonblocking mode */
197 if (nonblock) {
198 if (rfd != -1)
199 set_nonblock(rfd);
200 if (wfd != -1)
201 set_nonblock(wfd);
202 if (efd != -1)
203 set_nonblock(efd);
204 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000205}
206
207/*
208 * Allocate a new channel object and set its type and socket. This will cause
209 * remote_name to be freed.
210 */
211
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000212Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000213channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Damien Miller69b69aa2000-10-28 14:19:58 +1100214 int window, int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000215{
216 int i, found;
217 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000218
Damien Miller95def091999-11-25 00:26:21 +1100219 /* Do initial allocation if this is the first call. */
220 if (channels_alloc == 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000221 chan_init();
Damien Miller95def091999-11-25 00:26:21 +1100222 channels_alloc = 10;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000223 channels = xmalloc(channels_alloc * sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100224 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000225 channels[i] = NULL;
Damien Miller5428f641999-11-25 11:54:57 +1100226 /*
227 * Kludge: arrange a call to channel_stop_listening if we
228 * terminate with fatal().
229 */
Damien Miller95def091999-11-25 00:26:21 +1100230 fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL);
231 }
232 /* Try to find a free slot where to put the new channel. */
233 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000234 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100235 /* Found a free slot. */
236 found = i;
237 break;
238 }
239 if (found == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100240 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100241 found = channels_alloc;
242 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100243 debug2("channel: expanding %d", channels_alloc);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000244 channels = xrealloc(channels, channels_alloc * sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100245 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000246 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100247 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000248 /* Initialize and return new channel. */
249 c = channels[found] = xmalloc(sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100250 buffer_init(&c->input);
251 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000252 buffer_init(&c->extended);
Damien Miller95def091999-11-25 00:26:21 +1100253 chan_init_iostates(c);
Damien Miller69b69aa2000-10-28 14:19:58 +1100254 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100255 c->self = found;
256 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000257 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000258 c->local_window = window;
259 c->local_window_max = window;
260 c->local_consumed = 0;
261 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100262 c->remote_id = -1;
263 c->remote_name = remote_name;
Damien Millerb38eff82000-04-01 11:09:21 +1000264 c->remote_window = 0;
265 c->remote_maxpacket = 0;
266 c->cb_fn = NULL;
267 c->cb_arg = NULL;
268 c->cb_event = 0;
269 c->dettach_user = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000270 c->input_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100271 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000272 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000274
275/* Close all channel fd/socket. */
276
277void
278channel_close_fds(Channel *c)
279{
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000280 debug3("channel_close_fds: channel %d: r %d w %d e %d",
281 c->self, c->rfd, c->wfd, c->efd);
282
Damien Miller6f83b8e2000-05-02 09:23:45 +1000283 if (c->sock != -1) {
284 close(c->sock);
285 c->sock = -1;
286 }
287 if (c->rfd != -1) {
288 close(c->rfd);
289 c->rfd = -1;
290 }
291 if (c->wfd != -1) {
292 close(c->wfd);
293 c->wfd = -1;
294 }
295 if (c->efd != -1) {
296 close(c->efd);
297 c->efd = -1;
298 }
299}
300
301/* Free the channel and close its fd/socket. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000302
Damien Miller4af51302000-04-16 11:18:38 +1000303void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000304channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000305{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000306 char *s;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000307 int i, n;
308
309 for (n = 0, i = 0; i < channels_alloc; i++)
310 if (channels[i])
311 n++;
Ben Lindstrom4c247552001-06-05 20:56:47 +0000312 debug("channel_free: channel %d: %s, nchannels %d", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000313 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000314
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000315 s = channel_open_message();
Ben Lindstrom4c247552001-06-05 20:56:47 +0000316 debug3("channel_free: status: %s", s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000317 xfree(s);
318
Damien Miller33b13562000-04-04 14:38:59 +1000319 if (c->dettach_user != NULL) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000320 debug("channel_free: channel %d: dettaching channel user", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000321 c->dettach_user(c->self, NULL);
322 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000323 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000324 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000325 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000326 buffer_free(&c->input);
327 buffer_free(&c->output);
328 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000329 if (c->remote_name) {
330 xfree(c->remote_name);
331 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100332 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000333 channels[c->self] = NULL;
334 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000335}
336
Ben Lindstrome9c99912001-06-09 00:41:05 +0000337
338/*
339 * Stops listening for channels, and removes any unix domain sockets that we
340 * might have.
341 */
342
343void
344channel_stop_listening()
345{
346 int i;
347 Channel *c;
348
349 for (i = 0; i < channels_alloc; i++) {
350 c = channels[i];
351 if (c != NULL) {
352 switch (c->type) {
353 case SSH_CHANNEL_AUTH_SOCKET:
354 close(c->sock);
Ben Lindstromdf4981b2001-06-09 01:32:29 +0000355 /* auth_sock_cleanup_proc deletes the socket */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000356 channel_free(c);
357 break;
358 case SSH_CHANNEL_PORT_LISTENER:
359 case SSH_CHANNEL_RPORT_LISTENER:
360 case SSH_CHANNEL_X11_LISTENER:
361 close(c->sock);
362 channel_free(c);
363 break;
364 default:
365 break;
366 }
367 }
368 }
369}
370
371/*
372 * Closes the sockets/fds of all channels. This is used to close extra file
373 * descriptors after a fork.
374 */
375
376void
377channel_close_all()
378{
379 int i;
380
381 for (i = 0; i < channels_alloc; i++)
382 if (channels[i] != NULL)
383 channel_close_fds(channels[i]);
384}
385
386/*
387 * Returns true if no channel has too much buffered data, and false if one or
388 * more channel is overfull.
389 */
390
391int
392channel_not_very_much_buffered_data()
393{
394 u_int i;
395 Channel *c;
396
397 for (i = 0; i < channels_alloc; i++) {
398 c = channels[i];
399 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
400 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
401 debug("channel %d: big input buffer %d",
402 c->self, buffer_len(&c->input));
403 return 0;
404 }
405 if (buffer_len(&c->output) > packet_get_maxsize()) {
406 debug("channel %d: big output buffer %d",
407 c->self, buffer_len(&c->output));
408 return 0;
409 }
410 }
411 }
412 return 1;
413}
414
415/* Returns true if any channel is still open. */
416
417int
418channel_still_open()
419{
420 int i;
421 Channel *c;
422
423 for (i = 0; i < channels_alloc; i++) {
424 c = channels[i];
425 if (c == NULL)
426 continue;
427 switch (c->type) {
428 case SSH_CHANNEL_X11_LISTENER:
429 case SSH_CHANNEL_PORT_LISTENER:
430 case SSH_CHANNEL_RPORT_LISTENER:
431 case SSH_CHANNEL_CLOSED:
432 case SSH_CHANNEL_AUTH_SOCKET:
433 case SSH_CHANNEL_DYNAMIC:
434 case SSH_CHANNEL_CONNECTING:
435 case SSH_CHANNEL_ZOMBIE:
436 continue;
437 case SSH_CHANNEL_LARVAL:
438 if (!compat20)
439 fatal("cannot happen: SSH_CHANNEL_LARVAL");
440 continue;
441 case SSH_CHANNEL_OPENING:
442 case SSH_CHANNEL_OPEN:
443 case SSH_CHANNEL_X11_OPEN:
444 return 1;
445 case SSH_CHANNEL_INPUT_DRAINING:
446 case SSH_CHANNEL_OUTPUT_DRAINING:
447 if (!compat13)
448 fatal("cannot happen: OUT_DRAIN");
449 return 1;
450 default:
451 fatal("channel_still_open: bad channel type %d", c->type);
452 /* NOTREACHED */
453 }
454 }
455 return 0;
456}
457
458/* Returns the id of an open channel suitable for keepaliving */
459
460int
461channel_find_open()
462{
463 int i;
464 Channel *c;
465
466 for (i = 0; i < channels_alloc; i++) {
467 c = channels[i];
468 if (c == NULL)
469 continue;
470 switch (c->type) {
471 case SSH_CHANNEL_CLOSED:
472 case SSH_CHANNEL_DYNAMIC:
473 case SSH_CHANNEL_X11_LISTENER:
474 case SSH_CHANNEL_PORT_LISTENER:
475 case SSH_CHANNEL_RPORT_LISTENER:
476 case SSH_CHANNEL_OPENING:
477 case SSH_CHANNEL_CONNECTING:
478 case SSH_CHANNEL_ZOMBIE:
479 continue;
480 case SSH_CHANNEL_LARVAL:
481 case SSH_CHANNEL_AUTH_SOCKET:
482 case SSH_CHANNEL_OPEN:
483 case SSH_CHANNEL_X11_OPEN:
484 return i;
485 case SSH_CHANNEL_INPUT_DRAINING:
486 case SSH_CHANNEL_OUTPUT_DRAINING:
487 if (!compat13)
488 fatal("cannot happen: OUT_DRAIN");
489 return i;
490 default:
491 fatal("channel_find_open: bad channel type %d", c->type);
492 /* NOTREACHED */
493 }
494 }
495 return -1;
496}
497
498
499/*
500 * Returns a message describing the currently open forwarded connections,
501 * suitable for sending to the client. The message contains crlf pairs for
502 * newlines.
503 */
504
505char *
506channel_open_message()
507{
508 Buffer buffer;
509 Channel *c;
510 char buf[1024], *cp;
511 int i;
512
513 buffer_init(&buffer);
514 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
515 buffer_append(&buffer, buf, strlen(buf));
516 for (i = 0; i < channels_alloc; i++) {
517 c = channels[i];
518 if (c == NULL)
519 continue;
520 switch (c->type) {
521 case SSH_CHANNEL_X11_LISTENER:
522 case SSH_CHANNEL_PORT_LISTENER:
523 case SSH_CHANNEL_RPORT_LISTENER:
524 case SSH_CHANNEL_CLOSED:
525 case SSH_CHANNEL_AUTH_SOCKET:
526 case SSH_CHANNEL_ZOMBIE:
527 continue;
528 case SSH_CHANNEL_LARVAL:
529 case SSH_CHANNEL_OPENING:
530 case SSH_CHANNEL_CONNECTING:
531 case SSH_CHANNEL_DYNAMIC:
532 case SSH_CHANNEL_OPEN:
533 case SSH_CHANNEL_X11_OPEN:
534 case SSH_CHANNEL_INPUT_DRAINING:
535 case SSH_CHANNEL_OUTPUT_DRAINING:
536 snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
537 c->self, c->remote_name,
538 c->type, c->remote_id,
539 c->istate, buffer_len(&c->input),
540 c->ostate, buffer_len(&c->output),
541 c->rfd, c->wfd);
542 buffer_append(&buffer, buf, strlen(buf));
543 continue;
544 default:
545 fatal("channel_open_message: bad channel type %d", c->type);
546 /* NOTREACHED */
547 }
548 }
549 buffer_append(&buffer, "\0", 1);
550 cp = xstrdup(buffer_ptr(&buffer));
551 buffer_free(&buffer);
552 return cp;
553}
554
555void
556channel_send_open(int id)
557{
558 Channel *c = channel_lookup(id);
559 if (c == NULL) {
560 log("channel_send_open: %d: bad id", id);
561 return;
562 }
563 debug("send channel open %d", id);
564 packet_start(SSH2_MSG_CHANNEL_OPEN);
565 packet_put_cstring(c->ctype);
566 packet_put_int(c->self);
567 packet_put_int(c->local_window);
568 packet_put_int(c->local_maxpacket);
569 packet_send();
570}
571
572void
573channel_request(int id, char *service, int wantconfirm)
574{
575 channel_request_start(id, service, wantconfirm);
576 packet_send();
577 debug("channel request %d: %s", id, service) ;
578}
579void
580channel_request_start(int id, char *service, int wantconfirm)
581{
582 Channel *c = channel_lookup(id);
583 if (c == NULL) {
584 log("channel_request: %d: bad id", id);
585 return;
586 }
587 packet_start(SSH2_MSG_CHANNEL_REQUEST);
588 packet_put_int(c->remote_id);
589 packet_put_cstring(service);
590 packet_put_char(wantconfirm);
591}
592void
593channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
594{
595 Channel *c = channel_lookup(id);
596 if (c == NULL) {
597 log("channel_register_callback: %d: bad id", id);
598 return;
599 }
600 c->cb_event = mtype;
601 c->cb_fn = fn;
602 c->cb_arg = arg;
603}
604void
605channel_register_cleanup(int id, channel_callback_fn *fn)
606{
607 Channel *c = channel_lookup(id);
608 if (c == NULL) {
609 log("channel_register_cleanup: %d: bad id", id);
610 return;
611 }
612 c->dettach_user = fn;
613}
614void
615channel_cancel_cleanup(int id)
616{
617 Channel *c = channel_lookup(id);
618 if (c == NULL) {
619 log("channel_cancel_cleanup: %d: bad id", id);
620 return;
621 }
622 c->dettach_user = NULL;
623}
624void
625channel_register_filter(int id, channel_filter_fn *fn)
626{
627 Channel *c = channel_lookup(id);
628 if (c == NULL) {
629 log("channel_register_filter: %d: bad id", id);
630 return;
631 }
632 c->input_filter = fn;
633}
634
635void
636channel_set_fds(int id, int rfd, int wfd, int efd,
637 int extusage, int nonblock)
638{
639 Channel *c = channel_lookup(id);
640 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
641 fatal("channel_activate for non-larval channel %d.", id);
642 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
643 c->type = SSH_CHANNEL_OPEN;
644 /* XXX window size? */
645 c->local_window = c->local_window_max = c->local_maxpacket * 2;
646 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
647 packet_put_int(c->remote_id);
648 packet_put_int(c->local_window);
649 packet_send();
650}
651
Damien Miller5428f641999-11-25 11:54:57 +1100652/*
Damien Millerb38eff82000-04-01 11:09:21 +1000653 * 'channel_pre*' are called just before select() to add any bits relevant to
654 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100655 */
Damien Millerb38eff82000-04-01 11:09:21 +1000656/*
657 * 'channel_post*': perform any appropriate operations for channels which
658 * have events pending.
659 */
660typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
661chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
662chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
663
664void
665channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
666{
667 FD_SET(c->sock, readset);
668}
669
670void
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000671channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
672{
673 debug3("channel %d: waiting for connection", c->self);
674 FD_SET(c->sock, writeset);
675}
676
677void
Damien Millerb38eff82000-04-01 11:09:21 +1000678channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
679{
680 if (buffer_len(&c->input) < packet_get_maxsize())
681 FD_SET(c->sock, readset);
682 if (buffer_len(&c->output) > 0)
683 FD_SET(c->sock, writeset);
684}
685
686void
687channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
688{
689 /* test whether sockets are 'alive' for read/write */
690 if (c->istate == CHAN_INPUT_OPEN)
691 if (buffer_len(&c->input) < packet_get_maxsize())
692 FD_SET(c->sock, readset);
693 if (c->ostate == CHAN_OUTPUT_OPEN ||
694 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
695 if (buffer_len(&c->output) > 0) {
696 FD_SET(c->sock, writeset);
697 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
698 chan_obuf_empty(c);
699 }
700 }
701}
702
703void
Damien Miller33b13562000-04-04 14:38:59 +1000704channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
705{
706 if (c->istate == CHAN_INPUT_OPEN &&
707 c->remote_window > 0 &&
708 buffer_len(&c->input) < c->remote_window)
709 FD_SET(c->rfd, readset);
710 if (c->ostate == CHAN_OUTPUT_OPEN ||
711 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
712 if (buffer_len(&c->output) > 0) {
713 FD_SET(c->wfd, writeset);
714 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
715 chan_obuf_empty(c);
716 }
717 }
718 /** XXX check close conditions, too */
719 if (c->efd != -1) {
720 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
721 buffer_len(&c->extended) > 0)
722 FD_SET(c->efd, writeset);
723 else if (c->extended_usage == CHAN_EXTENDED_READ &&
724 buffer_len(&c->extended) < c->remote_window)
725 FD_SET(c->efd, readset);
726 }
727}
728
729void
Damien Millerb38eff82000-04-01 11:09:21 +1000730channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
731{
732 if (buffer_len(&c->input) == 0) {
733 packet_start(SSH_MSG_CHANNEL_CLOSE);
734 packet_put_int(c->remote_id);
735 packet_send();
736 c->type = SSH_CHANNEL_CLOSED;
Ben Lindstromc486d882001-04-11 16:08:34 +0000737 debug("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000738 }
739}
740
741void
742channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
743{
744 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000745 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000746 else
Damien Millerb38eff82000-04-01 11:09:21 +1000747 FD_SET(c->sock, writeset);
748}
749
750/*
751 * This is a special state for X11 authentication spoofing. An opened X11
752 * connection (when authentication spoofing is being done) remains in this
753 * state until the first packet has been completely read. The authentication
754 * data in that packet is then substituted by the real data if it matches the
755 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000756 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000757 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000758 */
759int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000760x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000761{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000762 u_char *ucp;
763 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000764
765 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000766 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000767 return 0;
768
769 /* Parse the lengths of variable-length fields. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000770 ucp = (u_char *) buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000771 if (ucp[0] == 0x42) { /* Byte order MSB first. */
772 proto_len = 256 * ucp[6] + ucp[7];
773 data_len = 256 * ucp[8] + ucp[9];
774 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
775 proto_len = ucp[6] + 256 * ucp[7];
776 data_len = ucp[8] + 256 * ucp[9];
777 } else {
778 debug("Initial X11 packet contains bad byte order byte: 0x%x",
779 ucp[0]);
780 return -1;
781 }
782
783 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000784 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000785 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
786 return 0;
787
788 /* Check if authentication protocol matches. */
789 if (proto_len != strlen(x11_saved_proto) ||
790 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
791 debug("X11 connection uses different authentication protocol.");
792 return -1;
793 }
794 /* Check if authentication data matches our fake data. */
795 if (data_len != x11_fake_data_len ||
796 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
797 x11_fake_data, x11_fake_data_len) != 0) {
798 debug("X11 auth data does not match fake data.");
799 return -1;
800 }
801 /* Check fake data length */
802 if (x11_fake_data_len != x11_saved_data_len) {
803 error("X11 fake_data_len %d != saved_data_len %d",
804 x11_fake_data_len, x11_saved_data_len);
805 return -1;
806 }
807 /*
808 * Received authentication protocol and data match
809 * our fake data. Substitute the fake data with real
810 * data.
811 */
812 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
813 x11_saved_data, x11_saved_data_len);
814 return 1;
815}
816
817void
818channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
819{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000820 int ret = x11_open_helper(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000821 if (ret == 1) {
822 /* Start normal processing for the channel. */
823 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000824 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000825 } else if (ret == -1) {
826 /*
827 * We have received an X11 connection that has bad
828 * authentication information.
829 */
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000830 log("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000831 buffer_clear(&c->input);
832 buffer_clear(&c->output);
833 close(c->sock);
834 c->sock = -1;
835 c->type = SSH_CHANNEL_CLOSED;
836 packet_start(SSH_MSG_CHANNEL_CLOSE);
837 packet_put_int(c->remote_id);
838 packet_send();
839 }
840}
841
842void
Damien Millerbd483e72000-04-30 10:00:53 +1000843channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000844{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000845 int ret = x11_open_helper(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000846 if (ret == 1) {
847 c->type = SSH_CHANNEL_OPEN;
Damien Miller30c3d422000-05-09 11:02:59 +1000848 if (compat20)
849 channel_pre_open_20(c, readset, writeset);
850 else
851 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000852 } else if (ret == -1) {
853 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerbd483e72000-04-30 10:00:53 +1000854 chan_read_failed(c); /** force close? */
Damien Millerb38eff82000-04-01 11:09:21 +1000855 chan_write_failed(c);
856 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
857 }
858}
859
Ben Lindstromb3921512001-04-11 15:57:50 +0000860/* try to decode a socks4 header */
861int
862channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000863{
864 u_char *p, *host;
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000865 int len, have, i, found;
Ben Lindstromb3921512001-04-11 15:57:50 +0000866 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000867 struct {
868 u_int8_t version;
869 u_int8_t command;
870 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000871 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000872 } s4_req, s4_rsp;
873
Ben Lindstromb3921512001-04-11 15:57:50 +0000874 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000875
876 have = buffer_len(&c->input);
877 len = sizeof(s4_req);
878 if (have < len)
879 return 0;
880 p = buffer_ptr(&c->input);
881 for (found = 0, i = len; i < have; i++) {
882 if (p[i] == '\0') {
883 found = 1;
884 break;
885 }
886 if (i > 1024) {
887 /* the peer is probably sending garbage */
888 debug("channel %d: decode socks4: too long",
889 c->self);
890 return -1;
891 }
892 }
893 if (!found)
894 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000895 buffer_get(&c->input, (char *)&s4_req.version, 1);
896 buffer_get(&c->input, (char *)&s4_req.command, 1);
897 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000898 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000899 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000900 p = buffer_ptr(&c->input);
901 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000902 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000903 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000904 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000905 c->self, len, have);
906 strlcpy(username, p, sizeof(username));
907 buffer_consume(&c->input, len);
908 buffer_consume(&c->input, 1); /* trailing '\0' */
909
Ben Lindstromb3921512001-04-11 15:57:50 +0000910 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000911 strlcpy(c->path, host, sizeof(c->path));
912 c->host_port = ntohs(s4_req.dest_port);
913
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000914 debug("channel %d: dynamic request: socks4 host %s port %u command %u",
915 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000916
Ben Lindstromb3921512001-04-11 15:57:50 +0000917 if (s4_req.command != 1) {
918 debug("channel %d: cannot handle: socks4 cn %d",
919 c->self, s4_req.command);
920 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000921 }
Ben Lindstromb3921512001-04-11 15:57:50 +0000922 s4_rsp.version = 0; /* vn: 0 for reply */
923 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000924 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +0000925 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000926 buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +0000927 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000928}
929
Ben Lindstromb3921512001-04-11 15:57:50 +0000930/* dynamic port forwarding */
931void
932channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
933{
934 u_char *p;
935 int have, ret;
936
937 have = buffer_len(&c->input);
938
939 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +0000940 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +0000941 /* check if the fixed size part of the packet is in buffer. */
942 if (have < 4) {
943 /* need more */
944 FD_SET(c->sock, readset);
945 return;
946 }
947 /* try to guess the protocol */
948 p = buffer_ptr(&c->input);
949 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000950 case 0x04:
951 ret = channel_decode_socks4(c, readset, writeset);
952 break;
Ben Lindstromb3921512001-04-11 15:57:50 +0000953 default:
954 ret = -1;
955 break;
956 }
957 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000958 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +0000959 } else if (ret == 0) {
960 debug2("channel %d: pre_dynamic: need more", c->self);
961 /* need more */
962 FD_SET(c->sock, readset);
963 } else {
964 /* switch to the next state */
965 c->type = SSH_CHANNEL_OPENING;
966 port_open_helper(c, "direct-tcpip");
967 }
968}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000969
Damien Millerb38eff82000-04-01 11:09:21 +1000970/* This is our fake X11 server socket. */
971void
972channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
973{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000974 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +1000975 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000976 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +1000977 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +1100978 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +1000979 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +1000980
981 if (FD_ISSET(c->sock, readset)) {
982 debug("X11 connection requested.");
983 addrlen = sizeof(addr);
984 newsock = accept(c->sock, &addr, &addrlen);
985 if (newsock < 0) {
986 error("accept: %.100s", strerror(errno));
987 return;
988 }
Damien Millerd83ff352001-01-30 09:19:34 +1100989 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +1000990 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +1000991 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +1100992 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +1000993
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000994 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +1000995 SSH_CHANNEL_OPENING, newsock, newsock, -1,
996 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100997 0, xstrdup(buf), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000998 if (nc == NULL) {
999 close(newsock);
1000 xfree(remote_ipaddr);
1001 return;
1002 }
Damien Millerbd483e72000-04-30 10:00:53 +10001003 if (compat20) {
1004 packet_start(SSH2_MSG_CHANNEL_OPEN);
1005 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001006 packet_put_int(nc->self);
Damien Millerbd483e72000-04-30 10:00:53 +10001007 packet_put_int(c->local_window_max);
1008 packet_put_int(c->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +11001009 /* originator ipaddr and port */
1010 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +10001011 if (datafellows & SSH_BUG_X11FWD) {
1012 debug("ssh2 x11 bug compat mode");
1013 } else {
1014 packet_put_int(remote_port);
1015 }
Damien Millerbd483e72000-04-30 10:00:53 +10001016 packet_send();
1017 } else {
1018 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001019 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001020 if (packet_get_protocol_flags() &
1021 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +00001022 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +10001023 packet_send();
1024 }
Damien Millerd83ff352001-01-30 09:19:34 +11001025 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001026 }
1027}
1028
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001029void
1030port_open_helper(Channel *c, char *rtype)
1031{
1032 int direct;
1033 char buf[1024];
1034 char *remote_ipaddr = get_peer_ipaddr(c->sock);
1035 u_short remote_port = get_peer_port(c->sock);
1036
1037 direct = (strcmp(rtype, "direct-tcpip") == 0);
1038
1039 snprintf(buf, sizeof buf,
1040 "%s: listening port %d for %.100s port %d, "
1041 "connect from %.200s port %d",
1042 rtype, c->listening_port, c->path, c->host_port,
1043 remote_ipaddr, remote_port);
1044
1045 xfree(c->remote_name);
1046 c->remote_name = xstrdup(buf);
1047
1048 if (compat20) {
1049 packet_start(SSH2_MSG_CHANNEL_OPEN);
1050 packet_put_cstring(rtype);
1051 packet_put_int(c->self);
1052 packet_put_int(c->local_window_max);
1053 packet_put_int(c->local_maxpacket);
1054 if (direct) {
1055 /* target host, port */
1056 packet_put_cstring(c->path);
1057 packet_put_int(c->host_port);
1058 } else {
1059 /* listen address, port */
1060 packet_put_cstring(c->path);
1061 packet_put_int(c->listening_port);
1062 }
1063 /* originator host and port */
1064 packet_put_cstring(remote_ipaddr);
1065 packet_put_int(remote_port);
1066 packet_send();
1067 } else {
1068 packet_start(SSH_MSG_PORT_OPEN);
1069 packet_put_int(c->self);
1070 packet_put_cstring(c->path);
1071 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001072 if (packet_get_protocol_flags() &
1073 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001074 packet_put_cstring(c->remote_name);
1075 packet_send();
1076 }
1077 xfree(remote_ipaddr);
1078}
1079
Damien Millerb38eff82000-04-01 11:09:21 +10001080/*
1081 * This socket is listening for connections to a forwarded TCP/IP port.
1082 */
1083void
1084channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
1085{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001086 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001087 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001088 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001089 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001090 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001091
Damien Millerb38eff82000-04-01 11:09:21 +10001092 if (FD_ISSET(c->sock, readset)) {
1093 debug("Connection to port %d forwarding "
1094 "to %.100s port %d requested.",
1095 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001096
1097 rtype = (c->type == SSH_CHANNEL_RPORT_LISTENER) ?
1098 "forwarded-tcpip" : "direct-tcpip";
Ben Lindstrom6d618462001-05-10 23:24:49 +00001099 nextstate = (c->host_port == 0 &&
1100 c->type != SSH_CHANNEL_RPORT_LISTENER) ?
1101 SSH_CHANNEL_DYNAMIC : SSH_CHANNEL_OPENING;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001102
Damien Millerb38eff82000-04-01 11:09:21 +10001103 addrlen = sizeof(addr);
1104 newsock = accept(c->sock, &addr, &addrlen);
1105 if (newsock < 0) {
1106 error("accept: %.100s", strerror(errno));
1107 return;
1108 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001109 nc = channel_new(rtype,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001110 nextstate, newsock, newsock, -1,
Damien Millerb38eff82000-04-01 11:09:21 +10001111 c->local_window_max, c->local_maxpacket,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001112 0, xstrdup(rtype), 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001113 if (nc == NULL) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001114 error("channel_post_port_listener: no new channel:");
1115 close(newsock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001116 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001117 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001118 nc->listening_port = c->listening_port;
1119 nc->host_port = c->host_port;
1120 strlcpy(nc->path, c->path, sizeof(nc->path));
1121
1122 if (nextstate != SSH_CHANNEL_DYNAMIC)
1123 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001124 }
1125}
1126
1127/*
1128 * This is the authentication agent socket listening for connections from
1129 * clients.
1130 */
1131void
1132channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
1133{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001134 Channel *nc;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001135 char *name;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001136 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001137 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001138 socklen_t addrlen;
1139
1140 if (FD_ISSET(c->sock, readset)) {
1141 addrlen = sizeof(addr);
1142 newsock = accept(c->sock, &addr, &addrlen);
1143 if (newsock < 0) {
1144 error("accept from auth socket: %.100s", strerror(errno));
1145 return;
1146 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001147 name = xstrdup("accepted auth socket");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001148 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001149 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1150 c->local_window_max, c->local_maxpacket,
Ben Lindstrome9c99912001-06-09 00:41:05 +00001151 0, name, 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001152 if (nc == NULL) {
1153 error("channel_post_auth_listener: channel_new failed");
Ben Lindstrome9c99912001-06-09 00:41:05 +00001154 xfree(name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001155 close(newsock);
1156 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11001157 if (compat20) {
1158 packet_start(SSH2_MSG_CHANNEL_OPEN);
1159 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001160 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001161 packet_put_int(c->local_window_max);
1162 packet_put_int(c->local_maxpacket);
1163 } else {
1164 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001165 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001166 }
Damien Millerb38eff82000-04-01 11:09:21 +10001167 packet_send();
1168 }
1169}
1170
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001171void
1172channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
1173{
Ben Lindstrom69128662001-05-08 20:07:39 +00001174 int err = 0;
1175 int sz = sizeof(err);
1176
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001177 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001178 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err,
1179 &sz) < 0) {
1180 err = errno;
1181 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001182 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001183 if (err == 0) {
1184 debug("channel %d: connected", c->self);
1185 c->type = SSH_CHANNEL_OPEN;
1186 if (compat20) {
1187 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1188 packet_put_int(c->remote_id);
1189 packet_put_int(c->self);
1190 packet_put_int(c->local_window);
1191 packet_put_int(c->local_maxpacket);
1192 } else {
1193 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1194 packet_put_int(c->remote_id);
1195 packet_put_int(c->self);
1196 }
1197 } else {
1198 debug("channel %d: not connected: %s",
1199 c->self, strerror(err));
1200 if (compat20) {
1201 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1202 packet_put_int(c->remote_id);
1203 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1204 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1205 packet_put_cstring(strerror(err));
1206 packet_put_cstring("");
1207 }
1208 } else {
1209 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1210 packet_put_int(c->remote_id);
1211 }
1212 chan_mark_dead(c);
1213 }
1214 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001215 }
1216}
1217
Damien Millerb38eff82000-04-01 11:09:21 +10001218int
1219channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
1220{
1221 char buf[16*1024];
1222 int len;
1223
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001224 if (c->rfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001225 FD_ISSET(c->rfd, readset)) {
1226 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +10001227 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1228 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001229 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +10001230 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001231 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001232 if (c->type != SSH_CHANNEL_OPEN) {
1233 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001234 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001235 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001236 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001237 buffer_consume(&c->output, buffer_len(&c->output));
1238 c->type = SSH_CHANNEL_INPUT_DRAINING;
Ben Lindstrome9c99912001-06-09 00:41:05 +00001239 debug("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001240 } else {
1241 chan_read_failed(c);
1242 }
1243 return -1;
1244 }
Damien Millerad833b32000-08-23 10:46:23 +10001245 if(c->input_filter != NULL) {
1246 if (c->input_filter(c, buf, len) == -1) {
Ben Lindstromc486d882001-04-11 16:08:34 +00001247 debug("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001248 chan_read_failed(c);
1249 }
1250 } else {
1251 buffer_append(&c->input, buf, len);
1252 }
Damien Millerb38eff82000-04-01 11:09:21 +10001253 }
1254 return 1;
1255}
1256int
1257channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
1258{
Ben Lindstrome229b252001-03-05 06:28:06 +00001259 struct termios tio;
Damien Millerb38eff82000-04-01 11:09:21 +10001260 int len;
1261
1262 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001263 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001264 FD_ISSET(c->wfd, writeset) &&
1265 buffer_len(&c->output) > 0) {
1266 len = write(c->wfd, buffer_ptr(&c->output),
Damien Miller6f83b8e2000-05-02 09:23:45 +10001267 buffer_len(&c->output));
1268 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1269 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001270 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001271 if (c->type != SSH_CHANNEL_OPEN) {
1272 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001273 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001274 return -1;
1275 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001276 buffer_consume(&c->output, buffer_len(&c->output));
Ben Lindstrome9c99912001-06-09 00:41:05 +00001277 debug("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001278 c->type = SSH_CHANNEL_INPUT_DRAINING;
1279 } else {
1280 chan_write_failed(c);
1281 }
1282 return -1;
1283 }
Damien Miller79438cc2001-02-16 12:34:57 +11001284 if (compat20 && c->isatty) {
Damien Miller79438cc2001-02-16 12:34:57 +11001285 if (tcgetattr(c->wfd, &tio) == 0 &&
1286 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1287 /*
1288 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001289 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001290 * size of a SSH2_MSG_CHANNEL_DATA message
1291 * (4 byte channel id + data)
Damien Miller79438cc2001-02-16 12:34:57 +11001292 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001293 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001294 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001295 }
1296 }
Damien Millerb38eff82000-04-01 11:09:21 +10001297 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001298 if (compat20 && len > 0) {
1299 c->local_consumed += len;
1300 }
1301 }
1302 return 1;
1303}
1304int
1305channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
1306{
1307 char buf[16*1024];
1308 int len;
1309
Damien Miller1383bd82000-04-06 12:32:37 +10001310/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001311 if (c->efd != -1) {
1312 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1313 FD_ISSET(c->efd, writeset) &&
1314 buffer_len(&c->extended) > 0) {
1315 len = write(c->efd, buffer_ptr(&c->extended),
1316 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001317 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001318 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001319 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1320 return 1;
1321 if (len <= 0) {
1322 debug2("channel %d: closing write-efd %d",
1323 c->self, c->efd);
1324 close(c->efd);
1325 c->efd = -1;
1326 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001327 buffer_consume(&c->extended, len);
1328 c->local_consumed += len;
1329 }
1330 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1331 FD_ISSET(c->efd, readset)) {
1332 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001333 debug2("channel %d: read %d from efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001334 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001335 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1336 return 1;
1337 if (len <= 0) {
1338 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001339 c->self, c->efd);
1340 close(c->efd);
1341 c->efd = -1;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001342 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001343 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001344 }
Damien Miller33b13562000-04-04 14:38:59 +10001345 }
1346 }
1347 return 1;
1348}
1349int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001350channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001351{
Ben Lindstromb3921512001-04-11 15:57:50 +00001352 if (c->type == SSH_CHANNEL_OPEN &&
1353 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001354 c->local_window < c->local_window_max/2 &&
1355 c->local_consumed > 0) {
1356 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1357 packet_put_int(c->remote_id);
1358 packet_put_int(c->local_consumed);
1359 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001360 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001361 c->self, c->local_window,
1362 c->local_consumed);
1363 c->local_window += c->local_consumed;
1364 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001365 }
1366 return 1;
1367}
1368
1369void
1370channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
1371{
1372 channel_handle_rfd(c, readset, writeset);
1373 channel_handle_wfd(c, readset, writeset);
1374}
1375
1376void
Damien Miller33b13562000-04-04 14:38:59 +10001377channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
1378{
1379 channel_handle_rfd(c, readset, writeset);
1380 channel_handle_wfd(c, readset, writeset);
1381 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001382
1383 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001384}
1385
1386void
Damien Millerb38eff82000-04-01 11:09:21 +10001387channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1388{
1389 int len;
1390 /* Send buffered output data to the socket. */
1391 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1392 len = write(c->sock, buffer_ptr(&c->output),
1393 buffer_len(&c->output));
1394 if (len <= 0)
1395 buffer_consume(&c->output, buffer_len(&c->output));
1396 else
1397 buffer_consume(&c->output, len);
1398 }
1399}
1400
1401void
Damien Miller33b13562000-04-04 14:38:59 +10001402channel_handler_init_20(void)
1403{
1404 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +10001405 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001406 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001407 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001408 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001409 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001410 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001411 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001412
1413 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
1414 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001415 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001416 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001417 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001418 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001419 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_2;
Damien Miller33b13562000-04-04 14:38:59 +10001420}
1421
1422void
Damien Millerb38eff82000-04-01 11:09:21 +10001423channel_handler_init_13(void)
1424{
1425 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1426 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1427 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1428 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1429 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1430 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1431 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001432 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001433 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001434
1435 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
1436 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1437 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1438 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1439 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001440 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001441 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001442}
1443
1444void
1445channel_handler_init_15(void)
1446{
1447 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +10001448 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001449 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1450 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1451 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001452 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001453 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001454
1455 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1456 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1457 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1458 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001459 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001460 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001461}
1462
1463void
1464channel_handler_init(void)
1465{
1466 int i;
1467 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1468 channel_pre[i] = NULL;
1469 channel_post[i] = NULL;
1470 }
Damien Miller33b13562000-04-04 14:38:59 +10001471 if (compat20)
1472 channel_handler_init_20();
1473 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001474 channel_handler_init_13();
1475 else
1476 channel_handler_init_15();
1477}
1478
Damien Miller4af51302000-04-16 11:18:38 +10001479void
Damien Millerb38eff82000-04-01 11:09:21 +10001480channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1481{
1482 static int did_init = 0;
1483 int i;
1484 Channel *c;
1485
1486 if (!did_init) {
1487 channel_handler_init();
1488 did_init = 1;
1489 }
1490 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001491 c = channels[i];
1492 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001493 continue;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001494 if (ftab[c->type] != NULL)
1495 (*ftab[c->type])(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001496 if (chan_is_dead(c)) {
1497 /*
1498 * we have to remove the fd's from the select mask
1499 * before the channels are free'd and the fd's are
1500 * closed
1501 */
1502 if (c->wfd != -1)
1503 FD_CLR(c->wfd, writeset);
1504 if (c->rfd != -1)
1505 FD_CLR(c->rfd, readset);
1506 if (c->efd != -1) {
1507 if (c->extended_usage == CHAN_EXTENDED_READ)
1508 FD_CLR(c->efd, readset);
1509 if (c->extended_usage == CHAN_EXTENDED_WRITE)
1510 FD_CLR(c->efd, writeset);
1511 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001512 channel_free(c);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001513 }
Damien Millerb38eff82000-04-01 11:09:21 +10001514 }
1515}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001516
Ben Lindstrome9c99912001-06-09 00:41:05 +00001517/*
1518 * Allocate/update select bitmasks and add any bits relevant to channels in
1519 * select bitmasks.
1520 */
Damien Miller4af51302000-04-16 11:18:38 +10001521void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001522channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1523 int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001524{
Damien Miller5e953212001-01-30 09:14:00 +11001525 int n;
1526 u_int sz;
1527
1528 n = MAX(*maxfdp, channel_max_fd);
1529
1530 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1531 if (*readsetp == NULL || n > *maxfdp) {
1532 if (*readsetp)
1533 xfree(*readsetp);
1534 if (*writesetp)
1535 xfree(*writesetp);
1536 *readsetp = xmalloc(sz);
1537 *writesetp = xmalloc(sz);
1538 *maxfdp = n;
1539 }
1540 memset(*readsetp, 0, sz);
1541 memset(*writesetp, 0, sz);
1542
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001543 if (!rekeying)
1544 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001545}
1546
Ben Lindstrome9c99912001-06-09 00:41:05 +00001547/*
1548 * After select, perform any appropriate operations for channels which have
1549 * events pending.
1550 */
Damien Miller4af51302000-04-16 11:18:38 +10001551void
Damien Miller95def091999-11-25 00:26:21 +11001552channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001553{
Damien Millerb38eff82000-04-01 11:09:21 +10001554 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001555}
1556
Ben Lindstrome9c99912001-06-09 00:41:05 +00001557
Damien Miller5e953212001-01-30 09:14:00 +11001558/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001559
Damien Miller4af51302000-04-16 11:18:38 +10001560void
Damien Miller95def091999-11-25 00:26:21 +11001561channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001562{
Damien Miller95def091999-11-25 00:26:21 +11001563 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001564 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001565
Damien Miller95def091999-11-25 00:26:21 +11001566 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001567 c = channels[i];
1568 if (c == NULL)
1569 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001570
Ben Lindstrome9c99912001-06-09 00:41:05 +00001571 /*
1572 * We are only interested in channels that can have buffered
1573 * incoming data.
1574 */
Damien Miller34132e52000-01-14 15:45:46 +11001575 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001576 if (c->type != SSH_CHANNEL_OPEN &&
1577 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001578 continue;
1579 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001580 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001581 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001582 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001583 if (compat20 &&
1584 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001585 /* XXX is this true? */
Ben Lindstromb1131e92001-03-05 07:27:13 +00001586 debug2("channel %d: no data after CLOSE", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001587 continue;
1588 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001589
Damien Miller95def091999-11-25 00:26:21 +11001590 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001591 if ((c->istate == CHAN_INPUT_OPEN ||
1592 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1593 (len = buffer_len(&c->input)) > 0) {
Ben Lindstrome9c99912001-06-09 00:41:05 +00001594 /*
1595 * Send some data for the other side over the secure
1596 * connection.
1597 */
Damien Miller33b13562000-04-04 14:38:59 +10001598 if (compat20) {
1599 if (len > c->remote_window)
1600 len = c->remote_window;
1601 if (len > c->remote_maxpacket)
1602 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001603 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001604 if (packet_is_interactive()) {
1605 if (len > 1024)
1606 len = 512;
1607 } else {
1608 /* Keep the packets at reasonable size. */
1609 if (len > packet_get_maxsize()/2)
1610 len = packet_get_maxsize()/2;
1611 }
Damien Miller95def091999-11-25 00:26:21 +11001612 }
Damien Millerb38eff82000-04-01 11:09:21 +10001613 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001614 packet_start(compat20 ?
1615 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001616 packet_put_int(c->remote_id);
1617 packet_put_string(buffer_ptr(&c->input), len);
1618 packet_send();
1619 buffer_consume(&c->input, len);
1620 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001621 }
1622 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001623 if (compat13)
1624 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001625 /*
1626 * input-buffer is empty and read-socket shutdown:
1627 * tell peer, that we will not send more data: send IEOF
1628 */
Damien Millerb38eff82000-04-01 11:09:21 +10001629 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001630 }
Damien Miller33b13562000-04-04 14:38:59 +10001631 /* Send extended data, i.e. stderr */
1632 if (compat20 &&
1633 c->remote_window > 0 &&
1634 (len = buffer_len(&c->extended)) > 0 &&
1635 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001636 debug2("channel %d: rwin %d elen %d euse %d",
1637 c->self, c->remote_window, buffer_len(&c->extended),
1638 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001639 if (len > c->remote_window)
1640 len = c->remote_window;
1641 if (len > c->remote_maxpacket)
1642 len = c->remote_maxpacket;
1643 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1644 packet_put_int(c->remote_id);
1645 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1646 packet_put_string(buffer_ptr(&c->extended), len);
1647 packet_send();
1648 buffer_consume(&c->extended, len);
1649 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001650 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001651 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001652 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001653}
1654
Ben Lindstrome9c99912001-06-09 00:41:05 +00001655
1656/* -- protocol input */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001657
Damien Miller4af51302000-04-16 11:18:38 +10001658void
Damien Miller62cee002000-09-23 17:15:56 +11001659channel_input_data(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001660{
Damien Miller34132e52000-01-14 15:45:46 +11001661 int id;
Damien Miller95def091999-11-25 00:26:21 +11001662 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001663 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001664 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001665
Damien Miller95def091999-11-25 00:26:21 +11001666 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001667 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001668 c = channel_lookup(id);
1669 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001670 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001671
Damien Miller95def091999-11-25 00:26:21 +11001672 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001673 if (c->type != SSH_CHANNEL_OPEN &&
1674 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001675 return;
1676
1677 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001678 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001679 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001680
Damien Miller95def091999-11-25 00:26:21 +11001681 /* Get the data. */
1682 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001683 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001684
Damien Miller33b13562000-04-04 14:38:59 +10001685 if (compat20){
1686 if (data_len > c->local_maxpacket) {
1687 log("channel %d: rcvd big packet %d, maxpack %d",
1688 c->self, data_len, c->local_maxpacket);
1689 }
1690 if (data_len > c->local_window) {
1691 log("channel %d: rcvd too much data %d, win %d",
1692 c->self, data_len, c->local_window);
1693 xfree(data);
1694 return;
1695 }
1696 c->local_window -= data_len;
1697 }else{
1698 packet_integrity_check(plen, 4 + 4 + data_len, type);
1699 }
Damien Millerb38eff82000-04-01 11:09:21 +10001700 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001701 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001702}
Ben Lindstrome9c99912001-06-09 00:41:05 +00001703
Damien Miller4af51302000-04-16 11:18:38 +10001704void
Damien Miller62cee002000-09-23 17:15:56 +11001705channel_input_extended_data(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001706{
1707 int id;
1708 int tcode;
1709 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001710 u_int data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001711 Channel *c;
1712
1713 /* Get the channel number and verify it. */
1714 id = packet_get_int();
1715 c = channel_lookup(id);
1716
1717 if (c == NULL)
1718 packet_disconnect("Received extended_data for bad channel %d.", id);
1719 if (c->type != SSH_CHANNEL_OPEN) {
1720 log("channel %d: ext data for non open", id);
1721 return;
1722 }
1723 tcode = packet_get_int();
1724 if (c->efd == -1 ||
1725 c->extended_usage != CHAN_EXTENDED_WRITE ||
1726 tcode != SSH2_EXTENDED_DATA_STDERR) {
1727 log("channel %d: bad ext data", c->self);
1728 return;
1729 }
1730 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001731 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001732 if (data_len > c->local_window) {
1733 log("channel %d: rcvd too much extended_data %d, win %d",
1734 c->self, data_len, c->local_window);
1735 xfree(data);
1736 return;
1737 }
Damien Millerd3444942000-09-30 14:20:03 +11001738 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001739 c->local_window -= data_len;
1740 buffer_append(&c->extended, data, data_len);
1741 xfree(data);
1742}
1743
Damien Miller4af51302000-04-16 11:18:38 +10001744void
Damien Miller62cee002000-09-23 17:15:56 +11001745channel_input_ieof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001746{
1747 int id;
1748 Channel *c;
1749
1750 packet_integrity_check(plen, 4, type);
1751
1752 id = packet_get_int();
1753 c = channel_lookup(id);
1754 if (c == NULL)
1755 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1756 chan_rcvd_ieof(c);
1757}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001758
Damien Miller4af51302000-04-16 11:18:38 +10001759void
Damien Miller62cee002000-09-23 17:15:56 +11001760channel_input_close(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001761{
Damien Millerb38eff82000-04-01 11:09:21 +10001762 int id;
1763 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001764
Damien Millerb38eff82000-04-01 11:09:21 +10001765 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001766
Damien Millerb38eff82000-04-01 11:09:21 +10001767 id = packet_get_int();
1768 c = channel_lookup(id);
1769 if (c == NULL)
1770 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001771
1772 /*
1773 * Send a confirmation that we have closed the channel and no more
1774 * data is coming for it.
1775 */
Damien Miller95def091999-11-25 00:26:21 +11001776 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001777 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001778 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001779
Damien Miller5428f641999-11-25 11:54:57 +11001780 /*
1781 * If the channel is in closed state, we have sent a close request,
1782 * and the other side will eventually respond with a confirmation.
1783 * Thus, we cannot free the channel here, because then there would be
1784 * no-one to receive the confirmation. The channel gets freed when
1785 * the confirmation arrives.
1786 */
Damien Millerb38eff82000-04-01 11:09:21 +10001787 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001788 /*
1789 * Not a closed channel - mark it as draining, which will
1790 * cause it to be freed later.
1791 */
Damien Millerb38eff82000-04-01 11:09:21 +10001792 buffer_consume(&c->input, buffer_len(&c->input));
1793 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001794 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001795}
1796
Damien Millerb38eff82000-04-01 11:09:21 +10001797/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001798void
Damien Miller62cee002000-09-23 17:15:56 +11001799channel_input_oclose(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001800{
Damien Millerb38eff82000-04-01 11:09:21 +10001801 int id = packet_get_int();
1802 Channel *c = channel_lookup(id);
1803 packet_integrity_check(plen, 4, type);
1804 if (c == NULL)
1805 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1806 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001807}
1808
Damien Miller4af51302000-04-16 11:18:38 +10001809void
Damien Miller62cee002000-09-23 17:15:56 +11001810channel_input_close_confirmation(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001811{
1812 int id = packet_get_int();
1813 Channel *c = channel_lookup(id);
1814
Damien Miller4af51302000-04-16 11:18:38 +10001815 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001816 if (c == NULL)
1817 packet_disconnect("Received close confirmation for "
1818 "out-of-range channel %d.", id);
1819 if (c->type != SSH_CHANNEL_CLOSED)
1820 packet_disconnect("Received close confirmation for "
1821 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001822 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001823}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001824
Damien Miller4af51302000-04-16 11:18:38 +10001825void
Damien Miller62cee002000-09-23 17:15:56 +11001826channel_input_open_confirmation(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001827{
Damien Millerb38eff82000-04-01 11:09:21 +10001828 int id, remote_id;
1829 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001830
Damien Miller33b13562000-04-04 14:38:59 +10001831 if (!compat20)
1832 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001833
Damien Millerb38eff82000-04-01 11:09:21 +10001834 id = packet_get_int();
1835 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001836
Damien Millerb38eff82000-04-01 11:09:21 +10001837 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1838 packet_disconnect("Received open confirmation for "
1839 "non-opening channel %d.", id);
1840 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001841 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001842 c->remote_id = remote_id;
1843 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001844
1845 if (compat20) {
1846 c->remote_window = packet_get_int();
1847 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001848 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001849 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001850 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001851 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001852 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001853 }
1854 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1855 c->remote_window, c->remote_maxpacket);
1856 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001857}
1858
Ben Lindstrom69128662001-05-08 20:07:39 +00001859char *
1860reason2txt(int reason)
1861{
1862 switch(reason) {
1863 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
1864 return "administratively prohibited";
1865 case SSH2_OPEN_CONNECT_FAILED:
1866 return "connect failed";
1867 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
1868 return "unknown channel type";
1869 case SSH2_OPEN_RESOURCE_SHORTAGE:
1870 return "resource shortage";
1871 }
Ben Lindstrome2595442001-06-05 20:01:39 +00001872 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00001873}
1874
Damien Miller4af51302000-04-16 11:18:38 +10001875void
Damien Miller62cee002000-09-23 17:15:56 +11001876channel_input_open_failure(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001877{
Kevin Steves12057502001-02-05 14:54:34 +00001878 int id, reason;
1879 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001880 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001881
Damien Miller33b13562000-04-04 14:38:59 +10001882 if (!compat20)
1883 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001884
1885 id = packet_get_int();
1886 c = channel_lookup(id);
1887
1888 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1889 packet_disconnect("Received open failure for "
1890 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001891 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00001892 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00001893 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00001894 msg = packet_get_string(NULL);
1895 lang = packet_get_string(NULL);
1896 }
Damien Miller4af51302000-04-16 11:18:38 +10001897 packet_done();
Ben Lindstrom69128662001-05-08 20:07:39 +00001898 log("channel %d: open failed: %s%s%s", id,
1899 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00001900 if (msg != NULL)
1901 xfree(msg);
1902 if (lang != NULL)
1903 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001904 }
Damien Miller95def091999-11-25 00:26:21 +11001905 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001906 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001907}
1908
Damien Miller33b13562000-04-04 14:38:59 +10001909void
Damien Miller62cee002000-09-23 17:15:56 +11001910channel_input_channel_request(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001911{
1912 int id;
1913 Channel *c;
1914
1915 id = packet_get_int();
1916 c = channel_lookup(id);
1917
1918 if (c == NULL ||
1919 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1920 packet_disconnect("Received request for "
1921 "non-open channel %d.", id);
1922 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001923 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001924 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001925 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001926 } else {
1927 char *service = packet_get_string(NULL);
Ben Lindstromcc74df72001-03-05 06:20:14 +00001928 debug("channel %d: rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001929 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001930 xfree(service);
1931 }
1932}
1933
Damien Miller4af51302000-04-16 11:18:38 +10001934void
Damien Miller62cee002000-09-23 17:15:56 +11001935channel_input_window_adjust(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001936{
1937 Channel *c;
1938 int id, adjust;
1939
1940 if (!compat20)
1941 return;
1942
1943 /* Get the channel number and verify it. */
1944 id = packet_get_int();
1945 c = channel_lookup(id);
1946
1947 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1948 log("Received window adjust for "
1949 "non-open channel %d.", id);
1950 return;
1951 }
1952 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001953 packet_done();
Damien Millerd3444942000-09-30 14:20:03 +11001954 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10001955 c->remote_window += adjust;
1956}
1957
Damien Miller4af51302000-04-16 11:18:38 +10001958void
Ben Lindstrome9c99912001-06-09 00:41:05 +00001959channel_input_port_open(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001960{
Ben Lindstrome9c99912001-06-09 00:41:05 +00001961 Channel *c = NULL;
1962 u_short host_port;
1963 char *host, *originator_string;
1964 int remote_id, sock = -1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001965
Ben Lindstrome9c99912001-06-09 00:41:05 +00001966 remote_id = packet_get_int();
1967 host = packet_get_string(NULL);
1968 host_port = packet_get_int();
1969
1970 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
1971 originator_string = packet_get_string(NULL);
1972 } else {
1973 originator_string = xstrdup("unknown (remote did not supply name)");
1974 }
1975 packet_done();
1976 sock = channel_connect_to(host, host_port);
1977 if (sock != -1) {
1978 c = channel_new("connected socket",
1979 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
1980 originator_string, 1);
1981 if (c == NULL) {
1982 error("channel_input_port_open: channel_new failed");
1983 close(sock);
1984 } else {
1985 c->remote_id = remote_id;
Damien Miller95def091999-11-25 00:26:21 +11001986 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001987 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001988 if (c == NULL) {
1989 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1990 packet_put_int(remote_id);
1991 packet_send();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001992 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001993 xfree(host);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001994}
1995
1996
Ben Lindstrome9c99912001-06-09 00:41:05 +00001997/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001998
Damien Miller5428f641999-11-25 11:54:57 +11001999/*
2000 * Initiate forwarding of connections to local port "port" through the secure
2001 * channel to host:port from remote side.
2002 */
Kevin Steves12057502001-02-05 14:54:34 +00002003int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002004channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
2005 u_short port_to_connect, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002006{
Kevin Steves12057502001-02-05 14:54:34 +00002007 return channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002008 NULL, listen_port,
2009 host_to_connect, port_to_connect,
2010 gateway_ports, /*remote_fwd*/ 0);
2011}
2012
2013/*
2014 * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
2015 * (SSH_CHANNEL_RPORT_LISTENER).
2016 */
Kevin Steves12057502001-02-05 14:54:34 +00002017int
Damien Miller0bc1bd82000-11-13 22:57:25 +11002018channel_request_forwarding(
2019 const char *listen_address, u_short listen_port,
2020 const char *host_to_connect, u_short port_to_connect,
2021 int gateway_ports, int remote_fwd)
2022{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002023 Channel *c;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002024 int success, sock, on = 1, type;
Damien Miller34132e52000-01-14 15:45:46 +11002025 struct addrinfo hints, *ai, *aitop;
2026 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0bc1bd82000-11-13 22:57:25 +11002027 const char *host;
Damien Miller5428f641999-11-25 11:54:57 +11002028 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002029
Kevin Steves12057502001-02-05 14:54:34 +00002030 success = 0;
2031
Damien Miller0bc1bd82000-11-13 22:57:25 +11002032 if (remote_fwd) {
2033 host = listen_address;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002034 type = SSH_CHANNEL_RPORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002035 } else {
2036 host = host_to_connect;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002037 type = SSH_CHANNEL_PORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002038 }
2039
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002040 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00002041 error("Forward host name too long.");
2042 return success;
2043 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002044
Damien Miller0bc1bd82000-11-13 22:57:25 +11002045 /* XXX listen_address is currently ignored */
Damien Miller5428f641999-11-25 11:54:57 +11002046 /*
Damien Miller34132e52000-01-14 15:45:46 +11002047 * getaddrinfo returns a loopback address if the hostname is
2048 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002049 */
Damien Miller34132e52000-01-14 15:45:46 +11002050 memset(&hints, 0, sizeof(hints));
2051 hints.ai_family = IPv4or6;
2052 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
2053 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002054 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002055 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
2056 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11002057
Damien Miller34132e52000-01-14 15:45:46 +11002058 for (ai = aitop; ai; ai = ai->ai_next) {
2059 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2060 continue;
2061 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2062 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11002063 error("channel_request_forwarding: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002064 continue;
2065 }
2066 /* Create a port to listen for the host. */
2067 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2068 if (sock < 0) {
2069 /* this is no error since kernel may not support ipv6 */
2070 verbose("socket: %.100s", strerror(errno));
2071 continue;
2072 }
2073 /*
2074 * Set socket options. We would like the socket to disappear
2075 * as soon as it has been closed for whatever reason.
2076 */
2077 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
2078 linger.l_onoff = 1;
2079 linger.l_linger = 5;
2080 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
2081 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002082
Damien Miller34132e52000-01-14 15:45:46 +11002083 /* Bind the socket to the address. */
2084 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2085 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002086 if (!ai->ai_next)
2087 error("bind: %.100s", strerror(errno));
2088 else
2089 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002090
Damien Miller34132e52000-01-14 15:45:46 +11002091 close(sock);
2092 continue;
2093 }
2094 /* Start listening for connections on the socket. */
2095 if (listen(sock, 5) < 0) {
2096 error("listen: %.100s", strerror(errno));
2097 close(sock);
2098 continue;
2099 }
2100 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002101 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002102 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002103 0, xstrdup("port listener"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002104 if (c == NULL) {
2105 error("channel_request_forwarding: channel_new failed");
2106 close(sock);
2107 continue;
2108 }
2109 strlcpy(c->path, host, sizeof(c->path));
2110 c->host_port = port_to_connect;
2111 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002112 success = 1;
2113 }
2114 if (success == 0)
Kevin Steves12057502001-02-05 14:54:34 +00002115 error("channel_request_forwarding: cannot listen to port: %d",
2116 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002117 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002118 return success;
Damien Miller95def091999-11-25 00:26:21 +11002119}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002120
Damien Miller5428f641999-11-25 11:54:57 +11002121/*
2122 * Initiate forwarding of connections to port "port" on remote host through
2123 * the secure channel to host:port from local side.
2124 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002125
Damien Miller4af51302000-04-16 11:18:38 +10002126void
Damien Miller0bc1bd82000-11-13 22:57:25 +11002127channel_request_remote_forwarding(u_short listen_port,
2128 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002129{
Damien Miller0bc1bd82000-11-13 22:57:25 +11002130 int payload_len, type, success = 0;
2131
Damien Miller95def091999-11-25 00:26:21 +11002132 /* Record locally that connection to this host/port is permitted. */
2133 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2134 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002135
Damien Miller95def091999-11-25 00:26:21 +11002136 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002137 if (compat20) {
2138 const char *address_to_bind = "0.0.0.0";
2139 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2140 packet_put_cstring("tcpip-forward");
2141 packet_put_char(0); /* boolean: want reply */
2142 packet_put_cstring(address_to_bind);
2143 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002144 packet_send();
2145 packet_write_wait();
2146 /* Assume that server accepts the request */
2147 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002148 } else {
2149 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002150 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002151 packet_put_cstring(host_to_connect);
2152 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002153 packet_send();
2154 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002155
2156 /* Wait for response from the remote side. */
2157 type = packet_read(&payload_len);
2158 switch (type) {
2159 case SSH_SMSG_SUCCESS:
2160 success = 1;
2161 break;
2162 case SSH_SMSG_FAILURE:
2163 log("Warning: Server denied remote port forwarding.");
2164 break;
2165 default:
2166 /* Unknown packet */
2167 packet_disconnect("Protocol error for port forward request:"
2168 "received packet type %d.", type);
2169 }
2170 }
2171 if (success) {
2172 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2173 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2174 permitted_opens[num_permitted_opens].listen_port = listen_port;
2175 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002176 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002177}
2178
Damien Miller5428f641999-11-25 11:54:57 +11002179/*
2180 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2181 * listening for the port, and sends back a success reply (or disconnect
2182 * message if there was an error). This never returns if there was an error.
2183 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002184
Damien Miller4af51302000-04-16 11:18:38 +10002185void
Damien Millere247cc42000-05-07 12:03:14 +10002186channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002187{
Damien Milleraae6c611999-12-06 11:47:28 +11002188 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11002189 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002190
Damien Miller95def091999-11-25 00:26:21 +11002191 /* Get arguments from the packet. */
2192 port = packet_get_int();
2193 hostname = packet_get_string(NULL);
2194 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002195
Damien Millerbac2d8a2000-09-05 16:13:06 +11002196#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002197 /*
2198 * Check that an unprivileged user is not trying to forward a
2199 * privileged port.
2200 */
Damien Miller95def091999-11-25 00:26:21 +11002201 if (port < IPPORT_RESERVED && !is_root)
2202 packet_disconnect("Requested forwarding of port %d but user is not root.",
2203 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11002204#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11002205 /* Initiate forwarding */
Damien Millere247cc42000-05-07 12:03:14 +10002206 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002207
2208 /* Free the argument string. */
2209 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002210}
2211
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002212/*
2213 * Permits opening to any host/port if permitted_opens[] is empty. This is
2214 * usually called by the server, because the user could connect to any port
2215 * anyway, and the server has no way to know but to trust the client anyway.
2216 */
2217void
2218channel_permit_all_opens()
2219{
2220 if (num_permitted_opens == 0)
2221 all_opens_permitted = 1;
2222}
2223
Ben Lindstroma3700052001-04-05 23:26:32 +00002224void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002225channel_add_permitted_opens(char *host, int port)
2226{
2227 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2228 fatal("channel_request_remote_forwarding: too many forwards");
2229 debug("allow port forwarding to host %s port %d", host, port);
2230
2231 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2232 permitted_opens[num_permitted_opens].port_to_connect = port;
2233 num_permitted_opens++;
2234
2235 all_opens_permitted = 0;
2236}
2237
Ben Lindstroma3700052001-04-05 23:26:32 +00002238void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002239channel_clear_permitted_opens(void)
2240{
2241 int i;
2242
2243 for (i = 0; i < num_permitted_opens; i++)
2244 xfree(permitted_opens[i].host_to_connect);
2245 num_permitted_opens = 0;
2246
2247}
2248
2249
2250/* return socket to remote host, port */
Damien Millerb38eff82000-04-01 11:09:21 +10002251int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002252connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002253{
Damien Miller34132e52000-01-14 15:45:46 +11002254 struct addrinfo hints, *ai, *aitop;
2255 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2256 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002257 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002258
Damien Miller34132e52000-01-14 15:45:46 +11002259 memset(&hints, 0, sizeof(hints));
2260 hints.ai_family = IPv4or6;
2261 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002262 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002263 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002264 error("connect_to %.100s: unknown host (%s)", host,
2265 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002266 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002267 }
Damien Miller34132e52000-01-14 15:45:46 +11002268 for (ai = aitop; ai; ai = ai->ai_next) {
2269 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2270 continue;
2271 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2272 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002273 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002274 continue;
2275 }
Damien Miller34132e52000-01-14 15:45:46 +11002276 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2277 if (sock < 0) {
2278 error("socket: %.100s", strerror(errno));
2279 continue;
2280 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +00002281 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002282 fatal("connect_to: F_SETFL: %s", strerror(errno));
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002283 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2284 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002285 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002286 strerror(errno));
2287 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002288 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002289 }
2290 break; /* success */
2291
2292 }
2293 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002294 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002295 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002296 return -1;
2297 }
2298 /* success */
2299 return sock;
2300}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002301
Damien Miller0bc1bd82000-11-13 22:57:25 +11002302int
2303channel_connect_by_listen_adress(u_short listen_port)
2304{
2305 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002306
Damien Miller0bc1bd82000-11-13 22:57:25 +11002307 for (i = 0; i < num_permitted_opens; i++)
2308 if (permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002309 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002310 permitted_opens[i].host_to_connect,
2311 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002312 error("WARNING: Server requests forwarding for unknown listen_port %d",
2313 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002314 return -1;
2315}
Damien Millerb38eff82000-04-01 11:09:21 +10002316
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002317/* Check if connecting to that port is permitted and connect. */
2318int
2319channel_connect_to(const char *host, u_short port)
2320{
2321 int i, permit;
2322
2323 permit = all_opens_permitted;
2324 if (!permit) {
2325 for (i = 0; i < num_permitted_opens; i++)
2326 if (permitted_opens[i].port_to_connect == port &&
2327 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2328 permit = 1;
2329
2330 }
2331 if (!permit) {
2332 log("Received request to connect to host %.100s port %d, "
2333 "but the request was denied.", host, port);
2334 return -1;
2335 }
2336 return connect_to(host, port);
2337}
2338
Ben Lindstrome9c99912001-06-09 00:41:05 +00002339/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002340
Damien Miller5428f641999-11-25 11:54:57 +11002341/*
2342 * Creates an internet domain socket for listening for X11 connections.
2343 * Returns a suitable value for the DISPLAY variable, or NULL if an error
2344 * occurs.
2345 */
Damien Miller95def091999-11-25 00:26:21 +11002346char *
Damien Millera34a28b1999-12-14 10:47:15 +11002347x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002348{
Damien Milleraae6c611999-12-06 11:47:28 +11002349 int display_number, sock;
2350 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002351 struct addrinfo hints, *ai, *aitop;
2352 char strport[NI_MAXSERV];
2353 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
2354 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11002355 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002356
Damien Millera34a28b1999-12-14 10:47:15 +11002357 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11002358 display_number < MAX_DISPLAYS;
2359 display_number++) {
2360 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002361 memset(&hints, 0, sizeof(hints));
2362 hints.ai_family = IPv4or6;
2363 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
2364 hints.ai_socktype = SOCK_STREAM;
2365 snprintf(strport, sizeof strport, "%d", port);
2366 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2367 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11002368 return NULL;
2369 }
Damien Miller34132e52000-01-14 15:45:46 +11002370 for (ai = aitop; ai; ai = ai->ai_next) {
2371 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2372 continue;
2373 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2374 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002375 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002376 error("socket: %.100s", strerror(errno));
2377 return NULL;
2378 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002379 debug("x11_create_display_inet: Socket family %d not supported",
2380 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002381 continue;
2382 }
Damien Miller34132e52000-01-14 15:45:46 +11002383 }
2384 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2385 debug("bind port %d: %.100s", port, strerror(errno));
2386 shutdown(sock, SHUT_RDWR);
2387 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002388
2389 if (ai->ai_next)
2390 continue;
2391
Damien Miller34132e52000-01-14 15:45:46 +11002392 for (n = 0; n < num_socks; n++) {
2393 shutdown(socks[n], SHUT_RDWR);
2394 close(socks[n]);
2395 }
2396 num_socks = 0;
2397 break;
2398 }
2399 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002400#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002401 if (num_socks == NUM_SOCKS)
2402 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002403#else
2404 break;
2405#endif
Damien Miller95def091999-11-25 00:26:21 +11002406 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002407 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002408 if (num_socks > 0)
2409 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002410 }
Damien Miller95def091999-11-25 00:26:21 +11002411 if (display_number >= MAX_DISPLAYS) {
2412 error("Failed to allocate internet-domain X11 display socket.");
2413 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002414 }
Damien Miller95def091999-11-25 00:26:21 +11002415 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002416 for (n = 0; n < num_socks; n++) {
2417 sock = socks[n];
2418 if (listen(sock, 5) < 0) {
2419 error("listen: %.100s", strerror(errno));
2420 shutdown(sock, SHUT_RDWR);
2421 close(sock);
2422 return NULL;
2423 }
Damien Miller95def091999-11-25 00:26:21 +11002424 }
Damien Miller34132e52000-01-14 15:45:46 +11002425
Damien Miller95def091999-11-25 00:26:21 +11002426 /* Set up a suitable value for the DISPLAY variable. */
2427 if (gethostname(hostname, sizeof(hostname)) < 0)
2428 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11002429
2430#ifdef IPADDR_IN_DISPLAY
Kevin Stevesef4eea92001-02-05 12:42:17 +00002431 /*
Damien Miller76112de1999-12-21 11:18:08 +11002432 * HPUX detects the local hostname in the DISPLAY variable and tries
2433 * to set up a shared memory connection to the server, which it
2434 * incorrectly supposes to be local.
2435 *
2436 * The workaround - as used in later $$H and other programs - is
2437 * is to set display to the host's IP address.
2438 */
2439 {
2440 struct hostent *he;
2441 struct in_addr my_addr;
2442
2443 he = gethostbyname(hostname);
2444 if (he == NULL) {
2445 error("[X11-broken-fwd-hostname-workaround] Could not get "
2446 "IP address for hostname %s.", hostname);
2447
2448 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
2449 "Could not get IP address for hostname %s.", hostname);
2450
2451 shutdown(sock, SHUT_RDWR);
2452 close(sock);
2453
2454 return NULL;
2455 }
2456
2457 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2458
2459 /* Set DISPLAY to <ip address>:screen.display */
Kevin Stevesef4eea92001-02-05 12:42:17 +00002460 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Kevin Stevesda6cd592000-12-29 18:41:21 +00002461 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002462 }
2463#else /* IPADDR_IN_DISPLAY */
2464 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11002465 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Kevin Stevesda6cd592000-12-29 18:41:21 +00002466 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002467#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002468
Damien Miller34132e52000-01-14 15:45:46 +11002469 /* Allocate a channel for each socket. */
2470 for (n = 0; n < num_socks; n++) {
2471 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10002472 (void) channel_new("x11 listener",
2473 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2474 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002475 0, xstrdup("X11 inet listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11002476 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002477
Damien Miller95def091999-11-25 00:26:21 +11002478 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11002479 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002480}
2481
2482#ifndef X_UNIX_PATH
2483#define X_UNIX_PATH "/tmp/.X11-unix/X"
2484#endif
2485
2486static
2487int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002488connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002489{
Damien Miller95def091999-11-25 00:26:21 +11002490 static const char *const x_sockets[] = {
2491 X_UNIX_PATH "%u",
2492 "/var/X/.X11-unix/X" "%u",
2493 "/usr/spool/sockets/X11/" "%u",
2494 NULL
2495 };
2496 int sock;
2497 struct sockaddr_un addr;
2498 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002499
Damien Miller95def091999-11-25 00:26:21 +11002500 for (path = x_sockets; *path; ++path) {
2501 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2502 if (sock < 0)
2503 error("socket: %.100s", strerror(errno));
2504 memset(&addr, 0, sizeof(addr));
2505 addr.sun_family = AF_UNIX;
2506 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
2507 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2508 return sock;
2509 close(sock);
2510 }
2511 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2512 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002513}
2514
Damien Millerbd483e72000-04-30 10:00:53 +10002515int
2516x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002517{
Damien Millerbd483e72000-04-30 10:00:53 +10002518 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002519 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002520 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002521 struct addrinfo hints, *ai, *aitop;
2522 char strport[NI_MAXSERV];
2523 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002524
Damien Miller95def091999-11-25 00:26:21 +11002525 /* Try to open a socket for the local X server. */
2526 display = getenv("DISPLAY");
2527 if (!display) {
2528 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002529 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002530 }
Damien Miller5428f641999-11-25 11:54:57 +11002531 /*
2532 * Now we decode the value of the DISPLAY variable and make a
2533 * connection to the real X server.
2534 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002535
Damien Miller5428f641999-11-25 11:54:57 +11002536 /*
2537 * Check if it is a unix domain socket. Unix domain displays are in
2538 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2539 */
Damien Miller95def091999-11-25 00:26:21 +11002540 if (strncmp(display, "unix:", 5) == 0 ||
2541 display[0] == ':') {
2542 /* Connect to the unix domain socket. */
2543 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2544 error("Could not parse display number from DISPLAY: %.100s",
2545 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002546 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002547 }
2548 /* Create a socket. */
2549 sock = connect_local_xsocket(display_number);
2550 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002551 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002552
2553 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002554 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002555 }
Damien Miller5428f641999-11-25 11:54:57 +11002556 /*
2557 * Connect to an inet socket. The DISPLAY value is supposedly
2558 * hostname:d[.s], where hostname may also be numeric IP address.
2559 */
Damien Miller95def091999-11-25 00:26:21 +11002560 strncpy(buf, display, sizeof(buf));
2561 buf[sizeof(buf) - 1] = 0;
2562 cp = strchr(buf, ':');
2563 if (!cp) {
2564 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002565 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002566 }
Damien Miller95def091999-11-25 00:26:21 +11002567 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002568 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002569 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2570 error("Could not parse display number from DISPLAY: %.100s",
2571 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002572 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002573 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002574
Damien Miller34132e52000-01-14 15:45:46 +11002575 /* Look up the host address */
2576 memset(&hints, 0, sizeof(hints));
2577 hints.ai_family = IPv4or6;
2578 hints.ai_socktype = SOCK_STREAM;
2579 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2580 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2581 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002582 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002583 }
Damien Miller34132e52000-01-14 15:45:46 +11002584 for (ai = aitop; ai; ai = ai->ai_next) {
2585 /* Create a socket. */
2586 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2587 if (sock < 0) {
2588 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002589 continue;
2590 }
2591 /* Connect it to the display. */
2592 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2593 debug("connect %.100s port %d: %.100s", buf,
2594 6000 + display_number, strerror(errno));
2595 close(sock);
2596 continue;
2597 }
2598 /* Success */
2599 break;
Damien Miller34132e52000-01-14 15:45:46 +11002600 }
Damien Miller34132e52000-01-14 15:45:46 +11002601 freeaddrinfo(aitop);
2602 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002603 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002604 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002605 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002606 }
Damien Millerbd483e72000-04-30 10:00:53 +10002607 return sock;
2608}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002609
Damien Millerbd483e72000-04-30 10:00:53 +10002610/*
2611 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2612 * the remote channel number. We should do whatever we want, and respond
2613 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2614 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002615
Damien Millerbd483e72000-04-30 10:00:53 +10002616void
Damien Miller62cee002000-09-23 17:15:56 +11002617x11_input_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002618{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002619 Channel *c = NULL;
2620 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002621 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10002622
2623 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002624
2625 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002626
2627 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002628 remote_host = packet_get_string(NULL);
2629 } else {
2630 remote_host = xstrdup("unknown (remote did not supply name)");
2631 }
2632 packet_done();
Damien Millerbd483e72000-04-30 10:00:53 +10002633
2634 /* Obtain a connection to the real X display. */
2635 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002636 if (sock != -1) {
2637 /* Allocate a channel for this connection. */
2638 c = channel_new("connected x11 socket",
2639 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
2640 remote_host, 1);
2641 if (c == NULL) {
2642 error("x11_input_open: channel_new failed");
2643 close(sock);
2644 } else {
2645 c->remote_id = remote_id;
2646 }
2647 }
2648 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10002649 /* Send refusal to the remote host. */
2650 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002651 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10002652 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10002653 /* Send a confirmation to the remote host. */
2654 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002655 packet_put_int(remote_id);
2656 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10002657 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002658 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002659}
2660
Damien Miller69b69aa2000-10-28 14:19:58 +11002661/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2662void
2663deny_input_open(int type, int plen, void *ctxt)
2664{
2665 int rchan = packet_get_int();
2666 switch(type){
2667 case SSH_SMSG_AGENT_OPEN:
2668 error("Warning: ssh server tried agent forwarding.");
2669 break;
2670 case SSH_SMSG_X11_OPEN:
2671 error("Warning: ssh server tried X11 forwarding.");
2672 break;
2673 default:
2674 error("deny_input_open: type %d plen %d", type, plen);
2675 break;
2676 }
2677 error("Warning: this is probably a break in attempt by a malicious server.");
2678 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2679 packet_put_int(rchan);
2680 packet_send();
2681}
2682
Damien Miller5428f641999-11-25 11:54:57 +11002683/*
2684 * Requests forwarding of X11 connections, generates fake authentication
2685 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00002686 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11002687 */
Damien Miller4af51302000-04-16 11:18:38 +10002688void
Damien Millerbd483e72000-04-30 10:00:53 +10002689x11_request_forwarding_with_spoofing(int client_session_id,
2690 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002691{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002692 u_int data_len = (u_int) strlen(data) / 2;
Ben Lindstromb3211a82001-02-10 22:33:19 +00002693 u_int i, value, len;
Damien Miller95def091999-11-25 00:26:21 +11002694 char *new_data;
2695 int screen_number;
2696 const char *cp;
2697 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002698
Damien Miller95def091999-11-25 00:26:21 +11002699 cp = getenv("DISPLAY");
2700 if (cp)
2701 cp = strchr(cp, ':');
2702 if (cp)
2703 cp = strchr(cp, '.');
2704 if (cp)
2705 screen_number = atoi(cp + 1);
2706 else
2707 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002708
Damien Miller95def091999-11-25 00:26:21 +11002709 /* Save protocol name. */
2710 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002711
Damien Miller5428f641999-11-25 11:54:57 +11002712 /*
2713 * Extract real authentication data and generate fake data of the
2714 * same length.
2715 */
Damien Miller95def091999-11-25 00:26:21 +11002716 x11_saved_data = xmalloc(data_len);
2717 x11_fake_data = xmalloc(data_len);
2718 for (i = 0; i < data_len; i++) {
2719 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2720 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2721 if (i % 4 == 0)
2722 rand = arc4random();
2723 x11_saved_data[i] = value;
2724 x11_fake_data[i] = rand & 0xff;
2725 rand >>= 8;
2726 }
2727 x11_saved_data_len = data_len;
2728 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002729
Damien Miller95def091999-11-25 00:26:21 +11002730 /* Convert the fake data into hex. */
Ben Lindstromb3211a82001-02-10 22:33:19 +00002731 len = 2 * data_len + 1;
2732 new_data = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +11002733 for (i = 0; i < data_len; i++)
Ben Lindstromb3211a82001-02-10 22:33:19 +00002734 snprintf(new_data + 2 * i, len - 2 * i,
2735 "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002736
Damien Miller95def091999-11-25 00:26:21 +11002737 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002738 if (compat20) {
2739 channel_request_start(client_session_id, "x11-req", 0);
2740 packet_put_char(0); /* XXX bool single connection */
2741 } else {
2742 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2743 }
2744 packet_put_cstring(proto);
2745 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002746 packet_put_int(screen_number);
2747 packet_send();
2748 packet_write_wait();
2749 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002750}
2751
Ben Lindstrome9c99912001-06-09 00:41:05 +00002752
2753/* -- agent forwarding */
2754
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002755/* Sends a message to the server to request authentication fd forwarding. */
2756
Damien Miller4af51302000-04-16 11:18:38 +10002757void
Damien Miller95def091999-11-25 00:26:21 +11002758auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002759{
Damien Miller95def091999-11-25 00:26:21 +11002760 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2761 packet_send();
2762 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002763}
2764
Damien Miller5428f641999-11-25 11:54:57 +11002765/*
2766 * Returns the name of the forwarded authentication socket. Returns NULL if
2767 * there is no forwarded authentication socket. The returned value points to
2768 * a static buffer.
2769 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002770
Damien Miller95def091999-11-25 00:26:21 +11002771char *
2772auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002773{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002774 return auth_sock_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002775}
2776
2777/* removes the agent forwarding socket */
2778
Damien Miller4af51302000-04-16 11:18:38 +10002779void
Ben Lindstrom983c0982001-06-09 01:20:06 +00002780auth_sock_cleanup_proc(void *_pw)
Damien Miller95def091999-11-25 00:26:21 +11002781{
Ben Lindstrom983c0982001-06-09 01:20:06 +00002782 struct passwd *pw = _pw;
2783
Ben Lindstrom838394c2001-06-09 01:11:59 +00002784 if (auth_sock_name) {
Ben Lindstrom983c0982001-06-09 01:20:06 +00002785 temporarily_use_uid(pw);
Ben Lindstrom838394c2001-06-09 01:11:59 +00002786 unlink(auth_sock_name);
2787 rmdir(auth_sock_dir);
2788 auth_sock_name = NULL;
Ben Lindstrom983c0982001-06-09 01:20:06 +00002789 restore_uid();
Ben Lindstrom838394c2001-06-09 01:11:59 +00002790 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002791}
2792
Damien Miller5428f641999-11-25 11:54:57 +11002793/*
Damien Millerd3a18572000-06-07 19:55:44 +10002794 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002795 * This starts forwarding authentication requests.
2796 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002797
Damien Millerd3a18572000-06-07 19:55:44 +10002798int
Damien Miller95def091999-11-25 00:26:21 +11002799auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002800{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002801 Channel *nc;
2802 int sock;
Damien Miller95def091999-11-25 00:26:21 +11002803 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002804
Ben Lindstrome9c99912001-06-09 00:41:05 +00002805 if (auth_get_socket_name() != NULL) {
2806 error("authentication forwarding requested twice.");
2807 return 0;
2808 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002809
Damien Miller95def091999-11-25 00:26:21 +11002810 /* Temporarily drop privileged uid for mkdir/bind. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00002811 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002812
Damien Miller95def091999-11-25 00:26:21 +11002813 /* Allocate a buffer for the socket name, and format the name. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002814 auth_sock_name = xmalloc(MAXPATHLEN);
2815 auth_sock_dir = xmalloc(MAXPATHLEN);
2816 strlcpy(auth_sock_dir, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002817
Damien Miller95def091999-11-25 00:26:21 +11002818 /* Create private directory for socket */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002819 if (mkdtemp(auth_sock_dir) == NULL) {
2820 packet_send_debug("Agent forwarding disabled: "
2821 "mkdtemp() failed: %.100s", strerror(errno));
Damien Millerd3a18572000-06-07 19:55:44 +10002822 restore_uid();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002823 xfree(auth_sock_name);
2824 xfree(auth_sock_dir);
2825 auth_sock_name = NULL;
2826 auth_sock_dir = NULL;
Damien Millerd3a18572000-06-07 19:55:44 +10002827 return 0;
2828 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002829 snprintf(auth_sock_name, MAXPATHLEN, "%s/agent.%d",
2830 auth_sock_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002831
Ben Lindstrom838394c2001-06-09 01:11:59 +00002832 /* delete agent socket on fatal() */
Ben Lindstrom983c0982001-06-09 01:20:06 +00002833 fatal_add_cleanup(auth_sock_cleanup_proc, pw);
Ben Lindstrom838394c2001-06-09 01:11:59 +00002834
Damien Miller95def091999-11-25 00:26:21 +11002835 /* Create the socket. */
2836 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2837 if (sock < 0)
2838 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002839
Damien Miller95def091999-11-25 00:26:21 +11002840 /* Bind it to the name. */
2841 memset(&sunaddr, 0, sizeof(sunaddr));
2842 sunaddr.sun_family = AF_UNIX;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002843 strncpy(sunaddr.sun_path, auth_sock_name,
Damien Miller95def091999-11-25 00:26:21 +11002844 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002845
Damien Miller95def091999-11-25 00:26:21 +11002846 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2847 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002848
Damien Miller95def091999-11-25 00:26:21 +11002849 /* Restore the privileged uid. */
2850 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002851
Damien Miller95def091999-11-25 00:26:21 +11002852 /* Start listening on the socket. */
2853 if (listen(sock, 5) < 0)
2854 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002855
Damien Miller95def091999-11-25 00:26:21 +11002856 /* Allocate a channel for the authentication agent socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002857 nc = channel_new("auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11002858 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2859 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2860 0, xstrdup("auth socket"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002861 if (nc == NULL) {
2862 error("auth_input_request_forwarding: channel_new failed");
Ben Lindstrom983c0982001-06-09 01:20:06 +00002863 auth_sock_cleanup_proc(pw);
Ben Lindstromdf4981b2001-06-09 01:32:29 +00002864 fatal_remove_cleanup(auth_sock_cleanup_proc, pw);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002865 close(sock);
2866 return 0;
2867 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002868 strlcpy(nc->path, auth_sock_name, sizeof(nc->path));
Damien Millerd3a18572000-06-07 19:55:44 +10002869 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002870}
2871
2872/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2873
Damien Miller4af51302000-04-16 11:18:38 +10002874void
Damien Miller62cee002000-09-23 17:15:56 +11002875auth_input_open_request(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002876{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002877 Channel *c = NULL;
2878 int remote_id, sock;
Ben Lindstrome9c99912001-06-09 00:41:05 +00002879 char *name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002880
Damien Millerb38eff82000-04-01 11:09:21 +10002881 packet_integrity_check(plen, 4, type);
2882
Damien Miller95def091999-11-25 00:26:21 +11002883 /* Read the remote channel number from the message. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002884 remote_id = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002885
Damien Miller5428f641999-11-25 11:54:57 +11002886 /*
2887 * Get a connection to the local authentication agent (this may again
2888 * get forwarded).
2889 */
Damien Miller95def091999-11-25 00:26:21 +11002890 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002891
Damien Miller5428f641999-11-25 11:54:57 +11002892 /*
2893 * If we could not connect the agent, send an error message back to
2894 * the server. This should never happen unless the agent dies,
2895 * because authentication forwarding is only enabled if we have an
2896 * agent.
2897 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002898 if (sock >= 0) {
Ben Lindstrome9c99912001-06-09 00:41:05 +00002899 name = xstrdup("authentication agent connection");
2900 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock,
2901 -1, 0, 0, 0, name, 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002902 if (c == NULL) {
2903 error("auth_input_open_request: channel_new failed");
Ben Lindstrome9c99912001-06-09 00:41:05 +00002904 xfree(name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002905 close(sock);
2906 } else {
2907 c->remote_id = remote_id;
2908 }
Damien Miller95def091999-11-25 00:26:21 +11002909 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002910 if (c == NULL) {
2911 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2912 packet_put_int(remote_id);
2913 } else {
2914 /* Send a confirmation to the remote host. */
2915 debug("Forwarding authentication connection.");
2916 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2917 packet_put_int(remote_id);
2918 packet_put_int(c->self);
2919 }
Damien Miller95def091999-11-25 00:26:21 +11002920 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002921}