blob: 5a7e56ed018021ac2d5397479978663bde7941ab [file] [log] [blame]
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001/* $OpenBSD: channels.c,v 1.358 2017/04/30 23:13:25 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) {
Ben Lindstrome9c99912001-06-09 00:41:05 +0000574 if (buffer_len(&c->output) > packet_get_maxsize()) {
Darren Tucker502d3842003-06-28 12:38:01 +1000575 debug2("channel %d: big output buffer %u > %u",
Damien Milleraf5f2e62001-10-10 15:01:16 +1000576 c->self, buffer_len(&c->output),
577 packet_get_maxsize());
Ben Lindstrome9c99912001-06-09 00:41:05 +0000578 return 0;
579 }
580 }
581 }
582 return 1;
583}
584
585/* Returns true if any channel is still open. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000586int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000587channel_still_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000588{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000589 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000590 Channel *c;
591
592 for (i = 0; i < channels_alloc; i++) {
593 c = channels[i];
594 if (c == NULL)
595 continue;
596 switch (c->type) {
597 case SSH_CHANNEL_X11_LISTENER:
598 case SSH_CHANNEL_PORT_LISTENER:
599 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100600 case SSH_CHANNEL_MUX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000601 case SSH_CHANNEL_CLOSED:
602 case SSH_CHANNEL_AUTH_SOCKET:
603 case SSH_CHANNEL_DYNAMIC:
604 case SSH_CHANNEL_CONNECTING:
605 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000606 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000607 case SSH_CHANNEL_UNIX_LISTENER:
608 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000609 continue;
610 case SSH_CHANNEL_LARVAL:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000611 continue;
612 case SSH_CHANNEL_OPENING:
613 case SSH_CHANNEL_OPEN:
614 case SSH_CHANNEL_X11_OPEN:
Damien Millere1537f92010-01-26 13:26:22 +1100615 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000616 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000617 return 1;
618 case SSH_CHANNEL_INPUT_DRAINING:
619 case SSH_CHANNEL_OUTPUT_DRAINING:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000620 fatal("cannot happen: OUT_DRAIN");
Ben Lindstrome9c99912001-06-09 00:41:05 +0000621 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000622 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000623 /* NOTREACHED */
624 }
625 }
626 return 0;
627}
628
629/* Returns the id of an open channel suitable for keepaliving */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000630int
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000631channel_find_open(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000632{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000633 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000634 Channel *c;
635
636 for (i = 0; i < channels_alloc; i++) {
637 c = channels[i];
Damien Miller3bbd8782004-06-18 22:23:22 +1000638 if (c == NULL || c->remote_id < 0)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000639 continue;
640 switch (c->type) {
641 case SSH_CHANNEL_CLOSED:
642 case SSH_CHANNEL_DYNAMIC:
643 case SSH_CHANNEL_X11_LISTENER:
644 case SSH_CHANNEL_PORT_LISTENER:
645 case SSH_CHANNEL_RPORT_LISTENER:
Damien Millere1537f92010-01-26 13:26:22 +1100646 case SSH_CHANNEL_MUX_LISTENER:
647 case SSH_CHANNEL_MUX_CLIENT:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000648 case SSH_CHANNEL_MUX_PROXY:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000649 case SSH_CHANNEL_OPENING:
650 case SSH_CHANNEL_CONNECTING:
651 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000652 case SSH_CHANNEL_ABANDONED:
Damien Miller7acefbb2014-07-18 14:11:24 +1000653 case SSH_CHANNEL_UNIX_LISTENER:
654 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000655 continue;
656 case SSH_CHANNEL_LARVAL:
657 case SSH_CHANNEL_AUTH_SOCKET:
658 case SSH_CHANNEL_OPEN:
659 case SSH_CHANNEL_X11_OPEN:
660 return i;
661 case SSH_CHANNEL_INPUT_DRAINING:
662 case SSH_CHANNEL_OUTPUT_DRAINING:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000663 fatal("cannot happen: OUT_DRAIN");
Ben Lindstrome9c99912001-06-09 00:41:05 +0000664 default:
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000665 fatal("%s: bad channel type %d", __func__, c->type);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000666 /* NOTREACHED */
667 }
668 }
669 return -1;
670}
671
Ben Lindstrome9c99912001-06-09 00:41:05 +0000672/*
673 * Returns a message describing the currently open forwarded connections,
674 * suitable for sending to the client. The message contains crlf pairs for
675 * newlines.
676 */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000677char *
Ben Lindstrom3c36bb22001-12-06 17:55:26 +0000678channel_open_message(void)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000679{
680 Buffer buffer;
681 Channel *c;
682 char buf[1024], *cp;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +1000683 u_int i;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000684
685 buffer_init(&buffer);
686 snprintf(buf, sizeof buf, "The following connections are open:\r\n");
687 buffer_append(&buffer, buf, strlen(buf));
688 for (i = 0; i < channels_alloc; i++) {
689 c = channels[i];
690 if (c == NULL)
691 continue;
692 switch (c->type) {
693 case SSH_CHANNEL_X11_LISTENER:
694 case SSH_CHANNEL_PORT_LISTENER:
695 case SSH_CHANNEL_RPORT_LISTENER:
696 case SSH_CHANNEL_CLOSED:
697 case SSH_CHANNEL_AUTH_SOCKET:
698 case SSH_CHANNEL_ZOMBIE:
Damien Miller36187092013-06-10 13:07:11 +1000699 case SSH_CHANNEL_ABANDONED:
Damien Millere1537f92010-01-26 13:26:22 +1100700 case SSH_CHANNEL_MUX_LISTENER:
Damien Miller7acefbb2014-07-18 14:11:24 +1000701 case SSH_CHANNEL_UNIX_LISTENER:
702 case SSH_CHANNEL_RUNIX_LISTENER:
Ben Lindstrome9c99912001-06-09 00:41:05 +0000703 continue;
704 case SSH_CHANNEL_LARVAL:
705 case SSH_CHANNEL_OPENING:
706 case SSH_CHANNEL_CONNECTING:
707 case SSH_CHANNEL_DYNAMIC:
708 case SSH_CHANNEL_OPEN:
709 case SSH_CHANNEL_X11_OPEN:
710 case SSH_CHANNEL_INPUT_DRAINING:
711 case SSH_CHANNEL_OUTPUT_DRAINING:
markus@openbsd.org8d057842016-09-30 09:19:13 +0000712 case SSH_CHANNEL_MUX_PROXY:
713 case SSH_CHANNEL_MUX_CLIENT:
Damien Miller0e220db2004-06-15 10:34:08 +1000714 snprintf(buf, sizeof buf,
djm@openbsd.orgb1d38a32015-10-15 23:51:40 +0000715 " #%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 +0000716 c->self, c->remote_name,
717 c->type, c->remote_id,
718 c->istate, buffer_len(&c->input),
719 c->ostate, buffer_len(&c->output),
Damien Millere1537f92010-01-26 13:26:22 +1100720 c->rfd, c->wfd, c->ctl_chan);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000721 buffer_append(&buffer, buf, strlen(buf));
722 continue;
723 default:
724 fatal("channel_open_message: bad channel type %d", c->type);
725 /* NOTREACHED */
726 }
727 }
728 buffer_append(&buffer, "\0", 1);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +0000729 cp = xstrdup((char *)buffer_ptr(&buffer));
Ben Lindstrome9c99912001-06-09 00:41:05 +0000730 buffer_free(&buffer);
731 return cp;
732}
733
734void
735channel_send_open(int id)
736{
737 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000738
Ben Lindstrome9c99912001-06-09 00:41:05 +0000739 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000740 logit("channel_send_open: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000741 return;
742 }
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000743 debug2("channel %d: send open", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000744 packet_start(SSH2_MSG_CHANNEL_OPEN);
745 packet_put_cstring(c->ctype);
746 packet_put_int(c->self);
747 packet_put_int(c->local_window);
748 packet_put_int(c->local_maxpacket);
749 packet_send();
750}
751
752void
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000753channel_request_start(int id, char *service, int wantconfirm)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000754{
Ben Lindstrom1d568f92002-12-23 02:44:36 +0000755 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000756
Ben Lindstrome9c99912001-06-09 00:41:05 +0000757 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000758 logit("channel_request_start: %d: unknown channel id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000759 return;
760 }
Damien Miller0e220db2004-06-15 10:34:08 +1000761 debug2("channel %d: request %s confirm %d", id, service, wantconfirm);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000762 packet_start(SSH2_MSG_CHANNEL_REQUEST);
763 packet_put_int(c->remote_id);
764 packet_put_cstring(service);
765 packet_put_char(wantconfirm);
766}
Damien Miller4f7becb2006-03-26 14:10:14 +1100767
Ben Lindstrome9c99912001-06-09 00:41:05 +0000768void
Damien Millerb84886b2008-05-19 15:05:07 +1000769channel_register_status_confirm(int id, channel_confirm_cb *cb,
770 channel_confirm_abandon_cb *abandon_cb, void *ctx)
771{
772 struct channel_confirm *cc;
773 Channel *c;
774
775 if ((c = channel_lookup(id)) == NULL)
776 fatal("channel_register_expect: %d: bad id", id);
777
Damien Miller6c81fee2013-11-08 12:19:55 +1100778 cc = xcalloc(1, sizeof(*cc));
Damien Millerb84886b2008-05-19 15:05:07 +1000779 cc->cb = cb;
780 cc->abandon_cb = abandon_cb;
781 cc->ctx = ctx;
782 TAILQ_INSERT_TAIL(&c->status_confirms, cc, entry);
783}
784
785void
Damien Millerd530f5f2010-05-21 14:57:10 +1000786channel_register_open_confirm(int id, channel_open_fn *fn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000787{
788 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000789
Ben Lindstrome9c99912001-06-09 00:41:05 +0000790 if (c == NULL) {
Damien Millera0094332008-11-03 19:26:35 +1100791 logit("channel_register_open_confirm: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000792 return;
793 }
Damien Millerb84886b2008-05-19 15:05:07 +1000794 c->open_confirm = fn;
795 c->open_confirm_ctx = ctx;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000796}
Damien Miller4f7becb2006-03-26 14:10:14 +1100797
Ben Lindstrome9c99912001-06-09 00:41:05 +0000798void
Damien Miller39eda6e2005-11-05 14:52:50 +1100799channel_register_cleanup(int id, channel_callback_fn *fn, int do_close)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000800{
Damien Millerd47c62a2005-12-13 19:33:57 +1100801 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000802
Ben Lindstrome9c99912001-06-09 00:41:05 +0000803 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000804 logit("channel_register_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000805 return;
806 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000807 c->detach_user = fn;
Damien Miller39eda6e2005-11-05 14:52:50 +1100808 c->detach_close = do_close;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000809}
Damien Miller4f7becb2006-03-26 14:10:14 +1100810
Ben Lindstrome9c99912001-06-09 00:41:05 +0000811void
812channel_cancel_cleanup(int id)
813{
Damien Millerd47c62a2005-12-13 19:33:57 +1100814 Channel *c = channel_by_id(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000815
Ben Lindstrome9c99912001-06-09 00:41:05 +0000816 if (c == NULL) {
Damien Miller996acd22003-04-09 20:59:48 +1000817 logit("channel_cancel_cleanup: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000818 return;
819 }
Ben Lindstrom809744e2001-07-04 05:26:06 +0000820 c->detach_user = NULL;
Damien Miller39eda6e2005-11-05 14:52:50 +1100821 c->detach_close = 0;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000822}
Damien Miller4f7becb2006-03-26 14:10:14 +1100823
Ben Lindstrome9c99912001-06-09 00:41:05 +0000824void
Damien Miller077b2382005-12-31 16:22:32 +1100825channel_register_filter(int id, channel_infilter_fn *ifn,
Darren Tucker84c56f52008-06-13 04:55:46 +1000826 channel_outfilter_fn *ofn, channel_filter_cleanup_fn *cfn, void *ctx)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000827{
828 Channel *c = channel_lookup(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_register_filter: %d: bad id", id);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000832 return;
833 }
Damien Miller077b2382005-12-31 16:22:32 +1100834 c->input_filter = ifn;
835 c->output_filter = ofn;
Darren Tucker2fb66ca2008-06-13 04:49:33 +1000836 c->filter_ctx = ctx;
Darren Tucker84c56f52008-06-13 04:55:46 +1000837 c->filter_cleanup = cfn;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000838}
839
840void
841channel_set_fds(int id, int rfd, int wfd, int efd,
Darren Tuckered3cdc02008-06-16 23:29:18 +1000842 int extusage, int nonblock, int is_tty, u_int window_max)
Ben Lindstrome9c99912001-06-09 00:41:05 +0000843{
844 Channel *c = channel_lookup(id);
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +0000845
Ben Lindstrome9c99912001-06-09 00:41:05 +0000846 if (c == NULL || c->type != SSH_CHANNEL_LARVAL)
847 fatal("channel_activate for non-larval channel %d.", id);
Darren Tuckered3cdc02008-06-16 23:29:18 +1000848 channel_register_fds(c, rfd, wfd, efd, extusage, nonblock, is_tty);
Ben Lindstrome9c99912001-06-09 00:41:05 +0000849 c->type = SSH_CHANNEL_OPEN;
Damien Miller2aa0c192002-02-19 15:20:08 +1100850 c->local_window = c->local_window_max = window_max;
Ben Lindstrome9c99912001-06-09 00:41:05 +0000851 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
852 packet_put_int(c->remote_id);
853 packet_put_int(c->local_window);
854 packet_send();
855}
856
Damien Miller5428f641999-11-25 11:54:57 +1100857/*
Damien Millerb38eff82000-04-01 11:09:21 +1000858 * 'channel_pre*' are called just before select() to add any bits relevant to
859 * channels in the select bitmasks.
Damien Miller5428f641999-11-25 11:54:57 +1100860 */
Damien Millerb38eff82000-04-01 11:09:21 +1000861/*
862 * 'channel_post*': perform any appropriate operations for channels which
863 * have events pending.
864 */
Damien Millerd62f2ca2006-03-26 13:57:41 +1100865typedef void chan_fn(Channel *c, fd_set *readset, fd_set *writeset);
Damien Millerb38eff82000-04-01 11:09:21 +1000866chan_fn *channel_pre[SSH_CHANNEL_MAX_TYPE];
867chan_fn *channel_post[SSH_CHANNEL_MAX_TYPE];
868
Damien Miller8473dd82006-07-24 14:08:32 +1000869/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000870static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100871channel_pre_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000872{
873 FD_SET(c->sock, readset);
874}
875
Damien Miller8473dd82006-07-24 14:08:32 +1000876/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +0000877static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100878channel_pre_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +0000879{
880 debug3("channel %d: waiting for connection", c->self);
881 FD_SET(c->sock, writeset);
882}
883
Ben Lindstrombba81212001-06-25 05:01:22 +0000884static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100885channel_pre_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000886{
Damien Miller33b13562000-04-04 14:38:59 +1000887 if (c->istate == CHAN_INPUT_OPEN &&
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000888 c->remote_window > 0 &&
889 buffer_len(&c->input) < c->remote_window &&
Damien Miller499a0d52006-04-23 12:06:03 +1000890 buffer_check_alloc(&c->input, CHAN_RBUF))
Damien Miller33b13562000-04-04 14:38:59 +1000891 FD_SET(c->rfd, readset);
892 if (c->ostate == CHAN_OUTPUT_OPEN ||
893 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
894 if (buffer_len(&c->output) > 0) {
895 FD_SET(c->wfd, writeset);
896 } else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
Ben Lindstromcf159442002-03-26 03:26:24 +0000897 if (CHANNEL_EFD_OUTPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +1000898 debug2("channel %d: obuf_empty delayed efd %d/(%d)",
899 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +0000900 else
901 chan_obuf_empty(c);
Damien Miller33b13562000-04-04 14:38:59 +1000902 }
903 }
904 /** XXX check close conditions, too */
djm@openbsd.org97f4d302017-04-30 23:13:25 +0000905 if (c->efd != -1 && !(c->istate == CHAN_INPUT_CLOSED &&
906 c->ostate == CHAN_OUTPUT_CLOSED)) {
Damien Miller33b13562000-04-04 14:38:59 +1000907 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
908 buffer_len(&c->extended) > 0)
909 FD_SET(c->efd, writeset);
Damien Miller8853ca52010-06-26 10:00:14 +1000910 else if (c->efd != -1 && !(c->flags & CHAN_EOF_SENT) &&
911 (c->extended_usage == CHAN_EXTENDED_READ ||
912 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Miller33b13562000-04-04 14:38:59 +1000913 buffer_len(&c->extended) < c->remote_window)
914 FD_SET(c->efd, readset);
915 }
Damien Miller0e220db2004-06-15 10:34:08 +1000916 /* XXX: What about efd? races? */
Damien Miller33b13562000-04-04 14:38:59 +1000917}
918
Damien Millerb38eff82000-04-01 11:09:21 +1000919/*
920 * This is a special state for X11 authentication spoofing. An opened X11
921 * connection (when authentication spoofing is being done) remains in this
922 * state until the first packet has been completely read. The authentication
923 * data in that packet is then substituted by the real data if it matches the
924 * fake data, and the channel is put into normal mode.
Damien Millerbd483e72000-04-30 10:00:53 +1000925 * XXX All this happens at the client side.
Ben Lindstrome9c99912001-06-09 00:41:05 +0000926 * Returns: 0 = need more data, -1 = wrong cookie, 1 = ok
Damien Millerb38eff82000-04-01 11:09:21 +1000927 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000928static int
Ben Lindstrome9c99912001-06-09 00:41:05 +0000929x11_open_helper(Buffer *b)
Damien Millerb38eff82000-04-01 11:09:21 +1000930{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000931 u_char *ucp;
932 u_int proto_len, data_len;
Damien Millerb38eff82000-04-01 11:09:21 +1000933
djm@openbsd.org1bf477d2015-07-01 02:26:31 +0000934 /* Is this being called after the refusal deadline? */
935 if (x11_refuse_time != 0 && (u_int)monotime() >= x11_refuse_time) {
936 verbose("Rejected X11 connection after ForwardX11Timeout "
937 "expired");
938 return -1;
939 }
940
Damien Millerb38eff82000-04-01 11:09:21 +1000941 /* Check if the fixed size part of the packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000942 if (buffer_len(b) < 12)
Damien Millerb38eff82000-04-01 11:09:21 +1000943 return 0;
944
945 /* Parse the lengths of variable-length fields. */
Damien Miller708d21c2002-01-22 23:18:15 +1100946 ucp = buffer_ptr(b);
Damien Millerb38eff82000-04-01 11:09:21 +1000947 if (ucp[0] == 0x42) { /* Byte order MSB first. */
948 proto_len = 256 * ucp[6] + ucp[7];
949 data_len = 256 * ucp[8] + ucp[9];
950 } else if (ucp[0] == 0x6c) { /* Byte order LSB first. */
951 proto_len = ucp[6] + 256 * ucp[7];
952 data_len = ucp[8] + 256 * ucp[9];
953 } else {
Damien Millerfbdeece2003-09-02 22:52:31 +1000954 debug2("Initial X11 packet contains bad byte order byte: 0x%x",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100955 ucp[0]);
Damien Millerb38eff82000-04-01 11:09:21 +1000956 return -1;
957 }
958
959 /* Check if the whole packet is in buffer. */
Ben Lindstrome9c99912001-06-09 00:41:05 +0000960 if (buffer_len(b) <
Damien Millerb38eff82000-04-01 11:09:21 +1000961 12 + ((proto_len + 3) & ~3) + ((data_len + 3) & ~3))
962 return 0;
963
964 /* Check if authentication protocol matches. */
965 if (proto_len != strlen(x11_saved_proto) ||
966 memcmp(ucp + 12, x11_saved_proto, proto_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000967 debug2("X11 connection uses different authentication protocol.");
Damien Millerb38eff82000-04-01 11:09:21 +1000968 return -1;
969 }
970 /* Check if authentication data matches our fake data. */
971 if (data_len != x11_fake_data_len ||
Damien Millerea1651c2010-07-16 13:58:37 +1000972 timingsafe_bcmp(ucp + 12 + ((proto_len + 3) & ~3),
Damien Millerb38eff82000-04-01 11:09:21 +1000973 x11_fake_data, x11_fake_data_len) != 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +1000974 debug2("X11 auth data does not match fake data.");
Damien Millerb38eff82000-04-01 11:09:21 +1000975 return -1;
976 }
977 /* Check fake data length */
978 if (x11_fake_data_len != x11_saved_data_len) {
979 error("X11 fake_data_len %d != saved_data_len %d",
980 x11_fake_data_len, x11_saved_data_len);
981 return -1;
982 }
983 /*
984 * Received authentication protocol and data match
985 * our fake data. Substitute the fake data with real
986 * data.
987 */
988 memcpy(ucp + 12 + ((proto_len + 3) & ~3),
989 x11_saved_data, x11_saved_data_len);
990 return 1;
991}
992
Ben Lindstrombba81212001-06-25 05:01:22 +0000993static void
Damien Millerd62f2ca2006-03-26 13:57:41 +1100994channel_pre_x11_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +1000995{
Ben Lindstrome9c99912001-06-09 00:41:05 +0000996 int ret = x11_open_helper(&c->output);
Ben Lindstrom944c4f02001-09-18 05:51:13 +0000997
998 /* c->force_drain = 1; */
999
Damien Millerb38eff82000-04-01 11:09:21 +10001000 if (ret == 1) {
1001 c->type = SSH_CHANNEL_OPEN;
Damien Millerde6987c2002-01-22 23:20:40 +11001002 channel_pre_open(c, readset, writeset);
Damien Millerb38eff82000-04-01 11:09:21 +10001003 } else if (ret == -1) {
Damien Miller996acd22003-04-09 20:59:48 +10001004 logit("X11 connection rejected because of wrong authentication.");
Damien Millerfbdeece2003-09-02 22:52:31 +10001005 debug2("X11 rejected %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millera90fc082002-01-22 23:19:38 +11001006 chan_read_failed(c);
1007 buffer_clear(&c->input);
1008 chan_ibuf_empty(c);
1009 buffer_clear(&c->output);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001010 chan_write_failed(c);
Damien Millerfbdeece2003-09-02 22:52:31 +10001011 debug2("X11 closed %d i%d/o%d", c->self, c->istate, c->ostate);
Damien Millerb38eff82000-04-01 11:09:21 +10001012 }
1013}
1014
Damien Millere1537f92010-01-26 13:26:22 +11001015static void
1016channel_pre_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1017{
Damien Millerd530f5f2010-05-21 14:57:10 +10001018 if (c->istate == CHAN_INPUT_OPEN && !c->mux_pause &&
Damien Millere1537f92010-01-26 13:26:22 +11001019 buffer_check_alloc(&c->input, CHAN_RBUF))
1020 FD_SET(c->rfd, readset);
1021 if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
1022 /* clear buffer immediately (discard any partial packet) */
1023 buffer_clear(&c->input);
1024 chan_ibuf_empty(c);
1025 /* Start output drain. XXX just kill chan? */
1026 chan_rcvd_oclose(c);
1027 }
1028 if (c->ostate == CHAN_OUTPUT_OPEN ||
1029 c->ostate == CHAN_OUTPUT_WAIT_DRAIN) {
1030 if (buffer_len(&c->output) > 0)
1031 FD_SET(c->wfd, writeset);
1032 else if (c->ostate == CHAN_OUTPUT_WAIT_DRAIN)
1033 chan_obuf_empty(c);
1034 }
1035}
1036
Ben Lindstromb3921512001-04-11 15:57:50 +00001037/* try to decode a socks4 header */
Damien Miller8473dd82006-07-24 14:08:32 +10001038/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001039static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001040channel_decode_socks4(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001041{
Damien Millera10f5612002-09-12 09:49:15 +10001042 char *p, *host;
Damien Miller1781f532009-01-28 16:24:41 +11001043 u_int len, have, i, found, need;
Damien Miller9f0f5c62001-12-21 14:45:46 +11001044 char username[256];
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001045 struct {
1046 u_int8_t version;
1047 u_int8_t command;
1048 u_int16_t dest_port;
Ben Lindstromb3921512001-04-11 15:57:50 +00001049 struct in_addr dest_addr;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001050 } s4_req, s4_rsp;
1051
Ben Lindstromb3921512001-04-11 15:57:50 +00001052 debug2("channel %d: decode socks4", c->self);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001053
1054 have = buffer_len(&c->input);
1055 len = sizeof(s4_req);
1056 if (have < len)
1057 return 0;
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001058 p = (char *)buffer_ptr(&c->input);
Damien Miller1781f532009-01-28 16:24:41 +11001059
1060 need = 1;
1061 /* SOCKS4A uses an invalid IP address 0.0.0.x */
1062 if (p[4] == 0 && p[5] == 0 && p[6] == 0 && p[7] != 0) {
1063 debug2("channel %d: socks4a request", c->self);
1064 /* ... and needs an extra string (the hostname) */
1065 need = 2;
1066 }
1067 /* Check for terminating NUL on the string(s) */
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001068 for (found = 0, i = len; i < have; i++) {
1069 if (p[i] == '\0') {
Damien Miller1781f532009-01-28 16:24:41 +11001070 found++;
1071 if (found == need)
1072 break;
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001073 }
1074 if (i > 1024) {
1075 /* the peer is probably sending garbage */
1076 debug("channel %d: decode socks4: too long",
1077 c->self);
1078 return -1;
1079 }
1080 }
Damien Miller1781f532009-01-28 16:24:41 +11001081 if (found < need)
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001082 return 0;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001083 buffer_get(&c->input, (char *)&s4_req.version, 1);
1084 buffer_get(&c->input, (char *)&s4_req.command, 1);
1085 buffer_get(&c->input, (char *)&s4_req.dest_port, 2);
Ben Lindstromb3921512001-04-11 15:57:50 +00001086 buffer_get(&c->input, (char *)&s4_req.dest_addr, 4);
Ben Lindstrom2b261b92001-04-17 18:14:34 +00001087 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001088 p = (char *)buffer_ptr(&c->input);
Damien Miller13481292014-02-27 10:18:32 +11001089 if (memchr(p, '\0', have) == NULL)
1090 fatal("channel %d: decode socks4: user not nul terminated",
1091 c->self);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001092 len = strlen(p);
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001093 debug2("channel %d: decode socks4: user %s/%d", c->self, p, len);
Damien Miller1781f532009-01-28 16:24:41 +11001094 len++; /* trailing '\0' */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001095 if (len > have)
Ben Lindstromb3921512001-04-11 15:57:50 +00001096 fatal("channel %d: decode socks4: len %d > have %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001097 c->self, len, have);
1098 strlcpy(username, p, sizeof(username));
1099 buffer_consume(&c->input, len);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001100
Darren Tuckera627d422013-06-02 07:31:17 +10001101 free(c->path);
1102 c->path = NULL;
Damien Miller1781f532009-01-28 16:24:41 +11001103 if (need == 1) { /* SOCKS4: one string */
1104 host = inet_ntoa(s4_req.dest_addr);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001105 c->path = xstrdup(host);
Damien Miller1781f532009-01-28 16:24:41 +11001106 } else { /* SOCKS4A: two strings */
1107 have = buffer_len(&c->input);
djm@openbsd.orgbb005dc2014-10-08 22:15:06 +00001108 p = (char *)buffer_ptr(&c->input);
Damien Miller1781f532009-01-28 16:24:41 +11001109 len = strlen(p);
1110 debug2("channel %d: decode socks4a: host %s/%d",
1111 c->self, p, len);
1112 len++; /* trailing '\0' */
1113 if (len > have)
1114 fatal("channel %d: decode socks4a: len %d > have %d",
1115 c->self, len, have);
Damien Millera1c1b6c2009-01-28 16:29:49 +11001116 if (len > NI_MAXHOST) {
Damien Miller1781f532009-01-28 16:24:41 +11001117 error("channel %d: hostname \"%.100s\" too long",
1118 c->self, p);
1119 return -1;
1120 }
Damien Millera1c1b6c2009-01-28 16:29:49 +11001121 c->path = xstrdup(p);
Damien Miller1781f532009-01-28 16:24:41 +11001122 buffer_consume(&c->input, len);
1123 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001124 c->host_port = ntohs(s4_req.dest_port);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001125
Damien Millerfbdeece2003-09-02 22:52:31 +10001126 debug2("channel %d: dynamic request: socks4 host %s port %u command %u",
Damien Miller1781f532009-01-28 16:24:41 +11001127 c->self, c->path, c->host_port, s4_req.command);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001128
Ben Lindstromb3921512001-04-11 15:57:50 +00001129 if (s4_req.command != 1) {
Damien Miller1781f532009-01-28 16:24:41 +11001130 debug("channel %d: cannot handle: %s cn %d",
1131 c->self, need == 1 ? "SOCKS4" : "SOCKS4A", s4_req.command);
Ben Lindstromb3921512001-04-11 15:57:50 +00001132 return -1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001133 }
Ben Lindstromb3921512001-04-11 15:57:50 +00001134 s4_rsp.version = 0; /* vn: 0 for reply */
1135 s4_rsp.command = 90; /* cd: req granted */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001136 s4_rsp.dest_port = 0; /* ignored */
Ben Lindstromb3921512001-04-11 15:57:50 +00001137 s4_rsp.dest_addr.s_addr = INADDR_ANY; /* ignored */
Damien Millera0fdce92006-03-26 14:28:50 +11001138 buffer_append(&c->output, &s4_rsp, sizeof(s4_rsp));
Ben Lindstromb3921512001-04-11 15:57:50 +00001139 return 1;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001140}
1141
Darren Tucker46471c92003-07-03 13:55:19 +10001142/* try to decode a socks5 header */
1143#define SSH_SOCKS5_AUTHDONE 0x1000
1144#define SSH_SOCKS5_NOAUTH 0x00
1145#define SSH_SOCKS5_IPV4 0x01
1146#define SSH_SOCKS5_DOMAIN 0x03
1147#define SSH_SOCKS5_IPV6 0x04
1148#define SSH_SOCKS5_CONNECT 0x01
1149#define SSH_SOCKS5_SUCCESS 0x00
1150
Damien Miller8473dd82006-07-24 14:08:32 +10001151/* ARGSUSED */
Darren Tucker46471c92003-07-03 13:55:19 +10001152static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001153channel_decode_socks5(Channel *c, fd_set *readset, fd_set *writeset)
Darren Tucker46471c92003-07-03 13:55:19 +10001154{
1155 struct {
1156 u_int8_t version;
1157 u_int8_t command;
1158 u_int8_t reserved;
1159 u_int8_t atyp;
1160 } s5_req, s5_rsp;
1161 u_int16_t dest_port;
Damien Millerce986542013-07-18 16:12:44 +10001162 char dest_addr[255+1], ntop[INET6_ADDRSTRLEN];
1163 u_char *p;
Damien Miller0f077072006-07-10 22:21:02 +10001164 u_int have, need, i, found, nmethods, addrlen, af;
Darren Tucker46471c92003-07-03 13:55:19 +10001165
1166 debug2("channel %d: decode socks5", c->self);
1167 p = buffer_ptr(&c->input);
1168 if (p[0] != 0x05)
1169 return -1;
1170 have = buffer_len(&c->input);
1171 if (!(c->flags & SSH_SOCKS5_AUTHDONE)) {
1172 /* format: ver | nmethods | methods */
Damien Millera8e06ce2003-11-21 23:48:55 +11001173 if (have < 2)
Darren Tucker46471c92003-07-03 13:55:19 +10001174 return 0;
1175 nmethods = p[1];
1176 if (have < nmethods + 2)
1177 return 0;
1178 /* look for method: "NO AUTHENTICATION REQUIRED" */
Damien Miller80163902007-01-05 16:30:16 +11001179 for (found = 0, i = 2; i < nmethods + 2; i++) {
Damien Miller4dec5d72006-08-05 11:38:40 +10001180 if (p[i] == SSH_SOCKS5_NOAUTH) {
Darren Tucker46471c92003-07-03 13:55:19 +10001181 found = 1;
1182 break;
1183 }
1184 }
1185 if (!found) {
1186 debug("channel %d: method SSH_SOCKS5_NOAUTH not found",
1187 c->self);
1188 return -1;
1189 }
1190 buffer_consume(&c->input, nmethods + 2);
1191 buffer_put_char(&c->output, 0x05); /* version */
1192 buffer_put_char(&c->output, SSH_SOCKS5_NOAUTH); /* method */
1193 FD_SET(c->sock, writeset);
1194 c->flags |= SSH_SOCKS5_AUTHDONE;
1195 debug2("channel %d: socks5 auth done", c->self);
1196 return 0; /* need more */
1197 }
1198 debug2("channel %d: socks5 post auth", c->self);
1199 if (have < sizeof(s5_req)+1)
1200 return 0; /* need more */
Damien Millere3b21a52006-03-26 14:29:06 +11001201 memcpy(&s5_req, p, sizeof(s5_req));
Darren Tucker46471c92003-07-03 13:55:19 +10001202 if (s5_req.version != 0x05 ||
1203 s5_req.command != SSH_SOCKS5_CONNECT ||
1204 s5_req.reserved != 0x00) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001205 debug2("channel %d: only socks5 connect supported", c->self);
Darren Tucker46471c92003-07-03 13:55:19 +10001206 return -1;
1207 }
Darren Tucker47eede72005-03-14 23:08:12 +11001208 switch (s5_req.atyp){
Darren Tucker46471c92003-07-03 13:55:19 +10001209 case SSH_SOCKS5_IPV4:
1210 addrlen = 4;
1211 af = AF_INET;
1212 break;
1213 case SSH_SOCKS5_DOMAIN:
1214 addrlen = p[sizeof(s5_req)];
1215 af = -1;
1216 break;
1217 case SSH_SOCKS5_IPV6:
1218 addrlen = 16;
1219 af = AF_INET6;
1220 break;
1221 default:
Damien Millerfbdeece2003-09-02 22:52:31 +10001222 debug2("channel %d: bad socks5 atyp %d", c->self, s5_req.atyp);
Darren Tucker46471c92003-07-03 13:55:19 +10001223 return -1;
1224 }
Damien Miller0f077072006-07-10 22:21:02 +10001225 need = sizeof(s5_req) + addrlen + 2;
1226 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1227 need++;
1228 if (have < need)
Darren Tucker46471c92003-07-03 13:55:19 +10001229 return 0;
1230 buffer_consume(&c->input, sizeof(s5_req));
1231 if (s5_req.atyp == SSH_SOCKS5_DOMAIN)
1232 buffer_consume(&c->input, 1); /* host string length */
Damien Millerce986542013-07-18 16:12:44 +10001233 buffer_get(&c->input, &dest_addr, addrlen);
Darren Tucker46471c92003-07-03 13:55:19 +10001234 buffer_get(&c->input, (char *)&dest_port, 2);
1235 dest_addr[addrlen] = '\0';
Darren Tuckera627d422013-06-02 07:31:17 +10001236 free(c->path);
1237 c->path = NULL;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001238 if (s5_req.atyp == SSH_SOCKS5_DOMAIN) {
Damien Miller9576ac42009-01-28 16:30:33 +11001239 if (addrlen >= NI_MAXHOST) {
Damien Millera1c1b6c2009-01-28 16:29:49 +11001240 error("channel %d: dynamic request: socks5 hostname "
1241 "\"%.100s\" too long", c->self, dest_addr);
1242 return -1;
1243 }
1244 c->path = xstrdup(dest_addr);
1245 } else {
1246 if (inet_ntop(af, dest_addr, ntop, sizeof(ntop)) == NULL)
1247 return -1;
1248 c->path = xstrdup(ntop);
1249 }
Darren Tucker46471c92003-07-03 13:55:19 +10001250 c->host_port = ntohs(dest_port);
Damien Miller787b2ec2003-11-21 23:56:47 +11001251
Damien Millerfbdeece2003-09-02 22:52:31 +10001252 debug2("channel %d: dynamic request: socks5 host %s port %u command %u",
Darren Tucker46471c92003-07-03 13:55:19 +10001253 c->self, c->path, c->host_port, s5_req.command);
1254
1255 s5_rsp.version = 0x05;
1256 s5_rsp.command = SSH_SOCKS5_SUCCESS;
1257 s5_rsp.reserved = 0; /* ignored */
1258 s5_rsp.atyp = SSH_SOCKS5_IPV4;
Darren Tucker46471c92003-07-03 13:55:19 +10001259 dest_port = 0; /* ignored */
1260
Damien Millera0fdce92006-03-26 14:28:50 +11001261 buffer_append(&c->output, &s5_rsp, sizeof(s5_rsp));
Damien Miller13840e02013-09-14 09:49:43 +10001262 buffer_put_int(&c->output, ntohl(INADDR_ANY)); /* bind address */
Damien Millera0fdce92006-03-26 14:28:50 +11001263 buffer_append(&c->output, &dest_port, sizeof(dest_port));
Darren Tucker46471c92003-07-03 13:55:19 +10001264 return 1;
1265}
1266
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001267Channel *
Damien Millere1537f92010-01-26 13:26:22 +11001268channel_connect_stdio_fwd(const char *host_to_connect, u_short port_to_connect,
1269 int in, int out)
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001270{
1271 Channel *c;
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001272
1273 debug("channel_connect_stdio_fwd %s:%d", host_to_connect,
1274 port_to_connect);
1275
Darren Tucker7ad8dd22010-01-12 19:40:27 +11001276 c = channel_new("stdio-forward", SSH_CHANNEL_OPENING, in, out,
1277 -1, CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
1278 0, "stdio-forward", /*nonblock*/0);
1279
1280 c->path = xstrdup(host_to_connect);
1281 c->host_port = port_to_connect;
1282 c->listening_port = 0;
1283 c->force_drain = 1;
1284
1285 channel_register_fds(c, in, out, -1, 0, 1, 0);
1286 port_open_helper(c, "direct-tcpip");
1287
1288 return c;
1289}
1290
Ben Lindstromb3921512001-04-11 15:57:50 +00001291/* dynamic port forwarding */
Ben Lindstrombba81212001-06-25 05:01:22 +00001292static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001293channel_pre_dynamic(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstromb3921512001-04-11 15:57:50 +00001294{
1295 u_char *p;
Damien Millereccb9de2005-06-17 12:59:34 +10001296 u_int have;
1297 int ret;
Ben Lindstromb3921512001-04-11 15:57:50 +00001298
1299 have = buffer_len(&c->input);
Ben Lindstromb3921512001-04-11 15:57:50 +00001300 debug2("channel %d: pre_dynamic: have %d", c->self, have);
Ben Lindstromc486d882001-04-11 16:08:34 +00001301 /* buffer_dump(&c->input); */
Ben Lindstromb3921512001-04-11 15:57:50 +00001302 /* check if the fixed size part of the packet is in buffer. */
Darren Tucker46471c92003-07-03 13:55:19 +10001303 if (have < 3) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001304 /* need more */
1305 FD_SET(c->sock, readset);
1306 return;
1307 }
1308 /* try to guess the protocol */
1309 p = buffer_ptr(&c->input);
1310 switch (p[0]) {
Ben Lindstrom6fa9d102001-04-11 23:08:17 +00001311 case 0x04:
1312 ret = channel_decode_socks4(c, readset, writeset);
1313 break;
Darren Tucker46471c92003-07-03 13:55:19 +10001314 case 0x05:
1315 ret = channel_decode_socks5(c, readset, writeset);
1316 break;
Ben Lindstromb3921512001-04-11 15:57:50 +00001317 default:
1318 ret = -1;
1319 break;
1320 }
1321 if (ret < 0) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001322 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001323 } else if (ret == 0) {
1324 debug2("channel %d: pre_dynamic: need more", c->self);
1325 /* need more */
1326 FD_SET(c->sock, readset);
1327 } else {
1328 /* switch to the next state */
1329 c->type = SSH_CHANNEL_OPENING;
1330 port_open_helper(c, "direct-tcpip");
1331 }
1332}
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001333
Damien Millerb38eff82000-04-01 11:09:21 +10001334/* This is our fake X11 server socket. */
Damien Miller8473dd82006-07-24 14:08:32 +10001335/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001336static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001337channel_post_x11_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001338{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001339 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001340 struct sockaddr_storage addr;
Damien Miller37f1c082013-04-23 15:20:43 +10001341 int newsock, oerrno;
Damien Millerb38eff82000-04-01 11:09:21 +10001342 socklen_t addrlen;
Damien Millerd83ff352001-01-30 09:19:34 +11001343 char buf[16384], *remote_ipaddr;
Damien Millerbd483e72000-04-30 10:00:53 +10001344 int remote_port;
Damien Millerb38eff82000-04-01 11:09:21 +10001345
1346 if (FD_ISSET(c->sock, readset)) {
1347 debug("X11 connection requested.");
1348 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001349 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millere7378562001-12-21 14:58:35 +11001350 if (c->single_connection) {
Damien Miller37f1c082013-04-23 15:20:43 +10001351 oerrno = errno;
Damien Millerfbdeece2003-09-02 22:52:31 +10001352 debug2("single_connection: closing X11 listener.");
Damien Millere7378562001-12-21 14:58:35 +11001353 channel_close_fd(&c->sock);
1354 chan_mark_dead(c);
Damien Miller37f1c082013-04-23 15:20:43 +10001355 errno = oerrno;
Damien Millere7378562001-12-21 14:58:35 +11001356 }
Damien Millerb38eff82000-04-01 11:09:21 +10001357 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001358 if (errno != EINTR && errno != EWOULDBLOCK &&
1359 errno != ECONNABORTED)
1360 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001361 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001362 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001363 return;
1364 }
Damien Miller398e1cf2002-02-05 11:52:13 +11001365 set_nodelay(newsock);
Damien Millerd83ff352001-01-30 09:19:34 +11001366 remote_ipaddr = get_peer_ipaddr(newsock);
Damien Millerbd483e72000-04-30 10:00:53 +10001367 remote_port = get_peer_port(newsock);
Damien Millerb38eff82000-04-01 11:09:21 +10001368 snprintf(buf, sizeof buf, "X11 connection from %.200s port %d",
Damien Millerd83ff352001-01-30 09:19:34 +11001369 remote_ipaddr, remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001370
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001371 nc = channel_new("accepted x11 socket",
Damien Millerbd483e72000-04-30 10:00:53 +10001372 SSH_CHANNEL_OPENING, newsock, newsock, -1,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001373 c->local_window_max, c->local_maxpacket, 0, buf, 1);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001374 packet_start(SSH2_MSG_CHANNEL_OPEN);
1375 packet_put_cstring("x11");
1376 packet_put_int(nc->self);
1377 packet_put_int(nc->local_window_max);
1378 packet_put_int(nc->local_maxpacket);
1379 /* originator ipaddr and port */
1380 packet_put_cstring(remote_ipaddr);
1381 if (datafellows & SSH_BUG_X11FWD) {
1382 debug2("ssh2 x11 bug compat mode");
Damien Millerbd483e72000-04-30 10:00:53 +10001383 } else {
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001384 packet_put_int(remote_port);
Damien Millerbd483e72000-04-30 10:00:53 +10001385 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001386 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10001387 free(remote_ipaddr);
Damien Millerb38eff82000-04-01 11:09:21 +10001388 }
1389}
1390
Ben Lindstrombba81212001-06-25 05:01:22 +00001391static void
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001392port_open_helper(Channel *c, char *rtype)
1393{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001394 char buf[1024];
Damien Miller4def1842013-12-29 17:45:26 +11001395 char *local_ipaddr = get_local_ipaddr(c->sock);
djm@openbsd.org95767262016-03-07 19:02:43 +00001396 int local_port = c->sock == -1 ? 65536 : get_local_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001397 char *remote_ipaddr = get_peer_ipaddr(c->sock);
Damien Miller677257f2005-06-17 12:55:03 +10001398 int remote_port = get_peer_port(c->sock);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001399
Damien Millerd6369432010-02-02 17:02:07 +11001400 if (remote_port == -1) {
1401 /* Fake addr/port to appease peers that validate it (Tectia) */
Darren Tuckera627d422013-06-02 07:31:17 +10001402 free(remote_ipaddr);
Damien Millerd6369432010-02-02 17:02:07 +11001403 remote_ipaddr = xstrdup("127.0.0.1");
1404 remote_port = 65535;
1405 }
1406
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001407 snprintf(buf, sizeof buf,
1408 "%s: listening port %d for %.100s port %d, "
Damien Miller4def1842013-12-29 17:45:26 +11001409 "connect from %.200s port %d to %.100s port %d",
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001410 rtype, c->listening_port, c->path, c->host_port,
Damien Miller4def1842013-12-29 17:45:26 +11001411 remote_ipaddr, remote_port, local_ipaddr, local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001412
Darren Tuckera627d422013-06-02 07:31:17 +10001413 free(c->remote_name);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001414 c->remote_name = xstrdup(buf);
1415
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001416 packet_start(SSH2_MSG_CHANNEL_OPEN);
1417 packet_put_cstring(rtype);
1418 packet_put_int(c->self);
1419 packet_put_int(c->local_window_max);
1420 packet_put_int(c->local_maxpacket);
1421 if (strcmp(rtype, "direct-tcpip") == 0) {
1422 /* target host, port */
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001423 packet_put_cstring(c->path);
1424 packet_put_int(c->host_port);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001425 } else if (strcmp(rtype, "direct-streamlocal@openssh.com") == 0) {
1426 /* target path */
1427 packet_put_cstring(c->path);
1428 } else if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1429 /* listen path */
1430 packet_put_cstring(c->path);
1431 } else {
1432 /* listen address, port */
1433 packet_put_cstring(c->path);
1434 packet_put_int(local_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001435 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001436 if (strcmp(rtype, "forwarded-streamlocal@openssh.com") == 0) {
1437 /* reserved for future owner/mode info */
1438 packet_put_cstring("");
1439 } else {
1440 /* originator host and port */
1441 packet_put_cstring(remote_ipaddr);
1442 packet_put_int((u_int)remote_port);
1443 }
1444 packet_send();
Darren Tuckera627d422013-06-02 07:31:17 +10001445 free(remote_ipaddr);
Damien Miller4def1842013-12-29 17:45:26 +11001446 free(local_ipaddr);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001447}
1448
Damien Miller5e7fd072005-11-05 14:53:39 +11001449static void
1450channel_set_reuseaddr(int fd)
1451{
1452 int on = 1;
1453
1454 /*
1455 * Set socket options.
1456 * Allow local port reuse in TIME_WAIT.
1457 */
1458 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1)
1459 error("setsockopt SO_REUSEADDR fd %d: %s", fd, strerror(errno));
1460}
1461
djm@openbsd.org1bf477d2015-07-01 02:26:31 +00001462void
1463channel_set_x11_refuse_time(u_int refuse_time)
1464{
1465 x11_refuse_time = refuse_time;
1466}
1467
Damien Millerb38eff82000-04-01 11:09:21 +10001468/*
1469 * This socket is listening for connections to a forwarded TCP/IP port.
1470 */
Damien Miller8473dd82006-07-24 14:08:32 +10001471/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001472static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001473channel_post_port_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001474{
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001475 Channel *nc;
Damien Miller163886f2008-07-14 11:28:58 +10001476 struct sockaddr_storage addr;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001477 int newsock, nextstate;
Damien Millerb38eff82000-04-01 11:09:21 +10001478 socklen_t addrlen;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001479 char *rtype;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001480
Damien Millerb38eff82000-04-01 11:09:21 +10001481 if (FD_ISSET(c->sock, readset)) {
1482 debug("Connection to port %d forwarding "
1483 "to %.100s port %d requested.",
1484 c->listening_port, c->path, c->host_port);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001485
Damien Miller4623a752001-10-10 15:03:58 +10001486 if (c->type == SSH_CHANNEL_RPORT_LISTENER) {
1487 nextstate = SSH_CHANNEL_OPENING;
1488 rtype = "forwarded-tcpip";
Damien Miller7acefbb2014-07-18 14:11:24 +10001489 } else if (c->type == SSH_CHANNEL_RUNIX_LISTENER) {
1490 nextstate = SSH_CHANNEL_OPENING;
1491 rtype = "forwarded-streamlocal@openssh.com";
1492 } else if (c->host_port == PORT_STREAMLOCAL) {
1493 nextstate = SSH_CHANNEL_OPENING;
1494 rtype = "direct-streamlocal@openssh.com";
1495 } else if (c->host_port == 0) {
1496 nextstate = SSH_CHANNEL_DYNAMIC;
1497 rtype = "dynamic-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001498 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10001499 nextstate = SSH_CHANNEL_OPENING;
1500 rtype = "direct-tcpip";
Damien Miller4623a752001-10-10 15:03:58 +10001501 }
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001502
Damien Millerb38eff82000-04-01 11:09:21 +10001503 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001504 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001505 if (newsock < 0) {
Damien Miller37f1c082013-04-23 15:20:43 +10001506 if (errno != EINTR && errno != EWOULDBLOCK &&
1507 errno != ECONNABORTED)
1508 error("accept: %.100s", strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001509 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001510 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001511 return;
1512 }
Damien Miller7acefbb2014-07-18 14:11:24 +10001513 if (c->host_port != PORT_STREAMLOCAL)
1514 set_nodelay(newsock);
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001515 nc = channel_new(rtype, nextstate, newsock, newsock, -1,
1516 c->local_window_max, c->local_maxpacket, 0, rtype, 1);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001517 nc->listening_port = c->listening_port;
1518 nc->host_port = c->host_port;
Damien Millera1c1b6c2009-01-28 16:29:49 +11001519 if (c->path != NULL)
1520 nc->path = xstrdup(c->path);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001521
Darren Tucker876045b2010-01-08 17:08:00 +11001522 if (nextstate != SSH_CHANNEL_DYNAMIC)
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001523 port_open_helper(nc, rtype);
Damien Millerb38eff82000-04-01 11:09:21 +10001524 }
1525}
1526
1527/*
1528 * This is the authentication agent socket listening for connections from
1529 * clients.
1530 */
Damien Miller8473dd82006-07-24 14:08:32 +10001531/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001532static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001533channel_post_auth_listener(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001534{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001535 Channel *nc;
1536 int newsock;
Damien Miller163886f2008-07-14 11:28:58 +10001537 struct sockaddr_storage addr;
Damien Millerb38eff82000-04-01 11:09:21 +10001538 socklen_t addrlen;
1539
1540 if (FD_ISSET(c->sock, readset)) {
1541 addrlen = sizeof(addr);
Damien Miller163886f2008-07-14 11:28:58 +10001542 newsock = accept(c->sock, (struct sockaddr *)&addr, &addrlen);
Damien Millerb38eff82000-04-01 11:09:21 +10001543 if (newsock < 0) {
Damien Millera6508752012-04-22 11:21:10 +10001544 error("accept from auth socket: %.100s",
1545 strerror(errno));
1546 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001547 c->notbefore = monotime() + 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001548 return;
1549 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001550 nc = channel_new("accepted auth socket",
Damien Miller0bc1bd82000-11-13 22:57:25 +11001551 SSH_CHANNEL_OPENING, newsock, newsock, -1,
1552 c->local_window_max, c->local_maxpacket,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10001553 0, "accepted auth socket", 1);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001554 packet_start(SSH2_MSG_CHANNEL_OPEN);
1555 packet_put_cstring("auth-agent@openssh.com");
1556 packet_put_int(nc->self);
1557 packet_put_int(c->local_window_max);
1558 packet_put_int(c->local_maxpacket);
Damien Millerb38eff82000-04-01 11:09:21 +10001559 packet_send();
1560 }
1561}
1562
Damien Miller8473dd82006-07-24 14:08:32 +10001563/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001564static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001565channel_post_connecting(Channel *c, fd_set *readset, fd_set *writeset)
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001566{
Damien Millerbd740252008-05-19 15:37:09 +10001567 int err = 0, sock;
Ben Lindstrom11180952001-07-04 05:13:35 +00001568 socklen_t sz = sizeof(err);
Ben Lindstrom69128662001-05-08 20:07:39 +00001569
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001570 if (FD_ISSET(c->sock, writeset)) {
Ben Lindstrom733a2352002-03-05 01:31:28 +00001571 if (getsockopt(c->sock, SOL_SOCKET, SO_ERROR, &err, &sz) < 0) {
Ben Lindstrom69128662001-05-08 20:07:39 +00001572 err = errno;
1573 error("getsockopt SO_ERROR failed");
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001574 }
Ben Lindstrom69128662001-05-08 20:07:39 +00001575 if (err == 0) {
Damien Millerbd740252008-05-19 15:37:09 +10001576 debug("channel %d: connected to %s port %d",
1577 c->self, c->connect_ctx.host, c->connect_ctx.port);
1578 channel_connect_ctx_free(&c->connect_ctx);
Ben Lindstrom69128662001-05-08 20:07:39 +00001579 c->type = SSH_CHANNEL_OPEN;
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001580 packet_start(SSH2_MSG_CHANNEL_OPEN_CONFIRMATION);
1581 packet_put_int(c->remote_id);
1582 packet_put_int(c->self);
1583 packet_put_int(c->local_window);
1584 packet_put_int(c->local_maxpacket);
Ben Lindstrom69128662001-05-08 20:07:39 +00001585 } else {
Damien Millerbd740252008-05-19 15:37:09 +10001586 debug("channel %d: connection failed: %s",
Ben Lindstrom69128662001-05-08 20:07:39 +00001587 c->self, strerror(err));
Damien Millerbd740252008-05-19 15:37:09 +10001588 /* Try next address, if any */
1589 if ((sock = connect_next(&c->connect_ctx)) > 0) {
1590 close(c->sock);
1591 c->sock = c->rfd = c->wfd = sock;
1592 channel_max_fd = channel_find_maxfd();
1593 return;
1594 }
1595 /* Exhausted all addresses */
1596 error("connect_to %.100s port %d: failed.",
1597 c->connect_ctx.host, c->connect_ctx.port);
1598 channel_connect_ctx_free(&c->connect_ctx);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001599 packet_start(SSH2_MSG_CHANNEL_OPEN_FAILURE);
1600 packet_put_int(c->remote_id);
1601 packet_put_int(SSH2_OPEN_CONNECT_FAILED);
1602 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
1603 packet_put_cstring(strerror(err));
1604 packet_put_cstring("");
Ben Lindstrom69128662001-05-08 20:07:39 +00001605 }
1606 chan_mark_dead(c);
1607 }
1608 packet_send();
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001609 }
1610}
1611
Damien Miller8473dd82006-07-24 14:08:32 +10001612/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001613static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001614channel_handle_rfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001615{
Darren Tucker11327cc2005-03-14 23:22:25 +11001616 char buf[CHAN_RBUF];
Damien Miller835284b2007-06-11 13:03:16 +10001617 int len, force;
Damien Millerb38eff82000-04-01 11:09:21 +10001618
Damien Miller835284b2007-06-11 13:03:16 +10001619 force = c->isatty && c->detach_close && c->istate != CHAN_INPUT_CLOSED;
1620 if (c->rfd != -1 && (force || FD_ISSET(c->rfd, readset))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001621 errno = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001622 len = read(c->rfd, buf, sizeof(buf));
Damien Millerd8968ad2008-07-04 23:10:49 +10001623 if (len < 0 && (errno == EINTR ||
1624 ((errno == EAGAIN || errno == EWOULDBLOCK) && !force)))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001625 return 1;
Darren Tucker9afe1152006-06-23 21:24:12 +10001626#ifndef PTY_ZEROREAD
Damien Millerb38eff82000-04-01 11:09:21 +10001627 if (len <= 0) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001628#else
Darren Tucker144e8d62006-06-25 08:25:25 +10001629 if ((!c->isatty && len <= 0) ||
1630 (c->isatty && (len < 0 || (len == 0 && errno != 0)))) {
Darren Tucker9afe1152006-06-23 21:24:12 +10001631#endif
Damien Millerfbdeece2003-09-02 22:52:31 +10001632 debug2("channel %d: read<=0 rfd %d len %d",
Damien Millerb38eff82000-04-01 11:09:21 +10001633 c->self, c->rfd, len);
Ben Lindstromb3921512001-04-11 15:57:50 +00001634 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001635 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001636 chan_mark_dead(c);
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001637 return -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001638 } else {
1639 chan_read_failed(c);
1640 }
1641 return -1;
1642 }
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00001643 if (c->input_filter != NULL) {
Damien Millerad833b32000-08-23 10:46:23 +10001644 if (c->input_filter(c, buf, len) == -1) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001645 debug2("channel %d: filter stops", c->self);
Damien Millerad833b32000-08-23 10:46:23 +10001646 chan_read_failed(c);
1647 }
Damien Millerd27b9472005-12-13 19:29:02 +11001648 } else if (c->datagram) {
1649 buffer_put_string(&c->input, buf, len);
Damien Millerad833b32000-08-23 10:46:23 +10001650 } else {
1651 buffer_append(&c->input, buf, len);
1652 }
Damien Millerb38eff82000-04-01 11:09:21 +10001653 }
1654 return 1;
1655}
Damien Miller4f7becb2006-03-26 14:10:14 +11001656
Damien Miller8473dd82006-07-24 14:08:32 +10001657/* ARGSUSED */
Ben Lindstrombba81212001-06-25 05:01:22 +00001658static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001659channel_handle_wfd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001660{
Ben Lindstrome229b252001-03-05 06:28:06 +00001661 struct termios tio;
Damien Miller077b2382005-12-31 16:22:32 +11001662 u_char *data = NULL, *buf;
Damien Miller7d457182010-08-05 23:09:48 +10001663 u_int dlen, olen = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001664 int len;
1665
1666 /* Send buffered output data to the socket. */
Ben Lindstromc0dee1a2001-06-05 20:52:50 +00001667 if (c->wfd != -1 &&
Damien Millerb38eff82000-04-01 11:09:21 +10001668 FD_ISSET(c->wfd, writeset) &&
1669 buffer_len(&c->output) > 0) {
Damien Miller7d457182010-08-05 23:09:48 +10001670 olen = buffer_len(&c->output);
Damien Miller077b2382005-12-31 16:22:32 +11001671 if (c->output_filter != NULL) {
1672 if ((buf = c->output_filter(c, &data, &dlen)) == NULL) {
1673 debug2("channel %d: filter stops", c->self);
Damien Millere204f6a2006-01-31 21:47:15 +11001674 if (c->type != SSH_CHANNEL_OPEN)
1675 chan_mark_dead(c);
1676 else
1677 chan_write_failed(c);
1678 return -1;
Damien Miller077b2382005-12-31 16:22:32 +11001679 }
1680 } else if (c->datagram) {
1681 buf = data = buffer_get_string(&c->output, &dlen);
1682 } else {
1683 buf = data = buffer_ptr(&c->output);
1684 dlen = buffer_len(&c->output);
1685 }
1686
Damien Millerd27b9472005-12-13 19:29:02 +11001687 if (c->datagram) {
Damien Millerd27b9472005-12-13 19:29:02 +11001688 /* ignore truncated writes, datagrams might get lost */
Damien Miller077b2382005-12-31 16:22:32 +11001689 len = write(c->wfd, buf, dlen);
Darren Tuckera627d422013-06-02 07:31:17 +10001690 free(data);
Damien Millerd8968ad2008-07-04 23:10:49 +10001691 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1692 errno == EWOULDBLOCK))
Damien Millerd27b9472005-12-13 19:29:02 +11001693 return 1;
1694 if (len <= 0) {
1695 if (c->type != SSH_CHANNEL_OPEN)
1696 chan_mark_dead(c);
1697 else
1698 chan_write_failed(c);
1699 return -1;
1700 }
Damien Miller7d457182010-08-05 23:09:48 +10001701 goto out;
Damien Millerd27b9472005-12-13 19:29:02 +11001702 }
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001703#ifdef _AIX
Damien Millera8e06ce2003-11-21 23:48:55 +11001704 /* XXX: Later AIX versions can't push as much data to tty */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001705 if (c->wfd_isatty)
Darren Tucker240fdfa2003-11-22 14:10:02 +11001706 dlen = MIN(dlen, 8*1024);
Ben Lindstrom92ea0ea2002-07-04 18:11:09 +00001707#endif
Damien Miller077b2382005-12-31 16:22:32 +11001708
1709 len = write(c->wfd, buf, dlen);
Damien Millerd8968ad2008-07-04 23:10:49 +10001710 if (len < 0 &&
1711 (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
Damien Miller6f83b8e2000-05-02 09:23:45 +10001712 return 1;
Damien Millerb38eff82000-04-01 11:09:21 +10001713 if (len <= 0) {
Ben Lindstromb3921512001-04-11 15:57:50 +00001714 if (c->type != SSH_CHANNEL_OPEN) {
Damien Millerfbdeece2003-09-02 22:52:31 +10001715 debug2("channel %d: not open", c->self);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00001716 chan_mark_dead(c);
Ben Lindstromb3921512001-04-11 15:57:50 +00001717 return -1;
Damien Millerb38eff82000-04-01 11:09:21 +10001718 } else {
1719 chan_write_failed(c);
1720 }
1721 return -1;
1722 }
Darren Tucker3980b632009-08-28 11:02:37 +10001723#ifndef BROKEN_TCGETATTR_ICANON
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001724 if (c->isatty && dlen >= 1 && buf[0] != '\r') {
Damien Miller79438cc2001-02-16 12:34:57 +11001725 if (tcgetattr(c->wfd, &tio) == 0 &&
1726 !(tio.c_lflag & ECHO) && (tio.c_lflag & ICANON)) {
1727 /*
1728 * Simulate echo to reduce the impact of
Ben Lindstromb40204b2001-03-05 06:29:44 +00001729 * traffic analysis. We need to match the
Ben Lindstrome229b252001-03-05 06:28:06 +00001730 * size of a SSH2_MSG_CHANNEL_DATA message
Damien Miller077b2382005-12-31 16:22:32 +11001731 * (4 byte channel id + buf)
Damien Miller79438cc2001-02-16 12:34:57 +11001732 */
Ben Lindstrome229b252001-03-05 06:28:06 +00001733 packet_send_ignore(4 + len);
Damien Miller79438cc2001-02-16 12:34:57 +11001734 packet_send();
Damien Miller79438cc2001-02-16 12:34:57 +11001735 }
1736 }
Darren Tucker3980b632009-08-28 11:02:37 +10001737#endif
Damien Millerb38eff82000-04-01 11:09:21 +10001738 buffer_consume(&c->output, len);
Damien Miller33b13562000-04-04 14:38:59 +10001739 }
Damien Miller7d457182010-08-05 23:09:48 +10001740 out:
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001741 if (olen > 0)
Damien Miller7d457182010-08-05 23:09:48 +10001742 c->local_consumed += olen - buffer_len(&c->output);
Damien Miller33b13562000-04-04 14:38:59 +10001743 return 1;
1744}
Damien Miller4f7becb2006-03-26 14:10:14 +11001745
Ben Lindstrombba81212001-06-25 05:01:22 +00001746static int
Damien Millerd62f2ca2006-03-26 13:57:41 +11001747channel_handle_efd(Channel *c, fd_set *readset, fd_set *writeset)
Damien Miller33b13562000-04-04 14:38:59 +10001748{
Darren Tucker11327cc2005-03-14 23:22:25 +11001749 char buf[CHAN_RBUF];
Damien Miller33b13562000-04-04 14:38:59 +10001750 int len;
1751
Damien Miller1383bd82000-04-06 12:32:37 +10001752/** XXX handle drain efd, too */
Damien Miller33b13562000-04-04 14:38:59 +10001753 if (c->efd != -1) {
1754 if (c->extended_usage == CHAN_EXTENDED_WRITE &&
1755 FD_ISSET(c->efd, writeset) &&
1756 buffer_len(&c->extended) > 0) {
1757 len = write(c->efd, buffer_ptr(&c->extended),
1758 buffer_len(&c->extended));
Damien Millerd3444942000-09-30 14:20:03 +11001759 debug2("channel %d: written %d to efd %d",
Damien Miller33b13562000-04-04 14:38:59 +10001760 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001761 if (len < 0 && (errno == EINTR || errno == EAGAIN ||
1762 errno == EWOULDBLOCK))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001763 return 1;
1764 if (len <= 0) {
1765 debug2("channel %d: closing write-efd %d",
1766 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001767 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001768 } else {
Damien Miller33b13562000-04-04 14:38:59 +10001769 buffer_consume(&c->extended, len);
1770 c->local_consumed += len;
1771 }
Damien Miller8853ca52010-06-26 10:00:14 +10001772 } else if (c->efd != -1 &&
1773 (c->extended_usage == CHAN_EXTENDED_READ ||
1774 c->extended_usage == CHAN_EXTENDED_IGNORE) &&
Damien Millere42bd242007-01-29 10:16:28 +11001775 (c->detach_close || FD_ISSET(c->efd, readset))) {
Damien Miller33b13562000-04-04 14:38:59 +10001776 len = read(c->efd, buf, sizeof(buf));
Damien Millerd3444942000-09-30 14:20:03 +11001777 debug2("channel %d: read %d from efd %d",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001778 c->self, len, c->efd);
Damien Millerd8968ad2008-07-04 23:10:49 +10001779 if (len < 0 && (errno == EINTR || ((errno == EAGAIN ||
1780 errno == EWOULDBLOCK) && !c->detach_close)))
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001781 return 1;
1782 if (len <= 0) {
1783 debug2("channel %d: closing read-efd %d",
Damien Miller1383bd82000-04-06 12:32:37 +10001784 c->self, c->efd);
Ben Lindstrom16d29d52001-07-18 16:01:46 +00001785 channel_close_fd(&c->efd);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001786 } else {
Damien Miller8853ca52010-06-26 10:00:14 +10001787 if (c->extended_usage == CHAN_EXTENDED_IGNORE) {
1788 debug3("channel %d: discard efd",
1789 c->self);
1790 } else
1791 buffer_append(&c->extended, buf, len);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001792 }
Damien Miller33b13562000-04-04 14:38:59 +10001793 }
1794 }
1795 return 1;
1796}
Damien Miller4f7becb2006-03-26 14:10:14 +11001797
Damien Miller0e220db2004-06-15 10:34:08 +10001798static int
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001799channel_check_window(Channel *c)
Damien Miller33b13562000-04-04 14:38:59 +10001800{
Ben Lindstromb3921512001-04-11 15:57:50 +00001801 if (c->type == SSH_CHANNEL_OPEN &&
1802 !(c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD)) &&
Darren Tuckerae09cb82007-06-25 19:04:46 +10001803 ((c->local_window_max - c->local_window >
Damien Miller3191a8e2007-06-11 18:33:15 +10001804 c->local_maxpacket*3) ||
1805 c->local_window < c->local_window_max/2) &&
Damien Miller33b13562000-04-04 14:38:59 +10001806 c->local_consumed > 0) {
1807 packet_start(SSH2_MSG_CHANNEL_WINDOW_ADJUST);
1808 packet_put_int(c->remote_id);
1809 packet_put_int(c->local_consumed);
1810 packet_send();
Damien Millerd3444942000-09-30 14:20:03 +11001811 debug2("channel %d: window %d sent adjust %d",
Damien Miller33b13562000-04-04 14:38:59 +10001812 c->self, c->local_window,
1813 c->local_consumed);
1814 c->local_window += c->local_consumed;
1815 c->local_consumed = 0;
Damien Millerb38eff82000-04-01 11:09:21 +10001816 }
1817 return 1;
1818}
1819
Ben Lindstrombba81212001-06-25 05:01:22 +00001820static void
Damien Millerd62f2ca2006-03-26 13:57:41 +11001821channel_post_open(Channel *c, fd_set *readset, fd_set *writeset)
Damien Millerb38eff82000-04-01 11:09:21 +10001822{
1823 channel_handle_rfd(c, readset, writeset);
1824 channel_handle_wfd(c, readset, writeset);
Damien Miller33b13562000-04-04 14:38:59 +10001825 channel_handle_efd(c, readset, writeset);
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00001826 channel_check_window(c);
Damien Miller33b13562000-04-04 14:38:59 +10001827}
1828
Damien Millere1537f92010-01-26 13:26:22 +11001829static u_int
1830read_mux(Channel *c, u_int need)
1831{
1832 char buf[CHAN_RBUF];
1833 int len;
1834 u_int rlen;
1835
1836 if (buffer_len(&c->input) < need) {
1837 rlen = need - buffer_len(&c->input);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00001838 len = read(c->rfd, buf, MINIMUM(rlen, CHAN_RBUF));
naddy@openbsd.org603ba412016-02-05 13:28:19 +00001839 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1840 return buffer_len(&c->input);
Damien Millere1537f92010-01-26 13:26:22 +11001841 if (len <= 0) {
naddy@openbsd.org603ba412016-02-05 13:28:19 +00001842 debug2("channel %d: ctl read<=0 rfd %d len %d",
1843 c->self, c->rfd, len);
1844 chan_read_failed(c);
1845 return 0;
Damien Millere1537f92010-01-26 13:26:22 +11001846 } else
1847 buffer_append(&c->input, buf, len);
1848 }
1849 return buffer_len(&c->input);
1850}
1851
1852static void
1853channel_post_mux_client(Channel *c, fd_set *readset, fd_set *writeset)
1854{
1855 u_int need;
1856 ssize_t len;
1857
Damien Millerd530f5f2010-05-21 14:57:10 +10001858 if (c->rfd != -1 && !c->mux_pause && FD_ISSET(c->rfd, readset) &&
Damien Millere1537f92010-01-26 13:26:22 +11001859 (c->istate == CHAN_INPUT_OPEN ||
1860 c->istate == CHAN_INPUT_WAIT_DRAIN)) {
1861 /*
1862 * Don't not read past the precise end of packets to
1863 * avoid disrupting fd passing.
1864 */
1865 if (read_mux(c, 4) < 4) /* read header */
1866 return;
1867 need = get_u32(buffer_ptr(&c->input));
1868#define CHANNEL_MUX_MAX_PACKET (256 * 1024)
1869 if (need > CHANNEL_MUX_MAX_PACKET) {
1870 debug2("channel %d: packet too big %u > %u",
1871 c->self, CHANNEL_MUX_MAX_PACKET, need);
1872 chan_rcvd_oclose(c);
1873 return;
1874 }
1875 if (read_mux(c, need + 4) < need + 4) /* read body */
1876 return;
1877 if (c->mux_rcb(c) != 0) {
1878 debug("channel %d: mux_rcb failed", c->self);
1879 chan_mark_dead(c);
1880 return;
1881 }
1882 }
1883
1884 if (c->wfd != -1 && FD_ISSET(c->wfd, writeset) &&
1885 buffer_len(&c->output) > 0) {
1886 len = write(c->wfd, buffer_ptr(&c->output),
1887 buffer_len(&c->output));
1888 if (len < 0 && (errno == EINTR || errno == EAGAIN))
1889 return;
1890 if (len <= 0) {
1891 chan_mark_dead(c);
1892 return;
1893 }
1894 buffer_consume(&c->output, len);
1895 }
1896}
1897
1898static void
1899channel_post_mux_listener(Channel *c, fd_set *readset, fd_set *writeset)
1900{
1901 Channel *nc;
1902 struct sockaddr_storage addr;
1903 socklen_t addrlen;
1904 int newsock;
1905 uid_t euid;
1906 gid_t egid;
1907
1908 if (!FD_ISSET(c->sock, readset))
1909 return;
1910
1911 debug("multiplexing control connection");
1912
1913 /*
1914 * Accept connection on control socket
1915 */
1916 memset(&addr, 0, sizeof(addr));
1917 addrlen = sizeof(addr);
1918 if ((newsock = accept(c->sock, (struct sockaddr*)&addr,
1919 &addrlen)) == -1) {
1920 error("%s accept: %s", __func__, strerror(errno));
Damien Millera6508752012-04-22 11:21:10 +10001921 if (errno == EMFILE || errno == ENFILE)
Darren Tuckerb759c9c2013-06-02 07:46:16 +10001922 c->notbefore = monotime() + 1;
Damien Millere1537f92010-01-26 13:26:22 +11001923 return;
1924 }
1925
1926 if (getpeereid(newsock, &euid, &egid) < 0) {
1927 error("%s getpeereid failed: %s", __func__,
1928 strerror(errno));
1929 close(newsock);
1930 return;
1931 }
1932 if ((euid != 0) && (getuid() != euid)) {
1933 error("multiplex uid mismatch: peer euid %u != uid %u",
1934 (u_int)euid, (u_int)getuid());
1935 close(newsock);
1936 return;
1937 }
1938 nc = channel_new("multiplex client", SSH_CHANNEL_MUX_CLIENT,
1939 newsock, newsock, -1, c->local_window_max,
1940 c->local_maxpacket, 0, "mux-control", 1);
1941 nc->mux_rcb = c->mux_rcb;
1942 debug3("%s: new mux channel %d fd %d", __func__,
1943 nc->self, nc->sock);
1944 /* establish state */
1945 nc->mux_rcb(nc);
1946 /* mux state transitions must not elicit protocol messages */
1947 nc->flags |= CHAN_LOCAL;
1948}
1949
Ben Lindstrombba81212001-06-25 05:01:22 +00001950static void
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001951channel_handler_init(void)
Damien Millerb38eff82000-04-01 11:09:21 +10001952{
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001953 int i;
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00001954
djm@openbsd.org97f4d302017-04-30 23:13:25 +00001955 for (i = 0; i < SSH_CHANNEL_MAX_TYPE; i++) {
1956 channel_pre[i] = NULL;
1957 channel_post[i] = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10001958 }
Damien Millerde6987c2002-01-22 23:20:40 +11001959 channel_pre[SSH_CHANNEL_OPEN] = &channel_pre_open;
Damien Millerbd483e72000-04-30 10:00:53 +10001960 channel_pre[SSH_CHANNEL_X11_OPEN] = &channel_pre_x11_open;
Damien Miller33b13562000-04-04 14:38:59 +10001961 channel_pre[SSH_CHANNEL_PORT_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001962 channel_pre[SSH_CHANNEL_RPORT_LISTENER] = &channel_pre_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10001963 channel_pre[SSH_CHANNEL_UNIX_LISTENER] = &channel_pre_listener;
1964 channel_pre[SSH_CHANNEL_RUNIX_LISTENER] = &channel_pre_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001965 channel_pre[SSH_CHANNEL_X11_LISTENER] = &channel_pre_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001966 channel_pre[SSH_CHANNEL_AUTH_SOCKET] = &channel_pre_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001967 channel_pre[SSH_CHANNEL_CONNECTING] = &channel_pre_connecting;
Ben Lindstrom3bb4f9d2001-04-08 18:30:26 +00001968 channel_pre[SSH_CHANNEL_DYNAMIC] = &channel_pre_dynamic;
Damien Millere1537f92010-01-26 13:26:22 +11001969 channel_pre[SSH_CHANNEL_MUX_LISTENER] = &channel_pre_listener;
1970 channel_pre[SSH_CHANNEL_MUX_CLIENT] = &channel_pre_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10001971
Damien Millerde6987c2002-01-22 23:20:40 +11001972 channel_post[SSH_CHANNEL_OPEN] = &channel_post_open;
Damien Miller33b13562000-04-04 14:38:59 +10001973 channel_post[SSH_CHANNEL_PORT_LISTENER] = &channel_post_port_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001974 channel_post[SSH_CHANNEL_RPORT_LISTENER] = &channel_post_port_listener;
Damien Miller7acefbb2014-07-18 14:11:24 +10001975 channel_post[SSH_CHANNEL_UNIX_LISTENER] = &channel_post_port_listener;
1976 channel_post[SSH_CHANNEL_RUNIX_LISTENER] = &channel_post_port_listener;
Damien Millerbd483e72000-04-30 10:00:53 +10001977 channel_post[SSH_CHANNEL_X11_LISTENER] = &channel_post_x11_listener;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001978 channel_post[SSH_CHANNEL_AUTH_SOCKET] = &channel_post_auth_listener;
Ben Lindstrom7ad97102000-12-06 01:42:49 +00001979 channel_post[SSH_CHANNEL_CONNECTING] = &channel_post_connecting;
Damien Millerde6987c2002-01-22 23:20:40 +11001980 channel_post[SSH_CHANNEL_DYNAMIC] = &channel_post_open;
Damien Millere1537f92010-01-26 13:26:22 +11001981 channel_post[SSH_CHANNEL_MUX_LISTENER] = &channel_post_mux_listener;
1982 channel_post[SSH_CHANNEL_MUX_CLIENT] = &channel_post_mux_client;
Damien Miller33b13562000-04-04 14:38:59 +10001983}
1984
Damien Miller3ec27592001-10-12 11:35:04 +10001985/* gc dead channels */
1986static void
1987channel_garbage_collect(Channel *c)
1988{
1989 if (c == NULL)
1990 return;
1991 if (c->detach_user != NULL) {
Damien Miller39eda6e2005-11-05 14:52:50 +11001992 if (!chan_is_dead(c, c->detach_close))
Damien Miller3ec27592001-10-12 11:35:04 +10001993 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001994 debug2("channel %d: gc: notify user", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10001995 c->detach_user(c->self, NULL);
1996 /* if we still have a callback */
1997 if (c->detach_user != NULL)
1998 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10001999 debug2("channel %d: gc: user detached", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002000 }
2001 if (!chan_is_dead(c, 1))
2002 return;
Damien Millerfbdeece2003-09-02 22:52:31 +10002003 debug2("channel %d: garbage collecting", c->self);
Damien Miller3ec27592001-10-12 11:35:04 +10002004 channel_free(c);
2005}
2006
Ben Lindstrombba81212001-06-25 05:01:22 +00002007static void
Damien Millera6508752012-04-22 11:21:10 +10002008channel_handler(chan_fn *ftab[], fd_set *readset, fd_set *writeset,
2009 time_t *unpause_secs)
Damien Millerb38eff82000-04-01 11:09:21 +10002010{
2011 static int did_init = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002012 u_int i, oalloc;
Damien Millerb38eff82000-04-01 11:09:21 +10002013 Channel *c;
Damien Millera6508752012-04-22 11:21:10 +10002014 time_t now;
Damien Millerb38eff82000-04-01 11:09:21 +10002015
2016 if (!did_init) {
2017 channel_handler_init();
2018 did_init = 1;
2019 }
Darren Tuckerb759c9c2013-06-02 07:46:16 +10002020 now = monotime();
Damien Millera6508752012-04-22 11:21:10 +10002021 if (unpause_secs != NULL)
2022 *unpause_secs = 0;
Darren Tucker876045b2010-01-08 17:08:00 +11002023 for (i = 0, oalloc = channels_alloc; i < oalloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002024 c = channels[i];
2025 if (c == NULL)
Damien Millerb38eff82000-04-01 11:09:21 +10002026 continue;
Darren Tucker876045b2010-01-08 17:08:00 +11002027 if (c->delayed) {
2028 if (ftab == channel_pre)
2029 c->delayed = 0;
2030 else
2031 continue;
2032 }
Damien Millera6508752012-04-22 11:21:10 +10002033 if (ftab[c->type] != NULL) {
2034 /*
2035 * Run handlers that are not paused.
2036 */
2037 if (c->notbefore <= now)
2038 (*ftab[c->type])(c, readset, writeset);
2039 else if (unpause_secs != NULL) {
2040 /*
2041 * Collect the time that the earliest
2042 * channel comes off pause.
2043 */
2044 debug3("%s: chan %d: skip for %d more seconds",
2045 __func__, c->self,
2046 (int)(c->notbefore - now));
2047 if (*unpause_secs == 0 ||
2048 (c->notbefore - now) < *unpause_secs)
2049 *unpause_secs = c->notbefore - now;
2050 }
2051 }
Damien Miller3ec27592001-10-12 11:35:04 +10002052 channel_garbage_collect(c);
Damien Millerb38eff82000-04-01 11:09:21 +10002053 }
Damien Millera6508752012-04-22 11:21:10 +10002054 if (unpause_secs != NULL && *unpause_secs != 0)
2055 debug3("%s: first channel unpauses in %d seconds",
2056 __func__, (int)*unpause_secs);
Damien Millerb38eff82000-04-01 11:09:21 +10002057}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002058
Ben Lindstrome9c99912001-06-09 00:41:05 +00002059/*
2060 * Allocate/update select bitmasks and add any bits relevant to channels in
2061 * select bitmasks.
2062 */
Damien Miller4af51302000-04-16 11:18:38 +10002063void
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002064channel_prepare_select(fd_set **readsetp, fd_set **writesetp, int *maxfdp,
Damien Millerba77e1f2012-04-23 18:21:05 +10002065 u_int *nallocp, time_t *minwait_secs, int rekeying)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002066{
Damien Miller36812092006-03-26 14:22:47 +11002067 u_int n, sz, nfdset;
Damien Miller5e953212001-01-30 09:14:00 +11002068
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00002069 n = MAXIMUM(*maxfdp, channel_max_fd);
Damien Miller5e953212001-01-30 09:14:00 +11002070
Damien Miller36812092006-03-26 14:22:47 +11002071 nfdset = howmany(n+1, NFDBITS);
2072 /* Explicitly test here, because xrealloc isn't always called */
millert@openbsd.orgfd368342015-02-06 23:21:59 +00002073 if (nfdset && SIZE_MAX / nfdset < sizeof(fd_mask))
Damien Miller36812092006-03-26 14:22:47 +11002074 fatal("channel_prepare_select: max_fd (%d) is too large", n);
2075 sz = nfdset * sizeof(fd_mask);
2076
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002077 /* perhaps check sz < nalloc/2 and shrink? */
2078 if (*readsetp == NULL || sz > *nallocp) {
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00002079 *readsetp = xreallocarray(*readsetp, nfdset, sizeof(fd_mask));
2080 *writesetp = xreallocarray(*writesetp, nfdset, sizeof(fd_mask));
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002081 *nallocp = sz;
Damien Miller5e953212001-01-30 09:14:00 +11002082 }
Ben Lindstrom16d29d52001-07-18 16:01:46 +00002083 *maxfdp = n;
Damien Miller5e953212001-01-30 09:14:00 +11002084 memset(*readsetp, 0, sz);
2085 memset(*writesetp, 0, sz);
2086
Ben Lindstrombe2cc432001-04-04 23:46:07 +00002087 if (!rekeying)
Damien Millera6508752012-04-22 11:21:10 +10002088 channel_handler(channel_pre, *readsetp, *writesetp,
2089 minwait_secs);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002090}
2091
Ben Lindstrome9c99912001-06-09 00:41:05 +00002092/*
2093 * After select, perform any appropriate operations for channels which have
2094 * events pending.
2095 */
Damien Miller4af51302000-04-16 11:18:38 +10002096void
Damien Millerd62f2ca2006-03-26 13:57:41 +11002097channel_after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002098{
Damien Millera6508752012-04-22 11:21:10 +10002099 channel_handler(channel_post, readset, writeset, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002100}
2101
Ben Lindstrome9c99912001-06-09 00:41:05 +00002102
Damien Miller5e953212001-01-30 09:14:00 +11002103/* If there is data to send to the connection, enqueue some of it now. */
Damien Miller4af51302000-04-16 11:18:38 +10002104void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00002105channel_output_poll(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002106{
Damien Millerb38eff82000-04-01 11:09:21 +10002107 Channel *c;
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10002108 u_int i, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002109
Damien Miller95def091999-11-25 00:26:21 +11002110 for (i = 0; i < channels_alloc; i++) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002111 c = channels[i];
2112 if (c == NULL)
2113 continue;
Damien Miller34132e52000-01-14 15:45:46 +11002114
Ben Lindstrome9c99912001-06-09 00:41:05 +00002115 /*
2116 * We are only interested in channels that can have buffered
2117 * incoming data.
2118 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002119 if (c->type != SSH_CHANNEL_OPEN)
2120 continue;
2121 if ((c->flags & (CHAN_CLOSE_SENT|CHAN_CLOSE_RCVD))) {
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002122 /* XXX is this true? */
Damien Miller3ec27592001-10-12 11:35:04 +10002123 debug3("channel %d: will not send data after close", c->self);
Damien Miller33b13562000-04-04 14:38:59 +10002124 continue;
2125 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002126
Damien Miller95def091999-11-25 00:26:21 +11002127 /* Get the amount of buffered data for this channel. */
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002128 if ((c->istate == CHAN_INPUT_OPEN ||
2129 c->istate == CHAN_INPUT_WAIT_DRAIN) &&
2130 (len = buffer_len(&c->input)) > 0) {
Damien Millerd27b9472005-12-13 19:29:02 +11002131 if (c->datagram) {
2132 if (len > 0) {
2133 u_char *data;
2134 u_int dlen;
2135
2136 data = buffer_get_string(&c->input,
2137 &dlen);
Damien Miller7d457182010-08-05 23:09:48 +10002138 if (dlen > c->remote_window ||
2139 dlen > c->remote_maxpacket) {
2140 debug("channel %d: datagram "
2141 "too big for channel",
2142 c->self);
Darren Tuckera627d422013-06-02 07:31:17 +10002143 free(data);
Damien Miller7d457182010-08-05 23:09:48 +10002144 continue;
2145 }
Damien Millerd27b9472005-12-13 19:29:02 +11002146 packet_start(SSH2_MSG_CHANNEL_DATA);
2147 packet_put_int(c->remote_id);
2148 packet_put_string(data, dlen);
2149 packet_send();
djm@openbsd.orgf715afe2015-06-30 05:23:25 +00002150 c->remote_window -= dlen;
Darren Tuckera627d422013-06-02 07:31:17 +10002151 free(data);
Damien Millerd27b9472005-12-13 19:29:02 +11002152 }
2153 continue;
2154 }
Ben Lindstrome9c99912001-06-09 00:41:05 +00002155 /*
2156 * Send some data for the other side over the secure
2157 * connection.
2158 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002159 if (len > c->remote_window)
2160 len = c->remote_window;
2161 if (len > c->remote_maxpacket)
2162 len = c->remote_maxpacket;
Damien Millerb38eff82000-04-01 11:09:21 +10002163 if (len > 0) {
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002164 packet_start(SSH2_MSG_CHANNEL_DATA);
Damien Millerb38eff82000-04-01 11:09:21 +10002165 packet_put_int(c->remote_id);
2166 packet_put_string(buffer_ptr(&c->input), len);
2167 packet_send();
2168 buffer_consume(&c->input, len);
2169 c->remote_window -= len;
Damien Millerb38eff82000-04-01 11:09:21 +10002170 }
2171 } else if (c->istate == CHAN_INPUT_WAIT_DRAIN) {
Damien Miller5428f641999-11-25 11:54:57 +11002172 /*
2173 * input-buffer is empty and read-socket shutdown:
Ben Lindstromcf159442002-03-26 03:26:24 +00002174 * tell peer, that we will not send more data: send IEOF.
2175 * hack for extended data: delay EOF if EFD still in use.
Damien Miller5428f641999-11-25 11:54:57 +11002176 */
Ben Lindstromcf159442002-03-26 03:26:24 +00002177 if (CHANNEL_EFD_INPUT_ACTIVE(c))
Damien Miller0dc1bef2005-07-17 17:22:45 +10002178 debug2("channel %d: ibuf_empty delayed efd %d/(%d)",
2179 c->self, c->efd, buffer_len(&c->extended));
Ben Lindstromcf159442002-03-26 03:26:24 +00002180 else
2181 chan_ibuf_empty(c);
Damien Miller95def091999-11-25 00:26:21 +11002182 }
Damien Miller33b13562000-04-04 14:38:59 +10002183 /* Send extended data, i.e. stderr */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002184 if (!(c->flags & CHAN_EOF_SENT) &&
Damien Miller33b13562000-04-04 14:38:59 +10002185 c->remote_window > 0 &&
2186 (len = buffer_len(&c->extended)) > 0 &&
2187 c->extended_usage == CHAN_EXTENDED_READ) {
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002188 debug2("channel %d: rwin %u elen %u euse %d",
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002189 c->self, c->remote_window, buffer_len(&c->extended),
2190 c->extended_usage);
Damien Miller33b13562000-04-04 14:38:59 +10002191 if (len > c->remote_window)
2192 len = c->remote_window;
2193 if (len > c->remote_maxpacket)
2194 len = c->remote_maxpacket;
2195 packet_start(SSH2_MSG_CHANNEL_EXTENDED_DATA);
2196 packet_put_int(c->remote_id);
2197 packet_put_int(SSH2_EXTENDED_DATA_STDERR);
2198 packet_put_string(buffer_ptr(&c->extended), len);
2199 packet_send();
2200 buffer_consume(&c->extended, len);
2201 c->remote_window -= len;
Ben Lindstrom7fbd4552001-03-05 06:16:11 +00002202 debug2("channel %d: sent ext data %d", c->self, len);
Damien Miller33b13562000-04-04 14:38:59 +10002203 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002204 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002205}
2206
markus@openbsd.org8d057842016-09-30 09:19:13 +00002207/* -- mux proxy support */
2208
2209/*
2210 * When multiplexing channel messages for mux clients we have to deal
2211 * with downstream messages from the mux client and upstream messages
2212 * from the ssh server:
2213 * 1) Handling downstream messages is straightforward and happens
2214 * in channel_proxy_downstream():
2215 * - We forward all messages (mostly) unmodified to the server.
2216 * - However, in order to route messages from upstream to the correct
2217 * downstream client, we have to replace the channel IDs used by the
2218 * mux clients with a unique channel ID because the mux clients might
2219 * use conflicting channel IDs.
2220 * - so we inspect and change both SSH2_MSG_CHANNEL_OPEN and
2221 * SSH2_MSG_CHANNEL_OPEN_CONFIRMATION messages, create a local
2222 * SSH_CHANNEL_MUX_PROXY channel and replace the mux clients ID
2223 * with the newly allocated channel ID.
2224 * 2) Upstream messages are received by matching SSH_CHANNEL_MUX_PROXY
2225 * channels and procesed by channel_proxy_upstream(). The local channel ID
2226 * is then translated back to the original mux client ID.
2227 * 3) In both cases we need to keep track of matching SSH2_MSG_CHANNEL_CLOSE
2228 * messages so we can clean up SSH_CHANNEL_MUX_PROXY channels.
2229 * 4) The SSH_CHANNEL_MUX_PROXY channels also need to closed when the
2230 * downstream mux client are removed.
2231 * 5) Handling SSH2_MSG_CHANNEL_OPEN messages from the upstream server
2232 * requires more work, because they are not addressed to a specific
2233 * channel. E.g. client_request_forwarded_tcpip() needs to figure
2234 * out whether the request is addressed to the local client or a
2235 * specific downstream client based on the listen-address/port.
2236 * 6) Agent and X11-Forwarding have a similar problem and are currenly
2237 * not supported as the matching session/channel cannot be identified
2238 * easily.
2239 */
2240
2241/*
2242 * receive packets from downstream mux clients:
2243 * channel callback fired on read from mux client, creates
2244 * SSH_CHANNEL_MUX_PROXY channels and translates channel IDs
2245 * on channel creation.
2246 */
2247int
2248channel_proxy_downstream(Channel *downstream)
2249{
2250 Channel *c = NULL;
2251 struct ssh *ssh = active_state;
2252 struct sshbuf *original = NULL, *modified = NULL;
2253 const u_char *cp;
2254 char *ctype = NULL, *listen_host = NULL;
2255 u_char type;
2256 size_t have;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002257 int ret = -1, r, idx;
2258 u_int id, remote_id, listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002259
2260 /* sshbuf_dump(&downstream->input, stderr); */
2261 if ((r = sshbuf_get_string_direct(&downstream->input, &cp, &have))
2262 != 0) {
2263 error("%s: malformed message: %s", __func__, ssh_err(r));
2264 return -1;
2265 }
2266 if (have < 2) {
2267 error("%s: short message", __func__);
2268 return -1;
2269 }
2270 type = cp[1];
2271 /* skip padlen + type */
2272 cp += 2;
2273 have -= 2;
2274 if (ssh_packet_log_type(type))
2275 debug3("%s: channel %u: down->up: type %u", __func__,
2276 downstream->self, type);
2277
2278 switch (type) {
2279 case SSH2_MSG_CHANNEL_OPEN:
2280 if ((original = sshbuf_from(cp, have)) == NULL ||
2281 (modified = sshbuf_new()) == NULL) {
2282 error("%s: alloc", __func__);
2283 goto out;
2284 }
2285 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0 ||
2286 (r = sshbuf_get_u32(original, &id)) != 0) {
2287 error("%s: parse error %s", __func__, ssh_err(r));
2288 goto out;
2289 }
2290 c = channel_new("mux proxy", SSH_CHANNEL_MUX_PROXY,
2291 -1, -1, -1, 0, 0, 0, ctype, 1);
2292 c->mux_ctx = downstream; /* point to mux client */
2293 c->mux_downstream_id = id; /* original downstream id */
2294 if ((r = sshbuf_put_cstring(modified, ctype)) != 0 ||
2295 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2296 (r = sshbuf_putb(modified, original)) != 0) {
2297 error("%s: compose error %s", __func__, ssh_err(r));
2298 channel_free(c);
2299 goto out;
2300 }
2301 break;
2302 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2303 /*
2304 * Almost the same as SSH2_MSG_CHANNEL_OPEN, except then we
2305 * need to parse 'remote_id' instead of 'ctype'.
2306 */
2307 if ((original = sshbuf_from(cp, have)) == NULL ||
2308 (modified = sshbuf_new()) == NULL) {
2309 error("%s: alloc", __func__);
2310 goto out;
2311 }
2312 if ((r = sshbuf_get_u32(original, &remote_id)) != 0 ||
2313 (r = sshbuf_get_u32(original, &id)) != 0) {
2314 error("%s: parse error %s", __func__, ssh_err(r));
2315 goto out;
2316 }
2317 c = channel_new("mux proxy", SSH_CHANNEL_MUX_PROXY,
2318 -1, -1, -1, 0, 0, 0, "mux-down-connect", 1);
2319 c->mux_ctx = downstream; /* point to mux client */
2320 c->mux_downstream_id = id;
2321 c->remote_id = remote_id;
2322 if ((r = sshbuf_put_u32(modified, remote_id)) != 0 ||
2323 (r = sshbuf_put_u32(modified, c->self)) != 0 ||
2324 (r = sshbuf_putb(modified, original)) != 0) {
2325 error("%s: compose error %s", __func__, ssh_err(r));
2326 channel_free(c);
2327 goto out;
2328 }
2329 break;
2330 case SSH2_MSG_GLOBAL_REQUEST:
2331 if ((original = sshbuf_from(cp, have)) == NULL) {
2332 error("%s: alloc", __func__);
2333 goto out;
2334 }
2335 if ((r = sshbuf_get_cstring(original, &ctype, NULL)) != 0) {
2336 error("%s: parse error %s", __func__, ssh_err(r));
2337 goto out;
2338 }
2339 if (strcmp(ctype, "tcpip-forward") != 0) {
2340 error("%s: unsupported request %s", __func__, ctype);
2341 goto out;
2342 }
2343 if ((r = sshbuf_get_u8(original, NULL)) != 0 ||
2344 (r = sshbuf_get_cstring(original, &listen_host, NULL)) != 0 ||
2345 (r = sshbuf_get_u32(original, &listen_port)) != 0) {
2346 error("%s: parse error %s", __func__, ssh_err(r));
2347 goto out;
2348 }
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002349 if (listen_port > 65535) {
2350 error("%s: tcpip-forward for %s: bad port %u",
2351 __func__, listen_host, listen_port);
2352 goto out;
2353 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002354 /* Record that connection to this host/port is permitted. */
2355 permitted_opens = xreallocarray(permitted_opens,
2356 num_permitted_opens + 1, sizeof(*permitted_opens));
2357 idx = num_permitted_opens++;
2358 permitted_opens[idx].host_to_connect = xstrdup("<mux>");
2359 permitted_opens[idx].port_to_connect = -1;
2360 permitted_opens[idx].listen_host = listen_host;
djm@openbsd.org2f78a2a2016-09-30 20:24:46 +00002361 permitted_opens[idx].listen_port = (int)listen_port;
markus@openbsd.org8d057842016-09-30 09:19:13 +00002362 permitted_opens[idx].downstream = downstream;
2363 listen_host = NULL;
2364 break;
2365 case SSH2_MSG_CHANNEL_CLOSE:
2366 if (have < 4)
2367 break;
2368 remote_id = PEEK_U32(cp);
2369 if ((c = channel_by_remote_id(remote_id)) != NULL) {
2370 if (c->flags & CHAN_CLOSE_RCVD)
2371 channel_free(c);
2372 else
2373 c->flags |= CHAN_CLOSE_SENT;
2374 }
2375 break;
2376 }
2377 if (modified) {
2378 if ((r = sshpkt_start(ssh, type)) != 0 ||
2379 (r = sshpkt_putb(ssh, modified)) != 0 ||
2380 (r = sshpkt_send(ssh)) != 0) {
2381 error("%s: send %s", __func__, ssh_err(r));
2382 goto out;
2383 }
2384 } else {
2385 if ((r = sshpkt_start(ssh, type)) != 0 ||
2386 (r = sshpkt_put(ssh, cp, have)) != 0 ||
2387 (r = sshpkt_send(ssh)) != 0) {
2388 error("%s: send %s", __func__, ssh_err(r));
2389 goto out;
2390 }
2391 }
2392 ret = 0;
2393 out:
2394 free(ctype);
2395 free(listen_host);
2396 sshbuf_free(original);
2397 sshbuf_free(modified);
2398 return ret;
2399}
2400
2401/*
2402 * receive packets from upstream server and de-multiplex packets
2403 * to correct downstream:
2404 * implemented as a helper for channel input handlers,
2405 * replaces local (proxy) channel ID with downstream channel ID.
2406 */
2407int
2408channel_proxy_upstream(Channel *c, int type, u_int32_t seq, void *ctxt)
2409{
2410 struct ssh *ssh = active_state;
2411 struct sshbuf *b = NULL;
2412 Channel *downstream;
2413 const u_char *cp = NULL;
2414 size_t len;
2415 int r;
2416
2417 /*
2418 * When receiving packets from the peer we need to check whether we
2419 * need to forward the packets to the mux client. In this case we
2420 * restore the orignal channel id and keep track of CLOSE messages,
2421 * so we can cleanup the channel.
2422 */
2423 if (c == NULL || c->type != SSH_CHANNEL_MUX_PROXY)
2424 return 0;
2425 if ((downstream = c->mux_ctx) == NULL)
2426 return 0;
2427 switch (type) {
2428 case SSH2_MSG_CHANNEL_CLOSE:
2429 case SSH2_MSG_CHANNEL_DATA:
2430 case SSH2_MSG_CHANNEL_EOF:
2431 case SSH2_MSG_CHANNEL_EXTENDED_DATA:
2432 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2433 case SSH2_MSG_CHANNEL_OPEN_FAILURE:
2434 case SSH2_MSG_CHANNEL_WINDOW_ADJUST:
2435 case SSH2_MSG_CHANNEL_SUCCESS:
2436 case SSH2_MSG_CHANNEL_FAILURE:
2437 case SSH2_MSG_CHANNEL_REQUEST:
2438 break;
2439 default:
2440 debug2("%s: channel %u: unsupported type %u", __func__,
2441 c->self, type);
2442 return 0;
2443 }
2444 if ((b = sshbuf_new()) == NULL) {
2445 error("%s: alloc reply", __func__);
2446 goto out;
2447 }
2448 /* get remaining payload (after id) */
2449 cp = sshpkt_ptr(ssh, &len);
2450 if (cp == NULL) {
2451 error("%s: no packet", __func__);
2452 goto out;
2453 }
2454 /* translate id and send to muxclient */
2455 if ((r = sshbuf_put_u8(b, 0)) != 0 || /* padlen */
2456 (r = sshbuf_put_u8(b, type)) != 0 ||
2457 (r = sshbuf_put_u32(b, c->mux_downstream_id)) != 0 ||
2458 (r = sshbuf_put(b, cp, len)) != 0 ||
2459 (r = sshbuf_put_stringb(&downstream->output, b)) != 0) {
2460 error("%s: compose for muxclient %s", __func__, ssh_err(r));
2461 goto out;
2462 }
2463 /* sshbuf_dump(b, stderr); */
2464 if (ssh_packet_log_type(type))
2465 debug3("%s: channel %u: up->down: type %u", __func__, c->self,
2466 type);
2467 out:
2468 /* update state */
2469 switch (type) {
2470 case SSH2_MSG_CHANNEL_OPEN_CONFIRMATION:
2471 /* record remote_id for SSH2_MSG_CHANNEL_CLOSE */
2472 if (cp && len > 4)
2473 c->remote_id = PEEK_U32(cp);
2474 break;
2475 case SSH2_MSG_CHANNEL_CLOSE:
2476 if (c->flags & CHAN_CLOSE_SENT)
2477 channel_free(c);
2478 else
2479 c->flags |= CHAN_CLOSE_RCVD;
2480 break;
2481 }
2482 sshbuf_free(b);
2483 return 1;
2484}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002485
2486/* -- protocol input */
Damien Millerd79b4242006-03-31 23:11:44 +11002487
2488/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002489int
Damien Miller630d6f42002-01-22 23:17:30 +11002490channel_input_data(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002491{
Damien Miller34132e52000-01-14 15:45:46 +11002492 int id;
Damien Miller633de332014-05-15 13:48:26 +10002493 const u_char *data;
Damien Miller7d457182010-08-05 23:09:48 +10002494 u_int data_len, win_len;
Damien Millerb38eff82000-04-01 11:09:21 +10002495 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002496
Damien Miller95def091999-11-25 00:26:21 +11002497 /* Get the channel number and verify it. */
Damien Miller34132e52000-01-14 15:45:46 +11002498 id = packet_get_int();
Damien Millerb38eff82000-04-01 11:09:21 +10002499 c = channel_lookup(id);
2500 if (c == NULL)
Damien Miller34132e52000-01-14 15:45:46 +11002501 packet_disconnect("Received data for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002502 if (channel_proxy_upstream(c, type, seq, ctxt))
2503 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002504
Damien Miller95def091999-11-25 00:26:21 +11002505 /* Ignore any data for non-open channels (might happen on close) */
Damien Millerb38eff82000-04-01 11:09:21 +10002506 if (c->type != SSH_CHANNEL_OPEN &&
2507 c->type != SSH_CHANNEL_X11_OPEN)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002508 return 0;
Damien Miller34132e52000-01-14 15:45:46 +11002509
Damien Miller95def091999-11-25 00:26:21 +11002510 /* Get the data. */
Damien Millerdb255ca2008-05-19 14:59:37 +10002511 data = packet_get_string_ptr(&data_len);
Damien Miller7d457182010-08-05 23:09:48 +10002512 win_len = data_len;
2513 if (c->datagram)
2514 win_len += 4; /* string length header */
Damien Millerb38eff82000-04-01 11:09:21 +10002515
Damien Millera04ad492004-01-21 11:02:09 +11002516 /*
2517 * Ignore data for protocol > 1.3 if output end is no longer open.
2518 * For protocol 2 the sending side is reducing its window as it sends
2519 * data, so we must 'fake' consumption of the data in order to ensure
2520 * that window updates are sent back. Otherwise the connection might
2521 * deadlock.
2522 */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002523 if (c->ostate != CHAN_OUTPUT_OPEN) {
2524 c->local_window -= win_len;
2525 c->local_consumed += win_len;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002526 return 0;
Damien Millera04ad492004-01-21 11:02:09 +11002527 }
2528
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002529 if (win_len > c->local_maxpacket) {
2530 logit("channel %d: rcvd big packet %d, maxpack %d",
2531 c->self, win_len, c->local_maxpacket);
Damien Miller33b13562000-04-04 14:38:59 +10002532 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002533 if (win_len > c->local_window) {
2534 logit("channel %d: rcvd too much data %d, win %d",
2535 c->self, win_len, c->local_window);
2536 return 0;
2537 }
2538 c->local_window -= win_len;
2539
Damien Millerd27b9472005-12-13 19:29:02 +11002540 if (c->datagram)
2541 buffer_put_string(&c->output, data, data_len);
2542 else
2543 buffer_append(&c->output, data, data_len);
Damien Millerdb255ca2008-05-19 14:59:37 +10002544 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002545 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002546}
Ben Lindstrome9c99912001-06-09 00:41:05 +00002547
Damien Millerd79b4242006-03-31 23:11:44 +11002548/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002549int
Damien Miller630d6f42002-01-22 23:17:30 +11002550channel_input_extended_data(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002551{
2552 int id;
Damien Miller33b13562000-04-04 14:38:59 +10002553 char *data;
Ben Lindstromdaa21792002-06-25 23:15:30 +00002554 u_int data_len, tcode;
Damien Miller33b13562000-04-04 14:38:59 +10002555 Channel *c;
2556
2557 /* Get the channel number and verify it. */
2558 id = packet_get_int();
2559 c = channel_lookup(id);
2560
2561 if (c == NULL)
2562 packet_disconnect("Received extended_data for bad channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002563 if (channel_proxy_upstream(c, type, seq, ctxt))
2564 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002565 if (c->type != SSH_CHANNEL_OPEN) {
Damien Miller996acd22003-04-09 20:59:48 +10002566 logit("channel %d: ext data for non open", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002567 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002568 }
Ben Lindstromcf159442002-03-26 03:26:24 +00002569 if (c->flags & CHAN_EOF_RCVD) {
2570 if (datafellows & SSH_BUG_EXTEOF)
2571 debug("channel %d: accepting ext data after eof", id);
2572 else
2573 packet_disconnect("Received extended_data after EOF "
2574 "on channel %d.", id);
2575 }
Damien Miller33b13562000-04-04 14:38:59 +10002576 tcode = packet_get_int();
2577 if (c->efd == -1 ||
2578 c->extended_usage != CHAN_EXTENDED_WRITE ||
2579 tcode != SSH2_EXTENDED_DATA_STDERR) {
Damien Miller996acd22003-04-09 20:59:48 +10002580 logit("channel %d: bad ext data", c->self);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002581 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002582 }
2583 data = packet_get_string(&data_len);
Damien Miller48b03fc2002-01-22 23:11:40 +11002584 packet_check_eom();
Damien Miller33b13562000-04-04 14:38:59 +10002585 if (data_len > c->local_window) {
Damien Miller996acd22003-04-09 20:59:48 +10002586 logit("channel %d: rcvd too much extended_data %d, win %d",
Damien Miller33b13562000-04-04 14:38:59 +10002587 c->self, data_len, c->local_window);
Darren Tuckera627d422013-06-02 07:31:17 +10002588 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002589 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002590 }
Damien Millerd3444942000-09-30 14:20:03 +11002591 debug2("channel %d: rcvd ext data %d", c->self, data_len);
Damien Miller33b13562000-04-04 14:38:59 +10002592 c->local_window -= data_len;
2593 buffer_append(&c->extended, data, data_len);
Darren Tuckera627d422013-06-02 07:31:17 +10002594 free(data);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002595 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002596}
2597
Damien Millerd79b4242006-03-31 23:11:44 +11002598/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002599int
Damien Miller630d6f42002-01-22 23:17:30 +11002600channel_input_ieof(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002601{
2602 int id;
2603 Channel *c;
2604
Damien Millerb38eff82000-04-01 11:09:21 +10002605 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002606 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002607 c = channel_lookup(id);
2608 if (c == NULL)
2609 packet_disconnect("Received ieof for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002610 if (channel_proxy_upstream(c, type, seq, ctxt))
2611 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002612 chan_rcvd_ieof(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002613
2614 /* XXX force input close */
Damien Millera90fc082002-01-22 23:19:38 +11002615 if (c->force_drain && c->istate == CHAN_INPUT_OPEN) {
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002616 debug("channel %d: FORCE input drain", c->self);
2617 c->istate = CHAN_INPUT_WAIT_DRAIN;
Damien Miller73f10742002-01-22 23:34:52 +11002618 if (buffer_len(&c->input) == 0)
2619 chan_ibuf_empty(c);
Ben Lindstrom944c4f02001-09-18 05:51:13 +00002620 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002621 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002622}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002623
Damien Millerd79b4242006-03-31 23:11:44 +11002624/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002625int
Damien Miller630d6f42002-01-22 23:17:30 +11002626channel_input_close(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002627{
Damien Millerb38eff82000-04-01 11:09:21 +10002628 int id;
2629 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002630
Damien Millerb38eff82000-04-01 11:09:21 +10002631 id = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002632 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002633 c = channel_lookup(id);
2634 if (c == NULL)
2635 packet_disconnect("Received close for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002636 if (channel_proxy_upstream(c, type, seq, ctxt))
2637 return 0;
Damien Miller5428f641999-11-25 11:54:57 +11002638 /*
2639 * Send a confirmation that we have closed the channel and no more
2640 * data is coming for it.
2641 */
Damien Miller95def091999-11-25 00:26:21 +11002642 packet_start(SSH_MSG_CHANNEL_CLOSE_CONFIRMATION);
Damien Millerb38eff82000-04-01 11:09:21 +10002643 packet_put_int(c->remote_id);
Damien Miller95def091999-11-25 00:26:21 +11002644 packet_send();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002645
Damien Miller5428f641999-11-25 11:54:57 +11002646 /*
2647 * If the channel is in closed state, we have sent a close request,
2648 * and the other side will eventually respond with a confirmation.
2649 * Thus, we cannot free the channel here, because then there would be
2650 * no-one to receive the confirmation. The channel gets freed when
2651 * the confirmation arrives.
2652 */
Damien Millerb38eff82000-04-01 11:09:21 +10002653 if (c->type != SSH_CHANNEL_CLOSED) {
Damien Miller5428f641999-11-25 11:54:57 +11002654 /*
2655 * Not a closed channel - mark it as draining, which will
2656 * cause it to be freed later.
2657 */
Damien Miller76765c02002-01-22 23:21:15 +11002658 buffer_clear(&c->input);
Damien Millerb38eff82000-04-01 11:09:21 +10002659 c->type = SSH_CHANNEL_OUTPUT_DRAINING;
Damien Miller95def091999-11-25 00:26:21 +11002660 }
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002661 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002662}
2663
Damien Millerb38eff82000-04-01 11:09:21 +10002664/* proto version 1.5 overloads CLOSE_CONFIRMATION with OCLOSE */
Damien Millerd79b4242006-03-31 23:11:44 +11002665/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002666int
Damien Miller630d6f42002-01-22 23:17:30 +11002667channel_input_oclose(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002668{
Damien Millerb38eff82000-04-01 11:09:21 +10002669 int id = packet_get_int();
2670 Channel *c = channel_lookup(id);
Damien Miller66823cd2002-01-22 23:11:38 +11002671
Damien Millerb38eff82000-04-01 11:09:21 +10002672 if (c == NULL)
2673 packet_disconnect("Received oclose for nonexistent channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002674 if (channel_proxy_upstream(c, type, seq, ctxt))
2675 return 0;
2676 packet_check_eom();
Damien Millerb38eff82000-04-01 11:09:21 +10002677 chan_rcvd_oclose(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002678 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002679}
2680
Damien Millerd79b4242006-03-31 23:11:44 +11002681/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002682int
Damien Miller630d6f42002-01-22 23:17:30 +11002683channel_input_close_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerb38eff82000-04-01 11:09:21 +10002684{
2685 int id = packet_get_int();
2686 Channel *c = channel_lookup(id);
2687
2688 if (c == NULL)
2689 packet_disconnect("Received close confirmation for "
2690 "out-of-range channel %d.", id);
markus@openbsd.org8d057842016-09-30 09:19:13 +00002691 if (channel_proxy_upstream(c, type, seq, ctxt))
2692 return 0;
2693 packet_check_eom();
Damien Miller36187092013-06-10 13:07:11 +10002694 if (c->type != SSH_CHANNEL_CLOSED && c->type != SSH_CHANNEL_ABANDONED)
Damien Millerb38eff82000-04-01 11:09:21 +10002695 packet_disconnect("Received close confirmation for "
2696 "non-closed channel %d (type %d).", id, c->type);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002697 channel_free(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002698 return 0;
Damien Millerb38eff82000-04-01 11:09:21 +10002699}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002700
Damien Millerd79b4242006-03-31 23:11:44 +11002701/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002702int
Damien Miller630d6f42002-01-22 23:17:30 +11002703channel_input_open_confirmation(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002704{
Damien Millerb38eff82000-04-01 11:09:21 +10002705 int id, remote_id;
2706 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002707
Damien Millerb38eff82000-04-01 11:09:21 +10002708 id = packet_get_int();
2709 c = channel_lookup(id);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002710
markus@openbsd.org8d057842016-09-30 09:19:13 +00002711 if (c==NULL)
2712 packet_disconnect("Received open confirmation for "
2713 "unknown channel %d.", id);
2714 if (channel_proxy_upstream(c, type, seq, ctxt))
2715 return 0;
2716 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002717 packet_disconnect("Received open confirmation for "
2718 "non-opening channel %d.", id);
2719 remote_id = packet_get_int();
Damien Miller5428f641999-11-25 11:54:57 +11002720 /* Record the remote channel number and mark that the channel is now open. */
Damien Millerb38eff82000-04-01 11:09:21 +10002721 c->remote_id = remote_id;
2722 c->type = SSH_CHANNEL_OPEN;
Damien Miller33b13562000-04-04 14:38:59 +10002723
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002724 c->remote_window = packet_get_int();
2725 c->remote_maxpacket = packet_get_int();
2726 if (c->open_confirm) {
2727 debug2("callback start");
2728 c->open_confirm(c->self, 1, c->open_confirm_ctx);
2729 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002730 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002731 debug2("channel %d: open confirm rwindow %u rmax %u", c->self,
2732 c->remote_window, c->remote_maxpacket);
Damien Miller48b03fc2002-01-22 23:11:40 +11002733 packet_check_eom();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002734 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002735}
2736
Ben Lindstrombba81212001-06-25 05:01:22 +00002737static char *
Ben Lindstrom69128662001-05-08 20:07:39 +00002738reason2txt(int reason)
2739{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00002740 switch (reason) {
Ben Lindstrom69128662001-05-08 20:07:39 +00002741 case SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED:
2742 return "administratively prohibited";
2743 case SSH2_OPEN_CONNECT_FAILED:
2744 return "connect failed";
2745 case SSH2_OPEN_UNKNOWN_CHANNEL_TYPE:
2746 return "unknown channel type";
2747 case SSH2_OPEN_RESOURCE_SHORTAGE:
2748 return "resource shortage";
2749 }
Ben Lindstrome2595442001-06-05 20:01:39 +00002750 return "unknown reason";
Ben Lindstrom69128662001-05-08 20:07:39 +00002751}
2752
Damien Millerd79b4242006-03-31 23:11:44 +11002753/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002754int
Damien Miller630d6f42002-01-22 23:17:30 +11002755channel_input_open_failure(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002756{
Kevin Steves12057502001-02-05 14:54:34 +00002757 int id, reason;
2758 char *msg = NULL, *lang = NULL;
Damien Millerb38eff82000-04-01 11:09:21 +10002759 Channel *c;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002760
Damien Millerb38eff82000-04-01 11:09:21 +10002761 id = packet_get_int();
2762 c = channel_lookup(id);
2763
markus@openbsd.org8d057842016-09-30 09:19:13 +00002764 if (c==NULL)
2765 packet_disconnect("Received open failure for "
2766 "unknown channel %d.", id);
2767 if (channel_proxy_upstream(c, type, seq, ctxt))
2768 return 0;
2769 if (c->type != SSH_CHANNEL_OPENING)
Damien Millerb38eff82000-04-01 11:09:21 +10002770 packet_disconnect("Received open failure for "
2771 "non-opening channel %d.", id);
djm@openbsd.org97f4d302017-04-30 23:13:25 +00002772 reason = packet_get_int();
2773 if (!(datafellows & SSH_BUG_OPENFAILURE)) {
2774 msg = packet_get_string(NULL);
2775 lang = packet_get_string(NULL);
2776 }
2777 logit("channel %d: open failed: %s%s%s", id,
2778 reason2txt(reason), msg ? ": ": "", msg ? msg : "");
2779 free(msg);
2780 free(lang);
2781 if (c->open_confirm) {
2782 debug2("callback start");
2783 c->open_confirm(c->self, 0, c->open_confirm_ctx);
2784 debug2("callback done");
Damien Miller33b13562000-04-04 14:38:59 +10002785 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002786 packet_check_eom();
Damien Miller7a606212009-01-28 16:22:34 +11002787 /* Schedule the channel for cleanup/deletion. */
2788 chan_mark_dead(c);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002789 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002790}
2791
Damien Millerd79b4242006-03-31 23:11:44 +11002792/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002793int
Damien Miller630d6f42002-01-22 23:17:30 +11002794channel_input_window_adjust(int type, u_int32_t seq, void *ctxt)
Damien Miller33b13562000-04-04 14:38:59 +10002795{
2796 Channel *c;
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002797 int id;
djm@openbsd.org629df772015-06-30 05:25:07 +00002798 u_int adjust, tmp;
Damien Miller33b13562000-04-04 14:38:59 +10002799
Damien Miller33b13562000-04-04 14:38:59 +10002800 /* Get the channel number and verify it. */
2801 id = packet_get_int();
2802 c = channel_lookup(id);
2803
Damien Millerd47c62a2005-12-13 19:33:57 +11002804 if (c == NULL) {
2805 logit("Received window adjust for non-open channel %d.", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002806 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002807 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002808 if (channel_proxy_upstream(c, type, seq, ctxt))
2809 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002810 adjust = packet_get_int();
Damien Miller48b03fc2002-01-22 23:11:40 +11002811 packet_check_eom();
Ben Lindstrom4fed2be2002-06-25 23:17:36 +00002812 debug2("channel %d: rcvd adjust %u", id, adjust);
djm@openbsd.org629df772015-06-30 05:25:07 +00002813 if ((tmp = c->remote_window + adjust) < c->remote_window)
2814 fatal("channel %d: adjust %u overflows remote window %u",
2815 id, adjust, c->remote_window);
2816 c->remote_window = tmp;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002817 return 0;
Damien Miller33b13562000-04-04 14:38:59 +10002818}
2819
Damien Millerd79b4242006-03-31 23:11:44 +11002820/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002821int
Damien Miller630d6f42002-01-22 23:17:30 +11002822channel_input_port_open(int type, u_int32_t seq, void *ctxt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002823{
Ben Lindstrome9c99912001-06-09 00:41:05 +00002824 Channel *c = NULL;
2825 u_short host_port;
2826 char *host, *originator_string;
Damien Millerbd740252008-05-19 15:37:09 +10002827 int remote_id;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002828
Ben Lindstrome9c99912001-06-09 00:41:05 +00002829 remote_id = packet_get_int();
2830 host = packet_get_string(NULL);
2831 host_port = packet_get_int();
2832
2833 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
2834 originator_string = packet_get_string(NULL);
2835 } else {
2836 originator_string = xstrdup("unknown (remote did not supply name)");
2837 }
Damien Miller48b03fc2002-01-22 23:11:40 +11002838 packet_check_eom();
Damien Miller7acefbb2014-07-18 14:11:24 +10002839 c = channel_connect_to_port(host, host_port,
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00002840 "connected socket", originator_string, NULL, NULL);
Darren Tuckera627d422013-06-02 07:31:17 +10002841 free(originator_string);
2842 free(host);
Ben Lindstrome9c99912001-06-09 00:41:05 +00002843 if (c == NULL) {
2844 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
2845 packet_put_int(remote_id);
2846 packet_send();
Damien Millerbd740252008-05-19 15:37:09 +10002847 } else
2848 c->remote_id = remote_id;
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002849 return 0;
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002850}
2851
Damien Millerb84886b2008-05-19 15:05:07 +10002852/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002853int
Damien Millerb84886b2008-05-19 15:05:07 +10002854channel_input_status_confirm(int type, u_int32_t seq, void *ctxt)
2855{
2856 Channel *c;
2857 struct channel_confirm *cc;
Damien Miller16a73072008-12-08 09:55:25 +11002858 int id;
Damien Millerb84886b2008-05-19 15:05:07 +10002859
2860 /* Reset keepalive timeout */
Darren Tuckerf7288d72009-06-21 18:12:20 +10002861 packet_set_alive_timeouts(0);
Damien Millerb84886b2008-05-19 15:05:07 +10002862
Damien Miller16a73072008-12-08 09:55:25 +11002863 id = packet_get_int();
Damien Miller16a73072008-12-08 09:55:25 +11002864 debug2("channel_input_status_confirm: type %d id %d", type, id);
Damien Millerb84886b2008-05-19 15:05:07 +10002865
Damien Miller16a73072008-12-08 09:55:25 +11002866 if ((c = channel_lookup(id)) == NULL) {
2867 logit("channel_input_status_confirm: %d: unknown", id);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002868 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002869 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00002870 if (channel_proxy_upstream(c, type, seq, ctxt))
2871 return 0;
2872 packet_check_eom();
Damien Millerb84886b2008-05-19 15:05:07 +10002873 if ((cc = TAILQ_FIRST(&c->status_confirms)) == NULL)
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002874 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002875 cc->cb(type, c, cc->ctx);
2876 TAILQ_REMOVE(&c->status_confirms, cc, entry);
Damien Miller1d2c4562014-02-04 11:18:20 +11002877 explicit_bzero(cc, sizeof(*cc));
Darren Tuckera627d422013-06-02 07:31:17 +10002878 free(cc);
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00002879 return 0;
Damien Millerb84886b2008-05-19 15:05:07 +10002880}
Ben Lindstrom5744dc42001-04-13 23:28:01 +00002881
Ben Lindstrome9c99912001-06-09 00:41:05 +00002882/* -- tcp forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002883
Ben Lindstrom908afed2001-10-03 17:34:59 +00002884void
2885channel_set_af(int af)
2886{
2887 IPv4or6 = af;
2888}
2889
Damien Millerf6dff7c2011-09-22 21:38:52 +10002890
2891/*
2892 * Determine whether or not a port forward listens to loopback, the
2893 * specified address or wildcard. On the client, a specified bind
2894 * address will always override gateway_ports. On the server, a
2895 * gateway_ports of 1 (``yes'') will override the client's specification
2896 * and force a wildcard bind, whereas a value of 2 (``clientspecified'')
2897 * will bind to whatever address the client asked for.
2898 *
2899 * Special-case listen_addrs are:
2900 *
2901 * "0.0.0.0" -> wildcard v4/v6 if SSH_OLD_FORWARD_ADDR
2902 * "" (empty string), "*" -> wildcard v4/v6
2903 * "localhost" -> loopback v4/v6
Damien Miller602943d2014-07-04 08:59:41 +10002904 * "127.0.0.1" / "::1" -> accepted even if gateway_ports isn't set
Damien Millerf6dff7c2011-09-22 21:38:52 +10002905 */
2906static const char *
2907channel_fwd_bind_addr(const char *listen_addr, int *wildcardp,
Damien Miller7acefbb2014-07-18 14:11:24 +10002908 int is_client, struct ForwardOptions *fwd_opts)
Damien Millerf6dff7c2011-09-22 21:38:52 +10002909{
2910 const char *addr = NULL;
2911 int wildcard = 0;
2912
2913 if (listen_addr == NULL) {
2914 /* No address specified: default to gateway_ports setting */
Damien Miller7acefbb2014-07-18 14:11:24 +10002915 if (fwd_opts->gateway_ports)
Damien Millerf6dff7c2011-09-22 21:38:52 +10002916 wildcard = 1;
Damien Miller7acefbb2014-07-18 14:11:24 +10002917 } else if (fwd_opts->gateway_ports || is_client) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10002918 if (((datafellows & SSH_OLD_FORWARD_ADDR) &&
2919 strcmp(listen_addr, "0.0.0.0") == 0 && is_client == 0) ||
2920 *listen_addr == '\0' || strcmp(listen_addr, "*") == 0 ||
Damien Miller7acefbb2014-07-18 14:11:24 +10002921 (!is_client && fwd_opts->gateway_ports == 1)) {
Damien Millerf6dff7c2011-09-22 21:38:52 +10002922 wildcard = 1;
Darren Tucker71152bc2013-10-10 10:27:21 +11002923 /*
2924 * Notify client if they requested a specific listen
2925 * address and it was overridden.
2926 */
2927 if (*listen_addr != '\0' &&
2928 strcmp(listen_addr, "0.0.0.0") != 0 &&
2929 strcmp(listen_addr, "*") != 0) {
2930 packet_send_debug("Forwarding listen address "
2931 "\"%s\" overridden by server "
2932 "GatewayPorts", listen_addr);
2933 }
Damien Miller602943d2014-07-04 08:59:41 +10002934 } else if (strcmp(listen_addr, "localhost") != 0 ||
2935 strcmp(listen_addr, "127.0.0.1") == 0 ||
2936 strcmp(listen_addr, "::1") == 0) {
2937 /* Accept localhost address when GatewayPorts=yes */
Damien Millere84d1032014-05-21 17:13:36 +10002938 addr = listen_addr;
Damien Miller602943d2014-07-04 08:59:41 +10002939 }
2940 } else if (strcmp(listen_addr, "127.0.0.1") == 0 ||
2941 strcmp(listen_addr, "::1") == 0) {
2942 /*
2943 * If a specific IPv4/IPv6 localhost address has been
2944 * requested then accept it even if gateway_ports is in
2945 * effect. This allows the client to prefer IPv4 or IPv6.
2946 */
2947 addr = listen_addr;
Damien Millerf6dff7c2011-09-22 21:38:52 +10002948 }
2949 if (wildcardp != NULL)
2950 *wildcardp = wildcard;
2951 return addr;
2952}
2953
Damien Millerb16461c2002-01-22 23:29:22 +11002954static int
Damien Miller7acefbb2014-07-18 14:11:24 +10002955channel_setup_fwd_listener_tcpip(int type, struct Forward *fwd,
2956 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Miller0bc1bd82000-11-13 22:57:25 +11002957{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00002958 Channel *c;
Damien Miller5e7fd072005-11-05 14:53:39 +11002959 int sock, r, success = 0, wildcard = 0, is_client;
Damien Miller34132e52000-01-14 15:45:46 +11002960 struct addrinfo hints, *ai, *aitop;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002961 const char *host, *addr;
Damien Millerb16461c2002-01-22 23:29:22 +11002962 char ntop[NI_MAXHOST], strport[NI_MAXSERV];
Damien Miller4bf648f2009-02-14 16:28:21 +11002963 in_port_t *lport_p;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002964
Damien Millerf91ee4c2005-03-01 21:24:33 +11002965 is_client = (type == SSH_CHANNEL_PORT_LISTENER);
Kevin Steves12057502001-02-05 14:54:34 +00002966
millert@openbsd.orgec04dc42015-06-05 15:13:13 +00002967 if (is_client && fwd->connect_path != NULL) {
2968 host = fwd->connect_path;
2969 } else {
2970 host = (type == SSH_CHANNEL_RPORT_LISTENER) ?
2971 fwd->listen_host : fwd->connect_host;
2972 if (host == NULL) {
2973 error("No forward host name.");
2974 return 0;
2975 }
2976 if (strlen(host) >= NI_MAXHOST) {
2977 error("Forward host name too long.");
2978 return 0;
2979 }
Kevin Steves12057502001-02-05 14:54:34 +00002980 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002981
Damien Millerf6dff7c2011-09-22 21:38:52 +10002982 /* Determine the bind address, cf. channel_fwd_bind_addr() comment */
Damien Miller7acefbb2014-07-18 14:11:24 +10002983 addr = channel_fwd_bind_addr(fwd->listen_host, &wildcard,
2984 is_client, fwd_opts);
2985 debug3("%s: type %d wildcard %d addr %s", __func__,
Damien Millerf91ee4c2005-03-01 21:24:33 +11002986 type, wildcard, (addr == NULL) ? "NULL" : addr);
2987
2988 /*
Damien Miller34132e52000-01-14 15:45:46 +11002989 * getaddrinfo returns a loopback address if the hostname is
2990 * set to NULL and hints.ai_flags is not AI_PASSIVE
Damien Miller5428f641999-11-25 11:54:57 +11002991 */
Damien Miller34132e52000-01-14 15:45:46 +11002992 memset(&hints, 0, sizeof(hints));
2993 hints.ai_family = IPv4or6;
Damien Millerf91ee4c2005-03-01 21:24:33 +11002994 hints.ai_flags = wildcard ? AI_PASSIVE : 0;
Damien Miller34132e52000-01-14 15:45:46 +11002995 hints.ai_socktype = SOCK_STREAM;
Damien Miller7acefbb2014-07-18 14:11:24 +10002996 snprintf(strport, sizeof strport, "%d", fwd->listen_port);
Damien Millerf91ee4c2005-03-01 21:24:33 +11002997 if ((r = getaddrinfo(addr, strport, &hints, &aitop)) != 0) {
2998 if (addr == NULL) {
2999 /* This really shouldn't happen */
3000 packet_disconnect("getaddrinfo: fatal error: %s",
Darren Tucker4abde772007-12-29 02:43:51 +11003001 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003002 } else {
Damien Miller7acefbb2014-07-18 14:11:24 +10003003 error("%s: getaddrinfo(%.64s): %s", __func__, addr,
Darren Tucker4abde772007-12-29 02:43:51 +11003004 ssh_gai_strerror(r));
Damien Millerf91ee4c2005-03-01 21:24:33 +11003005 }
Damien Millera7270302005-07-06 09:36:05 +10003006 return 0;
Damien Millerf91ee4c2005-03-01 21:24:33 +11003007 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003008 if (allocated_listen_port != NULL)
3009 *allocated_listen_port = 0;
Damien Miller34132e52000-01-14 15:45:46 +11003010 for (ai = aitop; ai; ai = ai->ai_next) {
Damien Miller4bf648f2009-02-14 16:28:21 +11003011 switch (ai->ai_family) {
3012 case AF_INET:
3013 lport_p = &((struct sockaddr_in *)ai->ai_addr)->
3014 sin_port;
3015 break;
3016 case AF_INET6:
3017 lport_p = &((struct sockaddr_in6 *)ai->ai_addr)->
3018 sin6_port;
3019 break;
3020 default:
Damien Miller34132e52000-01-14 15:45:46 +11003021 continue;
Damien Miller4bf648f2009-02-14 16:28:21 +11003022 }
3023 /*
3024 * If allocating a port for -R forwards, then use the
3025 * same port for all address families.
3026 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003027 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11003028 allocated_listen_port != NULL && *allocated_listen_port > 0)
3029 *lport_p = htons(*allocated_listen_port);
3030
Damien Miller34132e52000-01-14 15:45:46 +11003031 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, ntop, sizeof(ntop),
3032 strport, sizeof(strport), NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003033 error("%s: getnameinfo failed", __func__);
Damien Miller34132e52000-01-14 15:45:46 +11003034 continue;
3035 }
3036 /* Create a port to listen for the host. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11003037 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11003038 if (sock < 0) {
3039 /* this is no error since kernel may not support ipv6 */
3040 verbose("socket: %.100s", strerror(errno));
3041 continue;
3042 }
Damien Miller5e7fd072005-11-05 14:53:39 +11003043
3044 channel_set_reuseaddr(sock);
Damien Miller04ee0f82009-11-18 17:48:30 +11003045 if (ai->ai_family == AF_INET6)
3046 sock_set_v6only(sock);
Damien Millere1383ce2002-09-19 11:49:37 +10003047
Damien Miller4bf648f2009-02-14 16:28:21 +11003048 debug("Local forwarding listening on %s port %s.",
3049 ntop, strport);
Damien Miller95def091999-11-25 00:26:21 +11003050
Damien Miller34132e52000-01-14 15:45:46 +11003051 /* Bind the socket to the address. */
3052 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
3053 /* address can be in use ipv6 address is already bound */
Damien Miller3c7eeb22000-03-03 22:35:33 +11003054 if (!ai->ai_next)
3055 error("bind: %.100s", strerror(errno));
3056 else
3057 verbose("bind: %.100s", strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +00003058
Damien Miller34132e52000-01-14 15:45:46 +11003059 close(sock);
3060 continue;
3061 }
3062 /* Start listening for connections on the socket. */
Darren Tucker3175eb92003-12-09 19:15:11 +11003063 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11003064 error("listen: %.100s", strerror(errno));
3065 close(sock);
3066 continue;
3067 }
Damien Miller4bf648f2009-02-14 16:28:21 +11003068
3069 /*
Damien Miller7acefbb2014-07-18 14:11:24 +10003070 * fwd->listen_port == 0 requests a dynamically allocated port -
Damien Miller4bf648f2009-02-14 16:28:21 +11003071 * record what we got.
3072 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003073 if (type == SSH_CHANNEL_RPORT_LISTENER && fwd->listen_port == 0 &&
Damien Miller4bf648f2009-02-14 16:28:21 +11003074 allocated_listen_port != NULL &&
3075 *allocated_listen_port == 0) {
djm@openbsd.org95767262016-03-07 19:02:43 +00003076 *allocated_listen_port = get_local_port(sock);
Damien Miller4bf648f2009-02-14 16:28:21 +11003077 debug("Allocated listen port %d",
3078 *allocated_listen_port);
3079 }
3080
Damien Miller34132e52000-01-14 15:45:46 +11003081 /* Allocate a channel number for the socket. */
Ben Lindstrome9c99912001-06-09 00:41:05 +00003082 c = channel_new("port listener", type, sock, sock, -1,
Damien Millerbd483e72000-04-30 10:00:53 +10003083 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10003084 0, "port listener", 1);
Damien Millera1c1b6c2009-01-28 16:29:49 +11003085 c->path = xstrdup(host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003086 c->host_port = fwd->connect_port;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003087 c->listening_addr = addr == NULL ? NULL : xstrdup(addr);
Damien Miller7acefbb2014-07-18 14:11:24 +10003088 if (fwd->listen_port == 0 && allocated_listen_port != NULL &&
Darren Tucker68afb8c2011-10-02 18:59:03 +11003089 !(datafellows & SSH_BUG_DYNAMIC_RPORT))
3090 c->listening_port = *allocated_listen_port;
3091 else
Damien Miller7acefbb2014-07-18 14:11:24 +10003092 c->listening_port = fwd->listen_port;
Damien Miller34132e52000-01-14 15:45:46 +11003093 success = 1;
3094 }
3095 if (success == 0)
Damien Miller7acefbb2014-07-18 14:11:24 +10003096 error("%s: cannot listen to port: %d", __func__,
3097 fwd->listen_port);
Damien Miller34132e52000-01-14 15:45:46 +11003098 freeaddrinfo(aitop);
Kevin Steves12057502001-02-05 14:54:34 +00003099 return success;
Damien Miller95def091999-11-25 00:26:21 +11003100}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003101
Damien Miller7acefbb2014-07-18 14:11:24 +10003102static int
3103channel_setup_fwd_listener_streamlocal(int type, struct Forward *fwd,
3104 struct ForwardOptions *fwd_opts)
3105{
3106 struct sockaddr_un sunaddr;
3107 const char *path;
3108 Channel *c;
3109 int port, sock;
3110 mode_t omask;
3111
3112 switch (type) {
3113 case SSH_CHANNEL_UNIX_LISTENER:
3114 if (fwd->connect_path != NULL) {
3115 if (strlen(fwd->connect_path) > sizeof(sunaddr.sun_path)) {
3116 error("Local connecting path too long: %s",
3117 fwd->connect_path);
3118 return 0;
3119 }
3120 path = fwd->connect_path;
3121 port = PORT_STREAMLOCAL;
3122 } else {
3123 if (fwd->connect_host == NULL) {
3124 error("No forward host name.");
3125 return 0;
3126 }
3127 if (strlen(fwd->connect_host) >= NI_MAXHOST) {
3128 error("Forward host name too long.");
3129 return 0;
3130 }
3131 path = fwd->connect_host;
3132 port = fwd->connect_port;
3133 }
3134 break;
3135 case SSH_CHANNEL_RUNIX_LISTENER:
3136 path = fwd->listen_path;
3137 port = PORT_STREAMLOCAL;
3138 break;
3139 default:
3140 error("%s: unexpected channel type %d", __func__, type);
3141 return 0;
3142 }
3143
3144 if (fwd->listen_path == NULL) {
3145 error("No forward path name.");
3146 return 0;
3147 }
3148 if (strlen(fwd->listen_path) > sizeof(sunaddr.sun_path)) {
3149 error("Local listening path too long: %s", fwd->listen_path);
3150 return 0;
3151 }
3152
3153 debug3("%s: type %d path %s", __func__, type, fwd->listen_path);
3154
3155 /* Start a Unix domain listener. */
3156 omask = umask(fwd_opts->streamlocal_bind_mask);
3157 sock = unix_listener(fwd->listen_path, SSH_LISTEN_BACKLOG,
3158 fwd_opts->streamlocal_bind_unlink);
3159 umask(omask);
3160 if (sock < 0)
3161 return 0;
3162
3163 debug("Local forwarding listening on path %s.", fwd->listen_path);
3164
3165 /* Allocate a channel number for the socket. */
3166 c = channel_new("unix listener", type, sock, sock, -1,
3167 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT,
3168 0, "unix listener", 1);
3169 c->path = xstrdup(path);
3170 c->host_port = port;
3171 c->listening_port = PORT_STREAMLOCAL;
3172 c->listening_addr = xstrdup(fwd->listen_path);
3173 return 1;
3174}
3175
3176static int
3177channel_cancel_rport_listener_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003178{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003179 u_int i;
3180 int found = 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003181
Darren Tucker47eede72005-03-14 23:08:12 +11003182 for (i = 0; i < channels_alloc; i++) {
Darren Tuckere7066df2004-05-24 10:18:05 +10003183 Channel *c = channels[i];
Damien Millerf6dff7c2011-09-22 21:38:52 +10003184 if (c == NULL || c->type != SSH_CHANNEL_RPORT_LISTENER)
3185 continue;
3186 if (strcmp(c->path, host) == 0 && c->listening_port == port) {
3187 debug2("%s: close channel %d", __func__, i);
3188 channel_free(c);
3189 found = 1;
3190 }
3191 }
Darren Tuckere7066df2004-05-24 10:18:05 +10003192
Damien Millerf6dff7c2011-09-22 21:38:52 +10003193 return (found);
3194}
3195
Damien Miller7acefbb2014-07-18 14:11:24 +10003196static int
3197channel_cancel_rport_listener_streamlocal(const char *path)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003198{
3199 u_int i;
3200 int found = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003201
3202 for (i = 0; i < channels_alloc; i++) {
3203 Channel *c = channels[i];
3204 if (c == NULL || c->type != SSH_CHANNEL_RUNIX_LISTENER)
3205 continue;
3206 if (c->path == NULL)
3207 continue;
3208 if (strcmp(c->path, path) == 0) {
3209 debug2("%s: close channel %d", __func__, i);
3210 channel_free(c);
3211 found = 1;
3212 }
3213 }
3214
3215 return (found);
3216}
3217
3218int
3219channel_cancel_rport_listener(struct Forward *fwd)
3220{
3221 if (fwd->listen_path != NULL)
3222 return channel_cancel_rport_listener_streamlocal(fwd->listen_path);
3223 else
3224 return channel_cancel_rport_listener_tcpip(fwd->listen_host, fwd->listen_port);
3225}
3226
3227static int
3228channel_cancel_lport_listener_tcpip(const char *lhost, u_short lport,
3229 int cport, struct ForwardOptions *fwd_opts)
3230{
3231 u_int i;
3232 int found = 0;
3233 const char *addr = channel_fwd_bind_addr(lhost, NULL, 1, fwd_opts);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003234
3235 for (i = 0; i < channels_alloc; i++) {
3236 Channel *c = channels[i];
3237 if (c == NULL || c->type != SSH_CHANNEL_PORT_LISTENER)
3238 continue;
Damien Millerff773642011-09-22 21:39:48 +10003239 if (c->listening_port != lport)
Damien Millerf6dff7c2011-09-22 21:38:52 +10003240 continue;
Damien Millerff773642011-09-22 21:39:48 +10003241 if (cport == CHANNEL_CANCEL_PORT_STATIC) {
3242 /* skip dynamic forwardings */
3243 if (c->host_port == 0)
3244 continue;
3245 } else {
3246 if (c->host_port != cport)
3247 continue;
3248 }
Damien Millerf6dff7c2011-09-22 21:38:52 +10003249 if ((c->listening_addr == NULL && addr != NULL) ||
3250 (c->listening_addr != NULL && addr == NULL))
3251 continue;
3252 if (addr == NULL || strcmp(c->listening_addr, addr) == 0) {
Darren Tuckere6ed8392004-08-29 16:29:44 +10003253 debug2("%s: close channel %d", __func__, i);
Darren Tuckere7066df2004-05-24 10:18:05 +10003254 channel_free(c);
3255 found = 1;
3256 }
3257 }
3258
3259 return (found);
3260}
3261
Damien Miller7acefbb2014-07-18 14:11:24 +10003262static int
3263channel_cancel_lport_listener_streamlocal(const char *path)
3264{
3265 u_int i;
3266 int found = 0;
3267
3268 if (path == NULL) {
3269 error("%s: no path specified.", __func__);
3270 return 0;
3271 }
3272
3273 for (i = 0; i < channels_alloc; i++) {
3274 Channel *c = channels[i];
3275 if (c == NULL || c->type != SSH_CHANNEL_UNIX_LISTENER)
3276 continue;
3277 if (c->listening_addr == NULL)
3278 continue;
3279 if (strcmp(c->listening_addr, path) == 0) {
3280 debug2("%s: close channel %d", __func__, i);
3281 channel_free(c);
3282 found = 1;
3283 }
3284 }
3285
3286 return (found);
3287}
3288
3289int
3290channel_cancel_lport_listener(struct Forward *fwd, int cport, struct ForwardOptions *fwd_opts)
3291{
3292 if (fwd->listen_path != NULL)
3293 return channel_cancel_lport_listener_streamlocal(fwd->listen_path);
3294 else
3295 return channel_cancel_lport_listener_tcpip(fwd->listen_host, fwd->listen_port, cport, fwd_opts);
3296}
3297
Damien Millerb16461c2002-01-22 23:29:22 +11003298/* protocol local port fwd, used by ssh (and sshd in v1) */
3299int
Damien Miller7acefbb2014-07-18 14:11:24 +10003300channel_setup_local_fwd_listener(struct Forward *fwd, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003301{
Damien Miller7acefbb2014-07-18 14:11:24 +10003302 if (fwd->listen_path != NULL) {
3303 return channel_setup_fwd_listener_streamlocal(
3304 SSH_CHANNEL_UNIX_LISTENER, fwd, fwd_opts);
3305 } else {
3306 return channel_setup_fwd_listener_tcpip(SSH_CHANNEL_PORT_LISTENER,
3307 fwd, NULL, fwd_opts);
3308 }
Damien Millerb16461c2002-01-22 23:29:22 +11003309}
3310
3311/* protocol v2 remote port fwd, used by sshd */
3312int
Damien Miller7acefbb2014-07-18 14:11:24 +10003313channel_setup_remote_fwd_listener(struct Forward *fwd,
3314 int *allocated_listen_port, struct ForwardOptions *fwd_opts)
Damien Millerb16461c2002-01-22 23:29:22 +11003315{
Damien Miller7acefbb2014-07-18 14:11:24 +10003316 if (fwd->listen_path != NULL) {
3317 return channel_setup_fwd_listener_streamlocal(
3318 SSH_CHANNEL_RUNIX_LISTENER, fwd, fwd_opts);
3319 } else {
3320 return channel_setup_fwd_listener_tcpip(
3321 SSH_CHANNEL_RPORT_LISTENER, fwd, allocated_listen_port,
3322 fwd_opts);
3323 }
Damien Millerb16461c2002-01-22 23:29:22 +11003324}
3325
Damien Miller5428f641999-11-25 11:54:57 +11003326/*
Damien Millerf6dff7c2011-09-22 21:38:52 +10003327 * Translate the requested rfwd listen host to something usable for
3328 * this server.
3329 */
3330static const char *
3331channel_rfwd_bind_host(const char *listen_host)
3332{
3333 if (listen_host == NULL) {
3334 if (datafellows & SSH_BUG_RFWD_ADDR)
3335 return "127.0.0.1";
3336 else
3337 return "localhost";
3338 } else if (*listen_host == '\0' || strcmp(listen_host, "*") == 0) {
3339 if (datafellows & SSH_BUG_RFWD_ADDR)
3340 return "0.0.0.0";
3341 else
3342 return "";
3343 } else
3344 return listen_host;
3345}
3346
3347/*
Damien Miller5428f641999-11-25 11:54:57 +11003348 * Initiate forwarding of connections to port "port" on remote host through
3349 * the secure channel to host:port from local side.
Darren Tucker68afb8c2011-10-02 18:59:03 +11003350 * Returns handle (index) for updating the dynamic listen port with
3351 * channel_update_permitted_opens().
Damien Miller5428f641999-11-25 11:54:57 +11003352 */
Darren Tuckere7d4b192006-07-12 22:17:10 +10003353int
Damien Miller7acefbb2014-07-18 14:11:24 +10003354channel_request_remote_forwarding(struct Forward *fwd)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003355{
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003356 int success = 0, idx = -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003357
Damien Miller95def091999-11-25 00:26:21 +11003358 /* Send the forward request to the remote side. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003359 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3360 if (fwd->listen_path != NULL) {
3361 packet_put_cstring("streamlocal-forward@openssh.com");
3362 packet_put_char(1); /* boolean: want reply */
3363 packet_put_cstring(fwd->listen_path);
Damien Miller7acefbb2014-07-18 14:11:24 +10003364 } else {
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003365 packet_put_cstring("tcpip-forward");
3366 packet_put_char(1); /* boolean: want reply */
3367 packet_put_cstring(channel_rfwd_bind_host(fwd->listen_host));
3368 packet_put_int(fwd->listen_port);
Damien Miller0bc1bd82000-11-13 22:57:25 +11003369 }
djm@openbsd.org97f4d302017-04-30 23:13:25 +00003370 packet_send();
3371 packet_write_wait();
3372 /* Assume that server accepts the request */
3373 success = 1;
Damien Miller0bc1bd82000-11-13 22:57:25 +11003374 if (success) {
Damien Miller232cfb12010-06-26 09:50:30 +10003375 /* Record that connection to this host/port is permitted. */
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003376 permitted_opens = xreallocarray(permitted_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003377 num_permitted_opens + 1, sizeof(*permitted_opens));
Darren Tucker68afb8c2011-10-02 18:59:03 +11003378 idx = num_permitted_opens++;
Damien Miller7acefbb2014-07-18 14:11:24 +10003379 if (fwd->connect_path != NULL) {
3380 permitted_opens[idx].host_to_connect =
3381 xstrdup(fwd->connect_path);
3382 permitted_opens[idx].port_to_connect =
3383 PORT_STREAMLOCAL;
3384 } else {
3385 permitted_opens[idx].host_to_connect =
3386 xstrdup(fwd->connect_host);
3387 permitted_opens[idx].port_to_connect =
3388 fwd->connect_port;
3389 }
3390 if (fwd->listen_path != NULL) {
3391 permitted_opens[idx].listen_host = NULL;
3392 permitted_opens[idx].listen_path =
3393 xstrdup(fwd->listen_path);
3394 permitted_opens[idx].listen_port = PORT_STREAMLOCAL;
3395 } else {
3396 permitted_opens[idx].listen_host =
3397 fwd->listen_host ? xstrdup(fwd->listen_host) : NULL;
3398 permitted_opens[idx].listen_path = NULL;
3399 permitted_opens[idx].listen_port = fwd->listen_port;
3400 }
markus@openbsd.org8d057842016-09-30 09:19:13 +00003401 permitted_opens[idx].downstream = NULL;
Damien Miller33b13562000-04-04 14:38:59 +10003402 }
Darren Tucker68afb8c2011-10-02 18:59:03 +11003403 return (idx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003404}
3405
Damien Miller4b3ed642014-07-02 15:29:40 +10003406static int
3407open_match(ForwardPermission *allowed_open, const char *requestedhost,
Damien Miller7acefbb2014-07-18 14:11:24 +10003408 int requestedport)
Damien Miller4b3ed642014-07-02 15:29:40 +10003409{
3410 if (allowed_open->host_to_connect == NULL)
3411 return 0;
3412 if (allowed_open->port_to_connect != FWD_PERMIT_ANY_PORT &&
3413 allowed_open->port_to_connect != requestedport)
3414 return 0;
dtucker@openbsd.orgd7eabc82016-07-19 11:38:53 +00003415 if (strcmp(allowed_open->host_to_connect, FWD_PERMIT_ANY_HOST) != 0 &&
3416 strcmp(allowed_open->host_to_connect, requestedhost) != 0)
Damien Miller4b3ed642014-07-02 15:29:40 +10003417 return 0;
3418 return 1;
3419}
3420
3421/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003422 * Note that in the listen host/port case
Damien Miller4b3ed642014-07-02 15:29:40 +10003423 * we don't support FWD_PERMIT_ANY_PORT and
3424 * need to translate between the configured-host (listen_host)
3425 * and what we've sent to the remote server (channel_rfwd_bind_host)
3426 */
3427static int
Damien Miller7acefbb2014-07-18 14:11:24 +10003428open_listen_match_tcpip(ForwardPermission *allowed_open,
3429 const char *requestedhost, u_short requestedport, int translate)
Damien Miller4b3ed642014-07-02 15:29:40 +10003430{
3431 const char *allowed_host;
3432
3433 if (allowed_open->host_to_connect == NULL)
3434 return 0;
3435 if (allowed_open->listen_port != requestedport)
3436 return 0;
Damien Miller3a48cc02014-07-06 09:32:49 +10003437 if (!translate && allowed_open->listen_host == NULL &&
3438 requestedhost == NULL)
3439 return 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003440 allowed_host = translate ?
3441 channel_rfwd_bind_host(allowed_open->listen_host) :
3442 allowed_open->listen_host;
3443 if (allowed_host == NULL ||
3444 strcmp(allowed_host, requestedhost) != 0)
3445 return 0;
3446 return 1;
3447}
3448
Damien Miller7acefbb2014-07-18 14:11:24 +10003449static int
3450open_listen_match_streamlocal(ForwardPermission *allowed_open,
3451 const char *requestedpath)
3452{
3453 if (allowed_open->host_to_connect == NULL)
3454 return 0;
3455 if (allowed_open->listen_port != PORT_STREAMLOCAL)
3456 return 0;
3457 if (allowed_open->listen_path == NULL ||
3458 strcmp(allowed_open->listen_path, requestedpath) != 0)
3459 return 0;
3460 return 1;
3461}
3462
Damien Miller5428f641999-11-25 11:54:57 +11003463/*
Darren Tuckerfc959702004-07-17 16:12:08 +10003464 * Request cancellation of remote forwarding of connection host:port from
Darren Tuckere7066df2004-05-24 10:18:05 +10003465 * local side.
3466 */
Damien Miller7acefbb2014-07-18 14:11:24 +10003467static int
3468channel_request_rforward_cancel_tcpip(const char *host, u_short port)
Darren Tuckere7066df2004-05-24 10:18:05 +10003469{
3470 int i;
Darren Tuckere7066df2004-05-24 10:18:05 +10003471
Darren Tuckere7066df2004-05-24 10:18:05 +10003472 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003473 if (open_listen_match_tcpip(&permitted_opens[i], host, port, 0))
Darren Tuckere7066df2004-05-24 10:18:05 +10003474 break;
3475 }
3476 if (i >= num_permitted_opens) {
3477 debug("%s: requested forward not found", __func__);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003478 return -1;
Darren Tuckere7066df2004-05-24 10:18:05 +10003479 }
3480 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3481 packet_put_cstring("cancel-tcpip-forward");
3482 packet_put_char(0);
Damien Millerf6dff7c2011-09-22 21:38:52 +10003483 packet_put_cstring(channel_rfwd_bind_host(host));
Darren Tuckere7066df2004-05-24 10:18:05 +10003484 packet_put_int(port);
3485 packet_send();
3486
Damien Miller4b3ed642014-07-02 15:29:40 +10003487 permitted_opens[i].listen_port = 0;
Damien Miller7acefbb2014-07-18 14:11:24 +10003488 permitted_opens[i].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003489 free(permitted_opens[i].host_to_connect);
Darren Tuckere7066df2004-05-24 10:18:05 +10003490 permitted_opens[i].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003491 free(permitted_opens[i].listen_host);
3492 permitted_opens[i].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003493 permitted_opens[i].listen_path = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003494 permitted_opens[i].downstream = NULL;
Damien Millerf6dff7c2011-09-22 21:38:52 +10003495
3496 return 0;
Darren Tuckere7066df2004-05-24 10:18:05 +10003497}
3498
3499/*
Damien Miller7acefbb2014-07-18 14:11:24 +10003500 * Request cancellation of remote forwarding of Unix domain socket
3501 * path from local side.
3502 */
3503static int
3504channel_request_rforward_cancel_streamlocal(const char *path)
3505{
3506 int i;
3507
Damien Miller7acefbb2014-07-18 14:11:24 +10003508 for (i = 0; i < num_permitted_opens; i++) {
3509 if (open_listen_match_streamlocal(&permitted_opens[i], path))
3510 break;
3511 }
3512 if (i >= num_permitted_opens) {
3513 debug("%s: requested forward not found", __func__);
3514 return -1;
3515 }
3516 packet_start(SSH2_MSG_GLOBAL_REQUEST);
3517 packet_put_cstring("cancel-streamlocal-forward@openssh.com");
3518 packet_put_char(0);
3519 packet_put_cstring(path);
3520 packet_send();
3521
3522 permitted_opens[i].listen_port = 0;
3523 permitted_opens[i].port_to_connect = 0;
3524 free(permitted_opens[i].host_to_connect);
3525 permitted_opens[i].host_to_connect = NULL;
3526 permitted_opens[i].listen_host = NULL;
3527 free(permitted_opens[i].listen_path);
3528 permitted_opens[i].listen_path = NULL;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003529 permitted_opens[i].downstream = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003530
3531 return 0;
3532}
3533
3534/*
3535 * Request cancellation of remote forwarding of a connection from local side.
3536 */
3537int
3538channel_request_rforward_cancel(struct Forward *fwd)
3539{
3540 if (fwd->listen_path != NULL) {
3541 return (channel_request_rforward_cancel_streamlocal(
3542 fwd->listen_path));
3543 } else {
3544 return (channel_request_rforward_cancel_tcpip(fwd->listen_host,
3545 fwd->listen_port ? fwd->listen_port : fwd->allocated_port));
3546 }
3547}
3548
3549/*
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003550 * Permits opening to any host/port if permitted_opens[] is empty. This is
3551 * usually called by the server, because the user could connect to any port
3552 * anyway, and the server has no way to know but to trust the client anyway.
3553 */
3554void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00003555channel_permit_all_opens(void)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003556{
3557 if (num_permitted_opens == 0)
3558 all_opens_permitted = 1;
3559}
3560
Ben Lindstroma3700052001-04-05 23:26:32 +00003561void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003562channel_add_permitted_opens(char *host, int port)
3563{
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003564 debug("allow port forwarding to host %s port %d", host, port);
3565
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003566 permitted_opens = xreallocarray(permitted_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003567 num_permitted_opens + 1, sizeof(*permitted_opens));
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003568 permitted_opens[num_permitted_opens].host_to_connect = xstrdup(host);
3569 permitted_opens[num_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003570 permitted_opens[num_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003571 permitted_opens[num_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003572 permitted_opens[num_permitted_opens].listen_port = 0;
markus@openbsd.org8d057842016-09-30 09:19:13 +00003573 permitted_opens[num_permitted_opens].downstream = NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003574 num_permitted_opens++;
3575
3576 all_opens_permitted = 0;
3577}
3578
Darren Tucker68afb8c2011-10-02 18:59:03 +11003579/*
3580 * Update the listen port for a dynamic remote forward, after
3581 * the actual 'newport' has been allocated. If 'newport' < 0 is
3582 * passed then they entry will be invalidated.
3583 */
3584void
3585channel_update_permitted_opens(int idx, int newport)
3586{
3587 if (idx < 0 || idx >= num_permitted_opens) {
3588 debug("channel_update_permitted_opens: index out of range:"
3589 " %d num_permitted_opens %d", idx, num_permitted_opens);
3590 return;
3591 }
3592 debug("%s allowed port %d for forwarding to host %s port %d",
3593 newport > 0 ? "Updating" : "Removing",
3594 newport,
3595 permitted_opens[idx].host_to_connect,
3596 permitted_opens[idx].port_to_connect);
3597 if (newport >= 0) {
3598 permitted_opens[idx].listen_port =
3599 (datafellows & SSH_BUG_DYNAMIC_RPORT) ? 0 : newport;
3600 } else {
3601 permitted_opens[idx].listen_port = 0;
3602 permitted_opens[idx].port_to_connect = 0;
Darren Tuckera627d422013-06-02 07:31:17 +10003603 free(permitted_opens[idx].host_to_connect);
Darren Tucker68afb8c2011-10-02 18:59:03 +11003604 permitted_opens[idx].host_to_connect = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003605 free(permitted_opens[idx].listen_host);
3606 permitted_opens[idx].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003607 free(permitted_opens[idx].listen_path);
3608 permitted_opens[idx].listen_path = NULL;
Darren Tucker68afb8c2011-10-02 18:59:03 +11003609 }
3610}
3611
Damien Millera765cf42006-07-24 14:08:13 +10003612int
Damien Miller9b439df2006-07-24 14:04:00 +10003613channel_add_adm_permitted_opens(char *host, int port)
3614{
Damien Millera765cf42006-07-24 14:08:13 +10003615 debug("config allows port forwarding to host %s port %d", host, port);
Damien Miller9b439df2006-07-24 14:04:00 +10003616
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00003617 permitted_adm_opens = xreallocarray(permitted_adm_opens,
Damien Miller232cfb12010-06-26 09:50:30 +10003618 num_adm_permitted_opens + 1, sizeof(*permitted_adm_opens));
Damien Miller9b439df2006-07-24 14:04:00 +10003619 permitted_adm_opens[num_adm_permitted_opens].host_to_connect
3620 = xstrdup(host);
3621 permitted_adm_opens[num_adm_permitted_opens].port_to_connect = port;
Damien Miller4b3ed642014-07-02 15:29:40 +10003622 permitted_adm_opens[num_adm_permitted_opens].listen_host = NULL;
Damien Miller7acefbb2014-07-18 14:11:24 +10003623 permitted_adm_opens[num_adm_permitted_opens].listen_path = NULL;
Damien Miller4b3ed642014-07-02 15:29:40 +10003624 permitted_adm_opens[num_adm_permitted_opens].listen_port = 0;
Damien Millera765cf42006-07-24 14:08:13 +10003625 return ++num_adm_permitted_opens;
Damien Miller9b439df2006-07-24 14:04:00 +10003626}
3627
3628void
Damien Millerc6081482012-04-22 11:18:53 +10003629channel_disable_adm_local_opens(void)
3630{
Damien Milleraa5b3f82012-12-03 09:50:54 +11003631 channel_clear_adm_permitted_opens();
dtucker@openbsd.org297060f2015-05-08 03:25:07 +00003632 permitted_adm_opens = xcalloc(sizeof(*permitted_adm_opens), 1);
Damien Milleraa5b3f82012-12-03 09:50:54 +11003633 permitted_adm_opens[num_adm_permitted_opens].host_to_connect = NULL;
3634 num_adm_permitted_opens = 1;
Damien Millerc6081482012-04-22 11:18:53 +10003635}
3636
3637void
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003638channel_clear_permitted_opens(void)
3639{
3640 int i;
3641
Damien Miller4b3ed642014-07-02 15:29:40 +10003642 for (i = 0; i < num_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003643 free(permitted_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003644 free(permitted_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003645 free(permitted_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003646 }
Darren Tuckera627d422013-06-02 07:31:17 +10003647 free(permitted_opens);
3648 permitted_opens = NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003649 num_permitted_opens = 0;
Damien Miller9b439df2006-07-24 14:04:00 +10003650}
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003651
Damien Miller9b439df2006-07-24 14:04:00 +10003652void
3653channel_clear_adm_permitted_opens(void)
3654{
3655 int i;
3656
Damien Miller4b3ed642014-07-02 15:29:40 +10003657 for (i = 0; i < num_adm_permitted_opens; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +10003658 free(permitted_adm_opens[i].host_to_connect);
Damien Miller4b3ed642014-07-02 15:29:40 +10003659 free(permitted_adm_opens[i].listen_host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003660 free(permitted_adm_opens[i].listen_path);
Damien Miller4b3ed642014-07-02 15:29:40 +10003661 }
Darren Tuckera627d422013-06-02 07:31:17 +10003662 free(permitted_adm_opens);
3663 permitted_adm_opens = NULL;
Damien Miller9b439df2006-07-24 14:04:00 +10003664 num_adm_permitted_opens = 0;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003665}
3666
Darren Tuckere7140f22008-06-10 23:01:51 +10003667void
3668channel_print_adm_permitted_opens(void)
3669{
Damien Miller6ef17492008-07-16 22:42:06 +10003670 int i;
Darren Tuckere7140f22008-06-10 23:01:51 +10003671
Damien Millerb53d8a12009-01-28 16:13:04 +11003672 printf("permitopen");
Darren Tucker22662e82008-11-11 16:40:22 +11003673 if (num_adm_permitted_opens == 0) {
Damien Millerb53d8a12009-01-28 16:13:04 +11003674 printf(" any\n");
Darren Tucker22662e82008-11-11 16:40:22 +11003675 return;
3676 }
Darren Tuckere7140f22008-06-10 23:01:51 +10003677 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Millerc6081482012-04-22 11:18:53 +10003678 if (permitted_adm_opens[i].host_to_connect == NULL)
3679 printf(" none");
3680 else
Darren Tuckere7140f22008-06-10 23:01:51 +10003681 printf(" %s:%d", permitted_adm_opens[i].host_to_connect,
3682 permitted_adm_opens[i].port_to_connect);
Damien Millerb53d8a12009-01-28 16:13:04 +11003683 printf("\n");
Darren Tuckere7140f22008-06-10 23:01:51 +10003684}
3685
Darren Tucker1338b9e2011-10-02 18:57:35 +11003686/* returns port number, FWD_PERMIT_ANY_PORT or -1 on error */
3687int
3688permitopen_port(const char *p)
3689{
3690 int port;
3691
3692 if (strcmp(p, "*") == 0)
3693 return FWD_PERMIT_ANY_PORT;
3694 if ((port = a2port(p)) > 0)
3695 return port;
3696 return -1;
3697}
3698
Damien Millerbd740252008-05-19 15:37:09 +10003699/* Try to start non-blocking connect to next host in cctx list */
Ben Lindstrombba81212001-06-25 05:01:22 +00003700static int
Damien Millerbd740252008-05-19 15:37:09 +10003701connect_next(struct channel_connect *cctx)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003702{
Damien Millerbd740252008-05-19 15:37:09 +10003703 int sock, saved_errno;
Damien Miller7acefbb2014-07-18 14:11:24 +10003704 struct sockaddr_un *sunaddr;
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +00003705 char ntop[NI_MAXHOST], strport[MAXIMUM(NI_MAXSERV,sizeof(sunaddr->sun_path))];
Damien Miller95def091999-11-25 00:26:21 +11003706
Damien Millerbd740252008-05-19 15:37:09 +10003707 for (; cctx->ai; cctx->ai = cctx->ai->ai_next) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003708 switch (cctx->ai->ai_family) {
3709 case AF_UNIX:
3710 /* unix:pathname instead of host:port */
3711 sunaddr = (struct sockaddr_un *)cctx->ai->ai_addr;
3712 strlcpy(ntop, "unix", sizeof(ntop));
3713 strlcpy(strport, sunaddr->sun_path, sizeof(strport));
3714 break;
3715 case AF_INET:
3716 case AF_INET6:
3717 if (getnameinfo(cctx->ai->ai_addr, cctx->ai->ai_addrlen,
3718 ntop, sizeof(ntop), strport, sizeof(strport),
3719 NI_NUMERICHOST|NI_NUMERICSERV) != 0) {
3720 error("connect_next: getnameinfo failed");
3721 continue;
3722 }
3723 break;
3724 default:
Damien Miller34132e52000-01-14 15:45:46 +11003725 continue;
3726 }
Darren Tucker7bd98e72010-01-10 10:31:12 +11003727 if ((sock = socket(cctx->ai->ai_family, cctx->ai->ai_socktype,
3728 cctx->ai->ai_protocol)) == -1) {
Damien Millerbd740252008-05-19 15:37:09 +10003729 if (cctx->ai->ai_next == NULL)
Damien Millerb46b9f32003-01-10 21:45:12 +11003730 error("socket: %.100s", strerror(errno));
3731 else
3732 verbose("socket: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11003733 continue;
3734 }
Damien Miller232711f2004-06-15 10:35:30 +10003735 if (set_nonblock(sock) == -1)
3736 fatal("%s: set_nonblock(%d)", __func__, sock);
Damien Millerbd740252008-05-19 15:37:09 +10003737 if (connect(sock, cctx->ai->ai_addr,
3738 cctx->ai->ai_addrlen) == -1 && errno != EINPROGRESS) {
3739 debug("connect_next: host %.100s ([%.100s]:%s): "
3740 "%.100s", cctx->host, ntop, strport,
Damien Miller34132e52000-01-14 15:45:46 +11003741 strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10003742 saved_errno = errno;
Damien Miller34132e52000-01-14 15:45:46 +11003743 close(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003744 errno = saved_errno;
Kevin Stevesef4eea92001-02-05 12:42:17 +00003745 continue; /* fail -- try next */
Damien Miller34132e52000-01-14 15:45:46 +11003746 }
Damien Miller7acefbb2014-07-18 14:11:24 +10003747 if (cctx->ai->ai_family != AF_UNIX)
3748 set_nodelay(sock);
Damien Millerbd740252008-05-19 15:37:09 +10003749 debug("connect_next: host %.100s ([%.100s]:%s) "
3750 "in progress, fd=%d", cctx->host, ntop, strport, sock);
3751 cctx->ai = cctx->ai->ai_next;
Damien Millerbd740252008-05-19 15:37:09 +10003752 return sock;
Damien Miller34132e52000-01-14 15:45:46 +11003753 }
Damien Miller0bc1bd82000-11-13 22:57:25 +11003754 return -1;
3755}
Damien Millerb38eff82000-04-01 11:09:21 +10003756
Damien Millerbd740252008-05-19 15:37:09 +10003757static void
3758channel_connect_ctx_free(struct channel_connect *cctx)
3759{
Darren Tuckera627d422013-06-02 07:31:17 +10003760 free(cctx->host);
Damien Miller7acefbb2014-07-18 14:11:24 +10003761 if (cctx->aitop) {
3762 if (cctx->aitop->ai_family == AF_UNIX)
3763 free(cctx->aitop);
3764 else
3765 freeaddrinfo(cctx->aitop);
3766 }
Damien Miller1d2c4562014-02-04 11:18:20 +11003767 memset(cctx, 0, sizeof(*cctx));
Damien Millerbd740252008-05-19 15:37:09 +10003768}
3769
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003770/*
3771 * Return CONNECTING channel to remote host:port or local socket path,
3772 * passing back the failure reason if appropriate.
3773 */
Damien Millerbd740252008-05-19 15:37:09 +10003774static Channel *
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003775connect_to_reason(const char *name, int port, char *ctype, char *rname,
3776 int *reason, const char **errmsg)
Damien Millerbd740252008-05-19 15:37:09 +10003777{
3778 struct addrinfo hints;
3779 int gaierr;
3780 int sock = -1;
3781 char strport[NI_MAXSERV];
3782 struct channel_connect cctx;
3783 Channel *c;
3784
Damien Miller2bcb8662008-07-12 17:12:29 +10003785 memset(&cctx, 0, sizeof(cctx));
Damien Miller7acefbb2014-07-18 14:11:24 +10003786
3787 if (port == PORT_STREAMLOCAL) {
3788 struct sockaddr_un *sunaddr;
3789 struct addrinfo *ai;
3790
3791 if (strlen(name) > sizeof(sunaddr->sun_path)) {
3792 error("%.100s: %.100s", name, strerror(ENAMETOOLONG));
3793 return (NULL);
3794 }
3795
3796 /*
3797 * Fake up a struct addrinfo for AF_UNIX connections.
3798 * channel_connect_ctx_free() must check ai_family
3799 * and use free() not freeaddirinfo() for AF_UNIX.
3800 */
3801 ai = xmalloc(sizeof(*ai) + sizeof(*sunaddr));
3802 memset(ai, 0, sizeof(*ai) + sizeof(*sunaddr));
3803 ai->ai_addr = (struct sockaddr *)(ai + 1);
3804 ai->ai_addrlen = sizeof(*sunaddr);
3805 ai->ai_family = AF_UNIX;
3806 ai->ai_socktype = SOCK_STREAM;
3807 ai->ai_protocol = PF_UNSPEC;
3808 sunaddr = (struct sockaddr_un *)ai->ai_addr;
3809 sunaddr->sun_family = AF_UNIX;
3810 strlcpy(sunaddr->sun_path, name, sizeof(sunaddr->sun_path));
3811 cctx.aitop = ai;
3812 } else {
3813 memset(&hints, 0, sizeof(hints));
3814 hints.ai_family = IPv4or6;
3815 hints.ai_socktype = SOCK_STREAM;
3816 snprintf(strport, sizeof strport, "%d", port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003817 if ((gaierr = getaddrinfo(name, strport, &hints, &cctx.aitop))
3818 != 0) {
3819 if (errmsg != NULL)
3820 *errmsg = ssh_gai_strerror(gaierr);
3821 if (reason != NULL)
3822 *reason = SSH2_OPEN_CONNECT_FAILED;
Damien Miller7acefbb2014-07-18 14:11:24 +10003823 error("connect_to %.100s: unknown host (%s)", name,
3824 ssh_gai_strerror(gaierr));
3825 return NULL;
3826 }
Damien Millerbd740252008-05-19 15:37:09 +10003827 }
3828
Damien Miller7acefbb2014-07-18 14:11:24 +10003829 cctx.host = xstrdup(name);
Damien Millerbd740252008-05-19 15:37:09 +10003830 cctx.port = port;
3831 cctx.ai = cctx.aitop;
3832
3833 if ((sock = connect_next(&cctx)) == -1) {
3834 error("connect to %.100s port %d failed: %s",
Damien Miller7acefbb2014-07-18 14:11:24 +10003835 name, port, strerror(errno));
Damien Millerbd740252008-05-19 15:37:09 +10003836 channel_connect_ctx_free(&cctx);
3837 return NULL;
3838 }
3839 c = channel_new(ctype, SSH_CHANNEL_CONNECTING, sock, sock, -1,
3840 CHAN_TCP_WINDOW_DEFAULT, CHAN_TCP_PACKET_DEFAULT, 0, rname, 1);
3841 c->connect_ctx = cctx;
3842 return c;
3843}
3844
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003845/* Return CONNECTING channel to remote host:port or local socket path */
3846static Channel *
3847connect_to(const char *name, int port, char *ctype, char *rname)
3848{
3849 return connect_to_reason(name, port, ctype, rname, NULL, NULL);
3850}
3851
markus@openbsd.org8d057842016-09-30 09:19:13 +00003852/*
3853 * returns either the newly connected channel or the downstream channel
3854 * that needs to deal with this connection.
3855 */
Damien Millerbd740252008-05-19 15:37:09 +10003856Channel *
Damien Miller4b3ed642014-07-02 15:29:40 +10003857channel_connect_by_listen_address(const char *listen_host,
3858 u_short listen_port, char *ctype, char *rname)
Damien Millerbd740252008-05-19 15:37:09 +10003859{
3860 int i;
3861
3862 for (i = 0; i < num_permitted_opens; i++) {
Damien Miller7acefbb2014-07-18 14:11:24 +10003863 if (open_listen_match_tcpip(&permitted_opens[i], listen_host,
Damien Miller4b3ed642014-07-02 15:29:40 +10003864 listen_port, 1)) {
markus@openbsd.org8d057842016-09-30 09:19:13 +00003865 if (permitted_opens[i].downstream)
3866 return permitted_opens[i].downstream;
Damien Millerbd740252008-05-19 15:37:09 +10003867 return connect_to(
3868 permitted_opens[i].host_to_connect,
3869 permitted_opens[i].port_to_connect, ctype, rname);
3870 }
3871 }
3872 error("WARNING: Server requests forwarding for unknown listen_port %d",
3873 listen_port);
3874 return NULL;
3875}
3876
Damien Miller7acefbb2014-07-18 14:11:24 +10003877Channel *
3878channel_connect_by_listen_path(const char *path, char *ctype, char *rname)
3879{
3880 int i;
3881
3882 for (i = 0; i < num_permitted_opens; i++) {
3883 if (open_listen_match_streamlocal(&permitted_opens[i], path)) {
3884 return connect_to(
3885 permitted_opens[i].host_to_connect,
3886 permitted_opens[i].port_to_connect, ctype, rname);
3887 }
3888 }
3889 error("WARNING: Server requests forwarding for unknown path %.100s",
3890 path);
3891 return NULL;
3892}
3893
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003894/* Check if connecting to that port is permitted and connect. */
Damien Millerbd740252008-05-19 15:37:09 +10003895Channel *
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003896channel_connect_to_port(const char *host, u_short port, char *ctype,
3897 char *rname, int *reason, const char **errmsg)
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003898{
Damien Miller9b439df2006-07-24 14:04:00 +10003899 int i, permit, permit_adm = 1;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003900
3901 permit = all_opens_permitted;
3902 if (!permit) {
3903 for (i = 0; i < num_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10003904 if (open_match(&permitted_opens[i], host, port)) {
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003905 permit = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003906 break;
3907 }
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003908 }
Damien Miller9b439df2006-07-24 14:04:00 +10003909
3910 if (num_adm_permitted_opens > 0) {
3911 permit_adm = 0;
3912 for (i = 0; i < num_adm_permitted_opens; i++)
Damien Miller4b3ed642014-07-02 15:29:40 +10003913 if (open_match(&permitted_adm_opens[i], host, port)) {
Damien Miller9b439df2006-07-24 14:04:00 +10003914 permit_adm = 1;
Damien Miller4b3ed642014-07-02 15:29:40 +10003915 break;
3916 }
Damien Miller9b439df2006-07-24 14:04:00 +10003917 }
3918
3919 if (!permit || !permit_adm) {
Damien Miller996acd22003-04-09 20:59:48 +10003920 logit("Received request to connect to host %.100s port %d, "
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003921 "but the request was denied.", host, port);
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003922 if (reason != NULL)
3923 *reason = SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED;
Damien Millerbd740252008-05-19 15:37:09 +10003924 return NULL;
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003925 }
dtucker@openbsd.org858252f2017-02-01 02:59:09 +00003926 return connect_to_reason(host, port, ctype, rname, reason, errmsg);
Ben Lindstrom7bb8b492001-03-17 00:47:54 +00003927}
3928
Damien Miller7acefbb2014-07-18 14:11:24 +10003929/* Check if connecting to that path is permitted and connect. */
3930Channel *
3931channel_connect_to_path(const char *path, char *ctype, char *rname)
3932{
3933 int i, permit, permit_adm = 1;
3934
3935 permit = all_opens_permitted;
3936 if (!permit) {
3937 for (i = 0; i < num_permitted_opens; i++)
3938 if (open_match(&permitted_opens[i], path, PORT_STREAMLOCAL)) {
3939 permit = 1;
3940 break;
3941 }
3942 }
3943
3944 if (num_adm_permitted_opens > 0) {
3945 permit_adm = 0;
3946 for (i = 0; i < num_adm_permitted_opens; i++)
3947 if (open_match(&permitted_adm_opens[i], path, PORT_STREAMLOCAL)) {
3948 permit_adm = 1;
3949 break;
3950 }
3951 }
3952
3953 if (!permit || !permit_adm) {
3954 logit("Received request to connect to path %.100s, "
3955 "but the request was denied.", path);
3956 return NULL;
3957 }
3958 return connect_to(path, PORT_STREAMLOCAL, ctype, rname);
3959}
3960
Damien Miller0e220db2004-06-15 10:34:08 +10003961void
3962channel_send_window_changes(void)
3963{
Darren Tuckerc7a6fc42004-08-13 21:18:00 +10003964 u_int i;
Damien Miller0e220db2004-06-15 10:34:08 +10003965 struct winsize ws;
3966
3967 for (i = 0; i < channels_alloc; i++) {
Darren Tucker47eede72005-03-14 23:08:12 +11003968 if (channels[i] == NULL || !channels[i]->client_tty ||
Damien Miller0e220db2004-06-15 10:34:08 +10003969 channels[i]->type != SSH_CHANNEL_OPEN)
3970 continue;
3971 if (ioctl(channels[i]->rfd, TIOCGWINSZ, &ws) < 0)
3972 continue;
3973 channel_request_start(i, "window-change", 0);
Damien Miller71a73672006-03-26 14:04:36 +11003974 packet_put_int((u_int)ws.ws_col);
3975 packet_put_int((u_int)ws.ws_row);
3976 packet_put_int((u_int)ws.ws_xpixel);
3977 packet_put_int((u_int)ws.ws_ypixel);
Damien Miller0e220db2004-06-15 10:34:08 +10003978 packet_send();
3979 }
3980}
3981
Ben Lindstrome9c99912001-06-09 00:41:05 +00003982/* -- X11 forwarding */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003983
Damien Miller5428f641999-11-25 11:54:57 +11003984/*
3985 * Creates an internet domain socket for listening for X11 connections.
Ben Lindstroma9d2c892002-06-23 21:48:28 +00003986 * Returns 0 and a suitable display number for the DISPLAY variable
3987 * stored in display_numberp , or -1 if an error occurs.
Damien Miller5428f641999-11-25 11:54:57 +11003988 */
Kevin Steves366298c2001-12-19 17:58:01 +00003989int
Damien Miller95c249f2002-02-05 12:11:34 +11003990x11_create_display_inet(int x11_display_offset, int x11_use_localhost,
Damien Miller2b9b0452005-07-17 17:19:24 +10003991 int single_connection, u_int *display_numberp, int **chanids)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003992{
Damien Millere7378562001-12-21 14:58:35 +11003993 Channel *nc = NULL;
Damien Milleraae6c611999-12-06 11:47:28 +11003994 int display_number, sock;
3995 u_short port;
Damien Miller34132e52000-01-14 15:45:46 +11003996 struct addrinfo hints, *ai, *aitop;
3997 char strport[NI_MAXSERV];
3998 int gaierr, n, num_socks = 0, socks[NUM_SOCKS];
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003999
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004000 if (chanids == NULL)
4001 return -1;
4002
Damien Millera34a28b1999-12-14 10:47:15 +11004003 for (display_number = x11_display_offset;
Damien Miller9f0f5c62001-12-21 14:45:46 +11004004 display_number < MAX_DISPLAYS;
4005 display_number++) {
Damien Miller95def091999-11-25 00:26:21 +11004006 port = 6000 + display_number;
Damien Miller34132e52000-01-14 15:45:46 +11004007 memset(&hints, 0, sizeof(hints));
4008 hints.ai_family = IPv4or6;
Damien Miller95c249f2002-02-05 12:11:34 +11004009 hints.ai_flags = x11_use_localhost ? 0: AI_PASSIVE;
Damien Miller34132e52000-01-14 15:45:46 +11004010 hints.ai_socktype = SOCK_STREAM;
4011 snprintf(strport, sizeof strport, "%d", port);
4012 if ((gaierr = getaddrinfo(NULL, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004013 error("getaddrinfo: %.100s", ssh_gai_strerror(gaierr));
Kevin Steves366298c2001-12-19 17:58:01 +00004014 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004015 }
Damien Miller34132e52000-01-14 15:45:46 +11004016 for (ai = aitop; ai; ai = ai->ai_next) {
4017 if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
4018 continue;
Darren Tucker7bd98e72010-01-10 10:31:12 +11004019 sock = socket(ai->ai_family, ai->ai_socktype,
4020 ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004021 if (sock < 0) {
Damien Miller6480c632010-03-26 11:09:44 +11004022 if ((errno != EINVAL) && (errno != EAFNOSUPPORT)
4023#ifdef EPFNOSUPPORT
4024 && (errno != EPFNOSUPPORT)
4025#endif
4026 ) {
Damien Millere2192732000-01-17 13:22:55 +11004027 error("socket: %.100s", strerror(errno));
Damien Miller3e4dffb2004-06-15 10:27:15 +10004028 freeaddrinfo(aitop);
Kevin Steves366298c2001-12-19 17:58:01 +00004029 return -1;
Damien Millere2192732000-01-17 13:22:55 +11004030 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +11004031 debug("x11_create_display_inet: Socket family %d not supported",
4032 ai->ai_family);
Damien Millere2192732000-01-17 13:22:55 +11004033 continue;
4034 }
Damien Miller34132e52000-01-14 15:45:46 +11004035 }
Damien Miller04ee0f82009-11-18 17:48:30 +11004036 if (ai->ai_family == AF_INET6)
4037 sock_set_v6only(sock);
Damien Miller4401e452008-06-12 06:05:12 +10004038 if (x11_use_localhost)
4039 channel_set_reuseaddr(sock);
Damien Miller34132e52000-01-14 15:45:46 +11004040 if (bind(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004041 debug2("bind port %d: %.100s", port, strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004042 close(sock);
Damien Miller3c7eeb22000-03-03 22:35:33 +11004043
Damien Miller34132e52000-01-14 15:45:46 +11004044 for (n = 0; n < num_socks; n++) {
Damien Miller34132e52000-01-14 15:45:46 +11004045 close(socks[n]);
4046 }
4047 num_socks = 0;
4048 break;
4049 }
4050 socks[num_socks++] = sock;
4051 if (num_socks == NUM_SOCKS)
4052 break;
Damien Miller95def091999-11-25 00:26:21 +11004053 }
Ben Lindstrom87b147f2001-01-25 00:41:12 +00004054 freeaddrinfo(aitop);
Damien Miller34132e52000-01-14 15:45:46 +11004055 if (num_socks > 0)
4056 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004057 }
Damien Miller95def091999-11-25 00:26:21 +11004058 if (display_number >= MAX_DISPLAYS) {
4059 error("Failed to allocate internet-domain X11 display socket.");
Kevin Steves366298c2001-12-19 17:58:01 +00004060 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004061 }
Damien Miller95def091999-11-25 00:26:21 +11004062 /* Start listening for connections on the socket. */
Damien Miller34132e52000-01-14 15:45:46 +11004063 for (n = 0; n < num_socks; n++) {
4064 sock = socks[n];
Darren Tucker3175eb92003-12-09 19:15:11 +11004065 if (listen(sock, SSH_LISTEN_BACKLOG) < 0) {
Damien Miller34132e52000-01-14 15:45:46 +11004066 error("listen: %.100s", strerror(errno));
Damien Miller34132e52000-01-14 15:45:46 +11004067 close(sock);
Kevin Steves366298c2001-12-19 17:58:01 +00004068 return -1;
Damien Miller34132e52000-01-14 15:45:46 +11004069 }
Damien Miller95def091999-11-25 00:26:21 +11004070 }
Damien Miller34132e52000-01-14 15:45:46 +11004071
Damien Miller34132e52000-01-14 15:45:46 +11004072 /* Allocate a channel for each socket. */
Damien Miller07d86be2006-03-26 14:19:21 +11004073 *chanids = xcalloc(num_socks + 1, sizeof(**chanids));
Damien Miller34132e52000-01-14 15:45:46 +11004074 for (n = 0; n < num_socks; n++) {
4075 sock = socks[n];
Damien Millere7378562001-12-21 14:58:35 +11004076 nc = channel_new("x11 listener",
Damien Millerbd483e72000-04-30 10:00:53 +10004077 SSH_CHANNEL_X11_LISTENER, sock, sock, -1,
4078 CHAN_X11_WINDOW_DEFAULT, CHAN_X11_PACKET_DEFAULT,
Damien Millerb1ca8bb2003-05-14 13:45:42 +10004079 0, "X11 inet listener", 1);
Damien Miller699d0032002-02-08 22:07:16 +11004080 nc->single_connection = single_connection;
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004081 (*chanids)[n] = nc->self;
Damien Miller34132e52000-01-14 15:45:46 +11004082 }
Darren Tuckerd3d0fa12005-10-03 18:03:05 +10004083 (*chanids)[n] = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004084
Kevin Steves366298c2001-12-19 17:58:01 +00004085 /* Return the display number for the DISPLAY environment variable. */
Ben Lindstroma9d2c892002-06-23 21:48:28 +00004086 *display_numberp = display_number;
4087 return (0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004088}
4089
Ben Lindstrombba81212001-06-25 05:01:22 +00004090static int
Damien Miller819dbb62009-01-21 16:46:26 +11004091connect_local_xsocket_path(const char *pathname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004092{
Damien Miller95def091999-11-25 00:26:21 +11004093 int sock;
4094 struct sockaddr_un addr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004095
Damien Miller3afe3752001-12-21 12:39:51 +11004096 sock = socket(AF_UNIX, SOCK_STREAM, 0);
4097 if (sock < 0)
4098 error("socket: %.100s", strerror(errno));
4099 memset(&addr, 0, sizeof(addr));
4100 addr.sun_family = AF_UNIX;
Damien Miller819dbb62009-01-21 16:46:26 +11004101 strlcpy(addr.sun_path, pathname, sizeof addr.sun_path);
Damien Miller90967402006-03-26 14:07:26 +11004102 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0)
Damien Miller3afe3752001-12-21 12:39:51 +11004103 return sock;
4104 close(sock);
Damien Miller95def091999-11-25 00:26:21 +11004105 error("connect %.100s: %.100s", addr.sun_path, strerror(errno));
4106 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004107}
4108
Damien Miller819dbb62009-01-21 16:46:26 +11004109static int
4110connect_local_xsocket(u_int dnr)
4111{
4112 char buf[1024];
4113 snprintf(buf, sizeof buf, _PATH_UNIX_X, dnr);
4114 return connect_local_xsocket_path(buf);
4115}
4116
Darren Tuckerda39b092017-03-10 13:22:32 +11004117#ifdef __APPLE__
4118static int
4119is_path_to_xsocket(const char *display, char *path, size_t pathlen)
4120{
4121 struct stat sbuf;
4122
4123 if (strlcpy(path, display, pathlen) >= pathlen) {
4124 error("%s: display path too long", __func__);
4125 return 0;
4126 }
4127 if (display[0] != '/')
4128 return 0;
4129 if (stat(path, &sbuf) == 0) {
4130 return 1;
4131 } else {
4132 char *dot = strrchr(path, '.');
4133 if (dot != NULL) {
4134 *dot = '\0';
4135 if (stat(path, &sbuf) == 0) {
4136 return 1;
4137 }
4138 }
4139 }
4140 return 0;
4141}
4142#endif
4143
Damien Millerbd483e72000-04-30 10:00:53 +10004144int
4145x11_connect_display(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004146{
Damien Miller57c4e872006-03-31 23:11:07 +11004147 u_int display_number;
Damien Miller95def091999-11-25 00:26:21 +11004148 const char *display;
Damien Millerbd483e72000-04-30 10:00:53 +10004149 char buf[1024], *cp;
Damien Miller34132e52000-01-14 15:45:46 +11004150 struct addrinfo hints, *ai, *aitop;
4151 char strport[NI_MAXSERV];
Damien Miller57c4e872006-03-31 23:11:07 +11004152 int gaierr, sock = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004153
Damien Miller95def091999-11-25 00:26:21 +11004154 /* Try to open a socket for the local X server. */
4155 display = getenv("DISPLAY");
4156 if (!display) {
4157 error("DISPLAY not set.");
Damien Millerbd483e72000-04-30 10:00:53 +10004158 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004159 }
Damien Miller5428f641999-11-25 11:54:57 +11004160 /*
4161 * Now we decode the value of the DISPLAY variable and make a
4162 * connection to the real X server.
4163 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004164
Damien Miller819dbb62009-01-21 16:46:26 +11004165#ifdef __APPLE__
Darren Tuckerda39b092017-03-10 13:22:32 +11004166 /* Check if display is a path to a socket (as set by launchd). */
4167 {
4168 char path[PATH_MAX];
Damien Miller819dbb62009-01-21 16:46:26 +11004169
Darren Tuckerda39b092017-03-10 13:22:32 +11004170 if (is_path_to_xsocket(display, path, sizeof(path))) {
4171 debug("x11_connect_display: $DISPLAY is launchd");
4172
4173 /* Create a socket. */
4174 sock = connect_local_xsocket_path(path);
4175 if (sock < 0)
4176 return -1;
4177
4178 /* OK, we now have a connection to the display. */
4179 return sock;
4180 }
Damien Miller819dbb62009-01-21 16:46:26 +11004181 }
4182#endif
Damien Miller5428f641999-11-25 11:54:57 +11004183 /*
4184 * Check if it is a unix domain socket. Unix domain displays are in
4185 * one of the following formats: unix:d[.s], :d[.s], ::d[.s]
4186 */
Damien Miller95def091999-11-25 00:26:21 +11004187 if (strncmp(display, "unix:", 5) == 0 ||
4188 display[0] == ':') {
4189 /* Connect to the unix domain socket. */
Damien Miller57c4e872006-03-31 23:11:07 +11004190 if (sscanf(strrchr(display, ':') + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004191 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004192 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004193 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004194 }
4195 /* Create a socket. */
4196 sock = connect_local_xsocket(display_number);
4197 if (sock < 0)
Damien Millerbd483e72000-04-30 10:00:53 +10004198 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004199
4200 /* OK, we now have a connection to the display. */
Damien Millerbd483e72000-04-30 10:00:53 +10004201 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004202 }
Damien Miller5428f641999-11-25 11:54:57 +11004203 /*
4204 * Connect to an inet socket. The DISPLAY value is supposedly
4205 * hostname:d[.s], where hostname may also be numeric IP address.
4206 */
Ben Lindstromccd8d072001-12-07 17:26:48 +00004207 strlcpy(buf, display, sizeof(buf));
Damien Miller95def091999-11-25 00:26:21 +11004208 cp = strchr(buf, ':');
4209 if (!cp) {
4210 error("Could not find ':' in DISPLAY: %.100s", display);
Damien Millerbd483e72000-04-30 10:00:53 +10004211 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004212 }
Damien Miller95def091999-11-25 00:26:21 +11004213 *cp = 0;
Damien Miller5428f641999-11-25 11:54:57 +11004214 /* buf now contains the host name. But first we parse the display number. */
Damien Miller57c4e872006-03-31 23:11:07 +11004215 if (sscanf(cp + 1, "%u", &display_number) != 1) {
Damien Miller95def091999-11-25 00:26:21 +11004216 error("Could not parse display number from DISPLAY: %.100s",
Damien Miller9f0f5c62001-12-21 14:45:46 +11004217 display);
Damien Millerbd483e72000-04-30 10:00:53 +10004218 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004219 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004220
Damien Miller34132e52000-01-14 15:45:46 +11004221 /* Look up the host address */
4222 memset(&hints, 0, sizeof(hints));
4223 hints.ai_family = IPv4or6;
4224 hints.ai_socktype = SOCK_STREAM;
Damien Miller57c4e872006-03-31 23:11:07 +11004225 snprintf(strport, sizeof strport, "%u", 6000 + display_number);
Damien Miller34132e52000-01-14 15:45:46 +11004226 if ((gaierr = getaddrinfo(buf, strport, &hints, &aitop)) != 0) {
Darren Tucker4abde772007-12-29 02:43:51 +11004227 error("%.100s: unknown host. (%s)", buf,
4228 ssh_gai_strerror(gaierr));
Damien Millerbd483e72000-04-30 10:00:53 +10004229 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004230 }
Damien Miller34132e52000-01-14 15:45:46 +11004231 for (ai = aitop; ai; ai = ai->ai_next) {
4232 /* Create a socket. */
Darren Tucker7bd98e72010-01-10 10:31:12 +11004233 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
Damien Miller34132e52000-01-14 15:45:46 +11004234 if (sock < 0) {
Damien Millerfbdeece2003-09-02 22:52:31 +10004235 debug2("socket: %.100s", strerror(errno));
Damien Millerb38eff82000-04-01 11:09:21 +10004236 continue;
4237 }
4238 /* Connect it to the display. */
4239 if (connect(sock, ai->ai_addr, ai->ai_addrlen) < 0) {
Damien Miller57c4e872006-03-31 23:11:07 +11004240 debug2("connect %.100s port %u: %.100s", buf,
Damien Millerb38eff82000-04-01 11:09:21 +10004241 6000 + display_number, strerror(errno));
4242 close(sock);
4243 continue;
4244 }
4245 /* Success */
4246 break;
Damien Miller34132e52000-01-14 15:45:46 +11004247 }
Damien Miller34132e52000-01-14 15:45:46 +11004248 freeaddrinfo(aitop);
4249 if (!ai) {
Damien Miller57c4e872006-03-31 23:11:07 +11004250 error("connect %.100s port %u: %.100s", buf, 6000 + display_number,
Damien Miller34132e52000-01-14 15:45:46 +11004251 strerror(errno));
Damien Millerbd483e72000-04-30 10:00:53 +10004252 return -1;
Damien Miller95def091999-11-25 00:26:21 +11004253 }
Damien Miller398e1cf2002-02-05 11:52:13 +11004254 set_nodelay(sock);
Damien Millerbd483e72000-04-30 10:00:53 +10004255 return sock;
4256}
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004257
Damien Millerbd483e72000-04-30 10:00:53 +10004258/*
4259 * This is called when SSH_SMSG_X11_OPEN is received. The packet contains
4260 * the remote channel number. We should do whatever we want, and respond
4261 * with either SSH_MSG_OPEN_CONFIRMATION or SSH_MSG_OPEN_FAILURE.
4262 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004263
Damien Miller8473dd82006-07-24 14:08:32 +10004264/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004265int
Damien Miller630d6f42002-01-22 23:17:30 +11004266x11_input_open(int type, u_int32_t seq, void *ctxt)
Damien Millerbd483e72000-04-30 10:00:53 +10004267{
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004268 Channel *c = NULL;
4269 int remote_id, sock = 0;
Damien Millerbd483e72000-04-30 10:00:53 +10004270 char *remote_host;
Damien Millerbd483e72000-04-30 10:00:53 +10004271
4272 debug("Received X11 open request.");
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004273
4274 remote_id = packet_get_int();
Ben Lindstrome9c99912001-06-09 00:41:05 +00004275
4276 if (packet_get_protocol_flags() & SSH_PROTOFLAG_HOST_IN_FWD_OPEN) {
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004277 remote_host = packet_get_string(NULL);
4278 } else {
4279 remote_host = xstrdup("unknown (remote did not supply name)");
4280 }
Damien Miller48b03fc2002-01-22 23:11:40 +11004281 packet_check_eom();
Damien Millerbd483e72000-04-30 10:00:53 +10004282
4283 /* Obtain a connection to the real X display. */
4284 sock = x11_connect_display();
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004285 if (sock != -1) {
4286 /* Allocate a channel for this connection. */
4287 c = channel_new("connected x11 socket",
4288 SSH_CHANNEL_X11_OPEN, sock, sock, -1, 0, 0, 0,
4289 remote_host, 1);
Damien Miller699d0032002-02-08 22:07:16 +11004290 c->remote_id = remote_id;
4291 c->force_drain = 1;
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004292 }
Darren Tuckera627d422013-06-02 07:31:17 +10004293 free(remote_host);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004294 if (c == NULL) {
Damien Millerbd483e72000-04-30 10:00:53 +10004295 /* Send refusal to the remote host. */
4296 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004297 packet_put_int(remote_id);
Damien Millerbd483e72000-04-30 10:00:53 +10004298 } else {
Damien Millerbd483e72000-04-30 10:00:53 +10004299 /* Send a confirmation to the remote host. */
4300 packet_start(SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004301 packet_put_int(remote_id);
4302 packet_put_int(c->self);
Damien Millerbd483e72000-04-30 10:00:53 +10004303 }
Ben Lindstrom99c73b32001-05-05 04:09:47 +00004304 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004305 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004306}
4307
Damien Miller69b69aa2000-10-28 14:19:58 +11004308/* dummy protocol handler that denies SSH-1 requests (agent/x11) */
Damien Miller8473dd82006-07-24 14:08:32 +10004309/* ARGSUSED */
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004310int
Damien Miller630d6f42002-01-22 23:17:30 +11004311deny_input_open(int type, u_int32_t seq, void *ctxt)
Damien Miller69b69aa2000-10-28 14:19:58 +11004312{
4313 int rchan = packet_get_int();
Ben Lindstrom8b2eecd2002-07-07 22:11:51 +00004314
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +00004315 switch (type) {
Damien Miller69b69aa2000-10-28 14:19:58 +11004316 case SSH_SMSG_AGENT_OPEN:
4317 error("Warning: ssh server tried agent forwarding.");
4318 break;
4319 case SSH_SMSG_X11_OPEN:
4320 error("Warning: ssh server tried X11 forwarding.");
4321 break;
4322 default:
Damien Miller630d6f42002-01-22 23:17:30 +11004323 error("deny_input_open: type %d", type);
Damien Miller69b69aa2000-10-28 14:19:58 +11004324 break;
4325 }
Damien Miller5eb137c2005-12-31 16:19:53 +11004326 error("Warning: this is probably a break-in attempt by a malicious server.");
Damien Miller69b69aa2000-10-28 14:19:58 +11004327 packet_start(SSH_MSG_CHANNEL_OPEN_FAILURE);
4328 packet_put_int(rchan);
4329 packet_send();
markus@openbsd.org3fdc88a2015-01-19 20:07:45 +00004330 return 0;
Damien Miller69b69aa2000-10-28 14:19:58 +11004331}
4332
Damien Miller5428f641999-11-25 11:54:57 +11004333/*
4334 * Requests forwarding of X11 connections, generates fake authentication
4335 * data, and enables authentication spoofing.
Ben Lindstrome9c99912001-06-09 00:41:05 +00004336 * This should be called in the client only.
Damien Miller5428f641999-11-25 11:54:57 +11004337 */
Damien Miller4af51302000-04-16 11:18:38 +10004338void
Damien Miller17e7ed02005-06-17 12:54:33 +10004339x11_request_forwarding_with_spoofing(int client_session_id, const char *disp,
Damien Miller6d7b4372011-06-23 08:31:57 +10004340 const char *proto, const char *data, int want_reply)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004341{
Ben Lindstrom46c16222000-12-22 01:43:59 +00004342 u_int data_len = (u_int) strlen(data) / 2;
Damien Miller13390022005-07-06 09:44:19 +10004343 u_int i, value;
Damien Miller95def091999-11-25 00:26:21 +11004344 char *new_data;
4345 int screen_number;
4346 const char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004347
Damien Millerf92c0792005-07-06 09:45:26 +10004348 if (x11_saved_display == NULL)
4349 x11_saved_display = xstrdup(disp);
4350 else if (strcmp(disp, x11_saved_display) != 0) {
Damien Miller13390022005-07-06 09:44:19 +10004351 error("x11_request_forwarding_with_spoofing: different "
4352 "$DISPLAY already forwarded");
4353 return;
4354 }
4355
Damien Millerd5fe0ba2006-08-30 11:07:39 +10004356 cp = strchr(disp, ':');
Damien Miller95def091999-11-25 00:26:21 +11004357 if (cp)
4358 cp = strchr(cp, '.');
4359 if (cp)
Damien Miller08d61502006-03-26 14:28:32 +11004360 screen_number = (u_int)strtonum(cp + 1, 0, 400, NULL);
Damien Miller95def091999-11-25 00:26:21 +11004361 else
4362 screen_number = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004363
Damien Miller13390022005-07-06 09:44:19 +10004364 if (x11_saved_proto == NULL) {
4365 /* Save protocol name. */
4366 x11_saved_proto = xstrdup(proto);
natano@openbsd.org49271082016-09-19 07:52:42 +00004367
4368 /* Extract real authentication data. */
Damien Miller13390022005-07-06 09:44:19 +10004369 x11_saved_data = xmalloc(data_len);
Damien Miller13390022005-07-06 09:44:19 +10004370 for (i = 0; i < data_len; i++) {
4371 if (sscanf(data + 2 * i, "%2x", &value) != 1)
4372 fatal("x11_request_forwarding: bad "
4373 "authentication data: %.100s", data);
Damien Miller13390022005-07-06 09:44:19 +10004374 x11_saved_data[i] = value;
Damien Miller13390022005-07-06 09:44:19 +10004375 }
4376 x11_saved_data_len = data_len;
natano@openbsd.org49271082016-09-19 07:52:42 +00004377
4378 /* Generate fake data of the same length. */
4379 x11_fake_data = xmalloc(data_len);
4380 arc4random_buf(x11_fake_data, data_len);
Damien Miller13390022005-07-06 09:44:19 +10004381 x11_fake_data_len = data_len;
Damien Miller95def091999-11-25 00:26:21 +11004382 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004383
Damien Miller95def091999-11-25 00:26:21 +11004384 /* Convert the fake data into hex. */
Damien Miller13390022005-07-06 09:44:19 +10004385 new_data = tohex(x11_fake_data, data_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004386
Damien Miller95def091999-11-25 00:26:21 +11004387 /* Send the request packet. */
djm@openbsd.org97f4d302017-04-30 23:13:25 +00004388 channel_request_start(client_session_id, "x11-req", want_reply);
4389 packet_put_char(0); /* XXX bool single connection */
Damien Millerbd483e72000-04-30 10:00:53 +10004390 packet_put_cstring(proto);
4391 packet_put_cstring(new_data);
Damien Miller95def091999-11-25 00:26:21 +11004392 packet_put_int(screen_number);
4393 packet_send();
4394 packet_write_wait();
Darren Tuckera627d422013-06-02 07:31:17 +10004395 free(new_data);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004396}
4397
Ben Lindstrome9c99912001-06-09 00:41:05 +00004398
4399/* -- agent forwarding */
4400
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004401/* Sends a message to the server to request authentication fd forwarding. */
4402
Damien Miller4af51302000-04-16 11:18:38 +10004403void
Ben Lindstrom3c36bb22001-12-06 17:55:26 +00004404auth_request_forwarding(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004405{
Damien Miller95def091999-11-25 00:26:21 +11004406 packet_start(SSH_CMSG_AGENT_REQUEST_FORWARDING);
4407 packet_send();
4408 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +10004409}