blob: fecd4540e58519f5068ccd2f5a2b9a0ce496e1f9 [file] [log] [blame]
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00001/* $OpenBSD: channels.c,v 1.355 2016/09/30 20:24:46 djm 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
Damien Miller574c41f2006-03-15 11:40:10 +110044#include <sys/types.h>
Damien Miller7acefbb2014-07-18 14:11:24 +100045#include <sys/stat.h>
Damien Millerd7834352006-08-05 12:39:39 +100046#include <sys/ioctl.h>
Damien Miller574c41f2006-03-15 11:40:10 +110047#include <sys/un.h>
Damien Millerefc04e72006-07-10 20:26:27 +100048#include <sys/socket.h>
Damien Miller9aec9192006-08-05 10:57:45 +100049#ifdef HAVE_SYS_TIME_H
50# include <sys/time.h>
51#endif
Damien Millerefc04e72006-07-10 20:26:27 +100052
53#include <netinet/in.h>
54#include <arpa/inet.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110055
Darren Tucker39972492006-07-12 22:22:46 +100056#include <errno.h>
Darren Tucker6e7fe1c2010-01-08 17:07:22 +110057#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100058#include <netdb.h>
Darren Tucker37f92202015-02-23 03:07:24 +110059#ifdef HAVE_STDINT_H
millert@openbsd.orgfd368342015-02-06 23:21:59 +000060#include <stdint.h>
Darren Tucker37f92202015-02-23 03:07:24 +110061#endif
Damien Millera7a73ee2006-08-05 11:37:59 +100062#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100063#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100064#include <string.h>
Damien Miller99bd21e2006-03-15 11:11:28 +110065#include <termios.h>
Damien Millere6b3b612006-07-24 14:01:23 +100066#include <unistd.h>
Damien Millerd7834352006-08-05 12:39:39 +100067#include <stdarg.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068
Damien Millerb84886b2008-05-19 15:05:07 +100069#include "openbsd-compat/sys-queue.h"
Damien Millerd7834352006-08-05 12:39:39 +100070#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071#include "ssh.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000072#include "ssh1.h"
73#include "ssh2.h"
markus@openbsd.org8d057842016-09-30 09:19:13 +000074#include "ssherr.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075#include "packet.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000076#include "log.h"
77#include "misc.h"
Damien Millerd7834352006-08-05 12:39:39 +100078#include "buffer.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079#include "channels.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000081#include "canohost.h"
Damien Miller994cf142000-07-21 10:19:44 +100082#include "key.h"
83#include "authfd.h"
Damien Miller3afe3752001-12-21 12:39:51 +110084#include "pathnames.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Ben Lindstrome9c99912001-06-09 00:41:05 +000086/* -- channel core */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Damien Miller5428f641999-11-25 11:54:57 +110088/*
89 * Pointer to an array containing all allocated channels. The array is
90 * dynamically extended as needed.
91 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +000092static Channel **channels = NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Miller5428f641999-11-25 11:54:57 +110094/*
95 * Size of the channel array. All slots of the array must always be
Ben Lindstrom99c73b32001-05-05 04:09:47 +000096 * initialized (at least the type field); unused slots set to NULL
Damien Miller5428f641999-11-25 11:54:57 +110097 */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +100098static u_int channels_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100099
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * Maximum file descriptor value used in any of the channels. This is
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000102 * updated in channel_new.
Damien Miller5428f641999-11-25 11:54:57 +1100103 */
Damien Miller5e953212001-01-30 09:14:00 +1100104static int channel_max_fd = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Ben Lindstrome9c99912001-06-09 00:41:05 +0000107/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108
Damien Miller5428f641999-11-25 11:54:57 +1100109/*
110 * Data structure for storing which hosts are permitted for forward requests.
111 * The local sides of any remote forwards are stored in this array to prevent
112 * a corrupt remote server from accessing arbitrary TCP/IP ports on our local
113 * network (which might be behind a firewall).
114 */
Damien Miller7acefbb2014-07-18 14:11:24 +1000115/* XXX: streamlocal wants a path instead of host:port */
116/* Overload host_to_connect; we could just make this match Forward */
117/* XXX - can we use listen_host instead of listen_path? */
Damien Miller95def091999-11-25 00:26:21 +1100118typedef struct {
Damien Millerb38eff82000-04-01 11:09:21 +1000119 char *host_to_connect; /* Connect to 'host'. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000120 int port_to_connect; /* Connect to 'port'. */
Damien Miller4b3ed642014-07-02 15:29:40 +1000121 char *listen_host; /* Remote side should listen address. */
Damien Miller7acefbb2014-07-18 14:11:24 +1000122 char *listen_path; /* Remote side should listen path. */
123 int listen_port; /* Remote side should listen port. */
markus@openbsd.org8d057842016-09-30 09:19:13 +0000124 Channel *downstream; /* Downstream mux*/
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125} ForwardPermission;
126
Damien Miller9b439df2006-07-24 14:04:00 +1000127/* List of all permitted host/port pairs to connect by the user. */
Damien Miller232cfb12010-06-26 09:50:30 +1000128static ForwardPermission *permitted_opens = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000129
Damien Miller9b439df2006-07-24 14:04:00 +1000130/* List of all permitted host/port pairs to connect by the admin. */
Damien Miller232cfb12010-06-26 09:50:30 +1000131static ForwardPermission *permitted_adm_opens = NULL;
Damien Miller9b439df2006-07-24 14:04:00 +1000132
133/* Number of permitted host/port pairs in the array permitted by the user. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134static int num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +1000135
136/* Number of permitted host/port pair in the array permitted by the admin. */
137static int num_adm_permitted_opens = 0;
138
Darren Tucker1338b9e2011-10-02 18:57:35 +1100139/* special-case port number meaning allow any port */
140#define FWD_PERMIT_ANY_PORT 0
141
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +0000142/* special-case wildcard meaning allow any host */
143#define FWD_PERMIT_ANY_HOST "*"
144
Damien Miller5428f641999-11-25 11:54:57 +1100145/*
146 * If this is true, all opens are permitted. This is the case on the server
147 * on which we have to trust the client anyway, and the user could do
148 * anything after logging in anyway.
149 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150static int all_opens_permitted = 0;
151
Ben Lindstrome9c99912001-06-09 00:41:05 +0000152
153/* -- X11 forwarding */
154
155/* Maximum number of fake X11 displays to try. */
156#define MAX_DISPLAYS 1000
157
Damien Miller13390022005-07-06 09:44:19 +1000158/* Saved X11 local (client) display. */
159static char *x11_saved_display = NULL;
160
Ben Lindstrome9c99912001-06-09 00:41:05 +0000161/* Saved X11 authentication protocol name. */
162static char *x11_saved_proto = NULL;
163
164/* Saved X11 authentication data. This is the real data. */
165static char *x11_saved_data = NULL;
166static u_int x11_saved_data_len = 0;
167
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000168/* Deadline after which all X11 connections are refused */
169static u_int x11_refuse_time;
170
Ben Lindstrome9c99912001-06-09 00:41:05 +0000171/*
172 * Fake X11 authentication data. This is what the server will be sending us;
173 * we should replace any occurrences of this by the real data.
174 */
Damien Miller4ae97f12006-03-26 14:08:10 +1100175static u_char *x11_fake_data = NULL;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000176static u_int x11_fake_data_len;
177
178
179/* -- agent forwarding */
180
181#define NUM_SOCKS 10
182
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000183/* AF_UNSPEC or AF_INET or AF_INET6 */
Ben Lindstrom908afed2001-10-03 17:34:59 +0000184static int IPv4or6 = AF_UNSPEC;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000185
Ben Lindstrome9c99912001-06-09 00:41:05 +0000186/* helper */
Ben Lindstrombba81212001-06-25 05:01:22 +0000187static void port_open_helper(Channel *c, char *rtype);
markus@openbsd.org8d057842016-09-30 09:19:13 +0000188static const char *channel_rfwd_bind_host(const char *listen_host);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +0000189
Damien Millerbd740252008-05-19 15:37:09 +1000190/* non-blocking connect helpers */
191static int connect_next(struct channel_connect *);
192static void channel_connect_ctx_free(struct channel_connect *);
193
Ben Lindstrome9c99912001-06-09 00:41:05 +0000194/* -- channel core */
Damien Millerb38eff82000-04-01 11:09:21 +1000195
196Channel *
Damien Millerd47c62a2005-12-13 19:33:57 +1100197channel_by_id(int id)
Damien Millerb38eff82000-04-01 11:09:21 +1000198{
199 Channel *c;
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000200
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000201 if (id < 0 || (u_int)id >= channels_alloc) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100202 logit("channel_by_id: %d: bad id", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000203 return NULL;
204 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000205 c = channels[id];
206 if (c == NULL) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100207 logit("channel_by_id: %d: bad id: channel free", id);
Damien Millerb38eff82000-04-01 11:09:21 +1000208 return NULL;
209 }
210 return c;
211}
212
markus@openbsd.org8d057842016-09-30 09:19:13 +0000213Channel *
214channel_by_remote_id(int remote_id)
215{
216 Channel *c;
217 u_int i;
218
219 for (i = 0; i < channels_alloc; i++) {
220 c = channels[i];
221 if (c != NULL && c->remote_id == remote_id)
222 return c;
223 }
224 return NULL;
225}
226
Damien Miller5428f641999-11-25 11:54:57 +1100227/*
Damien Millerd47c62a2005-12-13 19:33:57 +1100228 * Returns the channel if it is allowed to receive protocol messages.
229 * Private channels, like listening sockets, may not receive messages.
230 */
231Channel *
232channel_lookup(int id)
233{
234 Channel *c;
235
236 if ((c = channel_by_id(id)) == NULL)
237 return (NULL);
238
Damien Millerd62f2ca2006-03-26 13:57:41 +1100239 switch (c->type) {
Damien Millerd47c62a2005-12-13 19:33:57 +1100240 case SSH_CHANNEL_X11_OPEN:
241 case SSH_CHANNEL_LARVAL:
242 case SSH_CHANNEL_CONNECTING:
243 case SSH_CHANNEL_DYNAMIC:
244 case SSH_CHANNEL_OPENING:
245 case SSH_CHANNEL_OPEN:
246 case SSH_CHANNEL_INPUT_DRAINING:
247 case SSH_CHANNEL_OUTPUT_DRAINING:
Damien Miller36187092013-06-10 13:07:11 +1000248 case SSH_CHANNEL_ABANDONED:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000249 case SSH_CHANNEL_MUX_PROXY:
Damien Millerd47c62a2005-12-13 19:33:57 +1100250 return (c);
Damien Millerd47c62a2005-12-13 19:33:57 +1100251 }
252 logit("Non-public channel %d, type %d.", id, c->type);
253 return (NULL);
254}
255
256/*
Damien Millere247cc42000-05-07 12:03:14 +1000257 * Register filedescriptors for a channel, used when allocating a channel or
Damien Miller6f83b8e2000-05-02 09:23:45 +1000258 * when the channel consumer/producer is ready, e.g. shell exec'd
Damien Miller5428f641999-11-25 11:54:57 +1100259 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000260static void
Damien Miller69b69aa2000-10-28 14:19:58 +1100261channel_register_fds(Channel *c, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000262 int extusage, int nonblock, int is_tty)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263{
Damien Miller95def091999-11-25 00:26:21 +1100264 /* Update the maximum file descriptor value. */
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000265 channel_max_fd = MAXIMUM(channel_max_fd, rfd);
266 channel_max_fd = MAXIMUM(channel_max_fd, wfd);
267 channel_max_fd = MAXIMUM(channel_max_fd, efd);
Damien Miller5e953212001-01-30 09:14:00 +1100268
Darren Tucker6e7fe1c2010-01-08 17:07:22 +1100269 if (rfd != -1)
270 fcntl(rfd, F_SETFD, FD_CLOEXEC);
271 if (wfd != -1 && wfd != rfd)
272 fcntl(wfd, F_SETFD, FD_CLOEXEC);
273 if (efd != -1 && efd != rfd && efd != wfd)
274 fcntl(efd, F_SETFD, FD_CLOEXEC);
Damien Millere247cc42000-05-07 12:03:14 +1000275
Damien Miller6f83b8e2000-05-02 09:23:45 +1000276 c->rfd = rfd;
277 c->wfd = wfd;
278 c->sock = (rfd == wfd) ? rfd : -1;
279 c->efd = efd;
280 c->extended_usage = extusage;
Damien Miller69b69aa2000-10-28 14:19:58 +1100281
Darren Tuckered3cdc02008-06-16 23:29:18 +1000282 if ((c->isatty = is_tty) != 0)
Damien Millerfbdeece2003-09-02 22:52:31 +1000283 debug2("channel %d: rfd %d isatty", c->self, c->rfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000284#ifdef _AIX
285 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker1a48aec2008-06-16 23:35:56 +1000286 c->wfd_isatty = is_tty || isatty(c->wfd);
Damien Millerc192a4c2013-08-01 14:29:20 +1000287#endif
Damien Miller79438cc2001-02-16 12:34:57 +1100288
Damien Miller69b69aa2000-10-28 14:19:58 +1100289 /* enable nonblocking mode */
290 if (nonblock) {
291 if (rfd != -1)
292 set_nonblock(rfd);
293 if (wfd != -1)
294 set_nonblock(wfd);
295 if (efd != -1)
296 set_nonblock(efd);
297 }
Damien Miller6f83b8e2000-05-02 09:23:45 +1000298}
299
300/*
301 * Allocate a new channel object and set its type and socket. This will cause
302 * remote_name to be freed.
303 */
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000304Channel *
Damien Miller6f83b8e2000-05-02 09:23:45 +1000305channel_new(char *ctype, int type, int rfd, int wfd, int efd,
Ben Lindstrom4fed2be2002-06-25 23:17:36 +0000306 u_int window, u_int maxpack, int extusage, char *remote_name, int nonblock)
Damien Miller6f83b8e2000-05-02 09:23:45 +1000307{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000308 int found;
309 u_int i;
Damien Miller6f83b8e2000-05-02 09:23:45 +1000310 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000311
Damien Miller95def091999-11-25 00:26:21 +1100312 /* Do initial allocation if this is the first call. */
313 if (channels_alloc == 0) {
314 channels_alloc = 10;
Damien Miller07d86be2006-03-26 14:19:21 +1100315 channels = xcalloc(channels_alloc, sizeof(Channel *));
Damien Miller95def091999-11-25 00:26:21 +1100316 for (i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000317 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100318 }
319 /* Try to find a free slot where to put the new channel. */
320 for (found = -1, i = 0; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000321 if (channels[i] == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100322 /* Found a free slot. */
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000323 found = (int)i;
Damien Miller95def091999-11-25 00:26:21 +1100324 break;
325 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000326 if (found < 0) {
Damien Miller5428f641999-11-25 11:54:57 +1100327 /* There are no free slots. Take last+1 slot and expand the array. */
Damien Miller95def091999-11-25 00:26:21 +1100328 found = channels_alloc;
Damien Miller9403aa22002-06-26 19:14:43 +1000329 if (channels_alloc > 10000)
330 fatal("channel_new: internal error: channels_alloc %d "
331 "too big.", channels_alloc);
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +0000332 channels = xreallocarray(channels, channels_alloc + 10,
Damien Miller36812092006-03-26 14:22:47 +1100333 sizeof(Channel *));
Damien Miller5efcecc2003-09-17 07:31:14 +1000334 channels_alloc += 10;
Damien Millerd3444942000-09-30 14:20:03 +1100335 debug2("channel: expanding %d", channels_alloc);
Damien Miller95def091999-11-25 00:26:21 +1100336 for (i = found; i < channels_alloc; i++)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000337 channels[i] = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100338 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000339 /* Initialize and return new channel. */
Damien Miller07d86be2006-03-26 14:19:21 +1100340 c = channels[found] = xcalloc(1, sizeof(Channel));
Damien Miller95def091999-11-25 00:26:21 +1100341 buffer_init(&c->input);
342 buffer_init(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +1000343 buffer_init(&c->extended);
Damien Millera1c1b6c2009-01-28 16:29:49 +1100344 c->path = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +1000345 c->listening_addr = NULL;
346 c->listening_port = 0;
Damien Miller5144df92002-01-22 23:28:45 +1100347 c->ostate = CHAN_OUTPUT_OPEN;
348 c->istate = CHAN_INPUT_OPEN;
349 c->flags = 0;
Damien Millerd310d512008-06-16 07:59:23 +1000350 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, 0);
Damien Millera6508752012-04-22 11:21:10 +1000351 c->notbefore = 0;
Damien Miller95def091999-11-25 00:26:21 +1100352 c->self = found;
353 c->type = type;
Damien Millerb38eff82000-04-01 11:09:21 +1000354 c->ctype = ctype;
Damien Millerbd483e72000-04-30 10:00:53 +1000355 c->local_window = window;
356 c->local_window_max = window;
357 c->local_consumed = 0;
358 c->local_maxpacket = maxpack;
Damien Miller95def091999-11-25 00:26:21 +1100359 c->remote_id = -1;
Damien Millerb1ca8bb2003-05-14 13:45:42 +1000360 c->remote_name = xstrdup(remote_name);
Damien Millerb38eff82000-04-01 11:09:21 +1000361 c->remote_window = 0;
362 c->remote_maxpacket = 0;
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000363 c->force_drain = 0;
Damien Millere7378562001-12-21 14:58:35 +1100364 c->single_connection = 0;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000365 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100366 c->detach_close = 0;
Damien Millerb84886b2008-05-19 15:05:07 +1000367 c->open_confirm = NULL;
368 c->open_confirm_ctx = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000369 c->input_filter = NULL;
Damien Miller077b2382005-12-31 16:22:32 +1100370 c->output_filter = NULL;
Darren Tucker84c56f52008-06-13 04:55:46 +1000371 c->filter_ctx = NULL;
372 c->filter_cleanup = NULL;
Damien Millere1537f92010-01-26 13:26:22 +1100373 c->ctl_chan = -1;
374 c->mux_rcb = NULL;
375 c->mux_ctx = NULL;
Damien Millerd530f5f2010-05-21 14:57:10 +1000376 c->mux_pause = 0;
Darren Tucker876045b2010-01-08 17:08:00 +1100377 c->delayed = 1; /* prevent call to channel_post handler */
Damien Millerb84886b2008-05-19 15:05:07 +1000378 TAILQ_INIT(&c->status_confirms);
Damien Miller95def091999-11-25 00:26:21 +1100379 debug("channel %d: new [%s]", found, remote_name);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000380 return c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000381}
Damien Miller6f83b8e2000-05-02 09:23:45 +1000382
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000383static int
384channel_find_maxfd(void)
385{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000386 u_int i;
387 int max = 0;
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000388 Channel *c;
389
390 for (i = 0; i < channels_alloc; i++) {
391 c = channels[i];
392 if (c != NULL) {
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000393 max = MAXIMUM(max, c->rfd);
394 max = MAXIMUM(max, c->wfd);
395 max = MAXIMUM(max, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000396 }
397 }
398 return max;
399}
400
401int
402channel_close_fd(int *fdp)
403{
404 int ret = 0, fd = *fdp;
405
406 if (fd != -1) {
407 ret = close(fd);
408 *fdp = -1;
409 if (fd == channel_max_fd)
410 channel_max_fd = channel_find_maxfd();
411 }
412 return ret;
413}
414
Damien Miller6f83b8e2000-05-02 09:23:45 +1000415/* Close all channel fd/socket. */
Ben Lindstrombba81212001-06-25 05:01:22 +0000416static void
Damien Miller6f83b8e2000-05-02 09:23:45 +1000417channel_close_fds(Channel *c)
418{
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000419 channel_close_fd(&c->sock);
420 channel_close_fd(&c->rfd);
421 channel_close_fd(&c->wfd);
422 channel_close_fd(&c->efd);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000423}
424
425/* Free the channel and close its fd/socket. */
Damien Miller4af51302000-04-16 11:18:38 +1000426void
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000427channel_free(Channel *c)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000428{
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000429 char *s;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000430 u_int i, n;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000431 Channel *other;
Damien Millerb84886b2008-05-19 15:05:07 +1000432 struct channel_confirm *cc;
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000433
markus@openbsd.org8d057842016-09-30 09:19:13 +0000434 for (n = 0, i = 0; i < channels_alloc; i++) {
435 if ((other = channels[i]) != NULL) {
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000436 n++;
markus@openbsd.org8d057842016-09-30 09:19:13 +0000437
438 /* detach from mux client and prepare for closing */
439 if (c->type == SSH_CHANNEL_MUX_CLIENT &&
440 other->type == SSH_CHANNEL_MUX_PROXY &&
441 other->mux_ctx == c) {
442 other->mux_ctx = NULL;
443 other->type = SSH_CHANNEL_OPEN;
444 other->istate = CHAN_INPUT_CLOSED;
445 other->ostate = CHAN_OUTPUT_CLOSED;
446 }
447 }
448 }
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000449 debug("channel %d: free: %s, nchannels %u", c->self,
Ben Lindstromc0dee1a2001-06-05 20:52:50 +0000450 c->remote_name ? c->remote_name : "???", n);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000451
markus@openbsd.org8d057842016-09-30 09:19:13 +0000452 /* XXX more MUX cleanup: remove remote forwardings */
453 if (c->type == SSH_CHANNEL_MUX_CLIENT) {
454 for (i = 0; i < (u_int)num_permitted_opens; i++) {
455 if (permitted_opens[i].downstream != c)
456 continue;
457 /* cancel on the server, since mux client is gone */
458 debug("channel %d: cleanup remote forward for %s:%u",
459 c->self,
460 permitted_opens[i].listen_host,
461 permitted_opens[i].listen_port);
462 packet_start(SSH2_MSG_GLOBAL_REQUEST);
463 packet_put_cstring("cancel-tcpip-forward");
464 packet_put_char(0);
465 packet_put_cstring(channel_rfwd_bind_host(
466 permitted_opens[i].listen_host));
467 packet_put_int(permitted_opens[i].listen_port);
468 packet_send();
469 /* unregister */
470 permitted_opens[i].listen_port = 0;
471 permitted_opens[i].port_to_connect = 0;
472 free(permitted_opens[i].host_to_connect);
473 permitted_opens[i].host_to_connect = NULL;
474 free(permitted_opens[i].listen_host);
475 permitted_opens[i].listen_host = NULL;
476 permitted_opens[i].listen_path = NULL;
477 permitted_opens[i].downstream = NULL;
478 }
479 }
480
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000481 s = channel_open_message();
Damien Millerfbdeece2003-09-02 22:52:31 +1000482 debug3("channel %d: status: %s", c->self, s);
Darren Tuckera627d422013-06-02 07:31:17 +1000483 free(s);
Ben Lindstrom6c3ae2b2000-12-30 03:25:14 +0000484
Damien Miller6f83b8e2000-05-02 09:23:45 +1000485 if (c->sock != -1)
Damien Millerb38eff82000-04-01 11:09:21 +1000486 shutdown(c->sock, SHUT_RDWR);
Damien Miller6f83b8e2000-05-02 09:23:45 +1000487 channel_close_fds(c);
Damien Millerb38eff82000-04-01 11:09:21 +1000488 buffer_free(&c->input);
489 buffer_free(&c->output);
490 buffer_free(&c->extended);
Darren Tuckera627d422013-06-02 07:31:17 +1000491 free(c->remote_name);
492 c->remote_name = NULL;
493 free(c->path);
494 c->path = NULL;
495 free(c->listening_addr);
496 c->listening_addr = NULL;
Damien Millerb84886b2008-05-19 15:05:07 +1000497 while ((cc = TAILQ_FIRST(&c->status_confirms)) != NULL) {
498 if (cc->abandon_cb != NULL)
499 cc->abandon_cb(c, cc->ctx);
500 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +1100501 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +1000502 free(cc);
Damien Millerb84886b2008-05-19 15:05:07 +1000503 }
Darren Tucker84c56f52008-06-13 04:55:46 +1000504 if (c->filter_cleanup != NULL && c->filter_ctx != NULL)
505 c->filter_cleanup(c->self, c->filter_ctx);
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000506 channels[c->self] = NULL;
Darren Tuckera627d422013-06-02 07:31:17 +1000507 free(c);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000508}
509
Ben Lindstrome9c99912001-06-09 00:41:05 +0000510void
Ben Lindstrom601e4362001-06-21 03:19:23 +0000511channel_free_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000512{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000513 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000514
Ben Lindstrom601e4362001-06-21 03:19:23 +0000515 for (i = 0; i < channels_alloc; i++)
516 if (channels[i] != NULL)
517 channel_free(channels[i]);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000518}
519
520/*
521 * Closes the sockets/fds of all channels. This is used to close extra file
522 * descriptors after a fork.
523 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000524void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000525channel_close_all(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000526{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000527 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000528
529 for (i = 0; i < channels_alloc; i++)
530 if (channels[i] != NULL)
531 channel_close_fds(channels[i]);
532}
533
534/*
Ben Lindstrom809744e2001-07-04 05:26:06 +0000535 * Stop listening to channels.
536 */
Ben Lindstrom809744e2001-07-04 05:26:06 +0000537void
538channel_stop_listening(void)
539{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000540 u_int i;
Ben Lindstrom809744e2001-07-04 05:26:06 +0000541 Channel *c;
542
543 for (i = 0; i < channels_alloc; i++) {
544 c = channels[i];
545 if (c != NULL) {
546 switch (c->type) {
547 case SSH_CHANNEL_AUTH_SOCKET:
548 case SSH_CHANNEL_PORT_LISTENER:
549 case SSH_CHANNEL_RPORT_LISTENER:
550 case SSH_CHANNEL_X11_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000551 case SSH_CHANNEL_UNIX_LISTENER:
552 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrom16d29d52001-07-18 16:01:46 +0000553 channel_close_fd(&c->sock);
Ben Lindstrom809744e2001-07-04 05:26:06 +0000554 channel_free(c);
555 break;
556 }
557 }
558 }
559}
560
561/*
Ben Lindstrome9c99912001-06-09 00:41:05 +0000562 * Returns true if no channel has too much buffered data, and false if one or
563 * more channel is overfull.
564 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000565int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000566channel_not_very_much_buffered_data(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000567{
568 u_int i;
569 Channel *c;
570
571 for (i = 0; i < channels_alloc; i++) {
572 c = channels[i];
573 if (c != NULL && c->type == SSH_CHANNEL_OPEN) {
Damien Milleraf5f2e62001-10-10 15:01:16 +1000574#if 0
575 if (!compat20 &&
576 buffer_len(&c->input) > packet_get_maxsize()) {
Damien Miller275295e2003-01-08 14:04:09 +1100577 debug2("channel %d: big input buffer %d",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000578 c->self, buffer_len(&c->input));
579 return 0;
580 }
Damien Milleraf5f2e62001-10-10 15:01:16 +1000581#endif
Ben Lindstrome9c99912001-06-09 00:41:05 +0000582 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000583 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000584 c->self, buffer_len(&c->output),
585 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000586 return 0;
587 }
588 }
589 }
590 return 1;
591}
592
593/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000594int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000595channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000596{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000597 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000598 Channel *c;
599
600 for (i = 0; i < channels_alloc; i++) {
601 c = channels[i];
602 if (c == NULL)
603 continue;
604 switch (c->type) {
605 case SSH_CHANNEL_X11_LISTENER:
606 case SSH_CHANNEL_PORT_LISTENER:
607 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100608 case SSH_CHANNEL_MUX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000609 case SSH_CHANNEL_CLOSED:
610 case SSH_CHANNEL_AUTH_SOCKET:
611 case SSH_CHANNEL_DYNAMIC:
612 case SSH_CHANNEL_CONNECTING:
613 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000614 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000615 case SSH_CHANNEL_UNIX_LISTENER:
616 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000617 continue;
618 case SSH_CHANNEL_LARVAL:
619 if (!compat20)
620 fatal("cannot happen: SSH_CHANNEL_LARVAL");
621 continue;
622 case SSH_CHANNEL_OPENING:
623 case SSH_CHANNEL_OPEN:
624 case SSH_CHANNEL_X11_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +1100625 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000626 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000627 return 1;
628 case SSH_CHANNEL_INPUT_DRAINING:
629 case SSH_CHANNEL_OUTPUT_DRAINING:
630 if (!compat13)
631 fatal("cannot happen: OUT_DRAIN");
632 return 1;
633 default:
634 fatal("channel_still_open: bad channel type %d", c->type);
635 /* NOTREACHED */
636 }
637 }
638 return 0;
639}
640
641/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000642int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000643channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000644{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000645 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000646 Channel *c;
647
648 for (i = 0; i < channels_alloc; i++) {
649 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000650 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000651 continue;
652 switch (c->type) {
653 case SSH_CHANNEL_CLOSED:
654 case SSH_CHANNEL_DYNAMIC:
655 case SSH_CHANNEL_X11_LISTENER:
656 case SSH_CHANNEL_PORT_LISTENER:
657 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100658 case SSH_CHANNEL_MUX_LISTENER:
659 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000660 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000661 case SSH_CHANNEL_OPENING:
662 case SSH_CHANNEL_CONNECTING:
663 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000664 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000665 case SSH_CHANNEL_UNIX_LISTENER:
666 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000667 continue;
668 case SSH_CHANNEL_LARVAL:
669 case SSH_CHANNEL_AUTH_SOCKET:
670 case SSH_CHANNEL_OPEN:
671 case SSH_CHANNEL_X11_OPEN:
672 return i;
673 case SSH_CHANNEL_INPUT_DRAINING:
674 case SSH_CHANNEL_OUTPUT_DRAINING:
675 if (!compat13)
676 fatal("cannot happen: OUT_DRAIN");
677 return i;
678 default:
679 fatal("channel_find_open: bad channel type %d", c->type);
680 /* NOTREACHED */
681 }
682 }
683 return -1;
684}
685
Ben Lindstrome9c99912001-06-09 00:41:05 +0000686/*
687 * Returns a message describing the currently open forwarded connections,
688 * suitable for sending to the client. The message contains crlf pairs for
689 * newlines.
690 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000691char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000692channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000693{
694 Buffer buffer;
695 Channel *c;
696 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000697 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000698
699 buffer_init(&buffer);
700 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
701 buffer_append(&buffer, buf, strlen(buf));
702 for (i = 0; i < channels_alloc; i++) {
703 c = channels[i];
704 if (c == NULL)
705 continue;
706 switch (c->type) {
707 case SSH_CHANNEL_X11_LISTENER:
708 case SSH_CHANNEL_PORT_LISTENER:
709 case SSH_CHANNEL_RPORT_LISTENER:
710 case SSH_CHANNEL_CLOSED:
711 case SSH_CHANNEL_AUTH_SOCKET:
712 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000713 case SSH_CHANNEL_ABANDONED:
Damien Millere1537f92010-01-26 13:26:22 +1100714 case SSH_CHANNEL_MUX_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000715 case SSH_CHANNEL_UNIX_LISTENER:
716 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000717 continue;
718 case SSH_CHANNEL_LARVAL:
719 case SSH_CHANNEL_OPENING:
720 case SSH_CHANNEL_CONNECTING:
721 case SSH_CHANNEL_DYNAMIC:
722 case SSH_CHANNEL_OPEN:
723 case SSH_CHANNEL_X11_OPEN:
724 case SSH_CHANNEL_INPUT_DRAINING:
725 case SSH_CHANNEL_OUTPUT_DRAINING:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000726 case SSH_CHANNEL_MUX_PROXY:
727 case SSH_CHANNEL_MUX_CLIENT:
Damien Miller0e220db2004-06-15 10:34:08 +1000728 snprintf(buf, sizeof buf,
djm@openbsd.orgb1d38a32015-10-15 23:51:40 +0000729 " #%d %.300s (t%d r%d i%u/%d o%u/%d fd %d/%d cc %d)\r\n",
Ben Lindstrome9c99912001-06-09 00:41:05 +0000730 c->self, c->remote_name,
731 c->type, c->remote_id,
732 c->istate, buffer_len(&c->input),
733 c->ostate, buffer_len(&c->output),
Damien Millere1537f92010-01-26 13:26:22 +1100734 c->rfd, c->wfd, c->ctl_chan);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000735 buffer_append(&buffer, buf, strlen(buf));
736 continue;
737 default:
738 fatal("channel_open_message: bad channel type %d", c->type);
739 /* NOTREACHED */
740 }
741 }
742 buffer_append(&buffer, "\0", 1);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +0000743 cp = xstrdup((char *)buffer_ptr(&buffer));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000744 buffer_free(&buffer);
745 return cp;
746}
747
748void
749channel_send_open(int id)
750{
751 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000752
Ben Lindstrome9c99912001-06-09 00:41:05 +0000753 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000754 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000755 return;
756 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000757 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000758 packet_start(SSH2_MSG_CHANNEL_OPEN);
759 packet_put_cstring(c->ctype);
760 packet_put_int(c->self);
761 packet_put_int(c->local_window);
762 packet_put_int(c->local_maxpacket);
763 packet_send();
764}
765
766void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000767channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000768{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000769 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000770
Ben Lindstrome9c99912001-06-09 00:41:05 +0000771 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000772 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000773 return;
774 }
Damien Miller0e220db2004-06-15 10:34:08 +1000775 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000776 packet_start(SSH2_MSG_CHANNEL_REQUEST);
777 packet_put_int(c->remote_id);
778 packet_put_cstring(service);
779 packet_put_char(wantconfirm);
780}
Damien Miller4f7becb2006-03-26 14:10:14 +1100781
Ben Lindstrome9c99912001-06-09 00:41:05 +0000782void
Damien Millerb84886b2008-05-19 15:05:07 +1000783channel_register_status_confirm(int id, channel_confirm_cb *cb,
784 channel_confirm_abandon_cb *abandon_cb, void *ctx)
785{
786 struct channel_confirm *cc;
787 Channel *c;
788
789 if ((c = channel_lookup(id)) == NULL)
790 fatal("channel_register_expect: %d: bad id", id);
791
Damien Miller6c81fee2013-11-08 12:19:55 +1100792 cc = xcalloc(1, sizeof(*cc));
Damien Millerb84886b2008-05-19 15:05:07 +1000793 cc->cb = cb;
794 cc->abandon_cb = abandon_cb;
795 cc->ctx = ctx;
796 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
797}
798
799void
Damien Millerd530f5f2010-05-21 14:57:10 +1000800channel_register_open_confirm(int id, channel_open_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000801{
802 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000803
Ben Lindstrome9c99912001-06-09 00:41:05 +0000804 if (c == NULL) {
Damien Millera0094332008-11-03 19:26:35 +1100805 logit("channel_register_open_confirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000806 return;
807 }
Damien Millerb84886b2008-05-19 15:05:07 +1000808 c->open_confirm = fn;
809 c->open_confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000810}
Damien Miller4f7becb2006-03-26 14:10:14 +1100811
Ben Lindstrome9c99912001-06-09 00:41:05 +0000812void
Damien Miller39eda6e2005-11-05 14:52:50 +1100813channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000814{
Damien Millerd47c62a2005-12-13 19:33:57 +1100815 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000816
Ben Lindstrome9c99912001-06-09 00:41:05 +0000817 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000818 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000819 return;
820 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000821 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100822 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000823}
Damien Miller4f7becb2006-03-26 14:10:14 +1100824
Ben Lindstrome9c99912001-06-09 00:41:05 +0000825void
826channel_cancel_cleanup(int id)
827{
Damien Millerd47c62a2005-12-13 19:33:57 +1100828 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000829
Ben Lindstrome9c99912001-06-09 00:41:05 +0000830 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000831 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000832 return;
833 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000834 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100835 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000836}
Damien Miller4f7becb2006-03-26 14:10:14 +1100837
Ben Lindstrome9c99912001-06-09 00:41:05 +0000838void
Damien Miller077b2382005-12-31 16:22:32 +1100839channel_register_filter(int id, channel_infilter_fn *ifn,
Darren Tucker84c56f52008-06-13 04:55:46 +1000840 channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000841{
842 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000843
Ben Lindstrome9c99912001-06-09 00:41:05 +0000844 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000845 logit("channel_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000846 return;
847 }
Damien Miller077b2382005-12-31 16:22:32 +1100848 c->input_filter = ifn;
849 c->output_filter = ofn;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000850 c->filter_ctx = ctx;
Darren Tucker84c56f52008-06-13 04:55:46 +1000851 c->filter_cleanup = cfn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000852}
853
854void
855channel_set_fds(int id, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000856 int extusage, int nonblock, int is_tty, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000857{
858 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000859
Ben Lindstrome9c99912001-06-09 00:41:05 +0000860 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
861 fatal("channel_activate for non-larval channel %d.", id);
Darren Tuckered3cdc02008-06-16 23:29:18 +1000862 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, is_tty);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000863 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100864 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000865 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
866 packet_put_int(c->remote_id);
867 packet_put_int(c->local_window);
868 packet_send();
869}
870
Damien Miller5428f641999-11-25 11:54:57 +1100871/*
Damien Millerb38eff82000-04-01 11:09:21 +1000872 * 'channel_pre*' are called just before select() to add any bits relevant to
873 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100874 */
Damien Millerb38eff82000-04-01 11:09:21 +1000875/*
876 * 'channel_post*': perform any appropriate operations for channels which
877 * have events pending.
878 */
Damien Millerd62f2ca2006-03-26 13:57:41 +1100879typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000880chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
881chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
882
Damien Miller8473dd82006-07-24 14:08:32 +1000883/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000884static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100885channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000886{
887 FD_SET(c->sock, readset);
888}
889
Damien Miller8473dd82006-07-24 14:08:32 +1000890/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000891static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100892channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000893{
894 debug3("channel %d: waiting for connection", c->self);
895 FD_SET(c->sock, writeset);
896}
897
Ben Lindstrombba81212001-06-25 05:01:22 +0000898static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100899channel_pre_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000900{
901 if (buffer_len(&c->input) < packet_get_maxsize())
902 FD_SET(c->sock, readset);
903 if (buffer_len(&c->output) > 0)
904 FD_SET(c->sock, writeset);
905}
906
Ben Lindstrombba81212001-06-25 05:01:22 +0000907static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100908channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000909{
Damien Millerde6987c2002-01-22 23:20:40 +1100910 u_int limit = compat20 ? c->remote_window : packet_get_maxsize();
Damien Millerb38eff82000-04-01 11:09:21 +1000911
Damien Miller33b13562000-04-04 14:38:59 +1000912 if (c->istate == CHAN_INPUT_OPEN &&
Damien Millerde6987c2002-01-22 23:20:40 +1100913 limit > 0 &&
Damien Miller499a0d52006-04-23 12:06:03 +1000914 buffer_len(&c->input) < limit &&
915 buffer_check_alloc(&c->input, CHAN_RBUF))
Damien Miller33b13562000-04-04 14:38:59 +1000916 FD_SET(c->rfd, readset);
917 if (c->ostate == CHAN_OUTPUT_OPEN ||
918 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
919 if (buffer_len(&c->output) > 0) {
920 FD_SET(c->wfd, writeset);
921 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000922 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +1000923 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
924 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000925 else
926 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000927 }
928 }
929 /** XXX check close conditions, too */
Damien Millerd654dd22008-05-19 16:05:41 +1000930 if (compat20 && c->efd != -1 &&
931 !(c->istate == CHAN_INPUT_CLOSED && c->ostate == CHAN_OUTPUT_CLOSED)) {
Damien Miller33b13562000-04-04 14:38:59 +1000932 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
933 buffer_len(&c->extended) > 0)
934 FD_SET(c->efd, writeset);
Damien Miller8853ca52010-06-26 10:00:14 +1000935 else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
936 (c->extended_usage == CHAN_EXTENDED_READ ||
937 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Miller33b13562000-04-04 14:38:59 +1000938 buffer_len(&c->extended) < c->remote_window)
939 FD_SET(c->efd, readset);
940 }
Damien Miller0e220db2004-06-15 10:34:08 +1000941 /* XXX: What about efd? races? */
Damien Miller33b13562000-04-04 14:38:59 +1000942}
943
Damien Miller8473dd82006-07-24 14:08:32 +1000944/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000945static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100946channel_pre_input_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000947{
948 if (buffer_len(&c->input) == 0) {
949 packet_start(SSH_MSG_CHANNEL_CLOSE);
950 packet_put_int(c->remote_id);
951 packet_send();
952 c->type = SSH_CHANNEL_CLOSED;
Damien Millerfbdeece2003-09-02 22:52:31 +1000953 debug2("channel %d: closing after input drain.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +1000954 }
955}
956
Damien Miller8473dd82006-07-24 14:08:32 +1000957/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000958static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100959channel_pre_output_draining(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000960{
961 if (buffer_len(&c->output) == 0)
Ben Lindstrom99c73b32001-05-05 04:09:47 +0000962 chan_mark_dead(c);
Damien Miller4af51302000-04-16 11:18:38 +1000963 else
Damien Millerb38eff82000-04-01 11:09:21 +1000964 FD_SET(c->sock, writeset);
965}
966
967/*
968 * This is a special state for X11 authentication spoofing. An opened X11
969 * connection (when authentication spoofing is being done) remains in this
970 * state until the first packet has been completely read. The authentication
971 * data in that packet is then substituted by the real data if it matches the
972 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000973 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000974 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000975 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000976static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000977x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000978{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000979 u_char *ucp;
980 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000981
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000982 /* Is this being called after the refusal deadline? */
983 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
984 verbose("Rejected X11 connection after ForwardX11Timeout "
985 "expired");
986 return -1;
987 }
988
Damien Millerb38eff82000-04-01 11:09:21 +1000989 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000990 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000991 return 0;
992
993 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100994 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000995 if (ucp[0] == 0x42) { /* Byte order MSB first. */
996 proto_len = 256 * ucp[6] + ucp[7];
997 data_len = 256 * ucp[8] + ucp[9];
998 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
999 proto_len = ucp[6] + 256 * ucp[7];
1000 data_len = ucp[8] + 256 * ucp[9];
1001 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +10001002 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001003 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +10001004 return -1;
1005 }
1006
1007 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00001008 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +10001009 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
1010 return 0;
1011
1012 /* Check if authentication protocol matches. */
1013 if (proto_len != strlen(x11_saved_proto) ||
1014 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001015 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +10001016 return -1;
1017 }
1018 /* Check if authentication data matches our fake data. */
1019 if (data_len != x11_fake_data_len ||
Damien Millerea1651c2010-07-16 13:58:37 +10001020 timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
Damien Millerb38eff82000-04-01 11:09:21 +10001021 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001022 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +10001023 return -1;
1024 }
1025 /* Check fake data length */
1026 if (x11_fake_data_len != x11_saved_data_len) {
1027 error("X11 fake_data_len %d != saved_data_len %d",
1028 x11_fake_data_len, x11_saved_data_len);
1029 return -1;
1030 }
1031 /*
1032 * Received authentication protocol and data match
1033 * our fake data. Substitute the fake data with real
1034 * data.
1035 */
1036 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
1037 x11_saved_data, x11_saved_data_len);
1038 return 1;
1039}
1040
Ben Lindstrombba81212001-06-25 05:01:22 +00001041static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001042channel_pre_x11_open_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001043{
Ben Lindstrome9c99912001-06-09 00:41:05 +00001044 int ret = x11_open_helper(&c->output);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001045
Damien Millerb38eff82000-04-01 11:09:21 +10001046 if (ret == 1) {
1047 /* Start normal processing for the channel. */
1048 c->type = SSH_CHANNEL_OPEN;
Damien Miller78928792000-04-12 20:17:38 +10001049 channel_pre_open_13(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +10001050 } else if (ret == -1) {
1051 /*
1052 * We have received an X11 connection that has bad
1053 * authentication information.
1054 */
Damien Miller996acd22003-04-09 20:59:48 +10001055 logit("X11 connection rejected because of wrong authentication.");
Damien Millerb38eff82000-04-01 11:09:21 +10001056 buffer_clear(&c->input);
1057 buffer_clear(&c->output);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001058 channel_close_fd(&c->sock);
Damien Millerb38eff82000-04-01 11:09:21 +10001059 c->sock = -1;
1060 c->type = SSH_CHANNEL_CLOSED;
1061 packet_start(SSH_MSG_CHANNEL_CLOSE);
1062 packet_put_int(c->remote_id);
1063 packet_send();
1064 }
1065}
1066
Ben Lindstrombba81212001-06-25 05:01:22 +00001067static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001068channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001069{
Ben Lindstrome9c99912001-06-09 00:41:05 +00001070 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00001071
1072 /* c->force_drain = 1; */
1073
Damien Millerb38eff82000-04-01 11:09:21 +10001074 if (ret == 1) {
1075 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +11001076 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +10001077 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +10001078 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +10001079 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +11001080 chan_read_failed(c);
1081 buffer_clear(&c->input);
1082 chan_ibuf_empty(c);
1083 buffer_clear(&c->output);
1084 /* for proto v1, the peer will send an IEOF */
1085 if (compat20)
1086 chan_write_failed(c);
1087 else
1088 c->type = SSH_CHANNEL_OPEN;
Damien Millerfbdeece2003-09-02 22:52:31 +10001089 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +10001090 }
1091}
1092
Damien Millere1537f92010-01-26 13:26:22 +11001093static void
1094channel_pre_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1095{
Damien Millerd530f5f2010-05-21 14:57:10 +10001096 if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
Damien Millere1537f92010-01-26 13:26:22 +11001097 buffer_check_alloc(&c->input, CHAN_RBUF))
1098 FD_SET(c->rfd, readset);
1099 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1100 /* clear buffer immediately (discard any partial packet) */
1101 buffer_clear(&c->input);
1102 chan_ibuf_empty(c);
1103 /* Start output drain. XXX just kill chan? */
1104 chan_rcvd_oclose(c);
1105 }
1106 if (c->ostate == CHAN_OUTPUT_OPEN ||
1107 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1108 if (buffer_len(&c->output) > 0)
1109 FD_SET(c->wfd, writeset);
1110 else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
1111 chan_obuf_empty(c);
1112 }
1113}
1114
Ben Lindstromb3921512001-04-11 15:57:50 +00001115/* try to decode a socks4 header */
Damien Miller8473dd82006-07-24 14:08:32 +10001116/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001117static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001118channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001119{
Damien Millera10f5612002-09-12 09:49:15 +10001120 char *p, *host;
Damien Miller1781f532009-01-28 16:24:41 +11001121 u_int len, have, i, found, need;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001122 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001123 struct {
1124 u_int8_t version;
1125 u_int8_t command;
1126 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +00001127 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001128 } s4_req, s4_rsp;
1129
Ben Lindstromb3921512001-04-11 15:57:50 +00001130 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001131
1132 have = buffer_len(&c->input);
1133 len = sizeof(s4_req);
1134 if (have < len)
1135 return 0;
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001136 p = (char *)buffer_ptr(&c->input);
Damien Miller1781f532009-01-28 16:24:41 +11001137
1138 need = 1;
1139 /* SOCKS4A uses an invalid IP address 0.0.0.x */
1140 if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1141 debug2("channel %d: socks4a request", c->self);
1142 /* ... and needs an extra string (the hostname) */
1143 need = 2;
1144 }
1145 /* Check for terminating NUL on the string(s) */
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001146 for (found = 0, i = len; i < have; i++) {
1147 if (p[i] == '\0') {
Damien Miller1781f532009-01-28 16:24:41 +11001148 found++;
1149 if (found == need)
1150 break;
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001151 }
1152 if (i > 1024) {
1153 /* the peer is probably sending garbage */
1154 debug("channel %d: decode socks4: too long",
1155 c->self);
1156 return -1;
1157 }
1158 }
Damien Miller1781f532009-01-28 16:24:41 +11001159 if (found < need)
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001160 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001161 buffer_get(&c->input, (char *)&s4_req.version, 1);
1162 buffer_get(&c->input, (char *)&s4_req.command, 1);
1163 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +00001164 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001165 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001166 p = (char *)buffer_ptr(&c->input);
Damien Miller13481292014-02-27 10:18:32 +11001167 if (memchr(p, '\0', have) == NULL)
1168 fatal("channel %d: decode socks4: user not nul terminated",
1169 c->self);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001170 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001171 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Damien Miller1781f532009-01-28 16:24:41 +11001172 len++; /* trailing '\0' */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001173 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +00001174 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001175 c->self, len, have);
1176 strlcpy(username, p, sizeof(username));
1177 buffer_consume(&c->input, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001178
Darren Tuckera627d422013-06-02 07:31:17 +10001179 free(c->path);
1180 c->path = NULL;
Damien Miller1781f532009-01-28 16:24:41 +11001181 if (need == 1) { /* SOCKS4: one string */
1182 host = inet_ntoa(s4_req.dest_addr);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001183 c->path = xstrdup(host);
Damien Miller1781f532009-01-28 16:24:41 +11001184 } else { /* SOCKS4A: two strings */
1185 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001186 p = (char *)buffer_ptr(&c->input);
Damien Miller1781f532009-01-28 16:24:41 +11001187 len = strlen(p);
1188 debug2("channel %d: decode socks4a: host %s/%d",
1189 c->self, p, len);
1190 len++; /* trailing '\0' */
1191 if (len > have)
1192 fatal("channel %d: decode socks4a: len %d > have %d",
1193 c->self, len, have);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001194 if (len > NI_MAXHOST) {
Damien Miller1781f532009-01-28 16:24:41 +11001195 error("channel %d: hostname \"%.100s\" too long",
1196 c->self, p);
1197 return -1;
1198 }
Damien Millera1c1b6c2009-01-28 16:29:49 +11001199 c->path = xstrdup(p);
Damien Miller1781f532009-01-28 16:24:41 +11001200 buffer_consume(&c->input, len);
1201 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001202 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001203
Damien Millerfbdeece2003-09-02 22:52:31 +10001204 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Damien Miller1781f532009-01-28 16:24:41 +11001205 c->self, c->path, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001206
Ben Lindstromb3921512001-04-11 15:57:50 +00001207 if (s4_req.command != 1) {
Damien Miller1781f532009-01-28 16:24:41 +11001208 debug("channel %d: cannot handle: %s cn %d",
1209 c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
Ben Lindstromb3921512001-04-11 15:57:50 +00001210 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001211 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001212 s4_rsp.version = 0; /* vn: 0 for reply */
1213 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001214 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001215 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Damien Millera0fdce92006-03-26 14:28:50 +11001216 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +00001217 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001218}
1219
Darren Tucker46471c92003-07-03 13:55:19 +10001220/* try to decode a socks5 header */
1221#define SSH_SOCKS5_AUTHDONE 0x1000
1222#define SSH_SOCKS5_NOAUTH 0x00
1223#define SSH_SOCKS5_IPV4 0x01
1224#define SSH_SOCKS5_DOMAIN 0x03
1225#define SSH_SOCKS5_IPV6 0x04
1226#define SSH_SOCKS5_CONNECT 0x01
1227#define SSH_SOCKS5_SUCCESS 0x00
1228
Damien Miller8473dd82006-07-24 14:08:32 +10001229/* ARGSUSED */
Darren Tucker46471c92003-07-03 13:55:19 +10001230static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001231channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001232{
1233 struct {
1234 u_int8_t version;
1235 u_int8_t command;
1236 u_int8_t reserved;
1237 u_int8_t atyp;
1238 } s5_req, s5_rsp;
1239 u_int16_t dest_port;
Damien Millerce986542013-07-18 16:12:44 +10001240 char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
1241 u_char *p;
Damien Miller0f077072006-07-10 22:21:02 +10001242 u_int have, need, i, found, nmethods, addrlen, af;
Darren Tucker46471c92003-07-03 13:55:19 +10001243
1244 debug2("channel %d: decode socks5", c->self);
1245 p = buffer_ptr(&c->input);
1246 if (p[0] != 0x05)
1247 return -1;
1248 have = buffer_len(&c->input);
1249 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1250 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001251 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001252 return 0;
1253 nmethods = p[1];
1254 if (have < nmethods + 2)
1255 return 0;
1256 /* look for method: "NO AUTHENTICATION REQUIRED" */
Damien Miller80163902007-01-05 16:30:16 +11001257 for (found = 0, i = 2; i < nmethods + 2; i++) {
Damien Miller4dec5d72006-08-05 11:38:40 +10001258 if (p[i] == SSH_SOCKS5_NOAUTH) {
Darren Tucker46471c92003-07-03 13:55:19 +10001259 found = 1;
1260 break;
1261 }
1262 }
1263 if (!found) {
1264 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1265 c->self);
1266 return -1;
1267 }
1268 buffer_consume(&c->input, nmethods + 2);
1269 buffer_put_char(&c->output, 0x05); /* version */
1270 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1271 FD_SET(c->sock, writeset);
1272 c->flags |= SSH_SOCKS5_AUTHDONE;
1273 debug2("channel %d: socks5 auth done", c->self);
1274 return 0; /* need more */
1275 }
1276 debug2("channel %d: socks5 post auth", c->self);
1277 if (have < sizeof(s5_req)+1)
1278 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001279 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001280 if (s5_req.version != 0x05 ||
1281 s5_req.command != SSH_SOCKS5_CONNECT ||
1282 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001283 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001284 return -1;
1285 }
Darren Tucker47eede72005-03-14 23:08:12 +11001286 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001287 case SSH_SOCKS5_IPV4:
1288 addrlen = 4;
1289 af = AF_INET;
1290 break;
1291 case SSH_SOCKS5_DOMAIN:
1292 addrlen = p[sizeof(s5_req)];
1293 af = -1;
1294 break;
1295 case SSH_SOCKS5_IPV6:
1296 addrlen = 16;
1297 af = AF_INET6;
1298 break;
1299 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001300 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001301 return -1;
1302 }
Damien Miller0f077072006-07-10 22:21:02 +10001303 need = sizeof(s5_req) + addrlen + 2;
1304 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1305 need++;
1306 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001307 return 0;
1308 buffer_consume(&c->input, sizeof(s5_req));
1309 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1310 buffer_consume(&c->input, 1); /* host string length */
Damien Millerce986542013-07-18 16:12:44 +10001311 buffer_get(&c->input, &dest_addr, addrlen);
Darren Tucker46471c92003-07-03 13:55:19 +10001312 buffer_get(&c->input, (char *)&dest_port, 2);
1313 dest_addr[addrlen] = '\0';
Darren Tuckera627d422013-06-02 07:31:17 +10001314 free(c->path);
1315 c->path = NULL;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001316 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
Damien Miller9576ac42009-01-28 16:30:33 +11001317 if (addrlen >= NI_MAXHOST) {
Damien Millera1c1b6c2009-01-28 16:29:49 +11001318 error("channel %d: dynamic request: socks5 hostname "
1319 "\"%.100s\" too long", c->self, dest_addr);
1320 return -1;
1321 }
1322 c->path = xstrdup(dest_addr);
1323 } else {
1324 if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1325 return -1;
1326 c->path = xstrdup(ntop);
1327 }
Darren Tucker46471c92003-07-03 13:55:19 +10001328 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001329
Damien Millerfbdeece2003-09-02 22:52:31 +10001330 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001331 c->self, c->path, c->host_port, s5_req.command);
1332
1333 s5_rsp.version = 0x05;
1334 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1335 s5_rsp.reserved = 0; /* ignored */
1336 s5_rsp.atyp = SSH_SOCKS5_IPV4;
Darren Tucker46471c92003-07-03 13:55:19 +10001337 dest_port = 0; /* ignored */
1338
Damien Millera0fdce92006-03-26 14:28:50 +11001339 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
Damien Miller13840e02013-09-14 09:49:43 +10001340 buffer_put_int(&c->output, ntohl(INADDR_ANY)); /* bind address */
Damien Millera0fdce92006-03-26 14:28:50 +11001341 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001342 return 1;
1343}
1344
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001345Channel *
Damien Millere1537f92010-01-26 13:26:22 +11001346channel_connect_stdio_fwd(const char *host_to_connect, u_short port_to_connect,
1347 int in, int out)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001348{
1349 Channel *c;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001350
1351 debug("channel_connect_stdio_fwd %s:%d", host_to_connect,
1352 port_to_connect);
1353
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001354 c = channel_new("stdio-forward", SSH_CHANNEL_OPENING, in, out,
1355 -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1356 0, "stdio-forward", /*nonblock*/0);
1357
1358 c->path = xstrdup(host_to_connect);
1359 c->host_port = port_to_connect;
1360 c->listening_port = 0;
1361 c->force_drain = 1;
1362
1363 channel_register_fds(c, in, out, -1, 0, 1, 0);
1364 port_open_helper(c, "direct-tcpip");
1365
1366 return c;
1367}
1368
Ben Lindstromb3921512001-04-11 15:57:50 +00001369/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001370static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001371channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001372{
1373 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001374 u_int have;
1375 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001376
1377 have = buffer_len(&c->input);
Ben Lindstromb3921512001-04-11 15:57:50 +00001378 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001379 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001380 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001381 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001382 /* need more */
1383 FD_SET(c->sock, readset);
1384 return;
1385 }
1386 /* try to guess the protocol */
1387 p = buffer_ptr(&c->input);
1388 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001389 case 0x04:
1390 ret = channel_decode_socks4(c, readset, writeset);
1391 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001392 case 0x05:
1393 ret = channel_decode_socks5(c, readset, writeset);
1394 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001395 default:
1396 ret = -1;
1397 break;
1398 }
1399 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001400 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001401 } else if (ret == 0) {
1402 debug2("channel %d: pre_dynamic: need more", c->self);
1403 /* need more */
1404 FD_SET(c->sock, readset);
1405 } else {
1406 /* switch to the next state */
1407 c->type = SSH_CHANNEL_OPENING;
1408 port_open_helper(c, "direct-tcpip");
1409 }
1410}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001411
Damien Millerb38eff82000-04-01 11:09:21 +10001412/* This is our fake X11 server socket. */
Damien Miller8473dd82006-07-24 14:08:32 +10001413/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001414static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001415channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001416{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001417 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001418 struct sockaddr_storage addr;
Damien Miller37f1c082013-04-23 15:20:43 +10001419 int newsock, oerrno;
Damien Millerb38eff82000-04-01 11:09:21 +10001420 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001421 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001422 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001423
1424 if (FD_ISSET(c->sock, readset)) {
1425 debug("X11 connection requested.");
1426 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001427 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001428 if (c->single_connection) {
Damien Miller37f1c082013-04-23 15:20:43 +10001429 oerrno = errno;
Damien Millerfbdeece2003-09-02 22:52:31 +10001430 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001431 channel_close_fd(&c->sock);
1432 chan_mark_dead(c);
Damien Miller37f1c082013-04-23 15:20:43 +10001433 errno = oerrno;
Damien Millere7378562001-12-21 14:58:35 +11001434 }
Damien Millerb38eff82000-04-01 11:09:21 +10001435 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001436 if (errno != EINTR && errno != EWOULDBLOCK &&
1437 errno != ECONNABORTED)
1438 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001439 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001440 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001441 return;
1442 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001443 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001444 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001445 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001446 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001447 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001448
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001449 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001450 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001451 c->local_window_max, c->local_maxpacket, 0, buf, 1);
Damien Millerbd483e72000-04-30 10:00:53 +10001452 if (compat20) {
1453 packet_start(SSH2_MSG_CHANNEL_OPEN);
1454 packet_put_cstring("x11");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001455 packet_put_int(nc->self);
Damien Millere7378562001-12-21 14:58:35 +11001456 packet_put_int(nc->local_window_max);
1457 packet_put_int(nc->local_maxpacket);
Damien Millerd83ff352001-01-30 09:19:34 +11001458 /* originator ipaddr and port */
1459 packet_put_cstring(remote_ipaddr);
Damien Miller30c3d422000-05-09 11:02:59 +10001460 if (datafellows & SSH_BUG_X11FWD) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001461 debug2("ssh2 x11 bug compat mode");
Damien Miller30c3d422000-05-09 11:02:59 +10001462 } else {
1463 packet_put_int(remote_port);
1464 }
Damien Millerbd483e72000-04-30 10:00:53 +10001465 packet_send();
1466 } else {
1467 packet_start(SSH_SMSG_X11_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001468 packet_put_int(nc->self);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001469 if (packet_get_protocol_flags() &
1470 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom664408d2001-06-09 01:42:01 +00001471 packet_put_cstring(buf);
Damien Millerbd483e72000-04-30 10:00:53 +10001472 packet_send();
1473 }
Darren Tuckera627d422013-06-02 07:31:17 +10001474 free(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001475 }
1476}
1477
Ben Lindstrombba81212001-06-25 05:01:22 +00001478static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001479port_open_helper(Channel *c, char *rtype)
1480{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001481 char buf[1024];
Damien Miller4def1842013-12-29 17:45:26 +11001482 char *local_ipaddr = get_local_ipaddr(c->sock);
djm@openbsd.org95767262016-03-07 19:02:43 +00001483 int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001484 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001485 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001486
Damien Millerd6369432010-02-02 17:02:07 +11001487 if (remote_port == -1) {
1488 /* Fake addr/port to appease peers that validate it (Tectia) */
Darren Tuckera627d422013-06-02 07:31:17 +10001489 free(remote_ipaddr);
Damien Millerd6369432010-02-02 17:02:07 +11001490 remote_ipaddr = xstrdup("127.0.0.1");
1491 remote_port = 65535;
1492 }
1493
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001494 snprintf(buf, sizeof buf,
1495 "%s: listening port %d for %.100s port %d, "
Damien Miller4def1842013-12-29 17:45:26 +11001496 "connect from %.200s port %d to %.100s port %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001497 rtype, c->listening_port, c->path, c->host_port,
Damien Miller4def1842013-12-29 17:45:26 +11001498 remote_ipaddr, remote_port, local_ipaddr, local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001499
Darren Tuckera627d422013-06-02 07:31:17 +10001500 free(c->remote_name);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001501 c->remote_name = xstrdup(buf);
1502
1503 if (compat20) {
1504 packet_start(SSH2_MSG_CHANNEL_OPEN);
1505 packet_put_cstring(rtype);
1506 packet_put_int(c->self);
1507 packet_put_int(c->local_window_max);
1508 packet_put_int(c->local_maxpacket);
Damien Miller7acefbb2014-07-18 14:11:24 +10001509 if (strcmp(rtype, "direct-tcpip") == 0) {
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001510 /* target host, port */
1511 packet_put_cstring(c->path);
1512 packet_put_int(c->host_port);
Damien Miller7acefbb2014-07-18 14:11:24 +10001513 } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1514 /* target path */
1515 packet_put_cstring(c->path);
1516 } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1517 /* listen path */
1518 packet_put_cstring(c->path);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001519 } else {
1520 /* listen address, port */
1521 packet_put_cstring(c->path);
Damien Miller4def1842013-12-29 17:45:26 +11001522 packet_put_int(local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001523 }
Damien Miller7acefbb2014-07-18 14:11:24 +10001524 if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1525 /* reserved for future owner/mode info */
1526 packet_put_cstring("");
1527 } else {
1528 /* originator host and port */
1529 packet_put_cstring(remote_ipaddr);
1530 packet_put_int((u_int)remote_port);
1531 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001532 packet_send();
1533 } else {
1534 packet_start(SSH_MSG_PORT_OPEN);
1535 packet_put_int(c->self);
1536 packet_put_cstring(c->path);
1537 packet_put_int(c->host_port);
Ben Lindstrome9c99912001-06-09 00:41:05 +00001538 if (packet_get_protocol_flags() &
1539 SSH_PROTOFLAG_HOST_IN_FWD_OPEN)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001540 packet_put_cstring(c->remote_name);
1541 packet_send();
1542 }
Darren Tuckera627d422013-06-02 07:31:17 +10001543 free(remote_ipaddr);
Damien Miller4def1842013-12-29 17:45:26 +11001544 free(local_ipaddr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001545}
1546
Damien Miller5e7fd072005-11-05 14:53:39 +11001547static void
1548channel_set_reuseaddr(int fd)
1549{
1550 int on = 1;
1551
1552 /*
1553 * Set socket options.
1554 * Allow local port reuse in TIME_WAIT.
1555 */
1556 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1557 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1558}
1559
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001560void
1561channel_set_x11_refuse_time(u_int refuse_time)
1562{
1563 x11_refuse_time = refuse_time;
1564}
1565
Damien Millerb38eff82000-04-01 11:09:21 +10001566/*
1567 * This socket is listening for connections to a forwarded TCP/IP port.
1568 */
Damien Miller8473dd82006-07-24 14:08:32 +10001569/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001570static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001571channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001572{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001573 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001574 struct sockaddr_storage addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001575 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001576 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001577 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001578
Damien Millerb38eff82000-04-01 11:09:21 +10001579 if (FD_ISSET(c->sock, readset)) {
1580 debug("Connection to port %d forwarding "
1581 "to %.100s port %d requested.",
1582 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001583
Damien Miller4623a752001-10-10 15:03:58 +10001584 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1585 nextstate = SSH_CHANNEL_OPENING;
1586 rtype = "forwarded-tcpip";
Damien Miller7acefbb2014-07-18 14:11:24 +10001587 } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1588 nextstate = SSH_CHANNEL_OPENING;
1589 rtype = "forwarded-streamlocal@openssh.com";
1590 } else if (c->host_port == PORT_STREAMLOCAL) {
1591 nextstate = SSH_CHANNEL_OPENING;
1592 rtype = "direct-streamlocal@openssh.com";
1593 } else if (c->host_port == 0) {
1594 nextstate = SSH_CHANNEL_DYNAMIC;
1595 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001596 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10001597 nextstate = SSH_CHANNEL_OPENING;
1598 rtype = "direct-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001599 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001600
Damien Millerb38eff82000-04-01 11:09:21 +10001601 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001602 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001603 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001604 if (errno != EINTR && errno != EWOULDBLOCK &&
1605 errno != ECONNABORTED)
1606 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001607 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001608 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001609 return;
1610 }
Damien Miller7acefbb2014-07-18 14:11:24 +10001611 if (c->host_port != PORT_STREAMLOCAL)
1612 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001613 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1614 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001615 nc->listening_port = c->listening_port;
1616 nc->host_port = c->host_port;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001617 if (c->path != NULL)
1618 nc->path = xstrdup(c->path);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001619
Darren Tucker876045b2010-01-08 17:08:00 +11001620 if (nextstate != SSH_CHANNEL_DYNAMIC)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001621 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001622 }
1623}
1624
1625/*
1626 * This is the authentication agent socket listening for connections from
1627 * clients.
1628 */
Damien Miller8473dd82006-07-24 14:08:32 +10001629/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001630static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001631channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001632{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001633 Channel *nc;
1634 int newsock;
Damien Miller163886f2008-07-14 11:28:58 +10001635 struct sockaddr_storage addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001636 socklen_t addrlen;
1637
1638 if (FD_ISSET(c->sock, readset)) {
1639 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001640 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001641 if (newsock < 0) {
Damien Millera6508752012-04-22 11:21:10 +10001642 error("accept from auth socket: %.100s",
1643 strerror(errno));
1644 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001645 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001646 return;
1647 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001648 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001649 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1650 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001651 0, "accepted auth socket", 1);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001652 if (compat20) {
1653 packet_start(SSH2_MSG_CHANNEL_OPEN);
1654 packet_put_cstring("auth-agent@openssh.com");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001655 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001656 packet_put_int(c->local_window_max);
1657 packet_put_int(c->local_maxpacket);
1658 } else {
1659 packet_start(SSH_SMSG_AGENT_OPEN);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001660 packet_put_int(nc->self);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001661 }
Damien Millerb38eff82000-04-01 11:09:21 +10001662 packet_send();
1663 }
1664}
1665
Damien Miller8473dd82006-07-24 14:08:32 +10001666/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001667static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001668channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001669{
Damien Millerbd740252008-05-19 15:37:09 +10001670 int err = 0, sock;
Ben Lindstrom11180952001-07-04 05:13:35 +00001671 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001672
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001673 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001674 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001675 err = errno;
1676 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001677 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001678 if (err == 0) {
Damien Millerbd740252008-05-19 15:37:09 +10001679 debug("channel %d: connected to %s port %d",
1680 c->self, c->connect_ctx.host, c->connect_ctx.port);
1681 channel_connect_ctx_free(&c->connect_ctx);
Ben Lindstrom69128662001-05-08 20:07:39 +00001682 c->type = SSH_CHANNEL_OPEN;
1683 if (compat20) {
1684 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1685 packet_put_int(c->remote_id);
1686 packet_put_int(c->self);
1687 packet_put_int(c->local_window);
1688 packet_put_int(c->local_maxpacket);
1689 } else {
1690 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
1691 packet_put_int(c->remote_id);
1692 packet_put_int(c->self);
1693 }
1694 } else {
Damien Millerbd740252008-05-19 15:37:09 +10001695 debug("channel %d: connection failed: %s",
Ben Lindstrom69128662001-05-08 20:07:39 +00001696 c->self, strerror(err));
Damien Millerbd740252008-05-19 15:37:09 +10001697 /* Try next address, if any */
1698 if ((sock = connect_next(&c->connect_ctx)) > 0) {
1699 close(c->sock);
1700 c->sock = c->rfd = c->wfd = sock;
1701 channel_max_fd = channel_find_maxfd();
1702 return;
1703 }
1704 /* Exhausted all addresses */
1705 error("connect_to %.100s port %d: failed.",
1706 c->connect_ctx.host, c->connect_ctx.port);
1707 channel_connect_ctx_free(&c->connect_ctx);
Ben Lindstrom69128662001-05-08 20:07:39 +00001708 if (compat20) {
1709 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1710 packet_put_int(c->remote_id);
1711 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1712 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1713 packet_put_cstring(strerror(err));
1714 packet_put_cstring("");
1715 }
1716 } else {
1717 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
1718 packet_put_int(c->remote_id);
1719 }
1720 chan_mark_dead(c);
1721 }
1722 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001723 }
1724}
1725
Damien Miller8473dd82006-07-24 14:08:32 +10001726/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001727static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001728channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001729{
Darren Tucker11327cc2005-03-14 23:22:25 +11001730 char buf[CHAN_RBUF];
Damien Miller835284b2007-06-11 13:03:16 +10001731 int len, force;
Damien Millerb38eff82000-04-01 11:09:21 +10001732
Damien Miller835284b2007-06-11 13:03:16 +10001733 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
1734 if (c->rfd != -1 && (force || FD_ISSET(c->rfd, readset))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001735 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001736 len = read(c->rfd, buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001737 if (len < 0 && (errno == EINTR ||
1738 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001739 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001740#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001741 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001742#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001743 if ((!c->isatty && len <= 0) ||
1744 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001745#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001746 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001747 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001748 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001749 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001750 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001751 return -1;
Ben Lindstromb3921512001-04-11 15:57:50 +00001752 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001753 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10001754 c->type = SSH_CHANNEL_INPUT_DRAINING;
Damien Millerfbdeece2003-09-02 22:52:31 +10001755 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001756 } else {
1757 chan_read_failed(c);
1758 }
1759 return -1;
1760 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001761 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001762 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001763 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001764 chan_read_failed(c);
1765 }
Damien Millerd27b9472005-12-13 19:29:02 +11001766 } else if (c->datagram) {
1767 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001768 } else {
1769 buffer_append(&c->input, buf, len);
1770 }
Damien Millerb38eff82000-04-01 11:09:21 +10001771 }
1772 return 1;
1773}
Damien Miller4f7becb2006-03-26 14:10:14 +11001774
Damien Miller8473dd82006-07-24 14:08:32 +10001775/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001776static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001777channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001778{
Ben Lindstrome229b252001-03-05 06:28:06 +00001779 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001780 u_char *data = NULL, *buf;
Damien Miller7d457182010-08-05 23:09:48 +10001781 u_int dlen, olen = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001782 int len;
1783
1784 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001785 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001786 FD_ISSET(c->wfd, writeset) &&
1787 buffer_len(&c->output) > 0) {
Damien Miller7d457182010-08-05 23:09:48 +10001788 olen = buffer_len(&c->output);
Damien Miller077b2382005-12-31 16:22:32 +11001789 if (c->output_filter != NULL) {
1790 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1791 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001792 if (c->type != SSH_CHANNEL_OPEN)
1793 chan_mark_dead(c);
1794 else
1795 chan_write_failed(c);
1796 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001797 }
1798 } else if (c->datagram) {
1799 buf = data = buffer_get_string(&c->output, &dlen);
1800 } else {
1801 buf = data = buffer_ptr(&c->output);
1802 dlen = buffer_len(&c->output);
1803 }
1804
Damien Millerd27b9472005-12-13 19:29:02 +11001805 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001806 /* ignore truncated writes, datagrams might get lost */
Damien Miller077b2382005-12-31 16:22:32 +11001807 len = write(c->wfd, buf, dlen);
Darren Tuckera627d422013-06-02 07:31:17 +10001808 free(data);
Damien Millerd8968ad2008-07-04 23:10:49 +10001809 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1810 errno == EWOULDBLOCK))
Damien Millerd27b9472005-12-13 19:29:02 +11001811 return 1;
1812 if (len <= 0) {
1813 if (c->type != SSH_CHANNEL_OPEN)
1814 chan_mark_dead(c);
1815 else
1816 chan_write_failed(c);
1817 return -1;
1818 }
Damien Miller7d457182010-08-05 23:09:48 +10001819 goto out;
Damien Millerd27b9472005-12-13 19:29:02 +11001820 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001821#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001822 /* XXX: Later AIX versions can't push as much data to tty */
Darren Tucker240fdfa2003-11-22 14:10:02 +11001823 if (compat20 && c->wfd_isatty)
1824 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001825#endif
Damien Miller077b2382005-12-31 16:22:32 +11001826
1827 len = write(c->wfd, buf, dlen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001828 if (len < 0 &&
1829 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001830 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001831 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001832 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001833 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001834 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001835 return -1;
1836 } else if (compat13) {
Damien Miller76765c02002-01-22 23:21:15 +11001837 buffer_clear(&c->output);
Damien Millerfbdeece2003-09-02 22:52:31 +10001838 debug2("channel %d: input draining.", c->self);
Damien Millerb38eff82000-04-01 11:09:21 +10001839 c->type = SSH_CHANNEL_INPUT_DRAINING;
1840 } else {
1841 chan_write_failed(c);
1842 }
1843 return -1;
1844 }
Darren Tucker3980b632009-08-28 11:02:37 +10001845#ifndef BROKEN_TCGETATTR_ICANON
Damien Miller077b2382005-12-31 16:22:32 +11001846 if (compat20 && c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001847 if (tcgetattr(c->wfd, &tio) == 0 &&
1848 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1849 /*
1850 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001851 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001852 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001853 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001854 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001855 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001856 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001857 }
1858 }
Darren Tucker3980b632009-08-28 11:02:37 +10001859#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001860 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001861 }
Damien Miller7d457182010-08-05 23:09:48 +10001862 out:
1863 if (compat20 && olen > 0)
1864 c->local_consumed += olen - buffer_len(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +10001865 return 1;
1866}
Damien Miller4f7becb2006-03-26 14:10:14 +11001867
Ben Lindstrombba81212001-06-25 05:01:22 +00001868static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001869channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001870{
Darren Tucker11327cc2005-03-14 23:22:25 +11001871 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001872 int len;
1873
Damien Miller1383bd82000-04-06 12:32:37 +10001874/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001875 if (c->efd != -1) {
1876 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1877 FD_ISSET(c->efd, writeset) &&
1878 buffer_len(&c->extended) > 0) {
1879 len = write(c->efd, buffer_ptr(&c->extended),
1880 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001881 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001882 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001883 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1884 errno == EWOULDBLOCK))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001885 return 1;
1886 if (len <= 0) {
1887 debug2("channel %d: closing write-efd %d",
1888 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001889 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001890 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001891 buffer_consume(&c->extended, len);
1892 c->local_consumed += len;
1893 }
Damien Miller8853ca52010-06-26 10:00:14 +10001894 } else if (c->efd != -1 &&
1895 (c->extended_usage == CHAN_EXTENDED_READ ||
1896 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Millere42bd242007-01-29 10:16:28 +11001897 (c->detach_close || FD_ISSET(c->efd, readset))) {
Damien Miller33b13562000-04-04 14:38:59 +10001898 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001899 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001900 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001901 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
1902 errno == EWOULDBLOCK) && !c->detach_close)))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001903 return 1;
1904 if (len <= 0) {
1905 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001906 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001907 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001908 } else {
Damien Miller8853ca52010-06-26 10:00:14 +10001909 if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
1910 debug3("channel %d: discard efd",
1911 c->self);
1912 } else
1913 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001914 }
Damien Miller33b13562000-04-04 14:38:59 +10001915 }
1916 }
1917 return 1;
1918}
Damien Miller4f7becb2006-03-26 14:10:14 +11001919
Damien Miller0e220db2004-06-15 10:34:08 +10001920static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001921channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001922{
Ben Lindstromb3921512001-04-11 15:57:50 +00001923 if (c->type == SSH_CHANNEL_OPEN &&
1924 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10001925 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10001926 c->local_maxpacket*3) ||
1927 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10001928 c->local_consumed > 0) {
1929 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1930 packet_put_int(c->remote_id);
1931 packet_put_int(c->local_consumed);
1932 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001933 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001934 c->self, c->local_window,
1935 c->local_consumed);
1936 c->local_window += c->local_consumed;
1937 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001938 }
1939 return 1;
1940}
1941
Ben Lindstrombba81212001-06-25 05:01:22 +00001942static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001943channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001944{
1945 channel_handle_rfd(c, readset, writeset);
1946 channel_handle_wfd(c, readset, writeset);
Damien Millerde6987c2002-01-22 23:20:40 +11001947 if (!compat20)
Damien Miller4623a752001-10-10 15:03:58 +10001948 return;
Damien Miller33b13562000-04-04 14:38:59 +10001949 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001950 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001951}
1952
Damien Millere1537f92010-01-26 13:26:22 +11001953static u_int
1954read_mux(Channel *c, u_int need)
1955{
1956 char buf[CHAN_RBUF];
1957 int len;
1958 u_int rlen;
1959
1960 if (buffer_len(&c->input) < need) {
1961 rlen = need - buffer_len(&c->input);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001962 len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
naddy@openbsd.org603ba412016-02-05 13:28:19 +00001963 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1964 return buffer_len(&c->input);
Damien Millere1537f92010-01-26 13:26:22 +11001965 if (len <= 0) {
naddy@openbsd.org603ba412016-02-05 13:28:19 +00001966 debug2("channel %d: ctl read<=0 rfd %d len %d",
1967 c->self, c->rfd, len);
1968 chan_read_failed(c);
1969 return 0;
Damien Millere1537f92010-01-26 13:26:22 +11001970 } else
1971 buffer_append(&c->input, buf, len);
1972 }
1973 return buffer_len(&c->input);
1974}
1975
1976static void
1977channel_post_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1978{
1979 u_int need;
1980 ssize_t len;
1981
1982 if (!compat20)
1983 fatal("%s: entered with !compat20", __func__);
1984
Damien Millerd530f5f2010-05-21 14:57:10 +10001985 if (c->rfd != -1 && !c->mux_pause && FD_ISSET(c->rfd, readset) &&
Damien Millere1537f92010-01-26 13:26:22 +11001986 (c->istate == CHAN_INPUT_OPEN ||
1987 c->istate == CHAN_INPUT_WAIT_DRAIN)) {
1988 /*
1989 * Don't not read past the precise end of packets to
1990 * avoid disrupting fd passing.
1991 */
1992 if (read_mux(c, 4) < 4) /* read header */
1993 return;
1994 need = get_u32(buffer_ptr(&c->input));
1995#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
1996 if (need > CHANNEL_MUX_MAX_PACKET) {
1997 debug2("channel %d: packet too big %u > %u",
1998 c->self, CHANNEL_MUX_MAX_PACKET, need);
1999 chan_rcvd_oclose(c);
2000 return;
2001 }
2002 if (read_mux(c, need + 4) < need + 4) /* read body */
2003 return;
2004 if (c->mux_rcb(c) != 0) {
2005 debug("channel %d: mux_rcb failed", c->self);
2006 chan_mark_dead(c);
2007 return;
2008 }
2009 }
2010
2011 if (c->wfd != -1 && FD_ISSET(c->wfd, writeset) &&
2012 buffer_len(&c->output) > 0) {
2013 len = write(c->wfd, buffer_ptr(&c->output),
2014 buffer_len(&c->output));
2015 if (len < 0 && (errno == EINTR || errno == EAGAIN))
2016 return;
2017 if (len <= 0) {
2018 chan_mark_dead(c);
2019 return;
2020 }
2021 buffer_consume(&c->output, len);
2022 }
2023}
2024
2025static void
2026channel_post_mux_listener(Channel *c, fd_set *readset, fd_set *writeset)
2027{
2028 Channel *nc;
2029 struct sockaddr_storage addr;
2030 socklen_t addrlen;
2031 int newsock;
2032 uid_t euid;
2033 gid_t egid;
2034
2035 if (!FD_ISSET(c->sock, readset))
2036 return;
2037
2038 debug("multiplexing control connection");
2039
2040 /*
2041 * Accept connection on control socket
2042 */
2043 memset(&addr, 0, sizeof(addr));
2044 addrlen = sizeof(addr);
2045 if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
2046 &addrlen)) == -1) {
2047 error("%s accept: %s", __func__, strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10002048 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002049 c->notbefore = monotime() + 1;
Damien Millere1537f92010-01-26 13:26:22 +11002050 return;
2051 }
2052
2053 if (getpeereid(newsock, &euid, &egid) < 0) {
2054 error("%s getpeereid failed: %s", __func__,
2055 strerror(errno));
2056 close(newsock);
2057 return;
2058 }
2059 if ((euid != 0) && (getuid() != euid)) {
2060 error("multiplex uid mismatch: peer euid %u != uid %u",
2061 (u_int)euid, (u_int)getuid());
2062 close(newsock);
2063 return;
2064 }
2065 nc = channel_new("multiplex client", SSH_CHANNEL_MUX_CLIENT,
2066 newsock, newsock, -1, c->local_window_max,
2067 c->local_maxpacket, 0, "mux-control", 1);
2068 nc->mux_rcb = c->mux_rcb;
2069 debug3("%s: new mux channel %d fd %d", __func__,
2070 nc->self, nc->sock);
2071 /* establish state */
2072 nc->mux_rcb(nc);
2073 /* mux state transitions must not elicit protocol messages */
2074 nc->flags |= CHAN_LOCAL;
2075}
2076
Damien Miller8473dd82006-07-24 14:08:32 +10002077/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00002078static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11002079channel_post_output_drain_13(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10002080{
2081 int len;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002082
Damien Millerb38eff82000-04-01 11:09:21 +10002083 /* Send buffered output data to the socket. */
2084 if (FD_ISSET(c->sock, writeset) && buffer_len(&c->output) > 0) {
2085 len = write(c->sock, buffer_ptr(&c->output),
2086 buffer_len(&c->output));
2087 if (len <= 0)
Damien Miller76765c02002-01-22 23:21:15 +11002088 buffer_clear(&c->output);
Damien Millerb38eff82000-04-01 11:09:21 +10002089 else
2090 buffer_consume(&c->output, len);
2091 }
2092}
2093
Ben Lindstrombba81212001-06-25 05:01:22 +00002094static void
Damien Miller33b13562000-04-04 14:38:59 +10002095channel_handler_init_20(void)
2096{
Damien Millerde6987c2002-01-22 23:20:40 +11002097 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10002098 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10002099 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002100 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10002101 channel_pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
2102 channel_pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10002103 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002104 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002105 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00002106 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millere1537f92010-01-26 13:26:22 +11002107 channel_pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
2108 channel_pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10002109
Damien Millerde6987c2002-01-22 23:20:40 +11002110 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10002111 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002112 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10002113 channel_post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
2114 channel_post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10002115 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002116 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002117 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11002118 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millere1537f92010-01-26 13:26:22 +11002119 channel_post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
2120 channel_post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10002121}
2122
Ben Lindstrombba81212001-06-25 05:01:22 +00002123static void
Damien Millerb38eff82000-04-01 11:09:21 +10002124channel_handler_init_13(void)
2125{
2126 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open_13;
2127 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open_13;
2128 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2129 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2130 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
2131 channel_pre[SSH_CHANNEL_INPUT_DRAINING] = &channel_pre_input_draining;
2132 channel_pre[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_pre_output_draining;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002133 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00002134 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10002135
Damien Millerde6987c2002-01-22 23:20:40 +11002136 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10002137 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2138 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2139 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
2140 channel_post[SSH_CHANNEL_OUTPUT_DRAINING] = &channel_post_output_drain_13;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002141 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11002142 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10002143}
2144
Ben Lindstrombba81212001-06-25 05:01:22 +00002145static void
Damien Millerb38eff82000-04-01 11:09:21 +10002146channel_handler_init_15(void)
2147{
Damien Millerde6987c2002-01-22 23:20:40 +11002148 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10002149 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Millerb38eff82000-04-01 11:09:21 +10002150 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
2151 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
2152 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002153 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00002154 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millerb38eff82000-04-01 11:09:21 +10002155
2156 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
2157 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
2158 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Damien Millerde6987c2002-01-22 23:20:40 +11002159 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00002160 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11002161 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millerb38eff82000-04-01 11:09:21 +10002162}
2163
Ben Lindstrombba81212001-06-25 05:01:22 +00002164static void
Damien Millerb38eff82000-04-01 11:09:21 +10002165channel_handler_init(void)
2166{
2167 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00002168
Damien Miller9f0f5c62001-12-21 14:45:46 +11002169 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
Damien Millerb38eff82000-04-01 11:09:21 +10002170 channel_pre[i] = NULL;
2171 channel_post[i] = NULL;
2172 }
Damien Miller33b13562000-04-04 14:38:59 +10002173 if (compat20)
2174 channel_handler_init_20();
2175 else if (compat13)
Damien Millerb38eff82000-04-01 11:09:21 +10002176 channel_handler_init_13();
2177 else
2178 channel_handler_init_15();
2179}
2180
Damien Miller3ec27592001-10-12 11:35:04 +10002181/* gc dead channels */
2182static void
2183channel_garbage_collect(Channel *c)
2184{
2185 if (c == NULL)
2186 return;
2187 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11002188 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10002189 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002190 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002191 c->detach_user(c->self, NULL);
2192 /* if we still have a callback */
2193 if (c->detach_user != NULL)
2194 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002195 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002196 }
2197 if (!chan_is_dead(c, 1))
2198 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002199 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002200 channel_free(c);
2201}
2202
Ben Lindstrombba81212001-06-25 05:01:22 +00002203static void
Damien Millera6508752012-04-22 11:21:10 +10002204channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset,
2205 time_t *unpause_secs)
Damien Millerb38eff82000-04-01 11:09:21 +10002206{
2207 static int did_init = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002208 u_int i, oalloc;
Damien Millerb38eff82000-04-01 11:09:21 +10002209 Channel *c;
Damien Millera6508752012-04-22 11:21:10 +10002210 time_t now;
Damien Millerb38eff82000-04-01 11:09:21 +10002211
2212 if (!did_init) {
2213 channel_handler_init();
2214 did_init = 1;
2215 }
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002216 now = monotime();
Damien Millera6508752012-04-22 11:21:10 +10002217 if (unpause_secs != NULL)
2218 *unpause_secs = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002219 for (i = 0, oalloc = channels_alloc; i < oalloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002220 c = channels[i];
2221 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10002222 continue;
Darren Tucker876045b2010-01-08 17:08:00 +11002223 if (c->delayed) {
2224 if (ftab == channel_pre)
2225 c->delayed = 0;
2226 else
2227 continue;
2228 }
Damien Millera6508752012-04-22 11:21:10 +10002229 if (ftab[c->type] != NULL) {
2230 /*
2231 * Run handlers that are not paused.
2232 */
2233 if (c->notbefore <= now)
2234 (*ftab[c->type])(c, readset, writeset);
2235 else if (unpause_secs != NULL) {
2236 /*
2237 * Collect the time that the earliest
2238 * channel comes off pause.
2239 */
2240 debug3("%s: chan %d: skip for %d more seconds",
2241 __func__, c->self,
2242 (int)(c->notbefore - now));
2243 if (*unpause_secs == 0 ||
2244 (c->notbefore - now) < *unpause_secs)
2245 *unpause_secs = c->notbefore - now;
2246 }
2247 }
Damien Miller3ec27592001-10-12 11:35:04 +10002248 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002249 }
Damien Millera6508752012-04-22 11:21:10 +10002250 if (unpause_secs != NULL && *unpause_secs != 0)
2251 debug3("%s: first channel unpauses in %d seconds",
2252 __func__, (int)*unpause_secs);
Damien Millerb38eff82000-04-01 11:09:21 +10002253}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002254
Ben Lindstrome9c99912001-06-09 00:41:05 +00002255/*
2256 * Allocate/update select bitmasks and add any bits relevant to channels in
2257 * select bitmasks.
2258 */
Damien Miller4af51302000-04-16 11:18:38 +10002259void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002260channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Damien Millerba77e1f2012-04-23 18:21:05 +10002261 u_int *nallocp, time_t *minwait_secs, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002262{
Damien Miller36812092006-03-26 14:22:47 +11002263 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11002264
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00002265 n = MAXIMUM(*maxfdp, channel_max_fd);
Damien Miller5e953212001-01-30 09:14:00 +11002266
Damien Miller36812092006-03-26 14:22:47 +11002267 nfdset = howmany(n+1, NFDBITS);
2268 /* Explicitly test here, because xrealloc isn't always called */
millert@openbsd.orgfd368342015-02-06 23:21:59 +00002269 if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask))
Damien Miller36812092006-03-26 14:22:47 +11002270 fatal("channel_prepare_select: max_fd (%d) is too large", n);
2271 sz = nfdset * sizeof(fd_mask);
2272
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002273 /* perhaps check sz < nalloc/2 and shrink? */
2274 if (*readsetp == NULL || sz > *nallocp) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002275 *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask));
2276 *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002277 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11002278 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002279 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11002280 memset(*readsetp, 0, sz);
2281 memset(*writesetp, 0, sz);
2282
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002283 if (!rekeying)
Damien Millera6508752012-04-22 11:21:10 +10002284 channel_handler(channel_pre, *readsetp, *writesetp,
2285 minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002286}
2287
Ben Lindstrome9c99912001-06-09 00:41:05 +00002288/*
2289 * After select, perform any appropriate operations for channels which have
2290 * events pending.
2291 */
Damien Miller4af51302000-04-16 11:18:38 +10002292void
Damien Millerd62f2ca2006-03-26 13:57:41 +11002293channel_after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002294{
Damien Millera6508752012-04-22 11:21:10 +10002295 channel_handler(channel_post, readset, writeset, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002296}
2297
Ben Lindstrome9c99912001-06-09 00:41:05 +00002298
Damien Miller5e953212001-01-30 09:14:00 +11002299/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10002300void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002301channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002302{
Damien Millerb38eff82000-04-01 11:09:21 +10002303 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002304 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002305
Damien Miller95def091999-11-25 00:26:21 +11002306 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002307 c = channels[i];
2308 if (c == NULL)
2309 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002310
Ben Lindstrome9c99912001-06-09 00:41:05 +00002311 /*
2312 * We are only interested in channels that can have buffered
2313 * incoming data.
2314 */
Damien Miller34132e52000-01-14 15:45:46 +11002315 if (compat13) {
Damien Millerb38eff82000-04-01 11:09:21 +10002316 if (c->type != SSH_CHANNEL_OPEN &&
2317 c->type != SSH_CHANNEL_INPUT_DRAINING)
Damien Miller34132e52000-01-14 15:45:46 +11002318 continue;
2319 } else {
Damien Millerb38eff82000-04-01 11:09:21 +10002320 if (c->type != SSH_CHANNEL_OPEN)
Damien Miller34132e52000-01-14 15:45:46 +11002321 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002322 }
Damien Millerefb4afe2000-04-12 18:45:05 +10002323 if (compat20 &&
2324 (c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002325 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10002326 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002327 continue;
2328 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002329
Damien Miller95def091999-11-25 00:26:21 +11002330 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002331 if ((c->istate == CHAN_INPUT_OPEN ||
2332 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
2333 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11002334 if (c->datagram) {
2335 if (len > 0) {
2336 u_char *data;
2337 u_int dlen;
2338
2339 data = buffer_get_string(&c->input,
2340 &dlen);
Damien Miller7d457182010-08-05 23:09:48 +10002341 if (dlen > c->remote_window ||
2342 dlen > c->remote_maxpacket) {
2343 debug("channel %d: datagram "
2344 "too big for channel",
2345 c->self);
Darren Tuckera627d422013-06-02 07:31:17 +10002346 free(data);
Damien Miller7d457182010-08-05 23:09:48 +10002347 continue;
2348 }
Damien Millerd27b9472005-12-13 19:29:02 +11002349 packet_start(SSH2_MSG_CHANNEL_DATA);
2350 packet_put_int(c->remote_id);
2351 packet_put_string(data, dlen);
2352 packet_send();
djm@openbsd.orgf715afe2015-06-30 05:23:25 +00002353 c->remote_window -= dlen;
Darren Tuckera627d422013-06-02 07:31:17 +10002354 free(data);
Damien Millerd27b9472005-12-13 19:29:02 +11002355 }
2356 continue;
2357 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002358 /*
2359 * Send some data for the other side over the secure
2360 * connection.
2361 */
Damien Miller33b13562000-04-04 14:38:59 +10002362 if (compat20) {
2363 if (len > c->remote_window)
2364 len = c->remote_window;
2365 if (len > c->remote_maxpacket)
2366 len = c->remote_maxpacket;
Damien Miller95def091999-11-25 00:26:21 +11002367 } else {
Damien Miller33b13562000-04-04 14:38:59 +10002368 if (packet_is_interactive()) {
2369 if (len > 1024)
2370 len = 512;
2371 } else {
2372 /* Keep the packets at reasonable size. */
2373 if (len > packet_get_maxsize()/2)
2374 len = packet_get_maxsize()/2;
2375 }
Damien Miller95def091999-11-25 00:26:21 +11002376 }
Damien Millerb38eff82000-04-01 11:09:21 +10002377 if (len > 0) {
Damien Miller33b13562000-04-04 14:38:59 +10002378 packet_start(compat20 ?
2379 SSH2_MSG_CHANNEL_DATA : SSH_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10002380 packet_put_int(c->remote_id);
2381 packet_put_string(buffer_ptr(&c->input), len);
2382 packet_send();
2383 buffer_consume(&c->input, len);
2384 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10002385 }
2386 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller95def091999-11-25 00:26:21 +11002387 if (compat13)
2388 fatal("cannot happen: istate == INPUT_WAIT_DRAIN for proto 1.3");
Damien Miller5428f641999-11-25 11:54:57 +11002389 /*
2390 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00002391 * tell peer, that we will not send more data: send IEOF.
2392 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11002393 */
Ben Lindstromcf159442002-03-26 03:26:24 +00002394 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10002395 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
2396 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00002397 else
2398 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11002399 }
Damien Miller33b13562000-04-04 14:38:59 +10002400 /* Send extended data, i.e. stderr */
2401 if (compat20 &&
Ben Lindstromcf159442002-03-26 03:26:24 +00002402 !(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10002403 c->remote_window > 0 &&
2404 (len = buffer_len(&c->extended)) > 0 &&
2405 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002406 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002407 c->self, c->remote_window, buffer_len(&c->extended),
2408 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10002409 if (len > c->remote_window)
2410 len = c->remote_window;
2411 if (len > c->remote_maxpacket)
2412 len = c->remote_maxpacket;
2413 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
2414 packet_put_int(c->remote_id);
2415 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
2416 packet_put_string(buffer_ptr(&c->extended), len);
2417 packet_send();
2418 buffer_consume(&c->extended, len);
2419 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002420 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10002421 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002422 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002423}
2424
markus@openbsd.org8d057842016-09-30 09:19:13 +00002425/* -- mux proxy support */
2426
2427/*
2428 * When multiplexing channel messages for mux clients we have to deal
2429 * with downstream messages from the mux client and upstream messages
2430 * from the ssh server:
2431 * 1) Handling downstream messages is straightforward and happens
2432 * in channel_proxy_downstream():
2433 * - We forward all messages (mostly) unmodified to the server.
2434 * - However, in order to route messages from upstream to the correct
2435 * downstream client, we have to replace the channel IDs used by the
2436 * mux clients with a unique channel ID because the mux clients might
2437 * use conflicting channel IDs.
2438 * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and
2439 * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local
2440 * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID
2441 * with the newly allocated channel ID.
2442 * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY
2443 * channels and procesed by channel_proxy_upstream(). The local channel ID
2444 * is then translated back to the original mux client ID.
2445 * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE
2446 * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels.
2447 * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the
2448 * downstream mux client are removed.
2449 * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server
2450 * requires more work, because they are not addressed to a specific
2451 * channel. E.g. client_request_forwarded_tcpip() needs to figure
2452 * out whether the request is addressed to the local client or a
2453 * specific downstream client based on the listen-address/port.
2454 * 6) Agent and X11-Forwarding have a similar problem and are currenly
2455 * not supported as the matching session/channel cannot be identified
2456 * easily.
2457 */
2458
2459/*
2460 * receive packets from downstream mux clients:
2461 * channel callback fired on read from mux client, creates
2462 * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs
2463 * on channel creation.
2464 */
2465int
2466channel_proxy_downstream(Channel *downstream)
2467{
2468 Channel *c = NULL;
2469 struct ssh *ssh = active_state;
2470 struct sshbuf *original = NULL, *modified = NULL;
2471 const u_char *cp;
2472 char *ctype = NULL, *listen_host = NULL;
2473 u_char type;
2474 size_t have;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002475 int ret = -1, r, idx;
2476 u_int id, remote_id, listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002477
2478 /* sshbuf_dump(&downstream->input, stderr); */
2479 if ((r = sshbuf_get_string_direct(&downstream->input, &cp, &have))
2480 != 0) {
2481 error("%s: malformed message: %s", __func__, ssh_err(r));
2482 return -1;
2483 }
2484 if (have < 2) {
2485 error("%s: short message", __func__);
2486 return -1;
2487 }
2488 type = cp[1];
2489 /* skip padlen + type */
2490 cp += 2;
2491 have -= 2;
2492 if (ssh_packet_log_type(type))
2493 debug3("%s: channel %u: down->up: type %u", __func__,
2494 downstream->self, type);
2495
2496 switch (type) {
2497 case SSH2_MSG_CHANNEL_OPEN:
2498 if ((original = sshbuf_from(cp, have)) == NULL ||
2499 (modified = sshbuf_new()) == NULL) {
2500 error("%s: alloc", __func__);
2501 goto out;
2502 }
2503 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 ||
2504 (r = sshbuf_get_u32(original, &id)) != 0) {
2505 error("%s: parse error %s", __func__, ssh_err(r));
2506 goto out;
2507 }
2508 c = channel_new("mux proxy", SSH_CHANNEL_MUX_PROXY,
2509 -1, -1, -1, 0, 0, 0, ctype, 1);
2510 c->mux_ctx = downstream; /* point to mux client */
2511 c->mux_downstream_id = id; /* original downstream id */
2512 if ((r = sshbuf_put_cstring(modified, ctype)) != 0 ||
2513 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2514 (r = sshbuf_putb(modified, original)) != 0) {
2515 error("%s: compose error %s", __func__, ssh_err(r));
2516 channel_free(c);
2517 goto out;
2518 }
2519 break;
2520 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2521 /*
2522 * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we
2523 * need to parse 'remote_id' instead of 'ctype'.
2524 */
2525 if ((original = sshbuf_from(cp, have)) == NULL ||
2526 (modified = sshbuf_new()) == NULL) {
2527 error("%s: alloc", __func__);
2528 goto out;
2529 }
2530 if ((r = sshbuf_get_u32(original, &remote_id)) != 0 ||
2531 (r = sshbuf_get_u32(original, &id)) != 0) {
2532 error("%s: parse error %s", __func__, ssh_err(r));
2533 goto out;
2534 }
2535 c = channel_new("mux proxy", SSH_CHANNEL_MUX_PROXY,
2536 -1, -1, -1, 0, 0, 0, "mux-down-connect", 1);
2537 c->mux_ctx = downstream; /* point to mux client */
2538 c->mux_downstream_id = id;
2539 c->remote_id = remote_id;
2540 if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
2541 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2542 (r = sshbuf_putb(modified, original)) != 0) {
2543 error("%s: compose error %s", __func__, ssh_err(r));
2544 channel_free(c);
2545 goto out;
2546 }
2547 break;
2548 case SSH2_MSG_GLOBAL_REQUEST:
2549 if ((original = sshbuf_from(cp, have)) == NULL) {
2550 error("%s: alloc", __func__);
2551 goto out;
2552 }
2553 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) {
2554 error("%s: parse error %s", __func__, ssh_err(r));
2555 goto out;
2556 }
2557 if (strcmp(ctype, "tcpip-forward") != 0) {
2558 error("%s: unsupported request %s", __func__, ctype);
2559 goto out;
2560 }
2561 if ((r = sshbuf_get_u8(original, NULL)) != 0 ||
2562 (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 ||
2563 (r = sshbuf_get_u32(original, &listen_port)) != 0) {
2564 error("%s: parse error %s", __func__, ssh_err(r));
2565 goto out;
2566 }
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002567 if (listen_port > 65535) {
2568 error("%s: tcpip-forward for %s: bad port %u",
2569 __func__, listen_host, listen_port);
2570 goto out;
2571 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002572 /* Record that connection to this host/port is permitted. */
2573 permitted_opens = xreallocarray(permitted_opens,
2574 num_permitted_opens + 1, sizeof(*permitted_opens));
2575 idx = num_permitted_opens++;
2576 permitted_opens[idx].host_to_connect = xstrdup("<mux>");
2577 permitted_opens[idx].port_to_connect = -1;
2578 permitted_opens[idx].listen_host = listen_host;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002579 permitted_opens[idx].listen_port = (int)listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002580 permitted_opens[idx].downstream = downstream;
2581 listen_host = NULL;
2582 break;
2583 case SSH2_MSG_CHANNEL_CLOSE:
2584 if (have < 4)
2585 break;
2586 remote_id = PEEK_U32(cp);
2587 if ((c = channel_by_remote_id(remote_id)) != NULL) {
2588 if (c->flags & CHAN_CLOSE_RCVD)
2589 channel_free(c);
2590 else
2591 c->flags |= CHAN_CLOSE_SENT;
2592 }
2593 break;
2594 }
2595 if (modified) {
2596 if ((r = sshpkt_start(ssh, type)) != 0 ||
2597 (r = sshpkt_putb(ssh, modified)) != 0 ||
2598 (r = sshpkt_send(ssh)) != 0) {
2599 error("%s: send %s", __func__, ssh_err(r));
2600 goto out;
2601 }
2602 } else {
2603 if ((r = sshpkt_start(ssh, type)) != 0 ||
2604 (r = sshpkt_put(ssh, cp, have)) != 0 ||
2605 (r = sshpkt_send(ssh)) != 0) {
2606 error("%s: send %s", __func__, ssh_err(r));
2607 goto out;
2608 }
2609 }
2610 ret = 0;
2611 out:
2612 free(ctype);
2613 free(listen_host);
2614 sshbuf_free(original);
2615 sshbuf_free(modified);
2616 return ret;
2617}
2618
2619/*
2620 * receive packets from upstream server and de-multiplex packets
2621 * to correct downstream:
2622 * implemented as a helper for channel input handlers,
2623 * replaces local (proxy) channel ID with downstream channel ID.
2624 */
2625int
2626channel_proxy_upstream(Channel *c, int type, u_int32_t seq, void *ctxt)
2627{
2628 struct ssh *ssh = active_state;
2629 struct sshbuf *b = NULL;
2630 Channel *downstream;
2631 const u_char *cp = NULL;
2632 size_t len;
2633 int r;
2634
2635 /*
2636 * When receiving packets from the peer we need to check whether we
2637 * need to forward the packets to the mux client. In this case we
2638 * restore the orignal channel id and keep track of CLOSE messages,
2639 * so we can cleanup the channel.
2640 */
2641 if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY)
2642 return 0;
2643 if ((downstream = c->mux_ctx) == NULL)
2644 return 0;
2645 switch (type) {
2646 case SSH2_MSG_CHANNEL_CLOSE:
2647 case SSH2_MSG_CHANNEL_DATA:
2648 case SSH2_MSG_CHANNEL_EOF:
2649 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
2650 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2651 case SSH2_MSG_CHANNEL_OPEN_FAILURE:
2652 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
2653 case SSH2_MSG_CHANNEL_SUCCESS:
2654 case SSH2_MSG_CHANNEL_FAILURE:
2655 case SSH2_MSG_CHANNEL_REQUEST:
2656 break;
2657 default:
2658 debug2("%s: channel %u: unsupported type %u", __func__,
2659 c->self, type);
2660 return 0;
2661 }
2662 if ((b = sshbuf_new()) == NULL) {
2663 error("%s: alloc reply", __func__);
2664 goto out;
2665 }
2666 /* get remaining payload (after id) */
2667 cp = sshpkt_ptr(ssh, &len);
2668 if (cp == NULL) {
2669 error("%s: no packet", __func__);
2670 goto out;
2671 }
2672 /* translate id and send to muxclient */
2673 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
2674 (r = sshbuf_put_u8(b, type)) != 0 ||
2675 (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 ||
2676 (r = sshbuf_put(b, cp, len)) != 0 ||
2677 (r = sshbuf_put_stringb(&downstream->output, b)) != 0) {
2678 error("%s: compose for muxclient %s", __func__, ssh_err(r));
2679 goto out;
2680 }
2681 /* sshbuf_dump(b, stderr); */
2682 if (ssh_packet_log_type(type))
2683 debug3("%s: channel %u: up->down: type %u", __func__, c->self,
2684 type);
2685 out:
2686 /* update state */
2687 switch (type) {
2688 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2689 /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
2690 if (cp && len > 4)
2691 c->remote_id = PEEK_U32(cp);
2692 break;
2693 case SSH2_MSG_CHANNEL_CLOSE:
2694 if (c->flags & CHAN_CLOSE_SENT)
2695 channel_free(c);
2696 else
2697 c->flags |= CHAN_CLOSE_RCVD;
2698 break;
2699 }
2700 sshbuf_free(b);
2701 return 1;
2702}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002703
2704/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002705
2706/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002707int
Damien Miller630d6f42002-01-22 23:17:30 +11002708channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002709{
Damien Miller34132e52000-01-14 15:45:46 +11002710 int id;
Damien Miller633de332014-05-15 13:48:26 +10002711 const u_char *data;
Damien Miller7d457182010-08-05 23:09:48 +10002712 u_int data_len, win_len;
Damien Millerb38eff82000-04-01 11:09:21 +10002713 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002714
Damien Miller95def091999-11-25 00:26:21 +11002715 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11002716 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10002717 c = channel_lookup(id);
2718 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11002719 packet_disconnect("Received data for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002720 if (channel_proxy_upstream(c, type, seq, ctxt))
2721 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002722
Damien Miller95def091999-11-25 00:26:21 +11002723 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002724 if (c->type != SSH_CHANNEL_OPEN &&
2725 c->type != SSH_CHANNEL_X11_OPEN)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002726 return 0;
Damien Miller34132e52000-01-14 15:45:46 +11002727
Damien Miller95def091999-11-25 00:26:21 +11002728 /* Get the data. */
Damien Millerdb255ca2008-05-19 14:59:37 +10002729 data = packet_get_string_ptr(&data_len);
Damien Miller7d457182010-08-05 23:09:48 +10002730 win_len = data_len;
2731 if (c->datagram)
2732 win_len += 4; /* string length header */
Damien Millerb38eff82000-04-01 11:09:21 +10002733
Damien Millera04ad492004-01-21 11:02:09 +11002734 /*
2735 * Ignore data for protocol > 1.3 if output end is no longer open.
2736 * For protocol 2 the sending side is reducing its window as it sends
2737 * data, so we must 'fake' consumption of the data in order to ensure
2738 * that window updates are sent back. Otherwise the connection might
2739 * deadlock.
2740 */
2741 if (!compat13 && c->ostate != CHAN_OUTPUT_OPEN) {
2742 if (compat20) {
Damien Miller7d457182010-08-05 23:09:48 +10002743 c->local_window -= win_len;
2744 c->local_consumed += win_len;
Damien Millera04ad492004-01-21 11:02:09 +11002745 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002746 return 0;
Damien Millera04ad492004-01-21 11:02:09 +11002747 }
2748
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002749 if (compat20) {
Damien Miller7d457182010-08-05 23:09:48 +10002750 if (win_len > c->local_maxpacket) {
Damien Miller996acd22003-04-09 20:59:48 +10002751 logit("channel %d: rcvd big packet %d, maxpack %d",
Damien Miller7d457182010-08-05 23:09:48 +10002752 c->self, win_len, c->local_maxpacket);
Damien Miller33b13562000-04-04 14:38:59 +10002753 }
Damien Miller7d457182010-08-05 23:09:48 +10002754 if (win_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002755 logit("channel %d: rcvd too much data %d, win %d",
Damien Miller7d457182010-08-05 23:09:48 +10002756 c->self, win_len, c->local_window);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002757 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002758 }
Damien Miller7d457182010-08-05 23:09:48 +10002759 c->local_window -= win_len;
Damien Miller33b13562000-04-04 14:38:59 +10002760 }
Damien Millerd27b9472005-12-13 19:29:02 +11002761 if (c->datagram)
2762 buffer_put_string(&c->output, data, data_len);
2763 else
2764 buffer_append(&c->output, data, data_len);
Damien Millerdb255ca2008-05-19 14:59:37 +10002765 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002766 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002767}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002768
Damien Millerd79b4242006-03-31 23:11:44 +11002769/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002770int
Damien Miller630d6f42002-01-22 23:17:30 +11002771channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002772{
2773 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002774 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002775 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002776 Channel *c;
2777
2778 /* Get the channel number and verify it. */
2779 id = packet_get_int();
2780 c = channel_lookup(id);
2781
2782 if (c == NULL)
2783 packet_disconnect("Received extended_data for bad channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002784 if (channel_proxy_upstream(c, type, seq, ctxt))
2785 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002786 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002787 logit("channel %d: ext data for non open", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002788 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002789 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002790 if (c->flags & CHAN_EOF_RCVD) {
2791 if (datafellows & SSH_BUG_EXTEOF)
2792 debug("channel %d: accepting ext data after eof", id);
2793 else
2794 packet_disconnect("Received extended_data after EOF "
2795 "on channel %d.", id);
2796 }
Damien Miller33b13562000-04-04 14:38:59 +10002797 tcode = packet_get_int();
2798 if (c->efd == -1 ||
2799 c->extended_usage != CHAN_EXTENDED_WRITE ||
2800 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002801 logit("channel %d: bad ext data", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002802 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002803 }
2804 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002805 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002806 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002807 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002808 c->self, data_len, c->local_window);
Darren Tuckera627d422013-06-02 07:31:17 +10002809 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002810 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002811 }
Damien Millerd3444942000-09-30 14:20:03 +11002812 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002813 c->local_window -= data_len;
2814 buffer_append(&c->extended, data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002815 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002816 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002817}
2818
Damien Millerd79b4242006-03-31 23:11:44 +11002819/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002820int
Damien Miller630d6f42002-01-22 23:17:30 +11002821channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002822{
2823 int id;
2824 Channel *c;
2825
Damien Millerb38eff82000-04-01 11:09:21 +10002826 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002827 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002828 c = channel_lookup(id);
2829 if (c == NULL)
2830 packet_disconnect("Received ieof for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002831 if (channel_proxy_upstream(c, type, seq, ctxt))
2832 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002833 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002834
2835 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002836 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002837 debug("channel %d: FORCE input drain", c->self);
2838 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002839 if (buffer_len(&c->input) == 0)
2840 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002841 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002842 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002843}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002844
Damien Millerd79b4242006-03-31 23:11:44 +11002845/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002846int
Damien Miller630d6f42002-01-22 23:17:30 +11002847channel_input_close(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002848{
Damien Millerb38eff82000-04-01 11:09:21 +10002849 int id;
2850 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002851
Damien Millerb38eff82000-04-01 11:09:21 +10002852 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002853 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002854 c = channel_lookup(id);
2855 if (c == NULL)
2856 packet_disconnect("Received close for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002857 if (channel_proxy_upstream(c, type, seq, ctxt))
2858 return 0;
Damien Miller5428f641999-11-25 11:54:57 +11002859 /*
2860 * Send a confirmation that we have closed the channel and no more
2861 * data is coming for it.
2862 */
Damien Miller95def091999-11-25 00:26:21 +11002863 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10002864 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11002865 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002866
Damien Miller5428f641999-11-25 11:54:57 +11002867 /*
2868 * If the channel is in closed state, we have sent a close request,
2869 * and the other side will eventually respond with a confirmation.
2870 * Thus, we cannot free the channel here, because then there would be
2871 * no-one to receive the confirmation. The channel gets freed when
2872 * the confirmation arrives.
2873 */
Damien Millerb38eff82000-04-01 11:09:21 +10002874 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11002875 /*
2876 * Not a closed channel - mark it as draining, which will
2877 * cause it to be freed later.
2878 */
Damien Miller76765c02002-01-22 23:21:15 +11002879 buffer_clear(&c->input);
Damien Millerb38eff82000-04-01 11:09:21 +10002880 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11002881 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002882 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002883}
2884
Damien Millerb38eff82000-04-01 11:09:21 +10002885/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Millerd79b4242006-03-31 23:11:44 +11002886/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002887int
Damien Miller630d6f42002-01-22 23:17:30 +11002888channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002889{
Damien Millerb38eff82000-04-01 11:09:21 +10002890 int id = packet_get_int();
2891 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002892
Damien Millerb38eff82000-04-01 11:09:21 +10002893 if (c == NULL)
2894 packet_disconnect("Received oclose for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002895 if (channel_proxy_upstream(c, type, seq, ctxt))
2896 return 0;
2897 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002898 chan_rcvd_oclose(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002899 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002900}
2901
Damien Millerd79b4242006-03-31 23:11:44 +11002902/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002903int
Damien Miller630d6f42002-01-22 23:17:30 +11002904channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002905{
2906 int id = packet_get_int();
2907 Channel *c = channel_lookup(id);
2908
2909 if (c == NULL)
2910 packet_disconnect("Received close confirmation for "
2911 "out-of-range channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002912 if (channel_proxy_upstream(c, type, seq, ctxt))
2913 return 0;
2914 packet_check_eom();
Damien Miller36187092013-06-10 13:07:11 +10002915 if (c->type != SSH_CHANNEL_CLOSED && c->type != SSH_CHANNEL_ABANDONED)
Damien Millerb38eff82000-04-01 11:09:21 +10002916 packet_disconnect("Received close confirmation for "
2917 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002918 channel_free(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002919 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002920}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002921
Damien Millerd79b4242006-03-31 23:11:44 +11002922/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002923int
Damien Miller630d6f42002-01-22 23:17:30 +11002924channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002925{
Damien Millerb38eff82000-04-01 11:09:21 +10002926 int id, remote_id;
2927 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002928
Damien Millerb38eff82000-04-01 11:09:21 +10002929 id = packet_get_int();
2930 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002931
markus@openbsd.org8d057842016-09-30 09:19:13 +00002932 if (c==NULL)
2933 packet_disconnect("Received open confirmation for "
2934 "unknown channel %d.", id);
2935 if (channel_proxy_upstream(c, type, seq, ctxt))
2936 return 0;
2937 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002938 packet_disconnect("Received open confirmation for "
2939 "non-opening channel %d.", id);
2940 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002941 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002942 c->remote_id = remote_id;
2943 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002944
2945 if (compat20) {
2946 c->remote_window = packet_get_int();
2947 c->remote_maxpacket = packet_get_int();
Damien Millerb84886b2008-05-19 15:05:07 +10002948 if (c->open_confirm) {
Damien Millerd3444942000-09-30 14:20:03 +11002949 debug2("callback start");
Damien Millerd530f5f2010-05-21 14:57:10 +10002950 c->open_confirm(c->self, 1, c->open_confirm_ctx);
Damien Millerd3444942000-09-30 14:20:03 +11002951 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002952 }
Damien Millerfbdeece2003-09-02 22:52:31 +10002953 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
Damien Miller33b13562000-04-04 14:38:59 +10002954 c->remote_window, c->remote_maxpacket);
2955 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002956 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002957 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002958}
2959
Ben Lindstrombba81212001-06-25 05:01:22 +00002960static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002961reason2txt(int reason)
2962{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002963 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002964 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2965 return "administratively prohibited";
2966 case SSH2_OPEN_CONNECT_FAILED:
2967 return "connect failed";
2968 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2969 return "unknown channel type";
2970 case SSH2_OPEN_RESOURCE_SHORTAGE:
2971 return "resource shortage";
2972 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002973 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002974}
2975
Damien Millerd79b4242006-03-31 23:11:44 +11002976/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002977int
Damien Miller630d6f42002-01-22 23:17:30 +11002978channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002979{
Kevin Steves12057502001-02-05 14:54:34 +00002980 int id, reason;
2981 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002982 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002983
Damien Millerb38eff82000-04-01 11:09:21 +10002984 id = packet_get_int();
2985 c = channel_lookup(id);
2986
markus@openbsd.org8d057842016-09-30 09:19:13 +00002987 if (c==NULL)
2988 packet_disconnect("Received open failure for "
2989 "unknown channel %d.", id);
2990 if (channel_proxy_upstream(c, type, seq, ctxt))
2991 return 0;
2992 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002993 packet_disconnect("Received open failure for "
2994 "non-opening channel %d.", id);
Damien Miller33b13562000-04-04 14:38:59 +10002995 if (compat20) {
Kevin Steves12057502001-02-05 14:54:34 +00002996 reason = packet_get_int();
Ben Lindstromf3436742001-04-29 19:52:00 +00002997 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
Kevin Steves12057502001-02-05 14:54:34 +00002998 msg = packet_get_string(NULL);
2999 lang = packet_get_string(NULL);
3000 }
Damien Miller996acd22003-04-09 20:59:48 +10003001 logit("channel %d: open failed: %s%s%s", id,
Ben Lindstrom69128662001-05-08 20:07:39 +00003002 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
Darren Tuckera627d422013-06-02 07:31:17 +10003003 free(msg);
3004 free(lang);
Damien Millerd530f5f2010-05-21 14:57:10 +10003005 if (c->open_confirm) {
3006 debug2("callback start");
3007 c->open_confirm(c->self, 0, c->open_confirm_ctx);
3008 debug2("callback done");
3009 }
Damien Miller33b13562000-04-04 14:38:59 +10003010 }
Damien Miller48b03fc2002-01-22 23:11:40 +11003011 packet_check_eom();
Damien Miller7a606212009-01-28 16:22:34 +11003012 /* Schedule the channel for cleanup/deletion. */
3013 chan_mark_dead(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003014 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003015}
3016
Damien Millerd79b4242006-03-31 23:11:44 +11003017/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003018int
Damien Miller630d6f42002-01-22 23:17:30 +11003019channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10003020{
3021 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00003022 int id;
djm@openbsd.org629df772015-06-30 05:25:07 +00003023 u_int adjust, tmp;
Damien Miller33b13562000-04-04 14:38:59 +10003024
3025 if (!compat20)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003026 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003027
3028 /* Get the channel number and verify it. */
3029 id = packet_get_int();
3030 c = channel_lookup(id);
3031
Damien Millerd47c62a2005-12-13 19:33:57 +11003032 if (c == NULL) {
3033 logit("Received window adjust for non-open channel %d.", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003034 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003035 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00003036 if (channel_proxy_upstream(c, type, seq, ctxt))
3037 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003038 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11003039 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00003040 debug2("channel %d: rcvd adjust %u", id, adjust);
djm@openbsd.org629df772015-06-30 05:25:07 +00003041 if ((tmp = c->remote_window + adjust) < c->remote_window)
3042 fatal("channel %d: adjust %u overflows remote window %u",
3043 id, adjust, c->remote_window);
3044 c->remote_window = tmp;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003045 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10003046}
3047
Damien Millerd79b4242006-03-31 23:11:44 +11003048/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003049int
Damien Miller630d6f42002-01-22 23:17:30 +11003050channel_input_port_open(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003051{
Ben Lindstrome9c99912001-06-09 00:41:05 +00003052 Channel *c = NULL;
3053 u_short host_port;
3054 char *host, *originator_string;
Damien Millerbd740252008-05-19 15:37:09 +10003055 int remote_id;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003056
Ben Lindstrome9c99912001-06-09 00:41:05 +00003057 remote_id = packet_get_int();
3058 host = packet_get_string(NULL);
3059 host_port = packet_get_int();
3060
3061 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
3062 originator_string = packet_get_string(NULL);
3063 } else {
3064 originator_string = xstrdup("unknown (remote did not supply name)");
3065 }
Damien Miller48b03fc2002-01-22 23:11:40 +11003066 packet_check_eom();
Damien Miller7acefbb2014-07-18 14:11:24 +10003067 c = channel_connect_to_port(host, host_port,
Damien Millerbd740252008-05-19 15:37:09 +10003068 "connected socket", originator_string);
Darren Tuckera627d422013-06-02 07:31:17 +10003069 free(originator_string);
3070 free(host);
Ben Lindstrome9c99912001-06-09 00:41:05 +00003071 if (c == NULL) {
3072 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
3073 packet_put_int(remote_id);
3074 packet_send();
Damien Millerbd740252008-05-19 15:37:09 +10003075 } else
3076 c->remote_id = remote_id;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003077 return 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +00003078}
3079
Damien Millerb84886b2008-05-19 15:05:07 +10003080/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003081int
Damien Millerb84886b2008-05-19 15:05:07 +10003082channel_input_status_confirm(int type, u_int32_t seq, void *ctxt)
3083{
3084 Channel *c;
3085 struct channel_confirm *cc;
Damien Miller16a73072008-12-08 09:55:25 +11003086 int id;
Damien Millerb84886b2008-05-19 15:05:07 +10003087
3088 /* Reset keepalive timeout */
Darren Tuckerf7288d72009-06-21 18:12:20 +10003089 packet_set_alive_timeouts(0);
Damien Millerb84886b2008-05-19 15:05:07 +10003090
Damien Miller16a73072008-12-08 09:55:25 +11003091 id = packet_get_int();
Damien Miller16a73072008-12-08 09:55:25 +11003092 debug2("channel_input_status_confirm: type %d id %d", type, id);
Damien Millerb84886b2008-05-19 15:05:07 +10003093
Damien Miller16a73072008-12-08 09:55:25 +11003094 if ((c = channel_lookup(id)) == NULL) {
3095 logit("channel_input_status_confirm: %d: unknown", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003096 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10003097 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00003098 if (channel_proxy_upstream(c, type, seq, ctxt))
3099 return 0;
3100 packet_check_eom();
Damien Millerb84886b2008-05-19 15:05:07 +10003101 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003102 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10003103 cc->cb(type, c, cc->ctx);
3104 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +11003105 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +10003106 free(cc);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00003107 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10003108}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00003109
Ben Lindstrome9c99912001-06-09 00:41:05 +00003110/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003111
Ben Lindstrom908afed2001-10-03 17:34:59 +00003112void
3113channel_set_af(int af)
3114{
3115 IPv4or6 = af;
3116}
3117
Damien Millerf6dff7c2011-09-22 21:38:52 +10003118
3119/*
3120 * Determine whether or not a port forward listens to loopback, the
3121 * specified address or wildcard. On the client, a specified bind
3122 * address will always override gateway_ports. On the server, a
3123 * gateway_ports of 1 (``yes'') will override the client's specification
3124 * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
3125 * will bind to whatever address the client asked for.
3126 *
3127 * Special-case listen_addrs are:
3128 *
3129 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
3130 * "" (empty string), "*" -> wildcard v4/v6
3131 * "localhost" -> loopback v4/v6
Damien Miller602943d2014-07-04 08:59:41 +10003132 * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
Damien Millerf6dff7c2011-09-22 21:38:52 +10003133 */
3134static const char *
3135channel_fwd_bind_addr(const char *listen_addr, int *wildcardp,
Damien Miller7acefbb2014-07-18 14:11:24 +10003136 int is_client, struct ForwardOptions *fwd_opts)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003137{
3138 const char *addr = NULL;
3139 int wildcard = 0;
3140
3141 if (listen_addr == NULL) {
3142 /* No address specified: default to gateway_ports setting */
Damien Miller7acefbb2014-07-18 14:11:24 +10003143 if (fwd_opts->gateway_ports)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003144 wildcard = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10003145 } else if (fwd_opts->gateway_ports || is_client) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003146 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
3147 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
3148 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +10003149 (!is_client && fwd_opts->gateway_ports == 1)) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10003150 wildcard = 1;
Darren Tucker71152bc2013-10-10 10:27:21 +11003151 /*
3152 * Notify client if they requested a specific listen
3153 * address and it was overridden.
3154 */
3155 if (*listen_addr != '\0' &&
3156 strcmp(listen_addr, "0.0.0.0") != 0 &&
3157 strcmp(listen_addr, "*") != 0) {
3158 packet_send_debug("Forwarding listen address "
3159 "\"%s\" overridden by server "
3160 "GatewayPorts", listen_addr);
3161 }
Damien Miller602943d2014-07-04 08:59:41 +10003162 } else if (strcmp(listen_addr, "localhost") != 0 ||
3163 strcmp(listen_addr, "127.0.0.1") == 0 ||
3164 strcmp(listen_addr, "::1") == 0) {
3165 /* Accept localhost address when GatewayPorts=yes */
Damien Millere84d1032014-05-21 17:13:36 +10003166 addr = listen_addr;
Damien Miller602943d2014-07-04 08:59:41 +10003167 }
3168 } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
3169 strcmp(listen_addr, "::1") == 0) {
3170 /*
3171 * If a specific IPv4/IPv6 localhost address has been
3172 * requested then accept it even if gateway_ports is in
3173 * effect. This allows the client to prefer IPv4 or IPv6.
3174 */
3175 addr = listen_addr;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003176 }
3177 if (wildcardp != NULL)
3178 *wildcardp = wildcard;
3179 return addr;
3180}
3181
Damien Millerb16461c2002-01-22 23:29:22 +11003182static int
Damien Miller7acefbb2014-07-18 14:11:24 +10003183channel_setup_fwd_listener_tcpip(int type, struct Forward *fwd,
3184 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Miller0bc1bd82000-11-13 22:57:25 +11003185{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00003186 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11003187 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11003188 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003189 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11003190 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller4bf648f2009-02-14 16:28:21 +11003191 in_port_t *lport_p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003192
Damien Millerf91ee4c2005-03-01 21:24:33 +11003193 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00003194
millert@openbsd.orgec04dc42015-06-05 15:13:13 +00003195 if (is_client && fwd->connect_path != NULL) {
3196 host = fwd->connect_path;
3197 } else {
3198 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
3199 fwd->listen_host : fwd->connect_host;
3200 if (host == NULL) {
3201 error("No forward host name.");
3202 return 0;
3203 }
3204 if (strlen(host) >= NI_MAXHOST) {
3205 error("Forward host name too long.");
3206 return 0;
3207 }
Kevin Steves12057502001-02-05 14:54:34 +00003208 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003209
Damien Millerf6dff7c2011-09-22 21:38:52 +10003210 /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
Damien Miller7acefbb2014-07-18 14:11:24 +10003211 addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard,
3212 is_client, fwd_opts);
3213 debug3("%s: type %d wildcard %d addr %s", __func__,
Damien Millerf91ee4c2005-03-01 21:24:33 +11003214 type, wildcard, (addr == NULL) ? "NULL" : addr);
3215
3216 /*
Damien Miller34132e52000-01-14 15:45:46 +11003217 * getaddrinfo returns a loopback address if the hostname is
3218 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11003219 */
Damien Miller34132e52000-01-14 15:45:46 +11003220 memset(&hints, 0, sizeof(hints));
3221 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003222 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11003223 hints.ai_socktype = SOCK_STREAM;
Damien Miller7acefbb2014-07-18 14:11:24 +10003224 snprintf(strport, sizeof strport, "%d", fwd->listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11003225 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
3226 if (addr == NULL) {
3227 /* This really shouldn't happen */
3228 packet_disconnect("getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11003229 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003230 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10003231 error("%s: getaddrinfo(%.64s): %s", __func__, addr,
Darren Tucker4abde772007-12-29 02:43:51 +11003232 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003233 }
Damien Millera7270302005-07-06 09:36:05 +10003234 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003235 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003236 if (allocated_listen_port != NULL)
3237 *allocated_listen_port = 0;
Damien Miller34132e52000-01-14 15:45:46 +11003238 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller4bf648f2009-02-14 16:28:21 +11003239 switch (ai->ai_family) {
3240 case AF_INET:
3241 lport_p = &((struct sockaddr_in *)ai->ai_addr)->
3242 sin_port;
3243 break;
3244 case AF_INET6:
3245 lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
3246 sin6_port;
3247 break;
3248 default:
Damien Miller34132e52000-01-14 15:45:46 +11003249 continue;
Damien Miller4bf648f2009-02-14 16:28:21 +11003250 }
3251 /*
3252 * If allocating a port for -R forwards, then use the
3253 * same port for all address families.
3254 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003255 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11003256 allocated_listen_port != NULL && *allocated_listen_port > 0)
3257 *lport_p = htons(*allocated_listen_port);
3258
Damien Miller34132e52000-01-14 15:45:46 +11003259 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
3260 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003261 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +11003262 continue;
3263 }
3264 /* Create a port to listen for the host. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11003265 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003266 if (sock < 0) {
3267 /* this is no error since kernel may not support ipv6 */
3268 verbose("socket: %.100s", strerror(errno));
3269 continue;
3270 }
Damien Miller5e7fd072005-11-05 14:53:39 +11003271
3272 channel_set_reuseaddr(sock);
Damien Miller04ee0f82009-11-18 17:48:30 +11003273 if (ai->ai_family == AF_INET6)
3274 sock_set_v6only(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10003275
Damien Miller4bf648f2009-02-14 16:28:21 +11003276 debug("Local forwarding listening on %s port %s.",
3277 ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11003278
Damien Miller34132e52000-01-14 15:45:46 +11003279 /* Bind the socket to the address. */
3280 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
3281 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11003282 if (!ai->ai_next)
3283 error("bind: %.100s", strerror(errno));
3284 else
3285 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00003286
Damien Miller34132e52000-01-14 15:45:46 +11003287 close(sock);
3288 continue;
3289 }
3290 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11003291 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11003292 error("listen: %.100s", strerror(errno));
3293 close(sock);
3294 continue;
3295 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003296
3297 /*
Damien Miller7acefbb2014-07-18 14:11:24 +10003298 * fwd->listen_port == 0 requests a dynamically allocated port -
Damien Miller4bf648f2009-02-14 16:28:21 +11003299 * record what we got.
3300 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003301 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11003302 allocated_listen_port != NULL &&
3303 *allocated_listen_port == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +00003304 *allocated_listen_port = get_local_port(sock);
Damien Miller4bf648f2009-02-14 16:28:21 +11003305 debug("Allocated listen port %d",
3306 *allocated_listen_port);
3307 }
3308
Damien Miller34132e52000-01-14 15:45:46 +11003309 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00003310 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10003311 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003312 0, "port listener", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +11003313 c->path = xstrdup(host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003314 c->host_port = fwd->connect_port;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003315 c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
Damien Miller7acefbb2014-07-18 14:11:24 +10003316 if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
Darren Tucker68afb8c2011-10-02 18:59:03 +11003317 !(datafellows & SSH_BUG_DYNAMIC_RPORT))
3318 c->listening_port = *allocated_listen_port;
3319 else
Damien Miller7acefbb2014-07-18 14:11:24 +10003320 c->listening_port = fwd->listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11003321 success = 1;
3322 }
3323 if (success == 0)
Damien Miller7acefbb2014-07-18 14:11:24 +10003324 error("%s: cannot listen to port: %d", __func__,
3325 fwd->listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11003326 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00003327 return success;
Damien Miller95def091999-11-25 00:26:21 +11003328}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003329
Damien Miller7acefbb2014-07-18 14:11:24 +10003330static int
3331channel_setup_fwd_listener_streamlocal(int type, struct Forward *fwd,
3332 struct ForwardOptions *fwd_opts)
3333{
3334 struct sockaddr_un sunaddr;
3335 const char *path;
3336 Channel *c;
3337 int port, sock;
3338 mode_t omask;
3339
3340 switch (type) {
3341 case SSH_CHANNEL_UNIX_LISTENER:
3342 if (fwd->connect_path != NULL) {
3343 if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3344 error("Local connecting path too long: %s",
3345 fwd->connect_path);
3346 return 0;
3347 }
3348 path = fwd->connect_path;
3349 port = PORT_STREAMLOCAL;
3350 } else {
3351 if (fwd->connect_host == NULL) {
3352 error("No forward host name.");
3353 return 0;
3354 }
3355 if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3356 error("Forward host name too long.");
3357 return 0;
3358 }
3359 path = fwd->connect_host;
3360 port = fwd->connect_port;
3361 }
3362 break;
3363 case SSH_CHANNEL_RUNIX_LISTENER:
3364 path = fwd->listen_path;
3365 port = PORT_STREAMLOCAL;
3366 break;
3367 default:
3368 error("%s: unexpected channel type %d", __func__, type);
3369 return 0;
3370 }
3371
3372 if (fwd->listen_path == NULL) {
3373 error("No forward path name.");
3374 return 0;
3375 }
3376 if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3377 error("Local listening path too long: %s", fwd->listen_path);
3378 return 0;
3379 }
3380
3381 debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
3382
3383 /* Start a Unix domain listener. */
3384 omask = umask(fwd_opts->streamlocal_bind_mask);
3385 sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3386 fwd_opts->streamlocal_bind_unlink);
3387 umask(omask);
3388 if (sock < 0)
3389 return 0;
3390
3391 debug("Local forwarding listening on path %s.", fwd->listen_path);
3392
3393 /* Allocate a channel number for the socket. */
3394 c = channel_new("unix listener", type, sock, sock, -1,
3395 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3396 0, "unix listener", 1);
3397 c->path = xstrdup(path);
3398 c->host_port = port;
3399 c->listening_port = PORT_STREAMLOCAL;
3400 c->listening_addr = xstrdup(fwd->listen_path);
3401 return 1;
3402}
3403
3404static int
3405channel_cancel_rport_listener_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003406{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003407 u_int i;
3408 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003409
Darren Tucker47eede72005-03-14 23:08:12 +11003410 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10003411 Channel *c = channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003412 if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3413 continue;
3414 if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3415 debug2("%s: close channel %d", __func__, i);
3416 channel_free(c);
3417 found = 1;
3418 }
3419 }
Darren Tuckere7066df2004-05-24 10:18:05 +10003420
Damien Millerf6dff7c2011-09-22 21:38:52 +10003421 return (found);
3422}
3423
Damien Miller7acefbb2014-07-18 14:11:24 +10003424static int
3425channel_cancel_rport_listener_streamlocal(const char *path)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003426{
3427 u_int i;
3428 int found = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003429
3430 for (i = 0; i < channels_alloc; i++) {
3431 Channel *c = channels[i];
3432 if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3433 continue;
3434 if (c->path == NULL)
3435 continue;
3436 if (strcmp(c->path, path) == 0) {
3437 debug2("%s: close channel %d", __func__, i);
3438 channel_free(c);
3439 found = 1;
3440 }
3441 }
3442
3443 return (found);
3444}
3445
3446int
3447channel_cancel_rport_listener(struct Forward *fwd)
3448{
3449 if (fwd->listen_path != NULL)
3450 return channel_cancel_rport_listener_streamlocal(fwd->listen_path);
3451 else
3452 return channel_cancel_rport_listener_tcpip(fwd->listen_host, fwd->listen_port);
3453}
3454
3455static int
3456channel_cancel_lport_listener_tcpip(const char *lhost, u_short lport,
3457 int cport, struct ForwardOptions *fwd_opts)
3458{
3459 u_int i;
3460 int found = 0;
3461 const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003462
3463 for (i = 0; i < channels_alloc; i++) {
3464 Channel *c = channels[i];
3465 if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3466 continue;
Damien Millerff773642011-09-22 21:39:48 +10003467 if (c->listening_port != lport)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003468 continue;
Damien Millerff773642011-09-22 21:39:48 +10003469 if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3470 /* skip dynamic forwardings */
3471 if (c->host_port == 0)
3472 continue;
3473 } else {
3474 if (c->host_port != cport)
3475 continue;
3476 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10003477 if ((c->listening_addr == NULL && addr != NULL) ||
3478 (c->listening_addr != NULL && addr == NULL))
3479 continue;
3480 if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10003481 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10003482 channel_free(c);
3483 found = 1;
3484 }
3485 }
3486
3487 return (found);
3488}
3489
Damien Miller7acefbb2014-07-18 14:11:24 +10003490static int
3491channel_cancel_lport_listener_streamlocal(const char *path)
3492{
3493 u_int i;
3494 int found = 0;
3495
3496 if (path == NULL) {
3497 error("%s: no path specified.", __func__);
3498 return 0;
3499 }
3500
3501 for (i = 0; i < channels_alloc; i++) {
3502 Channel *c = channels[i];
3503 if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3504 continue;
3505 if (c->listening_addr == NULL)
3506 continue;
3507 if (strcmp(c->listening_addr, path) == 0) {
3508 debug2("%s: close channel %d", __func__, i);
3509 channel_free(c);
3510 found = 1;
3511 }
3512 }
3513
3514 return (found);
3515}
3516
3517int
3518channel_cancel_lport_listener(struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
3519{
3520 if (fwd->listen_path != NULL)
3521 return channel_cancel_lport_listener_streamlocal(fwd->listen_path);
3522 else
3523 return channel_cancel_lport_listener_tcpip(fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3524}
3525
Damien Millerb16461c2002-01-22 23:29:22 +11003526/* protocol local port fwd, used by ssh (and sshd in v1) */
3527int
Damien Miller7acefbb2014-07-18 14:11:24 +10003528channel_setup_local_fwd_listener(struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003529{
Damien Miller7acefbb2014-07-18 14:11:24 +10003530 if (fwd->listen_path != NULL) {
3531 return channel_setup_fwd_listener_streamlocal(
3532 SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3533 } else {
3534 return channel_setup_fwd_listener_tcpip(SSH_CHANNEL_PORT_LISTENER,
3535 fwd, NULL, fwd_opts);
3536 }
Damien Millerb16461c2002-01-22 23:29:22 +11003537}
3538
3539/* protocol v2 remote port fwd, used by sshd */
3540int
Damien Miller7acefbb2014-07-18 14:11:24 +10003541channel_setup_remote_fwd_listener(struct Forward *fwd,
3542 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003543{
Damien Miller7acefbb2014-07-18 14:11:24 +10003544 if (fwd->listen_path != NULL) {
3545 return channel_setup_fwd_listener_streamlocal(
3546 SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3547 } else {
3548 return channel_setup_fwd_listener_tcpip(
3549 SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3550 fwd_opts);
3551 }
Damien Millerb16461c2002-01-22 23:29:22 +11003552}
3553
Damien Miller5428f641999-11-25 11:54:57 +11003554/*
Damien Millerf6dff7c2011-09-22 21:38:52 +10003555 * Translate the requested rfwd listen host to something usable for
3556 * this server.
3557 */
3558static const char *
3559channel_rfwd_bind_host(const char *listen_host)
3560{
3561 if (listen_host == NULL) {
3562 if (datafellows & SSH_BUG_RFWD_ADDR)
3563 return "127.0.0.1";
3564 else
3565 return "localhost";
3566 } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
3567 if (datafellows & SSH_BUG_RFWD_ADDR)
3568 return "0.0.0.0";
3569 else
3570 return "";
3571 } else
3572 return listen_host;
3573}
3574
3575/*
Damien Miller5428f641999-11-25 11:54:57 +11003576 * Initiate forwarding of connections to port "port" on remote host through
3577 * the secure channel to host:port from local side.
Darren Tucker68afb8c2011-10-02 18:59:03 +11003578 * Returns handle (index) for updating the dynamic listen port with
3579 * channel_update_permitted_opens().
Damien Miller5428f641999-11-25 11:54:57 +11003580 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003581int
Damien Miller7acefbb2014-07-18 14:11:24 +10003582channel_request_remote_forwarding(struct Forward *fwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003583{
Darren Tucker68afb8c2011-10-02 18:59:03 +11003584 int type, success = 0, idx = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003585
Damien Miller95def091999-11-25 00:26:21 +11003586 /* Send the forward request to the remote side. */
Damien Miller33b13562000-04-04 14:38:59 +10003587 if (compat20) {
Damien Miller33b13562000-04-04 14:38:59 +10003588 packet_start(SSH2_MSG_GLOBAL_REQUEST);
Damien Miller7acefbb2014-07-18 14:11:24 +10003589 if (fwd->listen_path != NULL) {
3590 packet_put_cstring("streamlocal-forward@openssh.com");
3591 packet_put_char(1); /* boolean: want reply */
3592 packet_put_cstring(fwd->listen_path);
3593 } else {
3594 packet_put_cstring("tcpip-forward");
3595 packet_put_char(1); /* boolean: want reply */
3596 packet_put_cstring(channel_rfwd_bind_host(fwd->listen_host));
3597 packet_put_int(fwd->listen_port);
3598 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11003599 packet_send();
3600 packet_write_wait();
3601 /* Assume that server accepts the request */
3602 success = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10003603 } else if (fwd->listen_path == NULL) {
Damien Miller33b13562000-04-04 14:38:59 +10003604 packet_start(SSH_CMSG_PORT_FORWARD_REQUEST);
Damien Miller7acefbb2014-07-18 14:11:24 +10003605 packet_put_int(fwd->listen_port);
3606 packet_put_cstring(fwd->connect_host);
3607 packet_put_int(fwd->connect_port);
Damien Miller33b13562000-04-04 14:38:59 +10003608 packet_send();
3609 packet_write_wait();
Damien Miller0bc1bd82000-11-13 22:57:25 +11003610
3611 /* Wait for response from the remote side. */
Damien Millerdff50992002-01-22 23:16:32 +11003612 type = packet_read();
Damien Miller0bc1bd82000-11-13 22:57:25 +11003613 switch (type) {
3614 case SSH_SMSG_SUCCESS:
3615 success = 1;
3616 break;
3617 case SSH_SMSG_FAILURE:
Damien Miller0bc1bd82000-11-13 22:57:25 +11003618 break;
3619 default:
3620 /* Unknown packet */
3621 packet_disconnect("Protocol error for port forward request:"
3622 "received packet type %d.", type);
3623 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003624 } else {
3625 logit("Warning: Server does not support remote stream local forwarding.");
Damien Miller0bc1bd82000-11-13 22:57:25 +11003626 }
3627 if (success) {
Damien Miller232cfb12010-06-26 09:50:30 +10003628 /* Record that connection to this host/port is permitted. */
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003629 permitted_opens = xreallocarray(permitted_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003630 num_permitted_opens + 1, sizeof(*permitted_opens));
Darren Tucker68afb8c2011-10-02 18:59:03 +11003631 idx = num_permitted_opens++;
Damien Miller7acefbb2014-07-18 14:11:24 +10003632 if (fwd->connect_path != NULL) {
3633 permitted_opens[idx].host_to_connect =
3634 xstrdup(fwd->connect_path);
3635 permitted_opens[idx].port_to_connect =
3636 PORT_STREAMLOCAL;
3637 } else {
3638 permitted_opens[idx].host_to_connect =
3639 xstrdup(fwd->connect_host);
3640 permitted_opens[idx].port_to_connect =
3641 fwd->connect_port;
3642 }
3643 if (fwd->listen_path != NULL) {
3644 permitted_opens[idx].listen_host = NULL;
3645 permitted_opens[idx].listen_path =
3646 xstrdup(fwd->listen_path);
3647 permitted_opens[idx].listen_port = PORT_STREAMLOCAL;
3648 } else {
3649 permitted_opens[idx].listen_host =
3650 fwd->listen_host ? xstrdup(fwd->listen_host) : NULL;
3651 permitted_opens[idx].listen_path = NULL;
3652 permitted_opens[idx].listen_port = fwd->listen_port;
3653 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00003654 permitted_opens[idx].downstream = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10003655 }
Darren Tucker68afb8c2011-10-02 18:59:03 +11003656 return (idx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003657}
3658
Damien Miller4b3ed642014-07-02 15:29:40 +10003659static int
3660open_match(ForwardPermission *allowed_open, const char *requestedhost,
Damien Miller7acefbb2014-07-18 14:11:24 +10003661 int requestedport)
Damien Miller4b3ed642014-07-02 15:29:40 +10003662{
3663 if (allowed_open->host_to_connect == NULL)
3664 return 0;
3665 if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3666 allowed_open->port_to_connect != requestedport)
3667 return 0;
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +00003668 if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
3669 strcmp(allowed_open->host_to_connect, requestedhost) != 0)
Damien Miller4b3ed642014-07-02 15:29:40 +10003670 return 0;
3671 return 1;
3672}
3673
3674/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003675 * Note that in the listen host/port case
Damien Miller4b3ed642014-07-02 15:29:40 +10003676 * we don't support FWD_PERMIT_ANY_PORT and
3677 * need to translate between the configured-host (listen_host)
3678 * and what we've sent to the remote server (channel_rfwd_bind_host)
3679 */
3680static int
Damien Miller7acefbb2014-07-18 14:11:24 +10003681open_listen_match_tcpip(ForwardPermission *allowed_open,
3682 const char *requestedhost, u_short requestedport, int translate)
Damien Miller4b3ed642014-07-02 15:29:40 +10003683{
3684 const char *allowed_host;
3685
3686 if (allowed_open->host_to_connect == NULL)
3687 return 0;
3688 if (allowed_open->listen_port != requestedport)
3689 return 0;
Damien Miller3a48cc02014-07-06 09:32:49 +10003690 if (!translate && allowed_open->listen_host == NULL &&
3691 requestedhost == NULL)
3692 return 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003693 allowed_host = translate ?
3694 channel_rfwd_bind_host(allowed_open->listen_host) :
3695 allowed_open->listen_host;
3696 if (allowed_host == NULL ||
3697 strcmp(allowed_host, requestedhost) != 0)
3698 return 0;
3699 return 1;
3700}
3701
Damien Miller7acefbb2014-07-18 14:11:24 +10003702static int
3703open_listen_match_streamlocal(ForwardPermission *allowed_open,
3704 const char *requestedpath)
3705{
3706 if (allowed_open->host_to_connect == NULL)
3707 return 0;
3708 if (allowed_open->listen_port != PORT_STREAMLOCAL)
3709 return 0;
3710 if (allowed_open->listen_path == NULL ||
3711 strcmp(allowed_open->listen_path, requestedpath) != 0)
3712 return 0;
3713 return 1;
3714}
3715
Damien Miller5428f641999-11-25 11:54:57 +11003716/*
Darren Tuckerfc959702004-07-17 16:12:08 +10003717 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10003718 * local side.
3719 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003720static int
3721channel_request_rforward_cancel_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003722{
3723 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10003724
3725 if (!compat20)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003726 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003727
3728 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003729 if (open_listen_match_tcpip(&permitted_opens[i], host, port, 0))
Darren Tuckere7066df2004-05-24 10:18:05 +10003730 break;
3731 }
3732 if (i >= num_permitted_opens) {
3733 debug("%s: requested forward not found", __func__);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003734 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003735 }
3736 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3737 packet_put_cstring("cancel-tcpip-forward");
3738 packet_put_char(0);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003739 packet_put_cstring(channel_rfwd_bind_host(host));
Darren Tuckere7066df2004-05-24 10:18:05 +10003740 packet_put_int(port);
3741 packet_send();
3742
Damien Miller4b3ed642014-07-02 15:29:40 +10003743 permitted_opens[i].listen_port = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003744 permitted_opens[i].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003745 free(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10003746 permitted_opens[i].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003747 free(permitted_opens[i].listen_host);
3748 permitted_opens[i].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003749 permitted_opens[i].listen_path = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003750 permitted_opens[i].downstream = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003751
3752 return 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003753}
3754
3755/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003756 * Request cancellation of remote forwarding of Unix domain socket
3757 * path from local side.
3758 */
3759static int
3760channel_request_rforward_cancel_streamlocal(const char *path)
3761{
3762 int i;
3763
3764 if (!compat20)
3765 return -1;
3766
3767 for (i = 0; i < num_permitted_opens; i++) {
3768 if (open_listen_match_streamlocal(&permitted_opens[i], path))
3769 break;
3770 }
3771 if (i >= num_permitted_opens) {
3772 debug("%s: requested forward not found", __func__);
3773 return -1;
3774 }
3775 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3776 packet_put_cstring("cancel-streamlocal-forward@openssh.com");
3777 packet_put_char(0);
3778 packet_put_cstring(path);
3779 packet_send();
3780
3781 permitted_opens[i].listen_port = 0;
3782 permitted_opens[i].port_to_connect = 0;
3783 free(permitted_opens[i].host_to_connect);
3784 permitted_opens[i].host_to_connect = NULL;
3785 permitted_opens[i].listen_host = NULL;
3786 free(permitted_opens[i].listen_path);
3787 permitted_opens[i].listen_path = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003788 permitted_opens[i].downstream = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003789
3790 return 0;
3791}
3792
3793/*
3794 * Request cancellation of remote forwarding of a connection from local side.
3795 */
3796int
3797channel_request_rforward_cancel(struct Forward *fwd)
3798{
3799 if (fwd->listen_path != NULL) {
3800 return (channel_request_rforward_cancel_streamlocal(
3801 fwd->listen_path));
3802 } else {
3803 return (channel_request_rforward_cancel_tcpip(fwd->listen_host,
3804 fwd->listen_port ? fwd->listen_port : fwd->allocated_port));
3805 }
3806}
3807
3808/*
Damien Miller5428f641999-11-25 11:54:57 +11003809 * This is called after receiving CHANNEL_FORWARDING_REQUEST. This initates
3810 * listening for the port, and sends back a success reply (or disconnect
Darren Tuckere7d4b192006-07-12 22:17:10 +10003811 * message if there was an error).
Damien Miller5428f641999-11-25 11:54:57 +11003812 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003813int
Damien Miller7acefbb2014-07-18 14:11:24 +10003814channel_input_port_forward_request(int is_root, struct ForwardOptions *fwd_opts)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003815{
Darren Tuckere7d4b192006-07-12 22:17:10 +10003816 int success = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003817 struct Forward fwd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003818
Damien Miller95def091999-11-25 00:26:21 +11003819 /* Get arguments from the packet. */
Damien Miller7acefbb2014-07-18 14:11:24 +10003820 memset(&fwd, 0, sizeof(fwd));
3821 fwd.listen_port = packet_get_int();
3822 fwd.connect_host = packet_get_string(NULL);
3823 fwd.connect_port = packet_get_int();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003824
Damien Millerbac2d8a2000-09-05 16:13:06 +11003825#ifndef HAVE_CYGWIN
Damien Miller5428f641999-11-25 11:54:57 +11003826 /*
3827 * Check that an unprivileged user is not trying to forward a
3828 * privileged port.
3829 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003830 if (fwd.listen_port < IPPORT_RESERVED && !is_root)
Darren Tucker9189ff82003-07-03 13:52:04 +10003831 packet_disconnect(
3832 "Requested forwarding of port %d but user is not root.",
Damien Miller7acefbb2014-07-18 14:11:24 +10003833 fwd.listen_port);
3834 if (fwd.connect_port == 0)
Darren Tucker9189ff82003-07-03 13:52:04 +10003835 packet_disconnect("Dynamic forwarding denied.");
Damien Millerbac2d8a2000-09-05 16:13:06 +11003836#endif
Darren Tucker9189ff82003-07-03 13:52:04 +10003837
Damien Miller0bc1bd82000-11-13 22:57:25 +11003838 /* Initiate forwarding */
Damien Miller7acefbb2014-07-18 14:11:24 +10003839 success = channel_setup_local_fwd_listener(&fwd, fwd_opts);
Damien Miller95def091999-11-25 00:26:21 +11003840
3841 /* Free the argument string. */
Damien Miller7acefbb2014-07-18 14:11:24 +10003842 free(fwd.connect_host);
Darren Tuckere7d4b192006-07-12 22:17:10 +10003843
3844 return (success ? 0 : -1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003845}
3846
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003847/*
3848 * Permits opening to any host/port if permitted_opens[] is empty. This is
3849 * usually called by the server, because the user could connect to any port
3850 * anyway, and the server has no way to know but to trust the client anyway.
3851 */
3852void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003853channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003854{
3855 if (num_permitted_opens == 0)
3856 all_opens_permitted = 1;
3857}
3858
Ben Lindstroma3700052001-04-05 23:26:32 +00003859void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003860channel_add_permitted_opens(char *host, int port)
3861{
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003862 debug("allow port forwarding to host %s port %d", host, port);
3863
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003864 permitted_opens = xreallocarray(permitted_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003865 num_permitted_opens + 1, sizeof(*permitted_opens));
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003866 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
3867 permitted_opens[num_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003868 permitted_opens[num_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003869 permitted_opens[num_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003870 permitted_opens[num_permitted_opens].listen_port = 0;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003871 permitted_opens[num_permitted_opens].downstream = NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003872 num_permitted_opens++;
3873
3874 all_opens_permitted = 0;
3875}
3876
Darren Tucker68afb8c2011-10-02 18:59:03 +11003877/*
3878 * Update the listen port for a dynamic remote forward, after
3879 * the actual 'newport' has been allocated. If 'newport' < 0 is
3880 * passed then they entry will be invalidated.
3881 */
3882void
3883channel_update_permitted_opens(int idx, int newport)
3884{
3885 if (idx < 0 || idx >= num_permitted_opens) {
3886 debug("channel_update_permitted_opens: index out of range:"
3887 " %d num_permitted_opens %d", idx, num_permitted_opens);
3888 return;
3889 }
3890 debug("%s allowed port %d for forwarding to host %s port %d",
3891 newport > 0 ? "Updating" : "Removing",
3892 newport,
3893 permitted_opens[idx].host_to_connect,
3894 permitted_opens[idx].port_to_connect);
3895 if (newport >= 0) {
3896 permitted_opens[idx].listen_port =
3897 (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
3898 } else {
3899 permitted_opens[idx].listen_port = 0;
3900 permitted_opens[idx].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003901 free(permitted_opens[idx].host_to_connect);
Darren Tucker68afb8c2011-10-02 18:59:03 +11003902 permitted_opens[idx].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003903 free(permitted_opens[idx].listen_host);
3904 permitted_opens[idx].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003905 free(permitted_opens[idx].listen_path);
3906 permitted_opens[idx].listen_path = NULL;
Darren Tucker68afb8c2011-10-02 18:59:03 +11003907 }
3908}
3909
Damien Millera765cf42006-07-24 14:08:13 +10003910int
Damien Miller9b439df2006-07-24 14:04:00 +10003911channel_add_adm_permitted_opens(char *host, int port)
3912{
Damien Millera765cf42006-07-24 14:08:13 +10003913 debug("config allows port forwarding to host %s port %d", host, port);
Damien Miller9b439df2006-07-24 14:04:00 +10003914
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003915 permitted_adm_opens = xreallocarray(permitted_adm_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003916 num_adm_permitted_opens + 1, sizeof(*permitted_adm_opens));
Damien Miller9b439df2006-07-24 14:04:00 +10003917 permitted_adm_opens[num_adm_permitted_opens].host_to_connect
3918 = xstrdup(host);
3919 permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003920 permitted_adm_opens[num_adm_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003921 permitted_adm_opens[num_adm_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003922 permitted_adm_opens[num_adm_permitted_opens].listen_port = 0;
Damien Millera765cf42006-07-24 14:08:13 +10003923 return ++num_adm_permitted_opens;
Damien Miller9b439df2006-07-24 14:04:00 +10003924}
3925
3926void
Damien Millerc6081482012-04-22 11:18:53 +10003927channel_disable_adm_local_opens(void)
3928{
Damien Milleraa5b3f82012-12-03 09:50:54 +11003929 channel_clear_adm_permitted_opens();
dtucker@openbsd.org297060f2015-05-08 03:25:07 +00003930 permitted_adm_opens = xcalloc(sizeof(*permitted_adm_opens), 1);
Damien Milleraa5b3f82012-12-03 09:50:54 +11003931 permitted_adm_opens[num_adm_permitted_opens].host_to_connect = NULL;
3932 num_adm_permitted_opens = 1;
Damien Millerc6081482012-04-22 11:18:53 +10003933}
3934
3935void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003936channel_clear_permitted_opens(void)
3937{
3938 int i;
3939
Damien Miller4b3ed642014-07-02 15:29:40 +10003940 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003941 free(permitted_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003942 free(permitted_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003943 free(permitted_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003944 }
Darren Tuckera627d422013-06-02 07:31:17 +10003945 free(permitted_opens);
3946 permitted_opens = NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003947 num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10003948}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003949
Damien Miller9b439df2006-07-24 14:04:00 +10003950void
3951channel_clear_adm_permitted_opens(void)
3952{
3953 int i;
3954
Damien Miller4b3ed642014-07-02 15:29:40 +10003955 for (i = 0; i < num_adm_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003956 free(permitted_adm_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003957 free(permitted_adm_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003958 free(permitted_adm_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003959 }
Darren Tuckera627d422013-06-02 07:31:17 +10003960 free(permitted_adm_opens);
3961 permitted_adm_opens = NULL;
Damien Miller9b439df2006-07-24 14:04:00 +10003962 num_adm_permitted_opens = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003963}
3964
Darren Tuckere7140f22008-06-10 23:01:51 +10003965void
3966channel_print_adm_permitted_opens(void)
3967{
Damien Miller6ef17492008-07-16 22:42:06 +10003968 int i;
Darren Tuckere7140f22008-06-10 23:01:51 +10003969
Damien Millerb53d8a12009-01-28 16:13:04 +11003970 printf("permitopen");
Darren Tucker22662e82008-11-11 16:40:22 +11003971 if (num_adm_permitted_opens == 0) {
Damien Millerb53d8a12009-01-28 16:13:04 +11003972 printf(" any\n");
Darren Tucker22662e82008-11-11 16:40:22 +11003973 return;
3974 }
Darren Tuckere7140f22008-06-10 23:01:51 +10003975 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Millerc6081482012-04-22 11:18:53 +10003976 if (permitted_adm_opens[i].host_to_connect == NULL)
3977 printf(" none");
3978 else
Darren Tuckere7140f22008-06-10 23:01:51 +10003979 printf(" %s:%d", permitted_adm_opens[i].host_to_connect,
3980 permitted_adm_opens[i].port_to_connect);
Damien Millerb53d8a12009-01-28 16:13:04 +11003981 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10003982}
3983
Darren Tucker1338b9e2011-10-02 18:57:35 +11003984/* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
3985int
3986permitopen_port(const char *p)
3987{
3988 int port;
3989
3990 if (strcmp(p, "*") == 0)
3991 return FWD_PERMIT_ANY_PORT;
3992 if ((port = a2port(p)) > 0)
3993 return port;
3994 return -1;
3995}
3996
Damien Millerbd740252008-05-19 15:37:09 +10003997/* Try to start non-blocking connect to next host in cctx list */
Ben Lindstrombba81212001-06-25 05:01:22 +00003998static int
Damien Millerbd740252008-05-19 15:37:09 +10003999connect_next(struct channel_connect *cctx)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004000{
Damien Millerbd740252008-05-19 15:37:09 +10004001 int sock, saved_errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10004002 struct sockaddr_un *sunaddr;
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00004003 char ntop[NI_MAXHOST], strport[MAXIMUM(NI_MAXSERV,sizeof(sunaddr->sun_path))];
Damien Miller95def091999-11-25 00:26:21 +11004004
Damien Millerbd740252008-05-19 15:37:09 +10004005 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004006 switch (cctx->ai->ai_family) {
4007 case AF_UNIX:
4008 /* unix:pathname instead of host:port */
4009 sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
4010 strlcpy(ntop, "unix", sizeof(ntop));
4011 strlcpy(strport, sunaddr->sun_path, sizeof(strport));
4012 break;
4013 case AF_INET:
4014 case AF_INET6:
4015 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
4016 ntop, sizeof(ntop), strport, sizeof(strport),
4017 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
4018 error("connect_next: getnameinfo failed");
4019 continue;
4020 }
4021 break;
4022 default:
Damien Miller34132e52000-01-14 15:45:46 +11004023 continue;
4024 }
Darren Tucker7bd98e72010-01-10 10:31:12 +11004025 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
4026 cctx->ai->ai_protocol)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10004027 if (cctx->ai->ai_next == NULL)
Damien Millerb46b9f32003-01-10 21:45:12 +11004028 error("socket: %.100s", strerror(errno));
4029 else
4030 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004031 continue;
4032 }
Damien Miller232711f2004-06-15 10:35:30 +10004033 if (set_nonblock(sock) == -1)
4034 fatal("%s: set_nonblock(%d)", __func__, sock);
Damien Millerbd740252008-05-19 15:37:09 +10004035 if (connect(sock, cctx->ai->ai_addr,
4036 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
4037 debug("connect_next: host %.100s ([%.100s]:%s): "
4038 "%.100s", cctx->host, ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11004039 strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10004040 saved_errno = errno;
Damien Miller34132e52000-01-14 15:45:46 +11004041 close(sock);
Damien Millerbd740252008-05-19 15:37:09 +10004042 errno = saved_errno;
Kevin Stevesef4eea92001-02-05 12:42:17 +00004043 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11004044 }
Damien Miller7acefbb2014-07-18 14:11:24 +10004045 if (cctx->ai->ai_family != AF_UNIX)
4046 set_nodelay(sock);
Damien Millerbd740252008-05-19 15:37:09 +10004047 debug("connect_next: host %.100s ([%.100s]:%s) "
4048 "in progress, fd=%d", cctx->host, ntop, strport, sock);
4049 cctx->ai = cctx->ai->ai_next;
Damien Millerbd740252008-05-19 15:37:09 +10004050 return sock;
Damien Miller34132e52000-01-14 15:45:46 +11004051 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11004052 return -1;
4053}
Damien Millerb38eff82000-04-01 11:09:21 +10004054
Damien Millerbd740252008-05-19 15:37:09 +10004055static void
4056channel_connect_ctx_free(struct channel_connect *cctx)
4057{
Darren Tuckera627d422013-06-02 07:31:17 +10004058 free(cctx->host);
Damien Miller7acefbb2014-07-18 14:11:24 +10004059 if (cctx->aitop) {
4060 if (cctx->aitop->ai_family == AF_UNIX)
4061 free(cctx->aitop);
4062 else
4063 freeaddrinfo(cctx->aitop);
4064 }
Damien Miller1d2c4562014-02-04 11:18:20 +11004065 memset(cctx, 0, sizeof(*cctx));
Damien Millerbd740252008-05-19 15:37:09 +10004066}
4067
Damien Miller7acefbb2014-07-18 14:11:24 +10004068/* Return CONNECTING channel to remote host:port or local socket path */
Damien Millerbd740252008-05-19 15:37:09 +10004069static Channel *
Damien Miller7acefbb2014-07-18 14:11:24 +10004070connect_to(const char *name, int port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10004071{
4072 struct addrinfo hints;
4073 int gaierr;
4074 int sock = -1;
4075 char strport[NI_MAXSERV];
4076 struct channel_connect cctx;
4077 Channel *c;
4078
Damien Miller2bcb8662008-07-12 17:12:29 +10004079 memset(&cctx, 0, sizeof(cctx));
Damien Miller7acefbb2014-07-18 14:11:24 +10004080
4081 if (port == PORT_STREAMLOCAL) {
4082 struct sockaddr_un *sunaddr;
4083 struct addrinfo *ai;
4084
4085 if (strlen(name) > sizeof(sunaddr->sun_path)) {
4086 error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
4087 return (NULL);
4088 }
4089
4090 /*
4091 * Fake up a struct addrinfo for AF_UNIX connections.
4092 * channel_connect_ctx_free() must check ai_family
4093 * and use free() not freeaddirinfo() for AF_UNIX.
4094 */
4095 ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
4096 memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
4097 ai->ai_addr = (struct sockaddr *)(ai + 1);
4098 ai->ai_addrlen = sizeof(*sunaddr);
4099 ai->ai_family = AF_UNIX;
4100 ai->ai_socktype = SOCK_STREAM;
4101 ai->ai_protocol = PF_UNSPEC;
4102 sunaddr = (struct sockaddr_un *)ai->ai_addr;
4103 sunaddr->sun_family = AF_UNIX;
4104 strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
4105 cctx.aitop = ai;
4106 } else {
4107 memset(&hints, 0, sizeof(hints));
4108 hints.ai_family = IPv4or6;
4109 hints.ai_socktype = SOCK_STREAM;
4110 snprintf(strport, sizeof strport, "%d", port);
4111 if ((gaierr = getaddrinfo(name, strport, &hints, &cctx.aitop)) != 0) {
4112 error("connect_to %.100s: unknown host (%s)", name,
4113 ssh_gai_strerror(gaierr));
4114 return NULL;
4115 }
Damien Millerbd740252008-05-19 15:37:09 +10004116 }
4117
Damien Miller7acefbb2014-07-18 14:11:24 +10004118 cctx.host = xstrdup(name);
Damien Millerbd740252008-05-19 15:37:09 +10004119 cctx.port = port;
4120 cctx.ai = cctx.aitop;
4121
4122 if ((sock = connect_next(&cctx)) == -1) {
4123 error("connect to %.100s port %d failed: %s",
Damien Miller7acefbb2014-07-18 14:11:24 +10004124 name, port, strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10004125 channel_connect_ctx_free(&cctx);
4126 return NULL;
4127 }
4128 c = channel_new(ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
4129 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
4130 c->connect_ctx = cctx;
4131 return c;
4132}
4133
markus@openbsd.org8d057842016-09-30 09:19:13 +00004134/*
4135 * returns either the newly connected channel or the downstream channel
4136 * that needs to deal with this connection.
4137 */
Damien Millerbd740252008-05-19 15:37:09 +10004138Channel *
Damien Miller4b3ed642014-07-02 15:29:40 +10004139channel_connect_by_listen_address(const char *listen_host,
4140 u_short listen_port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10004141{
4142 int i;
4143
4144 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10004145 if (open_listen_match_tcpip(&permitted_opens[i], listen_host,
Damien Miller4b3ed642014-07-02 15:29:40 +10004146 listen_port, 1)) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00004147 if (permitted_opens[i].downstream)
4148 return permitted_opens[i].downstream;
Damien Millerbd740252008-05-19 15:37:09 +10004149 return connect_to(
4150 permitted_opens[i].host_to_connect,
4151 permitted_opens[i].port_to_connect, ctype, rname);
4152 }
4153 }
4154 error("WARNING: Server requests forwarding for unknown listen_port %d",
4155 listen_port);
4156 return NULL;
4157}
4158
Damien Miller7acefbb2014-07-18 14:11:24 +10004159Channel *
4160channel_connect_by_listen_path(const char *path, char *ctype, char *rname)
4161{
4162 int i;
4163
4164 for (i = 0; i < num_permitted_opens; i++) {
4165 if (open_listen_match_streamlocal(&permitted_opens[i], path)) {
4166 return connect_to(
4167 permitted_opens[i].host_to_connect,
4168 permitted_opens[i].port_to_connect, ctype, rname);
4169 }
4170 }
4171 error("WARNING: Server requests forwarding for unknown path %.100s",
4172 path);
4173 return NULL;
4174}
4175
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004176/* Check if connecting to that port is permitted and connect. */
Damien Millerbd740252008-05-19 15:37:09 +10004177Channel *
Damien Miller7acefbb2014-07-18 14:11:24 +10004178channel_connect_to_port(const char *host, u_short port, char *ctype, char *rname)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004179{
Damien Miller9b439df2006-07-24 14:04:00 +10004180 int i, permit, permit_adm = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004181
4182 permit = all_opens_permitted;
4183 if (!permit) {
4184 for (i = 0; i < num_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10004185 if (open_match(&permitted_opens[i], host, port)) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004186 permit = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004187 break;
4188 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004189 }
Damien Miller9b439df2006-07-24 14:04:00 +10004190
4191 if (num_adm_permitted_opens > 0) {
4192 permit_adm = 0;
4193 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10004194 if (open_match(&permitted_adm_opens[i], host, port)) {
Damien Miller9b439df2006-07-24 14:04:00 +10004195 permit_adm = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10004196 break;
4197 }
Damien Miller9b439df2006-07-24 14:04:00 +10004198 }
4199
4200 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10004201 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004202 "but the request was denied.", host, port);
Damien Millerbd740252008-05-19 15:37:09 +10004203 return NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004204 }
Damien Millerbd740252008-05-19 15:37:09 +10004205 return connect_to(host, port, ctype, rname);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00004206}
4207
Damien Miller7acefbb2014-07-18 14:11:24 +10004208/* Check if connecting to that path is permitted and connect. */
4209Channel *
4210channel_connect_to_path(const char *path, char *ctype, char *rname)
4211{
4212 int i, permit, permit_adm = 1;
4213
4214 permit = all_opens_permitted;
4215 if (!permit) {
4216 for (i = 0; i < num_permitted_opens; i++)
4217 if (open_match(&permitted_opens[i], path, PORT_STREAMLOCAL)) {
4218 permit = 1;
4219 break;
4220 }
4221 }
4222
4223 if (num_adm_permitted_opens > 0) {
4224 permit_adm = 0;
4225 for (i = 0; i < num_adm_permitted_opens; i++)
4226 if (open_match(&permitted_adm_opens[i], path, PORT_STREAMLOCAL)) {
4227 permit_adm = 1;
4228 break;
4229 }
4230 }
4231
4232 if (!permit || !permit_adm) {
4233 logit("Received request to connect to path %.100s, "
4234 "but the request was denied.", path);
4235 return NULL;
4236 }
4237 return connect_to(path, PORT_STREAMLOCAL, ctype, rname);
4238}
4239
Damien Miller0e220db2004-06-15 10:34:08 +10004240void
4241channel_send_window_changes(void)
4242{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10004243 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10004244 struct winsize ws;
4245
4246 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11004247 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10004248 channels[i]->type != SSH_CHANNEL_OPEN)
4249 continue;
4250 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
4251 continue;
4252 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11004253 packet_put_int((u_int)ws.ws_col);
4254 packet_put_int((u_int)ws.ws_row);
4255 packet_put_int((u_int)ws.ws_xpixel);
4256 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10004257 packet_send();
4258 }
4259}
4260
Ben Lindstrome9c99912001-06-09 00:41:05 +00004261/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004262
Damien Miller5428f641999-11-25 11:54:57 +11004263/*
4264 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004265 * Returns 0 and a suitable display number for the DISPLAY variable
4266 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11004267 */
Kevin Steves366298c2001-12-19 17:58:01 +00004268int
Damien Miller95c249f2002-02-05 12:11:34 +11004269x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10004270 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004271{
Damien Millere7378562001-12-21 14:58:35 +11004272 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11004273 int display_number, sock;
4274 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11004275 struct addrinfo hints, *ai, *aitop;
4276 char strport[NI_MAXSERV];
4277 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004278
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004279 if (chanids == NULL)
4280 return -1;
4281
Damien Millera34a28b1999-12-14 10:47:15 +11004282 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11004283 display_number < MAX_DISPLAYS;
4284 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11004285 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11004286 memset(&hints, 0, sizeof(hints));
4287 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11004288 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11004289 hints.ai_socktype = SOCK_STREAM;
4290 snprintf(strport, sizeof strport, "%d", port);
4291 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004292 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00004293 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004294 }
Damien Miller34132e52000-01-14 15:45:46 +11004295 for (ai = aitop; ai; ai = ai->ai_next) {
4296 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
4297 continue;
Darren Tucker7bd98e72010-01-10 10:31:12 +11004298 sock = socket(ai->ai_family, ai->ai_socktype,
4299 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004300 if (sock < 0) {
Damien Miller6480c632010-03-26 11:09:44 +11004301 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
4302#ifdef EPFNOSUPPORT
4303 && (errno != EPFNOSUPPORT)
4304#endif
4305 ) {
Damien Millere2192732000-01-17 13:22:55 +11004306 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10004307 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00004308 return -1;
Damien Millere2192732000-01-17 13:22:55 +11004309 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11004310 debug("x11_create_display_inet: Socket family %d not supported",
4311 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11004312 continue;
4313 }
Damien Miller34132e52000-01-14 15:45:46 +11004314 }
Damien Miller04ee0f82009-11-18 17:48:30 +11004315 if (ai->ai_family == AF_INET6)
4316 sock_set_v6only(sock);
Damien Miller4401e452008-06-12 06:05:12 +10004317 if (x11_use_localhost)
4318 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11004319 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004320 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004321 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11004322
Damien Miller34132e52000-01-14 15:45:46 +11004323 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11004324 close(socks[n]);
4325 }
4326 num_socks = 0;
4327 break;
4328 }
4329 socks[num_socks++] = sock;
4330 if (num_socks == NUM_SOCKS)
4331 break;
Damien Miller95def091999-11-25 00:26:21 +11004332 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00004333 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11004334 if (num_socks > 0)
4335 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004336 }
Damien Miller95def091999-11-25 00:26:21 +11004337 if (display_number >= MAX_DISPLAYS) {
4338 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00004339 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004340 }
Damien Miller95def091999-11-25 00:26:21 +11004341 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11004342 for (n = 0; n < num_socks; n++) {
4343 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11004344 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11004345 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004346 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00004347 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11004348 }
Damien Miller95def091999-11-25 00:26:21 +11004349 }
Damien Miller34132e52000-01-14 15:45:46 +11004350
Damien Miller34132e52000-01-14 15:45:46 +11004351 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11004352 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11004353 for (n = 0; n < num_socks; n++) {
4354 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11004355 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10004356 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
4357 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10004358 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11004359 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004360 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11004361 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004362 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004363
Kevin Steves366298c2001-12-19 17:58:01 +00004364 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004365 *display_numberp = display_number;
4366 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004367}
4368
Ben Lindstrombba81212001-06-25 05:01:22 +00004369static int
Damien Miller819dbb62009-01-21 16:46:26 +11004370connect_local_xsocket_path(const char *pathname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004371{
Damien Miller95def091999-11-25 00:26:21 +11004372 int sock;
4373 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004374
Damien Miller3afe3752001-12-21 12:39:51 +11004375 sock = socket(AF_UNIX, SOCK_STREAM, 0);
4376 if (sock < 0)
4377 error("socket: %.100s", strerror(errno));
4378 memset(&addr, 0, sizeof(addr));
4379 addr.sun_family = AF_UNIX;
Damien Miller819dbb62009-01-21 16:46:26 +11004380 strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Damien Miller90967402006-03-26 14:07:26 +11004381 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11004382 return sock;
4383 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11004384 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
4385 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004386}
4387
Damien Miller819dbb62009-01-21 16:46:26 +11004388static int
4389connect_local_xsocket(u_int dnr)
4390{
4391 char buf[1024];
4392 snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
4393 return connect_local_xsocket_path(buf);
4394}
4395
Damien Millerbd483e72000-04-30 10:00:53 +10004396int
4397x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004398{
Damien Miller57c4e872006-03-31 23:11:07 +11004399 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11004400 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10004401 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11004402 struct addrinfo hints, *ai, *aitop;
4403 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11004404 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004405
Damien Miller95def091999-11-25 00:26:21 +11004406 /* Try to open a socket for the local X server. */
4407 display = getenv("DISPLAY");
4408 if (!display) {
4409 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10004410 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004411 }
Damien Miller5428f641999-11-25 11:54:57 +11004412 /*
4413 * Now we decode the value of the DISPLAY variable and make a
4414 * connection to the real X server.
4415 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004416
Damien Miller819dbb62009-01-21 16:46:26 +11004417 /* Check if the display is from launchd. */
4418#ifdef __APPLE__
4419 if (strncmp(display, "/tmp/launch", 11) == 0) {
4420 sock = connect_local_xsocket_path(display);
4421 if (sock < 0)
4422 return -1;
4423
4424 /* OK, we now have a connection to the display. */
4425 return sock;
4426 }
4427#endif
Damien Miller5428f641999-11-25 11:54:57 +11004428 /*
4429 * Check if it is a unix domain socket. Unix domain displays are in
4430 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4431 */
Damien Miller95def091999-11-25 00:26:21 +11004432 if (strncmp(display, "unix:", 5) == 0 ||
4433 display[0] == ':') {
4434 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11004435 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004436 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004437 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004438 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004439 }
4440 /* Create a socket. */
4441 sock = connect_local_xsocket(display_number);
4442 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10004443 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004444
4445 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10004446 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004447 }
Damien Miller5428f641999-11-25 11:54:57 +11004448 /*
4449 * Connect to an inet socket. The DISPLAY value is supposedly
4450 * hostname:d[.s], where hostname may also be numeric IP address.
4451 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00004452 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11004453 cp = strchr(buf, ':');
4454 if (!cp) {
4455 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004456 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004457 }
Damien Miller95def091999-11-25 00:26:21 +11004458 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11004459 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11004460 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004461 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004462 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004463 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004464 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004465
Damien Miller34132e52000-01-14 15:45:46 +11004466 /* Look up the host address */
4467 memset(&hints, 0, sizeof(hints));
4468 hints.ai_family = IPv4or6;
4469 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11004470 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11004471 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004472 error("%.100s: unknown host. (%s)", buf,
4473 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10004474 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004475 }
Damien Miller34132e52000-01-14 15:45:46 +11004476 for (ai = aitop; ai; ai = ai->ai_next) {
4477 /* Create a socket. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11004478 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004479 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004480 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10004481 continue;
4482 }
4483 /* Connect it to the display. */
4484 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11004485 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10004486 6000 + display_number, strerror(errno));
4487 close(sock);
4488 continue;
4489 }
4490 /* Success */
4491 break;
Damien Miller34132e52000-01-14 15:45:46 +11004492 }
Damien Miller34132e52000-01-14 15:45:46 +11004493 freeaddrinfo(aitop);
4494 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11004495 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11004496 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10004497 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004498 }
Damien Miller398e1cf2002-02-05 11:52:13 +11004499 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10004500 return sock;
4501}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004502
Damien Millerbd483e72000-04-30 10:00:53 +10004503/*
4504 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
4505 * the remote channel number. We should do whatever we want, and respond
4506 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
4507 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004508
Damien Miller8473dd82006-07-24 14:08:32 +10004509/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004510int
Damien Miller630d6f42002-01-22 23:17:30 +11004511x11_input_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10004512{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004513 Channel *c = NULL;
4514 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10004515 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10004516
4517 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004518
4519 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00004520
4521 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004522 remote_host = packet_get_string(NULL);
4523 } else {
4524 remote_host = xstrdup("unknown (remote did not supply name)");
4525 }
Damien Miller48b03fc2002-01-22 23:11:40 +11004526 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10004527
4528 /* Obtain a connection to the real X display. */
4529 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004530 if (sock != -1) {
4531 /* Allocate a channel for this connection. */
4532 c = channel_new("connected x11 socket",
4533 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
4534 remote_host, 1);
Damien Miller699d0032002-02-08 22:07:16 +11004535 c->remote_id = remote_id;
4536 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004537 }
Darren Tuckera627d422013-06-02 07:31:17 +10004538 free(remote_host);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004539 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10004540 /* Send refusal to the remote host. */
4541 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004542 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10004543 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10004544 /* Send a confirmation to the remote host. */
4545 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004546 packet_put_int(remote_id);
4547 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10004548 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004549 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004550 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004551}
4552
Damien Miller69b69aa2000-10-28 14:19:58 +11004553/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
Damien Miller8473dd82006-07-24 14:08:32 +10004554/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004555int
Damien Miller630d6f42002-01-22 23:17:30 +11004556deny_input_open(int type, u_int32_t seq, void *ctxt)
Damien Miller69b69aa2000-10-28 14:19:58 +11004557{
4558 int rchan = packet_get_int();
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00004559
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00004560 switch (type) {
Damien Miller69b69aa2000-10-28 14:19:58 +11004561 case SSH_SMSG_AGENT_OPEN:
4562 error("Warning: ssh server tried agent forwarding.");
4563 break;
4564 case SSH_SMSG_X11_OPEN:
4565 error("Warning: ssh server tried X11 forwarding.");
4566 break;
4567 default:
Damien Miller630d6f42002-01-22 23:17:30 +11004568 error("deny_input_open: type %d", type);
Damien Miller69b69aa2000-10-28 14:19:58 +11004569 break;
4570 }
Damien Miller5eb137c2005-12-31 16:19:53 +11004571 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller69b69aa2000-10-28 14:19:58 +11004572 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
4573 packet_put_int(rchan);
4574 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004575 return 0;
Damien Miller69b69aa2000-10-28 14:19:58 +11004576}
4577
Damien Miller5428f641999-11-25 11:54:57 +11004578/*
4579 * Requests forwarding of X11 connections, generates fake authentication
4580 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00004581 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11004582 */
Damien Miller4af51302000-04-16 11:18:38 +10004583void
Damien Miller17e7ed02005-06-17 12:54:33 +10004584x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Miller6d7b4372011-06-23 08:31:57 +10004585 const char *proto, const char *data, int want_reply)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004586{
Ben Lindstrom46c16222000-12-22 01:43:59 +00004587 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10004588 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11004589 char *new_data;
4590 int screen_number;
4591 const char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004592
Damien Millerf92c0792005-07-06 09:45:26 +10004593 if (x11_saved_display == NULL)
4594 x11_saved_display = xstrdup(disp);
4595 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10004596 error("x11_request_forwarding_with_spoofing: different "
4597 "$DISPLAY already forwarded");
4598 return;
4599 }
4600
Damien Millerd5fe0ba2006-08-30 11:07:39 +10004601 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11004602 if (cp)
4603 cp = strchr(cp, '.');
4604 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11004605 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11004606 else
4607 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004608
Damien Miller13390022005-07-06 09:44:19 +10004609 if (x11_saved_proto == NULL) {
4610 /* Save protocol name. */
4611 x11_saved_proto = xstrdup(proto);
natano@openbsd.org49271082016-09-19 07:52:42 +00004612
4613 /* Extract real authentication data. */
Damien Miller13390022005-07-06 09:44:19 +10004614 x11_saved_data = xmalloc(data_len);
Damien Miller13390022005-07-06 09:44:19 +10004615 for (i = 0; i < data_len; i++) {
4616 if (sscanf(data + 2 * i, "%2x", &value) != 1)
4617 fatal("x11_request_forwarding: bad "
4618 "authentication data: %.100s", data);
Damien Miller13390022005-07-06 09:44:19 +10004619 x11_saved_data[i] = value;
Damien Miller13390022005-07-06 09:44:19 +10004620 }
4621 x11_saved_data_len = data_len;
natano@openbsd.org49271082016-09-19 07:52:42 +00004622
4623 /* Generate fake data of the same length. */
4624 x11_fake_data = xmalloc(data_len);
4625 arc4random_buf(x11_fake_data, data_len);
Damien Miller13390022005-07-06 09:44:19 +10004626 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11004627 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004628
Damien Miller95def091999-11-25 00:26:21 +11004629 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10004630 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004631
Damien Miller95def091999-11-25 00:26:21 +11004632 /* Send the request packet. */
Damien Millerbd483e72000-04-30 10:00:53 +10004633 if (compat20) {
Damien Miller6d7b4372011-06-23 08:31:57 +10004634 channel_request_start(client_session_id, "x11-req", want_reply);
Damien Millerbd483e72000-04-30 10:00:53 +10004635 packet_put_char(0); /* XXX bool single connection */
4636 } else {
4637 packet_start(SSH_CMSG_X11_REQUEST_FORWARDING);
4638 }
4639 packet_put_cstring(proto);
4640 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11004641 packet_put_int(screen_number);
4642 packet_send();
4643 packet_write_wait();
Darren Tuckera627d422013-06-02 07:31:17 +10004644 free(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004645}
4646
Ben Lindstrome9c99912001-06-09 00:41:05 +00004647
4648/* -- agent forwarding */
4649
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004650/* Sends a message to the server to request authentication fd forwarding. */
4651
Damien Miller4af51302000-04-16 11:18:38 +10004652void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00004653auth_request_forwarding(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004654{
Damien Miller95def091999-11-25 00:26:21 +11004655 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
4656 packet_send();
4657 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004658}