blob: 2bb0e985121073977e35a490c6432ef271d0f0a4 [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 Lindstrom99c73b32001-05-05 04:09:47 +000043RCSID("$OpenBSD: channels.c,v 1.113 2001/05/04 23:47:33 markus Exp $");
Ben Lindstrom226cfa02001-01-22 05:34:40 +000044
45#include <openssl/rsa.h>
46#include <openssl/dsa.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047
48#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000049#include "ssh1.h"
50#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051#include "packet.h"
52#include "xmalloc.h"
53#include "buffer.h"
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +000054#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055#include "uidswap.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "log.h"
57#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058#include "channels.h"
59#include "nchan.h"
60#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000061#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100062#include "key.h"
63#include "authfd.h"
64
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065/* Maximum number of fake X11 displays to try. */
66#define MAX_DISPLAYS 1000
67
68/* Max len of agent socket */
69#define MAX_SOCKET_NAME 100
70
Damien Miller5428f641999-11-25 11:54:57 +110071/*
72 * Pointer to an array containing all allocated channels. The array is
73 * dynamically extended as needed.
74 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000075static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076
Damien Miller5428f641999-11-25 11:54:57 +110077/*
78 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000079 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110080 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081static int channels_alloc = 0;
82
Damien Miller5428f641999-11-25 11:54:57 +110083/*
84 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +000085 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +110086 */
Damien Miller5e953212001-01-30 09:14:00 +110087static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
89/* Name and directory of socket for authentication agent forwarding. */
90static char *channel_forwarded_auth_socket_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +110091static char *channel_forwarded_auth_socket_dir = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
93/* Saved X11 authentication protocol name. */
94char *x11_saved_proto = NULL;
95
96/* Saved X11 authentication data. This is the real data. */
97char *x11_saved_data = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +000098u_int x11_saved_data_len = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * Fake X11 authentication data. This is what the server will be sending us;
102 * we should replace any occurrences of this by the real data.
103 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104char *x11_fake_data = NULL;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000105u_int x11_fake_data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Miller5428f641999-11-25 11:54:57 +1100107/*
108 * Data structure for storing which hosts are permitted for forward requests.
109 * The local sides of any remote forwards are stored in this array to prevent
110 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
111 * network (which might be behind a firewall).
112 */
Damien Miller95def091999-11-25 00:26:21 +1100113typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000114 char *host_to_connect; /* Connect to 'host'. */
115 u_short port_to_connect; /* Connect to 'port'. */
116 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117} ForwardPermission;
118
119/* List of all permitted host/port pairs to connect. */
120static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
121/* Number of permitted host/port pairs in the array. */
122static int num_permitted_opens = 0;
Damien Miller5428f641999-11-25 11:54:57 +1100123/*
124 * If this is true, all opens are permitted. This is the case on the server
125 * on which we have to trust the client anyway, and the user could do
126 * anything after logging in anyway.
127 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128static int all_opens_permitted = 0;
129
130/* This is set to true if both sides support SSH_PROTOFLAG_HOST_IN_FWD_OPEN. */
131static int have_hostname_in_open = 0;
132
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000133/* AF_UNSPEC or AF_INET or AF_INET6 */
134extern int IPv4or6;
135
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000136void port_open_helper(Channel *c, char *rtype);
137
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138/* Sets specific protocol options. */
139
Damien Miller4af51302000-04-16 11:18:38 +1000140void
Damien Miller95def091999-11-25 00:26:21 +1100141channel_set_options(int hostname_in_open)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000142{
Damien Miller95def091999-11-25 00:26:21 +1100143 have_hostname_in_open = hostname_in_open;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144}
145
Damien Millerb38eff82000-04-01 11:09:21 +1000146/* lookup channel by id */
147
148Channel *
149channel_lookup(int id)
150{
151 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000152
Damien Millerc0fd17f2000-06-26 10:22:53 +1000153 if (id < 0 || id > channels_alloc) {
Damien Millerb38eff82000-04-01 11:09:21 +1000154 log("channel_lookup: %d: bad id", id);
155 return NULL;
156 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000157 c = channels[id];
158 if (c == NULL) {
Damien Millerb38eff82000-04-01 11:09:21 +1000159 log("channel_lookup: %d: bad id: channel free", id);
160 return NULL;
161 }
162 return c;
163}
164
Damien Miller5428f641999-11-25 11:54:57 +1100165/*
Damien Millere247cc42000-05-07 12:03:14 +1000166 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000167 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100168 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000169
Damien Miller6f83b8e2000-05-02 09:23:45 +1000170void
Damien Miller69b69aa2000-10-28 14:19:58 +1100171channel_register_fds(Channel *c, int rfd, int wfd, int efd,
172 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173{
Damien Miller95def091999-11-25 00:26:21 +1100174 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100175 channel_max_fd = MAX(channel_max_fd, rfd);
176 channel_max_fd = MAX(channel_max_fd, wfd);
177 channel_max_fd = MAX(channel_max_fd, efd);
178
Damien Miller5428f641999-11-25 11:54:57 +1100179 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000180
Damien Miller6f83b8e2000-05-02 09:23:45 +1000181 c->rfd = rfd;
182 c->wfd = wfd;
183 c->sock = (rfd == wfd) ? rfd : -1;
184 c->efd = efd;
185 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100186
Damien Miller79438cc2001-02-16 12:34:57 +1100187 /* XXX ugly hack: nonblock is only set by the server */
188 if (nonblock && isatty(c->rfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000189 debug("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100190 c->isatty = 1;
191 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000192 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100193 c->self, c->wfd);
194 }
195 } else {
196 c->isatty = 0;
197 }
198
Damien Miller69b69aa2000-10-28 14:19:58 +1100199 /* enable nonblocking mode */
200 if (nonblock) {
201 if (rfd != -1)
202 set_nonblock(rfd);
203 if (wfd != -1)
204 set_nonblock(wfd);
205 if (efd != -1)
206 set_nonblock(efd);
207 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000208}
209
210/*
211 * Allocate a new channel object and set its type and socket. This will cause
212 * remote_name to be freed.
213 */
214
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000215Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000216channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Damien Miller69b69aa2000-10-28 14:19:58 +1100217 int window, int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000218{
219 int i, found;
220 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221
Damien Miller95def091999-11-25 00:26:21 +1100222 /* Do initial allocation if this is the first call. */
223 if (channels_alloc == 0) {
Damien Miller33b13562000-04-04 14:38:59 +1000224 chan_init();
Damien Miller95def091999-11-25 00:26:21 +1100225 channels_alloc = 10;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000226 channels = xmalloc(channels_alloc * sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100227 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000228 channels[i] = NULL;
Damien Miller5428f641999-11-25 11:54:57 +1100229 /*
230 * Kludge: arrange a call to channel_stop_listening if we
231 * terminate with fatal().
232 */
Damien Miller95def091999-11-25 00:26:21 +1100233 fatal_add_cleanup((void (*) (void *)) channel_stop_listening, NULL);
234 }
235 /* Try to find a free slot where to put the new channel. */
236 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000237 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100238 /* Found a free slot. */
239 found = i;
240 break;
241 }
242 if (found == -1) {
Damien Miller5428f641999-11-25 11:54:57 +1100243 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100244 found = channels_alloc;
245 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100246 debug2("channel: expanding %d", channels_alloc);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000247 channels = xrealloc(channels, channels_alloc * sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100248 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000249 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100250 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000251 /* Initialize and return new channel. */
252 c = channels[found] = xmalloc(sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100253 buffer_init(&c->input);
254 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000255 buffer_init(&c->extended);
Damien Miller95def091999-11-25 00:26:21 +1100256 chan_init_iostates(c);
Damien Miller69b69aa2000-10-28 14:19:58 +1100257 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100258 c->self = found;
259 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000260 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000261 c->local_window = window;
262 c->local_window_max = window;
263 c->local_consumed = 0;
264 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100265 c->remote_id = -1;
266 c->remote_name = remote_name;
Damien Millerb38eff82000-04-01 11:09:21 +1000267 c->remote_window = 0;
268 c->remote_maxpacket = 0;
269 c->cb_fn = NULL;
270 c->cb_arg = NULL;
271 c->cb_event = 0;
272 c->dettach_user = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000273 c->input_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100274 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000275 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000277
278/* Close all channel fd/socket. */
279
280void
281channel_close_fds(Channel *c)
282{
283 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 Lindstrom6c3ae2b2000-12-30 03:25:14 +0000307
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000308 s = channel_open_message();
309 debug("channel_free: channel %d: status: %s", c->self, s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000310 xfree(s);
311
Damien Miller33b13562000-04-04 14:38:59 +1000312 if (c->dettach_user != NULL) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000313 debug("channel_free: channel %d: dettaching channel user", c->self);
Damien Miller33b13562000-04-04 14:38:59 +1000314 c->dettach_user(c->self, NULL);
315 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000316 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000317 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000318 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000319 buffer_free(&c->input);
320 buffer_free(&c->output);
321 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000322 if (c->remote_name) {
323 xfree(c->remote_name);
324 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100325 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000326 channels[c->self] = NULL;
327 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000328}
329
Damien Miller5428f641999-11-25 11:54:57 +1100330/*
Damien Millerb38eff82000-04-01 11:09:21 +1000331 * 'channel_pre*' are called just before select() to add any bits relevant to
332 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100333 */
Damien Millerb38eff82000-04-01 11:09:21 +1000334/*
335 * 'channel_post*': perform any appropriate operations for channels which
336 * have events pending.
337 */
338typedef void chan_fn(Channel *c, fd_set * readset, fd_set * writeset);
339chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
340chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
341
342void
343channel_pre_listener(Channel *c, fd_set * readset, fd_set * writeset)
344{
345 FD_SET(c->sock, readset);
346}
347
348void
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000349channel_pre_connecting(Channel *c, fd_set * readset, fd_set * writeset)
350{
351 debug3("channel %d: waiting for connection", c->self);
352 FD_SET(c->sock, writeset);
353}
354
355void
Damien Millerb38eff82000-04-01 11:09:21 +1000356channel_pre_open_13(Channel *c, fd_set * readset, fd_set * writeset)
357{
358 if (buffer_len(&c->input) < packet_get_maxsize())
359 FD_SET(c->sock, readset);
360 if (buffer_len(&c->output) > 0)
361 FD_SET(c->sock, writeset);
362}
363
364void
365channel_pre_open_15(Channel *c, fd_set * readset, fd_set * writeset)
366{
367 /* test whether sockets are 'alive' for read/write */
368 if (c->istate == CHAN_INPUT_OPEN)
369 if (buffer_len(&c->input) < packet_get_maxsize())
370 FD_SET(c->sock, readset);
371 if (c->ostate == CHAN_OUTPUT_OPEN ||
372 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
373 if (buffer_len(&c->output) > 0) {
374 FD_SET(c->sock, writeset);
375 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
376 chan_obuf_empty(c);
377 }
378 }
379}
380
381void
Damien Miller33b13562000-04-04 14:38:59 +1000382channel_pre_open_20(Channel *c, fd_set * readset, fd_set * writeset)
383{
384 if (c->istate == CHAN_INPUT_OPEN &&
385 c->remote_window > 0 &&
386 buffer_len(&c->input) < c->remote_window)
387 FD_SET(c->rfd, readset);
388 if (c->ostate == CHAN_OUTPUT_OPEN ||
389 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
390 if (buffer_len(&c->output) > 0) {
391 FD_SET(c->wfd, writeset);
392 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
393 chan_obuf_empty(c);
394 }
395 }
396 /** XXX check close conditions, too */
397 if (c->efd != -1) {
398 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
399 buffer_len(&c->extended) > 0)
400 FD_SET(c->efd, writeset);
401 else if (c->extended_usage == CHAN_EXTENDED_READ &&
402 buffer_len(&c->extended) < c->remote_window)
403 FD_SET(c->efd, readset);
404 }
405}
406
407void
Damien Millerb38eff82000-04-01 11:09:21 +1000408channel_pre_input_draining(Channel *c, fd_set * readset, fd_set * writeset)
409{
410 if (buffer_len(&c->input) == 0) {
411 packet_start(SSH_MSG_CHANNEL_CLOSE);
412 packet_put_int(c->remote_id);
413 packet_send();
414 c->type = SSH_CHANNEL_CLOSED;
Ben Lindstromc486d882001-04-11 16:08:34 +0000415 debug("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000416 }
417}
418
419void
420channel_pre_output_draining(Channel *c, fd_set * readset, fd_set * writeset)
421{
422 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000423 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000424 else
Damien Millerb38eff82000-04-01 11:09:21 +1000425 FD_SET(c->sock, writeset);
426}
427
428/*
429 * This is a special state for X11 authentication spoofing. An opened X11
430 * connection (when authentication spoofing is being done) remains in this
431 * state until the first packet has been completely read. The authentication
432 * data in that packet is then substituted by the real data if it matches the
433 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000434 * XXX All this happens at the client side.
Damien Millerb38eff82000-04-01 11:09:21 +1000435 */
436int
437x11_open_helper(Channel *c)
438{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000439 u_char *ucp;
440 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000441
442 /* Check if the fixed size part of the packet is in buffer. */
443 if (buffer_len(&c->output) < 12)
444 return 0;
445
446 /* Parse the lengths of variable-length fields. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000447 ucp = (u_char *) buffer_ptr(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000448 if (ucp[0] == 0x42) { /* Byte order MSB first. */
449 proto_len = 256 * ucp[6] + ucp[7];
450 data_len = 256 * ucp[8] + ucp[9];
451 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
452 proto_len = ucp[6] + 256 * ucp[7];
453 data_len = ucp[8] + 256 * ucp[9];
454 } else {
455 debug("Initial X11 packet contains bad byte order byte: 0x%x",
456 ucp[0]);
457 return -1;
458 }
459
460 /* Check if the whole packet is in buffer. */
461 if (buffer_len(&c->output) <
462 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
463 return 0;
464
465 /* Check if authentication protocol matches. */
466 if (proto_len != strlen(x11_saved_proto) ||
467 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
468 debug("X11 connection uses different authentication protocol.");
469 return -1;
470 }
471 /* Check if authentication data matches our fake data. */
472 if (data_len != x11_fake_data_len ||
473 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
474 x11_fake_data, x11_fake_data_len) != 0) {
475 debug("X11 auth data does not match fake data.");
476 return -1;
477 }
478 /* Check fake data length */
479 if (x11_fake_data_len != x11_saved_data_len) {
480 error("X11 fake_data_len %d != saved_data_len %d",
481 x11_fake_data_len, x11_saved_data_len);
482 return -1;
483 }
484 /*
485 * Received authentication protocol and data match
486 * our fake data. Substitute the fake data with real
487 * data.
488 */
489 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
490 x11_saved_data, x11_saved_data_len);
491 return 1;
492}
493
494void
495channel_pre_x11_open_13(Channel *c, fd_set * readset, fd_set * writeset)
496{
497 int ret = x11_open_helper(c);
498 if (ret == 1) {
499 /* Start normal processing for the channel. */
500 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000501 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000502 } else if (ret == -1) {
503 /*
504 * We have received an X11 connection that has bad
505 * authentication information.
506 */
Ben Lindstrom6df8ef42001-03-05 07:47:23 +0000507 log("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000508 buffer_clear(&c->input);
509 buffer_clear(&c->output);
510 close(c->sock);
511 c->sock = -1;
512 c->type = SSH_CHANNEL_CLOSED;
513 packet_start(SSH_MSG_CHANNEL_CLOSE);
514 packet_put_int(c->remote_id);
515 packet_send();
516 }
517}
518
519void
Damien Millerbd483e72000-04-30 10:00:53 +1000520channel_pre_x11_open(Channel *c, fd_set * readset, fd_set * writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000521{
522 int ret = x11_open_helper(c);
523 if (ret == 1) {
524 c->type = SSH_CHANNEL_OPEN;
Damien Miller30c3d422000-05-09 11:02:59 +1000525 if (compat20)
526 channel_pre_open_20(c, readset, writeset);
527 else
528 channel_pre_open_15(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000529 } else if (ret == -1) {
530 debug("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerbd483e72000-04-30 10:00:53 +1000531 chan_read_failed(c); /** force close? */
Damien Millerb38eff82000-04-01 11:09:21 +1000532 chan_write_failed(c);
533 debug("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
534 }
535}
536
Ben Lindstromb3921512001-04-11 15:57:50 +0000537/* try to decode a socks4 header */
538int
539channel_decode_socks4(Channel *c, fd_set * readset, fd_set * writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000540{
541 u_char *p, *host;
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000542 int len, have, i, found;
Ben Lindstromb3921512001-04-11 15:57:50 +0000543 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000544 struct {
545 u_int8_t version;
546 u_int8_t command;
547 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000548 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000549 } s4_req, s4_rsp;
550
Ben Lindstromb3921512001-04-11 15:57:50 +0000551 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000552
553 have = buffer_len(&c->input);
554 len = sizeof(s4_req);
555 if (have < len)
556 return 0;
557 p = buffer_ptr(&c->input);
558 for (found = 0, i = len; i < have; i++) {
559 if (p[i] == '\0') {
560 found = 1;
561 break;
562 }
563 if (i > 1024) {
564 /* the peer is probably sending garbage */
565 debug("channel %d: decode socks4: too long",
566 c->self);
567 return -1;
568 }
569 }
570 if (!found)
571 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000572 buffer_get(&c->input, (char *)&s4_req.version, 1);
573 buffer_get(&c->input, (char *)&s4_req.command, 1);
574 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000575 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000576 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000577 p = buffer_ptr(&c->input);
578 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000579 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000580 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000581 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000582 c->self, len, have);
583 strlcpy(username, p, sizeof(username));
584 buffer_consume(&c->input, len);
585 buffer_consume(&c->input, 1); /* trailing '\0' */
586
Ben Lindstromb3921512001-04-11 15:57:50 +0000587 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000588 strlcpy(c->path, host, sizeof(c->path));
589 c->host_port = ntohs(s4_req.dest_port);
590
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000591 debug("channel %d: dynamic request: socks4 host %s port %u command %u",
592 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000593
Ben Lindstromb3921512001-04-11 15:57:50 +0000594 if (s4_req.command != 1) {
595 debug("channel %d: cannot handle: socks4 cn %d",
596 c->self, s4_req.command);
597 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000598 }
Ben Lindstromb3921512001-04-11 15:57:50 +0000599 s4_rsp.version = 0; /* vn: 0 for reply */
600 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000601 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +0000602 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000603 buffer_append(&c->output, (char *)&s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +0000604 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000605}
606
Ben Lindstromb3921512001-04-11 15:57:50 +0000607/* dynamic port forwarding */
608void
609channel_pre_dynamic(Channel *c, fd_set * readset, fd_set * writeset)
610{
611 u_char *p;
612 int have, ret;
613
614 have = buffer_len(&c->input);
615
616 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +0000617 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +0000618 /* check if the fixed size part of the packet is in buffer. */
619 if (have < 4) {
620 /* need more */
621 FD_SET(c->sock, readset);
622 return;
623 }
624 /* try to guess the protocol */
625 p = buffer_ptr(&c->input);
626 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000627 case 0x04:
628 ret = channel_decode_socks4(c, readset, writeset);
629 break;
Ben Lindstromb3921512001-04-11 15:57:50 +0000630 default:
631 ret = -1;
632 break;
633 }
634 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000635 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +0000636 } else if (ret == 0) {
637 debug2("channel %d: pre_dynamic: need more", c->self);
638 /* need more */
639 FD_SET(c->sock, readset);
640 } else {
641 /* switch to the next state */
642 c->type = SSH_CHANNEL_OPENING;
643 port_open_helper(c, "direct-tcpip");
644 }
645}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000646
Damien Millerb38eff82000-04-01 11:09:21 +1000647/* This is our fake X11 server socket. */
648void
649channel_post_x11_listener(Channel *c, fd_set * readset, fd_set * writeset)
650{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000651 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +1000652 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000653 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +1000654 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +1100655 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +1000656 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +1000657
658 if (FD_ISSET(c->sock, readset)) {
659 debug("X11 connection requested.");
660 addrlen = sizeof(addr);
661 newsock = accept(c->sock, &addr, &addrlen);
662 if (newsock < 0) {
663 error("accept: %.100s", strerror(errno));
664 return;
665 }
Damien Millerd83ff352001-01-30 09:19:34 +1100666 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +1000667 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +1000668 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +1100669 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +1000670
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000671 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +1000672 SSH_CHANNEL_OPENING, newsock, newsock, -1,
673 c->local_window_max, c->local_maxpacket,
Damien Miller69b69aa2000-10-28 14:19:58 +1100674 0, xstrdup(buf), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000675 if (nc == NULL) {
676 close(newsock);
677 xfree(remote_ipaddr);
678 return;
679 }
Damien Millerbd483e72000-04-30 10:00:53 +1000680 if (compat20) {
681 packet_start(SSH2_MSG_CHANNEL_OPEN);
682 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000683 packet_put_int(nc->self);
Damien Millerbd483e72000-04-30 10:00:53 +1000684 packet_put_int(c->local_window_max);
685 packet_put_int(c->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +1100686 /* originator ipaddr and port */
687 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +1000688 if (datafellows & SSH_BUG_X11FWD) {
689 debug("ssh2 x11 bug compat mode");
690 } else {
691 packet_put_int(remote_port);
692 }
Damien Millerbd483e72000-04-30 10:00:53 +1000693 packet_send();
694 } else {
695 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000696 packet_put_int(nc->self);
Damien Millerbd483e72000-04-30 10:00:53 +1000697 if (have_hostname_in_open)
698 packet_put_string(buf, strlen(buf));
699 packet_send();
700 }
Damien Millerd83ff352001-01-30 09:19:34 +1100701 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +1000702 }
703}
704
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000705void
706port_open_helper(Channel *c, char *rtype)
707{
708 int direct;
709 char buf[1024];
710 char *remote_ipaddr = get_peer_ipaddr(c->sock);
711 u_short remote_port = get_peer_port(c->sock);
712
713 direct = (strcmp(rtype, "direct-tcpip") == 0);
714
715 snprintf(buf, sizeof buf,
716 "%s: listening port %d for %.100s port %d, "
717 "connect from %.200s port %d",
718 rtype, c->listening_port, c->path, c->host_port,
719 remote_ipaddr, remote_port);
720
721 xfree(c->remote_name);
722 c->remote_name = xstrdup(buf);
723
724 if (compat20) {
725 packet_start(SSH2_MSG_CHANNEL_OPEN);
726 packet_put_cstring(rtype);
727 packet_put_int(c->self);
728 packet_put_int(c->local_window_max);
729 packet_put_int(c->local_maxpacket);
730 if (direct) {
731 /* target host, port */
732 packet_put_cstring(c->path);
733 packet_put_int(c->host_port);
734 } else {
735 /* listen address, port */
736 packet_put_cstring(c->path);
737 packet_put_int(c->listening_port);
738 }
739 /* originator host and port */
740 packet_put_cstring(remote_ipaddr);
741 packet_put_int(remote_port);
742 packet_send();
743 } else {
744 packet_start(SSH_MSG_PORT_OPEN);
745 packet_put_int(c->self);
746 packet_put_cstring(c->path);
747 packet_put_int(c->host_port);
748 if (have_hostname_in_open)
749 packet_put_cstring(c->remote_name);
750 packet_send();
751 }
752 xfree(remote_ipaddr);
753}
754
Damien Millerb38eff82000-04-01 11:09:21 +1000755/*
756 * This socket is listening for connections to a forwarded TCP/IP port.
757 */
758void
759channel_post_port_listener(Channel *c, fd_set * readset, fd_set * writeset)
760{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000761 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +1000762 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000763 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +1000764 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000765 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100766
Damien Millerb38eff82000-04-01 11:09:21 +1000767 if (FD_ISSET(c->sock, readset)) {
768 debug("Connection to port %d forwarding "
769 "to %.100s port %d requested.",
770 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000771
772 rtype = (c->type == SSH_CHANNEL_RPORT_LISTENER) ?
773 "forwarded-tcpip" : "direct-tcpip";
774 nextstate = (c->host_port == 0) ? SSH_CHANNEL_DYNAMIC :
775 SSH_CHANNEL_OPENING;
776
Damien Millerb38eff82000-04-01 11:09:21 +1000777 addrlen = sizeof(addr);
778 newsock = accept(c->sock, &addr, &addrlen);
779 if (newsock < 0) {
780 error("accept: %.100s", strerror(errno));
781 return;
782 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000783 nc = channel_new(rtype,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000784 nextstate, newsock, newsock, -1,
Damien Millerb38eff82000-04-01 11:09:21 +1000785 c->local_window_max, c->local_maxpacket,
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000786 0, xstrdup(rtype), 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000787 if (nc == NULL) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000788 error("channel_post_port_listener: no new channel:");
789 close(newsock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000790 return;
Damien Millerb38eff82000-04-01 11:09:21 +1000791 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000792 nc->listening_port = c->listening_port;
793 nc->host_port = c->host_port;
794 strlcpy(nc->path, c->path, sizeof(nc->path));
795
796 if (nextstate != SSH_CHANNEL_DYNAMIC)
797 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +1000798 }
799}
800
801/*
802 * This is the authentication agent socket listening for connections from
803 * clients.
804 */
805void
806channel_post_auth_listener(Channel *c, fd_set * readset, fd_set * writeset)
807{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000808 Channel *nc;
809 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +1000810 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +1000811 socklen_t addrlen;
812
813 if (FD_ISSET(c->sock, readset)) {
814 addrlen = sizeof(addr);
815 newsock = accept(c->sock, &addr, &addrlen);
816 if (newsock < 0) {
817 error("accept from auth socket: %.100s", strerror(errno));
818 return;
819 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000820 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +1100821 SSH_CHANNEL_OPENING, newsock, newsock, -1,
822 c->local_window_max, c->local_maxpacket,
823 0, xstrdup("accepted auth socket"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000824 if (nc == NULL) {
825 error("channel_post_auth_listener: channel_new failed");
826 close(newsock);
827 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100828 if (compat20) {
829 packet_start(SSH2_MSG_CHANNEL_OPEN);
830 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000831 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100832 packet_put_int(c->local_window_max);
833 packet_put_int(c->local_maxpacket);
834 } else {
835 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000836 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100837 }
Damien Millerb38eff82000-04-01 11:09:21 +1000838 packet_send();
839 }
840}
841
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000842void
843channel_post_connecting(Channel *c, fd_set * readset, fd_set * writeset)
844{
845 if (FD_ISSET(c->sock, writeset)) {
846 int err = 0;
847 int sz = sizeof(err);
848 c->type = SSH_CHANNEL_OPEN;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000849 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, (char *)&err, &sz) < 0) {
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000850 debug("getsockopt SO_ERROR failed");
851 } else {
852 if (err == 0) {
Ben Lindstrom2b451802001-05-03 22:35:32 +0000853 debug("channel %d: connected", c->self);
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000854 } else {
855 debug("channel %d: not connected: %s",
856 c->self, strerror(err));
857 chan_read_failed(c);
858 chan_write_failed(c);
859 }
860 }
861 }
862}
863
Damien Millerb38eff82000-04-01 11:09:21 +1000864int
865channel_handle_rfd(Channel *c, fd_set * readset, fd_set * writeset)
866{
867 char buf[16*1024];
868 int len;
869
870 if (c->rfd != -1 &&
871 FD_ISSET(c->rfd, readset)) {
872 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +1000873 if (len < 0 && (errno == EINTR || errno == EAGAIN))
874 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000875 if (len <= 0) {
Damien Millerbd483e72000-04-30 10:00:53 +1000876 debug("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +1000877 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +0000878 if (c->type != SSH_CHANNEL_OPEN) {
879 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000880 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000881 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +0000882 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000883 buffer_consume(&c->output, buffer_len(&c->output));
884 c->type = SSH_CHANNEL_INPUT_DRAINING;
Ben Lindstromc486d882001-04-11 16:08:34 +0000885 debug("channel %d: status set to input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000886 } else {
887 chan_read_failed(c);
888 }
889 return -1;
890 }
Damien Millerad833b32000-08-23 10:46:23 +1000891 if(c->input_filter != NULL) {
892 if (c->input_filter(c, buf, len) == -1) {
Ben Lindstromc486d882001-04-11 16:08:34 +0000893 debug("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +1000894 chan_read_failed(c);
895 }
896 } else {
897 buffer_append(&c->input, buf, len);
898 }
Damien Millerb38eff82000-04-01 11:09:21 +1000899 }
900 return 1;
901}
902int
903channel_handle_wfd(Channel *c, fd_set * readset, fd_set * writeset)
904{
Ben Lindstrome229b252001-03-05 06:28:06 +0000905 struct termios tio;
Damien Millerb38eff82000-04-01 11:09:21 +1000906 int len;
907
908 /* Send buffered output data to the socket. */
909 if (c->wfd != -1 &&
910 FD_ISSET(c->wfd, writeset) &&
911 buffer_len(&c->output) > 0) {
912 len = write(c->wfd, buffer_ptr(&c->output),
Damien Miller6f83b8e2000-05-02 09:23:45 +1000913 buffer_len(&c->output));
914 if (len < 0 && (errno == EINTR || errno == EAGAIN))
915 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +1000916 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +0000917 if (c->type != SSH_CHANNEL_OPEN) {
918 debug("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000919 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +0000920 return -1;
921 } else if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +1000922 buffer_consume(&c->output, buffer_len(&c->output));
Ben Lindstromc486d882001-04-11 16:08:34 +0000923 debug("channel %d: status set to input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000924 c->type = SSH_CHANNEL_INPUT_DRAINING;
925 } else {
926 chan_write_failed(c);
927 }
928 return -1;
929 }
Damien Miller79438cc2001-02-16 12:34:57 +1100930 if (compat20 && c->isatty) {
Damien Miller79438cc2001-02-16 12:34:57 +1100931 if (tcgetattr(c->wfd, &tio) == 0 &&
932 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
933 /*
934 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +0000935 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +0000936 * size of a SSH2_MSG_CHANNEL_DATA message
937 * (4 byte channel id + data)
Damien Miller79438cc2001-02-16 12:34:57 +1100938 */
Ben Lindstrome229b252001-03-05 06:28:06 +0000939 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +1100940 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +1100941 }
942 }
Damien Millerb38eff82000-04-01 11:09:21 +1000943 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +1000944 if (compat20 && len > 0) {
945 c->local_consumed += len;
946 }
947 }
948 return 1;
949}
950int
951channel_handle_efd(Channel *c, fd_set * readset, fd_set * writeset)
952{
953 char buf[16*1024];
954 int len;
955
Damien Miller1383bd82000-04-06 12:32:37 +1000956/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +1000957 if (c->efd != -1) {
958 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
959 FD_ISSET(c->efd, writeset) &&
960 buffer_len(&c->extended) > 0) {
961 len = write(c->efd, buffer_ptr(&c->extended),
962 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +1100963 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000964 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000965 if (len < 0 && (errno == EINTR || errno == EAGAIN))
966 return 1;
967 if (len <= 0) {
968 debug2("channel %d: closing write-efd %d",
969 c->self, c->efd);
970 close(c->efd);
971 c->efd = -1;
972 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000973 buffer_consume(&c->extended, len);
974 c->local_consumed += len;
975 }
976 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
977 FD_ISSET(c->efd, readset)) {
978 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +1100979 debug2("channel %d: read %d from efd %d",
Damien Miller33b13562000-04-04 14:38:59 +1000980 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000981 if (len < 0 && (errno == EINTR || errno == EAGAIN))
982 return 1;
983 if (len <= 0) {
984 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +1000985 c->self, c->efd);
986 close(c->efd);
987 c->efd = -1;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000988 } else {
Damien Miller33b13562000-04-04 14:38:59 +1000989 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000990 }
Damien Miller33b13562000-04-04 14:38:59 +1000991 }
992 }
993 return 1;
994}
995int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +0000996channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +1000997{
Ben Lindstromb3921512001-04-11 15:57:50 +0000998 if (c->type == SSH_CHANNEL_OPEN &&
999 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001000 c->local_window < c->local_window_max/2 &&
1001 c->local_consumed > 0) {
1002 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1003 packet_put_int(c->remote_id);
1004 packet_put_int(c->local_consumed);
1005 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001006 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001007 c->self, c->local_window,
1008 c->local_consumed);
1009 c->local_window += c->local_consumed;
1010 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001011 }
1012 return 1;
1013}
1014
1015void
1016channel_post_open_1(Channel *c, fd_set * readset, fd_set * writeset)
1017{
1018 channel_handle_rfd(c, readset, writeset);
1019 channel_handle_wfd(c, readset, writeset);
1020}
1021
1022void
Damien Miller33b13562000-04-04 14:38:59 +10001023channel_post_open_2(Channel *c, fd_set * readset, fd_set * writeset)
1024{
1025 channel_handle_rfd(c, readset, writeset);
1026 channel_handle_wfd(c, readset, writeset);
1027 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001028
1029 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001030}
1031
1032void
Damien Millerb38eff82000-04-01 11:09:21 +10001033channel_post_output_drain_13(Channel *c, fd_set * readset, fd_set * writeset)
1034{
1035 int len;
1036 /* Send buffered output data to the socket. */
1037 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1038 len = write(c->sock, buffer_ptr(&c->output),
1039 buffer_len(&c->output));
1040 if (len <= 0)
1041 buffer_consume(&c->output, buffer_len(&c->output));
1042 else
1043 buffer_consume(&c->output, len);
1044 }
1045}
1046
1047void
Damien Miller33b13562000-04-04 14:38:59 +10001048channel_handler_init_20(void)
1049{
1050 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_20;
Damien Millerbd483e72000-04-30 10:00:53 +10001051 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001052 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001053 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001054 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001055 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001056 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001057 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001058
1059 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_2;
1060 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001061 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001062 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001063 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001064 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001065 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_2;
Damien Miller33b13562000-04-04 14:38:59 +10001066}
1067
1068void
Damien Millerb38eff82000-04-01 11:09:21 +10001069channel_handler_init_13(void)
1070{
1071 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1072 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1073 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1074 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1075 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1076 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1077 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001078 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001079 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001080
1081 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
1082 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1083 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1084 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1085 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001086 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001087 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001088}
1089
1090void
1091channel_handler_init_15(void)
1092{
1093 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_15;
Damien Millerbd483e72000-04-30 10:00:53 +10001094 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001095 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1096 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1097 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001098 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001099 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001100
1101 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1102 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1103 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1104 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open_1;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001105 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Ben Lindstromb3921512001-04-11 15:57:50 +00001106 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open_1;
Damien Millerb38eff82000-04-01 11:09:21 +10001107}
1108
1109void
1110channel_handler_init(void)
1111{
1112 int i;
1113 for(i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1114 channel_pre[i] = NULL;
1115 channel_post[i] = NULL;
1116 }
Damien Miller33b13562000-04-04 14:38:59 +10001117 if (compat20)
1118 channel_handler_init_20();
1119 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001120 channel_handler_init_13();
1121 else
1122 channel_handler_init_15();
1123}
1124
Damien Miller4af51302000-04-16 11:18:38 +10001125void
Damien Millerb38eff82000-04-01 11:09:21 +10001126channel_handler(chan_fn *ftab[], fd_set * readset, fd_set * writeset)
1127{
1128 static int did_init = 0;
1129 int i;
1130 Channel *c;
1131
1132 if (!did_init) {
1133 channel_handler_init();
1134 did_init = 1;
1135 }
1136 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001137 c = channels[i];
1138 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001139 continue;
1140 if (ftab[c->type] == NULL)
1141 continue;
1142 (*ftab[c->type])(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001143 if (chan_is_dead(c)) {
1144 /*
1145 * we have to remove the fd's from the select mask
1146 * before the channels are free'd and the fd's are
1147 * closed
1148 */
1149 if (c->wfd != -1)
1150 FD_CLR(c->wfd, writeset);
1151 if (c->rfd != -1)
1152 FD_CLR(c->rfd, readset);
1153 if (c->efd != -1) {
1154 if (c->extended_usage == CHAN_EXTENDED_READ)
1155 FD_CLR(c->efd, readset);
1156 if (c->extended_usage == CHAN_EXTENDED_WRITE)
1157 FD_CLR(c->efd, writeset);
1158 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001159 channel_free(c);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001160 }
Damien Millerb38eff82000-04-01 11:09:21 +10001161 }
1162}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001163
Damien Miller4af51302000-04-16 11:18:38 +10001164void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001165channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
1166 int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001167{
Damien Miller5e953212001-01-30 09:14:00 +11001168 int n;
1169 u_int sz;
1170
1171 n = MAX(*maxfdp, channel_max_fd);
1172
1173 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
1174 if (*readsetp == NULL || n > *maxfdp) {
1175 if (*readsetp)
1176 xfree(*readsetp);
1177 if (*writesetp)
1178 xfree(*writesetp);
1179 *readsetp = xmalloc(sz);
1180 *writesetp = xmalloc(sz);
1181 *maxfdp = n;
1182 }
1183 memset(*readsetp, 0, sz);
1184 memset(*writesetp, 0, sz);
1185
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001186 if (!rekeying)
1187 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001188}
1189
Damien Miller4af51302000-04-16 11:18:38 +10001190void
Damien Miller95def091999-11-25 00:26:21 +11001191channel_after_select(fd_set * readset, fd_set * writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192{
Damien Millerb38eff82000-04-01 11:09:21 +10001193 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001194}
1195
Damien Miller5e953212001-01-30 09:14:00 +11001196/* If there is data to send to the connection, enqueue some of it now. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001197
Damien Miller4af51302000-04-16 11:18:38 +10001198void
Damien Miller95def091999-11-25 00:26:21 +11001199channel_output_poll()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001200{
Damien Miller95def091999-11-25 00:26:21 +11001201 int len, i;
Damien Millerb38eff82000-04-01 11:09:21 +10001202 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001203
Damien Miller95def091999-11-25 00:26:21 +11001204 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001205
1206 c = channels[i];
1207 if (c == NULL)
1208 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001209
Damien Miller5428f641999-11-25 11:54:57 +11001210 /* We are only interested in channels that can have buffered incoming data. */
Damien Miller34132e52000-01-14 15:45:46 +11001211 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001212 if (c->type != SSH_CHANNEL_OPEN &&
1213 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001214 continue;
1215 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001216 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001217 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001218 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001219 if (compat20 &&
1220 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001221 /* XXX is this true? */
Ben Lindstromb1131e92001-03-05 07:27:13 +00001222 debug2("channel %d: no data after CLOSE", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001223 continue;
1224 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001225
Damien Miller95def091999-11-25 00:26:21 +11001226 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001227 if ((c->istate == CHAN_INPUT_OPEN ||
1228 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1229 (len = buffer_len(&c->input)) > 0) {
Damien Miller5428f641999-11-25 11:54:57 +11001230 /* Send some data for the other side over the secure connection. */
Damien Miller33b13562000-04-04 14:38:59 +10001231 if (compat20) {
1232 if (len > c->remote_window)
1233 len = c->remote_window;
1234 if (len > c->remote_maxpacket)
1235 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001236 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001237 if (packet_is_interactive()) {
1238 if (len > 1024)
1239 len = 512;
1240 } else {
1241 /* Keep the packets at reasonable size. */
1242 if (len > packet_get_maxsize()/2)
1243 len = packet_get_maxsize()/2;
1244 }
Damien Miller95def091999-11-25 00:26:21 +11001245 }
Damien Millerb38eff82000-04-01 11:09:21 +10001246 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001247 packet_start(compat20 ?
1248 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001249 packet_put_int(c->remote_id);
1250 packet_put_string(buffer_ptr(&c->input), len);
1251 packet_send();
1252 buffer_consume(&c->input, len);
1253 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001254 }
1255 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001256 if (compat13)
1257 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001258 /*
1259 * input-buffer is empty and read-socket shutdown:
1260 * tell peer, that we will not send more data: send IEOF
1261 */
Damien Millerb38eff82000-04-01 11:09:21 +10001262 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001263 }
Damien Miller33b13562000-04-04 14:38:59 +10001264 /* Send extended data, i.e. stderr */
1265 if (compat20 &&
1266 c->remote_window > 0 &&
1267 (len = buffer_len(&c->extended)) > 0 &&
1268 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001269 debug2("channel %d: rwin %d elen %d euse %d",
1270 c->self, c->remote_window, buffer_len(&c->extended),
1271 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001272 if (len > c->remote_window)
1273 len = c->remote_window;
1274 if (len > c->remote_maxpacket)
1275 len = c->remote_maxpacket;
1276 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1277 packet_put_int(c->remote_id);
1278 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1279 packet_put_string(buffer_ptr(&c->extended), len);
1280 packet_send();
1281 buffer_consume(&c->extended, len);
1282 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001283 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001284 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001285 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001286}
1287
Damien Miller5428f641999-11-25 11:54:57 +11001288/*
1289 * This is called when a packet of type CHANNEL_DATA has just been received.
1290 * The message type has already been consumed, but channel number and data is
1291 * still there.
1292 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001293
Damien Miller4af51302000-04-16 11:18:38 +10001294void
Damien Miller62cee002000-09-23 17:15:56 +11001295channel_input_data(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001296{
Damien Miller34132e52000-01-14 15:45:46 +11001297 int id;
Damien Miller95def091999-11-25 00:26:21 +11001298 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001299 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001300 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001301
Damien Miller95def091999-11-25 00:26:21 +11001302 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001303 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001304 c = channel_lookup(id);
1305 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11001306 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001307
Damien Miller95def091999-11-25 00:26:21 +11001308 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10001309 if (c->type != SSH_CHANNEL_OPEN &&
1310 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001311 return;
1312
1313 /* same for protocol 1.5 if output end is no longer open */
Damien Millerb38eff82000-04-01 11:09:21 +10001314 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN)
Damien Miller95def091999-11-25 00:26:21 +11001315 return;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001316
Damien Miller95def091999-11-25 00:26:21 +11001317 /* Get the data. */
1318 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001319 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001320
Damien Miller33b13562000-04-04 14:38:59 +10001321 if (compat20){
1322 if (data_len > c->local_maxpacket) {
1323 log("channel %d: rcvd big packet %d, maxpack %d",
1324 c->self, data_len, c->local_maxpacket);
1325 }
1326 if (data_len > c->local_window) {
1327 log("channel %d: rcvd too much data %d, win %d",
1328 c->self, data_len, c->local_window);
1329 xfree(data);
1330 return;
1331 }
1332 c->local_window -= data_len;
1333 }else{
1334 packet_integrity_check(plen, 4 + 4 + data_len, type);
1335 }
Damien Millerb38eff82000-04-01 11:09:21 +10001336 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11001337 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001338}
Damien Miller4af51302000-04-16 11:18:38 +10001339void
Damien Miller62cee002000-09-23 17:15:56 +11001340channel_input_extended_data(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001341{
1342 int id;
1343 int tcode;
1344 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001345 u_int data_len;
Damien Miller33b13562000-04-04 14:38:59 +10001346 Channel *c;
1347
1348 /* Get the channel number and verify it. */
1349 id = packet_get_int();
1350 c = channel_lookup(id);
1351
1352 if (c == NULL)
1353 packet_disconnect("Received extended_data for bad channel %d.", id);
1354 if (c->type != SSH_CHANNEL_OPEN) {
1355 log("channel %d: ext data for non open", id);
1356 return;
1357 }
1358 tcode = packet_get_int();
1359 if (c->efd == -1 ||
1360 c->extended_usage != CHAN_EXTENDED_WRITE ||
1361 tcode != SSH2_EXTENDED_DATA_STDERR) {
1362 log("channel %d: bad ext data", c->self);
1363 return;
1364 }
1365 data = packet_get_string(&data_len);
Damien Miller4af51302000-04-16 11:18:38 +10001366 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001367 if (data_len > c->local_window) {
1368 log("channel %d: rcvd too much extended_data %d, win %d",
1369 c->self, data_len, c->local_window);
1370 xfree(data);
1371 return;
1372 }
Damien Millerd3444942000-09-30 14:20:03 +11001373 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10001374 c->local_window -= data_len;
1375 buffer_append(&c->extended, data, data_len);
1376 xfree(data);
1377}
1378
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001379
Damien Miller5428f641999-11-25 11:54:57 +11001380/*
1381 * Returns true if no channel has too much buffered data, and false if one or
1382 * more channel is overfull.
1383 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001384
Damien Miller4af51302000-04-16 11:18:38 +10001385int
Damien Miller95def091999-11-25 00:26:21 +11001386channel_not_very_much_buffered_data()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001387{
Ben Lindstrom46c16222000-12-22 01:43:59 +00001388 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001389 Channel *c;
Damien Miller95def091999-11-25 00:26:21 +11001390
1391 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001392 c = channels[i];
1393 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Miller33b13562000-04-04 14:38:59 +10001394 if (!compat20 && buffer_len(&c->input) > packet_get_maxsize()) {
Damien Millerb38eff82000-04-01 11:09:21 +10001395 debug("channel %d: big input buffer %d",
1396 c->self, buffer_len(&c->input));
Damien Miller95def091999-11-25 00:26:21 +11001397 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001398 }
1399 if (buffer_len(&c->output) > packet_get_maxsize()) {
1400 debug("channel %d: big output buffer %d",
1401 c->self, buffer_len(&c->output));
Damien Miller95def091999-11-25 00:26:21 +11001402 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001403 }
Damien Miller95def091999-11-25 00:26:21 +11001404 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001405 }
Damien Miller95def091999-11-25 00:26:21 +11001406 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001407}
1408
Damien Miller4af51302000-04-16 11:18:38 +10001409void
Damien Miller62cee002000-09-23 17:15:56 +11001410channel_input_ieof(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001411{
1412 int id;
1413 Channel *c;
1414
1415 packet_integrity_check(plen, 4, type);
1416
1417 id = packet_get_int();
1418 c = channel_lookup(id);
1419 if (c == NULL)
1420 packet_disconnect("Received ieof for nonexistent channel %d.", id);
1421 chan_rcvd_ieof(c);
1422}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001423
Damien Miller4af51302000-04-16 11:18:38 +10001424void
Damien Miller62cee002000-09-23 17:15:56 +11001425channel_input_close(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001426{
Damien Millerb38eff82000-04-01 11:09:21 +10001427 int id;
1428 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001429
Damien Millerb38eff82000-04-01 11:09:21 +10001430 packet_integrity_check(plen, 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001431
Damien Millerb38eff82000-04-01 11:09:21 +10001432 id = packet_get_int();
1433 c = channel_lookup(id);
1434 if (c == NULL)
1435 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11001436
1437 /*
1438 * Send a confirmation that we have closed the channel and no more
1439 * data is coming for it.
1440 */
Damien Miller95def091999-11-25 00:26:21 +11001441 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10001442 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11001443 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001444
Damien Miller5428f641999-11-25 11:54:57 +11001445 /*
1446 * If the channel is in closed state, we have sent a close request,
1447 * and the other side will eventually respond with a confirmation.
1448 * Thus, we cannot free the channel here, because then there would be
1449 * no-one to receive the confirmation. The channel gets freed when
1450 * the confirmation arrives.
1451 */
Damien Millerb38eff82000-04-01 11:09:21 +10001452 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11001453 /*
1454 * Not a closed channel - mark it as draining, which will
1455 * cause it to be freed later.
1456 */
Damien Millerb38eff82000-04-01 11:09:21 +10001457 buffer_consume(&c->input, buffer_len(&c->input));
1458 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11001459 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001460}
1461
Damien Millerb38eff82000-04-01 11:09:21 +10001462/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Miller4af51302000-04-16 11:18:38 +10001463void
Damien Miller62cee002000-09-23 17:15:56 +11001464channel_input_oclose(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001465{
Damien Millerb38eff82000-04-01 11:09:21 +10001466 int id = packet_get_int();
1467 Channel *c = channel_lookup(id);
1468 packet_integrity_check(plen, 4, type);
1469 if (c == NULL)
1470 packet_disconnect("Received oclose for nonexistent channel %d.", id);
1471 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001472}
1473
Damien Miller4af51302000-04-16 11:18:38 +10001474void
Damien Miller62cee002000-09-23 17:15:56 +11001475channel_input_close_confirmation(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10001476{
1477 int id = packet_get_int();
1478 Channel *c = channel_lookup(id);
1479
Damien Miller4af51302000-04-16 11:18:38 +10001480 packet_done();
Damien Millerb38eff82000-04-01 11:09:21 +10001481 if (c == NULL)
1482 packet_disconnect("Received close confirmation for "
1483 "out-of-range channel %d.", id);
1484 if (c->type != SSH_CHANNEL_CLOSED)
1485 packet_disconnect("Received close confirmation for "
1486 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001487 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001488}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001489
Damien Miller4af51302000-04-16 11:18:38 +10001490void
Damien Miller62cee002000-09-23 17:15:56 +11001491channel_input_open_confirmation(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001492{
Damien Millerb38eff82000-04-01 11:09:21 +10001493 int id, remote_id;
1494 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001495
Damien Miller33b13562000-04-04 14:38:59 +10001496 if (!compat20)
1497 packet_integrity_check(plen, 4 + 4, type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001498
Damien Millerb38eff82000-04-01 11:09:21 +10001499 id = packet_get_int();
1500 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001501
Damien Millerb38eff82000-04-01 11:09:21 +10001502 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1503 packet_disconnect("Received open confirmation for "
1504 "non-opening channel %d.", id);
1505 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11001506 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10001507 c->remote_id = remote_id;
1508 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10001509
1510 if (compat20) {
1511 c->remote_window = packet_get_int();
1512 c->remote_maxpacket = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001513 packet_done();
Damien Miller33b13562000-04-04 14:38:59 +10001514 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001515 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001516 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001517 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001518 }
1519 debug("channel %d: open confirm rwindow %d rmax %d", c->self,
1520 c->remote_window, c->remote_maxpacket);
1521 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001522}
1523
Damien Miller4af51302000-04-16 11:18:38 +10001524void
Damien Miller62cee002000-09-23 17:15:56 +11001525channel_input_open_failure(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001526{
Kevin Steves12057502001-02-05 14:54:34 +00001527 int id, reason;
1528 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001529 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001530
Damien Miller33b13562000-04-04 14:38:59 +10001531 if (!compat20)
1532 packet_integrity_check(plen, 4, type);
Damien Millerb38eff82000-04-01 11:09:21 +10001533
1534 id = packet_get_int();
1535 c = channel_lookup(id);
1536
1537 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
1538 packet_disconnect("Received open failure for "
1539 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10001540 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00001541 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00001542 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00001543 msg = packet_get_string(NULL);
1544 lang = packet_get_string(NULL);
1545 }
Damien Miller4af51302000-04-16 11:18:38 +10001546 packet_done();
Kevin Steves12057502001-02-05 14:54:34 +00001547 log("channel_open_failure: %d: reason %d %s", id,
1548 reason, msg ? msg : "<no additional info>");
1549 if (msg != NULL)
1550 xfree(msg);
1551 if (lang != NULL)
1552 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10001553 }
Damien Miller95def091999-11-25 00:26:21 +11001554 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001555 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001556}
1557
Damien Miller33b13562000-04-04 14:38:59 +10001558void
Damien Miller62cee002000-09-23 17:15:56 +11001559channel_input_channel_request(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001560{
1561 int id;
1562 Channel *c;
1563
1564 id = packet_get_int();
1565 c = channel_lookup(id);
1566
1567 if (c == NULL ||
1568 (c->type != SSH_CHANNEL_OPEN && c->type != SSH_CHANNEL_LARVAL))
1569 packet_disconnect("Received request for "
1570 "non-open channel %d.", id);
1571 if (c->cb_fn != NULL && c->cb_event == type) {
Damien Millerd3444942000-09-30 14:20:03 +11001572 debug2("callback start");
Damien Miller33b13562000-04-04 14:38:59 +10001573 c->cb_fn(c->self, c->cb_arg);
Damien Millerd3444942000-09-30 14:20:03 +11001574 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10001575 } else {
1576 char *service = packet_get_string(NULL);
Ben Lindstromcc74df72001-03-05 06:20:14 +00001577 debug("channel %d: rcvd request for %s", c->self, service);
Damien Millerd3444942000-09-30 14:20:03 +11001578 debug("cb_fn %p cb_event %d", c->cb_fn , c->cb_event);
Damien Miller33b13562000-04-04 14:38:59 +10001579 xfree(service);
1580 }
1581}
1582
Damien Miller4af51302000-04-16 11:18:38 +10001583void
Damien Miller62cee002000-09-23 17:15:56 +11001584channel_input_window_adjust(int type, int plen, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10001585{
1586 Channel *c;
1587 int id, adjust;
1588
1589 if (!compat20)
1590 return;
1591
1592 /* Get the channel number and verify it. */
1593 id = packet_get_int();
1594 c = channel_lookup(id);
1595
1596 if (c == NULL || c->type != SSH_CHANNEL_OPEN) {
1597 log("Received window adjust for "
1598 "non-open channel %d.", id);
1599 return;
1600 }
1601 adjust = packet_get_int();
Damien Miller4af51302000-04-16 11:18:38 +10001602 packet_done();
Damien Millerd3444942000-09-30 14:20:03 +11001603 debug2("channel %d: rcvd adjust %d", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10001604 c->remote_window += adjust;
1605}
1606
Damien Miller5428f641999-11-25 11:54:57 +11001607/*
1608 * Stops listening for channels, and removes any unix domain sockets that we
1609 * might have.
1610 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001611
Damien Miller4af51302000-04-16 11:18:38 +10001612void
Damien Miller95def091999-11-25 00:26:21 +11001613channel_stop_listening()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001614{
Damien Miller95def091999-11-25 00:26:21 +11001615 int i;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001616 Channel *c;
1617
Damien Miller95def091999-11-25 00:26:21 +11001618 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001619 c = channels[i];
1620 if (c != NULL) {
1621 switch (c->type) {
1622 case SSH_CHANNEL_AUTH_SOCKET:
1623 close(c->sock);
1624 unlink(c->path);
1625 channel_free(c);
1626 break;
1627 case SSH_CHANNEL_PORT_LISTENER:
1628 case SSH_CHANNEL_RPORT_LISTENER:
1629 case SSH_CHANNEL_X11_LISTENER:
1630 close(c->sock);
1631 channel_free(c);
1632 break;
1633 default:
1634 break;
1635 }
Damien Miller95def091999-11-25 00:26:21 +11001636 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001637 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001638}
1639
Damien Miller5428f641999-11-25 11:54:57 +11001640/*
Damien Miller6f83b8e2000-05-02 09:23:45 +10001641 * Closes the sockets/fds of all channels. This is used to close extra file
Damien Miller5428f641999-11-25 11:54:57 +11001642 * descriptors after a fork.
1643 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001644
Damien Miller4af51302000-04-16 11:18:38 +10001645void
Damien Miller95def091999-11-25 00:26:21 +11001646channel_close_all()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001647{
Damien Miller95def091999-11-25 00:26:21 +11001648 int i;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001649
Damien Miller6f83b8e2000-05-02 09:23:45 +10001650 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001651 if (channels[i] != NULL)
1652 channel_close_fds(channels[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001653}
1654
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001655/* Returns true if any channel is still open. */
1656
Damien Miller4af51302000-04-16 11:18:38 +10001657int
Damien Miller95def091999-11-25 00:26:21 +11001658channel_still_open()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001659{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001660 int i;
1661 Channel *c;
1662
1663 for (i = 0; i < channels_alloc; i++) {
1664 c = channels[i];
1665 if (c == NULL)
1666 continue;
1667 switch (c->type) {
Damien Miller95def091999-11-25 00:26:21 +11001668 case SSH_CHANNEL_X11_LISTENER:
1669 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001670 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001671 case SSH_CHANNEL_CLOSED:
1672 case SSH_CHANNEL_AUTH_SOCKET:
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001673 case SSH_CHANNEL_DYNAMIC:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001674 case SSH_CHANNEL_CONNECTING: /* XXX ??? */
Damien Miller95def091999-11-25 00:26:21 +11001675 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001676 case SSH_CHANNEL_LARVAL:
1677 if (!compat20)
1678 fatal("cannot happen: SSH_CHANNEL_LARVAL");
1679 continue;
Damien Miller95def091999-11-25 00:26:21 +11001680 case SSH_CHANNEL_OPENING:
1681 case SSH_CHANNEL_OPEN:
1682 case SSH_CHANNEL_X11_OPEN:
1683 return 1;
1684 case SSH_CHANNEL_INPUT_DRAINING:
1685 case SSH_CHANNEL_OUTPUT_DRAINING:
1686 if (!compat13)
1687 fatal("cannot happen: OUT_DRAIN");
1688 return 1;
1689 default:
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001690 fatal("channel_still_open: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001691 /* NOTREACHED */
1692 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001693 }
Damien Miller95def091999-11-25 00:26:21 +11001694 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001695}
1696
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001697/* Returns the id of an open channel suitable for keepaliving */
1698
1699int
1700channel_find_open()
1701{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001702 int i;
1703 Channel *c;
1704
1705 for (i = 0; i < channels_alloc; i++) {
1706 c = channels[i];
1707 if (c == NULL)
1708 continue;
1709 switch (c->type) {
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001710 case SSH_CHANNEL_CLOSED:
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001711 case SSH_CHANNEL_DYNAMIC:
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001712 case SSH_CHANNEL_X11_LISTENER:
1713 case SSH_CHANNEL_PORT_LISTENER:
1714 case SSH_CHANNEL_RPORT_LISTENER:
1715 case SSH_CHANNEL_OPENING:
Ben Lindstromd334b272001-04-14 23:08:36 +00001716 continue;
1717 case SSH_CHANNEL_LARVAL:
1718 case SSH_CHANNEL_AUTH_SOCKET:
1719 case SSH_CHANNEL_CONNECTING: /* XXX ??? */
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001720 case SSH_CHANNEL_OPEN:
1721 case SSH_CHANNEL_X11_OPEN:
1722 return i;
1723 case SSH_CHANNEL_INPUT_DRAINING:
1724 case SSH_CHANNEL_OUTPUT_DRAINING:
1725 if (!compat13)
1726 fatal("cannot happen: OUT_DRAIN");
1727 return i;
1728 default:
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001729 fatal("channel_find_open: bad channel type %d", c->type);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001730 /* NOTREACHED */
1731 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001732 }
Ben Lindstrom5744dc42001-04-13 23:28:01 +00001733 return -1;
1734}
1735
1736
Damien Miller5428f641999-11-25 11:54:57 +11001737/*
1738 * Returns a message describing the currently open forwarded connections,
1739 * suitable for sending to the client. The message contains crlf pairs for
1740 * newlines.
1741 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001742
Damien Miller95def091999-11-25 00:26:21 +11001743char *
1744channel_open_message()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001745{
Damien Miller95def091999-11-25 00:26:21 +11001746 Buffer buffer;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001747 Channel *c;
1748 char buf[1024], *cp;
Damien Miller95def091999-11-25 00:26:21 +11001749 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001750
Damien Miller95def091999-11-25 00:26:21 +11001751 buffer_init(&buffer);
1752 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001753 buffer_append(&buffer, buf, strlen(buf));
Damien Miller95def091999-11-25 00:26:21 +11001754 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001755 c = channels[i];
1756 if (c == NULL)
1757 continue;
Damien Miller95def091999-11-25 00:26:21 +11001758 switch (c->type) {
Damien Miller95def091999-11-25 00:26:21 +11001759 case SSH_CHANNEL_X11_LISTENER:
1760 case SSH_CHANNEL_PORT_LISTENER:
Damien Miller0bc1bd82000-11-13 22:57:25 +11001761 case SSH_CHANNEL_RPORT_LISTENER:
Damien Miller95def091999-11-25 00:26:21 +11001762 case SSH_CHANNEL_CLOSED:
1763 case SSH_CHANNEL_AUTH_SOCKET:
1764 continue;
Damien Miller33b13562000-04-04 14:38:59 +10001765 case SSH_CHANNEL_LARVAL:
Damien Miller95def091999-11-25 00:26:21 +11001766 case SSH_CHANNEL_OPENING:
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001767 case SSH_CHANNEL_CONNECTING:
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001768 case SSH_CHANNEL_DYNAMIC:
Damien Miller95def091999-11-25 00:26:21 +11001769 case SSH_CHANNEL_OPEN:
1770 case SSH_CHANNEL_X11_OPEN:
1771 case SSH_CHANNEL_INPUT_DRAINING:
1772 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Millerb38eff82000-04-01 11:09:21 +10001773 snprintf(buf, sizeof buf, " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d)\r\n",
Damien Miller34132e52000-01-14 15:45:46 +11001774 c->self, c->remote_name,
1775 c->type, c->remote_id,
1776 c->istate, buffer_len(&c->input),
Damien Millerb38eff82000-04-01 11:09:21 +10001777 c->ostate, buffer_len(&c->output),
1778 c->rfd, c->wfd);
Damien Miller95def091999-11-25 00:26:21 +11001779 buffer_append(&buffer, buf, strlen(buf));
1780 continue;
1781 default:
Damien Millerb38eff82000-04-01 11:09:21 +10001782 fatal("channel_open_message: bad channel type %d", c->type);
Damien Miller95def091999-11-25 00:26:21 +11001783 /* NOTREACHED */
1784 }
1785 }
1786 buffer_append(&buffer, "\0", 1);
1787 cp = xstrdup(buffer_ptr(&buffer));
1788 buffer_free(&buffer);
1789 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001790}
1791
Damien Miller5428f641999-11-25 11:54:57 +11001792/*
1793 * Initiate forwarding of connections to local port "port" through the secure
1794 * channel to host:port from remote side.
1795 */
Kevin Steves12057502001-02-05 14:54:34 +00001796int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001797channel_request_local_forwarding(u_short listen_port, const char *host_to_connect,
1798 u_short port_to_connect, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001799{
Kevin Steves12057502001-02-05 14:54:34 +00001800 return channel_request_forwarding(
Damien Miller0bc1bd82000-11-13 22:57:25 +11001801 NULL, listen_port,
1802 host_to_connect, port_to_connect,
1803 gateway_ports, /*remote_fwd*/ 0);
1804}
1805
1806/*
1807 * If 'remote_fwd' is true we have a '-R style' listener for protocol 2
1808 * (SSH_CHANNEL_RPORT_LISTENER).
1809 */
Kevin Steves12057502001-02-05 14:54:34 +00001810int
Damien Miller0bc1bd82000-11-13 22:57:25 +11001811channel_request_forwarding(
1812 const char *listen_address, u_short listen_port,
1813 const char *host_to_connect, u_short port_to_connect,
1814 int gateway_ports, int remote_fwd)
1815{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001816 Channel *c;
1817 int success, sock, on = 1, ctype;
Damien Miller34132e52000-01-14 15:45:46 +11001818 struct addrinfo hints, *ai, *aitop;
1819 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller0bc1bd82000-11-13 22:57:25 +11001820 const char *host;
Damien Miller5428f641999-11-25 11:54:57 +11001821 struct linger linger;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001822
Kevin Steves12057502001-02-05 14:54:34 +00001823 success = 0;
1824
Damien Miller0bc1bd82000-11-13 22:57:25 +11001825 if (remote_fwd) {
1826 host = listen_address;
Kevin Stevesef4eea92001-02-05 12:42:17 +00001827 ctype = SSH_CHANNEL_RPORT_LISTENER;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001828 } else {
1829 host = host_to_connect;
1830 ctype =SSH_CHANNEL_PORT_LISTENER;
1831 }
1832
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001833 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00001834 error("Forward host name too long.");
1835 return success;
1836 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001837
Damien Miller0bc1bd82000-11-13 22:57:25 +11001838 /* XXX listen_address is currently ignored */
Damien Miller5428f641999-11-25 11:54:57 +11001839 /*
Damien Miller34132e52000-01-14 15:45:46 +11001840 * getaddrinfo returns a loopback address if the hostname is
1841 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11001842 */
Damien Miller34132e52000-01-14 15:45:46 +11001843 memset(&hints, 0, sizeof(hints));
1844 hints.ai_family = IPv4or6;
1845 hints.ai_flags = gateway_ports ? AI_PASSIVE : 0;
1846 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001847 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001848 if (getaddrinfo(NULL, strport, &hints, &aitop) != 0)
1849 packet_disconnect("getaddrinfo: fatal error");
Damien Miller5428f641999-11-25 11:54:57 +11001850
Damien Miller34132e52000-01-14 15:45:46 +11001851 for (ai = aitop; ai; ai = ai->ai_next) {
1852 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
1853 continue;
1854 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
1855 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +11001856 error("channel_request_forwarding: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11001857 continue;
1858 }
1859 /* Create a port to listen for the host. */
1860 sock = socket(ai->ai_family, SOCK_STREAM, 0);
1861 if (sock < 0) {
1862 /* this is no error since kernel may not support ipv6 */
1863 verbose("socket: %.100s", strerror(errno));
1864 continue;
1865 }
1866 /*
1867 * Set socket options. We would like the socket to disappear
1868 * as soon as it has been closed for whatever reason.
1869 */
1870 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
1871 linger.l_onoff = 1;
1872 linger.l_linger = 5;
1873 setsockopt(sock, SOL_SOCKET, SO_LINGER, (void *)&linger, sizeof(linger));
1874 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11001875
Damien Miller34132e52000-01-14 15:45:46 +11001876 /* Bind the socket to the address. */
1877 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
1878 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11001879 if (!ai->ai_next)
1880 error("bind: %.100s", strerror(errno));
1881 else
1882 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00001883
Damien Miller34132e52000-01-14 15:45:46 +11001884 close(sock);
1885 continue;
1886 }
1887 /* Start listening for connections on the socket. */
1888 if (listen(sock, 5) < 0) {
1889 error("listen: %.100s", strerror(errno));
1890 close(sock);
1891 continue;
1892 }
1893 /* Allocate a channel number for the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001894 c = channel_new("port listener", ctype, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10001895 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11001896 0, xstrdup("port listener"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001897 if (c == NULL) {
1898 error("channel_request_forwarding: channel_new failed");
1899 close(sock);
1900 continue;
1901 }
1902 strlcpy(c->path, host, sizeof(c->path));
1903 c->host_port = port_to_connect;
1904 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11001905 success = 1;
1906 }
1907 if (success == 0)
Kevin Steves12057502001-02-05 14:54:34 +00001908 error("channel_request_forwarding: cannot listen to port: %d",
1909 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11001910 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00001911 return success;
Damien Miller95def091999-11-25 00:26:21 +11001912}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001913
Damien Miller5428f641999-11-25 11:54:57 +11001914/*
1915 * Initiate forwarding of connections to port "port" on remote host through
1916 * the secure channel to host:port from local side.
1917 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001918
Damien Miller4af51302000-04-16 11:18:38 +10001919void
Damien Miller0bc1bd82000-11-13 22:57:25 +11001920channel_request_remote_forwarding(u_short listen_port,
1921 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001922{
Damien Miller0bc1bd82000-11-13 22:57:25 +11001923 int payload_len, type, success = 0;
1924
Damien Miller95def091999-11-25 00:26:21 +11001925 /* Record locally that connection to this host/port is permitted. */
1926 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
1927 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001928
Damien Miller95def091999-11-25 00:26:21 +11001929 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10001930 if (compat20) {
1931 const char *address_to_bind = "0.0.0.0";
1932 packet_start(SSH2_MSG_GLOBAL_REQUEST);
1933 packet_put_cstring("tcpip-forward");
1934 packet_put_char(0); /* boolean: want reply */
1935 packet_put_cstring(address_to_bind);
1936 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001937 packet_send();
1938 packet_write_wait();
1939 /* Assume that server accepts the request */
1940 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10001941 } else {
1942 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10001943 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10001944 packet_put_cstring(host_to_connect);
1945 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10001946 packet_send();
1947 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11001948
1949 /* Wait for response from the remote side. */
1950 type = packet_read(&payload_len);
1951 switch (type) {
1952 case SSH_SMSG_SUCCESS:
1953 success = 1;
1954 break;
1955 case SSH_SMSG_FAILURE:
1956 log("Warning: Server denied remote port forwarding.");
1957 break;
1958 default:
1959 /* Unknown packet */
1960 packet_disconnect("Protocol error for port forward request:"
1961 "received packet type %d.", type);
1962 }
1963 }
1964 if (success) {
1965 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
1966 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
1967 permitted_opens[num_permitted_opens].listen_port = listen_port;
1968 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10001969 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001970}
1971
Damien Miller5428f641999-11-25 11:54:57 +11001972/*
1973 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
1974 * listening for the port, and sends back a success reply (or disconnect
1975 * message if there was an error). This never returns if there was an error.
1976 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001977
Damien Miller4af51302000-04-16 11:18:38 +10001978void
Damien Millere247cc42000-05-07 12:03:14 +10001979channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001980{
Damien Milleraae6c611999-12-06 11:47:28 +11001981 u_short port, host_port;
Damien Miller95def091999-11-25 00:26:21 +11001982 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001983
Damien Miller95def091999-11-25 00:26:21 +11001984 /* Get arguments from the packet. */
1985 port = packet_get_int();
1986 hostname = packet_get_string(NULL);
1987 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001988
Damien Millerbac2d8a2000-09-05 16:13:06 +11001989#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11001990 /*
1991 * Check that an unprivileged user is not trying to forward a
1992 * privileged port.
1993 */
Damien Miller95def091999-11-25 00:26:21 +11001994 if (port < IPPORT_RESERVED && !is_root)
1995 packet_disconnect("Requested forwarding of port %d but user is not root.",
1996 port);
Damien Millerbac2d8a2000-09-05 16:13:06 +11001997#endif
Damien Miller0bc1bd82000-11-13 22:57:25 +11001998 /* Initiate forwarding */
Damien Millere247cc42000-05-07 12:03:14 +10001999 channel_request_local_forwarding(port, hostname, host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002000
2001 /* Free the argument string. */
2002 xfree(hostname);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002003}
2004
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002005/*
2006 * Permits opening to any host/port if permitted_opens[] is empty. This is
2007 * usually called by the server, because the user could connect to any port
2008 * anyway, and the server has no way to know but to trust the client anyway.
2009 */
2010void
2011channel_permit_all_opens()
2012{
2013 if (num_permitted_opens == 0)
2014 all_opens_permitted = 1;
2015}
2016
Ben Lindstroma3700052001-04-05 23:26:32 +00002017void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002018channel_add_permitted_opens(char *host, int port)
2019{
2020 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2021 fatal("channel_request_remote_forwarding: too many forwards");
2022 debug("allow port forwarding to host %s port %d", host, port);
2023
2024 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2025 permitted_opens[num_permitted_opens].port_to_connect = port;
2026 num_permitted_opens++;
2027
2028 all_opens_permitted = 0;
2029}
2030
Ben Lindstroma3700052001-04-05 23:26:32 +00002031void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002032channel_clear_permitted_opens(void)
2033{
2034 int i;
2035
2036 for (i = 0; i < num_permitted_opens; i++)
2037 xfree(permitted_opens[i].host_to_connect);
2038 num_permitted_opens = 0;
2039
2040}
2041
2042
2043/* return socket to remote host, port */
Damien Millerb38eff82000-04-01 11:09:21 +10002044int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002045connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002046{
Damien Miller34132e52000-01-14 15:45:46 +11002047 struct addrinfo hints, *ai, *aitop;
2048 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2049 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002050 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002051
Damien Miller34132e52000-01-14 15:45:46 +11002052 memset(&hints, 0, sizeof(hints));
2053 hints.ai_family = IPv4or6;
2054 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002055 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002056 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002057 error("connect_to %.100s: unknown host (%s)", host,
2058 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002059 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002060 }
Damien Miller34132e52000-01-14 15:45:46 +11002061 for (ai = aitop; ai; ai = ai->ai_next) {
2062 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2063 continue;
2064 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2065 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002066 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002067 continue;
2068 }
Damien Miller34132e52000-01-14 15:45:46 +11002069 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2070 if (sock < 0) {
2071 error("socket: %.100s", strerror(errno));
2072 continue;
2073 }
Ben Lindstrom48bd7c12001-01-09 00:35:42 +00002074 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002075 fatal("connect_to: F_SETFL: %s", strerror(errno));
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002076 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2077 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002078 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002079 strerror(errno));
2080 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002081 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002082 }
2083 break; /* success */
2084
2085 }
2086 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002087 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002088 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002089 return -1;
2090 }
2091 /* success */
2092 return sock;
2093}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002094
Damien Miller0bc1bd82000-11-13 22:57:25 +11002095int
2096channel_connect_by_listen_adress(u_short listen_port)
2097{
2098 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002099
Damien Miller0bc1bd82000-11-13 22:57:25 +11002100 for (i = 0; i < num_permitted_opens; i++)
2101 if (permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002102 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002103 permitted_opens[i].host_to_connect,
2104 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002105 error("WARNING: Server requests forwarding for unknown listen_port %d",
2106 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002107 return -1;
2108}
Damien Millerb38eff82000-04-01 11:09:21 +10002109
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002110/* Check if connecting to that port is permitted and connect. */
2111int
2112channel_connect_to(const char *host, u_short port)
2113{
2114 int i, permit;
2115
2116 permit = all_opens_permitted;
2117 if (!permit) {
2118 for (i = 0; i < num_permitted_opens; i++)
2119 if (permitted_opens[i].port_to_connect == port &&
2120 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2121 permit = 1;
2122
2123 }
2124 if (!permit) {
2125 log("Received request to connect to host %.100s port %d, "
2126 "but the request was denied.", host, port);
2127 return -1;
2128 }
2129 return connect_to(host, port);
2130}
2131
Damien Millerb38eff82000-04-01 11:09:21 +10002132/*
2133 * This is called after receiving PORT_OPEN message. This attempts to
2134 * connect to the given host:port, and sends back CHANNEL_OPEN_CONFIRMATION
2135 * or CHANNEL_OPEN_FAILURE.
2136 */
2137
Damien Miller4af51302000-04-16 11:18:38 +10002138void
Damien Miller62cee002000-09-23 17:15:56 +11002139channel_input_port_open(int type, int plen, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002140{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002141 Channel *c = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002142 u_short host_port;
2143 char *host, *originator_string;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002144 int remote_id, sock = -1;
Damien Millerb38eff82000-04-01 11:09:21 +10002145
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002146 remote_id = packet_get_int();
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002147 host = packet_get_string(NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10002148 host_port = packet_get_int();
2149
Damien Millerb38eff82000-04-01 11:09:21 +10002150 if (have_hostname_in_open) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002151 originator_string = packet_get_string(NULL);
Damien Millerb38eff82000-04-01 11:09:21 +10002152 } else {
2153 originator_string = xstrdup("unknown (remote did not supply name)");
Damien Miller95def091999-11-25 00:26:21 +11002154 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002155 packet_done();
2156 sock = channel_connect_to(host, host_port);
2157 if (sock != -1) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002158 c = channel_new("connected socket",
2159 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2160 originator_string, 1);
2161 if (c == NULL) {
2162 error("channel_input_port_open: channel_new failed");
2163 close(sock);
2164 } else {
2165 c->remote_id = remote_id;
2166 }
2167 }
2168 if (c == NULL) {
2169 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2170 packet_put_int(remote_id);
2171 } else {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002172 /*XXX delay answer? */
Damien Millerb38eff82000-04-01 11:09:21 +10002173 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002174 packet_put_int(remote_id);
2175 packet_put_int(c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10002176 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002177 packet_send();
Damien Miller95def091999-11-25 00:26:21 +11002178 xfree(host);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002179}
2180
Damien Miller5428f641999-11-25 11:54:57 +11002181/*
2182 * Creates an internet domain socket for listening for X11 connections.
2183 * Returns a suitable value for the DISPLAY variable, or NULL if an error
2184 * occurs.
2185 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002186
Damien Miller34132e52000-01-14 15:45:46 +11002187#define NUM_SOCKS 10
2188
Damien Miller95def091999-11-25 00:26:21 +11002189char *
Damien Millera34a28b1999-12-14 10:47:15 +11002190x11_create_display_inet(int screen_number, int x11_display_offset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002191{
Damien Milleraae6c611999-12-06 11:47:28 +11002192 int display_number, sock;
2193 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002194 struct addrinfo hints, *ai, *aitop;
2195 char strport[NI_MAXSERV];
2196 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
2197 char display[512];
Damien Miller95def091999-11-25 00:26:21 +11002198 char hostname[MAXHOSTNAMELEN];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002199
Damien Millera34a28b1999-12-14 10:47:15 +11002200 for (display_number = x11_display_offset;
Damien Miller95def091999-11-25 00:26:21 +11002201 display_number < MAX_DISPLAYS;
2202 display_number++) {
2203 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002204 memset(&hints, 0, sizeof(hints));
2205 hints.ai_family = IPv4or6;
2206 hints.ai_flags = AI_PASSIVE; /* XXX loopback only ? */
2207 hints.ai_socktype = SOCK_STREAM;
2208 snprintf(strport, sizeof strport, "%d", port);
2209 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2210 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Damien Miller95def091999-11-25 00:26:21 +11002211 return NULL;
2212 }
Damien Miller34132e52000-01-14 15:45:46 +11002213 for (ai = aitop; ai; ai = ai->ai_next) {
2214 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2215 continue;
2216 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2217 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002218 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002219 error("socket: %.100s", strerror(errno));
2220 return NULL;
2221 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002222 debug("x11_create_display_inet: Socket family %d not supported",
2223 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002224 continue;
2225 }
Damien Miller34132e52000-01-14 15:45:46 +11002226 }
2227 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2228 debug("bind port %d: %.100s", port, strerror(errno));
2229 shutdown(sock, SHUT_RDWR);
2230 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002231
2232 if (ai->ai_next)
2233 continue;
2234
Damien Miller34132e52000-01-14 15:45:46 +11002235 for (n = 0; n < num_socks; n++) {
2236 shutdown(socks[n], SHUT_RDWR);
2237 close(socks[n]);
2238 }
2239 num_socks = 0;
2240 break;
2241 }
2242 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002243#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002244 if (num_socks == NUM_SOCKS)
2245 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002246#else
2247 break;
2248#endif
Damien Miller95def091999-11-25 00:26:21 +11002249 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002250 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002251 if (num_socks > 0)
2252 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002253 }
Damien Miller95def091999-11-25 00:26:21 +11002254 if (display_number >= MAX_DISPLAYS) {
2255 error("Failed to allocate internet-domain X11 display socket.");
2256 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002257 }
Damien Miller95def091999-11-25 00:26:21 +11002258 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002259 for (n = 0; n < num_socks; n++) {
2260 sock = socks[n];
2261 if (listen(sock, 5) < 0) {
2262 error("listen: %.100s", strerror(errno));
2263 shutdown(sock, SHUT_RDWR);
2264 close(sock);
2265 return NULL;
2266 }
Damien Miller95def091999-11-25 00:26:21 +11002267 }
Damien Miller34132e52000-01-14 15:45:46 +11002268
Damien Miller95def091999-11-25 00:26:21 +11002269 /* Set up a suitable value for the DISPLAY variable. */
2270 if (gethostname(hostname, sizeof(hostname)) < 0)
2271 fatal("gethostname: %.100s", strerror(errno));
Damien Miller76112de1999-12-21 11:18:08 +11002272
2273#ifdef IPADDR_IN_DISPLAY
Kevin Stevesef4eea92001-02-05 12:42:17 +00002274 /*
Damien Miller76112de1999-12-21 11:18:08 +11002275 * HPUX detects the local hostname in the DISPLAY variable and tries
2276 * to set up a shared memory connection to the server, which it
2277 * incorrectly supposes to be local.
2278 *
2279 * The workaround - as used in later $$H and other programs - is
2280 * is to set display to the host's IP address.
2281 */
2282 {
2283 struct hostent *he;
2284 struct in_addr my_addr;
2285
2286 he = gethostbyname(hostname);
2287 if (he == NULL) {
2288 error("[X11-broken-fwd-hostname-workaround] Could not get "
2289 "IP address for hostname %s.", hostname);
2290
2291 packet_send_debug("[X11-broken-fwd-hostname-workaround]"
2292 "Could not get IP address for hostname %s.", hostname);
2293
2294 shutdown(sock, SHUT_RDWR);
2295 close(sock);
2296
2297 return NULL;
2298 }
2299
2300 memcpy(&my_addr, he->h_addr_list[0], sizeof(struct in_addr));
2301
2302 /* Set DISPLAY to <ip address>:screen.display */
Kevin Stevesef4eea92001-02-05 12:42:17 +00002303 snprintf(display, sizeof(display), "%.50s:%d.%d", inet_ntoa(my_addr),
Kevin Stevesda6cd592000-12-29 18:41:21 +00002304 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002305 }
2306#else /* IPADDR_IN_DISPLAY */
2307 /* Just set DISPLAY to hostname:screen.display */
Damien Miller34132e52000-01-14 15:45:46 +11002308 snprintf(display, sizeof display, "%.400s:%d.%d", hostname,
Kevin Stevesda6cd592000-12-29 18:41:21 +00002309 display_number, screen_number);
Damien Miller76112de1999-12-21 11:18:08 +11002310#endif /* IPADDR_IN_DISPLAY */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002311
Damien Miller34132e52000-01-14 15:45:46 +11002312 /* Allocate a channel for each socket. */
2313 for (n = 0; n < num_socks; n++) {
2314 sock = socks[n];
Damien Millerbd483e72000-04-30 10:00:53 +10002315 (void) channel_new("x11 listener",
2316 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2317 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Miller69b69aa2000-10-28 14:19:58 +11002318 0, xstrdup("X11 inet listener"), 1);
Damien Miller34132e52000-01-14 15:45:46 +11002319 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002320
Damien Miller95def091999-11-25 00:26:21 +11002321 /* Return a suitable value for the DISPLAY environment variable. */
Damien Miller34132e52000-01-14 15:45:46 +11002322 return xstrdup(display);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002323}
2324
2325#ifndef X_UNIX_PATH
2326#define X_UNIX_PATH "/tmp/.X11-unix/X"
2327#endif
2328
2329static
2330int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002331connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002332{
Damien Miller95def091999-11-25 00:26:21 +11002333 static const char *const x_sockets[] = {
2334 X_UNIX_PATH "%u",
2335 "/var/X/.X11-unix/X" "%u",
2336 "/usr/spool/sockets/X11/" "%u",
2337 NULL
2338 };
2339 int sock;
2340 struct sockaddr_un addr;
2341 const char *const * path;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002342
Damien Miller95def091999-11-25 00:26:21 +11002343 for (path = x_sockets; *path; ++path) {
2344 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2345 if (sock < 0)
2346 error("socket: %.100s", strerror(errno));
2347 memset(&addr, 0, sizeof(addr));
2348 addr.sun_family = AF_UNIX;
2349 snprintf(addr.sun_path, sizeof addr.sun_path, *path, dnr);
2350 if (connect(sock, (struct sockaddr *) & addr, sizeof(addr)) == 0)
2351 return sock;
2352 close(sock);
2353 }
2354 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2355 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002356}
2357
Damien Millerbd483e72000-04-30 10:00:53 +10002358int
2359x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002360{
Damien Millerbd483e72000-04-30 10:00:53 +10002361 int display_number, sock = 0;
Damien Miller95def091999-11-25 00:26:21 +11002362 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002363 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002364 struct addrinfo hints, *ai, *aitop;
2365 char strport[NI_MAXSERV];
2366 int gaierr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002367
Damien Miller95def091999-11-25 00:26:21 +11002368 /* Try to open a socket for the local X server. */
2369 display = getenv("DISPLAY");
2370 if (!display) {
2371 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002372 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002373 }
Damien Miller5428f641999-11-25 11:54:57 +11002374 /*
2375 * Now we decode the value of the DISPLAY variable and make a
2376 * connection to the real X server.
2377 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002378
Damien Miller5428f641999-11-25 11:54:57 +11002379 /*
2380 * Check if it is a unix domain socket. Unix domain displays are in
2381 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2382 */
Damien Miller95def091999-11-25 00:26:21 +11002383 if (strncmp(display, "unix:", 5) == 0 ||
2384 display[0] == ':') {
2385 /* Connect to the unix domain socket. */
2386 if (sscanf(strrchr(display, ':') + 1, "%d", &display_number) != 1) {
2387 error("Could not parse display number from DISPLAY: %.100s",
2388 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002389 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002390 }
2391 /* Create a socket. */
2392 sock = connect_local_xsocket(display_number);
2393 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10002394 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002395
2396 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10002397 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002398 }
Damien Miller5428f641999-11-25 11:54:57 +11002399 /*
2400 * Connect to an inet socket. The DISPLAY value is supposedly
2401 * hostname:d[.s], where hostname may also be numeric IP address.
2402 */
Damien Miller95def091999-11-25 00:26:21 +11002403 strncpy(buf, display, sizeof(buf));
2404 buf[sizeof(buf) - 1] = 0;
2405 cp = strchr(buf, ':');
2406 if (!cp) {
2407 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10002408 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002409 }
Damien Miller95def091999-11-25 00:26:21 +11002410 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11002411 /* buf now contains the host name. But first we parse the display number. */
Damien Miller95def091999-11-25 00:26:21 +11002412 if (sscanf(cp + 1, "%d", &display_number) != 1) {
2413 error("Could not parse display number from DISPLAY: %.100s",
2414 display);
Damien Millerbd483e72000-04-30 10:00:53 +10002415 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002416 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002417
Damien Miller34132e52000-01-14 15:45:46 +11002418 /* Look up the host address */
2419 memset(&hints, 0, sizeof(hints));
2420 hints.ai_family = IPv4or6;
2421 hints.ai_socktype = SOCK_STREAM;
2422 snprintf(strport, sizeof strport, "%d", 6000 + display_number);
2423 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
2424 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10002425 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002426 }
Damien Miller34132e52000-01-14 15:45:46 +11002427 for (ai = aitop; ai; ai = ai->ai_next) {
2428 /* Create a socket. */
2429 sock = socket(ai->ai_family, SOCK_STREAM, 0);
2430 if (sock < 0) {
2431 debug("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10002432 continue;
2433 }
2434 /* Connect it to the display. */
2435 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2436 debug("connect %.100s port %d: %.100s", buf,
2437 6000 + display_number, strerror(errno));
2438 close(sock);
2439 continue;
2440 }
2441 /* Success */
2442 break;
Damien Miller34132e52000-01-14 15:45:46 +11002443 }
Damien Miller34132e52000-01-14 15:45:46 +11002444 freeaddrinfo(aitop);
2445 if (!ai) {
Damien Miller4af51302000-04-16 11:18:38 +10002446 error("connect %.100s port %d: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11002447 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10002448 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002449 }
Damien Millerbd483e72000-04-30 10:00:53 +10002450 return sock;
2451}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002452
Damien Millerbd483e72000-04-30 10:00:53 +10002453/*
2454 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
2455 * the remote channel number. We should do whatever we want, and respond
2456 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
2457 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002458
Damien Millerbd483e72000-04-30 10:00:53 +10002459void
Damien Miller62cee002000-09-23 17:15:56 +11002460x11_input_open(int type, int plen, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10002461{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002462 Channel *c = NULL;
2463 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10002464 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10002465
2466 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002467
2468 remote_id = packet_get_int();
2469 if (have_hostname_in_open) {
2470 remote_host = packet_get_string(NULL);
2471 } else {
2472 remote_host = xstrdup("unknown (remote did not supply name)");
2473 }
2474 packet_done();
Damien Millerbd483e72000-04-30 10:00:53 +10002475
2476 /* Obtain a connection to the real X display. */
2477 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002478 if (sock != -1) {
2479 /* Allocate a channel for this connection. */
2480 c = channel_new("connected x11 socket",
2481 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
2482 remote_host, 1);
2483 if (c == NULL) {
2484 error("x11_input_open: channel_new failed");
2485 close(sock);
2486 } else {
2487 c->remote_id = remote_id;
2488 }
2489 }
2490 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10002491 /* Send refusal to the remote host. */
2492 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002493 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10002494 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10002495 /* Send a confirmation to the remote host. */
2496 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002497 packet_put_int(remote_id);
2498 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10002499 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002500 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002501}
2502
Damien Miller69b69aa2000-10-28 14:19:58 +11002503/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
2504void
2505deny_input_open(int type, int plen, void *ctxt)
2506{
2507 int rchan = packet_get_int();
2508 switch(type){
2509 case SSH_SMSG_AGENT_OPEN:
2510 error("Warning: ssh server tried agent forwarding.");
2511 break;
2512 case SSH_SMSG_X11_OPEN:
2513 error("Warning: ssh server tried X11 forwarding.");
2514 break;
2515 default:
2516 error("deny_input_open: type %d plen %d", type, plen);
2517 break;
2518 }
2519 error("Warning: this is probably a break in attempt by a malicious server.");
2520 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2521 packet_put_int(rchan);
2522 packet_send();
2523}
2524
Damien Miller5428f641999-11-25 11:54:57 +11002525/*
2526 * Requests forwarding of X11 connections, generates fake authentication
2527 * data, and enables authentication spoofing.
2528 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002529
Damien Miller4af51302000-04-16 11:18:38 +10002530void
Damien Millerbd483e72000-04-30 10:00:53 +10002531x11_request_forwarding_with_spoofing(int client_session_id,
2532 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002533{
Ben Lindstrom46c16222000-12-22 01:43:59 +00002534 u_int data_len = (u_int) strlen(data) / 2;
Ben Lindstromb3211a82001-02-10 22:33:19 +00002535 u_int i, value, len;
Damien Miller95def091999-11-25 00:26:21 +11002536 char *new_data;
2537 int screen_number;
2538 const char *cp;
2539 u_int32_t rand = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002540
Damien Miller95def091999-11-25 00:26:21 +11002541 cp = getenv("DISPLAY");
2542 if (cp)
2543 cp = strchr(cp, ':');
2544 if (cp)
2545 cp = strchr(cp, '.');
2546 if (cp)
2547 screen_number = atoi(cp + 1);
2548 else
2549 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002550
Damien Miller95def091999-11-25 00:26:21 +11002551 /* Save protocol name. */
2552 x11_saved_proto = xstrdup(proto);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002553
Damien Miller5428f641999-11-25 11:54:57 +11002554 /*
2555 * Extract real authentication data and generate fake data of the
2556 * same length.
2557 */
Damien Miller95def091999-11-25 00:26:21 +11002558 x11_saved_data = xmalloc(data_len);
2559 x11_fake_data = xmalloc(data_len);
2560 for (i = 0; i < data_len; i++) {
2561 if (sscanf(data + 2 * i, "%2x", &value) != 1)
2562 fatal("x11_request_forwarding: bad authentication data: %.100s", data);
2563 if (i % 4 == 0)
2564 rand = arc4random();
2565 x11_saved_data[i] = value;
2566 x11_fake_data[i] = rand & 0xff;
2567 rand >>= 8;
2568 }
2569 x11_saved_data_len = data_len;
2570 x11_fake_data_len = data_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002571
Damien Miller95def091999-11-25 00:26:21 +11002572 /* Convert the fake data into hex. */
Ben Lindstromb3211a82001-02-10 22:33:19 +00002573 len = 2 * data_len + 1;
2574 new_data = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +11002575 for (i = 0; i < data_len; i++)
Ben Lindstromb3211a82001-02-10 22:33:19 +00002576 snprintf(new_data + 2 * i, len - 2 * i,
2577 "%02x", (u_char) x11_fake_data[i]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002578
Damien Miller95def091999-11-25 00:26:21 +11002579 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10002580 if (compat20) {
2581 channel_request_start(client_session_id, "x11-req", 0);
2582 packet_put_char(0); /* XXX bool single connection */
2583 } else {
2584 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
2585 }
2586 packet_put_cstring(proto);
2587 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11002588 packet_put_int(screen_number);
2589 packet_send();
2590 packet_write_wait();
2591 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002592}
2593
2594/* Sends a message to the server to request authentication fd forwarding. */
2595
Damien Miller4af51302000-04-16 11:18:38 +10002596void
Damien Miller95def091999-11-25 00:26:21 +11002597auth_request_forwarding()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002598{
Damien Miller95def091999-11-25 00:26:21 +11002599 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
2600 packet_send();
2601 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002602}
2603
Damien Miller5428f641999-11-25 11:54:57 +11002604/*
2605 * Returns the name of the forwarded authentication socket. Returns NULL if
2606 * there is no forwarded authentication socket. The returned value points to
2607 * a static buffer.
2608 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002609
Damien Miller95def091999-11-25 00:26:21 +11002610char *
2611auth_get_socket_name()
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002612{
Damien Miller95def091999-11-25 00:26:21 +11002613 return channel_forwarded_auth_socket_name;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002614}
2615
2616/* removes the agent forwarding socket */
2617
Damien Miller4af51302000-04-16 11:18:38 +10002618void
Damien Miller95def091999-11-25 00:26:21 +11002619cleanup_socket(void)
2620{
Damien Miller78315eb2000-09-29 23:01:36 +11002621 unlink(channel_forwarded_auth_socket_name);
Damien Miller95def091999-11-25 00:26:21 +11002622 rmdir(channel_forwarded_auth_socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002623}
2624
Damien Miller5428f641999-11-25 11:54:57 +11002625/*
Damien Millerd3a18572000-06-07 19:55:44 +10002626 * This is called to process SSH_CMSG_AGENT_REQUEST_FORWARDING on the server.
Damien Miller5428f641999-11-25 11:54:57 +11002627 * This starts forwarding authentication requests.
2628 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002629
Damien Millerd3a18572000-06-07 19:55:44 +10002630int
Damien Miller95def091999-11-25 00:26:21 +11002631auth_input_request_forwarding(struct passwd * pw)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002632{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002633 Channel *nc;
2634 int sock;
Damien Miller95def091999-11-25 00:26:21 +11002635 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002636
Damien Miller95def091999-11-25 00:26:21 +11002637 if (auth_get_socket_name() != NULL)
2638 fatal("Protocol error: authentication forwarding requested twice.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002639
Damien Miller95def091999-11-25 00:26:21 +11002640 /* Temporarily drop privileged uid for mkdir/bind. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +00002641 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002642
Damien Miller95def091999-11-25 00:26:21 +11002643 /* Allocate a buffer for the socket name, and format the name. */
2644 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
2645 channel_forwarded_auth_socket_dir = xmalloc(MAX_SOCKET_NAME);
2646 strlcpy(channel_forwarded_auth_socket_dir, "/tmp/ssh-XXXXXXXX", MAX_SOCKET_NAME);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002647
Damien Miller95def091999-11-25 00:26:21 +11002648 /* Create private directory for socket */
Damien Millerd3a18572000-06-07 19:55:44 +10002649 if (mkdtemp(channel_forwarded_auth_socket_dir) == NULL) {
2650 packet_send_debug("Agent forwarding disabled: mkdtemp() failed: %.100s",
2651 strerror(errno));
2652 restore_uid();
2653 xfree(channel_forwarded_auth_socket_name);
2654 xfree(channel_forwarded_auth_socket_dir);
2655 channel_forwarded_auth_socket_name = NULL;
2656 channel_forwarded_auth_socket_dir = NULL;
2657 return 0;
2658 }
Damien Miller95def091999-11-25 00:26:21 +11002659 snprintf(channel_forwarded_auth_socket_name, MAX_SOCKET_NAME, "%s/agent.%d",
2660 channel_forwarded_auth_socket_dir, (int) getpid());
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002661
Damien Miller95def091999-11-25 00:26:21 +11002662 if (atexit(cleanup_socket) < 0) {
2663 int saved = errno;
2664 cleanup_socket();
2665 packet_disconnect("socket: %.100s", strerror(saved));
2666 }
2667 /* Create the socket. */
2668 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2669 if (sock < 0)
2670 packet_disconnect("socket: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002671
Damien Miller95def091999-11-25 00:26:21 +11002672 /* Bind it to the name. */
2673 memset(&sunaddr, 0, sizeof(sunaddr));
2674 sunaddr.sun_family = AF_UNIX;
2675 strncpy(sunaddr.sun_path, channel_forwarded_auth_socket_name,
2676 sizeof(sunaddr.sun_path));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002677
Damien Miller95def091999-11-25 00:26:21 +11002678 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0)
2679 packet_disconnect("bind: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002680
Damien Miller95def091999-11-25 00:26:21 +11002681 /* Restore the privileged uid. */
2682 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002683
Damien Miller95def091999-11-25 00:26:21 +11002684 /* Start listening on the socket. */
2685 if (listen(sock, 5) < 0)
2686 packet_disconnect("listen: %.100s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002687
Damien Miller95def091999-11-25 00:26:21 +11002688 /* Allocate a channel for the authentication agent socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002689 nc = channel_new("auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11002690 SSH_CHANNEL_AUTH_SOCKET, sock, sock, -1,
2691 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
2692 0, xstrdup("auth socket"), 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002693 if (nc == NULL) {
2694 error("auth_input_request_forwarding: channel_new failed");
2695 close(sock);
2696 return 0;
2697 }
2698 strlcpy(nc->path, channel_forwarded_auth_socket_name, sizeof(nc->path));
Damien Millerd3a18572000-06-07 19:55:44 +10002699 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002700}
2701
2702/* This is called to process an SSH_SMSG_AGENT_OPEN message. */
2703
Damien Miller4af51302000-04-16 11:18:38 +10002704void
Damien Miller62cee002000-09-23 17:15:56 +11002705auth_input_open_request(int type, int plen, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002706{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002707 Channel *c = NULL;
2708 int remote_id, sock;
Damien Miller95def091999-11-25 00:26:21 +11002709 char *dummyname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002710
Damien Millerb38eff82000-04-01 11:09:21 +10002711 packet_integrity_check(plen, 4, type);
2712
Damien Miller95def091999-11-25 00:26:21 +11002713 /* Read the remote channel number from the message. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002714 remote_id = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002715
Damien Miller5428f641999-11-25 11:54:57 +11002716 /*
2717 * Get a connection to the local authentication agent (this may again
2718 * get forwarded).
2719 */
Damien Miller95def091999-11-25 00:26:21 +11002720 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002721
Damien Miller5428f641999-11-25 11:54:57 +11002722 /*
2723 * If we could not connect the agent, send an error message back to
2724 * the server. This should never happen unless the agent dies,
2725 * because authentication forwarding is only enabled if we have an
2726 * agent.
2727 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002728 if (sock >= 0) {
2729 dummyname = xstrdup("authentication agent connection");
2730 c = channel_new("", SSH_CHANNEL_OPEN, sock, sock, -1, 0, 0, 0, dummyname, 1);
2731 if (c == NULL) {
2732 error("auth_input_open_request: channel_new failed");
2733 close(sock);
2734 } else {
2735 c->remote_id = remote_id;
2736 }
Damien Miller95def091999-11-25 00:26:21 +11002737 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002738 if (c == NULL) {
2739 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2740 packet_put_int(remote_id);
2741 } else {
2742 /* Send a confirmation to the remote host. */
2743 debug("Forwarding authentication connection.");
2744 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
2745 packet_put_int(remote_id);
2746 packet_put_int(c->self);
2747 }
Damien Miller95def091999-11-25 00:26:21 +11002748 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002749}
Damien Miller33b13562000-04-04 14:38:59 +10002750
2751void
Damien Millerbd483e72000-04-30 10:00:53 +10002752channel_start_open(int id)
Damien Miller33b13562000-04-04 14:38:59 +10002753{
2754 Channel *c = channel_lookup(id);
2755 if (c == NULL) {
2756 log("channel_open: %d: bad id", id);
2757 return;
2758 }
Damien Millerbd483e72000-04-30 10:00:53 +10002759 debug("send channel open %d", id);
Damien Miller33b13562000-04-04 14:38:59 +10002760 packet_start(SSH2_MSG_CHANNEL_OPEN);
2761 packet_put_cstring(c->ctype);
2762 packet_put_int(c->self);
2763 packet_put_int(c->local_window);
2764 packet_put_int(c->local_maxpacket);
Damien Millerbd483e72000-04-30 10:00:53 +10002765}
2766void
2767channel_open(int id)
2768{
2769 /* XXX REMOVE ME */
2770 channel_start_open(id);
Damien Miller33b13562000-04-04 14:38:59 +10002771 packet_send();
Damien Miller33b13562000-04-04 14:38:59 +10002772}
2773void
2774channel_request(int id, char *service, int wantconfirm)
2775{
2776 channel_request_start(id, service, wantconfirm);
2777 packet_send();
2778 debug("channel request %d: %s", id, service) ;
2779}
2780void
2781channel_request_start(int id, char *service, int wantconfirm)
2782{
2783 Channel *c = channel_lookup(id);
2784 if (c == NULL) {
2785 log("channel_request: %d: bad id", id);
2786 return;
2787 }
2788 packet_start(SSH2_MSG_CHANNEL_REQUEST);
2789 packet_put_int(c->remote_id);
2790 packet_put_cstring(service);
2791 packet_put_char(wantconfirm);
2792}
2793void
2794channel_register_callback(int id, int mtype, channel_callback_fn *fn, void *arg)
2795{
2796 Channel *c = channel_lookup(id);
2797 if (c == NULL) {
2798 log("channel_register_callback: %d: bad id", id);
2799 return;
2800 }
2801 c->cb_event = mtype;
2802 c->cb_fn = fn;
2803 c->cb_arg = arg;
2804}
2805void
2806channel_register_cleanup(int id, channel_callback_fn *fn)
2807{
2808 Channel *c = channel_lookup(id);
2809 if (c == NULL) {
2810 log("channel_register_cleanup: %d: bad id", id);
2811 return;
2812 }
2813 c->dettach_user = fn;
2814}
2815void
2816channel_cancel_cleanup(int id)
2817{
2818 Channel *c = channel_lookup(id);
2819 if (c == NULL) {
2820 log("channel_cancel_cleanup: %d: bad id", id);
2821 return;
2822 }
2823 c->dettach_user = NULL;
2824}
Kevin Stevesef4eea92001-02-05 12:42:17 +00002825void
Damien Millerad833b32000-08-23 10:46:23 +10002826channel_register_filter(int id, channel_filter_fn *fn)
2827{
2828 Channel *c = channel_lookup(id);
2829 if (c == NULL) {
2830 log("channel_register_filter: %d: bad id", id);
2831 return;
2832 }
2833 c->input_filter = fn;
2834}
Damien Miller33b13562000-04-04 14:38:59 +10002835
2836void
Damien Miller69b69aa2000-10-28 14:19:58 +11002837channel_set_fds(int id, int rfd, int wfd, int efd,
2838 int extusage, int nonblock)
Damien Miller33b13562000-04-04 14:38:59 +10002839{
2840 Channel *c = channel_lookup(id);
2841 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
2842 fatal("channel_activate for non-larval channel %d.", id);
Damien Miller69b69aa2000-10-28 14:19:58 +11002843 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller33b13562000-04-04 14:38:59 +10002844 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002845 /* XXX window size? */
Damien Millere4340be2000-09-16 13:29:08 +11002846 c->local_window = c->local_window_max = c->local_maxpacket * 2;
Damien Miller33b13562000-04-04 14:38:59 +10002847 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
2848 packet_put_int(c->remote_id);
2849 packet_put_int(c->local_window);
2850 packet_send();
2851}