blob: f59afe01b54a0453f6bc072f9dc026680a298a53 [file] [log] [blame]
Damien Miller9aec9192006-08-05 10:57:45 +10001/* $OpenBSD: channels.c,v 1.261 2006/07/25 02:59:21 stevesk Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * This file contains functions for generic socket connection forwarding.
7 * There is also code for initiating connection forwarding for X11 connections,
8 * arbitrary tcp/ip connections, and the authentication agent connection.
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Millere4340be2000-09-16 13:29:08 +110010 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose. Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 *
Damien Miller33b13562000-04-04 14:38:59 +100016 * SSH2 support added by Markus Friedl.
Damien Millera90fc082002-01-22 23:19:38 +110017 * Copyright (c) 1999, 2000, 2001, 2002 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110018 * 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"
Damien Miller17e91c02006-03-15 11:28:34 +110043
44#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110045#include <sys/types.h>
46#include <sys/un.h>
Damien Millerefc04e72006-07-10 20:26:27 +100047#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100048#ifdef HAVE_SYS_TIME_H
49# include <sys/time.h>
50#endif
Damien Millerefc04e72006-07-10 20:26:27 +100051
52#include <netinet/in.h>
53#include <arpa/inet.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110054
Darren Tucker39972492006-07-12 22:22:46 +100055#include <errno.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100056#include <netdb.h>
Damien Millere3476ed2006-07-24 14:13:33 +100057#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110058#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100059#include <unistd.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
61#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000062#include "ssh1.h"
63#include "ssh2.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064#include "packet.h"
65#include "xmalloc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066#include "log.h"
67#include "misc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000070#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100071#include "key.h"
72#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110073#include "pathnames.h"
Darren Tucker46471c92003-07-03 13:55:19 +100074#include "bufaux.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Ben Lindstrome9c99912001-06-09 00:41:05 +000076/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077
Damien Miller5428f641999-11-25 11:54:57 +110078/*
79 * Pointer to an array containing all allocated channels. The array is
80 * dynamically extended as needed.
81 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000082static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083
Damien Miller5428f641999-11-25 11:54:57 +110084/*
85 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000086 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110087 */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +100088static u_int channels_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Damien Miller5428f641999-11-25 11:54:57 +110090/*
91 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +000092 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +110093 */
Damien Miller5e953212001-01-30 09:14:00 +110094static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
Ben Lindstrome9c99912001-06-09 00:41:05 +000097/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Damien Miller5428f641999-11-25 11:54:57 +110099/*
100 * Data structure for storing which hosts are permitted for forward requests.
101 * The local sides of any remote forwards are stored in this array to prevent
102 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
103 * network (which might be behind a firewall).
104 */
Damien Miller95def091999-11-25 00:26:21 +1100105typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000106 char *host_to_connect; /* Connect to 'host'. */
107 u_short port_to_connect; /* Connect to 'port'. */
108 u_short listen_port; /* Remote side should listen port number. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109} ForwardPermission;
110
Damien Miller9b439df2006-07-24 14:04:00 +1000111/* List of all permitted host/port pairs to connect by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112static ForwardPermission permitted_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
Ben Lindstrome9c99912001-06-09 00:41:05 +0000113
Damien Miller9b439df2006-07-24 14:04:00 +1000114/* List of all permitted host/port pairs to connect by the admin. */
115static ForwardPermission permitted_adm_opens[SSH_MAX_FORWARDS_PER_DIRECTION];
116
117/* Number of permitted host/port pairs in the array permitted by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118static int num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +1000119
120/* Number of permitted host/port pair in the array permitted by the admin. */
121static int num_adm_permitted_opens = 0;
122
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
Ben Lindstrome9c99912001-06-09 00:41:05 +0000130
131/* -- X11 forwarding */
132
133/* Maximum number of fake X11 displays to try. */
134#define MAX_DISPLAYS 1000
135
Damien Miller13390022005-07-06 09:44:19 +1000136/* Saved X11 local (client) display. */
137static char *x11_saved_display = NULL;
138
Ben Lindstrome9c99912001-06-09 00:41:05 +0000139/* Saved X11 authentication protocol name. */
140static char *x11_saved_proto = NULL;
141
142/* Saved X11 authentication data. This is the real data. */
143static char *x11_saved_data = NULL;
144static u_int x11_saved_data_len = 0;
145
146/*
147 * Fake X11 authentication data. This is what the server will be sending us;
148 * we should replace any occurrences of this by the real data.
149 */
Damien Miller4ae97f12006-03-26 14:08:10 +1100150static u_char *x11_fake_data = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000151static u_int x11_fake_data_len;
152
153
154/* -- agent forwarding */
155
156#define NUM_SOCKS 10
157
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000158/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000159static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000160
Ben Lindstrome9c99912001-06-09 00:41:05 +0000161/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000162static void port_open_helper(Channel *c, char *rtype);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000163
Ben Lindstrome9c99912001-06-09 00:41:05 +0000164/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000165
166Channel *
Damien Millerd47c62a2005-12-13 19:33:57 +1100167channel_by_id(int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000168{
169 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000170
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000171 if (id < 0 || (u_int)id >= channels_alloc) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100172 logit("channel_by_id: %d: bad id", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000173 return NULL;
174 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000175 c = channels[id];
176 if (c == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100177 logit("channel_by_id: %d: bad id: channel free", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000178 return NULL;
179 }
180 return c;
181}
182
Damien Miller5428f641999-11-25 11:54:57 +1100183/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100184 * Returns the channel if it is allowed to receive protocol messages.
185 * Private channels, like listening sockets, may not receive messages.
186 */
187Channel *
188channel_lookup(int id)
189{
190 Channel *c;
191
192 if ((c = channel_by_id(id)) == NULL)
193 return (NULL);
194
Damien Millerd62f2ca2006-03-26 13:57:41 +1100195 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100196 case SSH_CHANNEL_X11_OPEN:
197 case SSH_CHANNEL_LARVAL:
198 case SSH_CHANNEL_CONNECTING:
199 case SSH_CHANNEL_DYNAMIC:
200 case SSH_CHANNEL_OPENING:
201 case SSH_CHANNEL_OPEN:
202 case SSH_CHANNEL_INPUT_DRAINING:
203 case SSH_CHANNEL_OUTPUT_DRAINING:
204 return (c);
Damien Millerd47c62a2005-12-13 19:33:57 +1100205 }
206 logit("Non-public channel %d, type %d.", id, c->type);
207 return (NULL);
208}
209
210/*
Damien Millere247cc42000-05-07 12:03:14 +1000211 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000212 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100213 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000214static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100215channel_register_fds(Channel *c, int rfd, int wfd, int efd,
216 int extusage, int nonblock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000217{
Damien Miller95def091999-11-25 00:26:21 +1100218 /* Update the maximum file descriptor value. */
Damien Miller5e953212001-01-30 09:14:00 +1100219 channel_max_fd = MAX(channel_max_fd, rfd);
220 channel_max_fd = MAX(channel_max_fd, wfd);
221 channel_max_fd = MAX(channel_max_fd, efd);
222
Damien Miller5428f641999-11-25 11:54:57 +1100223 /* XXX set close-on-exec -markus */
Damien Millere247cc42000-05-07 12:03:14 +1000224
Damien Miller6f83b8e2000-05-02 09:23:45 +1000225 c->rfd = rfd;
226 c->wfd = wfd;
227 c->sock = (rfd == wfd) ? rfd : -1;
Damien Miller0e220db2004-06-15 10:34:08 +1000228 c->ctl_fd = -1; /* XXX: set elsewhere */
Damien Miller6f83b8e2000-05-02 09:23:45 +1000229 c->efd = efd;
230 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100231
Damien Miller79438cc2001-02-16 12:34:57 +1100232 /* XXX ugly hack: nonblock is only set by the server */
233 if (nonblock && isatty(c->rfd)) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000234 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100235 c->isatty = 1;
236 if (!isatty(c->wfd)) {
Ben Lindstromcc74df72001-03-05 06:20:14 +0000237 error("channel %d: wfd %d is not a tty?",
Damien Miller79438cc2001-02-16 12:34:57 +1100238 c->self, c->wfd);
239 }
240 } else {
241 c->isatty = 0;
242 }
Ben Lindstrombeb5f332002-07-22 15:28:53 +0000243 c->wfd_isatty = isatty(c->wfd);
Damien Miller79438cc2001-02-16 12:34:57 +1100244
Damien Miller69b69aa2000-10-28 14:19:58 +1100245 /* enable nonblocking mode */
246 if (nonblock) {
247 if (rfd != -1)
248 set_nonblock(rfd);
249 if (wfd != -1)
250 set_nonblock(wfd);
251 if (efd != -1)
252 set_nonblock(efd);
253 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000254}
255
256/*
257 * Allocate a new channel object and set its type and socket. This will cause
258 * remote_name to be freed.
259 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000260Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000261channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000262 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000263{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000264 int found;
265 u_int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000266 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267
Damien Miller95def091999-11-25 00:26:21 +1100268 /* Do initial allocation if this is the first call. */
269 if (channels_alloc == 0) {
270 channels_alloc = 10;
Damien Miller07d86be2006-03-26 14:19:21 +1100271 channels = xcalloc(channels_alloc, sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100272 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000273 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100274 }
275 /* Try to find a free slot where to put the new channel. */
276 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000277 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100278 /* Found a free slot. */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000279 found = (int)i;
Damien Miller95def091999-11-25 00:26:21 +1100280 break;
281 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000282 if (found < 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100283 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100284 found = channels_alloc;
Damien Miller9403aa22002-06-26 19:14:43 +1000285 if (channels_alloc > 10000)
286 fatal("channel_new: internal error: channels_alloc %d "
287 "too big.", channels_alloc);
Damien Miller36812092006-03-26 14:22:47 +1100288 channels = xrealloc(channels, channels_alloc + 10,
289 sizeof(Channel *));
Damien Miller5efcecc2003-09-17 07:31:14 +1000290 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100291 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100292 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000293 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100294 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000295 /* Initialize and return new channel. */
Damien Miller07d86be2006-03-26 14:19:21 +1100296 c = channels[found] = xcalloc(1, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100297 buffer_init(&c->input);
298 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000299 buffer_init(&c->extended);
Damien Miller5144df92002-01-22 23:28:45 +1100300 c->ostate = CHAN_OUTPUT_OPEN;
301 c->istate = CHAN_INPUT_OPEN;
302 c->flags = 0;
Damien Miller69b69aa2000-10-28 14:19:58 +1100303 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
Damien Miller95def091999-11-25 00:26:21 +1100304 c->self = found;
305 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000306 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000307 c->local_window = window;
308 c->local_window_max = window;
309 c->local_consumed = 0;
310 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100311 c->remote_id = -1;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000312 c->remote_name = xstrdup(remote_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000313 c->remote_window = 0;
314 c->remote_maxpacket = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000315 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100316 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000317 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100318 c->detach_close = 0;
Damien Miller67f0bc02002-02-05 12:23:08 +1100319 c->confirm = NULL;
Damien Miller0e220db2004-06-15 10:34:08 +1000320 c->confirm_ctx = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000321 c->input_filter = NULL;
Damien Miller077b2382005-12-31 16:22:32 +1100322 c->output_filter = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100323 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000324 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000325}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000326
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000327static int
328channel_find_maxfd(void)
329{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000330 u_int i;
331 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000332 Channel *c;
333
334 for (i = 0; i < channels_alloc; i++) {
335 c = channels[i];
336 if (c != NULL) {
337 max = MAX(max, c->rfd);
338 max = MAX(max, c->wfd);
339 max = MAX(max, c->efd);
340 }
341 }
342 return max;
343}
344
345int
346channel_close_fd(int *fdp)
347{
348 int ret = 0, fd = *fdp;
349
350 if (fd != -1) {
351 ret = close(fd);
352 *fdp = -1;
353 if (fd == channel_max_fd)
354 channel_max_fd = channel_find_maxfd();
355 }
356 return ret;
357}
358
Damien Miller6f83b8e2000-05-02 09:23:45 +1000359/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000360static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000361channel_close_fds(Channel *c)
362{
Damien Miller0e220db2004-06-15 10:34:08 +1000363 debug3("channel %d: close_fds r %d w %d e %d c %d",
364 c->self, c->rfd, c->wfd, c->efd, c->ctl_fd);
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000365
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000366 channel_close_fd(&c->sock);
Damien Miller0e220db2004-06-15 10:34:08 +1000367 channel_close_fd(&c->ctl_fd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000368 channel_close_fd(&c->rfd);
369 channel_close_fd(&c->wfd);
370 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000371}
372
373/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000374void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000375channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000376{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000377 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000378 u_int i, n;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000379
380 for (n = 0, i = 0; i < channels_alloc; i++)
381 if (channels[i])
382 n++;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000383 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000384 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000385
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000386 s = channel_open_message();
Damien Millerfbdeece2003-09-02 22:52:31 +1000387 debug3("channel %d: status: %s", c->self, s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000388 xfree(s);
389
Damien Miller6f83b8e2000-05-02 09:23:45 +1000390 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000391 shutdown(c->sock, SHUT_RDWR);
Damien Miller0e220db2004-06-15 10:34:08 +1000392 if (c->ctl_fd != -1)
393 shutdown(c->ctl_fd, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000394 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000395 buffer_free(&c->input);
396 buffer_free(&c->output);
397 buffer_free(&c->extended);
Damien Millerb38eff82000-04-01 11:09:21 +1000398 if (c->remote_name) {
399 xfree(c->remote_name);
400 c->remote_name = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100401 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000402 channels[c->self] = NULL;
403 xfree(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000404}
405
Ben Lindstrome9c99912001-06-09 00:41:05 +0000406void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000407channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000408{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000409 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000410
Ben Lindstrom601e4362001-06-21 03:19:23 +0000411 for (i = 0; i < channels_alloc; i++)
412 if (channels[i] != NULL)
413 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000414}
415
416/*
417 * Closes the sockets/fds of all channels. This is used to close extra file
418 * descriptors after a fork.
419 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000420void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000421channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000422{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000423 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000424
425 for (i = 0; i < channels_alloc; i++)
426 if (channels[i] != NULL)
427 channel_close_fds(channels[i]);
428}
429
430/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000431 * Stop listening to channels.
432 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000433void
434channel_stop_listening(void)
435{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000436 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000437 Channel *c;
438
439 for (i = 0; i < channels_alloc; i++) {
440 c = channels[i];
441 if (c != NULL) {
442 switch (c->type) {
443 case SSH_CHANNEL_AUTH_SOCKET:
444 case SSH_CHANNEL_PORT_LISTENER:
445 case SSH_CHANNEL_RPORT_LISTENER:
446 case SSH_CHANNEL_X11_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000447 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000448 channel_free(c);
449 break;
450 }
451 }
452 }
453}
454
455/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000456 * Returns true if no channel has too much buffered data, and false if one or
457 * more channel is overfull.
458 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000459int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000460channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000461{
462 u_int i;
463 Channel *c;
464
465 for (i = 0; i < channels_alloc; i++) {
466 c = channels[i];
467 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000468#if 0
469 if (!compat20 &&
470 buffer_len(&c->input) > packet_get_maxsize()) {
Damien Miller275295e2003-01-08 14:04:09 +1100471 debug2("channel %d: big input buffer %d",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000472 c->self, buffer_len(&c->input));
473 return 0;
474 }
Damien Milleraf5f2e62001-10-10 15:01:16 +1000475#endif
Ben Lindstrome9c99912001-06-09 00:41:05 +0000476 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000477 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000478 c->self, buffer_len(&c->output),
479 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000480 return 0;
481 }
482 }
483 }
484 return 1;
485}
486
487/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000488int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000489channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000490{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000491 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000492 Channel *c;
493
494 for (i = 0; i < channels_alloc; i++) {
495 c = channels[i];
496 if (c == NULL)
497 continue;
498 switch (c->type) {
499 case SSH_CHANNEL_X11_LISTENER:
500 case SSH_CHANNEL_PORT_LISTENER:
501 case SSH_CHANNEL_RPORT_LISTENER:
502 case SSH_CHANNEL_CLOSED:
503 case SSH_CHANNEL_AUTH_SOCKET:
504 case SSH_CHANNEL_DYNAMIC:
505 case SSH_CHANNEL_CONNECTING:
506 case SSH_CHANNEL_ZOMBIE:
507 continue;
508 case SSH_CHANNEL_LARVAL:
509 if (!compat20)
510 fatal("cannot happen: SSH_CHANNEL_LARVAL");
511 continue;
512 case SSH_CHANNEL_OPENING:
513 case SSH_CHANNEL_OPEN:
514 case SSH_CHANNEL_X11_OPEN:
515 return 1;
516 case SSH_CHANNEL_INPUT_DRAINING:
517 case SSH_CHANNEL_OUTPUT_DRAINING:
518 if (!compat13)
519 fatal("cannot happen: OUT_DRAIN");
520 return 1;
521 default:
522 fatal("channel_still_open: bad channel type %d", c->type);
523 /* NOTREACHED */
524 }
525 }
526 return 0;
527}
528
529/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000530int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000531channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000532{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000533 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000534 Channel *c;
535
536 for (i = 0; i < channels_alloc; i++) {
537 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000538 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000539 continue;
540 switch (c->type) {
541 case SSH_CHANNEL_CLOSED:
542 case SSH_CHANNEL_DYNAMIC:
543 case SSH_CHANNEL_X11_LISTENER:
544 case SSH_CHANNEL_PORT_LISTENER:
545 case SSH_CHANNEL_RPORT_LISTENER:
546 case SSH_CHANNEL_OPENING:
547 case SSH_CHANNEL_CONNECTING:
548 case SSH_CHANNEL_ZOMBIE:
549 continue;
550 case SSH_CHANNEL_LARVAL:
551 case SSH_CHANNEL_AUTH_SOCKET:
552 case SSH_CHANNEL_OPEN:
553 case SSH_CHANNEL_X11_OPEN:
554 return i;
555 case SSH_CHANNEL_INPUT_DRAINING:
556 case SSH_CHANNEL_OUTPUT_DRAINING:
557 if (!compat13)
558 fatal("cannot happen: OUT_DRAIN");
559 return i;
560 default:
561 fatal("channel_find_open: bad channel type %d", c->type);
562 /* NOTREACHED */
563 }
564 }
565 return -1;
566}
567
568
569/*
570 * Returns a message describing the currently open forwarded connections,
571 * suitable for sending to the client. The message contains crlf pairs for
572 * newlines.
573 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000574char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000575channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000576{
577 Buffer buffer;
578 Channel *c;
579 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000580 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000581
582 buffer_init(&buffer);
583 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
584 buffer_append(&buffer, buf, strlen(buf));
585 for (i = 0; i < channels_alloc; i++) {
586 c = channels[i];
587 if (c == NULL)
588 continue;
589 switch (c->type) {
590 case SSH_CHANNEL_X11_LISTENER:
591 case SSH_CHANNEL_PORT_LISTENER:
592 case SSH_CHANNEL_RPORT_LISTENER:
593 case SSH_CHANNEL_CLOSED:
594 case SSH_CHANNEL_AUTH_SOCKET:
595 case SSH_CHANNEL_ZOMBIE:
596 continue;
597 case SSH_CHANNEL_LARVAL:
598 case SSH_CHANNEL_OPENING:
599 case SSH_CHANNEL_CONNECTING:
600 case SSH_CHANNEL_DYNAMIC:
601 case SSH_CHANNEL_OPEN:
602 case SSH_CHANNEL_X11_OPEN:
603 case SSH_CHANNEL_INPUT_DRAINING:
604 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Miller0e220db2004-06-15 10:34:08 +1000605 snprintf(buf, sizeof buf,
606 " #%d %.300s (t%d r%d i%d/%d o%d/%d fd %d/%d cfd %d)\r\n",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000607 c->self, c->remote_name,
608 c->type, c->remote_id,
609 c->istate, buffer_len(&c->input),
610 c->ostate, buffer_len(&c->output),
Damien Miller0e220db2004-06-15 10:34:08 +1000611 c->rfd, c->wfd, c->ctl_fd);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000612 buffer_append(&buffer, buf, strlen(buf));
613 continue;
614 default:
615 fatal("channel_open_message: bad channel type %d", c->type);
616 /* NOTREACHED */
617 }
618 }
619 buffer_append(&buffer, "\0", 1);
620 cp = xstrdup(buffer_ptr(&buffer));
621 buffer_free(&buffer);
622 return cp;
623}
624
625void
626channel_send_open(int id)
627{
628 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000629
Ben Lindstrome9c99912001-06-09 00:41:05 +0000630 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000631 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000632 return;
633 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000634 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000635 packet_start(SSH2_MSG_CHANNEL_OPEN);
636 packet_put_cstring(c->ctype);
637 packet_put_int(c->self);
638 packet_put_int(c->local_window);
639 packet_put_int(c->local_maxpacket);
640 packet_send();
641}
642
643void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000644channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000645{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000646 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000647
Ben Lindstrome9c99912001-06-09 00:41:05 +0000648 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000649 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000650 return;
651 }
Damien Miller0e220db2004-06-15 10:34:08 +1000652 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000653 packet_start(SSH2_MSG_CHANNEL_REQUEST);
654 packet_put_int(c->remote_id);
655 packet_put_cstring(service);
656 packet_put_char(wantconfirm);
657}
Damien Miller4f7becb2006-03-26 14:10:14 +1100658
Ben Lindstrome9c99912001-06-09 00:41:05 +0000659void
Damien Miller0e220db2004-06-15 10:34:08 +1000660channel_register_confirm(int id, channel_callback_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000661{
662 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000663
Ben Lindstrome9c99912001-06-09 00:41:05 +0000664 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000665 logit("channel_register_comfirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000666 return;
667 }
Damien Miller67f0bc02002-02-05 12:23:08 +1100668 c->confirm = fn;
Damien Miller0e220db2004-06-15 10:34:08 +1000669 c->confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000670}
Damien Miller4f7becb2006-03-26 14:10:14 +1100671
Ben Lindstrome9c99912001-06-09 00:41:05 +0000672void
Damien Miller39eda6e2005-11-05 14:52:50 +1100673channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000674{
Damien Millerd47c62a2005-12-13 19:33:57 +1100675 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000676
Ben Lindstrome9c99912001-06-09 00:41:05 +0000677 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000678 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000679 return;
680 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000681 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100682 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000683}
Damien Miller4f7becb2006-03-26 14:10:14 +1100684
Ben Lindstrome9c99912001-06-09 00:41:05 +0000685void
686channel_cancel_cleanup(int id)
687{
Damien Millerd47c62a2005-12-13 19:33:57 +1100688 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000689
Ben Lindstrome9c99912001-06-09 00:41:05 +0000690 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000691 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000692 return;
693 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000694 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100695 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000696}
Damien Miller4f7becb2006-03-26 14:10:14 +1100697
Ben Lindstrome9c99912001-06-09 00:41:05 +0000698void
Damien Miller077b2382005-12-31 16:22:32 +1100699channel_register_filter(int id, channel_infilter_fn *ifn,
700 channel_outfilter_fn *ofn)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000701{
702 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000703
Ben Lindstrome9c99912001-06-09 00:41:05 +0000704 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000705 logit("channel_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000706 return;
707 }
Damien Miller077b2382005-12-31 16:22:32 +1100708 c->input_filter = ifn;
709 c->output_filter = ofn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000710}
711
712void
713channel_set_fds(int id, int rfd, int wfd, int efd,
Damien Miller2aa0c192002-02-19 15:20:08 +1100714 int extusage, int nonblock, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000715{
716 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000717
Ben Lindstrome9c99912001-06-09 00:41:05 +0000718 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
719 fatal("channel_activate for non-larval channel %d.", id);
720 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock);
721 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100722 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000723 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
724 packet_put_int(c->remote_id);
725 packet_put_int(c->local_window);
726 packet_send();
727}
728
Damien Miller5428f641999-11-25 11:54:57 +1100729/*
Damien Millerb38eff82000-04-01 11:09:21 +1000730 * 'channel_pre*' are called just before select() to add any bits relevant to
731 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100732 */
Damien Millerb38eff82000-04-01 11:09:21 +1000733/*
734 * 'channel_post*': perform any appropriate operations for channels which
735 * have events pending.
736 */
Damien Millerd62f2ca2006-03-26 13:57:41 +1100737typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000738chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
739chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
740
Damien Miller8473dd82006-07-24 14:08:32 +1000741/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000742static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100743channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000744{
745 FD_SET(c->sock, readset);
746}
747
Damien Miller8473dd82006-07-24 14:08:32 +1000748/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000749static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100750channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000751{
752 debug3("channel %d: waiting for connection", c->self);
753 FD_SET(c->sock, writeset);
754}
755
Ben Lindstrombba81212001-06-25 05:01:22 +0000756static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100757channel_pre_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000758{
759 if (buffer_len(&c->input) < packet_get_maxsize())
760 FD_SET(c->sock, readset);
761 if (buffer_len(&c->output) > 0)
762 FD_SET(c->sock, writeset);
763}
764
Ben Lindstrombba81212001-06-25 05:01:22 +0000765static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100766channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000767{
Damien Millerde6987c2002-01-22 23:20:40 +1100768 u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
Damien Millerb38eff82000-04-01 11:09:21 +1000769
Damien Miller33b13562000-04-04 14:38:59 +1000770 if (c->istate == CHAN_INPUT_OPEN &&
Damien Millerde6987c2002-01-22 23:20:40 +1100771 limit > 0 &&
Damien Miller499a0d52006-04-23 12:06:03 +1000772 buffer_len(&c->input) < limit &&
773 buffer_check_alloc(&c->input, CHAN_RBUF))
Damien Miller33b13562000-04-04 14:38:59 +1000774 FD_SET(c->rfd, readset);
775 if (c->ostate == CHAN_OUTPUT_OPEN ||
776 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
777 if (buffer_len(&c->output) > 0) {
778 FD_SET(c->wfd, writeset);
779 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000780 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +1000781 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
782 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000783 else
784 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000785 }
786 }
787 /** XXX check close conditions, too */
Damien Millerde6987c2002-01-22 23:20:40 +1100788 if (compat20 && c->efd != -1) {
Damien Miller33b13562000-04-04 14:38:59 +1000789 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
790 buffer_len(&c->extended) > 0)
791 FD_SET(c->efd, writeset);
Ben Lindstromcf159442002-03-26 03:26:24 +0000792 else if (!(c->flags & CHAN_EOF_SENT) &&
793 c->extended_usage == CHAN_EXTENDED_READ &&
Damien Miller33b13562000-04-04 14:38:59 +1000794 buffer_len(&c->extended) < c->remote_window)
795 FD_SET(c->efd, readset);
796 }
Damien Miller0e220db2004-06-15 10:34:08 +1000797 /* XXX: What about efd? races? */
Darren Tuckerfc959702004-07-17 16:12:08 +1000798 if (compat20 && c->ctl_fd != -1 &&
Damien Miller0e220db2004-06-15 10:34:08 +1000799 c->istate == CHAN_INPUT_OPEN && c->ostate == CHAN_OUTPUT_OPEN)
800 FD_SET(c->ctl_fd, readset);
Damien Miller33b13562000-04-04 14:38:59 +1000801}
802
Damien Miller8473dd82006-07-24 14:08:32 +1000803/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000804static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100805channel_pre_input_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000806{
807 if (buffer_len(&c->input) == 0) {
808 packet_start(SSH_MSG_CHANNEL_CLOSE);
809 packet_put_int(c->remote_id);
810 packet_send();
811 c->type = SSH_CHANNEL_CLOSED;
Damien Millerfbdeece2003-09-02 22:52:31 +1000812 debug2("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000813 }
814}
815
Damien Miller8473dd82006-07-24 14:08:32 +1000816/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000817static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100818channel_pre_output_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000819{
820 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000821 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000822 else
Damien Millerb38eff82000-04-01 11:09:21 +1000823 FD_SET(c->sock, writeset);
824}
825
826/*
827 * This is a special state for X11 authentication spoofing. An opened X11
828 * connection (when authentication spoofing is being done) remains in this
829 * state until the first packet has been completely read. The authentication
830 * data in that packet is then substituted by the real data if it matches the
831 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000832 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000833 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000834 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000835static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000836x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000837{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000838 u_char *ucp;
839 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000840
841 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000842 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000843 return 0;
844
845 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100846 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000847 if (ucp[0] == 0x42) { /* Byte order MSB first. */
848 proto_len = 256 * ucp[6] + ucp[7];
849 data_len = 256 * ucp[8] + ucp[9];
850 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
851 proto_len = ucp[6] + 256 * ucp[7];
852 data_len = ucp[8] + 256 * ucp[9];
853 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +1000854 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100855 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000856 return -1;
857 }
858
859 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000860 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000861 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
862 return 0;
863
864 /* Check if authentication protocol matches. */
865 if (proto_len != strlen(x11_saved_proto) ||
866 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000867 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +1000868 return -1;
869 }
870 /* Check if authentication data matches our fake data. */
871 if (data_len != x11_fake_data_len ||
872 memcmp(ucp + 12 + ((proto_len + 3) & ~3),
873 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000874 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +1000875 return -1;
876 }
877 /* Check fake data length */
878 if (x11_fake_data_len != x11_saved_data_len) {
879 error("X11 fake_data_len %d != saved_data_len %d",
880 x11_fake_data_len, x11_saved_data_len);
881 return -1;
882 }
883 /*
884 * Received authentication protocol and data match
885 * our fake data. Substitute the fake data with real
886 * data.
887 */
888 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
889 x11_saved_data, x11_saved_data_len);
890 return 1;
891}
892
Ben Lindstrombba81212001-06-25 05:01:22 +0000893static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100894channel_pre_x11_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000895{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000896 int ret = x11_open_helper(&c->output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000897
Damien Millerb38eff82000-04-01 11:09:21 +1000898 if (ret == 1) {
899 /* Start normal processing for the channel. */
900 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +1000901 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000902 } else if (ret == -1) {
903 /*
904 * We have received an X11 connection that has bad
905 * authentication information.
906 */
Damien Miller996acd22003-04-09 20:59:48 +1000907 logit("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +1000908 buffer_clear(&c->input);
909 buffer_clear(&c->output);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000910 channel_close_fd(&c->sock);
Damien Millerb38eff82000-04-01 11:09:21 +1000911 c->sock = -1;
912 c->type = SSH_CHANNEL_CLOSED;
913 packet_start(SSH_MSG_CHANNEL_CLOSE);
914 packet_put_int(c->remote_id);
915 packet_send();
916 }
917}
918
Ben Lindstrombba81212001-06-25 05:01:22 +0000919static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100920channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000921{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000922 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000923
924 /* c->force_drain = 1; */
925
Damien Millerb38eff82000-04-01 11:09:21 +1000926 if (ret == 1) {
927 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +1100928 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000929 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +1000930 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +1000931 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +1100932 chan_read_failed(c);
933 buffer_clear(&c->input);
934 chan_ibuf_empty(c);
935 buffer_clear(&c->output);
936 /* for proto v1, the peer will send an IEOF */
937 if (compat20)
938 chan_write_failed(c);
939 else
940 c->type = SSH_CHANNEL_OPEN;
Damien Millerfbdeece2003-09-02 22:52:31 +1000941 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +1000942 }
943}
944
Ben Lindstromb3921512001-04-11 15:57:50 +0000945/* try to decode a socks4 header */
Damien Miller8473dd82006-07-24 14:08:32 +1000946/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000947static int
Damien Millerd62f2ca2006-03-26 13:57:41 +1100948channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000949{
Damien Millera10f5612002-09-12 09:49:15 +1000950 char *p, *host;
Damien Millereccb9de2005-06-17 12:59:34 +1000951 u_int len, have, i, found;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100952 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000953 struct {
954 u_int8_t version;
955 u_int8_t command;
956 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +0000957 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000958 } s4_req, s4_rsp;
959
Ben Lindstromb3921512001-04-11 15:57:50 +0000960 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000961
962 have = buffer_len(&c->input);
963 len = sizeof(s4_req);
964 if (have < len)
965 return 0;
966 p = buffer_ptr(&c->input);
967 for (found = 0, i = len; i < have; i++) {
968 if (p[i] == '\0') {
969 found = 1;
970 break;
971 }
972 if (i > 1024) {
973 /* the peer is probably sending garbage */
974 debug("channel %d: decode socks4: too long",
975 c->self);
976 return -1;
977 }
978 }
979 if (!found)
980 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000981 buffer_get(&c->input, (char *)&s4_req.version, 1);
982 buffer_get(&c->input, (char *)&s4_req.command, 1);
983 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +0000984 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +0000985 have = buffer_len(&c->input);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000986 p = buffer_ptr(&c->input);
987 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +0000988 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000989 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +0000990 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000991 c->self, len, have);
992 strlcpy(username, p, sizeof(username));
993 buffer_consume(&c->input, len);
994 buffer_consume(&c->input, 1); /* trailing '\0' */
995
Ben Lindstromb3921512001-04-11 15:57:50 +0000996 host = inet_ntoa(s4_req.dest_addr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000997 strlcpy(c->path, host, sizeof(c->path));
998 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100999
Damien Millerfbdeece2003-09-02 22:52:31 +10001000 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001001 c->self, host, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001002
Ben Lindstromb3921512001-04-11 15:57:50 +00001003 if (s4_req.command != 1) {
1004 debug("channel %d: cannot handle: socks4 cn %d",
1005 c->self, s4_req.command);
1006 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001007 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001008 s4_rsp.version = 0; /* vn: 0 for reply */
1009 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001010 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001011 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Damien Millera0fdce92006-03-26 14:28:50 +11001012 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +00001013 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001014}
1015
Darren Tucker46471c92003-07-03 13:55:19 +10001016/* try to decode a socks5 header */
1017#define SSH_SOCKS5_AUTHDONE 0x1000
1018#define SSH_SOCKS5_NOAUTH 0x00
1019#define SSH_SOCKS5_IPV4 0x01
1020#define SSH_SOCKS5_DOMAIN 0x03
1021#define SSH_SOCKS5_IPV6 0x04
1022#define SSH_SOCKS5_CONNECT 0x01
1023#define SSH_SOCKS5_SUCCESS 0x00
1024
Damien Miller8473dd82006-07-24 14:08:32 +10001025/* ARGSUSED */
Darren Tucker46471c92003-07-03 13:55:19 +10001026static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001027channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001028{
1029 struct {
1030 u_int8_t version;
1031 u_int8_t command;
1032 u_int8_t reserved;
1033 u_int8_t atyp;
1034 } s5_req, s5_rsp;
1035 u_int16_t dest_port;
1036 u_char *p, dest_addr[255+1];
Damien Miller0f077072006-07-10 22:21:02 +10001037 u_int have, need, i, found, nmethods, addrlen, af;
Darren Tucker46471c92003-07-03 13:55:19 +10001038
1039 debug2("channel %d: decode socks5", c->self);
1040 p = buffer_ptr(&c->input);
1041 if (p[0] != 0x05)
1042 return -1;
1043 have = buffer_len(&c->input);
1044 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1045 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001046 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001047 return 0;
1048 nmethods = p[1];
1049 if (have < nmethods + 2)
1050 return 0;
1051 /* look for method: "NO AUTHENTICATION REQUIRED" */
1052 for (found = 0, i = 2 ; i < nmethods + 2; i++) {
1053 if (p[i] == SSH_SOCKS5_NOAUTH ) {
1054 found = 1;
1055 break;
1056 }
1057 }
1058 if (!found) {
1059 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1060 c->self);
1061 return -1;
1062 }
1063 buffer_consume(&c->input, nmethods + 2);
1064 buffer_put_char(&c->output, 0x05); /* version */
1065 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1066 FD_SET(c->sock, writeset);
1067 c->flags |= SSH_SOCKS5_AUTHDONE;
1068 debug2("channel %d: socks5 auth done", c->self);
1069 return 0; /* need more */
1070 }
1071 debug2("channel %d: socks5 post auth", c->self);
1072 if (have < sizeof(s5_req)+1)
1073 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001074 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001075 if (s5_req.version != 0x05 ||
1076 s5_req.command != SSH_SOCKS5_CONNECT ||
1077 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001078 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001079 return -1;
1080 }
Darren Tucker47eede72005-03-14 23:08:12 +11001081 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001082 case SSH_SOCKS5_IPV4:
1083 addrlen = 4;
1084 af = AF_INET;
1085 break;
1086 case SSH_SOCKS5_DOMAIN:
1087 addrlen = p[sizeof(s5_req)];
1088 af = -1;
1089 break;
1090 case SSH_SOCKS5_IPV6:
1091 addrlen = 16;
1092 af = AF_INET6;
1093 break;
1094 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001095 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001096 return -1;
1097 }
Damien Miller0f077072006-07-10 22:21:02 +10001098 need = sizeof(s5_req) + addrlen + 2;
1099 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1100 need++;
1101 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001102 return 0;
1103 buffer_consume(&c->input, sizeof(s5_req));
1104 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1105 buffer_consume(&c->input, 1); /* host string length */
1106 buffer_get(&c->input, (char *)&dest_addr, addrlen);
1107 buffer_get(&c->input, (char *)&dest_port, 2);
1108 dest_addr[addrlen] = '\0';
1109 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
Darren Tucker1f8311c2004-05-13 16:39:33 +10001110 strlcpy(c->path, (char *)dest_addr, sizeof(c->path));
Darren Tucker46471c92003-07-03 13:55:19 +10001111 else if (inet_ntop(af, dest_addr, c->path, sizeof(c->path)) == NULL)
1112 return -1;
1113 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001114
Damien Millerfbdeece2003-09-02 22:52:31 +10001115 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001116 c->self, c->path, c->host_port, s5_req.command);
1117
1118 s5_rsp.version = 0x05;
1119 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1120 s5_rsp.reserved = 0; /* ignored */
1121 s5_rsp.atyp = SSH_SOCKS5_IPV4;
1122 ((struct in_addr *)&dest_addr)->s_addr = INADDR_ANY;
1123 dest_port = 0; /* ignored */
1124
Damien Millera0fdce92006-03-26 14:28:50 +11001125 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
1126 buffer_append(&c->output, &dest_addr, sizeof(struct in_addr));
1127 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001128 return 1;
1129}
1130
Ben Lindstromb3921512001-04-11 15:57:50 +00001131/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001132static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001133channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001134{
1135 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001136 u_int have;
1137 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001138
1139 have = buffer_len(&c->input);
Damien Miller4623a752001-10-10 15:03:58 +10001140 c->delayed = 0;
Ben Lindstromb3921512001-04-11 15:57:50 +00001141 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001142 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001143 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001144 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001145 /* need more */
1146 FD_SET(c->sock, readset);
1147 return;
1148 }
1149 /* try to guess the protocol */
1150 p = buffer_ptr(&c->input);
1151 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001152 case 0x04:
1153 ret = channel_decode_socks4(c, readset, writeset);
1154 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001155 case 0x05:
1156 ret = channel_decode_socks5(c, readset, writeset);
1157 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001158 default:
1159 ret = -1;
1160 break;
1161 }
1162 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001163 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001164 } else if (ret == 0) {
1165 debug2("channel %d: pre_dynamic: need more", c->self);
1166 /* need more */
1167 FD_SET(c->sock, readset);
1168 } else {
1169 /* switch to the next state */
1170 c->type = SSH_CHANNEL_OPENING;
1171 port_open_helper(c, "direct-tcpip");
1172 }
1173}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001174
Damien Millerb38eff82000-04-01 11:09:21 +10001175/* This is our fake X11 server socket. */
Damien Miller8473dd82006-07-24 14:08:32 +10001176/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001177static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001178channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001179{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001180 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001181 struct sockaddr addr;
Damien Miller398e1cf2002-02-05 11:52:13 +11001182 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001183 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001184 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001185 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001186
1187 if (FD_ISSET(c->sock, readset)) {
1188 debug("X11 connection requested.");
1189 addrlen = sizeof(addr);
1190 newsock = accept(c->sock, &addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001191 if (c->single_connection) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001192 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001193 channel_close_fd(&c->sock);
1194 chan_mark_dead(c);
1195 }
Damien Millerb38eff82000-04-01 11:09:21 +10001196 if (newsock < 0) {
1197 error("accept: %.100s", strerror(errno));
1198 return;
1199 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001200 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001201 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001202 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001203 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001204 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001205
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001206 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001207 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001208 c->local_window_max, c->local_maxpacket, 0, buf, 1);
Damien Millerbd483e72000-04-30 10:00:53 +10001209 if (compat20) {
1210 packet_start(SSH2_MSG_CHANNEL_OPEN);
1211 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001212 packet_put_int(nc->self);
Damien Millere7378562001-12-21 14:58:35 +11001213 packet_put_int(nc->local_window_max);
1214 packet_put_int(nc->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +11001215 /* originator ipaddr and port */
1216 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +10001217 if (datafellows & SSH_BUG_X11FWD) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001218 debug2("ssh2 x11 bug compat mode");
Damien Miller30c3d422000-05-09 11:02:59 +10001219 } else {
1220 packet_put_int(remote_port);
1221 }
Damien Millerbd483e72000-04-30 10:00:53 +10001222 packet_send();
1223 } else {
1224 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001225 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001226 if (packet_get_protocol_flags() &
1227 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +00001228 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +10001229 packet_send();
1230 }
Damien Millerd83ff352001-01-30 09:19:34 +11001231 xfree(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001232 }
1233}
1234
Ben Lindstrombba81212001-06-25 05:01:22 +00001235static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001236port_open_helper(Channel *c, char *rtype)
1237{
1238 int direct;
1239 char buf[1024];
1240 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001241 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001242
1243 direct = (strcmp(rtype, "direct-tcpip") == 0);
1244
1245 snprintf(buf, sizeof buf,
1246 "%s: listening port %d for %.100s port %d, "
1247 "connect from %.200s port %d",
1248 rtype, c->listening_port, c->path, c->host_port,
1249 remote_ipaddr, remote_port);
1250
1251 xfree(c->remote_name);
1252 c->remote_name = xstrdup(buf);
1253
1254 if (compat20) {
1255 packet_start(SSH2_MSG_CHANNEL_OPEN);
1256 packet_put_cstring(rtype);
1257 packet_put_int(c->self);
1258 packet_put_int(c->local_window_max);
1259 packet_put_int(c->local_maxpacket);
1260 if (direct) {
1261 /* target host, port */
1262 packet_put_cstring(c->path);
1263 packet_put_int(c->host_port);
1264 } else {
1265 /* listen address, port */
1266 packet_put_cstring(c->path);
1267 packet_put_int(c->listening_port);
1268 }
1269 /* originator host and port */
1270 packet_put_cstring(remote_ipaddr);
Damien Miller677257f2005-06-17 12:55:03 +10001271 packet_put_int((u_int)remote_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001272 packet_send();
1273 } else {
1274 packet_start(SSH_MSG_PORT_OPEN);
1275 packet_put_int(c->self);
1276 packet_put_cstring(c->path);
1277 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001278 if (packet_get_protocol_flags() &
1279 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001280 packet_put_cstring(c->remote_name);
1281 packet_send();
1282 }
1283 xfree(remote_ipaddr);
1284}
1285
Damien Miller5e7fd072005-11-05 14:53:39 +11001286static void
1287channel_set_reuseaddr(int fd)
1288{
1289 int on = 1;
1290
1291 /*
1292 * Set socket options.
1293 * Allow local port reuse in TIME_WAIT.
1294 */
1295 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1296 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1297}
1298
Damien Millerb38eff82000-04-01 11:09:21 +10001299/*
1300 * This socket is listening for connections to a forwarded TCP/IP port.
1301 */
Damien Miller8473dd82006-07-24 14:08:32 +10001302/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001303static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001304channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001305{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001306 Channel *nc;
Damien Millerb38eff82000-04-01 11:09:21 +10001307 struct sockaddr addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001308 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001309 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001310 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001311
Damien Millerb38eff82000-04-01 11:09:21 +10001312 if (FD_ISSET(c->sock, readset)) {
1313 debug("Connection to port %d forwarding "
1314 "to %.100s port %d requested.",
1315 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001316
Damien Miller4623a752001-10-10 15:03:58 +10001317 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1318 nextstate = SSH_CHANNEL_OPENING;
1319 rtype = "forwarded-tcpip";
1320 } else {
1321 if (c->host_port == 0) {
1322 nextstate = SSH_CHANNEL_DYNAMIC;
Damien Millerd3c04b92001-10-10 15:04:20 +10001323 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001324 } else {
1325 nextstate = SSH_CHANNEL_OPENING;
1326 rtype = "direct-tcpip";
1327 }
1328 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001329
Damien Millerb38eff82000-04-01 11:09:21 +10001330 addrlen = sizeof(addr);
1331 newsock = accept(c->sock, &addr, &addrlen);
1332 if (newsock < 0) {
1333 error("accept: %.100s", strerror(errno));
1334 return;
1335 }
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00001336 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001337 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1338 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001339 nc->listening_port = c->listening_port;
1340 nc->host_port = c->host_port;
1341 strlcpy(nc->path, c->path, sizeof(nc->path));
1342
Damien Miller4623a752001-10-10 15:03:58 +10001343 if (nextstate == SSH_CHANNEL_DYNAMIC) {
1344 /*
1345 * do not call the channel_post handler until
1346 * this flag has been reset by a pre-handler.
1347 * otherwise the FD_ISSET calls might overflow
1348 */
1349 nc->delayed = 1;
1350 } else {
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001351 port_open_helper(nc, rtype);
Damien Miller4623a752001-10-10 15:03:58 +10001352 }
Damien Millerb38eff82000-04-01 11:09:21 +10001353 }
1354}
1355
1356/*
1357 * This is the authentication agent socket listening for connections from
1358 * clients.
1359 */
Damien Miller8473dd82006-07-24 14:08:32 +10001360/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001361static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001362channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001363{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001364 Channel *nc;
1365 int newsock;
Damien Millerb38eff82000-04-01 11:09:21 +10001366 struct sockaddr addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001367 socklen_t addrlen;
1368
1369 if (FD_ISSET(c->sock, readset)) {
1370 addrlen = sizeof(addr);
1371 newsock = accept(c->sock, &addr, &addrlen);
1372 if (newsock < 0) {
1373 error("accept from auth socket: %.100s", strerror(errno));
1374 return;
1375 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001376 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001377 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1378 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001379 0, "accepted auth socket", 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001380 if (compat20) {
1381 packet_start(SSH2_MSG_CHANNEL_OPEN);
1382 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001383 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001384 packet_put_int(c->local_window_max);
1385 packet_put_int(c->local_maxpacket);
1386 } else {
1387 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001388 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001389 }
Damien Millerb38eff82000-04-01 11:09:21 +10001390 packet_send();
1391 }
1392}
1393
Damien Miller8473dd82006-07-24 14:08:32 +10001394/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001395static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001396channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001397{
Ben Lindstrom69128662001-05-08 20:07:39 +00001398 int err = 0;
Ben Lindstrom11180952001-07-04 05:13:35 +00001399 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001400
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001401 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001402 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001403 err = errno;
1404 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001405 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001406 if (err == 0) {
1407 debug("channel %d: connected", c->self);
1408 c->type = SSH_CHANNEL_OPEN;
1409 if (compat20) {
1410 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1411 packet_put_int(c->remote_id);
1412 packet_put_int(c->self);
1413 packet_put_int(c->local_window);
1414 packet_put_int(c->local_maxpacket);
1415 } else {
1416 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1417 packet_put_int(c->remote_id);
1418 packet_put_int(c->self);
1419 }
1420 } else {
1421 debug("channel %d: not connected: %s",
1422 c->self, strerror(err));
1423 if (compat20) {
1424 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1425 packet_put_int(c->remote_id);
1426 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1427 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1428 packet_put_cstring(strerror(err));
1429 packet_put_cstring("");
1430 }
1431 } else {
1432 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1433 packet_put_int(c->remote_id);
1434 }
1435 chan_mark_dead(c);
1436 }
1437 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001438 }
1439}
1440
Damien Miller8473dd82006-07-24 14:08:32 +10001441/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001442static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001443channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001444{
Darren Tucker11327cc2005-03-14 23:22:25 +11001445 char buf[CHAN_RBUF];
Damien Millerb38eff82000-04-01 11:09:21 +10001446 int len;
1447
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001448 if (c->rfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001449 FD_ISSET(c->rfd, readset)) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001450 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001451 len = read(c->rfd, buf, sizeof(buf));
Damien Miller6f83b8e2000-05-02 09:23:45 +10001452 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1453 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001454#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001455 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001456#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001457 if ((!c->isatty && len <= 0) ||
1458 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001459#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001460 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001461 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001462 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001463 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001464 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001465 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001466 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001467 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001468 c->type = SSH_CHANNEL_INPUT_DRAINING;
Damien Millerfbdeece2003-09-02 22:52:31 +10001469 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001470 } else {
1471 chan_read_failed(c);
1472 }
1473 return -1;
1474 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001475 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001476 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001477 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001478 chan_read_failed(c);
1479 }
Damien Millerd27b9472005-12-13 19:29:02 +11001480 } else if (c->datagram) {
1481 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001482 } else {
1483 buffer_append(&c->input, buf, len);
1484 }
Damien Millerb38eff82000-04-01 11:09:21 +10001485 }
1486 return 1;
1487}
Damien Miller4f7becb2006-03-26 14:10:14 +11001488
Damien Miller8473dd82006-07-24 14:08:32 +10001489/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001490static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001491channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001492{
Ben Lindstrome229b252001-03-05 06:28:06 +00001493 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001494 u_char *data = NULL, *buf;
Ben Lindstrom6d218f42001-09-18 05:53:12 +00001495 u_int dlen;
Damien Millerb38eff82000-04-01 11:09:21 +10001496 int len;
1497
1498 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001499 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001500 FD_ISSET(c->wfd, writeset) &&
1501 buffer_len(&c->output) > 0) {
Damien Miller077b2382005-12-31 16:22:32 +11001502 if (c->output_filter != NULL) {
1503 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1504 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001505 if (c->type != SSH_CHANNEL_OPEN)
1506 chan_mark_dead(c);
1507 else
1508 chan_write_failed(c);
1509 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001510 }
1511 } else if (c->datagram) {
1512 buf = data = buffer_get_string(&c->output, &dlen);
1513 } else {
1514 buf = data = buffer_ptr(&c->output);
1515 dlen = buffer_len(&c->output);
1516 }
1517
Damien Millerd27b9472005-12-13 19:29:02 +11001518 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001519 /* ignore truncated writes, datagrams might get lost */
1520 c->local_consumed += dlen + 4;
Damien Miller077b2382005-12-31 16:22:32 +11001521 len = write(c->wfd, buf, dlen);
Damien Millerd27b9472005-12-13 19:29:02 +11001522 xfree(data);
1523 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1524 return 1;
1525 if (len <= 0) {
1526 if (c->type != SSH_CHANNEL_OPEN)
1527 chan_mark_dead(c);
1528 else
1529 chan_write_failed(c);
1530 return -1;
1531 }
1532 return 1;
1533 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001534#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001535 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker240fdfa2003-11-22 14:10:02 +11001536 if (compat20 && c->wfd_isatty)
1537 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001538#endif
Damien Miller077b2382005-12-31 16:22:32 +11001539
1540 len = write(c->wfd, buf, dlen);
Damien Miller6f83b8e2000-05-02 09:23:45 +10001541 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1542 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001543 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001544 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001545 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001546 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001547 return -1;
1548 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001549 buffer_clear(&c->output);
Damien Millerfbdeece2003-09-02 22:52:31 +10001550 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001551 c->type = SSH_CHANNEL_INPUT_DRAINING;
1552 } else {
1553 chan_write_failed(c);
1554 }
1555 return -1;
1556 }
Damien Miller077b2382005-12-31 16:22:32 +11001557 if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001558 if (tcgetattr(c->wfd, &tio) == 0 &&
1559 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1560 /*
1561 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001562 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001563 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001564 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001565 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001566 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001567 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001568 }
1569 }
Damien Millerb38eff82000-04-01 11:09:21 +10001570 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001571 if (compat20 && len > 0) {
1572 c->local_consumed += len;
1573 }
1574 }
1575 return 1;
1576}
Damien Miller4f7becb2006-03-26 14:10:14 +11001577
Ben Lindstrombba81212001-06-25 05:01:22 +00001578static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001579channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001580{
Darren Tucker11327cc2005-03-14 23:22:25 +11001581 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001582 int len;
1583
Damien Miller1383bd82000-04-06 12:32:37 +10001584/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001585 if (c->efd != -1) {
1586 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1587 FD_ISSET(c->efd, writeset) &&
1588 buffer_len(&c->extended) > 0) {
1589 len = write(c->efd, buffer_ptr(&c->extended),
1590 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001591 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001592 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001593 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1594 return 1;
1595 if (len <= 0) {
1596 debug2("channel %d: closing write-efd %d",
1597 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001598 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001599 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001600 buffer_consume(&c->extended, len);
1601 c->local_consumed += len;
1602 }
1603 } else if (c->extended_usage == CHAN_EXTENDED_READ &&
1604 FD_ISSET(c->efd, readset)) {
1605 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001606 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001607 c->self, len, c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001608 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1609 return 1;
1610 if (len <= 0) {
1611 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001612 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001613 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001614 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001615 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001616 }
Damien Miller33b13562000-04-04 14:38:59 +10001617 }
1618 }
1619 return 1;
1620}
Damien Miller4f7becb2006-03-26 14:10:14 +11001621
Damien Miller8473dd82006-07-24 14:08:32 +10001622/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001623static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001624channel_handle_ctl(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller0e220db2004-06-15 10:34:08 +10001625{
1626 char buf[16];
1627 int len;
1628
1629 /* Monitor control fd to detect if the slave client exits */
1630 if (c->ctl_fd != -1 && FD_ISSET(c->ctl_fd, readset)) {
1631 len = read(c->ctl_fd, buf, sizeof(buf));
1632 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1633 return 1;
1634 if (len <= 0) {
1635 debug2("channel %d: ctl read<=0", c->self);
1636 if (c->type != SSH_CHANNEL_OPEN) {
1637 debug2("channel %d: not open", c->self);
1638 chan_mark_dead(c);
1639 return -1;
1640 } else {
1641 chan_read_failed(c);
1642 chan_write_failed(c);
1643 }
1644 return -1;
1645 } else
1646 fatal("%s: unexpected data on ctl fd", __func__);
1647 }
1648 return 1;
1649}
Damien Miller4f7becb2006-03-26 14:10:14 +11001650
Damien Miller0e220db2004-06-15 10:34:08 +10001651static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001652channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001653{
Ben Lindstromb3921512001-04-11 15:57:50 +00001654 if (c->type == SSH_CHANNEL_OPEN &&
1655 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Damien Miller33b13562000-04-04 14:38:59 +10001656 c->local_window < c->local_window_max/2 &&
1657 c->local_consumed > 0) {
1658 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1659 packet_put_int(c->remote_id);
1660 packet_put_int(c->local_consumed);
1661 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001662 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001663 c->self, c->local_window,
1664 c->local_consumed);
1665 c->local_window += c->local_consumed;
1666 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001667 }
1668 return 1;
1669}
1670
Ben Lindstrombba81212001-06-25 05:01:22 +00001671static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001672channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001673{
Damien Miller4623a752001-10-10 15:03:58 +10001674 if (c->delayed)
1675 return;
Damien Millerb38eff82000-04-01 11:09:21 +10001676 channel_handle_rfd(c, readset, writeset);
1677 channel_handle_wfd(c, readset, writeset);
Damien Millerde6987c2002-01-22 23:20:40 +11001678 if (!compat20)
Damien Miller4623a752001-10-10 15:03:58 +10001679 return;
Damien Miller33b13562000-04-04 14:38:59 +10001680 channel_handle_efd(c, readset, writeset);
Damien Miller0e220db2004-06-15 10:34:08 +10001681 channel_handle_ctl(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001682 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001683}
1684
Damien Miller8473dd82006-07-24 14:08:32 +10001685/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001686static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001687channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001688{
1689 int len;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001690
Damien Millerb38eff82000-04-01 11:09:21 +10001691 /* Send buffered output data to the socket. */
1692 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
1693 len = write(c->sock, buffer_ptr(&c->output),
1694 buffer_len(&c->output));
1695 if (len <= 0)
Damien Miller76765c02002-01-22 23:21:15 +11001696 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001697 else
1698 buffer_consume(&c->output, len);
1699 }
1700}
1701
Ben Lindstrombba81212001-06-25 05:01:22 +00001702static void
Damien Miller33b13562000-04-04 14:38:59 +10001703channel_handler_init_20(void)
1704{
Damien Millerde6987c2002-01-22 23:20:40 +11001705 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001706 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001707 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001708 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001709 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001710 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001711 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001712 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Miller33b13562000-04-04 14:38:59 +10001713
Damien Millerde6987c2002-01-22 23:20:40 +11001714 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001715 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001716 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001717 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001718 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001719 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001720 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001721}
1722
Ben Lindstrombba81212001-06-25 05:01:22 +00001723static void
Damien Millerb38eff82000-04-01 11:09:21 +10001724channel_handler_init_13(void)
1725{
1726 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
1727 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
1728 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1729 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1730 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
1731 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
1732 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001733 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001734 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001735
Damien Millerde6987c2002-01-22 23:20:40 +11001736 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001737 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1738 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1739 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
1740 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001741 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001742 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001743}
1744
Ben Lindstrombba81212001-06-25 05:01:22 +00001745static void
Damien Millerb38eff82000-04-01 11:09:21 +10001746channel_handler_init_15(void)
1747{
Damien Millerde6987c2002-01-22 23:20:40 +11001748 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001749 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001750 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
1751 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
1752 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001753 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001754 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10001755
1756 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
1757 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
1758 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Damien Millerde6987c2002-01-22 23:20:40 +11001759 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001760 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001761 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10001762}
1763
Ben Lindstrombba81212001-06-25 05:01:22 +00001764static void
Damien Millerb38eff82000-04-01 11:09:21 +10001765channel_handler_init(void)
1766{
1767 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001768
Damien Miller9f0f5c62001-12-21 14:45:46 +11001769 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10001770 channel_pre[i] = NULL;
1771 channel_post[i] = NULL;
1772 }
Damien Miller33b13562000-04-04 14:38:59 +10001773 if (compat20)
1774 channel_handler_init_20();
1775 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10001776 channel_handler_init_13();
1777 else
1778 channel_handler_init_15();
1779}
1780
Damien Miller3ec27592001-10-12 11:35:04 +10001781/* gc dead channels */
1782static void
1783channel_garbage_collect(Channel *c)
1784{
1785 if (c == NULL)
1786 return;
1787 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11001788 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10001789 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001790 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001791 c->detach_user(c->self, NULL);
1792 /* if we still have a callback */
1793 if (c->detach_user != NULL)
1794 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001795 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001796 }
1797 if (!chan_is_dead(c, 1))
1798 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001799 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001800 channel_free(c);
1801}
1802
Ben Lindstrombba81212001-06-25 05:01:22 +00001803static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001804channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001805{
1806 static int did_init = 0;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001807 u_int i;
Damien Millerb38eff82000-04-01 11:09:21 +10001808 Channel *c;
1809
1810 if (!did_init) {
1811 channel_handler_init();
1812 did_init = 1;
1813 }
1814 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001815 c = channels[i];
1816 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10001817 continue;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001818 if (ftab[c->type] != NULL)
1819 (*ftab[c->type])(c, readset, writeset);
Damien Miller3ec27592001-10-12 11:35:04 +10001820 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10001821 }
1822}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001823
Ben Lindstrome9c99912001-06-09 00:41:05 +00001824/*
1825 * Allocate/update select bitmasks and add any bits relevant to channels in
1826 * select bitmasks.
1827 */
Damien Miller4af51302000-04-16 11:18:38 +10001828void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001829channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001830 u_int *nallocp, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001831{
Damien Miller36812092006-03-26 14:22:47 +11001832 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11001833
1834 n = MAX(*maxfdp, channel_max_fd);
1835
Damien Miller36812092006-03-26 14:22:47 +11001836 nfdset = howmany(n+1, NFDBITS);
1837 /* Explicitly test here, because xrealloc isn't always called */
1838 if (nfdset && SIZE_T_MAX / nfdset < sizeof(fd_mask))
1839 fatal("channel_prepare_select: max_fd (%d) is too large", n);
1840 sz = nfdset * sizeof(fd_mask);
1841
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001842 /* perhaps check sz < nalloc/2 and shrink? */
1843 if (*readsetp == NULL || sz > *nallocp) {
Damien Miller36812092006-03-26 14:22:47 +11001844 *readsetp = xrealloc(*readsetp, nfdset, sizeof(fd_mask));
1845 *writesetp = xrealloc(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001846 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11001847 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001848 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11001849 memset(*readsetp, 0, sz);
1850 memset(*writesetp, 0, sz);
1851
Ben Lindstrombe2cc432001-04-04 23:46:07 +00001852 if (!rekeying)
1853 channel_handler(channel_pre, *readsetp, *writesetp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001854}
1855
Ben Lindstrome9c99912001-06-09 00:41:05 +00001856/*
1857 * After select, perform any appropriate operations for channels which have
1858 * events pending.
1859 */
Damien Miller4af51302000-04-16 11:18:38 +10001860void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001861channel_after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001862{
Damien Millerb38eff82000-04-01 11:09:21 +10001863 channel_handler(channel_post, readset, writeset);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001864}
1865
Ben Lindstrome9c99912001-06-09 00:41:05 +00001866
Damien Miller5e953212001-01-30 09:14:00 +11001867/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10001868void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00001869channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001870{
Damien Millerb38eff82000-04-01 11:09:21 +10001871 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10001872 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001873
Damien Miller95def091999-11-25 00:26:21 +11001874 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001875 c = channels[i];
1876 if (c == NULL)
1877 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001878
Ben Lindstrome9c99912001-06-09 00:41:05 +00001879 /*
1880 * We are only interested in channels that can have buffered
1881 * incoming data.
1882 */
Damien Miller34132e52000-01-14 15:45:46 +11001883 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10001884 if (c->type != SSH_CHANNEL_OPEN &&
1885 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11001886 continue;
1887 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10001888 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11001889 continue;
Damien Miller34132e52000-01-14 15:45:46 +11001890 }
Damien Millerefb4afe2000-04-12 18:45:05 +10001891 if (compat20 &&
1892 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001893 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10001894 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10001895 continue;
1896 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001897
Damien Miller95def091999-11-25 00:26:21 +11001898 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001899 if ((c->istate == CHAN_INPUT_OPEN ||
1900 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
1901 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11001902 if (c->datagram) {
1903 if (len > 0) {
1904 u_char *data;
1905 u_int dlen;
1906
1907 data = buffer_get_string(&c->input,
1908 &dlen);
1909 packet_start(SSH2_MSG_CHANNEL_DATA);
1910 packet_put_int(c->remote_id);
1911 packet_put_string(data, dlen);
1912 packet_send();
1913 c->remote_window -= dlen + 4;
1914 xfree(data);
1915 }
1916 continue;
1917 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00001918 /*
1919 * Send some data for the other side over the secure
1920 * connection.
1921 */
Damien Miller33b13562000-04-04 14:38:59 +10001922 if (compat20) {
1923 if (len > c->remote_window)
1924 len = c->remote_window;
1925 if (len > c->remote_maxpacket)
1926 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11001927 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001928 if (packet_is_interactive()) {
1929 if (len > 1024)
1930 len = 512;
1931 } else {
1932 /* Keep the packets at reasonable size. */
1933 if (len > packet_get_maxsize()/2)
1934 len = packet_get_maxsize()/2;
1935 }
Damien Miller95def091999-11-25 00:26:21 +11001936 }
Damien Millerb38eff82000-04-01 11:09:21 +10001937 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10001938 packet_start(compat20 ?
1939 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10001940 packet_put_int(c->remote_id);
1941 packet_put_string(buffer_ptr(&c->input), len);
1942 packet_send();
1943 buffer_consume(&c->input, len);
1944 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10001945 }
1946 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11001947 if (compat13)
1948 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11001949 /*
1950 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00001951 * tell peer, that we will not send more data: send IEOF.
1952 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11001953 */
Ben Lindstromcf159442002-03-26 03:26:24 +00001954 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10001955 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
1956 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00001957 else
1958 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11001959 }
Damien Miller33b13562000-04-04 14:38:59 +10001960 /* Send extended data, i.e. stderr */
1961 if (compat20 &&
Ben Lindstromcf159442002-03-26 03:26:24 +00001962 !(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10001963 c->remote_window > 0 &&
1964 (len = buffer_len(&c->extended)) > 0 &&
1965 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00001966 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001967 c->self, c->remote_window, buffer_len(&c->extended),
1968 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10001969 if (len > c->remote_window)
1970 len = c->remote_window;
1971 if (len > c->remote_maxpacket)
1972 len = c->remote_maxpacket;
1973 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
1974 packet_put_int(c->remote_id);
1975 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
1976 packet_put_string(buffer_ptr(&c->extended), len);
1977 packet_send();
1978 buffer_consume(&c->extended, len);
1979 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001980 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10001981 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001982 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001983}
1984
Ben Lindstrome9c99912001-06-09 00:41:05 +00001985
1986/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11001987
1988/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10001989void
Damien Miller630d6f42002-01-22 23:17:30 +11001990channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001991{
Damien Miller34132e52000-01-14 15:45:46 +11001992 int id;
Damien Miller95def091999-11-25 00:26:21 +11001993 char *data;
Ben Lindstrom46c16222000-12-22 01:43:59 +00001994 u_int data_len;
Damien Millerb38eff82000-04-01 11:09:21 +10001995 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001996
Damien Miller95def091999-11-25 00:26:21 +11001997 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11001998 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10001999 c = channel_lookup(id);
2000 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11002001 packet_disconnect("Received data for nonexistent channel %d.", id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002002
Damien Miller95def091999-11-25 00:26:21 +11002003 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002004 if (c->type != SSH_CHANNEL_OPEN &&
2005 c->type != SSH_CHANNEL_X11_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11002006 return;
2007
Damien Miller95def091999-11-25 00:26:21 +11002008 /* Get the data. */
2009 data = packet_get_string(&data_len);
Damien Millerb38eff82000-04-01 11:09:21 +10002010
Damien Millera04ad492004-01-21 11:02:09 +11002011 /*
2012 * Ignore data for protocol > 1.3 if output end is no longer open.
2013 * For protocol 2 the sending side is reducing its window as it sends
2014 * data, so we must 'fake' consumption of the data in order to ensure
2015 * that window updates are sent back. Otherwise the connection might
2016 * deadlock.
2017 */
2018 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
2019 if (compat20) {
2020 c->local_window -= data_len;
2021 c->local_consumed += data_len;
2022 }
2023 xfree(data);
2024 return;
2025 }
2026
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002027 if (compat20) {
Damien Miller33b13562000-04-04 14:38:59 +10002028 if (data_len > c->local_maxpacket) {
Damien Miller996acd22003-04-09 20:59:48 +10002029 logit("channel %d: rcvd big packet %d, maxpack %d",
Damien Miller33b13562000-04-04 14:38:59 +10002030 c->self, data_len, c->local_maxpacket);
2031 }
2032 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002033 logit("channel %d: rcvd too much data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002034 c->self, data_len, c->local_window);
2035 xfree(data);
2036 return;
2037 }
2038 c->local_window -= data_len;
Damien Miller33b13562000-04-04 14:38:59 +10002039 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002040 packet_check_eom();
Damien Millerd27b9472005-12-13 19:29:02 +11002041 if (c->datagram)
2042 buffer_put_string(&c->output, data, data_len);
2043 else
2044 buffer_append(&c->output, data, data_len);
Damien Miller95def091999-11-25 00:26:21 +11002045 xfree(data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002046}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002047
Damien Millerd79b4242006-03-31 23:11:44 +11002048/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002049void
Damien Miller630d6f42002-01-22 23:17:30 +11002050channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002051{
2052 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002053 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002054 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002055 Channel *c;
2056
2057 /* Get the channel number and verify it. */
2058 id = packet_get_int();
2059 c = channel_lookup(id);
2060
2061 if (c == NULL)
2062 packet_disconnect("Received extended_data for bad channel %d.", id);
2063 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002064 logit("channel %d: ext data for non open", id);
Damien Miller33b13562000-04-04 14:38:59 +10002065 return;
2066 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002067 if (c->flags & CHAN_EOF_RCVD) {
2068 if (datafellows & SSH_BUG_EXTEOF)
2069 debug("channel %d: accepting ext data after eof", id);
2070 else
2071 packet_disconnect("Received extended_data after EOF "
2072 "on channel %d.", id);
2073 }
Damien Miller33b13562000-04-04 14:38:59 +10002074 tcode = packet_get_int();
2075 if (c->efd == -1 ||
2076 c->extended_usage != CHAN_EXTENDED_WRITE ||
2077 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002078 logit("channel %d: bad ext data", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002079 return;
2080 }
2081 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002082 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002083 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002084 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002085 c->self, data_len, c->local_window);
2086 xfree(data);
2087 return;
2088 }
Damien Millerd3444942000-09-30 14:20:03 +11002089 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002090 c->local_window -= data_len;
2091 buffer_append(&c->extended, data, data_len);
2092 xfree(data);
2093}
2094
Damien Millerd79b4242006-03-31 23:11:44 +11002095/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002096void
Damien Miller630d6f42002-01-22 23:17:30 +11002097channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002098{
2099 int id;
2100 Channel *c;
2101
Damien Millerb38eff82000-04-01 11:09:21 +10002102 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002103 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002104 c = channel_lookup(id);
2105 if (c == NULL)
2106 packet_disconnect("Received ieof for nonexistent channel %d.", id);
2107 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002108
2109 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002110 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002111 debug("channel %d: FORCE input drain", c->self);
2112 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002113 if (buffer_len(&c->input) == 0)
2114 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002115 }
2116
Damien Millerb38eff82000-04-01 11:09:21 +10002117}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002118
Damien Millerd79b4242006-03-31 23:11:44 +11002119/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002120void
Damien Miller630d6f42002-01-22 23:17:30 +11002121channel_input_close(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002122{
Damien Millerb38eff82000-04-01 11:09:21 +10002123 int id;
2124 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002125
Damien Millerb38eff82000-04-01 11:09:21 +10002126 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002127 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002128 c = channel_lookup(id);
2129 if (c == NULL)
2130 packet_disconnect("Received close for nonexistent channel %d.", id);
Damien Miller5428f641999-11-25 11:54:57 +11002131
2132 /*
2133 * Send a confirmation that we have closed the channel and no more
2134 * data is coming for it.
2135 */
Damien Miller95def091999-11-25 00:26:21 +11002136 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10002137 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11002138 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002139
Damien Miller5428f641999-11-25 11:54:57 +11002140 /*
2141 * If the channel is in closed state, we have sent a close request,
2142 * and the other side will eventually respond with a confirmation.
2143 * Thus, we cannot free the channel here, because then there would be
2144 * no-one to receive the confirmation. The channel gets freed when
2145 * the confirmation arrives.
2146 */
Damien Millerb38eff82000-04-01 11:09:21 +10002147 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11002148 /*
2149 * Not a closed channel - mark it as draining, which will
2150 * cause it to be freed later.
2151 */
Damien Miller76765c02002-01-22 23:21:15 +11002152 buffer_clear(&c->input);
Damien Millerb38eff82000-04-01 11:09:21 +10002153 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11002154 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002155}
2156
Damien Millerb38eff82000-04-01 11:09:21 +10002157/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Millerd79b4242006-03-31 23:11:44 +11002158/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002159void
Damien Miller630d6f42002-01-22 23:17:30 +11002160channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002161{
Damien Millerb38eff82000-04-01 11:09:21 +10002162 int id = packet_get_int();
2163 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002164
Damien Miller48b03fc2002-01-22 23:11:40 +11002165 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002166 if (c == NULL)
2167 packet_disconnect("Received oclose for nonexistent channel %d.", id);
2168 chan_rcvd_oclose(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002169}
2170
Damien Millerd79b4242006-03-31 23:11:44 +11002171/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002172void
Damien Miller630d6f42002-01-22 23:17:30 +11002173channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002174{
2175 int id = packet_get_int();
2176 Channel *c = channel_lookup(id);
2177
Damien Miller48b03fc2002-01-22 23:11:40 +11002178 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002179 if (c == NULL)
2180 packet_disconnect("Received close confirmation for "
2181 "out-of-range channel %d.", id);
2182 if (c->type != SSH_CHANNEL_CLOSED)
2183 packet_disconnect("Received close confirmation for "
2184 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002185 channel_free(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002186}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002187
Damien Millerd79b4242006-03-31 23:11:44 +11002188/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002189void
Damien Miller630d6f42002-01-22 23:17:30 +11002190channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002191{
Damien Millerb38eff82000-04-01 11:09:21 +10002192 int id, remote_id;
2193 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002194
Damien Millerb38eff82000-04-01 11:09:21 +10002195 id = packet_get_int();
2196 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002197
Damien Millerb38eff82000-04-01 11:09:21 +10002198 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2199 packet_disconnect("Received open confirmation for "
2200 "non-opening channel %d.", id);
2201 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002202 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002203 c->remote_id = remote_id;
2204 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002205
2206 if (compat20) {
2207 c->remote_window = packet_get_int();
2208 c->remote_maxpacket = packet_get_int();
Damien Miller67f0bc02002-02-05 12:23:08 +11002209 if (c->confirm) {
Damien Millerd3444942000-09-30 14:20:03 +11002210 debug2("callback start");
Damien Miller0e220db2004-06-15 10:34:08 +10002211 c->confirm(c->self, c->confirm_ctx);
Damien Millerd3444942000-09-30 14:20:03 +11002212 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002213 }
Damien Millerfbdeece2003-09-02 22:52:31 +10002214 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
Damien Miller33b13562000-04-04 14:38:59 +10002215 c->remote_window, c->remote_maxpacket);
2216 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002217 packet_check_eom();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002218}
2219
Ben Lindstrombba81212001-06-25 05:01:22 +00002220static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002221reason2txt(int reason)
2222{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002223 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002224 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2225 return "administratively prohibited";
2226 case SSH2_OPEN_CONNECT_FAILED:
2227 return "connect failed";
2228 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2229 return "unknown channel type";
2230 case SSH2_OPEN_RESOURCE_SHORTAGE:
2231 return "resource shortage";
2232 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002233 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002234}
2235
Damien Millerd79b4242006-03-31 23:11:44 +11002236/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002237void
Damien Miller630d6f42002-01-22 23:17:30 +11002238channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002239{
Kevin Steves12057502001-02-05 14:54:34 +00002240 int id, reason;
2241 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002242 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002243
Damien Millerb38eff82000-04-01 11:09:21 +10002244 id = packet_get_int();
2245 c = channel_lookup(id);
2246
2247 if (c==NULL || c->type != SSH_CHANNEL_OPENING)
2248 packet_disconnect("Received open failure for "
2249 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002250 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00002251 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00002252 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00002253 msg = packet_get_string(NULL);
2254 lang = packet_get_string(NULL);
2255 }
Damien Miller996acd22003-04-09 20:59:48 +10002256 logit("channel %d: open failed: %s%s%s", id,
Ben Lindstrom69128662001-05-08 20:07:39 +00002257 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Kevin Steves12057502001-02-05 14:54:34 +00002258 if (msg != NULL)
2259 xfree(msg);
2260 if (lang != NULL)
2261 xfree(lang);
Damien Miller33b13562000-04-04 14:38:59 +10002262 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002263 packet_check_eom();
Damien Miller95def091999-11-25 00:26:21 +11002264 /* Free the channel. This will also close the socket. */
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002265 channel_free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002266}
2267
Damien Millerd79b4242006-03-31 23:11:44 +11002268/* ARGSUSED */
Damien Miller33b13562000-04-04 14:38:59 +10002269void
Damien Miller630d6f42002-01-22 23:17:30 +11002270channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002271{
2272 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002273 int id;
2274 u_int adjust;
Damien Miller33b13562000-04-04 14:38:59 +10002275
2276 if (!compat20)
2277 return;
2278
2279 /* Get the channel number and verify it. */
2280 id = packet_get_int();
2281 c = channel_lookup(id);
2282
Damien Millerd47c62a2005-12-13 19:33:57 +11002283 if (c == NULL) {
2284 logit("Received window adjust for non-open channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002285 return;
2286 }
2287 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002288 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002289 debug2("channel %d: rcvd adjust %u", id, adjust);
Damien Miller33b13562000-04-04 14:38:59 +10002290 c->remote_window += adjust;
2291}
2292
Damien Millerd79b4242006-03-31 23:11:44 +11002293/* ARGSUSED */
Damien Miller4af51302000-04-16 11:18:38 +10002294void
Damien Miller630d6f42002-01-22 23:17:30 +11002295channel_input_port_open(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002296{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002297 Channel *c = NULL;
2298 u_short host_port;
2299 char *host, *originator_string;
2300 int remote_id, sock = -1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002301
Ben Lindstrome9c99912001-06-09 00:41:05 +00002302 remote_id = packet_get_int();
2303 host = packet_get_string(NULL);
2304 host_port = packet_get_int();
2305
2306 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2307 originator_string = packet_get_string(NULL);
2308 } else {
2309 originator_string = xstrdup("unknown (remote did not supply name)");
2310 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002311 packet_check_eom();
Ben Lindstrome9c99912001-06-09 00:41:05 +00002312 sock = channel_connect_to(host, host_port);
2313 if (sock != -1) {
2314 c = channel_new("connected socket",
2315 SSH_CHANNEL_CONNECTING, sock, sock, -1, 0, 0, 0,
2316 originator_string, 1);
Damien Miller699d0032002-02-08 22:07:16 +11002317 c->remote_id = remote_id;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002318 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002319 xfree(originator_string);
Ben Lindstrome9c99912001-06-09 00:41:05 +00002320 if (c == NULL) {
2321 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2322 packet_put_int(remote_id);
2323 packet_send();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002324 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002325 xfree(host);
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002326}
2327
2328
Ben Lindstrome9c99912001-06-09 00:41:05 +00002329/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002330
Ben Lindstrom908afed2001-10-03 17:34:59 +00002331void
2332channel_set_af(int af)
2333{
2334 IPv4or6 = af;
2335}
2336
Damien Millerb16461c2002-01-22 23:29:22 +11002337static int
2338channel_setup_fwd_listener(int type, const char *listen_addr, u_short listen_port,
2339 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002340{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002341 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11002342 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11002343 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002344 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11002345 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002346
Damien Millerb16461c2002-01-22 23:29:22 +11002347 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2348 listen_addr : host_to_connect;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002349 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00002350
Damien Millerb16461c2002-01-22 23:29:22 +11002351 if (host == NULL) {
2352 error("No forward host name.");
Damien Millera7270302005-07-06 09:36:05 +10002353 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002354 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002355 if (strlen(host) > SSH_CHANNEL_PATH_LEN - 1) {
Kevin Steves12057502001-02-05 14:54:34 +00002356 error("Forward host name too long.");
Damien Millera7270302005-07-06 09:36:05 +10002357 return 0;
Kevin Steves12057502001-02-05 14:54:34 +00002358 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002359
Damien Miller5428f641999-11-25 11:54:57 +11002360 /*
Damien Millerf91ee4c2005-03-01 21:24:33 +11002361 * Determine whether or not a port forward listens to loopback,
Darren Tucker47eede72005-03-14 23:08:12 +11002362 * specified address or wildcard. On the client, a specified bind
2363 * address will always override gateway_ports. On the server, a
2364 * gateway_ports of 1 (``yes'') will override the client's
2365 * specification and force a wildcard bind, whereas a value of 2
2366 * (``clientspecified'') will bind to whatever address the client
Damien Millerf91ee4c2005-03-01 21:24:33 +11002367 * asked for.
2368 *
2369 * Special-case listen_addrs are:
2370 *
2371 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2372 * "" (empty string), "*" -> wildcard v4/v6
2373 * "localhost" -> loopback v4/v6
2374 */
2375 addr = NULL;
2376 if (listen_addr == NULL) {
2377 /* No address specified: default to gateway_ports setting */
2378 if (gateway_ports)
2379 wildcard = 1;
2380 } else if (gateway_ports || is_client) {
2381 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2382 strcmp(listen_addr, "0.0.0.0") == 0) ||
2383 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
2384 (!is_client && gateway_ports == 1))
2385 wildcard = 1;
2386 else if (strcmp(listen_addr, "localhost") != 0)
2387 addr = listen_addr;
2388 }
2389
2390 debug3("channel_setup_fwd_listener: type %d wildcard %d addr %s",
2391 type, wildcard, (addr == NULL) ? "NULL" : addr);
2392
2393 /*
Damien Miller34132e52000-01-14 15:45:46 +11002394 * getaddrinfo returns a loopback address if the hostname is
2395 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002396 */
Damien Miller34132e52000-01-14 15:45:46 +11002397 memset(&hints, 0, sizeof(hints));
2398 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002399 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002400 hints.ai_socktype = SOCK_STREAM;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002401 snprintf(strport, sizeof strport, "%d", listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002402 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2403 if (addr == NULL) {
2404 /* This really shouldn't happen */
2405 packet_disconnect("getaddrinfo: fatal error: %s",
2406 gai_strerror(r));
2407 } else {
Damien Millera7270302005-07-06 09:36:05 +10002408 error("channel_setup_fwd_listener: "
Damien Millerf91ee4c2005-03-01 21:24:33 +11002409 "getaddrinfo(%.64s): %s", addr, gai_strerror(r));
2410 }
Damien Millera7270302005-07-06 09:36:05 +10002411 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002412 }
Damien Miller5428f641999-11-25 11:54:57 +11002413
Damien Miller34132e52000-01-14 15:45:46 +11002414 for (ai = aitop; ai; ai = ai->ai_next) {
2415 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2416 continue;
2417 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2418 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Millerb16461c2002-01-22 23:29:22 +11002419 error("channel_setup_fwd_listener: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002420 continue;
2421 }
2422 /* Create a port to listen for the host. */
Damien Miller2372ace2003-05-14 13:42:23 +10002423 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002424 if (sock < 0) {
2425 /* this is no error since kernel may not support ipv6 */
2426 verbose("socket: %.100s", strerror(errno));
2427 continue;
2428 }
Damien Miller5e7fd072005-11-05 14:53:39 +11002429
2430 channel_set_reuseaddr(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10002431
Damien Miller34132e52000-01-14 15:45:46 +11002432 debug("Local forwarding listening on %s port %s.", ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11002433
Damien Miller34132e52000-01-14 15:45:46 +11002434 /* Bind the socket to the address. */
2435 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
2436 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11002437 if (!ai->ai_next)
2438 error("bind: %.100s", strerror(errno));
2439 else
2440 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00002441
Damien Miller34132e52000-01-14 15:45:46 +11002442 close(sock);
2443 continue;
2444 }
2445 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11002446 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002447 error("listen: %.100s", strerror(errno));
2448 close(sock);
2449 continue;
2450 }
2451 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00002452 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10002453 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002454 0, "port listener", 1);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002455 strlcpy(c->path, host, sizeof(c->path));
2456 c->host_port = port_to_connect;
2457 c->listening_port = listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11002458 success = 1;
2459 }
2460 if (success == 0)
Damien Millerb16461c2002-01-22 23:29:22 +11002461 error("channel_setup_fwd_listener: cannot listen to port: %d",
Kevin Steves12057502001-02-05 14:54:34 +00002462 listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11002463 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00002464 return success;
Damien Miller95def091999-11-25 00:26:21 +11002465}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002466
Darren Tuckere7066df2004-05-24 10:18:05 +10002467int
2468channel_cancel_rport_listener(const char *host, u_short port)
2469{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002470 u_int i;
2471 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10002472
Darren Tucker47eede72005-03-14 23:08:12 +11002473 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10002474 Channel *c = channels[i];
2475
2476 if (c != NULL && c->type == SSH_CHANNEL_RPORT_LISTENER &&
2477 strncmp(c->path, host, sizeof(c->path)) == 0 &&
Darren Tuckerfc959702004-07-17 16:12:08 +10002478 c->listening_port == port) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10002479 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10002480 channel_free(c);
2481 found = 1;
2482 }
2483 }
2484
2485 return (found);
2486}
2487
Damien Millerb16461c2002-01-22 23:29:22 +11002488/* protocol local port fwd, used by ssh (and sshd in v1) */
2489int
Damien Millerf91ee4c2005-03-01 21:24:33 +11002490channel_setup_local_fwd_listener(const char *listen_host, u_short listen_port,
Damien Millerb16461c2002-01-22 23:29:22 +11002491 const char *host_to_connect, u_short port_to_connect, int gateway_ports)
2492{
2493 return channel_setup_fwd_listener(SSH_CHANNEL_PORT_LISTENER,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002494 listen_host, listen_port, host_to_connect, port_to_connect,
2495 gateway_ports);
Damien Millerb16461c2002-01-22 23:29:22 +11002496}
2497
2498/* protocol v2 remote port fwd, used by sshd */
2499int
2500channel_setup_remote_fwd_listener(const char *listen_address,
2501 u_short listen_port, int gateway_ports)
2502{
2503 return channel_setup_fwd_listener(SSH_CHANNEL_RPORT_LISTENER,
2504 listen_address, listen_port, NULL, 0, gateway_ports);
2505}
2506
Damien Miller5428f641999-11-25 11:54:57 +11002507/*
2508 * Initiate forwarding of connections to port "port" on remote host through
2509 * the secure channel to host:port from local side.
2510 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002511
Darren Tuckere7d4b192006-07-12 22:17:10 +10002512int
Damien Millerf91ee4c2005-03-01 21:24:33 +11002513channel_request_remote_forwarding(const char *listen_host, u_short listen_port,
Damien Miller0bc1bd82000-11-13 22:57:25 +11002514 const char *host_to_connect, u_short port_to_connect)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002515{
Damien Millerdff50992002-01-22 23:16:32 +11002516 int type, success = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002517
Damien Miller95def091999-11-25 00:26:21 +11002518 /* Record locally that connection to this host/port is permitted. */
2519 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2520 fatal("channel_request_remote_forwarding: too many forwards");
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002521
Damien Miller95def091999-11-25 00:26:21 +11002522 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10002523 if (compat20) {
Damien Millerf91ee4c2005-03-01 21:24:33 +11002524 const char *address_to_bind;
2525 if (listen_host == NULL)
2526 address_to_bind = "localhost";
2527 else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0)
2528 address_to_bind = "";
2529 else
2530 address_to_bind = listen_host;
2531
Damien Miller33b13562000-04-04 14:38:59 +10002532 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2533 packet_put_cstring("tcpip-forward");
Damien Miller2797f7f2002-04-23 21:09:44 +10002534 packet_put_char(1); /* boolean: want reply */
Damien Miller33b13562000-04-04 14:38:59 +10002535 packet_put_cstring(address_to_bind);
2536 packet_put_int(listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002537 packet_send();
2538 packet_write_wait();
2539 /* Assume that server accepts the request */
2540 success = 1;
Damien Miller33b13562000-04-04 14:38:59 +10002541 } else {
2542 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller33b13562000-04-04 14:38:59 +10002543 packet_put_int(listen_port);
Damien Miller8bb73be2000-04-19 16:26:12 +10002544 packet_put_cstring(host_to_connect);
2545 packet_put_int(port_to_connect);
Damien Miller33b13562000-04-04 14:38:59 +10002546 packet_send();
2547 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002548
2549 /* Wait for response from the remote side. */
Damien Millerdff50992002-01-22 23:16:32 +11002550 type = packet_read();
Damien Miller0bc1bd82000-11-13 22:57:25 +11002551 switch (type) {
2552 case SSH_SMSG_SUCCESS:
2553 success = 1;
2554 break;
2555 case SSH_SMSG_FAILURE:
Damien Miller0bc1bd82000-11-13 22:57:25 +11002556 break;
2557 default:
2558 /* Unknown packet */
2559 packet_disconnect("Protocol error for port forward request:"
2560 "received packet type %d.", type);
2561 }
2562 }
2563 if (success) {
2564 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host_to_connect);
2565 permitted_opens[num_permitted_opens].port_to_connect = port_to_connect;
2566 permitted_opens[num_permitted_opens].listen_port = listen_port;
2567 num_permitted_opens++;
Damien Miller33b13562000-04-04 14:38:59 +10002568 }
Darren Tuckere7d4b192006-07-12 22:17:10 +10002569 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002570}
2571
Damien Miller5428f641999-11-25 11:54:57 +11002572/*
Darren Tuckerfc959702004-07-17 16:12:08 +10002573 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10002574 * local side.
2575 */
Darren Tuckere7066df2004-05-24 10:18:05 +10002576void
Damien Millerf91ee4c2005-03-01 21:24:33 +11002577channel_request_rforward_cancel(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10002578{
2579 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10002580
2581 if (!compat20)
2582 return;
2583
2584 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckerfc959702004-07-17 16:12:08 +10002585 if (permitted_opens[i].host_to_connect != NULL &&
Darren Tuckere7066df2004-05-24 10:18:05 +10002586 permitted_opens[i].listen_port == port)
2587 break;
2588 }
2589 if (i >= num_permitted_opens) {
2590 debug("%s: requested forward not found", __func__);
2591 return;
2592 }
2593 packet_start(SSH2_MSG_GLOBAL_REQUEST);
2594 packet_put_cstring("cancel-tcpip-forward");
2595 packet_put_char(0);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002596 packet_put_cstring(host == NULL ? "" : host);
Darren Tuckere7066df2004-05-24 10:18:05 +10002597 packet_put_int(port);
2598 packet_send();
2599
2600 permitted_opens[i].listen_port = 0;
2601 permitted_opens[i].port_to_connect = 0;
Damien Miller0a0176e2005-11-05 15:07:59 +11002602 xfree(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10002603 permitted_opens[i].host_to_connect = NULL;
2604}
2605
2606/*
Damien Miller5428f641999-11-25 11:54:57 +11002607 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
2608 * listening for the port, and sends back a success reply (or disconnect
Darren Tuckere7d4b192006-07-12 22:17:10 +10002609 * message if there was an error).
Damien Miller5428f641999-11-25 11:54:57 +11002610 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002611int
Damien Millere247cc42000-05-07 12:03:14 +10002612channel_input_port_forward_request(int is_root, int gateway_ports)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002613{
Damien Milleraae6c611999-12-06 11:47:28 +11002614 u_short port, host_port;
Darren Tuckere7d4b192006-07-12 22:17:10 +10002615 int success = 0;
Damien Miller95def091999-11-25 00:26:21 +11002616 char *hostname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002617
Damien Miller95def091999-11-25 00:26:21 +11002618 /* Get arguments from the packet. */
2619 port = packet_get_int();
2620 hostname = packet_get_string(NULL);
2621 host_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002622
Damien Millerbac2d8a2000-09-05 16:13:06 +11002623#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11002624 /*
2625 * Check that an unprivileged user is not trying to forward a
2626 * privileged port.
2627 */
Damien Miller95def091999-11-25 00:26:21 +11002628 if (port < IPPORT_RESERVED && !is_root)
Darren Tucker9189ff82003-07-03 13:52:04 +10002629 packet_disconnect(
2630 "Requested forwarding of port %d but user is not root.",
2631 port);
2632 if (host_port == 0)
2633 packet_disconnect("Dynamic forwarding denied.");
Damien Millerbac2d8a2000-09-05 16:13:06 +11002634#endif
Darren Tucker9189ff82003-07-03 13:52:04 +10002635
Damien Miller0bc1bd82000-11-13 22:57:25 +11002636 /* Initiate forwarding */
Darren Tuckere7d4b192006-07-12 22:17:10 +10002637 success = channel_setup_local_fwd_listener(NULL, port, hostname,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002638 host_port, gateway_ports);
Damien Miller95def091999-11-25 00:26:21 +11002639
2640 /* Free the argument string. */
2641 xfree(hostname);
Darren Tuckere7d4b192006-07-12 22:17:10 +10002642
2643 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002644}
2645
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002646/*
2647 * Permits opening to any host/port if permitted_opens[] is empty. This is
2648 * usually called by the server, because the user could connect to any port
2649 * anyway, and the server has no way to know but to trust the client anyway.
2650 */
2651void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002652channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002653{
2654 if (num_permitted_opens == 0)
2655 all_opens_permitted = 1;
2656}
2657
Ben Lindstroma3700052001-04-05 23:26:32 +00002658void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002659channel_add_permitted_opens(char *host, int port)
2660{
2661 if (num_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
Darren Tuckere7d4b192006-07-12 22:17:10 +10002662 fatal("channel_add_permitted_opens: too many forwards");
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002663 debug("allow port forwarding to host %s port %d", host, port);
2664
2665 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
2666 permitted_opens[num_permitted_opens].port_to_connect = port;
2667 num_permitted_opens++;
2668
2669 all_opens_permitted = 0;
2670}
2671
Damien Millera765cf42006-07-24 14:08:13 +10002672int
Damien Miller9b439df2006-07-24 14:04:00 +10002673channel_add_adm_permitted_opens(char *host, int port)
2674{
2675 if (num_adm_permitted_opens >= SSH_MAX_FORWARDS_PER_DIRECTION)
2676 fatal("channel_add_adm_permitted_opens: too many forwards");
Damien Millera765cf42006-07-24 14:08:13 +10002677 debug("config allows port forwarding to host %s port %d", host, port);
Damien Miller9b439df2006-07-24 14:04:00 +10002678
2679 permitted_adm_opens[num_adm_permitted_opens].host_to_connect
2680 = xstrdup(host);
2681 permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
Damien Millera765cf42006-07-24 14:08:13 +10002682 return ++num_adm_permitted_opens;
Damien Miller9b439df2006-07-24 14:04:00 +10002683}
2684
2685void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002686channel_clear_permitted_opens(void)
2687{
2688 int i;
2689
2690 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002691 if (permitted_opens[i].host_to_connect != NULL)
2692 xfree(permitted_opens[i].host_to_connect);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002693 num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10002694}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002695
Damien Miller9b439df2006-07-24 14:04:00 +10002696void
2697channel_clear_adm_permitted_opens(void)
2698{
2699 int i;
2700
2701 for (i = 0; i < num_adm_permitted_opens; i++)
2702 if (permitted_adm_opens[i].host_to_connect != NULL)
2703 xfree(permitted_adm_opens[i].host_to_connect);
2704 num_adm_permitted_opens = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002705}
2706
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002707/* return socket to remote host, port */
Ben Lindstrombba81212001-06-25 05:01:22 +00002708static int
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002709connect_to(const char *host, u_short port)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002710{
Damien Miller34132e52000-01-14 15:45:46 +11002711 struct addrinfo hints, *ai, *aitop;
2712 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
2713 int gaierr;
Damien Millerb38eff82000-04-01 11:09:21 +10002714 int sock = -1;
Damien Miller95def091999-11-25 00:26:21 +11002715
Damien Miller34132e52000-01-14 15:45:46 +11002716 memset(&hints, 0, sizeof(hints));
2717 hints.ai_family = IPv4or6;
2718 hints.ai_socktype = SOCK_STREAM;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002719 snprintf(strport, sizeof strport, "%d", port);
Damien Miller34132e52000-01-14 15:45:46 +11002720 if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002721 error("connect_to %.100s: unknown host (%s)", host,
2722 gai_strerror(gaierr));
Damien Millerb38eff82000-04-01 11:09:21 +10002723 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002724 }
Damien Miller34132e52000-01-14 15:45:46 +11002725 for (ai = aitop; ai; ai = ai->ai_next) {
2726 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2727 continue;
2728 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
2729 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002730 error("connect_to: getnameinfo failed");
Damien Miller34132e52000-01-14 15:45:46 +11002731 continue;
2732 }
Damien Miller2372ace2003-05-14 13:42:23 +10002733 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002734 if (sock < 0) {
Damien Millerb46b9f32003-01-10 21:45:12 +11002735 if (ai->ai_next == NULL)
2736 error("socket: %.100s", strerror(errno));
2737 else
2738 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002739 continue;
2740 }
Damien Miller232711f2004-06-15 10:35:30 +10002741 if (set_nonblock(sock) == -1)
2742 fatal("%s: set_nonblock(%d)", __func__, sock);
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002743 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0 &&
2744 errno != EINPROGRESS) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002745 error("connect_to %.100s port %s: %.100s", ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11002746 strerror(errno));
2747 close(sock);
Kevin Stevesef4eea92001-02-05 12:42:17 +00002748 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11002749 }
2750 break; /* success */
2751
2752 }
2753 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002754 if (!ai) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002755 error("connect_to %.100s port %d: failed.", host, port);
Damien Millerb38eff82000-04-01 11:09:21 +10002756 return -1;
2757 }
2758 /* success */
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +00002759 set_nodelay(sock);
Damien Millerb38eff82000-04-01 11:09:21 +10002760 return sock;
2761}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002762
Damien Miller0bc1bd82000-11-13 22:57:25 +11002763int
Ben Lindstrom173e6462001-07-04 05:15:15 +00002764channel_connect_by_listen_address(u_short listen_port)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002765{
2766 int i;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002767
Damien Miller0bc1bd82000-11-13 22:57:25 +11002768 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002769 if (permitted_opens[i].host_to_connect != NULL &&
2770 permitted_opens[i].listen_port == listen_port)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002771 return connect_to(
Damien Miller0bc1bd82000-11-13 22:57:25 +11002772 permitted_opens[i].host_to_connect,
2773 permitted_opens[i].port_to_connect);
Ben Lindstromc72745a2000-12-02 19:03:54 +00002774 error("WARNING: Server requests forwarding for unknown listen_port %d",
2775 listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002776 return -1;
2777}
Damien Millerb38eff82000-04-01 11:09:21 +10002778
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002779/* Check if connecting to that port is permitted and connect. */
2780int
2781channel_connect_to(const char *host, u_short port)
2782{
Damien Miller9b439df2006-07-24 14:04:00 +10002783 int i, permit, permit_adm = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002784
2785 permit = all_opens_permitted;
2786 if (!permit) {
2787 for (i = 0; i < num_permitted_opens; i++)
Darren Tuckere7066df2004-05-24 10:18:05 +10002788 if (permitted_opens[i].host_to_connect != NULL &&
2789 permitted_opens[i].port_to_connect == port &&
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002790 strcmp(permitted_opens[i].host_to_connect, host) == 0)
2791 permit = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002792 }
Damien Miller9b439df2006-07-24 14:04:00 +10002793
2794 if (num_adm_permitted_opens > 0) {
2795 permit_adm = 0;
2796 for (i = 0; i < num_adm_permitted_opens; i++)
2797 if (permitted_adm_opens[i].host_to_connect != NULL &&
2798 permitted_adm_opens[i].port_to_connect == port &&
2799 strcmp(permitted_adm_opens[i].host_to_connect, host)
2800 == 0)
2801 permit_adm = 1;
2802 }
2803
2804 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10002805 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00002806 "but the request was denied.", host, port);
2807 return -1;
2808 }
2809 return connect_to(host, port);
2810}
2811
Damien Miller0e220db2004-06-15 10:34:08 +10002812void
2813channel_send_window_changes(void)
2814{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002815 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10002816 struct winsize ws;
2817
2818 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11002819 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10002820 channels[i]->type != SSH_CHANNEL_OPEN)
2821 continue;
2822 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
2823 continue;
2824 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11002825 packet_put_int((u_int)ws.ws_col);
2826 packet_put_int((u_int)ws.ws_row);
2827 packet_put_int((u_int)ws.ws_xpixel);
2828 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10002829 packet_send();
2830 }
2831}
2832
Ben Lindstrome9c99912001-06-09 00:41:05 +00002833/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002834
Damien Miller5428f641999-11-25 11:54:57 +11002835/*
2836 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002837 * Returns 0 and a suitable display number for the DISPLAY variable
2838 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11002839 */
Kevin Steves366298c2001-12-19 17:58:01 +00002840int
Damien Miller95c249f2002-02-05 12:11:34 +11002841x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10002842 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002843{
Damien Millere7378562001-12-21 14:58:35 +11002844 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11002845 int display_number, sock;
2846 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11002847 struct addrinfo hints, *ai, *aitop;
2848 char strport[NI_MAXSERV];
2849 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002850
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002851 if (chanids == NULL)
2852 return -1;
2853
Damien Millera34a28b1999-12-14 10:47:15 +11002854 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11002855 display_number < MAX_DISPLAYS;
2856 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11002857 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11002858 memset(&hints, 0, sizeof(hints));
2859 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11002860 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11002861 hints.ai_socktype = SOCK_STREAM;
2862 snprintf(strport, sizeof strport, "%d", port);
2863 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
2864 error("getaddrinfo: %.100s", gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00002865 return -1;
Damien Miller95def091999-11-25 00:26:21 +11002866 }
Damien Miller34132e52000-01-14 15:45:46 +11002867 for (ai = aitop; ai; ai = ai->ai_next) {
2868 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
2869 continue;
Damien Miller2372ace2003-05-14 13:42:23 +10002870 sock = socket(ai->ai_family, ai->ai_socktype,
2871 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11002872 if (sock < 0) {
Damien Miller89d97962000-10-14 12:37:19 +11002873 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)) {
Damien Millere2192732000-01-17 13:22:55 +11002874 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10002875 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00002876 return -1;
Damien Millere2192732000-01-17 13:22:55 +11002877 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11002878 debug("x11_create_display_inet: Socket family %d not supported",
2879 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11002880 continue;
2881 }
Damien Miller34132e52000-01-14 15:45:46 +11002882 }
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002883#ifdef IPV6_V6ONLY
2884 if (ai->ai_family == AF_INET6) {
2885 int on = 1;
2886 if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0)
2887 error("setsockopt IPV6_V6ONLY: %.100s", strerror(errno));
2888 }
2889#endif
Damien Miller5e7fd072005-11-05 14:53:39 +11002890 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11002891 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10002892 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002893 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11002894
2895 if (ai->ai_next)
2896 continue;
2897
Damien Miller34132e52000-01-14 15:45:46 +11002898 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11002899 close(socks[n]);
2900 }
2901 num_socks = 0;
2902 break;
2903 }
2904 socks[num_socks++] = sock;
Damien Miller7bcb0892000-03-11 20:45:40 +11002905#ifndef DONT_TRY_OTHER_AF
Damien Miller34132e52000-01-14 15:45:46 +11002906 if (num_socks == NUM_SOCKS)
2907 break;
Damien Miller7bcb0892000-03-11 20:45:40 +11002908#else
Kevin Stevesdf75dd22002-06-04 20:52:19 +00002909 if (x11_use_localhost) {
2910 if (num_socks == NUM_SOCKS)
2911 break;
2912 } else {
2913 break;
2914 }
Damien Miller7bcb0892000-03-11 20:45:40 +11002915#endif
Damien Miller95def091999-11-25 00:26:21 +11002916 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00002917 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11002918 if (num_socks > 0)
2919 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002920 }
Damien Miller95def091999-11-25 00:26:21 +11002921 if (display_number >= MAX_DISPLAYS) {
2922 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00002923 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002924 }
Damien Miller95def091999-11-25 00:26:21 +11002925 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11002926 for (n = 0; n < num_socks; n++) {
2927 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11002928 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11002929 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11002930 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00002931 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11002932 }
Damien Miller95def091999-11-25 00:26:21 +11002933 }
Damien Miller34132e52000-01-14 15:45:46 +11002934
Damien Miller34132e52000-01-14 15:45:46 +11002935 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11002936 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11002937 for (n = 0; n < num_socks; n++) {
2938 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11002939 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10002940 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
2941 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10002942 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11002943 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002944 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11002945 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10002946 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002947
Kevin Steves366298c2001-12-19 17:58:01 +00002948 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00002949 *display_numberp = display_number;
2950 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002951}
2952
Ben Lindstrombba81212001-06-25 05:01:22 +00002953static int
Ben Lindstrom46c16222000-12-22 01:43:59 +00002954connect_local_xsocket(u_int dnr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002955{
Damien Miller95def091999-11-25 00:26:21 +11002956 int sock;
2957 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002958
Damien Miller3afe3752001-12-21 12:39:51 +11002959 sock = socket(AF_UNIX, SOCK_STREAM, 0);
2960 if (sock < 0)
2961 error("socket: %.100s", strerror(errno));
2962 memset(&addr, 0, sizeof(addr));
2963 addr.sun_family = AF_UNIX;
2964 snprintf(addr.sun_path, sizeof addr.sun_path, _PATH_UNIX_X, dnr);
Damien Miller90967402006-03-26 14:07:26 +11002965 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11002966 return sock;
2967 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11002968 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
2969 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002970}
2971
Damien Millerbd483e72000-04-30 10:00:53 +10002972int
2973x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002974{
Damien Miller57c4e872006-03-31 23:11:07 +11002975 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11002976 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10002977 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11002978 struct addrinfo hints, *ai, *aitop;
2979 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11002980 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002981
Damien Miller95def091999-11-25 00:26:21 +11002982 /* Try to open a socket for the local X server. */
2983 display = getenv("DISPLAY");
2984 if (!display) {
2985 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10002986 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002987 }
Damien Miller5428f641999-11-25 11:54:57 +11002988 /*
2989 * Now we decode the value of the DISPLAY variable and make a
2990 * connection to the real X server.
2991 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002992
Damien Miller5428f641999-11-25 11:54:57 +11002993 /*
2994 * Check if it is a unix domain socket. Unix domain displays are in
2995 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
2996 */
Damien Miller95def091999-11-25 00:26:21 +11002997 if (strncmp(display, "unix:", 5) == 0 ||
2998 display[0] == ':') {
2999 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11003000 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11003001 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11003002 display);
Damien Millerbd483e72000-04-30 10:00:53 +10003003 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003004 }
3005 /* Create a socket. */
3006 sock = connect_local_xsocket(display_number);
3007 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10003008 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003009
3010 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10003011 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003012 }
Damien Miller5428f641999-11-25 11:54:57 +11003013 /*
3014 * Connect to an inet socket. The DISPLAY value is supposedly
3015 * hostname:d[.s], where hostname may also be numeric IP address.
3016 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00003017 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11003018 cp = strchr(buf, ':');
3019 if (!cp) {
3020 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10003021 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003022 }
Damien Miller95def091999-11-25 00:26:21 +11003023 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11003024 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11003025 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11003026 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11003027 display);
Damien Millerbd483e72000-04-30 10:00:53 +10003028 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003029 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003030
Damien Miller34132e52000-01-14 15:45:46 +11003031 /* Look up the host address */
3032 memset(&hints, 0, sizeof(hints));
3033 hints.ai_family = IPv4or6;
3034 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11003035 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11003036 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
3037 error("%.100s: unknown host. (%s)", buf, gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10003038 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003039 }
Damien Miller34132e52000-01-14 15:45:46 +11003040 for (ai = aitop; ai; ai = ai->ai_next) {
3041 /* Create a socket. */
Damien Miller2372ace2003-05-14 13:42:23 +10003042 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003043 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10003044 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10003045 continue;
3046 }
3047 /* Connect it to the display. */
3048 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11003049 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10003050 6000 + display_number, strerror(errno));
3051 close(sock);
3052 continue;
3053 }
3054 /* Success */
3055 break;
Damien Miller34132e52000-01-14 15:45:46 +11003056 }
Damien Miller34132e52000-01-14 15:45:46 +11003057 freeaddrinfo(aitop);
3058 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11003059 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11003060 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10003061 return -1;
Damien Miller95def091999-11-25 00:26:21 +11003062 }
Damien Miller398e1cf2002-02-05 11:52:13 +11003063 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10003064 return sock;
3065}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003066
Damien Millerbd483e72000-04-30 10:00:53 +10003067/*
3068 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
3069 * the remote channel number. We should do whatever we want, and respond
3070 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
3071 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003072
Damien Miller8473dd82006-07-24 14:08:32 +10003073/* ARGSUSED */
Damien Millerbd483e72000-04-30 10:00:53 +10003074void
Damien Miller630d6f42002-01-22 23:17:30 +11003075x11_input_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10003076{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003077 Channel *c = NULL;
3078 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10003079 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10003080
3081 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003082
3083 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00003084
3085 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003086 remote_host = packet_get_string(NULL);
3087 } else {
3088 remote_host = xstrdup("unknown (remote did not supply name)");
3089 }
Damien Miller48b03fc2002-01-22 23:11:40 +11003090 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10003091
3092 /* Obtain a connection to the real X display. */
3093 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003094 if (sock != -1) {
3095 /* Allocate a channel for this connection. */
3096 c = channel_new("connected x11 socket",
3097 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
3098 remote_host, 1);
Damien Miller699d0032002-02-08 22:07:16 +11003099 c->remote_id = remote_id;
3100 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003101 }
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003102 xfree(remote_host);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003103 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10003104 /* Send refusal to the remote host. */
3105 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003106 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10003107 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10003108 /* Send a confirmation to the remote host. */
3109 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003110 packet_put_int(remote_id);
3111 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10003112 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003113 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003114}
3115
Damien Miller69b69aa2000-10-28 14:19:58 +11003116/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
Damien Miller8473dd82006-07-24 14:08:32 +10003117/* ARGSUSED */
Damien Miller69b69aa2000-10-28 14:19:58 +11003118void
Damien Miller630d6f42002-01-22 23:17:30 +11003119deny_input_open(int type, u_int32_t seq, void *ctxt)
Damien Miller69b69aa2000-10-28 14:19:58 +11003120{
3121 int rchan = packet_get_int();
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00003122
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00003123 switch (type) {
Damien Miller69b69aa2000-10-28 14:19:58 +11003124 case SSH_SMSG_AGENT_OPEN:
3125 error("Warning: ssh server tried agent forwarding.");
3126 break;
3127 case SSH_SMSG_X11_OPEN:
3128 error("Warning: ssh server tried X11 forwarding.");
3129 break;
3130 default:
Damien Miller630d6f42002-01-22 23:17:30 +11003131 error("deny_input_open: type %d", type);
Damien Miller69b69aa2000-10-28 14:19:58 +11003132 break;
3133 }
Damien Miller5eb137c2005-12-31 16:19:53 +11003134 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller69b69aa2000-10-28 14:19:58 +11003135 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
3136 packet_put_int(rchan);
3137 packet_send();
3138}
3139
Damien Miller5428f641999-11-25 11:54:57 +11003140/*
3141 * Requests forwarding of X11 connections, generates fake authentication
3142 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00003143 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11003144 */
Damien Miller4af51302000-04-16 11:18:38 +10003145void
Damien Miller17e7ed02005-06-17 12:54:33 +10003146x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Millerbd483e72000-04-30 10:00:53 +10003147 const char *proto, const char *data)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003148{
Ben Lindstrom46c16222000-12-22 01:43:59 +00003149 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10003150 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11003151 char *new_data;
3152 int screen_number;
3153 const char *cp;
Darren Tucker3f9fdc72004-06-22 12:56:01 +10003154 u_int32_t rnd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003155
Damien Millerf92c0792005-07-06 09:45:26 +10003156 if (x11_saved_display == NULL)
3157 x11_saved_display = xstrdup(disp);
3158 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10003159 error("x11_request_forwarding_with_spoofing: different "
3160 "$DISPLAY already forwarded");
3161 return;
3162 }
3163
Damien Miller17e7ed02005-06-17 12:54:33 +10003164 cp = disp;
3165 if (disp)
3166 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11003167 if (cp)
3168 cp = strchr(cp, '.');
3169 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11003170 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11003171 else
3172 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003173
Damien Miller13390022005-07-06 09:44:19 +10003174 if (x11_saved_proto == NULL) {
3175 /* Save protocol name. */
3176 x11_saved_proto = xstrdup(proto);
3177 /*
Damien Miller46d38de2005-07-17 17:02:09 +10003178 * Extract real authentication data and generate fake data
Damien Miller13390022005-07-06 09:44:19 +10003179 * of the same length.
3180 */
3181 x11_saved_data = xmalloc(data_len);
3182 x11_fake_data = xmalloc(data_len);
3183 for (i = 0; i < data_len; i++) {
3184 if (sscanf(data + 2 * i, "%2x", &value) != 1)
3185 fatal("x11_request_forwarding: bad "
3186 "authentication data: %.100s", data);
3187 if (i % 4 == 0)
3188 rnd = arc4random();
3189 x11_saved_data[i] = value;
3190 x11_fake_data[i] = rnd & 0xff;
3191 rnd >>= 8;
3192 }
3193 x11_saved_data_len = data_len;
3194 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11003195 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003196
Damien Miller95def091999-11-25 00:26:21 +11003197 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10003198 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003199
Damien Miller95def091999-11-25 00:26:21 +11003200 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10003201 if (compat20) {
3202 channel_request_start(client_session_id, "x11-req", 0);
3203 packet_put_char(0); /* XXX bool single connection */
3204 } else {
3205 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
3206 }
3207 packet_put_cstring(proto);
3208 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11003209 packet_put_int(screen_number);
3210 packet_send();
3211 packet_write_wait();
3212 xfree(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003213}
3214
Ben Lindstrome9c99912001-06-09 00:41:05 +00003215
3216/* -- agent forwarding */
3217
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003218/* Sends a message to the server to request authentication fd forwarding. */
3219
Damien Miller4af51302000-04-16 11:18:38 +10003220void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003221auth_request_forwarding(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003222{
Damien Miller95def091999-11-25 00:26:21 +11003223 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
3224 packet_send();
3225 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003226}